blob: 5ba80f2fedbe58f84c2a803130b8bd3999427a9f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaare0ad3652015-01-27 12:59:55 +0100247/* re_flags passed to nfa_regcomp() */
248static int nfa_re_flags;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200251static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaar428e9872013-05-30 17:05:39 +0200253/* NFA regexp \1 .. \9 encountered. */
254static int nfa_has_backref;
255
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257/* NFA regexp has \z( ), set zsubexpr. */
258static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200259#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200260
Bram Moolenaar963fee22013-05-26 21:47:28 +0200261/* Number of sub expressions actually being used during execution. 1 if only
262 * the whole match (subexpr 0) is used. */
263static int nfa_nsubexpr;
264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265static int *post_start; /* holds the postfix form of r.e. */
266static int *post_end;
267static int *post_ptr;
268
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200269static int nstate; /* Number of states in the NFA. Also used when
270 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200271static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar307aa162013-06-02 16:34:21 +0200273/* If not NULL match must end at this position */
274static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200276/* listid is global, so that it increases on recursive calls to
277 * nfa_regmatch(), which means we don't have to clear the lastlist field of
278 * all the states. */
279static int nfa_listid;
280static int nfa_alt_listid;
281
282/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
283static int nfa_ll_index = 0;
284
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100285static int nfa_regcomp_start(char_u *expr, int re_flags);
286static int nfa_get_reganch(nfa_state_T *start, int depth);
287static int nfa_get_regstart(nfa_state_T *start, int depth);
288static char_u *nfa_get_match_text(nfa_state_T *start);
289static int realloc_post_list(void);
290static int nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl);
291static int nfa_emit_equi_class(int c);
292static int nfa_regatom(void);
293static int nfa_regpiece(void);
294static int nfa_regconcat(void);
295static int nfa_regbranch(void);
296static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100298static void nfa_set_code(int c);
299static void nfa_postfix_dump(char_u *expr, int retval);
300static void nfa_print_state(FILE *debugf, nfa_state_T *state);
301static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
302static void nfa_dump(nfa_regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100304static int *re2post(void);
305static nfa_state_T *alloc_state(int c, nfa_state_T *out, nfa_state_T *out1);
306static void st_error(int *postfix, int *end, int *p);
307static int nfa_max_width(nfa_state_T *startstate, int depth);
308static nfa_state_T *post2nfa(int *postfix, int *end, int nfa_calc_size);
309static void nfa_postprocess(nfa_regprog_T *prog);
310static int check_char_class(int class, int c);
311static void nfa_save_listids(nfa_regprog_T *prog, int *list);
312static void nfa_restore_listids(nfa_regprog_T *prog, int *list);
313static int nfa_re_num_cmp(long_u val, int op, long_u pos);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200314static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm, int *timed_out);
315static long nfa_regexec_both(char_u *line, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100316static regprog_T *nfa_regcomp(char_u *expr, int re_flags);
317static void nfa_regfree(regprog_T *prog);
318static int nfa_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200319static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100320static int match_follows(nfa_state_T *startstate, int depth);
321static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322
323/* helper functions used when doing re2post() ... regatom() parsing */
324#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200325 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326 return FAIL; \
327 *post_ptr++ = c; \
328 } while (0)
329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330/*
331 * Initialize internal variables before NFA compilation.
332 * Return OK on success, FAIL otherwise.
333 */
334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335nfa_regcomp_start(
336 char_u *expr,
337 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200339 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200340 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341
342 nstate = 0;
343 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200344 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200345 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200348 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200349 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200350
351 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200352 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354 post_start = (int *)lalloc(postfix_size, TRUE);
355 if (post_start == NULL)
356 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200358 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200360 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200361
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200362 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200363 regcomp_start(expr, re_flags);
364
365 return OK;
366}
367
368/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200369 * Figure out if the NFA state list starts with an anchor, must match at start
370 * of the line.
371 */
372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100373nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200374{
375 nfa_state_T *p = start;
376
377 if (depth > 4)
378 return 0;
379
380 while (p != NULL)
381 {
382 switch (p->c)
383 {
384 case NFA_BOL:
385 case NFA_BOF:
386 return 1; /* yes! */
387
388 case NFA_ZSTART:
389 case NFA_ZEND:
390 case NFA_CURSOR:
391 case NFA_VISUAL:
392
393 case NFA_MOPEN:
394 case NFA_MOPEN1:
395 case NFA_MOPEN2:
396 case NFA_MOPEN3:
397 case NFA_MOPEN4:
398 case NFA_MOPEN5:
399 case NFA_MOPEN6:
400 case NFA_MOPEN7:
401 case NFA_MOPEN8:
402 case NFA_MOPEN9:
403 case NFA_NOPEN:
404#ifdef FEAT_SYN_HL
405 case NFA_ZOPEN:
406 case NFA_ZOPEN1:
407 case NFA_ZOPEN2:
408 case NFA_ZOPEN3:
409 case NFA_ZOPEN4:
410 case NFA_ZOPEN5:
411 case NFA_ZOPEN6:
412 case NFA_ZOPEN7:
413 case NFA_ZOPEN8:
414 case NFA_ZOPEN9:
415#endif
416 p = p->out;
417 break;
418
419 case NFA_SPLIT:
420 return nfa_get_reganch(p->out, depth + 1)
421 && nfa_get_reganch(p->out1, depth + 1);
422
423 default:
424 return 0; /* noooo */
425 }
426 }
427 return 0;
428}
429
430/*
431 * Figure out if the NFA state list starts with a character which must match
432 * at start of the match.
433 */
434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100435nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200436{
437 nfa_state_T *p = start;
438
439 if (depth > 4)
440 return 0;
441
442 while (p != NULL)
443 {
444 switch (p->c)
445 {
446 /* all kinds of zero-width matches */
447 case NFA_BOL:
448 case NFA_BOF:
449 case NFA_BOW:
450 case NFA_EOW:
451 case NFA_ZSTART:
452 case NFA_ZEND:
453 case NFA_CURSOR:
454 case NFA_VISUAL:
455 case NFA_LNUM:
456 case NFA_LNUM_GT:
457 case NFA_LNUM_LT:
458 case NFA_COL:
459 case NFA_COL_GT:
460 case NFA_COL_LT:
461 case NFA_VCOL:
462 case NFA_VCOL_GT:
463 case NFA_VCOL_LT:
464 case NFA_MARK:
465 case NFA_MARK_GT:
466 case NFA_MARK_LT:
467
468 case NFA_MOPEN:
469 case NFA_MOPEN1:
470 case NFA_MOPEN2:
471 case NFA_MOPEN3:
472 case NFA_MOPEN4:
473 case NFA_MOPEN5:
474 case NFA_MOPEN6:
475 case NFA_MOPEN7:
476 case NFA_MOPEN8:
477 case NFA_MOPEN9:
478 case NFA_NOPEN:
479#ifdef FEAT_SYN_HL
480 case NFA_ZOPEN:
481 case NFA_ZOPEN1:
482 case NFA_ZOPEN2:
483 case NFA_ZOPEN3:
484 case NFA_ZOPEN4:
485 case NFA_ZOPEN5:
486 case NFA_ZOPEN6:
487 case NFA_ZOPEN7:
488 case NFA_ZOPEN8:
489 case NFA_ZOPEN9:
490#endif
491 p = p->out;
492 break;
493
494 case NFA_SPLIT:
495 {
496 int c1 = nfa_get_regstart(p->out, depth + 1);
497 int c2 = nfa_get_regstart(p->out1, depth + 1);
498
499 if (c1 == c2)
500 return c1; /* yes! */
501 return 0;
502 }
503
504 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200505 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200506 return p->c; /* yes! */
507 return 0;
508 }
509 }
510 return 0;
511}
512
513/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200514 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200515 * else. If so return a string in allocated memory with what must match after
516 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200517 */
518 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100519nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200520{
521 nfa_state_T *p = start;
522 int len = 0;
523 char_u *ret;
524 char_u *s;
525
526 if (p->c != NFA_MOPEN)
527 return NULL; /* just in case */
528 p = p->out;
529 while (p->c > 0)
530 {
531 len += MB_CHAR2LEN(p->c);
532 p = p->out;
533 }
534 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
535 return NULL;
536
537 ret = alloc(len);
538 if (ret != NULL)
539 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200540 p = start->out->out; /* skip first char, it goes into regstart */
541 s = ret;
542 while (p->c > 0)
543 {
544#ifdef FEAT_MBYTE
545 if (has_mbyte)
546 s += (*mb_char2bytes)(p->c, s);
547 else
548#endif
549 *s++ = p->c;
550 p = p->out;
551 }
552 *s = NUL;
553 }
554 return ret;
555}
556
557/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 * Allocate more space for post_start. Called when
559 * running above the estimated number of states.
560 */
561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100562realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200563{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200564 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200565 int new_max = nstate_max + 1000;
566 int *new_start;
567 int *old_start;
568
569 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
570 if (new_start == NULL)
571 return FAIL;
572 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200573 old_start = post_start;
574 post_start = new_start;
575 post_ptr = new_start + (post_ptr - old_start);
576 post_end = post_start + new_max;
577 vim_free(old_start);
578 return OK;
579}
580
581/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 * Search between "start" and "end" and try to recognize a
583 * character class in expanded form. For example [0-9].
584 * On success, return the id the character class to be emitted.
585 * On failure, return 0 (=FAIL)
586 * Start points to the first char of the range, while end should point
587 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200588 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
589 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200590 */
591 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100592nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200594# define CLASS_not 0x80
595# define CLASS_af 0x40
596# define CLASS_AF 0x20
597# define CLASS_az 0x10
598# define CLASS_AZ 0x08
599# define CLASS_o7 0x04
600# define CLASS_o9 0x02
601# define CLASS_underscore 0x01
602
603 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200605 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200606
607 if (extra_newl == TRUE)
608 newl = TRUE;
609
610 if (*end != ']')
611 return FAIL;
612 p = start;
613 if (*p == '^')
614 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200615 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200616 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617 }
618
619 while (p < end)
620 {
621 if (p + 2 < end && *(p + 1) == '-')
622 {
623 switch (*p)
624 {
625 case '0':
626 if (*(p + 2) == '9')
627 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200628 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 break;
630 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200631 if (*(p + 2) == '7')
632 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200633 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 break;
635 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200636 return FAIL;
637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 case 'a':
639 if (*(p + 2) == 'z')
640 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200641 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200642 break;
643 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 if (*(p + 2) == 'f')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200649 return FAIL;
650
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200651 case 'A':
652 if (*(p + 2) == 'Z')
653 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200654 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200655 break;
656 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200657 if (*(p + 2) == 'F')
658 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200659 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660 break;
661 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200662 return FAIL;
663
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200664 default:
665 return FAIL;
666 }
667 p += 3;
668 }
669 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
670 {
671 newl = TRUE;
672 p += 2;
673 }
674 else if (*p == '_')
675 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677 p ++;
678 }
679 else if (*p == '\n')
680 {
681 newl = TRUE;
682 p ++;
683 }
684 else
685 return FAIL;
686 } /* while (p < end) */
687
688 if (p != end)
689 return FAIL;
690
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200691 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200692 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200693
694 switch (config)
695 {
696 case CLASS_o9:
697 return extra_newl + NFA_DIGIT;
698 case CLASS_not | CLASS_o9:
699 return extra_newl + NFA_NDIGIT;
700 case CLASS_af | CLASS_AF | CLASS_o9:
701 return extra_newl + NFA_HEX;
702 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
703 return extra_newl + NFA_NHEX;
704 case CLASS_o7:
705 return extra_newl + NFA_OCTAL;
706 case CLASS_not | CLASS_o7:
707 return extra_newl + NFA_NOCTAL;
708 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
709 return extra_newl + NFA_WORD;
710 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
711 return extra_newl + NFA_NWORD;
712 case CLASS_az | CLASS_AZ | CLASS_underscore:
713 return extra_newl + NFA_HEAD;
714 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
715 return extra_newl + NFA_NHEAD;
716 case CLASS_az | CLASS_AZ:
717 return extra_newl + NFA_ALPHA;
718 case CLASS_not | CLASS_az | CLASS_AZ:
719 return extra_newl + NFA_NALPHA;
720 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200721 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200722 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200723 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200724 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200725 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200726 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200727 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200729 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730}
731
732/*
733 * Produce the bytes for equivalence class "c".
734 * Currently only handles latin1, latin9 and utf-8.
735 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
736 * equivalent to 'a OR b OR c'
737 *
738 * NOTE! When changing this function, also update reg_equi_class()
739 */
740 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100741nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200742{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200743#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
744#ifdef FEAT_MBYTE
745# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
746#else
747# define EMITMBC(c)
748#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749
750#ifdef FEAT_MBYTE
751 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
752 || STRCMP(p_enc, "iso-8859-15") == 0)
753#endif
754 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200755#ifdef EBCDIC
756# define A_circumflex 0x62
757# define A_diaeresis 0x63
758# define A_grave 0x64
759# define A_acute 0x65
760# define A_virguilla 0x66
761# define A_ring 0x67
762# define C_cedilla 0x68
763# define E_acute 0x71
764# define E_circumflex 0x72
765# define E_diaeresis 0x73
766# define E_grave 0x74
767# define I_acute 0x75
768# define I_circumflex 0x76
769# define I_diaeresis 0x77
770# define I_grave 0x78
771# define N_virguilla 0x69
772# define O_circumflex 0xeb
773# define O_diaeresis 0xec
774# define O_grave 0xed
775# define O_acute 0xee
776# define O_virguilla 0xef
777# define O_slash 0x80
778# define U_circumflex 0xfb
779# define U_diaeresis 0xfc
780# define U_grave 0xfd
781# define U_acute 0xfe
782# define Y_acute 0xba
783# define a_grave 0x42
784# define a_acute 0x43
785# define a_circumflex 0x44
786# define a_virguilla 0x45
787# define a_diaeresis 0x46
788# define a_ring 0x47
789# define c_cedilla 0x48
790# define e_grave 0x51
791# define e_acute 0x52
792# define e_circumflex 0x53
793# define e_diaeresis 0x54
794# define i_grave 0x55
795# define i_acute 0x56
796# define i_circumflex 0x57
797# define i_diaeresis 0x58
798# define n_virguilla 0x49
799# define o_grave 0xcb
800# define o_acute 0xcc
801# define o_circumflex 0xcd
802# define o_virguilla 0xce
803# define o_diaeresis 0xcf
804# define o_slash 0x70
805# define u_grave 0xdb
806# define u_acute 0xdc
807# define u_circumflex 0xdd
808# define u_diaeresis 0xde
809# define y_acute 0x8d
810# define y_diaeresis 0xdf
811#else
812# define A_grave 0xc0
813# define A_acute 0xc1
814# define A_circumflex 0xc2
815# define A_virguilla 0xc3
816# define A_diaeresis 0xc4
817# define A_ring 0xc5
818# define C_cedilla 0xc7
819# define E_grave 0xc8
820# define E_acute 0xc9
821# define E_circumflex 0xca
822# define E_diaeresis 0xcb
823# define I_grave 0xcc
824# define I_acute 0xcd
825# define I_circumflex 0xce
826# define I_diaeresis 0xcf
827# define N_virguilla 0xd1
828# define O_grave 0xd2
829# define O_acute 0xd3
830# define O_circumflex 0xd4
831# define O_virguilla 0xd5
832# define O_diaeresis 0xd6
833# define O_slash 0xd8
834# define U_grave 0xd9
835# define U_acute 0xda
836# define U_circumflex 0xdb
837# define U_diaeresis 0xdc
838# define Y_acute 0xdd
839# define a_grave 0xe0
840# define a_acute 0xe1
841# define a_circumflex 0xe2
842# define a_virguilla 0xe3
843# define a_diaeresis 0xe4
844# define a_ring 0xe5
845# define c_cedilla 0xe7
846# define e_grave 0xe8
847# define e_acute 0xe9
848# define e_circumflex 0xea
849# define e_diaeresis 0xeb
850# define i_grave 0xec
851# define i_acute 0xed
852# define i_circumflex 0xee
853# define i_diaeresis 0xef
854# define n_virguilla 0xf1
855# define o_grave 0xf2
856# define o_acute 0xf3
857# define o_circumflex 0xf4
858# define o_virguilla 0xf5
859# define o_diaeresis 0xf6
860# define o_slash 0xf8
861# define u_grave 0xf9
862# define u_acute 0xfa
863# define u_circumflex 0xfb
864# define u_diaeresis 0xfc
865# define y_acute 0xfd
866# define y_diaeresis 0xff
867#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 switch (c)
869 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 case 'A': case A_grave: case A_acute: case A_circumflex:
871 case A_virguilla: case A_diaeresis: case A_ring:
872 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
873 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
874 CASEMBC(0x1ea2)
875 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
876 EMIT2(A_circumflex); EMIT2(A_virguilla);
877 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200878 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
879 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
880 EMITMBC(0x1ea2)
881 return OK;
882
883 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
884 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200885 return OK;
886
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200887 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
888 CASEMBC(0x10a) CASEMBC(0x10c)
889 EMIT2('C'); EMIT2(C_cedilla);
890 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200891 EMITMBC(0x10a) EMITMBC(0x10c)
892 return OK;
893
894 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200895 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200896 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
897 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200898 return OK;
899
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200900 case 'E': case E_grave: case E_acute: case E_circumflex:
901 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
902 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
903 CASEMBC(0x1eba) CASEMBC(0x1ebc)
904 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
905 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200906 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
907 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
908 EMITMBC(0x1ebc)
909 return OK;
910
911 case 'F': CASEMBC(0x1e1e)
912 EMIT2('F'); EMITMBC(0x1e1e)
913 return OK;
914
915 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200916 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
917 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200918 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
919 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
920 EMITMBC(0x1f4) EMITMBC(0x1e20)
921 return OK;
922
923 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200924 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200925 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
926 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200927 return OK;
928
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 case 'I': case I_grave: case I_acute: case I_circumflex:
930 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
931 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
932 CASEMBC(0x1cf) CASEMBC(0x1ec8)
933 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
934 EMIT2(I_circumflex); EMIT2(I_diaeresis);
935 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
937 EMITMBC(0x1cf) EMITMBC(0x1ec8)
938 return OK;
939
940 case 'J': CASEMBC(0x134)
941 EMIT2('J'); EMITMBC(0x134)
942 return OK;
943
944 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200945 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200946 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
947 EMITMBC(0x1e34)
948 return OK;
949
950 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200951 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200952 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
953 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
954 return OK;
955
956 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
957 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200958 return OK;
959
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200960 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
961 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
962 EMIT2('N'); EMIT2(N_virguilla);
963 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200964 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 return OK;
966
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200967 case 'O': case O_grave: case O_acute: case O_circumflex:
968 case O_virguilla: case O_diaeresis: case O_slash:
969 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
970 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
971 CASEMBC(0x1ec) CASEMBC(0x1ece)
972 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
973 EMIT2(O_circumflex); EMIT2(O_virguilla);
974 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200975 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
976 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
977 EMITMBC(0x1ec) EMITMBC(0x1ece)
978 return OK;
979
980 case 'P': case 0x1e54: case 0x1e56:
981 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
982 return OK;
983
984 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200985 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200986 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
987 EMITMBC(0x1e58) EMITMBC(0x1e5e)
988 return OK;
989
990 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200991 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200992 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
993 EMITMBC(0x160) EMITMBC(0x1e60)
994 return OK;
995
996 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200997 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200998 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
999 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return OK;
1001
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001002 case 'U': case U_grave: case U_acute: case U_diaeresis:
1003 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
1004 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
1005 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
1006 CASEMBC(0x1ee6)
1007 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
1008 EMIT2(U_diaeresis); EMIT2(U_circumflex);
1009 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
1011 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
1012 EMITMBC(0x1ee6)
1013 return OK;
1014
1015 case 'V': CASEMBC(0x1e7c)
1016 EMIT2('V'); EMITMBC(0x1e7c)
1017 return OK;
1018
1019 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001020 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001021 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
1022 EMITMBC(0x1e84) EMITMBC(0x1e86)
1023 return OK;
1024
1025 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
1026 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001027 return OK;
1028
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001029 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
1030 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
1031 CASEMBC(0x1ef8)
1032 EMIT2('Y'); EMIT2(Y_acute);
1033 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001034 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
1035 EMITMBC(0x1ef8)
1036 return OK;
1037
1038 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001039 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001040 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
1041 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 case 'a': case a_grave: case a_acute: case a_circumflex:
1045 case a_virguilla: case a_diaeresis: case a_ring:
1046 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
1047 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1048 CASEMBC(0x1ea3)
1049 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1050 EMIT2(a_circumflex); EMIT2(a_virguilla);
1051 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001052 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1053 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1054 EMITMBC(0x1ea3)
1055 return OK;
1056
1057 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1058 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001059 return OK;
1060
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001061 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1062 CASEMBC(0x10b) CASEMBC(0x10d)
1063 EMIT2('c'); EMIT2(c_cedilla);
1064 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001065 EMITMBC(0x10b) EMITMBC(0x10d)
1066 return OK;
1067
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001068 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001069 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001070 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1071 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001072 return OK;
1073
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001074 case 'e': case e_grave: case e_acute: case e_circumflex:
1075 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1076 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1077 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1078 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1079 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1080 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001081 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1082 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1083 return OK;
1084
1085 case 'f': CASEMBC(0x1e1f)
1086 EMIT2('f'); EMITMBC(0x1e1f)
1087 return OK;
1088
1089 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001090 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1091 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1093 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1094 EMITMBC(0x1f5) EMITMBC(0x1e21)
1095 return OK;
1096
1097 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001098 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001099 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1100 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001101 return OK;
1102
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001103 case 'i': case i_grave: case i_acute: case i_circumflex:
1104 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1105 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1106 CASEMBC(0x1ec9)
1107 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1108 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1109 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001110 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1111 EMITMBC(0x1ec9)
1112 return OK;
1113
1114 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1115 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1116 return OK;
1117
1118 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001120 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1121 EMITMBC(0x1e35)
1122 return OK;
1123
1124 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001125 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001126 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1127 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1128 return OK;
1129
1130 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1131 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 return OK;
1133
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001134 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1135 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1136 CASEMBC(0x1e49)
1137 EMIT2('n'); EMIT2(n_virguilla);
1138 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001139 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1140 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 return OK;
1142
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001143 case 'o': case o_grave: case o_acute: case o_circumflex:
1144 case o_virguilla: case o_diaeresis: case o_slash:
1145 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1146 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1147 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1148 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1149 EMIT2(o_circumflex); EMIT2(o_virguilla);
1150 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001151 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1152 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1153 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1154 return OK;
1155
1156 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1157 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1158 return OK;
1159
1160 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001161 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001162 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1163 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1164 return OK;
1165
1166 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001167 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1169 EMITMBC(0x161) EMITMBC(0x1e61)
1170 return OK;
1171
1172 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001173 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001174 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1175 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001176 return OK;
1177
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001178 case 'u': case u_grave: case u_acute: case u_circumflex:
1179 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1180 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1181 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1182 CASEMBC(0x1ee7)
1183 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1184 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1185 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001186 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1187 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1188 EMITMBC(0x1ee7)
1189 return OK;
1190
1191 case 'v': CASEMBC(0x1e7d)
1192 EMIT2('v'); EMITMBC(0x1e7d)
1193 return OK;
1194
1195 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001196 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001197 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1198 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1199 return OK;
1200
1201 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1202 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 return OK;
1204
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001205 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1206 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1207 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1208 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1209 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001210 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1211 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 return OK;
1213
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001214 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001215 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001216 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1217 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1218 return OK;
1219
1220 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 }
1222 }
1223
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001224 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 return OK;
1226#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001227#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228}
1229
1230/*
1231 * Code to parse regular expression.
1232 *
1233 * We try to reuse parsing functions in regexp.c to
1234 * minimize surprise and keep the syntax consistent.
1235 */
1236
1237/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 * Parse the lowest level.
1239 *
1240 * An atom can be one of a long list of items. Many atoms match one character
1241 * in the text. It is often an ordinary character or a character class.
1242 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1243 * is only for syntax highlighting.
1244 *
1245 * atom ::= ordinary-atom
1246 * or \( pattern \)
1247 * or \%( pattern \)
1248 * or \z( pattern \)
1249 */
1250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001251nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001252{
1253 int c;
1254 int charclass;
1255 int equiclass;
1256 int collclass;
1257 int got_coll_char;
1258 char_u *p;
1259 char_u *endp;
1260#ifdef FEAT_MBYTE
1261 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262#endif
1263 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 int emit_range;
1265 int negated;
1266 int result;
1267 int startc = -1;
1268 int endc = -1;
1269 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001270 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001272 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 switch (c)
1274 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001275 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001276 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001277
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001278 case Magic('^'):
1279 EMIT(NFA_BOL);
1280 break;
1281
1282 case Magic('$'):
1283 EMIT(NFA_EOL);
1284#if defined(FEAT_SYN_HL) || defined(PROTO)
1285 had_eol = TRUE;
1286#endif
1287 break;
1288
1289 case Magic('<'):
1290 EMIT(NFA_BOW);
1291 break;
1292
1293 case Magic('>'):
1294 EMIT(NFA_EOW);
1295 break;
1296
1297 case Magic('_'):
1298 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001299 if (c == NUL)
1300 EMSG_RET_FAIL(_(e_nul_found));
1301
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001302 if (c == '^') /* "\_^" is start-of-line */
1303 {
1304 EMIT(NFA_BOL);
1305 break;
1306 }
1307 if (c == '$') /* "\_$" is end-of-line */
1308 {
1309 EMIT(NFA_EOL);
1310#if defined(FEAT_SYN_HL) || defined(PROTO)
1311 had_eol = TRUE;
1312#endif
1313 break;
1314 }
1315
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001316 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317
1318 /* "\_[" is collection plus newline */
1319 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001320 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321
1322 /* "\_x" is character class plus newline */
1323 /*FALLTHROUGH*/
1324
1325 /*
1326 * Character classes.
1327 */
1328 case Magic('.'):
1329 case Magic('i'):
1330 case Magic('I'):
1331 case Magic('k'):
1332 case Magic('K'):
1333 case Magic('f'):
1334 case Magic('F'):
1335 case Magic('p'):
1336 case Magic('P'):
1337 case Magic('s'):
1338 case Magic('S'):
1339 case Magic('d'):
1340 case Magic('D'):
1341 case Magic('x'):
1342 case Magic('X'):
1343 case Magic('o'):
1344 case Magic('O'):
1345 case Magic('w'):
1346 case Magic('W'):
1347 case Magic('h'):
1348 case Magic('H'):
1349 case Magic('a'):
1350 case Magic('A'):
1351 case Magic('l'):
1352 case Magic('L'):
1353 case Magic('u'):
1354 case Magic('U'):
1355 p = vim_strchr(classchars, no_Magic(c));
1356 if (p == NULL)
1357 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001358 if (extra == NFA_ADD_NL)
1359 {
1360 EMSGN(_(e_ill_char_class), c);
1361 rc_did_emsg = TRUE;
1362 return FAIL;
1363 }
Bram Moolenaarde330112017-01-08 20:50:52 +01001364 IEMSGN("INTERNAL: Unknown character class char: %ld", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001365 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 }
1367#ifdef FEAT_MBYTE
1368 /* When '.' is followed by a composing char ignore the dot, so that
1369 * the composing char is matched here. */
1370 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1371 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001372 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001373 c = getchr();
1374 goto nfa_do_multibyte;
1375 }
1376#endif
1377 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001378 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001379 {
1380 EMIT(NFA_NEWL);
1381 EMIT(NFA_OR);
1382 regflags |= RF_HASNL;
1383 }
1384 break;
1385
1386 case Magic('n'):
1387 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001388 /* In a string "\n" matches a newline character. */
1389 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 else
1391 {
1392 /* In buffer text "\n" matches the end of a line. */
1393 EMIT(NFA_NEWL);
1394 regflags |= RF_HASNL;
1395 }
1396 break;
1397
1398 case Magic('('):
1399 if (nfa_reg(REG_PAREN) == FAIL)
1400 return FAIL; /* cascaded error */
1401 break;
1402
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 case Magic('|'):
1404 case Magic('&'):
1405 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001406 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 return FAIL;
1408
1409 case Magic('='):
1410 case Magic('?'):
1411 case Magic('+'):
1412 case Magic('@'):
1413 case Magic('*'):
1414 case Magic('{'):
1415 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001416 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 return FAIL;
1418
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001419 case Magic('~'):
1420 {
1421 char_u *lp;
1422
1423 /* Previous substitute pattern.
1424 * Generated as "\%(pattern\)". */
1425 if (reg_prev_sub == NULL)
1426 {
1427 EMSG(_(e_nopresub));
1428 return FAIL;
1429 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001430 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001431 {
1432 EMIT(PTR2CHAR(lp));
1433 if (lp != reg_prev_sub)
1434 EMIT(NFA_CONCAT);
1435 }
1436 EMIT(NFA_NOPEN);
1437 break;
1438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439
Bram Moolenaar428e9872013-05-30 17:05:39 +02001440 case Magic('1'):
1441 case Magic('2'):
1442 case Magic('3'):
1443 case Magic('4'):
1444 case Magic('5'):
1445 case Magic('6'):
1446 case Magic('7'):
1447 case Magic('8'):
1448 case Magic('9'):
1449 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1450 nfa_has_backref = TRUE;
1451 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452
1453 case Magic('z'):
1454 c = no_Magic(getchr());
1455 switch (c)
1456 {
1457 case 's':
1458 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001459 if (re_mult_next("\\zs") == FAIL)
1460 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461 break;
1462 case 'e':
1463 EMIT(NFA_ZEND);
1464 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001465 if (re_mult_next("\\ze") == FAIL)
1466 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001467 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001468#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001469 case '1':
1470 case '2':
1471 case '3':
1472 case '4':
1473 case '5':
1474 case '6':
1475 case '7':
1476 case '8':
1477 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001478 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001479 if (reg_do_extmatch != REX_USE)
1480 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001481 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1482 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001483 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001484 re_has_z = REX_USE;
1485 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001487 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001488 if (reg_do_extmatch != REX_SET)
1489 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001490 if (nfa_reg(REG_ZPAREN) == FAIL)
1491 return FAIL; /* cascaded error */
1492 re_has_z = REX_SET;
1493 break;
1494#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001496 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 no_Magic(c));
1498 return FAIL;
1499 }
1500 break;
1501
1502 case Magic('%'):
1503 c = no_Magic(getchr());
1504 switch (c)
1505 {
1506 /* () without a back reference */
1507 case '(':
1508 if (nfa_reg(REG_NPAREN) == FAIL)
1509 return FAIL;
1510 EMIT(NFA_NOPEN);
1511 break;
1512
1513 case 'd': /* %d123 decimal */
1514 case 'o': /* %o123 octal */
1515 case 'x': /* %xab hex 2 */
1516 case 'u': /* %uabcd hex 4 */
1517 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001518 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001519 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520
Bram Moolenaar47196582013-05-25 22:04:23 +02001521 switch (c)
1522 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001523 case 'd': nr = getdecchrs(); break;
1524 case 'o': nr = getoctchrs(); break;
1525 case 'x': nr = gethexchrs(2); break;
1526 case 'u': nr = gethexchrs(4); break;
1527 case 'U': nr = gethexchrs(8); break;
1528 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001529 }
1530
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001531 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001532 EMSG2_RET_FAIL(
1533 _("E678: Invalid character after %s%%[dxouU]"),
1534 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001535 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001536 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001537 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001538 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 break;
1540
1541 /* Catch \%^ and \%$ regardless of where they appear in the
1542 * pattern -- regardless of whether or not it makes sense. */
1543 case '^':
1544 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 break;
1546
1547 case '$':
1548 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549 break;
1550
1551 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001552 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 break;
1554
1555 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001556 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001557 break;
1558
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001559 case 'C':
1560 EMIT(NFA_ANY_COMPOSING);
1561 break;
1562
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001564 {
1565 int n;
1566
1567 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001568 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001569 {
1570 if (c == NUL)
1571 EMSG2_RET_FAIL(_(e_missing_sb),
1572 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001573 /* recursive call! */
1574 if (nfa_regatom() == FAIL)
1575 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001576 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001577 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001578 if (n == 0)
1579 EMSG2_RET_FAIL(_(e_empty_sb),
1580 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001581 EMIT(NFA_OPT_CHARS);
1582 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001583
1584 /* Emit as "\%(\%[abc]\)" to be able to handle
1585 * "\%[abc]*" which would cause the empty string to be
1586 * matched an unlimited number of times. NFA_NOPEN is
1587 * added only once at a position, while NFA_SPLIT is
1588 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001589 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001590 * a lot. */
1591 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001592 break;
1593 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001594
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001595 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001596 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001597 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001598 int cmp = c;
1599
1600 if (c == '<' || c == '>')
1601 c = getchr();
1602 while (VIM_ISDIGIT(c))
1603 {
1604 n = n * 10 + (c - '0');
1605 c = getchr();
1606 }
1607 if (c == 'l' || c == 'c' || c == 'v')
1608 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001609 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001610 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001611 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001612 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001613 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001614 if (save_prev_at_start)
1615 at_start = TRUE;
1616 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001617 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001618 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001619 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001620 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001621 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001622 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001623 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001624 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001625 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001626 break;
1627 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001628 else if (c == '\'' && n == 0)
1629 {
1630 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001631 EMIT(cmp == '<' ? NFA_MARK_LT :
1632 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001633 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001634 break;
1635 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001636 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001637 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1638 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 return FAIL;
1640 }
1641 break;
1642
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001643 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001644collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001646 * [abc] uses NFA_START_COLL - NFA_END_COLL
1647 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1648 * Each character is produced as a regular state, using
1649 * NFA_CONCAT to bind them together.
1650 * Besides normal characters there can be:
1651 * - character classes NFA_CLASS_*
1652 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 */
1654
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001655 p = regparse;
1656 endp = skip_anyof(p);
1657 if (*endp == ']')
1658 {
1659 /*
1660 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001661 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001662 * and perform the necessary substitutions in the NFA.
1663 */
1664 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001665 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 if (result != FAIL)
1667 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001668 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001670 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001671 EMIT(NFA_NEWL);
1672 EMIT(NFA_OR);
1673 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001674 else
1675 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001677 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 return OK;
1679 }
1680 /*
1681 * Failed to recognize a character class. Use the simple
1682 * version that turns [abc] into 'a' OR 'b' OR 'c'
1683 */
1684 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001685 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001686 if (*regparse == '^') /* negated range */
1687 {
1688 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001689 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001690 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001692 else
1693 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694 if (*regparse == '-')
1695 {
1696 startc = '-';
1697 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001698 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001699 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001700 }
1701 /* Emit the OR branches for each character in the [] */
1702 emit_range = FALSE;
1703 while (regparse < endp)
1704 {
1705 oldstartc = startc;
1706 startc = -1;
1707 got_coll_char = FALSE;
1708 if (*regparse == '[')
1709 {
1710 /* Check for [: :], [= =], [. .] */
1711 equiclass = collclass = 0;
1712 charclass = get_char_class(&regparse);
1713 if (charclass == CLASS_NONE)
1714 {
1715 equiclass = get_equi_class(&regparse);
1716 if (equiclass == 0)
1717 collclass = get_coll_element(&regparse);
1718 }
1719
1720 /* Character class like [:alpha:] */
1721 if (charclass != CLASS_NONE)
1722 {
1723 switch (charclass)
1724 {
1725 case CLASS_ALNUM:
1726 EMIT(NFA_CLASS_ALNUM);
1727 break;
1728 case CLASS_ALPHA:
1729 EMIT(NFA_CLASS_ALPHA);
1730 break;
1731 case CLASS_BLANK:
1732 EMIT(NFA_CLASS_BLANK);
1733 break;
1734 case CLASS_CNTRL:
1735 EMIT(NFA_CLASS_CNTRL);
1736 break;
1737 case CLASS_DIGIT:
1738 EMIT(NFA_CLASS_DIGIT);
1739 break;
1740 case CLASS_GRAPH:
1741 EMIT(NFA_CLASS_GRAPH);
1742 break;
1743 case CLASS_LOWER:
1744 EMIT(NFA_CLASS_LOWER);
1745 break;
1746 case CLASS_PRINT:
1747 EMIT(NFA_CLASS_PRINT);
1748 break;
1749 case CLASS_PUNCT:
1750 EMIT(NFA_CLASS_PUNCT);
1751 break;
1752 case CLASS_SPACE:
1753 EMIT(NFA_CLASS_SPACE);
1754 break;
1755 case CLASS_UPPER:
1756 EMIT(NFA_CLASS_UPPER);
1757 break;
1758 case CLASS_XDIGIT:
1759 EMIT(NFA_CLASS_XDIGIT);
1760 break;
1761 case CLASS_TAB:
1762 EMIT(NFA_CLASS_TAB);
1763 break;
1764 case CLASS_RETURN:
1765 EMIT(NFA_CLASS_RETURN);
1766 break;
1767 case CLASS_BACKSPACE:
1768 EMIT(NFA_CLASS_BACKSPACE);
1769 break;
1770 case CLASS_ESCAPE:
1771 EMIT(NFA_CLASS_ESCAPE);
1772 break;
1773 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001774 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001775 continue;
1776 }
1777 /* Try equivalence class [=a=] and the like */
1778 if (equiclass != 0)
1779 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001780 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001781 if (result == FAIL)
1782 {
1783 /* should never happen */
1784 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1785 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786 continue;
1787 }
1788 /* Try collating class like [. .] */
1789 if (collclass != 0)
1790 {
1791 startc = collclass; /* allow [.a.]-x as a range */
1792 /* Will emit the proper atom at the end of the
1793 * while loop. */
1794 }
1795 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001796 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1797 * start character. */
1798 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 {
1800 emit_range = TRUE;
1801 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001802 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001803 continue; /* reading the end of the range */
1804 }
1805
1806 /* Now handle simple and escaped characters.
1807 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1808 * accepts "\t", "\e", etc., but only when the 'l' flag in
1809 * 'cpoptions' is not included.
1810 * Posix doesn't recognize backslash at all.
1811 */
1812 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001813 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001814 && regparse + 1 <= endp
1815 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001816 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001817 && vim_strchr(REGEXP_ABBR, regparse[1])
1818 != NULL)
1819 )
1820 )
1821 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001822 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001824 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001825 startc = reg_string ? NL : NFA_NEWL;
1826 else
1827 if (*regparse == 'd'
1828 || *regparse == 'o'
1829 || *regparse == 'x'
1830 || *regparse == 'u'
1831 || *regparse == 'U'
1832 )
1833 {
1834 /* TODO(RE) This needs more testing */
1835 startc = coll_get_char();
1836 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001837 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 }
1839 else
1840 {
1841 /* \r,\t,\e,\b */
1842 startc = backslash_trans(*regparse);
1843 }
1844 }
1845
1846 /* Normal printable char */
1847 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001848 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001849
1850 /* Previous char was '-', so this char is end of range. */
1851 if (emit_range)
1852 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001853 endc = startc;
1854 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001856 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001857
1858 if (endc > startc + 2)
1859 {
1860 /* Emit a range instead of the sequence of
1861 * individual characters. */
1862 if (startc == 0)
1863 /* \x00 is translated to \x0a, start at \x01. */
1864 EMIT(1);
1865 else
1866 --post_ptr; /* remove NFA_CONCAT */
1867 EMIT(endc);
1868 EMIT(NFA_RANGE);
1869 EMIT(NFA_CONCAT);
1870 }
1871 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001873 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 || (*mb_char2len)(endc) > 1))
1875 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001876 /* Emit the characters in the range.
1877 * "startc" was already emitted, so skip it.
1878 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001879 for (c = startc + 1; c <= endc; c++)
1880 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001881 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001882 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001883 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884 }
1885 else
1886#endif
1887 {
1888#ifdef EBCDIC
1889 int alpha_only = FALSE;
1890
1891 /* for alphabetical range skip the gaps
1892 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1893 if (isalpha(startc) && isalpha(endc))
1894 alpha_only = TRUE;
1895#endif
1896 /* Emit the range. "startc" was already emitted, so
1897 * skip it. */
1898 for (c = startc + 1; c <= endc; c++)
1899#ifdef EBCDIC
1900 if (!alpha_only || isalpha(startc))
1901#endif
1902 {
1903 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001904 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001907 emit_range = FALSE;
1908 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 }
1910 else
1911 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001912 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001913 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914 * Normally, simply emit startc. But if we get char
1915 * code=0 from a collating char, then replace it with
1916 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001917 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001918 * the backtracking engine. */
1919 if (startc == NFA_NEWL)
1920 {
1921 /* Line break can't be matched as part of the
1922 * collection, add an OR below. But not for negated
1923 * range. */
1924 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001925 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001926 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001928 {
1929 if (got_coll_char == TRUE && startc == 0)
1930 EMIT(0x0a);
1931 else
1932 EMIT(startc);
1933 EMIT(NFA_CONCAT);
1934 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 }
1936
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001937 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938 } /* while (p < endp) */
1939
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001940 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 if (*regparse == '-') /* if last, '-' is just a char */
1942 {
1943 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001944 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001946
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001947 /* skip the trailing ] */
1948 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001949 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001950
1951 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001953 EMIT(NFA_END_NEG_COLL);
1954 else
1955 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001956
1957 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001958 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001959 {
1960 EMIT(reg_string ? NL : NFA_NEWL);
1961 EMIT(NFA_OR);
1962 }
1963
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001965 } /* if exists closing ] */
1966
1967 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001969 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001971 default:
1972 {
1973#ifdef FEAT_MBYTE
1974 int plen;
1975
1976nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001977 /* plen is length of current char with composing chars */
1978 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001979 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001980 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001981 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001982 int i = 0;
1983
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001984 /* A base character plus composing characters, or just one
1985 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001986 * This requires creating a separate atom as if enclosing
1987 * the characters in (), where NFA_COMPOSING is the ( and
1988 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989 * building the postfix form, not the NFA itself;
1990 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001991 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001992 for (;;)
1993 {
1994 EMIT(c);
1995 if (i > 0)
1996 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001997 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001998 break;
1999 c = utf_ptr2char(old_regparse + i);
2000 }
2001 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002 regparse = old_regparse + plen;
2003 }
2004 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002005#endif
2006 {
2007 c = no_Magic(c);
2008 EMIT(c);
2009 }
2010 return OK;
2011 }
2012 }
2013
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014 return OK;
2015}
2016
2017/*
2018 * Parse something followed by possible [*+=].
2019 *
2020 * A piece is an atom, possibly followed by a multi, an indication of how many
2021 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2022 * characters: "", "a", "aa", etc.
2023 *
2024 * piece ::= atom
2025 * or atom multi
2026 */
2027 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002028nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029{
2030 int i;
2031 int op;
2032 int ret;
2033 long minval, maxval;
2034 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002035 parse_state_T old_state;
2036 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002038 int old_post_pos;
2039 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040 int quest;
2041
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002042 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2043 * next. */
2044 save_parse_state(&old_state);
2045
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002047 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002048
2049 ret = nfa_regatom();
2050 if (ret == FAIL)
2051 return FAIL; /* cascaded error */
2052
2053 op = peekchr();
2054 if (re_multi_type(op) == NOT_MULTI)
2055 return OK;
2056
2057 skipchr();
2058 switch (op)
2059 {
2060 case Magic('*'):
2061 EMIT(NFA_STAR);
2062 break;
2063
2064 case Magic('+'):
2065 /*
2066 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2067 * first and only submatch would be "aaa". But the backtracking
2068 * engine interprets the plus as "try matching one more time", and
2069 * a* matches a second time at the end of the input, the empty
2070 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002071 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002073 * In order to be consistent with the old engine, we replace
2074 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002075 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002076 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 curchr = -1;
2078 if (nfa_regatom() == FAIL)
2079 return FAIL;
2080 EMIT(NFA_STAR);
2081 EMIT(NFA_CONCAT);
2082 skipchr(); /* skip the \+ */
2083 break;
2084
2085 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002086 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002088 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002089 switch(op)
2090 {
2091 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002092 /* \@= */
2093 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002094 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002095 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002096 /* \@! */
2097 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002098 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002099 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002100 op = no_Magic(getchr());
2101 if (op == '=')
2102 /* \@<= */
2103 i = NFA_PREV_ATOM_JUST_BEFORE;
2104 else if (op == '!')
2105 /* \@<! */
2106 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2107 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002108 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002109 /* \@> */
2110 i = NFA_PREV_ATOM_LIKE_PATTERN;
2111 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002113 if (i == 0)
2114 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002115 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2116 return FAIL;
2117 }
2118 EMIT(i);
2119 if (i == NFA_PREV_ATOM_JUST_BEFORE
2120 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2121 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122 break;
2123
2124 case Magic('?'):
2125 case Magic('='):
2126 EMIT(NFA_QUEST);
2127 break;
2128
2129 case Magic('{'):
2130 /* a{2,5} will expand to 'aaa?a?a?'
2131 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2132 * version of '?'
2133 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2134 * parenthesis have the same id
2135 */
2136
2137 greedy = TRUE;
2138 c2 = peekchr();
2139 if (c2 == '-' || c2 == Magic('-'))
2140 {
2141 skipchr();
2142 greedy = FALSE;
2143 }
2144 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002145 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002146
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2148 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002149 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002151 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002152 /* \{}, \{0,} */
2153 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002154 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002155 /* \{-}, \{-0,} */
2156 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002157 break;
2158 }
2159
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002160 /* Special case: x{0} or x{-0} */
2161 if (maxval == 0)
2162 {
2163 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002164 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002165 /* NFA_EMPTY is 0-length and works everywhere */
2166 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002167 return OK;
2168 }
2169
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002170 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002171 * maximum is much larger than the minimum and when the maximum is
2172 * large. Bail out if we can use the other engine. */
2173 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002174 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002175 return FAIL;
2176
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002177 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002178 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002179 /* Save parse state after the repeated atom and the \{} */
2180 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2183 for (i = 0; i < maxval; i++)
2184 {
2185 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002186 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002187 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002188 if (nfa_regatom() == FAIL)
2189 return FAIL;
2190 /* after "minval" times, atoms are optional */
2191 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002192 {
2193 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002194 {
2195 if (greedy)
2196 EMIT(NFA_STAR);
2197 else
2198 EMIT(NFA_STAR_NONGREEDY);
2199 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002200 else
2201 EMIT(quest);
2202 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002203 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002204 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002205 if (i + 1 > minval && maxval == MAX_LIMIT)
2206 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207 }
2208
2209 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002210 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002211 curchr = -1;
2212
2213 break;
2214
2215
2216 default:
2217 break;
2218 } /* end switch */
2219
2220 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002222 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002223
2224 return OK;
2225}
2226
2227/*
2228 * Parse one or more pieces, concatenated. It matches a match for the
2229 * first piece, followed by a match for the second piece, etc. Example:
2230 * "f[0-9]b", first matches "f", then a digit and then "b".
2231 *
2232 * concat ::= piece
2233 * or piece piece
2234 * or piece piece piece
2235 * etc.
2236 */
2237 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002238nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002239{
2240 int cont = TRUE;
2241 int first = TRUE;
2242
2243 while (cont)
2244 {
2245 switch (peekchr())
2246 {
2247 case NUL:
2248 case Magic('|'):
2249 case Magic('&'):
2250 case Magic(')'):
2251 cont = FALSE;
2252 break;
2253
2254 case Magic('Z'):
2255#ifdef FEAT_MBYTE
2256 regflags |= RF_ICOMBINE;
2257#endif
2258 skipchr_keepstart();
2259 break;
2260 case Magic('c'):
2261 regflags |= RF_ICASE;
2262 skipchr_keepstart();
2263 break;
2264 case Magic('C'):
2265 regflags |= RF_NOICASE;
2266 skipchr_keepstart();
2267 break;
2268 case Magic('v'):
2269 reg_magic = MAGIC_ALL;
2270 skipchr_keepstart();
2271 curchr = -1;
2272 break;
2273 case Magic('m'):
2274 reg_magic = MAGIC_ON;
2275 skipchr_keepstart();
2276 curchr = -1;
2277 break;
2278 case Magic('M'):
2279 reg_magic = MAGIC_OFF;
2280 skipchr_keepstart();
2281 curchr = -1;
2282 break;
2283 case Magic('V'):
2284 reg_magic = MAGIC_NONE;
2285 skipchr_keepstart();
2286 curchr = -1;
2287 break;
2288
2289 default:
2290 if (nfa_regpiece() == FAIL)
2291 return FAIL;
2292 if (first == FALSE)
2293 EMIT(NFA_CONCAT);
2294 else
2295 first = FALSE;
2296 break;
2297 }
2298 }
2299
2300 return OK;
2301}
2302
2303/*
2304 * Parse a branch, one or more concats, separated by "\&". It matches the
2305 * last concat, but only if all the preceding concats also match at the same
2306 * position. Examples:
2307 * "foobeep\&..." matches "foo" in "foobeep".
2308 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2309 *
2310 * branch ::= concat
2311 * or concat \& concat
2312 * or concat \& concat \& concat
2313 * etc.
2314 */
2315 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002316nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317{
2318 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002319 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002320
Bram Moolenaar16299b52013-05-30 18:45:23 +02002321 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002322
2323 /* First branch, possibly the only one */
2324 if (nfa_regconcat() == FAIL)
2325 return FAIL;
2326
2327 ch = peekchr();
2328 /* Try next concats */
2329 while (ch == Magic('&'))
2330 {
2331 skipchr();
2332 EMIT(NFA_NOPEN);
2333 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002334 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002335 if (nfa_regconcat() == FAIL)
2336 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002337 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002338 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002339 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340 EMIT(NFA_CONCAT);
2341 ch = peekchr();
2342 }
2343
Bram Moolenaar699c1202013-09-25 16:41:54 +02002344 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002345 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002346 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002347
2348 return OK;
2349}
2350
2351/*
2352 * Parse a pattern, one or more branches, separated by "\|". It matches
2353 * anything that matches one of the branches. Example: "foo\|beep" matches
2354 * "foo" and matches "beep". If more than one branch matches, the first one
2355 * is used.
2356 *
2357 * pattern ::= branch
2358 * or branch \| branch
2359 * or branch \| branch \| branch
2360 * etc.
2361 */
2362 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002363nfa_reg(
2364 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365{
2366 int parno = 0;
2367
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002368 if (paren == REG_PAREN)
2369 {
2370 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 parno = regnpar++;
2373 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002374#ifdef FEAT_SYN_HL
2375 else if (paren == REG_ZPAREN)
2376 {
2377 /* Make a ZOPEN node. */
2378 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002379 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002380 parno = regnzpar++;
2381 }
2382#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383
2384 if (nfa_regbranch() == FAIL)
2385 return FAIL; /* cascaded error */
2386
2387 while (peekchr() == Magic('|'))
2388 {
2389 skipchr();
2390 if (nfa_regbranch() == FAIL)
2391 return FAIL; /* cascaded error */
2392 EMIT(NFA_OR);
2393 }
2394
2395 /* Check for proper termination. */
2396 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2397 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398 if (paren == REG_NPAREN)
2399 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2400 else
2401 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2402 }
2403 else if (paren == REG_NOPAREN && peekchr() != NUL)
2404 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002405 if (peekchr() == Magic(')'))
2406 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2407 else
2408 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2409 }
2410 /*
2411 * Here we set the flag allowing back references to this set of
2412 * parentheses.
2413 */
2414 if (paren == REG_PAREN)
2415 {
2416 had_endbrace[parno] = TRUE; /* have seen the close paren */
2417 EMIT(NFA_MOPEN + parno);
2418 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002419#ifdef FEAT_SYN_HL
2420 else if (paren == REG_ZPAREN)
2421 EMIT(NFA_ZOPEN + parno);
2422#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002423
2424 return OK;
2425}
2426
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002427#ifdef DEBUG
2428static char_u code[50];
2429
2430 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002431nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432{
2433 int addnl = FALSE;
2434
2435 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2436 {
2437 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002438 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 }
2440
2441 STRCPY(code, "");
2442 switch (c)
2443 {
2444 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2445 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2446 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2447 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2448 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2449 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2450
Bram Moolenaar5714b802013-05-28 22:03:20 +02002451 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2452 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2453 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2454 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2455 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2456 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2457 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2458 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2459 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002460#ifdef FEAT_SYN_HL
2461 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2462 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2463 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2464 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2465 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2466 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2467 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2468 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2469 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2470#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002471 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2472
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 case NFA_PREV_ATOM_NO_WIDTH:
2474 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002475 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2476 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002477 case NFA_PREV_ATOM_JUST_BEFORE:
2478 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2479 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2480 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002481 case NFA_PREV_ATOM_LIKE_PATTERN:
2482 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2483
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002484 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2485 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002487 case NFA_START_INVISIBLE_FIRST:
2488 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002489 case NFA_START_INVISIBLE_NEG:
2490 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002491 case NFA_START_INVISIBLE_NEG_FIRST:
2492 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002493 case NFA_START_INVISIBLE_BEFORE:
2494 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002495 case NFA_START_INVISIBLE_BEFORE_FIRST:
2496 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002497 case NFA_START_INVISIBLE_BEFORE_NEG:
2498 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002499 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2500 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002501 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002502 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002503 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002504 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2507 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002508 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002509
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002510 case NFA_MOPEN:
2511 case NFA_MOPEN1:
2512 case NFA_MOPEN2:
2513 case NFA_MOPEN3:
2514 case NFA_MOPEN4:
2515 case NFA_MOPEN5:
2516 case NFA_MOPEN6:
2517 case NFA_MOPEN7:
2518 case NFA_MOPEN8:
2519 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520 STRCPY(code, "NFA_MOPEN(x)");
2521 code[10] = c - NFA_MOPEN + '0';
2522 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002523 case NFA_MCLOSE:
2524 case NFA_MCLOSE1:
2525 case NFA_MCLOSE2:
2526 case NFA_MCLOSE3:
2527 case NFA_MCLOSE4:
2528 case NFA_MCLOSE5:
2529 case NFA_MCLOSE6:
2530 case NFA_MCLOSE7:
2531 case NFA_MCLOSE8:
2532 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002533 STRCPY(code, "NFA_MCLOSE(x)");
2534 code[11] = c - NFA_MCLOSE + '0';
2535 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002536#ifdef FEAT_SYN_HL
2537 case NFA_ZOPEN:
2538 case NFA_ZOPEN1:
2539 case NFA_ZOPEN2:
2540 case NFA_ZOPEN3:
2541 case NFA_ZOPEN4:
2542 case NFA_ZOPEN5:
2543 case NFA_ZOPEN6:
2544 case NFA_ZOPEN7:
2545 case NFA_ZOPEN8:
2546 case NFA_ZOPEN9:
2547 STRCPY(code, "NFA_ZOPEN(x)");
2548 code[10] = c - NFA_ZOPEN + '0';
2549 break;
2550 case NFA_ZCLOSE:
2551 case NFA_ZCLOSE1:
2552 case NFA_ZCLOSE2:
2553 case NFA_ZCLOSE3:
2554 case NFA_ZCLOSE4:
2555 case NFA_ZCLOSE5:
2556 case NFA_ZCLOSE6:
2557 case NFA_ZCLOSE7:
2558 case NFA_ZCLOSE8:
2559 case NFA_ZCLOSE9:
2560 STRCPY(code, "NFA_ZCLOSE(x)");
2561 code[11] = c - NFA_ZCLOSE + '0';
2562 break;
2563#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002564 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2565 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2566 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2567 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002568 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2569 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002570 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2571 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2572 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2573 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2574 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2575 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2576 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2577 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2578 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2579 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2580 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2581 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2582 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2583 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002584 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002585
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002586 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002587 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2588 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2589 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002590 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002591 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002592
2593 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2594 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2595 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2596 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2597 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2598 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2599 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002601 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2602 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2603 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2604 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2605 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2606 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2607 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2608 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2609 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2610 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2611 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2612 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2613 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2614 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2615 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2616 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2617
2618 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2619 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2620 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2621 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2622 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2623 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2624 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2625 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2626 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2627 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2628 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2629 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2630 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2631 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2632 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2633 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2634 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2635 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2636 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2637 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2638 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2639 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2640 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2641 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2642 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2643 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2644 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002645 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2646 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2647 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2648 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649
2650 default:
2651 STRCPY(code, "CHAR(x)");
2652 code[5] = c;
2653 }
2654
2655 if (addnl == TRUE)
2656 STRCAT(code, " + NEWLINE ");
2657
2658}
2659
2660#ifdef ENABLE_LOG
2661static FILE *log_fd;
2662
2663/*
2664 * Print the postfix notation of the current regexp.
2665 */
2666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002667nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002668{
2669 int *p;
2670 FILE *f;
2671
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002672 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002673 if (f != NULL)
2674 {
2675 fprintf(f, "\n-------------------------\n");
2676 if (retval == FAIL)
2677 fprintf(f, ">>> NFA engine failed ... \n");
2678 else if (retval == OK)
2679 fprintf(f, ">>> NFA engine succeeded !\n");
2680 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002681 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002682 {
2683 nfa_set_code(*p);
2684 fprintf(f, "%s, ", code);
2685 }
2686 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002687 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002688 fprintf(f, "%d ", *p);
2689 fprintf(f, "\n\n");
2690 fclose(f);
2691 }
2692}
2693
2694/*
2695 * Print the NFA starting with a root node "state".
2696 */
2697 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002698nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002700 garray_T indent;
2701
2702 ga_init2(&indent, 1, 64);
2703 ga_append(&indent, '\0');
2704 nfa_print_state2(debugf, state, &indent);
2705 ga_clear(&indent);
2706}
2707
2708 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002709nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002710{
2711 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712
2713 if (state == NULL)
2714 return;
2715
2716 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002717
2718 /* Output indent */
2719 p = (char_u *)indent->ga_data;
2720 if (indent->ga_len >= 3)
2721 {
2722 int last = indent->ga_len - 3;
2723 char_u save[2];
2724
2725 STRNCPY(save, &p[last], 2);
2726 STRNCPY(&p[last], "+-", 2);
2727 fprintf(debugf, " %s", p);
2728 STRNCPY(&p[last], save, 2);
2729 }
2730 else
2731 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732
2733 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002734 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002735 code,
2736 state->c,
2737 abs(state->id),
2738 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739 if (state->id < 0)
2740 return;
2741
2742 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002743
2744 /* grow indent for state->out */
2745 indent->ga_len -= 1;
2746 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002747 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002748 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002749 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002750 ga_append(indent, '\0');
2751
2752 nfa_print_state2(debugf, state->out, indent);
2753
2754 /* replace last part of indent for state->out1 */
2755 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002756 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002757 ga_append(indent, '\0');
2758
2759 nfa_print_state2(debugf, state->out1, indent);
2760
2761 /* shrink indent */
2762 indent->ga_len -= 3;
2763 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002764}
2765
2766/*
2767 * Print the NFA state machine.
2768 */
2769 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002770nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002771{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002772 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002773
2774 if (debugf != NULL)
2775 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002776 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002777
Bram Moolenaar473de612013-06-08 18:19:48 +02002778 if (prog->reganch)
2779 fprintf(debugf, "reganch: %d\n", prog->reganch);
2780 if (prog->regstart != NUL)
2781 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2782 prog->regstart, prog->regstart);
2783 if (prog->match_text != NULL)
2784 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002785
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002786 fclose(debugf);
2787 }
2788}
2789#endif /* ENABLE_LOG */
2790#endif /* DEBUG */
2791
2792/*
2793 * Parse r.e. @expr and convert it into postfix form.
2794 * Return the postfix string on success, NULL otherwise.
2795 */
2796 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002797re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798{
2799 if (nfa_reg(REG_NOPAREN) == FAIL)
2800 return NULL;
2801 EMIT(NFA_MOPEN);
2802 return post_start;
2803}
2804
2805/* NB. Some of the code below is inspired by Russ's. */
2806
2807/*
2808 * Represents an NFA state plus zero or one or two arrows exiting.
2809 * if c == MATCH, no arrows out; matching state.
2810 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2811 * If c < 256, labeled arrow with character c to out.
2812 */
2813
2814static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2815
2816/*
2817 * Allocate and initialize nfa_state_T.
2818 */
2819 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002820alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821{
2822 nfa_state_T *s;
2823
2824 if (istate >= nstate)
2825 return NULL;
2826
2827 s = &state_ptr[istate++];
2828
2829 s->c = c;
2830 s->out = out;
2831 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002832 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002833
2834 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002835 s->lastlist[0] = 0;
2836 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002837
2838 return s;
2839}
2840
2841/*
2842 * A partially built NFA without the matching state filled in.
2843 * Frag_T.start points at the start state.
2844 * Frag_T.out is a list of places that need to be set to the
2845 * next state for this fragment.
2846 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002847
2848/* Since the out pointers in the list are always
2849 * uninitialized, we use the pointers themselves
2850 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002852union Ptrlist
2853{
2854 Ptrlist *next;
2855 nfa_state_T *s;
2856};
2857
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002858struct Frag
2859{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002860 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002861 Ptrlist *out;
2862};
2863typedef struct Frag Frag_T;
2864
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002865static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2866static Ptrlist *list1(nfa_state_T **outp);
2867static void patch(Ptrlist *l, nfa_state_T *s);
2868static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2869static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2870static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002871
2872/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002873 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874 */
2875 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002876frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002878 Frag_T n;
2879
2880 n.start = start;
2881 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 return n;
2883}
2884
2885/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 * Create singleton list containing just outp.
2887 */
2888 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002889list1(
2890 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891{
2892 Ptrlist *l;
2893
2894 l = (Ptrlist *)outp;
2895 l->next = NULL;
2896 return l;
2897}
2898
2899/*
2900 * Patch the list of states at out to point to start.
2901 */
2902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002903patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904{
2905 Ptrlist *next;
2906
2907 for (; l; l = next)
2908 {
2909 next = l->next;
2910 l->s = s;
2911 }
2912}
2913
2914
2915/*
2916 * Join the two lists l1 and l2, returning the combination.
2917 */
2918 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002919append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002920{
2921 Ptrlist *oldl1;
2922
2923 oldl1 = l1;
2924 while (l1->next)
2925 l1 = l1->next;
2926 l1->next = l2;
2927 return oldl1;
2928}
2929
2930/*
2931 * Stack used for transforming postfix form into NFA.
2932 */
2933static Frag_T empty;
2934
2935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002936st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002937{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002938#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002939 FILE *df;
2940 int *p2;
2941
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002942 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002943 if (df)
2944 {
2945 fprintf(df, "Error popping the stack!\n");
2946#ifdef DEBUG
2947 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2948#endif
2949 fprintf(df, "Postfix form is: ");
2950#ifdef DEBUG
2951 for (p2 = postfix; p2 < end; p2++)
2952 {
2953 nfa_set_code(*p2);
2954 fprintf(df, "%s, ", code);
2955 }
2956 nfa_set_code(*p);
2957 fprintf(df, "\nCurrent position is: ");
2958 for (p2 = postfix; p2 <= p; p2 ++)
2959 {
2960 nfa_set_code(*p2);
2961 fprintf(df, "%s, ", code);
2962 }
2963#else
2964 for (p2 = postfix; p2 < end; p2++)
2965 {
2966 fprintf(df, "%d, ", *p2);
2967 }
2968 fprintf(df, "\nCurrent position is: ");
2969 for (p2 = postfix; p2 <= p; p2 ++)
2970 {
2971 fprintf(df, "%d, ", *p2);
2972 }
2973#endif
2974 fprintf(df, "\n--------------------------\n");
2975 fclose(df);
2976 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002977#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002978 EMSG(_("E874: (NFA) Could not pop the stack !"));
2979}
2980
2981/*
2982 * Push an item onto the stack.
2983 */
2984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002985st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002986{
2987 Frag_T *stackp = *p;
2988
2989 if (stackp >= stack_end)
2990 return;
2991 *stackp = s;
2992 *p = *p + 1;
2993}
2994
2995/*
2996 * Pop an item from the stack.
2997 */
2998 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002999st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000{
3001 Frag_T *stackp;
3002
3003 *p = *p - 1;
3004 stackp = *p;
3005 if (stackp < stack)
3006 return empty;
3007 return **p;
3008}
3009
3010/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003011 * Estimate the maximum byte length of anything matching "state".
3012 * When unknown or unlimited return -1.
3013 */
3014 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003015nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003016{
3017 int l, r;
3018 nfa_state_T *state = startstate;
3019 int len = 0;
3020
3021 /* detect looping in a NFA_SPLIT */
3022 if (depth > 4)
3023 return -1;
3024
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003025 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003026 {
3027 switch (state->c)
3028 {
3029 case NFA_END_INVISIBLE:
3030 case NFA_END_INVISIBLE_NEG:
3031 /* the end, return what we have */
3032 return len;
3033
3034 case NFA_SPLIT:
3035 /* two alternatives, use the maximum */
3036 l = nfa_max_width(state->out, depth + 1);
3037 r = nfa_max_width(state->out1, depth + 1);
3038 if (l < 0 || r < 0)
3039 return -1;
3040 return len + (l > r ? l : r);
3041
3042 case NFA_ANY:
3043 case NFA_START_COLL:
3044 case NFA_START_NEG_COLL:
3045 /* matches some character, including composing chars */
3046#ifdef FEAT_MBYTE
3047 if (enc_utf8)
3048 len += MB_MAXBYTES;
3049 else if (has_mbyte)
3050 len += 2;
3051 else
3052#endif
3053 ++len;
3054 if (state->c != NFA_ANY)
3055 {
3056 /* skip over the characters */
3057 state = state->out1->out;
3058 continue;
3059 }
3060 break;
3061
3062 case NFA_DIGIT:
3063 case NFA_WHITE:
3064 case NFA_HEX:
3065 case NFA_OCTAL:
3066 /* ascii */
3067 ++len;
3068 break;
3069
3070 case NFA_IDENT:
3071 case NFA_SIDENT:
3072 case NFA_KWORD:
3073 case NFA_SKWORD:
3074 case NFA_FNAME:
3075 case NFA_SFNAME:
3076 case NFA_PRINT:
3077 case NFA_SPRINT:
3078 case NFA_NWHITE:
3079 case NFA_NDIGIT:
3080 case NFA_NHEX:
3081 case NFA_NOCTAL:
3082 case NFA_WORD:
3083 case NFA_NWORD:
3084 case NFA_HEAD:
3085 case NFA_NHEAD:
3086 case NFA_ALPHA:
3087 case NFA_NALPHA:
3088 case NFA_LOWER:
3089 case NFA_NLOWER:
3090 case NFA_UPPER:
3091 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003092 case NFA_LOWER_IC:
3093 case NFA_NLOWER_IC:
3094 case NFA_UPPER_IC:
3095 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003096 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003097 /* possibly non-ascii */
3098#ifdef FEAT_MBYTE
3099 if (has_mbyte)
3100 len += 3;
3101 else
3102#endif
3103 ++len;
3104 break;
3105
3106 case NFA_START_INVISIBLE:
3107 case NFA_START_INVISIBLE_NEG:
3108 case NFA_START_INVISIBLE_BEFORE:
3109 case NFA_START_INVISIBLE_BEFORE_NEG:
3110 /* zero-width, out1 points to the END state */
3111 state = state->out1->out;
3112 continue;
3113
3114 case NFA_BACKREF1:
3115 case NFA_BACKREF2:
3116 case NFA_BACKREF3:
3117 case NFA_BACKREF4:
3118 case NFA_BACKREF5:
3119 case NFA_BACKREF6:
3120 case NFA_BACKREF7:
3121 case NFA_BACKREF8:
3122 case NFA_BACKREF9:
3123#ifdef FEAT_SYN_HL
3124 case NFA_ZREF1:
3125 case NFA_ZREF2:
3126 case NFA_ZREF3:
3127 case NFA_ZREF4:
3128 case NFA_ZREF5:
3129 case NFA_ZREF6:
3130 case NFA_ZREF7:
3131 case NFA_ZREF8:
3132 case NFA_ZREF9:
3133#endif
3134 case NFA_NEWL:
3135 case NFA_SKIP:
3136 /* unknown width */
3137 return -1;
3138
3139 case NFA_BOL:
3140 case NFA_EOL:
3141 case NFA_BOF:
3142 case NFA_EOF:
3143 case NFA_BOW:
3144 case NFA_EOW:
3145 case NFA_MOPEN:
3146 case NFA_MOPEN1:
3147 case NFA_MOPEN2:
3148 case NFA_MOPEN3:
3149 case NFA_MOPEN4:
3150 case NFA_MOPEN5:
3151 case NFA_MOPEN6:
3152 case NFA_MOPEN7:
3153 case NFA_MOPEN8:
3154 case NFA_MOPEN9:
3155#ifdef FEAT_SYN_HL
3156 case NFA_ZOPEN:
3157 case NFA_ZOPEN1:
3158 case NFA_ZOPEN2:
3159 case NFA_ZOPEN3:
3160 case NFA_ZOPEN4:
3161 case NFA_ZOPEN5:
3162 case NFA_ZOPEN6:
3163 case NFA_ZOPEN7:
3164 case NFA_ZOPEN8:
3165 case NFA_ZOPEN9:
3166 case NFA_ZCLOSE:
3167 case NFA_ZCLOSE1:
3168 case NFA_ZCLOSE2:
3169 case NFA_ZCLOSE3:
3170 case NFA_ZCLOSE4:
3171 case NFA_ZCLOSE5:
3172 case NFA_ZCLOSE6:
3173 case NFA_ZCLOSE7:
3174 case NFA_ZCLOSE8:
3175 case NFA_ZCLOSE9:
3176#endif
3177 case NFA_MCLOSE:
3178 case NFA_MCLOSE1:
3179 case NFA_MCLOSE2:
3180 case NFA_MCLOSE3:
3181 case NFA_MCLOSE4:
3182 case NFA_MCLOSE5:
3183 case NFA_MCLOSE6:
3184 case NFA_MCLOSE7:
3185 case NFA_MCLOSE8:
3186 case NFA_MCLOSE9:
3187 case NFA_NOPEN:
3188 case NFA_NCLOSE:
3189
3190 case NFA_LNUM_GT:
3191 case NFA_LNUM_LT:
3192 case NFA_COL_GT:
3193 case NFA_COL_LT:
3194 case NFA_VCOL_GT:
3195 case NFA_VCOL_LT:
3196 case NFA_MARK_GT:
3197 case NFA_MARK_LT:
3198 case NFA_VISUAL:
3199 case NFA_LNUM:
3200 case NFA_CURSOR:
3201 case NFA_COL:
3202 case NFA_VCOL:
3203 case NFA_MARK:
3204
3205 case NFA_ZSTART:
3206 case NFA_ZEND:
3207 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003208 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003209 case NFA_START_PATTERN:
3210 case NFA_END_PATTERN:
3211 case NFA_COMPOSING:
3212 case NFA_END_COMPOSING:
3213 /* zero-width */
3214 break;
3215
3216 default:
3217 if (state->c < 0)
3218 /* don't know what this is */
3219 return -1;
3220 /* normal character */
3221 len += MB_CHAR2LEN(state->c);
3222 break;
3223 }
3224
3225 /* normal way to continue */
3226 state = state->out;
3227 }
3228
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003229 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003230 return -1;
3231}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003232
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003233/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 * Convert a postfix form into its equivalent NFA.
3235 * Return the NFA start state on success, NULL otherwise.
3236 */
3237 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003238post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239{
3240 int *p;
3241 int mopen;
3242 int mclose;
3243 Frag_T *stack = NULL;
3244 Frag_T *stackp = NULL;
3245 Frag_T *stack_end = NULL;
3246 Frag_T e1;
3247 Frag_T e2;
3248 Frag_T e;
3249 nfa_state_T *s;
3250 nfa_state_T *s1;
3251 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003252 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253
3254 if (postfix == NULL)
3255 return NULL;
3256
Bram Moolenaar053bb602013-05-20 13:55:21 +02003257#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003258#define POP() st_pop(&stackp, stack); \
3259 if (stackp < stack) \
3260 { \
3261 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003262 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263 return NULL; \
3264 }
3265
3266 if (nfa_calc_size == FALSE)
3267 {
3268 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003269 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003271 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003272 }
3273
3274 for (p = postfix; p < end; ++p)
3275 {
3276 switch (*p)
3277 {
3278 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003279 /* Concatenation.
3280 * Pay attention: this operator does not exist in the r.e. itself
3281 * (it is implicit, really). It is added when r.e. is translated
3282 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 if (nfa_calc_size == TRUE)
3284 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003285 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 break;
3287 }
3288 e2 = POP();
3289 e1 = POP();
3290 patch(e1.out, e2.start);
3291 PUSH(frag(e1.start, e2.out));
3292 break;
3293
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 case NFA_OR:
3295 /* Alternation */
3296 if (nfa_calc_size == TRUE)
3297 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003298 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 break;
3300 }
3301 e2 = POP();
3302 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003303 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003305 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306 PUSH(frag(s, append(e1.out, e2.out)));
3307 break;
3308
3309 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003310 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311 if (nfa_calc_size == TRUE)
3312 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003313 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 break;
3315 }
3316 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003317 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003319 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 patch(e.out, s);
3321 PUSH(frag(s, list1(&s->out1)));
3322 break;
3323
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003324 case NFA_STAR_NONGREEDY:
3325 /* Zero or more, prefer zero */
3326 if (nfa_calc_size == TRUE)
3327 {
3328 nstate++;
3329 break;
3330 }
3331 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003332 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003333 if (s == NULL)
3334 goto theend;
3335 patch(e.out, s);
3336 PUSH(frag(s, list1(&s->out)));
3337 break;
3338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339 case NFA_QUEST:
3340 /* one or zero atoms=> greedy match */
3341 if (nfa_calc_size == TRUE)
3342 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003343 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003344 break;
3345 }
3346 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003347 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003349 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350 PUSH(frag(s, append(e.out, list1(&s->out1))));
3351 break;
3352
3353 case NFA_QUEST_NONGREEDY:
3354 /* zero or one atoms => non-greedy match */
3355 if (nfa_calc_size == TRUE)
3356 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003357 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 break;
3359 }
3360 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003361 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003363 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 PUSH(frag(s, append(e.out, list1(&s->out))));
3365 break;
3366
Bram Moolenaar417bad22013-06-07 14:08:30 +02003367 case NFA_END_COLL:
3368 case NFA_END_NEG_COLL:
3369 /* On the stack is the sequence starting with NFA_START_COLL or
3370 * NFA_START_NEG_COLL and all possible characters. Patch it to
3371 * add the output to the start. */
3372 if (nfa_calc_size == TRUE)
3373 {
3374 nstate++;
3375 break;
3376 }
3377 e = POP();
3378 s = alloc_state(NFA_END_COLL, NULL, NULL);
3379 if (s == NULL)
3380 goto theend;
3381 patch(e.out, s);
3382 e.start->out1 = s;
3383 PUSH(frag(e.start, list1(&s->out)));
3384 break;
3385
3386 case NFA_RANGE:
3387 /* Before this are two characters, the low and high end of a
3388 * range. Turn them into two states with MIN and MAX. */
3389 if (nfa_calc_size == TRUE)
3390 {
3391 /* nstate += 0; */
3392 break;
3393 }
3394 e2 = POP();
3395 e1 = POP();
3396 e2.start->val = e2.start->c;
3397 e2.start->c = NFA_RANGE_MAX;
3398 e1.start->val = e1.start->c;
3399 e1.start->c = NFA_RANGE_MIN;
3400 patch(e1.out, e2.start);
3401 PUSH(frag(e1.start, e2.out));
3402 break;
3403
Bram Moolenaar699c1202013-09-25 16:41:54 +02003404 case NFA_EMPTY:
3405 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003406 if (nfa_calc_size == TRUE)
3407 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003408 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003409 break;
3410 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003411 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003413 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003414 PUSH(frag(s, list1(&s->out)));
3415 break;
3416
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003417 case NFA_OPT_CHARS:
3418 {
3419 int n;
3420
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003421 /* \%[abc] implemented as:
3422 * NFA_SPLIT
3423 * +-CHAR(a)
3424 * | +-NFA_SPLIT
3425 * | +-CHAR(b)
3426 * | | +-NFA_SPLIT
3427 * | | +-CHAR(c)
3428 * | | | +-next
3429 * | | +- next
3430 * | +- next
3431 * +- next
3432 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003433 n = *++p; /* get number of characters */
3434 if (nfa_calc_size == TRUE)
3435 {
3436 nstate += n;
3437 break;
3438 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003439 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003440 e1.out = NULL; /* stores list with out1's */
3441 s1 = NULL; /* previous NFA_SPLIT to connect to */
3442 while (n-- > 0)
3443 {
3444 e = POP(); /* get character */
3445 s = alloc_state(NFA_SPLIT, e.start, NULL);
3446 if (s == NULL)
3447 goto theend;
3448 if (e1.out == NULL)
3449 e1 = e;
3450 patch(e.out, s1);
3451 append(e1.out, list1(&s->out1));
3452 s1 = s;
3453 }
3454 PUSH(frag(s, e1.out));
3455 break;
3456 }
3457
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003459 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003460 case NFA_PREV_ATOM_JUST_BEFORE:
3461 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003462 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003463 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003464 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3465 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003466 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003467 int start_state;
3468 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003469 int n = 0;
3470 nfa_state_T *zend;
3471 nfa_state_T *skip;
3472
Bram Moolenaardecd9542013-06-07 16:31:50 +02003473 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003474 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003475 case NFA_PREV_ATOM_NO_WIDTH:
3476 start_state = NFA_START_INVISIBLE;
3477 end_state = NFA_END_INVISIBLE;
3478 break;
3479 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3480 start_state = NFA_START_INVISIBLE_NEG;
3481 end_state = NFA_END_INVISIBLE_NEG;
3482 break;
3483 case NFA_PREV_ATOM_JUST_BEFORE:
3484 start_state = NFA_START_INVISIBLE_BEFORE;
3485 end_state = NFA_END_INVISIBLE;
3486 break;
3487 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3488 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3489 end_state = NFA_END_INVISIBLE_NEG;
3490 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003491 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003492 start_state = NFA_START_PATTERN;
3493 end_state = NFA_END_PATTERN;
3494 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003495 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003496
3497 if (before)
3498 n = *++p; /* get the count */
3499
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003500 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003501 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003502 * The \@<= operator: match for the preceding atom.
3503 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003505 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003506
3507 if (nfa_calc_size == TRUE)
3508 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003509 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 break;
3511 }
3512 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003513 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003515 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003516
Bram Moolenaar87953742013-06-05 18:52:40 +02003517 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003519 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003520 if (pattern)
3521 {
3522 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3523 skip = alloc_state(NFA_SKIP, NULL, NULL);
3524 zend = alloc_state(NFA_ZEND, s1, NULL);
3525 s1->out= skip;
3526 patch(e.out, zend);
3527 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003528 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003529 else
3530 {
3531 patch(e.out, s1);
3532 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003533 if (before)
3534 {
3535 if (n <= 0)
3536 /* See if we can guess the maximum width, it avoids a
3537 * lot of pointless tries. */
3538 n = nfa_max_width(e.start, 0);
3539 s->val = n; /* store the count */
3540 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003541 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003543 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003544
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003545#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003546 case NFA_COMPOSING: /* char with composing char */
3547#if 0
3548 /* TODO */
3549 if (regflags & RF_ICOMBINE)
3550 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003551 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003552 }
3553#endif
3554 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003555#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003556
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003557 case NFA_MOPEN: /* \( \) Submatch */
3558 case NFA_MOPEN1:
3559 case NFA_MOPEN2:
3560 case NFA_MOPEN3:
3561 case NFA_MOPEN4:
3562 case NFA_MOPEN5:
3563 case NFA_MOPEN6:
3564 case NFA_MOPEN7:
3565 case NFA_MOPEN8:
3566 case NFA_MOPEN9:
3567#ifdef FEAT_SYN_HL
3568 case NFA_ZOPEN: /* \z( \) Submatch */
3569 case NFA_ZOPEN1:
3570 case NFA_ZOPEN2:
3571 case NFA_ZOPEN3:
3572 case NFA_ZOPEN4:
3573 case NFA_ZOPEN5:
3574 case NFA_ZOPEN6:
3575 case NFA_ZOPEN7:
3576 case NFA_ZOPEN8:
3577 case NFA_ZOPEN9:
3578#endif
3579 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 if (nfa_calc_size == TRUE)
3581 {
3582 nstate += 2;
3583 break;
3584 }
3585
3586 mopen = *p;
3587 switch (*p)
3588 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003589 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3590#ifdef FEAT_SYN_HL
3591 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3592 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3593 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3594 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3595 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3596 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3597 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3598 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3599 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3600 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3601#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003602#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003603 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003604#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003606 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003607 mclose = *p + NSUBEXP;
3608 break;
3609 }
3610
3611 /* Allow "NFA_MOPEN" as a valid postfix representation for
3612 * the empty regexp "". In this case, the NFA will be
3613 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3614 * empty groups of parenthesis, and empty mbyte chars */
3615 if (stackp == stack)
3616 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003617 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003618 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003619 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003620 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003622 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 patch(list1(&s->out), s1);
3624 PUSH(frag(s, list1(&s1->out)));
3625 break;
3626 }
3627
3628 /* At least one node was emitted before NFA_MOPEN, so
3629 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3630 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003631 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003633 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634
Bram Moolenaar525666f2013-06-02 16:40:55 +02003635 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003637 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003638 patch(e.out, s1);
3639
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003640#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003641 if (mopen == NFA_COMPOSING)
3642 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003644#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003645
3646 PUSH(frag(s, list1(&s1->out)));
3647 break;
3648
Bram Moolenaar5714b802013-05-28 22:03:20 +02003649 case NFA_BACKREF1:
3650 case NFA_BACKREF2:
3651 case NFA_BACKREF3:
3652 case NFA_BACKREF4:
3653 case NFA_BACKREF5:
3654 case NFA_BACKREF6:
3655 case NFA_BACKREF7:
3656 case NFA_BACKREF8:
3657 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003658#ifdef FEAT_SYN_HL
3659 case NFA_ZREF1:
3660 case NFA_ZREF2:
3661 case NFA_ZREF3:
3662 case NFA_ZREF4:
3663 case NFA_ZREF5:
3664 case NFA_ZREF6:
3665 case NFA_ZREF7:
3666 case NFA_ZREF8:
3667 case NFA_ZREF9:
3668#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003669 if (nfa_calc_size == TRUE)
3670 {
3671 nstate += 2;
3672 break;
3673 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003674 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003675 if (s == NULL)
3676 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003677 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003678 if (s1 == NULL)
3679 goto theend;
3680 patch(list1(&s->out), s1);
3681 PUSH(frag(s, list1(&s1->out)));
3682 break;
3683
Bram Moolenaar423532e2013-05-29 21:14:42 +02003684 case NFA_LNUM:
3685 case NFA_LNUM_GT:
3686 case NFA_LNUM_LT:
3687 case NFA_VCOL:
3688 case NFA_VCOL_GT:
3689 case NFA_VCOL_LT:
3690 case NFA_COL:
3691 case NFA_COL_GT:
3692 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003693 case NFA_MARK:
3694 case NFA_MARK_GT:
3695 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003696 {
3697 int n = *++p; /* lnum, col or mark name */
3698
Bram Moolenaar423532e2013-05-29 21:14:42 +02003699 if (nfa_calc_size == TRUE)
3700 {
3701 nstate += 1;
3702 break;
3703 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003704 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003705 if (s == NULL)
3706 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003707 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003708 PUSH(frag(s, list1(&s->out)));
3709 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003710 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003711
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712 case NFA_ZSTART:
3713 case NFA_ZEND:
3714 default:
3715 /* Operands */
3716 if (nfa_calc_size == TRUE)
3717 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003718 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003719 break;
3720 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003721 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003723 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 PUSH(frag(s, list1(&s->out)));
3725 break;
3726
3727 } /* switch(*p) */
3728
3729 } /* for(p = postfix; *p; ++p) */
3730
3731 if (nfa_calc_size == TRUE)
3732 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003733 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003734 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003735 }
3736
3737 e = POP();
3738 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003739 {
3740 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741 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 +02003742 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003743
3744 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003745 {
3746 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003748 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750 matchstate = &state_ptr[istate++]; /* the match state */
3751 matchstate->c = NFA_MATCH;
3752 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003753 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754
3755 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003756 ret = e.start;
3757
3758theend:
3759 vim_free(stack);
3760 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003761
3762#undef POP1
3763#undef PUSH1
3764#undef POP2
3765#undef PUSH2
3766#undef POP
3767#undef PUSH
3768}
3769
Bram Moolenaara2947e22013-06-11 22:44:09 +02003770/*
3771 * After building the NFA program, inspect it to add optimization hints.
3772 */
3773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003774nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003775{
3776 int i;
3777 int c;
3778
3779 for (i = 0; i < prog->nstate; ++i)
3780 {
3781 c = prog->state[i].c;
3782 if (c == NFA_START_INVISIBLE
3783 || c == NFA_START_INVISIBLE_NEG
3784 || c == NFA_START_INVISIBLE_BEFORE
3785 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3786 {
3787 int directly;
3788
3789 /* Do it directly when what follows is possibly the end of the
3790 * match. */
3791 if (match_follows(prog->state[i].out1->out, 0))
3792 directly = TRUE;
3793 else
3794 {
3795 int ch_invisible = failure_chance(prog->state[i].out, 0);
3796 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3797
3798 /* Postpone when the invisible match is expensive or has a
3799 * lower chance of failing. */
3800 if (c == NFA_START_INVISIBLE_BEFORE
3801 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3802 {
3803 /* "before" matches are very expensive when
3804 * unbounded, always prefer what follows then,
3805 * unless what follows will always match.
3806 * Otherwise strongly prefer what follows. */
3807 if (prog->state[i].val <= 0 && ch_follows > 0)
3808 directly = FALSE;
3809 else
3810 directly = ch_follows * 10 < ch_invisible;
3811 }
3812 else
3813 {
3814 /* normal invisible, first do the one with the
3815 * highest failure chance */
3816 directly = ch_follows < ch_invisible;
3817 }
3818 }
3819 if (directly)
3820 /* switch to the _FIRST state */
3821 ++prog->state[i].c;
3822 }
3823 }
3824}
3825
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003826/****************************************************************
3827 * NFA execution code.
3828 ****************************************************************/
3829
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003830typedef struct
3831{
3832 int in_use; /* number of subexpr with useful info */
3833
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003834 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003835 union
3836 {
3837 struct multipos
3838 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003839 linenr_T start_lnum;
3840 linenr_T end_lnum;
3841 colnr_T start_col;
3842 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003843 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003844 struct linepos
3845 {
3846 char_u *start;
3847 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003848 } line[NSUBEXP];
3849 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003850} regsub_T;
3851
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003852typedef struct
3853{
3854 regsub_T norm; /* \( .. \) matches */
3855#ifdef FEAT_SYN_HL
3856 regsub_T synt; /* \z( .. \) matches */
3857#endif
3858} regsubs_T;
3859
Bram Moolenaara2d95102013-06-04 14:23:05 +02003860/* nfa_pim_T stores a Postponed Invisible Match. */
3861typedef struct nfa_pim_S nfa_pim_T;
3862struct nfa_pim_S
3863{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003864 int result; /* NFA_PIM_*, see below */
3865 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003866 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003867 union
3868 {
3869 lpos_T pos;
3870 char_u *ptr;
3871 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003872};
3873
3874/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003875#define NFA_PIM_UNUSED 0 /* pim not used */
3876#define NFA_PIM_TODO 1 /* pim not done yet */
3877#define NFA_PIM_MATCH 2 /* pim executed, matches */
3878#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003879
3880
Bram Moolenaar963fee22013-05-26 21:47:28 +02003881/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003882typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003883{
3884 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003885 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003886 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3887 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003888 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003889} nfa_thread_T;
3890
Bram Moolenaar963fee22013-05-26 21:47:28 +02003891/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003892typedef struct
3893{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003894 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003895 int n; /* nr of states currently in "t" */
3896 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003897 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003898 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003899} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900
Bram Moolenaar5714b802013-05-28 22:03:20 +02003901#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003902static void log_subsexpr(regsubs_T *subs);
3903static void log_subexpr(regsub_T *sub);
3904static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003905
3906 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003907log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003908{
3909 log_subexpr(&subs->norm);
3910# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003911 if (nfa_has_zsubexpr)
3912 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003913# endif
3914}
3915
Bram Moolenaar5714b802013-05-28 22:03:20 +02003916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003917log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003918{
3919 int j;
3920
3921 for (j = 0; j < sub->in_use; j++)
3922 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003923 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003924 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003925 sub->list.multi[j].start_col,
3926 (int)sub->list.multi[j].start_lnum,
3927 sub->list.multi[j].end_col,
3928 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003929 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003930 {
3931 char *s = (char *)sub->list.line[j].start;
3932 char *e = (char *)sub->list.line[j].end;
3933
Bram Moolenaar87953742013-06-05 18:52:40 +02003934 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003935 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003936 s == NULL ? "NULL" : s,
3937 e == NULL ? "NULL" : e);
3938 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003939}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003940
3941 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003942pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003943{
3944 static char buf[30];
3945
3946 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3947 buf[0] = NUL;
3948 else
3949 {
3950 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3951 : (int)(pim->end.ptr - reginput));
3952 }
3953 return buf;
3954}
3955
Bram Moolenaar5714b802013-05-28 22:03:20 +02003956#endif
3957
Bram Moolenaar963fee22013-05-26 21:47:28 +02003958/* Used during execution: whether a match has been found. */
3959static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003960#ifdef FEAT_RELTIME
3961static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003962static int *nfa_timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003963static int nfa_time_count;
3964#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003965
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003966static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3967static void clear_sub(regsub_T *sub);
3968static void copy_sub(regsub_T *to, regsub_T *from);
3969static void copy_sub_off(regsub_T *to, regsub_T *from);
3970static void copy_ze_off(regsub_T *to, regsub_T *from);
3971static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3972static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3973static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3974static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3975static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3976static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3977static 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 +02003978
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003979/*
3980 * Copy postponed invisible match info from "from" to "to".
3981 */
3982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003983copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003984{
3985 to->result = from->result;
3986 to->state = from->state;
3987 copy_sub(&to->subs.norm, &from->subs.norm);
3988#ifdef FEAT_SYN_HL
3989 if (nfa_has_zsubexpr)
3990 copy_sub(&to->subs.synt, &from->subs.synt);
3991#endif
3992 to->end = from->end;
3993}
3994
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003995 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003996clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003997{
3998 if (REG_MULTI)
3999 /* Use 0xff to set lnum to -1 */
4000 vim_memset(sub->list.multi, 0xff,
4001 sizeof(struct multipos) * nfa_nsubexpr);
4002 else
4003 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4004 sub->in_use = 0;
4005}
4006
4007/*
4008 * Copy the submatches from "from" to "to".
4009 */
4010 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004011copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004012{
4013 to->in_use = from->in_use;
4014 if (from->in_use > 0)
4015 {
4016 /* Copy the match start and end positions. */
4017 if (REG_MULTI)
4018 mch_memmove(&to->list.multi[0],
4019 &from->list.multi[0],
4020 sizeof(struct multipos) * from->in_use);
4021 else
4022 mch_memmove(&to->list.line[0],
4023 &from->list.line[0],
4024 sizeof(struct linepos) * from->in_use);
4025 }
4026}
4027
4028/*
4029 * Like copy_sub() but exclude the main match.
4030 */
4031 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004032copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004033{
4034 if (to->in_use < from->in_use)
4035 to->in_use = from->in_use;
4036 if (from->in_use > 1)
4037 {
4038 /* Copy the match start and end positions. */
4039 if (REG_MULTI)
4040 mch_memmove(&to->list.multi[1],
4041 &from->list.multi[1],
4042 sizeof(struct multipos) * (from->in_use - 1));
4043 else
4044 mch_memmove(&to->list.line[1],
4045 &from->list.line[1],
4046 sizeof(struct linepos) * (from->in_use - 1));
4047 }
4048}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004049
Bram Moolenaar428e9872013-05-30 17:05:39 +02004050/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004051 * Like copy_sub() but only do the end of the main match if \ze is present.
4052 */
4053 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004054copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004055{
4056 if (nfa_has_zend)
4057 {
4058 if (REG_MULTI)
4059 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004060 if (from->list.multi[0].end_lnum >= 0)
4061 {
4062 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4063 to->list.multi[0].end_col = from->list.multi[0].end_col;
4064 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004065 }
4066 else
4067 {
4068 if (from->list.line[0].end != NULL)
4069 to->list.line[0].end = from->list.line[0].end;
4070 }
4071 }
4072}
4073
4074/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004075 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004076 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004077 */
4078 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004079sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004080{
4081 int i;
4082 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004083 linenr_T s1;
4084 linenr_T s2;
4085 char_u *sp1;
4086 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004087
4088 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4089 if (REG_MULTI)
4090 {
4091 for (i = 0; i < todo; ++i)
4092 {
4093 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004094 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004095 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004096 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004097 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004098 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004099 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004100 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004101 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004102 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004103 if (s1 != -1 && sub1->list.multi[i].start_col
4104 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004105 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004106
4107 if (nfa_has_backref)
4108 {
4109 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004110 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004111 else
4112 s1 = -1;
4113 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004114 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004115 else
4116 s2 = -1;
4117 if (s1 != s2)
4118 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004119 if (s1 != -1 && sub1->list.multi[i].end_col
4120 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004121 return FALSE;
4122 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004123 }
4124 }
4125 else
4126 {
4127 for (i = 0; i < todo; ++i)
4128 {
4129 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004130 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004131 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004132 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004133 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004134 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004135 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004136 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004137 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004138 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004139 if (nfa_has_backref)
4140 {
4141 if (i < sub1->in_use)
4142 sp1 = sub1->list.line[i].end;
4143 else
4144 sp1 = NULL;
4145 if (i < sub2->in_use)
4146 sp2 = sub2->list.line[i].end;
4147 else
4148 sp2 = NULL;
4149 if (sp1 != sp2)
4150 return FALSE;
4151 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004152 }
4153 }
4154
4155 return TRUE;
4156}
4157
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004158#ifdef ENABLE_LOG
4159 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004160report_state(char *action,
4161 regsub_T *sub,
4162 nfa_state_T *state,
4163 int lid,
4164 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004165{
4166 int col;
4167
4168 if (sub->in_use <= 0)
4169 col = -1;
4170 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004171 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004172 else
4173 col = (int)(sub->list.line[0].start - regline);
4174 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004175 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4176 action, abs(state->id), lid, state->c, code, col,
4177 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004178}
4179#endif
4180
Bram Moolenaar43e02982013-06-07 17:31:29 +02004181/*
4182 * Return TRUE if the same state is already in list "l" with the same
4183 * positions as "subs".
4184 */
4185 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004186has_state_with_pos(
4187 nfa_list_T *l, /* runtime state list */
4188 nfa_state_T *state, /* state to update */
4189 regsubs_T *subs, /* pointers to subexpressions */
4190 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004191{
4192 nfa_thread_T *thread;
4193 int i;
4194
4195 for (i = 0; i < l->n; ++i)
4196 {
4197 thread = &l->t[i];
4198 if (thread->state->id == state->id
4199 && sub_equal(&thread->subs.norm, &subs->norm)
4200#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004201 && (!nfa_has_zsubexpr
4202 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004203#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004204 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004205 return TRUE;
4206 }
4207 return FALSE;
4208}
4209
4210/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004211 * Return TRUE if "one" and "two" are equal. That includes when both are not
4212 * set.
4213 */
4214 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004215pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004216{
4217 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4218 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4219
4220 if (one_unused)
4221 /* one is unused: equal when two is also unused */
4222 return two_unused;
4223 if (two_unused)
4224 /* one is used and two is not: not equal */
4225 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004226 /* compare the state id */
4227 if (one->state->id != two->state->id)
4228 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004229 /* compare the position */
4230 if (REG_MULTI)
4231 return one->end.pos.lnum == two->end.pos.lnum
4232 && one->end.pos.col == two->end.pos.col;
4233 return one->end.ptr == two->end.ptr;
4234}
4235
4236/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004237 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4238 */
4239 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004240match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004241{
4242 nfa_state_T *state = startstate;
4243
4244 /* avoid too much recursion */
4245 if (depth > 10)
4246 return FALSE;
4247
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004248 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004249 {
4250 switch (state->c)
4251 {
4252 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004253 case NFA_MCLOSE:
4254 case NFA_END_INVISIBLE:
4255 case NFA_END_INVISIBLE_NEG:
4256 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004257 return TRUE;
4258
4259 case NFA_SPLIT:
4260 return match_follows(state->out, depth + 1)
4261 || match_follows(state->out1, depth + 1);
4262
4263 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004264 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004265 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004266 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004267 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004268 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004269 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004270 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004271 case NFA_COMPOSING:
4272 /* skip ahead to next state */
4273 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004274 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004275
4276 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004277 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004278 case NFA_IDENT:
4279 case NFA_SIDENT:
4280 case NFA_KWORD:
4281 case NFA_SKWORD:
4282 case NFA_FNAME:
4283 case NFA_SFNAME:
4284 case NFA_PRINT:
4285 case NFA_SPRINT:
4286 case NFA_WHITE:
4287 case NFA_NWHITE:
4288 case NFA_DIGIT:
4289 case NFA_NDIGIT:
4290 case NFA_HEX:
4291 case NFA_NHEX:
4292 case NFA_OCTAL:
4293 case NFA_NOCTAL:
4294 case NFA_WORD:
4295 case NFA_NWORD:
4296 case NFA_HEAD:
4297 case NFA_NHEAD:
4298 case NFA_ALPHA:
4299 case NFA_NALPHA:
4300 case NFA_LOWER:
4301 case NFA_NLOWER:
4302 case NFA_UPPER:
4303 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004304 case NFA_LOWER_IC:
4305 case NFA_NLOWER_IC:
4306 case NFA_UPPER_IC:
4307 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004308 case NFA_START_COLL:
4309 case NFA_START_NEG_COLL:
4310 case NFA_NEWL:
4311 /* state will advance input */
4312 return FALSE;
4313
4314 default:
4315 if (state->c > 0)
4316 /* state will advance input */
4317 return FALSE;
4318
4319 /* Others: zero-width or possibly zero-width, might still find
4320 * a match at the same position, keep looking. */
4321 break;
4322 }
4323 state = state->out;
4324 }
4325 return FALSE;
4326}
4327
4328
4329/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004330 * Return TRUE if "state" is already in list "l".
4331 */
4332 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004333state_in_list(
4334 nfa_list_T *l, /* runtime state list */
4335 nfa_state_T *state, /* state to update */
4336 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004337{
4338 if (state->lastlist[nfa_ll_index] == l->id)
4339 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004340 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004341 return TRUE;
4342 }
4343 return FALSE;
4344}
4345
Bram Moolenaar16b35782016-09-09 20:29:50 +02004346/* Offset used for "off" by addstate_here(). */
4347#define ADDSTATE_HERE_OFFSET 10
4348
Bram Moolenaard05bf562013-06-30 23:24:08 +02004349/*
4350 * Add "state" and possibly what follows to state list ".".
4351 * Returns "subs_arg", possibly copied into temp_subs.
4352 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004353 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004354addstate(
4355 nfa_list_T *l, /* runtime state list */
4356 nfa_state_T *state, /* state to update */
4357 regsubs_T *subs_arg, /* pointers to subexpressions */
4358 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004359 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004360{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004361 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004362 int off = off_arg;
4363 int add_here = FALSE;
4364 int listindex = 0;
4365 int k;
4366 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004367 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004368 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004369 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004370 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004371 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004372 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004373 regsubs_T *subs = subs_arg;
4374 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004375#ifdef ENABLE_LOG
4376 int did_print = FALSE;
4377#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004378
Bram Moolenaar16b35782016-09-09 20:29:50 +02004379 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4380 {
4381 add_here = TRUE;
4382 off = 0;
4383 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4384 }
4385
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004386 switch (state->c)
4387 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388 case NFA_NCLOSE:
4389 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004390 case NFA_MCLOSE1:
4391 case NFA_MCLOSE2:
4392 case NFA_MCLOSE3:
4393 case NFA_MCLOSE4:
4394 case NFA_MCLOSE5:
4395 case NFA_MCLOSE6:
4396 case NFA_MCLOSE7:
4397 case NFA_MCLOSE8:
4398 case NFA_MCLOSE9:
4399#ifdef FEAT_SYN_HL
4400 case NFA_ZCLOSE:
4401 case NFA_ZCLOSE1:
4402 case NFA_ZCLOSE2:
4403 case NFA_ZCLOSE3:
4404 case NFA_ZCLOSE4:
4405 case NFA_ZCLOSE5:
4406 case NFA_ZCLOSE6:
4407 case NFA_ZCLOSE7:
4408 case NFA_ZCLOSE8:
4409 case NFA_ZCLOSE9:
4410#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004411 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004412 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004413 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004414 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004415 /* These nodes are not added themselves but their "out" and/or
4416 * "out1" may be added below. */
4417 break;
4418
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004419 case NFA_BOL:
4420 case NFA_BOF:
4421 /* "^" won't match past end-of-line, don't bother trying.
4422 * Except when at the end of the line, or when we are going to the
4423 * next line for a look-behind match. */
4424 if (reginput > regline
4425 && *reginput != NUL
4426 && (nfa_endp == NULL
4427 || !REG_MULTI
4428 || reglnum == nfa_endp->se_u.pos.lnum))
4429 goto skip_add;
4430 /* FALLTHROUGH */
4431
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004432 case NFA_MOPEN1:
4433 case NFA_MOPEN2:
4434 case NFA_MOPEN3:
4435 case NFA_MOPEN4:
4436 case NFA_MOPEN5:
4437 case NFA_MOPEN6:
4438 case NFA_MOPEN7:
4439 case NFA_MOPEN8:
4440 case NFA_MOPEN9:
4441#ifdef FEAT_SYN_HL
4442 case NFA_ZOPEN:
4443 case NFA_ZOPEN1:
4444 case NFA_ZOPEN2:
4445 case NFA_ZOPEN3:
4446 case NFA_ZOPEN4:
4447 case NFA_ZOPEN5:
4448 case NFA_ZOPEN6:
4449 case NFA_ZOPEN7:
4450 case NFA_ZOPEN8:
4451 case NFA_ZOPEN9:
4452#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004453 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004454 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004455 /* These nodes need to be added so that we can bail out when it
4456 * was added to this list before at the same position to avoid an
4457 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004458
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004459 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004460 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004461 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004462 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004463 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004464 * when there is a PIM. For NFA_MATCH check the position,
4465 * lower position is preferred. */
4466 if (!nfa_has_backref && pim == NULL && !l->has_pim
4467 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004468 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004469 /* When called from addstate_here() do insert before
4470 * existing states. */
4471 if (add_here)
4472 {
4473 for (k = 0; k < l->n && k < listindex; ++k)
4474 if (l->t[k].state->id == state->id)
4475 {
4476 found = TRUE;
4477 break;
4478 }
4479 }
4480 if (!add_here || found)
4481 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004482skip_add:
4483#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004484 nfa_set_code(state->c);
4485 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4486 abs(state->id), l->id, state->c, code,
4487 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004488#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004489 return subs;
4490 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004491 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004492
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004493 /* Do not add the state again when it exists with the same
4494 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004495 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004496 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004497 }
4498
Bram Moolenaara0169122013-06-26 18:16:58 +02004499 /* When there are backreferences or PIMs the number of states may
4500 * be (a lot) bigger than anticipated. */
4501 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004502 {
4503 int newlen = l->len * 3 / 2 + 50;
4504
Bram Moolenaard05bf562013-06-30 23:24:08 +02004505 if (subs != &temp_subs)
4506 {
4507 /* "subs" may point into the current array, need to make a
4508 * copy before it becomes invalid. */
4509 copy_sub(&temp_subs.norm, &subs->norm);
4510#ifdef FEAT_SYN_HL
4511 if (nfa_has_zsubexpr)
4512 copy_sub(&temp_subs.synt, &subs->synt);
4513#endif
4514 subs = &temp_subs;
4515 }
4516
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004517 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004518 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4519 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004520 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004521
4522 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004523 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004524 thread = &l->t[l->n++];
4525 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004526 if (pim == NULL)
4527 thread->pim.result = NFA_PIM_UNUSED;
4528 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004529 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004530 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004531 l->has_pim = TRUE;
4532 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004533 copy_sub(&thread->subs.norm, &subs->norm);
4534#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004535 if (nfa_has_zsubexpr)
4536 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004537#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004538#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004539 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004540 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004541#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004542 }
4543
4544#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004545 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004546 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004547#endif
4548 switch (state->c)
4549 {
4550 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004551 break;
4552
4553 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004554 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004555 subs = addstate(l, state->out, subs, pim, off_arg);
4556 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557 break;
4558
Bram Moolenaar699c1202013-09-25 16:41:54 +02004559 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 case NFA_NOPEN:
4561 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004562 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 break;
4564
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004565 case NFA_MOPEN:
4566 case NFA_MOPEN1:
4567 case NFA_MOPEN2:
4568 case NFA_MOPEN3:
4569 case NFA_MOPEN4:
4570 case NFA_MOPEN5:
4571 case NFA_MOPEN6:
4572 case NFA_MOPEN7:
4573 case NFA_MOPEN8:
4574 case NFA_MOPEN9:
4575#ifdef FEAT_SYN_HL
4576 case NFA_ZOPEN:
4577 case NFA_ZOPEN1:
4578 case NFA_ZOPEN2:
4579 case NFA_ZOPEN3:
4580 case NFA_ZOPEN4:
4581 case NFA_ZOPEN5:
4582 case NFA_ZOPEN6:
4583 case NFA_ZOPEN7:
4584 case NFA_ZOPEN8:
4585 case NFA_ZOPEN9:
4586#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004587 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004589 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004590 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004591 sub = &subs->norm;
4592 }
4593#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004594 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004595 {
4596 subidx = state->c - NFA_ZOPEN;
4597 sub = &subs->synt;
4598 }
4599#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004600 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004601 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004602 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004603 sub = &subs->norm;
4604 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004605
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004606 /* avoid compiler warnings */
4607 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004608 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004609
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004610 /* Set the position (with "off" added) in the subexpression. Save
4611 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004612 if (REG_MULTI)
4613 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004614 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004615 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004616 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004617 save_in_use = -1;
4618 }
4619 else
4620 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004621 save_in_use = sub->in_use;
4622 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004623 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004624 sub->list.multi[i].start_lnum = -1;
4625 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004626 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004627 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004628 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004629 if (off == -1)
4630 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004631 sub->list.multi[subidx].start_lnum = reglnum + 1;
4632 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004633 }
4634 else
4635 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004636 sub->list.multi[subidx].start_lnum = reglnum;
4637 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004638 (colnr_T)(reginput - regline + off);
4639 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004640 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004641 }
4642 else
4643 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004644 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004645 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004646 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004647 save_in_use = -1;
4648 }
4649 else
4650 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004651 save_in_use = sub->in_use;
4652 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004653 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004654 sub->list.line[i].start = NULL;
4655 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004656 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004657 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004658 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004659 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004660 }
4661
Bram Moolenaar16b35782016-09-09 20:29:50 +02004662 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004663 /* "subs" may have changed, need to set "sub" again */
4664#ifdef FEAT_SYN_HL
4665 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4666 sub = &subs->synt;
4667 else
4668#endif
4669 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004670
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004671 if (save_in_use == -1)
4672 {
4673 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004674 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004675 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004676 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004677 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004678 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004679 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680 break;
4681
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004682 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004683 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004684 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004685 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004687 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004688 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004689 break;
4690 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004691 case NFA_MCLOSE1:
4692 case NFA_MCLOSE2:
4693 case NFA_MCLOSE3:
4694 case NFA_MCLOSE4:
4695 case NFA_MCLOSE5:
4696 case NFA_MCLOSE6:
4697 case NFA_MCLOSE7:
4698 case NFA_MCLOSE8:
4699 case NFA_MCLOSE9:
4700#ifdef FEAT_SYN_HL
4701 case NFA_ZCLOSE:
4702 case NFA_ZCLOSE1:
4703 case NFA_ZCLOSE2:
4704 case NFA_ZCLOSE3:
4705 case NFA_ZCLOSE4:
4706 case NFA_ZCLOSE5:
4707 case NFA_ZCLOSE6:
4708 case NFA_ZCLOSE7:
4709 case NFA_ZCLOSE8:
4710 case NFA_ZCLOSE9:
4711#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004712 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004713 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004714 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004715 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004716 sub = &subs->norm;
4717 }
4718#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004719 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004720 {
4721 subidx = state->c - NFA_ZCLOSE;
4722 sub = &subs->synt;
4723 }
4724#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004725 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004726 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004727 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004728 sub = &subs->norm;
4729 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004731 /* We don't fill in gaps here, there must have been an MOPEN that
4732 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004733 save_in_use = sub->in_use;
4734 if (sub->in_use <= subidx)
4735 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736 if (REG_MULTI)
4737 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004738 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004739 if (off == -1)
4740 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004741 sub->list.multi[subidx].end_lnum = reglnum + 1;
4742 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004743 }
4744 else
4745 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004746 sub->list.multi[subidx].end_lnum = reglnum;
4747 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004748 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004749 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004750 /* avoid compiler warnings */
4751 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752 }
4753 else
4754 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004755 save_ptr = sub->list.line[subidx].end;
4756 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004757 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004758 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004759 }
4760
Bram Moolenaar16b35782016-09-09 20:29:50 +02004761 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004762 /* "subs" may have changed, need to set "sub" again */
4763#ifdef FEAT_SYN_HL
4764 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4765 sub = &subs->synt;
4766 else
4767#endif
4768 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004769
4770 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004771 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004772 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004773 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004774 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004775 break;
4776 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004777 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778}
4779
4780/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004781 * Like addstate(), but the new state(s) are put at position "*ip".
4782 * Used for zero-width matches, next state to use is the added one.
4783 * This makes sure the order of states to be tried does not change, which
4784 * matters for alternatives.
4785 */
4786 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004787addstate_here(
4788 nfa_list_T *l, /* runtime state list */
4789 nfa_state_T *state, /* state to update */
4790 regsubs_T *subs, /* pointers to subexpressions */
4791 nfa_pim_T *pim, /* postponed look-behind match */
4792 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004793{
4794 int tlen = l->n;
4795 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004796 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004797
Bram Moolenaar16b35782016-09-09 20:29:50 +02004798 /* First add the state(s) at the end, so that we know how many there are.
4799 * Pass the listidx as offset (avoids adding another argument to
4800 * addstate(). */
4801 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004802
Bram Moolenaar4b417062013-05-25 20:19:50 +02004803 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004804 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004805 return;
4806
4807 /* re-order to put the new state at the current position */
4808 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004809 if (count == 0)
4810 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004811 if (count == 1)
4812 {
4813 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004814 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004815 }
4816 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004817 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004818 if (l->n + count - 1 >= l->len)
4819 {
4820 /* not enough space to move the new states, reallocate the list
4821 * and move the states to the right position */
4822 nfa_thread_T *newl;
4823
4824 l->len = l->len * 3 / 2 + 50;
4825 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4826 if (newl == NULL)
4827 return;
4828 mch_memmove(&(newl[0]),
4829 &(l->t[0]),
4830 sizeof(nfa_thread_T) * listidx);
4831 mch_memmove(&(newl[listidx]),
4832 &(l->t[l->n - count]),
4833 sizeof(nfa_thread_T) * count);
4834 mch_memmove(&(newl[listidx + count]),
4835 &(l->t[listidx + 1]),
4836 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4837 vim_free(l->t);
4838 l->t = newl;
4839 }
4840 else
4841 {
4842 /* make space for new states, then move them from the
4843 * end to the current position */
4844 mch_memmove(&(l->t[listidx + count]),
4845 &(l->t[listidx + 1]),
4846 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4847 mch_memmove(&(l->t[listidx]),
4848 &(l->t[l->n - 1]),
4849 sizeof(nfa_thread_T) * count);
4850 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004851 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004852 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004853 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004854}
4855
4856/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004857 * Check character class "class" against current character c.
4858 */
4859 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004860check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004861{
4862 switch (class)
4863 {
4864 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004865 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866 return OK;
4867 break;
4868 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004869 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004870 return OK;
4871 break;
4872 case NFA_CLASS_BLANK:
4873 if (c == ' ' || c == '\t')
4874 return OK;
4875 break;
4876 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004877 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878 return OK;
4879 break;
4880 case NFA_CLASS_DIGIT:
4881 if (VIM_ISDIGIT(c))
4882 return OK;
4883 break;
4884 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004885 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004886 return OK;
4887 break;
4888 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004889 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004890 return OK;
4891 break;
4892 case NFA_CLASS_PRINT:
4893 if (vim_isprintc(c))
4894 return OK;
4895 break;
4896 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004897 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004898 return OK;
4899 break;
4900 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004901 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004902 return OK;
4903 break;
4904 case NFA_CLASS_UPPER:
4905 if (MB_ISUPPER(c))
4906 return OK;
4907 break;
4908 case NFA_CLASS_XDIGIT:
4909 if (vim_isxdigit(c))
4910 return OK;
4911 break;
4912 case NFA_CLASS_TAB:
4913 if (c == '\t')
4914 return OK;
4915 break;
4916 case NFA_CLASS_RETURN:
4917 if (c == '\r')
4918 return OK;
4919 break;
4920 case NFA_CLASS_BACKSPACE:
4921 if (c == '\b')
4922 return OK;
4923 break;
4924 case NFA_CLASS_ESCAPE:
4925 if (c == '\033')
4926 return OK;
4927 break;
4928
4929 default:
4930 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004931 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004932 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004933 }
4934 return FAIL;
4935}
4936
Bram Moolenaar5714b802013-05-28 22:03:20 +02004937/*
4938 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004939 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004940 */
4941 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004942match_backref(
4943 regsub_T *sub, /* pointers to subexpressions */
4944 int subidx,
4945 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004946{
4947 int len;
4948
4949 if (sub->in_use <= subidx)
4950 {
4951retempty:
4952 /* backref was not set, match an empty string */
4953 *bytelen = 0;
4954 return TRUE;
4955 }
4956
4957 if (REG_MULTI)
4958 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004959 if (sub->list.multi[subidx].start_lnum < 0
4960 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004961 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004962 if (sub->list.multi[subidx].start_lnum == reglnum
4963 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004964 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004965 len = sub->list.multi[subidx].end_col
4966 - sub->list.multi[subidx].start_col;
4967 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004968 reginput, &len) == 0)
4969 {
4970 *bytelen = len;
4971 return TRUE;
4972 }
4973 }
4974 else
4975 {
4976 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004977 sub->list.multi[subidx].start_lnum,
4978 sub->list.multi[subidx].start_col,
4979 sub->list.multi[subidx].end_lnum,
4980 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004981 bytelen) == RA_MATCH)
4982 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004983 }
4984 }
4985 else
4986 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004987 if (sub->list.line[subidx].start == NULL
4988 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004989 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004990 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4991 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004992 {
4993 *bytelen = len;
4994 return TRUE;
4995 }
4996 }
4997 return FALSE;
4998}
4999
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005000#ifdef FEAT_SYN_HL
5001
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005002static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005003
5004/*
5005 * Check for a match with \z subexpression "subidx".
5006 * Return TRUE if it matches.
5007 */
5008 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005009match_zref(
5010 int subidx,
5011 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005012{
5013 int len;
5014
5015 cleanup_zsubexpr();
5016 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5017 {
5018 /* backref was not set, match an empty string */
5019 *bytelen = 0;
5020 return TRUE;
5021 }
5022
5023 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
5024 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5025 {
5026 *bytelen = len;
5027 return TRUE;
5028 }
5029 return FALSE;
5030}
5031#endif
5032
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005033/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005034 * Save list IDs for all NFA states of "prog" into "list".
5035 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005036 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005037 */
5038 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005039nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005040{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005041 int i;
5042 nfa_state_T *p;
5043
5044 /* Order in the list is reverse, it's a bit faster that way. */
5045 p = &prog->state[0];
5046 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005047 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005048 list[i] = p->lastlist[1];
5049 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005050 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051 }
5052}
5053
5054/*
5055 * Restore list IDs from "list" to all NFA states.
5056 */
5057 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005058nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005059{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005060 int i;
5061 nfa_state_T *p;
5062
5063 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 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005067 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068 }
5069}
5070
Bram Moolenaar423532e2013-05-29 21:14:42 +02005071 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005072nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005073{
5074 if (op == 1) return pos > val;
5075 if (op == 2) return pos < val;
5076 return val == pos;
5077}
5078
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005079static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5080static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005081
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005082/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005083 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005084 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5085 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005086 */
5087 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005088recursive_regmatch(
5089 nfa_state_T *state,
5090 nfa_pim_T *pim,
5091 nfa_regprog_T *prog,
5092 regsubs_T *submatch,
5093 regsubs_T *m,
5094 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005095{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005096 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097 int save_reglnum = reglnum;
5098 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005099 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005100 save_se_T *save_nfa_endp = nfa_endp;
5101 save_se_T endpos;
5102 save_se_T *endposp = NULL;
5103 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005104 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005105
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005106 if (pim != NULL)
5107 {
5108 /* start at the position where the postponed match was */
5109 if (REG_MULTI)
5110 reginput = regline + pim->end.pos.col;
5111 else
5112 reginput = pim->end.ptr;
5113 }
5114
Bram Moolenaardecd9542013-06-07 16:31:50 +02005115 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005116 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5117 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5118 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005119 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005120 /* The recursive match must end at the current position. When "pim" is
5121 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005122 endposp = &endpos;
5123 if (REG_MULTI)
5124 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005125 if (pim == NULL)
5126 {
5127 endpos.se_u.pos.col = (int)(reginput - regline);
5128 endpos.se_u.pos.lnum = reglnum;
5129 }
5130 else
5131 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005132 }
5133 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005134 {
5135 if (pim == NULL)
5136 endpos.se_u.ptr = reginput;
5137 else
5138 endpos.se_u.ptr = pim->end.ptr;
5139 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005140
5141 /* Go back the specified number of bytes, or as far as the
5142 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005143 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005144 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005145 if (state->val <= 0)
5146 {
5147 if (REG_MULTI)
5148 {
5149 regline = reg_getline(--reglnum);
5150 if (regline == NULL)
5151 /* can't go before the first line */
5152 regline = reg_getline(++reglnum);
5153 }
5154 reginput = regline;
5155 }
5156 else
5157 {
5158 if (REG_MULTI && (int)(reginput - regline) < state->val)
5159 {
5160 /* Not enough bytes in this line, go to end of
5161 * previous line. */
5162 regline = reg_getline(--reglnum);
5163 if (regline == NULL)
5164 {
5165 /* can't go before the first line */
5166 regline = reg_getline(++reglnum);
5167 reginput = regline;
5168 }
5169 else
5170 reginput = regline + STRLEN(regline);
5171 }
5172 if ((int)(reginput - regline) >= state->val)
5173 {
5174 reginput -= state->val;
5175#ifdef FEAT_MBYTE
5176 if (has_mbyte)
5177 reginput -= mb_head_off(regline, reginput);
5178#endif
5179 }
5180 else
5181 reginput = regline;
5182 }
5183 }
5184
Bram Moolenaarf46da702013-06-02 22:37:42 +02005185#ifdef ENABLE_LOG
5186 if (log_fd != stderr)
5187 fclose(log_fd);
5188 log_fd = NULL;
5189#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005190 /* Have to clear the lastlist field of the NFA nodes, so that
5191 * nfa_regmatch() and addstate() can run properly after recursion. */
5192 if (nfa_ll_index == 1)
5193 {
5194 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5195 * values and clear them. */
5196 if (*listids == NULL)
5197 {
5198 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5199 if (*listids == NULL)
5200 {
5201 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5202 return 0;
5203 }
5204 }
5205 nfa_save_listids(prog, *listids);
5206 need_restore = TRUE;
5207 /* any value of nfa_listid will do */
5208 }
5209 else
5210 {
5211 /* First recursive nfa_regmatch() call, switch to the second lastlist
5212 * entry. Make sure nfa_listid is different from a previous recursive
5213 * call, because some states may still have this ID. */
5214 ++nfa_ll_index;
5215 if (nfa_listid <= nfa_alt_listid)
5216 nfa_listid = nfa_alt_listid;
5217 }
5218
5219 /* Call nfa_regmatch() to check if the current concat matches at this
5220 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005221 nfa_endp = endposp;
5222 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005223
5224 if (need_restore)
5225 nfa_restore_listids(prog, *listids);
5226 else
5227 {
5228 --nfa_ll_index;
5229 nfa_alt_listid = nfa_listid;
5230 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005231
5232 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005233 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005234 if (REG_MULTI)
5235 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005236 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005237 if (result != NFA_TOO_EXPENSIVE)
5238 {
5239 nfa_match = save_nfa_match;
5240 nfa_listid = save_nfa_listid;
5241 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005242 nfa_endp = save_nfa_endp;
5243
5244#ifdef ENABLE_LOG
5245 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5246 if (log_fd != NULL)
5247 {
5248 fprintf(log_fd, "****************************\n");
5249 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5250 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5251 fprintf(log_fd, "****************************\n");
5252 }
5253 else
5254 {
5255 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5256 log_fd = stderr;
5257 }
5258#endif
5259
5260 return result;
5261}
5262
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005263static int skip_to_start(int c, colnr_T *colp);
5264static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005265
5266/*
5267 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005268 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005269 * NFA_ANY: 1
5270 * specific character: 99
5271 */
5272 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005273failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005274{
5275 int c = state->c;
5276 int l, r;
5277
5278 /* detect looping */
5279 if (depth > 4)
5280 return 1;
5281
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005282 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005283 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005284 case NFA_SPLIT:
5285 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5286 /* avoid recursive stuff */
5287 return 1;
5288 /* two alternatives, use the lowest failure chance */
5289 l = failure_chance(state->out, depth + 1);
5290 r = failure_chance(state->out1, depth + 1);
5291 return l < r ? l : r;
5292
5293 case NFA_ANY:
5294 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005295 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005296
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005297 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005298 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005299 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005300 /* empty match works always */
5301 return 0;
5302
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005303 case NFA_START_INVISIBLE:
5304 case NFA_START_INVISIBLE_FIRST:
5305 case NFA_START_INVISIBLE_NEG:
5306 case NFA_START_INVISIBLE_NEG_FIRST:
5307 case NFA_START_INVISIBLE_BEFORE:
5308 case NFA_START_INVISIBLE_BEFORE_FIRST:
5309 case NFA_START_INVISIBLE_BEFORE_NEG:
5310 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5311 case NFA_START_PATTERN:
5312 /* recursive regmatch is expensive, use low failure chance */
5313 return 5;
5314
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005315 case NFA_BOL:
5316 case NFA_EOL:
5317 case NFA_BOF:
5318 case NFA_EOF:
5319 case NFA_NEWL:
5320 return 99;
5321
5322 case NFA_BOW:
5323 case NFA_EOW:
5324 return 90;
5325
5326 case NFA_MOPEN:
5327 case NFA_MOPEN1:
5328 case NFA_MOPEN2:
5329 case NFA_MOPEN3:
5330 case NFA_MOPEN4:
5331 case NFA_MOPEN5:
5332 case NFA_MOPEN6:
5333 case NFA_MOPEN7:
5334 case NFA_MOPEN8:
5335 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005336#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005337 case NFA_ZOPEN:
5338 case NFA_ZOPEN1:
5339 case NFA_ZOPEN2:
5340 case NFA_ZOPEN3:
5341 case NFA_ZOPEN4:
5342 case NFA_ZOPEN5:
5343 case NFA_ZOPEN6:
5344 case NFA_ZOPEN7:
5345 case NFA_ZOPEN8:
5346 case NFA_ZOPEN9:
5347 case NFA_ZCLOSE:
5348 case NFA_ZCLOSE1:
5349 case NFA_ZCLOSE2:
5350 case NFA_ZCLOSE3:
5351 case NFA_ZCLOSE4:
5352 case NFA_ZCLOSE5:
5353 case NFA_ZCLOSE6:
5354 case NFA_ZCLOSE7:
5355 case NFA_ZCLOSE8:
5356 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005357#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005358 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005359 case NFA_MCLOSE1:
5360 case NFA_MCLOSE2:
5361 case NFA_MCLOSE3:
5362 case NFA_MCLOSE4:
5363 case NFA_MCLOSE5:
5364 case NFA_MCLOSE6:
5365 case NFA_MCLOSE7:
5366 case NFA_MCLOSE8:
5367 case NFA_MCLOSE9:
5368 case NFA_NCLOSE:
5369 return failure_chance(state->out, depth + 1);
5370
5371 case NFA_BACKREF1:
5372 case NFA_BACKREF2:
5373 case NFA_BACKREF3:
5374 case NFA_BACKREF4:
5375 case NFA_BACKREF5:
5376 case NFA_BACKREF6:
5377 case NFA_BACKREF7:
5378 case NFA_BACKREF8:
5379 case NFA_BACKREF9:
5380#ifdef FEAT_SYN_HL
5381 case NFA_ZREF1:
5382 case NFA_ZREF2:
5383 case NFA_ZREF3:
5384 case NFA_ZREF4:
5385 case NFA_ZREF5:
5386 case NFA_ZREF6:
5387 case NFA_ZREF7:
5388 case NFA_ZREF8:
5389 case NFA_ZREF9:
5390#endif
5391 /* backreferences don't match in many places */
5392 return 94;
5393
5394 case NFA_LNUM_GT:
5395 case NFA_LNUM_LT:
5396 case NFA_COL_GT:
5397 case NFA_COL_LT:
5398 case NFA_VCOL_GT:
5399 case NFA_VCOL_LT:
5400 case NFA_MARK_GT:
5401 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005402 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005403 /* before/after positions don't match very often */
5404 return 85;
5405
5406 case NFA_LNUM:
5407 return 90;
5408
5409 case NFA_CURSOR:
5410 case NFA_COL:
5411 case NFA_VCOL:
5412 case NFA_MARK:
5413 /* specific positions rarely match */
5414 return 98;
5415
5416 case NFA_COMPOSING:
5417 return 95;
5418
5419 default:
5420 if (c > 0)
5421 /* character match fails often */
5422 return 95;
5423 }
5424
5425 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005426 return 50;
5427}
5428
Bram Moolenaarf46da702013-06-02 22:37:42 +02005429/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005430 * Skip until the char "c" we know a match must start with.
5431 */
5432 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005433skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005434{
5435 char_u *s;
5436
5437 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005438 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005439#ifdef FEAT_MBYTE
5440 && !has_mbyte
5441#endif
5442 )
5443 s = vim_strbyte(regline + *colp, c);
5444 else
5445 s = cstrchr(regline + *colp, c);
5446 if (s == NULL)
5447 return FAIL;
5448 *colp = (int)(s - regline);
5449 return OK;
5450}
5451
5452/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005453 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005454 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005455 * Returns zero for no match, 1 for a match.
5456 */
5457 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005458find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005459{
5460 colnr_T col = startcol;
5461 int c1, c2;
5462 int len1, len2;
5463 int match;
5464
5465 for (;;)
5466 {
5467 match = TRUE;
5468 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5469 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5470 {
5471 c1 = PTR2CHAR(match_text + len1);
5472 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005473 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005474 {
5475 match = FALSE;
5476 break;
5477 }
5478 len2 += MB_CHAR2LEN(c2);
5479 }
5480 if (match
5481#ifdef FEAT_MBYTE
5482 /* check that no composing char follows */
5483 && !(enc_utf8
5484 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5485#endif
5486 )
5487 {
5488 cleanup_subexpr();
5489 if (REG_MULTI)
5490 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005491 rex.reg_startpos[0].lnum = reglnum;
5492 rex.reg_startpos[0].col = col;
5493 rex.reg_endpos[0].lnum = reglnum;
5494 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005495 }
5496 else
5497 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005498 rex.reg_startp[0] = regline + col;
5499 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005500 }
5501 return 1L;
5502 }
5503
5504 /* Try finding regstart after the current match. */
5505 col += MB_CHAR2LEN(regstart); /* skip regstart */
5506 if (skip_to_start(regstart, &col) == FAIL)
5507 break;
5508 }
5509 return 0L;
5510}
5511
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005512#ifdef FEAT_RELTIME
5513 static int
5514nfa_did_time_out()
5515{
5516 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5517 {
5518 if (nfa_timed_out != NULL)
5519 *nfa_timed_out = TRUE;
5520 return TRUE;
5521 }
5522 return FALSE;
5523}
5524#endif
5525
Bram Moolenaar473de612013-06-08 18:19:48 +02005526/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005527 * Main matching routine.
5528 *
5529 * Run NFA to determine whether it matches reginput.
5530 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005531 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005532 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005534 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005535 * Note: Caller must ensure that: start != NULL.
5536 */
5537 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005538nfa_regmatch(
5539 nfa_regprog_T *prog,
5540 nfa_state_T *start,
5541 regsubs_T *submatch,
5542 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005543{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005544 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005545 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005547 int go_to_nextline = FALSE;
5548 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005549 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005550 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005551 nfa_list_T *thislist;
5552 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005554 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005555 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005556 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005557 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005558 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005559#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005560 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005561#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005562
Bram Moolenaar41f12052013-08-25 17:01:42 +02005563 /* Some patterns may take a long time to match, especially when using
5564 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5565 fast_breakcheck();
5566 if (got_int)
5567 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005568#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005569 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005570 return FALSE;
5571#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005572
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005573#ifdef NFA_REGEXP_DEBUG_LOG
5574 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5575 if (debug == NULL)
5576 {
5577 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
5578 return FALSE;
5579 }
5580#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005581 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005583 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005584 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005585
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005586 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005587 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005588 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005589 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005590 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005592
5593#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005594 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005595 if (log_fd != NULL)
5596 {
5597 fprintf(log_fd, "**********************************\n");
5598 nfa_set_code(start->c);
5599 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5600 abs(start->id), code);
5601 fprintf(log_fd, "**********************************\n");
5602 }
5603 else
5604 {
5605 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5606 log_fd = stderr;
5607 }
5608#endif
5609
5610 thislist = &list[0];
5611 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005612 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005613 nextlist = &list[1];
5614 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005615 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005616#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005617 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005618#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005619 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005620
5621 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5622 * it's the first MOPEN. */
5623 if (toplevel)
5624 {
5625 if (REG_MULTI)
5626 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005627 m->norm.list.multi[0].start_lnum = reglnum;
5628 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005629 }
5630 else
5631 m->norm.list.line[0].start = reginput;
5632 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005633 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005634 }
5635 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005636 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005638#define ADD_STATE_IF_MATCH(state) \
5639 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005640 add_state = state->out; \
5641 add_off = clen; \
5642 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005643
5644 /*
5645 * Run for each character.
5646 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005647 for (;;)
5648 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005649 int curc;
5650 int clen;
5651
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005652#ifdef FEAT_MBYTE
5653 if (has_mbyte)
5654 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005655 curc = (*mb_ptr2char)(reginput);
5656 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005657 }
5658 else
5659#endif
5660 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005661 curc = *reginput;
5662 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005663 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005664 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005665 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005666 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005667 go_to_nextline = FALSE;
5668 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005669
5670 /* swap lists */
5671 thislist = &list[flag];
5672 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005673 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005674 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005675 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005676 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5677 {
5678 /* too many states, retry with old engine */
5679 nfa_match = NFA_TOO_EXPENSIVE;
5680 goto theend;
5681 }
5682
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005683 thislist->id = nfa_listid;
5684 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005685
5686#ifdef ENABLE_LOG
5687 fprintf(log_fd, "------------------------------------------\n");
5688 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005689 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005690 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005691 {
5692 int i;
5693
5694 for (i = 0; i < thislist->n; i++)
5695 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5696 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005697 fprintf(log_fd, "\n");
5698#endif
5699
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005700#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005701 fprintf(debug, "\n-------------------\n");
5702#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005703 /*
5704 * If the state lists are empty we can stop.
5705 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005706 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005707 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005708
5709 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005710 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005711 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005712 /* If the list gets very long there probably is something wrong.
5713 * At least allow interrupting with CTRL-C. */
5714 fast_breakcheck();
5715 if (got_int)
5716 break;
5717#ifdef FEAT_RELTIME
5718 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5719 {
5720 nfa_time_count = 0;
5721 if (nfa_did_time_out())
5722 break;
5723 }
5724#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005725 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005726
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005727#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005728 nfa_set_code(t->state->c);
5729 fprintf(debug, "%s, ", code);
5730#endif
5731#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005732 {
5733 int col;
5734
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005735 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005736 col = -1;
5737 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005738 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005739 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005740 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005741 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005742 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5743 abs(t->state->id), (int)t->state->c, code, col,
5744 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005745 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005746#endif
5747
5748 /*
5749 * Handle the possible codes of the current state.
5750 * The most important is NFA_MATCH.
5751 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005752 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005753 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005754 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005755 switch (t->state->c)
5756 {
5757 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005758 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005759#ifdef FEAT_MBYTE
5760 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005761 * rex.reg_icombine is not set, that is not really a match. */
5762 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005763 break;
5764#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005765 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005766 copy_sub(&submatch->norm, &t->subs.norm);
5767#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005768 if (nfa_has_zsubexpr)
5769 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005770#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005771#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005772 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005773#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005774 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005775 * states at this position. When the list of states is going
5776 * to be empty quit without advancing, so that "reginput" is
5777 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005778 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005779 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005780 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005781 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005782
5783 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005784 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005785 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005786 /*
5787 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005788 * NFA_START_INVISIBLE_BEFORE node.
5789 * They surround a zero-width group, used with "\@=", "\&",
5790 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005791 * If we got here, it means that the current "invisible" group
5792 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005793 * nfa_regmatch(). For a look-behind match only when it ends
5794 * in the position in "nfa_endp".
5795 * Submatches are stored in *m, and used in the parent call.
5796 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005797#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005798 if (nfa_endp != NULL)
5799 {
5800 if (REG_MULTI)
5801 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5802 (int)reglnum,
5803 (int)nfa_endp->se_u.pos.lnum,
5804 (int)(reginput - regline),
5805 nfa_endp->se_u.pos.col);
5806 else
5807 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5808 (int)(reginput - regline),
5809 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005810 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005811#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005812 /* If "nfa_endp" is set it's only a match if it ends at
5813 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005814 if (nfa_endp != NULL && (REG_MULTI
5815 ? (reglnum != nfa_endp->se_u.pos.lnum
5816 || (int)(reginput - regline)
5817 != nfa_endp->se_u.pos.col)
5818 : reginput != nfa_endp->se_u.ptr))
5819 break;
5820
5821 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005822 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005823 {
5824 copy_sub(&m->norm, &t->subs.norm);
5825#ifdef FEAT_SYN_HL
5826 if (nfa_has_zsubexpr)
5827 copy_sub(&m->synt, &t->subs.synt);
5828#endif
5829 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005830#ifdef ENABLE_LOG
5831 fprintf(log_fd, "Match found:\n");
5832 log_subsexpr(m);
5833#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005834 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005835 /* See comment above at "goto nextchar". */
5836 if (nextlist->n == 0)
5837 clen = 0;
5838 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005839
5840 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005841 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005842 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005843 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005844 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005845 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005846 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005847 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005848 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005849#ifdef ENABLE_LOG
5850 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5851 failure_chance(t->state->out, 0),
5852 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005853#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005854 /* Do it directly if there already is a PIM or when
5855 * nfa_postprocess() detected it will work better. */
5856 if (t->pim.result != NFA_PIM_UNUSED
5857 || t->state->c == NFA_START_INVISIBLE_FIRST
5858 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5859 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5860 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005861 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005862 int in_use = m->norm.in_use;
5863
Bram Moolenaar699c1202013-09-25 16:41:54 +02005864 /* Copy submatch info for the recursive call, opposite
5865 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005866 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005867#ifdef FEAT_SYN_HL
5868 if (nfa_has_zsubexpr)
5869 copy_sub_off(&m->synt, &t->subs.synt);
5870#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005871
Bram Moolenaara2d95102013-06-04 14:23:05 +02005872 /*
5873 * First try matching the invisible match, then what
5874 * follows.
5875 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005876 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005877 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005878 if (result == NFA_TOO_EXPENSIVE)
5879 {
5880 nfa_match = result;
5881 goto theend;
5882 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005883
Bram Moolenaardecd9542013-06-07 16:31:50 +02005884 /* for \@! and \@<! it is a match when the result is
5885 * FALSE */
5886 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005887 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5888 || t->state->c
5889 == NFA_START_INVISIBLE_BEFORE_NEG
5890 || t->state->c
5891 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005892 {
5893 /* Copy submatch info from the recursive call */
5894 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005895#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005896 if (nfa_has_zsubexpr)
5897 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005898#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005899 /* If the pattern has \ze and it matched in the
5900 * sub pattern, use it. */
5901 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005902
Bram Moolenaara2d95102013-06-04 14:23:05 +02005903 /* t->state->out1 is the corresponding
5904 * END_INVISIBLE node; Add its out to the current
5905 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005906 add_here = TRUE;
5907 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005908 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005909 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005910 }
5911 else
5912 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005913 nfa_pim_T pim;
5914
Bram Moolenaara2d95102013-06-04 14:23:05 +02005915 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005916 * First try matching what follows. Only if a match
5917 * is found verify the invisible match matches. Add a
5918 * nfa_pim_T to the following states, it contains info
5919 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005920 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005921 pim.state = t->state;
5922 pim.result = NFA_PIM_TODO;
5923 pim.subs.norm.in_use = 0;
5924#ifdef FEAT_SYN_HL
5925 pim.subs.synt.in_use = 0;
5926#endif
5927 if (REG_MULTI)
5928 {
5929 pim.end.pos.col = (int)(reginput - regline);
5930 pim.end.pos.lnum = reglnum;
5931 }
5932 else
5933 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005934
5935 /* t->state->out1 is the corresponding END_INVISIBLE
5936 * node; Add its out to the current list (zero-width
5937 * match). */
5938 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005939 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005940 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005941 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005942 break;
5943
Bram Moolenaar87953742013-06-05 18:52:40 +02005944 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005945 {
5946 nfa_state_T *skip = NULL;
5947#ifdef ENABLE_LOG
5948 int skip_lid = 0;
5949#endif
5950
5951 /* There is no point in trying to match the pattern if the
5952 * output state is not going to be added to the list. */
5953 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5954 {
5955 skip = t->state->out1->out;
5956#ifdef ENABLE_LOG
5957 skip_lid = nextlist->id;
5958#endif
5959 }
5960 else if (state_in_list(nextlist,
5961 t->state->out1->out->out, &t->subs))
5962 {
5963 skip = t->state->out1->out->out;
5964#ifdef ENABLE_LOG
5965 skip_lid = nextlist->id;
5966#endif
5967 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005968 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005969 t->state->out1->out->out, &t->subs))
5970 {
5971 skip = t->state->out1->out->out;
5972#ifdef ENABLE_LOG
5973 skip_lid = thislist->id;
5974#endif
5975 }
5976 if (skip != NULL)
5977 {
5978#ifdef ENABLE_LOG
5979 nfa_set_code(skip->c);
5980 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5981 abs(skip->id), skip_lid, skip->c, code);
5982#endif
5983 break;
5984 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005985 /* Copy submatch info to the recursive call, opposite of what
5986 * happens afterwards. */
5987 copy_sub_off(&m->norm, &t->subs.norm);
5988#ifdef FEAT_SYN_HL
5989 if (nfa_has_zsubexpr)
5990 copy_sub_off(&m->synt, &t->subs.synt);
5991#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005992
Bram Moolenaar87953742013-06-05 18:52:40 +02005993 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005994 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005995 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005996 if (result == NFA_TOO_EXPENSIVE)
5997 {
5998 nfa_match = result;
5999 goto theend;
6000 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006001 if (result)
6002 {
6003 int bytelen;
6004
6005#ifdef ENABLE_LOG
6006 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6007 log_subsexpr(m);
6008#endif
6009 /* Copy submatch info from the recursive call */
6010 copy_sub_off(&t->subs.norm, &m->norm);
6011#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006012 if (nfa_has_zsubexpr)
6013 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006014#endif
6015 /* Now we need to skip over the matched text and then
6016 * continue with what follows. */
6017 if (REG_MULTI)
6018 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006019 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02006020 - (int)(reginput - regline);
6021 else
6022 bytelen = (int)(m->norm.list.line[0].end - reginput);
6023
6024#ifdef ENABLE_LOG
6025 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6026#endif
6027 if (bytelen == 0)
6028 {
6029 /* empty match, output of corresponding
6030 * NFA_END_PATTERN/NFA_SKIP to be used at current
6031 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006032 add_here = TRUE;
6033 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006034 }
6035 else if (bytelen <= clen)
6036 {
6037 /* match current character, output of corresponding
6038 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006039 add_state = t->state->out1->out->out;
6040 add_off = clen;
6041 }
6042 else
6043 {
6044 /* skip over the matched characters, set character
6045 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006046 add_state = t->state->out1->out;
6047 add_off = bytelen;
6048 add_count = bytelen - clen;
6049 }
6050 }
6051 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006052 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006053
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054 case NFA_BOL:
6055 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006056 {
6057 add_here = TRUE;
6058 add_state = t->state->out;
6059 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006060 break;
6061
6062 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006063 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006064 {
6065 add_here = TRUE;
6066 add_state = t->state->out;
6067 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006068 break;
6069
6070 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006071 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006072
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006073 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006074 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006075#ifdef FEAT_MBYTE
6076 else if (has_mbyte)
6077 {
6078 int this_class;
6079
6080 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006081 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006082 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006083 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006084 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006085 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006086 }
6087#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006088 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006089 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006090 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006091 result = FALSE;
6092 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006093 {
6094 add_here = TRUE;
6095 add_state = t->state->out;
6096 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006097 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006098
6099 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006100 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006101 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006102 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006103#ifdef FEAT_MBYTE
6104 else if (has_mbyte)
6105 {
6106 int this_class, prev_class;
6107
6108 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006109 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006110 prev_class = reg_prev_class();
6111 if (this_class == prev_class
6112 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006113 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006114 }
6115#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006116 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006117 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006118 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006119 result = FALSE;
6120 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006121 {
6122 add_here = TRUE;
6123 add_state = t->state->out;
6124 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006125 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006126
Bram Moolenaar4b780632013-05-31 22:14:52 +02006127 case NFA_BOF:
6128 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006129 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006130 {
6131 add_here = TRUE;
6132 add_state = t->state->out;
6133 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006134 break;
6135
6136 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006137 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006138 {
6139 add_here = TRUE;
6140 add_state = t->state->out;
6141 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006142 break;
6143
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006144#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006145 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006146 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006147 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006148 int len = 0;
6149 nfa_state_T *end;
6150 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006151 int cchars[MAX_MCO];
6152 int ccount = 0;
6153 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006154
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006155 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006156 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006157 if (utf_iscomposing(sta->c))
6158 {
6159 /* Only match composing character(s), ignore base
6160 * character. Used for ".{composing}" and "{composing}"
6161 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006162 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006163 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006164 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006165 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006166 /* If \Z was present, then ignore composing characters.
6167 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006168 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006169 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006170 else
6171 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006172 while (sta->c != NFA_END_COMPOSING)
6173 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006174 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006175
6176 /* Check base character matches first, unless ignored. */
6177 else if (len > 0 || mc == sta->c)
6178 {
6179 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006180 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006181 len += mb_char2len(mc);
6182 sta = sta->out;
6183 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006184
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006185 /* We don't care about the order of composing characters.
6186 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006187 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006188 {
6189 mc = mb_ptr2char(reginput + len);
6190 cchars[ccount++] = mc;
6191 len += mb_char2len(mc);
6192 if (ccount == MAX_MCO)
6193 break;
6194 }
6195
6196 /* Check that each composing char in the pattern matches a
6197 * composing char in the text. We do not check if all
6198 * composing chars are matched. */
6199 result = OK;
6200 while (sta->c != NFA_END_COMPOSING)
6201 {
6202 for (j = 0; j < ccount; ++j)
6203 if (cchars[j] == sta->c)
6204 break;
6205 if (j == ccount)
6206 {
6207 result = FAIL;
6208 break;
6209 }
6210 sta = sta->out;
6211 }
6212 }
6213 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006214 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006215
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006216 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006217 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006218 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006219 }
6220#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006221
6222 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006223 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6224 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006225 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006226 go_to_nextline = TRUE;
6227 /* Pass -1 for the offset, which means taking the position
6228 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006229 add_state = t->state->out;
6230 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006231 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006232 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006233 {
6234 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006235 add_state = t->state->out;
6236 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006237 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006238 break;
6239
Bram Moolenaar417bad22013-06-07 14:08:30 +02006240 case NFA_START_COLL:
6241 case NFA_START_NEG_COLL:
6242 {
6243 /* What follows is a list of characters, until NFA_END_COLL.
6244 * One of them must match or none of them must match. */
6245 nfa_state_T *state;
6246 int result_if_matched;
6247 int c1, c2;
6248
6249 /* Never match EOL. If it's part of the collection it is added
6250 * as a separate state with an OR. */
6251 if (curc == NUL)
6252 break;
6253
6254 state = t->state->out;
6255 result_if_matched = (t->state->c == NFA_START_COLL);
6256 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006257 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006258 if (state->c == NFA_END_COLL)
6259 {
6260 result = !result_if_matched;
6261 break;
6262 }
6263 if (state->c == NFA_RANGE_MIN)
6264 {
6265 c1 = state->val;
6266 state = state->out; /* advance to NFA_RANGE_MAX */
6267 c2 = state->val;
6268#ifdef ENABLE_LOG
6269 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6270 curc, c1, c2);
6271#endif
6272 if (curc >= c1 && curc <= c2)
6273 {
6274 result = result_if_matched;
6275 break;
6276 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006277 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006278 {
6279 int curc_low = MB_TOLOWER(curc);
6280 int done = FALSE;
6281
6282 for ( ; c1 <= c2; ++c1)
6283 if (MB_TOLOWER(c1) == curc_low)
6284 {
6285 result = result_if_matched;
6286 done = TRUE;
6287 break;
6288 }
6289 if (done)
6290 break;
6291 }
6292 }
6293 else if (state->c < 0 ? check_char_class(state->c, curc)
6294 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006295 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006296 == MB_TOLOWER(state->c))))
6297 {
6298 result = result_if_matched;
6299 break;
6300 }
6301 state = state->out;
6302 }
6303 if (result)
6304 {
6305 /* next state is in out of the NFA_END_COLL, out1 of
6306 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006307 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006308 add_off = clen;
6309 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006310 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006311 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006312
6313 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006314 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006315 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006316 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006317 add_state = t->state->out;
6318 add_off = clen;
6319 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006320 break;
6321
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006322 case NFA_ANY_COMPOSING:
6323 /* On a composing character skip over it. Otherwise do
6324 * nothing. Always matches. */
6325#ifdef FEAT_MBYTE
6326 if (enc_utf8 && utf_iscomposing(curc))
6327 {
6328 add_off = clen;
6329 }
6330 else
6331#endif
6332 {
6333 add_here = TRUE;
6334 add_off = 0;
6335 }
6336 add_state = t->state->out;
6337 break;
6338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006339 /*
6340 * Character classes like \a for alpha, \d for digit etc.
6341 */
6342 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006343 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006344 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006345 break;
6346
6347 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006348 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006349 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006350 break;
6351
6352 case NFA_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006353 result = vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006354 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006355 break;
6356
6357 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006358 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006359 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006360 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006361 break;
6362
6363 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006364 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006365 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 break;
6367
6368 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006369 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006370 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006371 break;
6372
6373 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006374 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006375 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006376 break;
6377
6378 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006379 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006380 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006381 break;
6382
6383 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006384 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006385 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386 break;
6387
6388 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006389 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006390 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006391 break;
6392
6393 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006394 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006395 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006396 break;
6397
6398 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006399 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006400 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401 break;
6402
6403 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006404 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006405 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006406 break;
6407
6408 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006409 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006410 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006411 break;
6412
6413 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006414 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006415 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006416 break;
6417
6418 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006419 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006420 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006421 break;
6422
6423 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006424 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006425 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006426 break;
6427
6428 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006429 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006430 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006431 break;
6432
6433 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006434 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006435 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006436 break;
6437
6438 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006439 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006440 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006441 break;
6442
6443 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006444 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006445 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006446 break;
6447
6448 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006449 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006450 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006451 break;
6452
6453 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006454 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006455 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006456 break;
6457
6458 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006459 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006460 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006461 break;
6462
6463 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006464 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006465 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006466 break;
6467
6468 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006469 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006470 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006471 break;
6472
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006473 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006474 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006475 ADD_STATE_IF_MATCH(t->state);
6476 break;
6477
6478 case NFA_NLOWER_IC: /* [^a-z] */
6479 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006480 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006481 ADD_STATE_IF_MATCH(t->state);
6482 break;
6483
6484 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006485 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006486 ADD_STATE_IF_MATCH(t->state);
6487 break;
6488
6489 case NFA_NUPPER_IC: /* ^[A-Z] */
6490 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006491 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006492 ADD_STATE_IF_MATCH(t->state);
6493 break;
6494
Bram Moolenaar5714b802013-05-28 22:03:20 +02006495 case NFA_BACKREF1:
6496 case NFA_BACKREF2:
6497 case NFA_BACKREF3:
6498 case NFA_BACKREF4:
6499 case NFA_BACKREF5:
6500 case NFA_BACKREF6:
6501 case NFA_BACKREF7:
6502 case NFA_BACKREF8:
6503 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006504#ifdef FEAT_SYN_HL
6505 case NFA_ZREF1:
6506 case NFA_ZREF2:
6507 case NFA_ZREF3:
6508 case NFA_ZREF4:
6509 case NFA_ZREF5:
6510 case NFA_ZREF6:
6511 case NFA_ZREF7:
6512 case NFA_ZREF8:
6513 case NFA_ZREF9:
6514#endif
6515 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006516 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006517 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006518 int bytelen;
6519
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006520 if (t->state->c <= NFA_BACKREF9)
6521 {
6522 subidx = t->state->c - NFA_BACKREF1 + 1;
6523 result = match_backref(&t->subs.norm, subidx, &bytelen);
6524 }
6525#ifdef FEAT_SYN_HL
6526 else
6527 {
6528 subidx = t->state->c - NFA_ZREF1 + 1;
6529 result = match_zref(subidx, &bytelen);
6530 }
6531#endif
6532
Bram Moolenaar5714b802013-05-28 22:03:20 +02006533 if (result)
6534 {
6535 if (bytelen == 0)
6536 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006537 /* empty match always works, output of NFA_SKIP to be
6538 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006539 add_here = TRUE;
6540 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006541 }
6542 else if (bytelen <= clen)
6543 {
6544 /* match current character, jump ahead to out of
6545 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006546 add_state = t->state->out->out;
6547 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006548 }
6549 else
6550 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006551 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006552 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006553 add_state = t->state->out;
6554 add_off = bytelen;
6555 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006556 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006557 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006558 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006559 }
6560 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006561 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006562 if (t->count - clen <= 0)
6563 {
6564 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006565 add_state = t->state->out;
6566 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006567 }
6568 else
6569 {
6570 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006571 add_state = t->state;
6572 add_off = 0;
6573 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006574 }
6575 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006576
Bram Moolenaar423532e2013-05-29 21:14:42 +02006577 case NFA_LNUM:
6578 case NFA_LNUM_GT:
6579 case NFA_LNUM_LT:
6580 result = (REG_MULTI &&
6581 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006582 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006583 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006584 {
6585 add_here = TRUE;
6586 add_state = t->state->out;
6587 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006588 break;
6589
6590 case NFA_COL:
6591 case NFA_COL_GT:
6592 case NFA_COL_LT:
6593 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6594 (long_u)(reginput - regline) + 1);
6595 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006596 {
6597 add_here = TRUE;
6598 add_state = t->state->out;
6599 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006600 break;
6601
6602 case NFA_VCOL:
6603 case NFA_VCOL_GT:
6604 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006605 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006606 int op = t->state->c - NFA_VCOL;
6607 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006608 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006609
6610 /* Bail out quickly when there can't be a match, avoid the
6611 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006612 if (op != 1 && col > t->state->val
6613#ifdef FEAT_MBYTE
6614 * (has_mbyte ? MB_MAXBYTES : 1)
6615#endif
6616 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006617 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006618 result = FALSE;
6619 if (op == 1 && col - 1 > t->state->val && col > 100)
6620 {
6621 int ts = wp->w_buffer->b_p_ts;
6622
6623 /* Guess that a character won't use more columns than
6624 * 'tabstop', with a minimum of 4. */
6625 if (ts < 4)
6626 ts = 4;
6627 result = col > t->state->val * ts;
6628 }
6629 if (!result)
6630 result = nfa_re_num_cmp(t->state->val, op,
6631 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006632 if (result)
6633 {
6634 add_here = TRUE;
6635 add_state = t->state->out;
6636 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006637 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006638 break;
6639
Bram Moolenaar044aa292013-06-04 21:27:38 +02006640 case NFA_MARK:
6641 case NFA_MARK_GT:
6642 case NFA_MARK_LT:
6643 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006644 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006645
6646 /* Compare the mark position to the match position. */
6647 result = (pos != NULL /* mark doesn't exist */
6648 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006649 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006650 ? (pos->col == (colnr_T)(reginput - regline)
6651 ? t->state->c == NFA_MARK
6652 : (pos->col < (colnr_T)(reginput - regline)
6653 ? t->state->c == NFA_MARK_GT
6654 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006655 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006656 ? t->state->c == NFA_MARK_GT
6657 : t->state->c == NFA_MARK_LT)));
6658 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006659 {
6660 add_here = TRUE;
6661 add_state = t->state->out;
6662 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006663 break;
6664 }
6665
Bram Moolenaar423532e2013-05-29 21:14:42 +02006666 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006667 result = (rex.reg_win != NULL
6668 && (reglnum + rex.reg_firstlnum
6669 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006670 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006671 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006672 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006673 {
6674 add_here = TRUE;
6675 add_state = t->state->out;
6676 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006677 break;
6678
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006679 case NFA_VISUAL:
6680 result = reg_match_visual();
6681 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006682 {
6683 add_here = TRUE;
6684 add_state = t->state->out;
6685 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006686 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006687
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006688 case NFA_MOPEN1:
6689 case NFA_MOPEN2:
6690 case NFA_MOPEN3:
6691 case NFA_MOPEN4:
6692 case NFA_MOPEN5:
6693 case NFA_MOPEN6:
6694 case NFA_MOPEN7:
6695 case NFA_MOPEN8:
6696 case NFA_MOPEN9:
6697#ifdef FEAT_SYN_HL
6698 case NFA_ZOPEN:
6699 case NFA_ZOPEN1:
6700 case NFA_ZOPEN2:
6701 case NFA_ZOPEN3:
6702 case NFA_ZOPEN4:
6703 case NFA_ZOPEN5:
6704 case NFA_ZOPEN6:
6705 case NFA_ZOPEN7:
6706 case NFA_ZOPEN8:
6707 case NFA_ZOPEN9:
6708#endif
6709 case NFA_NOPEN:
6710 case NFA_ZSTART:
6711 /* These states are only added to be able to bail out when
6712 * they are added again, nothing is to be done. */
6713 break;
6714
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006715 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006716 {
6717 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006718
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006719#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006720 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006721 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006722#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006723 result = (c == curc);
6724
Bram Moolenaar6100d022016-10-02 16:51:57 +02006725 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006726 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006727#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006728 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006729 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006730 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006731 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006732#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006733 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006734 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006735 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006736
6737 } /* switch (t->state->c) */
6738
6739 if (add_state != NULL)
6740 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006741 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006742 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006743
6744 if (t->pim.result == NFA_PIM_UNUSED)
6745 pim = NULL;
6746 else
6747 pim = &t->pim;
6748
6749 /* Handle the postponed invisible match if the match might end
6750 * without advancing and before the end of the line. */
6751 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006752 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006753 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006754 {
6755#ifdef ENABLE_LOG
6756 fprintf(log_fd, "\n");
6757 fprintf(log_fd, "==================================\n");
6758 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6759 fprintf(log_fd, "\n");
6760#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006761 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006762 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006763 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006764 /* for \@! and \@<! it is a match when the result is
6765 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006766 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006767 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6768 || pim->state->c
6769 == NFA_START_INVISIBLE_BEFORE_NEG
6770 || pim->state->c
6771 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006772 {
6773 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006774 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006775#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006776 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006777 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006778#endif
6779 }
6780 }
6781 else
6782 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006783 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006784#ifdef ENABLE_LOG
6785 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006786 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006787 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6788 fprintf(log_fd, "\n");
6789#endif
6790 }
6791
Bram Moolenaardecd9542013-06-07 16:31:50 +02006792 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006793 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006794 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6795 || pim->state->c
6796 == NFA_START_INVISIBLE_BEFORE_NEG
6797 || pim->state->c
6798 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006799 {
6800 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006801 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006802#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006803 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006804 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006805#endif
6806 }
6807 else
6808 /* look-behind match failed, don't add the state */
6809 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006810
6811 /* Postponed invisible match was handled, don't add it to
6812 * following states. */
6813 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006814 }
6815
Bram Moolenaara951e352013-10-06 15:46:11 +02006816 /* If "pim" points into l->t it will become invalid when
6817 * adding the state causes the list to be reallocated. Make a
6818 * local copy to avoid that. */
6819 if (pim == &t->pim)
6820 {
6821 copy_pim(&pim_copy, pim);
6822 pim = &pim_copy;
6823 }
6824
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006825 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006826 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006827 else
6828 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006829 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006830 if (add_count > 0)
6831 nextlist->t[nextlist->n - 1].count = add_count;
6832 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006833 }
6834
6835 } /* for (thislist = thislist; thislist->state; thislist++) */
6836
Bram Moolenaare23febd2013-05-26 18:40:14 +02006837 /* Look for the start of a match in the current position by adding the
6838 * start state to the list of states.
6839 * The first found match is the leftmost one, thus the order of states
6840 * matters!
6841 * Do not add the start state in recursive calls of nfa_regmatch(),
6842 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006843 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006844 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006845 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006846 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006847 && reglnum == 0
6848 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006849 && (rex.reg_maxcol == 0
6850 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006851 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006852 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006853 ? (reglnum < nfa_endp->se_u.pos.lnum
6854 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006855 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006856 < nfa_endp->se_u.pos.col))
6857 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006858 {
6859#ifdef ENABLE_LOG
6860 fprintf(log_fd, "(---) STARTSTATE\n");
6861#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006862 /* Inline optimized code for addstate() if we know the state is
6863 * the first MOPEN. */
6864 if (toplevel)
6865 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006866 int add = TRUE;
6867 int c;
6868
6869 if (prog->regstart != NUL && clen != 0)
6870 {
6871 if (nextlist->n == 0)
6872 {
6873 colnr_T col = (colnr_T)(reginput - regline) + clen;
6874
6875 /* Nextlist is empty, we can skip ahead to the
6876 * character that must appear at the start. */
6877 if (skip_to_start(prog->regstart, &col) == FAIL)
6878 break;
6879#ifdef ENABLE_LOG
6880 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6881 col - ((colnr_T)(reginput - regline) + clen));
6882#endif
6883 reginput = regline + col - clen;
6884 }
6885 else
6886 {
6887 /* Checking if the required start character matches is
6888 * cheaper than adding a state that won't match. */
6889 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006890 if (c != prog->regstart && (!rex.reg_ic
6891 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006892 {
6893#ifdef ENABLE_LOG
6894 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6895#endif
6896 add = FALSE;
6897 }
6898 }
6899 }
6900
6901 if (add)
6902 {
6903 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006904 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006905 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006906 else
6907 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006908 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006909 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006910 }
6911 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006912 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006913 }
6914
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006915#ifdef ENABLE_LOG
6916 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006917 {
6918 int i;
6919
6920 for (i = 0; i < thislist->n; i++)
6921 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6922 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006923 fprintf(log_fd, "\n");
6924#endif
6925
6926nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006927 /* Advance to the next character, or advance to the next line, or
6928 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006929 if (clen != 0)
6930 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006931 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6932 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006933 reg_nextline();
6934 else
6935 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006936
6937 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006938 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006939 if (got_int)
6940 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006941#ifdef FEAT_RELTIME
6942 /* Check for timeout once in a twenty times to avoid overhead. */
6943 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6944 {
6945 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006946 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006947 break;
6948 }
6949#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006950 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006951
6952#ifdef ENABLE_LOG
6953 if (log_fd != stderr)
6954 fclose(log_fd);
6955 log_fd = NULL;
6956#endif
6957
6958theend:
6959 /* Free memory */
6960 vim_free(list[0].t);
6961 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006962 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006963#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006964#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006965 fclose(debug);
6966#endif
6967
Bram Moolenaar963fee22013-05-26 21:47:28 +02006968 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006969}
6970
6971/*
6972 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006973 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006974 */
6975 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006976nfa_regtry(
6977 nfa_regprog_T *prog,
6978 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006979 proftime_T *tm UNUSED, /* timeout limit or NULL */
6980 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006981{
6982 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006983 regsubs_T subs, m;
6984 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006985 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006986#ifdef ENABLE_LOG
6987 FILE *f;
6988#endif
6989
6990 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006991#ifdef FEAT_RELTIME
6992 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006993 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006994 nfa_time_count = 0;
6995#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006996
6997#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006998 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006999 if (f != NULL)
7000 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007001 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007002#ifdef DEBUG
7003 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7004#endif
7005 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02007006 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007007 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007008 fprintf(f, "\n\n");
7009 fclose(f);
7010 }
7011 else
7012 EMSG(_("Could not open temporary log file for writing "));
7013#endif
7014
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007015 clear_sub(&subs.norm);
7016 clear_sub(&m.norm);
7017#ifdef FEAT_SYN_HL
7018 clear_sub(&subs.synt);
7019 clear_sub(&m.synt);
7020#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007021
Bram Moolenaarfda37292014-11-05 14:27:36 +01007022 result = nfa_regmatch(prog, start, &subs, &m);
7023 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007024 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007025 else if (result == NFA_TOO_EXPENSIVE)
7026 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007027
7028 cleanup_subexpr();
7029 if (REG_MULTI)
7030 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007031 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007032 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007033 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7034 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007035
Bram Moolenaar6100d022016-10-02 16:51:57 +02007036 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7037 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007038 }
7039
Bram Moolenaar6100d022016-10-02 16:51:57 +02007040 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007041 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007042 rex.reg_startpos[0].lnum = 0;
7043 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007044 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007045 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007046 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007047 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007048 rex.reg_endpos[0].lnum = reglnum;
7049 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007050 }
7051 else
7052 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007053 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007054 }
7055 else
7056 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007057 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007058 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007059 rex.reg_startp[i] = subs.norm.list.line[i].start;
7060 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007061 }
7062
Bram Moolenaar6100d022016-10-02 16:51:57 +02007063 if (rex.reg_startp[0] == NULL)
7064 rex.reg_startp[0] = regline + col;
7065 if (rex.reg_endp[0] == NULL)
7066 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007067 }
7068
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007069#ifdef FEAT_SYN_HL
7070 /* Package any found \z(...\) matches for export. Default is none. */
7071 unref_extmatch(re_extmatch_out);
7072 re_extmatch_out = NULL;
7073
7074 if (prog->reghasz == REX_SET)
7075 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007076 cleanup_zsubexpr();
7077 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007078 /* Loop over \z1, \z2, etc. There is no \z0. */
7079 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007080 {
7081 if (REG_MULTI)
7082 {
7083 struct multipos *mpos = &subs.synt.list.multi[i];
7084
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007085 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007086 if (mpos->start_lnum >= 0
7087 && mpos->start_lnum == mpos->end_lnum
7088 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007089 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007090 vim_strnsave(reg_getline(mpos->start_lnum)
7091 + mpos->start_col,
7092 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007093 }
7094 else
7095 {
7096 struct linepos *lpos = &subs.synt.list.line[i];
7097
7098 if (lpos->start != NULL && lpos->end != NULL)
7099 re_extmatch_out->matches[i] =
7100 vim_strnsave(lpos->start,
7101 (int)(lpos->end - lpos->start));
7102 }
7103 }
7104 }
7105#endif
7106
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007107 return 1 + reglnum;
7108}
7109
7110/*
7111 * Match a regexp against a string ("line" points to the string) or multiple
7112 * lines ("line" is NULL, use reg_getline()).
7113 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007114 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007115 */
7116 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007117nfa_regexec_both(
7118 char_u *line,
7119 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007120 proftime_T *tm, /* timeout limit or NULL */
7121 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007122{
7123 nfa_regprog_T *prog;
7124 long retval = 0L;
7125 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007126 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007127
7128 if (REG_MULTI)
7129 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007130 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007131 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007132 rex.reg_startpos = rex.reg_mmatch->startpos;
7133 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007134 }
7135 else
7136 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007137 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7138 rex.reg_startp = rex.reg_match->startp;
7139 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007140 }
7141
7142 /* Be paranoid... */
7143 if (prog == NULL || line == NULL)
7144 {
7145 EMSG(_(e_null));
7146 goto theend;
7147 }
7148
Bram Moolenaar6100d022016-10-02 16:51:57 +02007149 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007150 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007151 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007152 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007153 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007154
7155#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007156 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007157 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007158 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007159#endif
7160
7161 regline = line;
7162 reglnum = 0; /* relative to line */
7163
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007164 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007165 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007166 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007167 nfa_listid = 1;
7168 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007169 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007170
Bram Moolenaard89616e2013-06-06 18:46:06 +02007171 if (prog->reganch && col > 0)
7172 return 0L;
7173
Bram Moolenaar473de612013-06-08 18:19:48 +02007174 need_clear_subexpr = TRUE;
7175#ifdef FEAT_SYN_HL
7176 /* Clear the external match subpointers if necessary. */
7177 if (prog->reghasz == REX_SET)
7178 {
7179 nfa_has_zsubexpr = TRUE;
7180 need_clear_zsubexpr = TRUE;
7181 }
7182 else
7183 nfa_has_zsubexpr = FALSE;
7184#endif
7185
Bram Moolenaard89616e2013-06-06 18:46:06 +02007186 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007187 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007188 /* Skip ahead until a character we know the match must start with.
7189 * When there is none there is no match. */
7190 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007191 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007192
Bram Moolenaar473de612013-06-08 18:19:48 +02007193 /* If match_text is set it contains the full text that must match.
7194 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007195 if (prog->match_text != NULL
7196#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007197 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007198#endif
7199 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007200 return find_match_text(col, prog->regstart, prog->match_text);
7201 }
7202
Bram Moolenaard89616e2013-06-06 18:46:06 +02007203 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007204 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007205 goto theend;
7206
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007207 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208 for (i = 0; i < nstate; ++i)
7209 {
7210 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007211 prog->state[i].lastlist[0] = 0;
7212 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007213 }
7214
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007215 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007216
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007217 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007218
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219theend:
7220 return retval;
7221}
7222
7223/*
7224 * Compile a regular expression into internal code for the NFA matcher.
7225 * Returns the program in allocated space. Returns NULL for an error.
7226 */
7227 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007228nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007229{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007230 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007231 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007232 int *postfix;
7233
7234 if (expr == NULL)
7235 return NULL;
7236
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007237 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007238 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007239
7240 init_class_tab();
7241
7242 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7243 return NULL;
7244
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007245 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007246 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007247 postfix = re2post();
7248 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007249 {
7250 /* TODO: only give this error for debugging? */
7251 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007252 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007253 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007254 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007255
7256 /*
7257 * In order to build the NFA, we parse the input regexp twice:
7258 * 1. first pass to count size (so we can allocate space)
7259 * 2. second to emit code
7260 */
7261#ifdef ENABLE_LOG
7262 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007263 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007264
7265 if (f != NULL)
7266 {
7267 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7268 fclose(f);
7269 }
7270 }
7271#endif
7272
7273 /*
7274 * PASS 1
7275 * Count number of NFA states in "nstate". Do not build the NFA.
7276 */
7277 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007278
Bram Moolenaar16619a22013-06-11 18:42:36 +02007279 /* allocate the regprog with space for the compiled regexp */
7280 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007281 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7282 if (prog == NULL)
7283 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007284 state_ptr = prog->state;
7285
7286 /*
7287 * PASS 2
7288 * Build the NFA
7289 */
7290 prog->start = post2nfa(postfix, post_ptr, FALSE);
7291 if (prog->start == NULL)
7292 goto fail;
7293
7294 prog->regflags = regflags;
7295 prog->engine = &nfa_regengine;
7296 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007297 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007298 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007299 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007300
Bram Moolenaara2947e22013-06-11 22:44:09 +02007301 nfa_postprocess(prog);
7302
Bram Moolenaard89616e2013-06-06 18:46:06 +02007303 prog->reganch = nfa_get_reganch(prog->start, 0);
7304 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007305 prog->match_text = nfa_get_match_text(prog->start);
7306
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007307#ifdef ENABLE_LOG
7308 nfa_postfix_dump(expr, OK);
7309 nfa_dump(prog);
7310#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007311#ifdef FEAT_SYN_HL
7312 /* Remember whether this pattern has any \z specials in it. */
7313 prog->reghasz = re_has_z;
7314#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007315 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007316 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007317
7318out:
7319 vim_free(post_start);
7320 post_start = post_ptr = post_end = NULL;
7321 state_ptr = NULL;
7322 return (regprog_T *)prog;
7323
7324fail:
7325 vim_free(prog);
7326 prog = NULL;
7327#ifdef ENABLE_LOG
7328 nfa_postfix_dump(expr, FAIL);
7329#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007330 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007331 goto out;
7332}
7333
Bram Moolenaar473de612013-06-08 18:19:48 +02007334/*
7335 * Free a compiled regexp program, returned by nfa_regcomp().
7336 */
7337 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007338nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007339{
7340 if (prog != NULL)
7341 {
7342 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007343 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007344 vim_free(prog);
7345 }
7346}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007347
7348/*
7349 * Match a regexp against a string.
7350 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7351 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007352 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007353 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007354 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007355 */
7356 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007357nfa_regexec_nl(
7358 regmatch_T *rmp,
7359 char_u *line, /* string to match against */
7360 colnr_T col, /* column to start looking for match */
7361 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007362{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007363 rex.reg_match = rmp;
7364 rex.reg_mmatch = NULL;
7365 rex.reg_maxline = 0;
7366 rex.reg_line_lbr = line_lbr;
7367 rex.reg_buf = curbuf;
7368 rex.reg_win = NULL;
7369 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007370#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007371 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007372#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007373 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007374 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007375}
7376
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007377
7378/*
7379 * Match a regexp against multiple lines.
7380 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7381 * Uses curbuf for line count and 'iskeyword'.
7382 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007383 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007384 * match otherwise.
7385 *
7386 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7387 *
7388 * ! Also NOTE : match may actually be in another line. e.g.:
7389 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7390 *
7391 * +-------------------------+
7392 * |a |
7393 * |b |
7394 * |c |
7395 * | |
7396 * +-------------------------+
7397 *
7398 * then nfa_regexec_multi() returns 3. while the original
7399 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7400 *
7401 * FIXME if this behavior is not compatible.
7402 */
7403 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007404nfa_regexec_multi(
7405 regmmatch_T *rmp,
7406 win_T *win, /* window in which to search or NULL */
7407 buf_T *buf, /* buffer in which to search */
7408 linenr_T lnum, /* nr of line to start looking for match */
7409 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007410 proftime_T *tm, /* timeout limit or NULL */
7411 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007412{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007413 rex.reg_match = NULL;
7414 rex.reg_mmatch = rmp;
7415 rex.reg_buf = buf;
7416 rex.reg_win = win;
7417 rex.reg_firstlnum = lnum;
7418 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7419 rex.reg_line_lbr = FALSE;
7420 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007421#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007422 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007423#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007424 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007425
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007426 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007427}
7428
7429#ifdef DEBUG
7430# undef ENABLE_LOG
7431#endif