blob: b38fcd7519905bb84d9e367e1bf8029392de910a [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) */
53 NFA_STAR, /* greedy * (posfix only) */
54 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaare0ad3652015-01-27 12:59:55 +0100247/* re_flags passed to nfa_regcomp() */
248static int nfa_re_flags;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200251static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaar428e9872013-05-30 17:05:39 +0200253/* NFA regexp \1 .. \9 encountered. */
254static int nfa_has_backref;
255
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257/* NFA regexp has \z( ), set zsubexpr. */
258static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200259#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200260
Bram Moolenaar963fee22013-05-26 21:47:28 +0200261/* Number of sub expressions actually being used during execution. 1 if only
262 * the whole match (subexpr 0) is used. */
263static int nfa_nsubexpr;
264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265static int *post_start; /* holds the postfix form of r.e. */
266static int *post_end;
267static int *post_ptr;
268
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200269static int nstate; /* Number of states in the NFA. Also used when
270 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200271static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar307aa162013-06-02 16:34:21 +0200273/* If not NULL match must end at this position */
274static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200276/* listid is global, so that it increases on recursive calls to
277 * nfa_regmatch(), which means we don't have to clear the lastlist field of
278 * all the states. */
279static int nfa_listid;
280static int nfa_alt_listid;
281
282/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
283static int nfa_ll_index = 0;
284
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100285static int nfa_regcomp_start(char_u *expr, int re_flags);
286static int nfa_get_reganch(nfa_state_T *start, int depth);
287static int nfa_get_regstart(nfa_state_T *start, int depth);
288static char_u *nfa_get_match_text(nfa_state_T *start);
289static int realloc_post_list(void);
290static int nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl);
291static int nfa_emit_equi_class(int c);
292static int nfa_regatom(void);
293static int nfa_regpiece(void);
294static int nfa_regconcat(void);
295static int nfa_regbranch(void);
296static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100298static void nfa_set_code(int c);
299static void nfa_postfix_dump(char_u *expr, int retval);
300static void nfa_print_state(FILE *debugf, nfa_state_T *state);
301static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
302static void nfa_dump(nfa_regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100304static int *re2post(void);
305static nfa_state_T *alloc_state(int c, nfa_state_T *out, nfa_state_T *out1);
306static void st_error(int *postfix, int *end, int *p);
307static int nfa_max_width(nfa_state_T *startstate, int depth);
308static nfa_state_T *post2nfa(int *postfix, int *end, int nfa_calc_size);
309static void nfa_postprocess(nfa_regprog_T *prog);
310static int check_char_class(int class, int c);
311static void nfa_save_listids(nfa_regprog_T *prog, int *list);
312static void nfa_restore_listids(nfa_regprog_T *prog, int *list);
313static int nfa_re_num_cmp(long_u val, int op, long_u pos);
314static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm);
315static long nfa_regexec_both(char_u *line, colnr_T col, proftime_T *tm);
316static regprog_T *nfa_regcomp(char_u *expr, int re_flags);
317static void nfa_regfree(regprog_T *prog);
318static int nfa_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr);
319static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm);
320static int match_follows(nfa_state_T *startstate, int depth);
321static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322
323/* helper functions used when doing re2post() ... regatom() parsing */
324#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200325 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326 return FAIL; \
327 *post_ptr++ = c; \
328 } while (0)
329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330/*
331 * Initialize internal variables before NFA compilation.
332 * Return OK on success, FAIL otherwise.
333 */
334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335nfa_regcomp_start(
336 char_u *expr,
337 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200339 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200340 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341
342 nstate = 0;
343 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200344 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200345 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200348 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200349 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200350
351 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200352 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354 post_start = (int *)lalloc(postfix_size, TRUE);
355 if (post_start == NULL)
356 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200358 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200360 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200361
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200362 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200363 regcomp_start(expr, re_flags);
364
365 return OK;
366}
367
368/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200369 * Figure out if the NFA state list starts with an anchor, must match at start
370 * of the line.
371 */
372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100373nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200374{
375 nfa_state_T *p = start;
376
377 if (depth > 4)
378 return 0;
379
380 while (p != NULL)
381 {
382 switch (p->c)
383 {
384 case NFA_BOL:
385 case NFA_BOF:
386 return 1; /* yes! */
387
388 case NFA_ZSTART:
389 case NFA_ZEND:
390 case NFA_CURSOR:
391 case NFA_VISUAL:
392
393 case NFA_MOPEN:
394 case NFA_MOPEN1:
395 case NFA_MOPEN2:
396 case NFA_MOPEN3:
397 case NFA_MOPEN4:
398 case NFA_MOPEN5:
399 case NFA_MOPEN6:
400 case NFA_MOPEN7:
401 case NFA_MOPEN8:
402 case NFA_MOPEN9:
403 case NFA_NOPEN:
404#ifdef FEAT_SYN_HL
405 case NFA_ZOPEN:
406 case NFA_ZOPEN1:
407 case NFA_ZOPEN2:
408 case NFA_ZOPEN3:
409 case NFA_ZOPEN4:
410 case NFA_ZOPEN5:
411 case NFA_ZOPEN6:
412 case NFA_ZOPEN7:
413 case NFA_ZOPEN8:
414 case NFA_ZOPEN9:
415#endif
416 p = p->out;
417 break;
418
419 case NFA_SPLIT:
420 return nfa_get_reganch(p->out, depth + 1)
421 && nfa_get_reganch(p->out1, depth + 1);
422
423 default:
424 return 0; /* noooo */
425 }
426 }
427 return 0;
428}
429
430/*
431 * Figure out if the NFA state list starts with a character which must match
432 * at start of the match.
433 */
434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100435nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200436{
437 nfa_state_T *p = start;
438
439 if (depth > 4)
440 return 0;
441
442 while (p != NULL)
443 {
444 switch (p->c)
445 {
446 /* all kinds of zero-width matches */
447 case NFA_BOL:
448 case NFA_BOF:
449 case NFA_BOW:
450 case NFA_EOW:
451 case NFA_ZSTART:
452 case NFA_ZEND:
453 case NFA_CURSOR:
454 case NFA_VISUAL:
455 case NFA_LNUM:
456 case NFA_LNUM_GT:
457 case NFA_LNUM_LT:
458 case NFA_COL:
459 case NFA_COL_GT:
460 case NFA_COL_LT:
461 case NFA_VCOL:
462 case NFA_VCOL_GT:
463 case NFA_VCOL_LT:
464 case NFA_MARK:
465 case NFA_MARK_GT:
466 case NFA_MARK_LT:
467
468 case NFA_MOPEN:
469 case NFA_MOPEN1:
470 case NFA_MOPEN2:
471 case NFA_MOPEN3:
472 case NFA_MOPEN4:
473 case NFA_MOPEN5:
474 case NFA_MOPEN6:
475 case NFA_MOPEN7:
476 case NFA_MOPEN8:
477 case NFA_MOPEN9:
478 case NFA_NOPEN:
479#ifdef FEAT_SYN_HL
480 case NFA_ZOPEN:
481 case NFA_ZOPEN1:
482 case NFA_ZOPEN2:
483 case NFA_ZOPEN3:
484 case NFA_ZOPEN4:
485 case NFA_ZOPEN5:
486 case NFA_ZOPEN6:
487 case NFA_ZOPEN7:
488 case NFA_ZOPEN8:
489 case NFA_ZOPEN9:
490#endif
491 p = p->out;
492 break;
493
494 case NFA_SPLIT:
495 {
496 int c1 = nfa_get_regstart(p->out, depth + 1);
497 int c2 = nfa_get_regstart(p->out1, depth + 1);
498
499 if (c1 == c2)
500 return c1; /* yes! */
501 return 0;
502 }
503
504 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200505 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200506 return p->c; /* yes! */
507 return 0;
508 }
509 }
510 return 0;
511}
512
513/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200514 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200515 * else. If so return a string in allocated memory with what must match after
516 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200517 */
518 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100519nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200520{
521 nfa_state_T *p = start;
522 int len = 0;
523 char_u *ret;
524 char_u *s;
525
526 if (p->c != NFA_MOPEN)
527 return NULL; /* just in case */
528 p = p->out;
529 while (p->c > 0)
530 {
531 len += MB_CHAR2LEN(p->c);
532 p = p->out;
533 }
534 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
535 return NULL;
536
537 ret = alloc(len);
538 if (ret != NULL)
539 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200540 p = start->out->out; /* skip first char, it goes into regstart */
541 s = ret;
542 while (p->c > 0)
543 {
544#ifdef FEAT_MBYTE
545 if (has_mbyte)
546 s += (*mb_char2bytes)(p->c, s);
547 else
548#endif
549 *s++ = p->c;
550 p = p->out;
551 }
552 *s = NUL;
553 }
554 return ret;
555}
556
557/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 * Allocate more space for post_start. Called when
559 * running above the estimated number of states.
560 */
561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100562realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200563{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200564 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200565 int new_max = nstate_max + 1000;
566 int *new_start;
567 int *old_start;
568
569 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
570 if (new_start == NULL)
571 return FAIL;
572 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200573 old_start = post_start;
574 post_start = new_start;
575 post_ptr = new_start + (post_ptr - old_start);
576 post_end = post_start + new_max;
577 vim_free(old_start);
578 return OK;
579}
580
581/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 * Search between "start" and "end" and try to recognize a
583 * character class in expanded form. For example [0-9].
584 * On success, return the id the character class to be emitted.
585 * On failure, return 0 (=FAIL)
586 * Start points to the first char of the range, while end should point
587 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200588 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
589 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200590 */
591 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100592nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200594# define CLASS_not 0x80
595# define CLASS_af 0x40
596# define CLASS_AF 0x20
597# define CLASS_az 0x10
598# define CLASS_AZ 0x08
599# define CLASS_o7 0x04
600# define CLASS_o9 0x02
601# define CLASS_underscore 0x01
602
603 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200605 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200606
607 if (extra_newl == TRUE)
608 newl = TRUE;
609
610 if (*end != ']')
611 return FAIL;
612 p = start;
613 if (*p == '^')
614 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200615 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200616 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617 }
618
619 while (p < end)
620 {
621 if (p + 2 < end && *(p + 1) == '-')
622 {
623 switch (*p)
624 {
625 case '0':
626 if (*(p + 2) == '9')
627 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200628 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 break;
630 }
631 else
632 if (*(p + 2) == '7')
633 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200634 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 break;
636 }
637 case 'a':
638 if (*(p + 2) == 'z')
639 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200640 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 break;
642 }
643 else
644 if (*(p + 2) == 'f')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
649 case 'A':
650 if (*(p + 2) == 'Z')
651 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200652 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200653 break;
654 }
655 else
656 if (*(p + 2) == 'F')
657 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200658 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200659 break;
660 }
661 /* FALLTHROUGH */
662 default:
663 return FAIL;
664 }
665 p += 3;
666 }
667 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
668 {
669 newl = TRUE;
670 p += 2;
671 }
672 else if (*p == '_')
673 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200674 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675 p ++;
676 }
677 else if (*p == '\n')
678 {
679 newl = TRUE;
680 p ++;
681 }
682 else
683 return FAIL;
684 } /* while (p < end) */
685
686 if (p != end)
687 return FAIL;
688
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200690 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200691
692 switch (config)
693 {
694 case CLASS_o9:
695 return extra_newl + NFA_DIGIT;
696 case CLASS_not | CLASS_o9:
697 return extra_newl + NFA_NDIGIT;
698 case CLASS_af | CLASS_AF | CLASS_o9:
699 return extra_newl + NFA_HEX;
700 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
701 return extra_newl + NFA_NHEX;
702 case CLASS_o7:
703 return extra_newl + NFA_OCTAL;
704 case CLASS_not | CLASS_o7:
705 return extra_newl + NFA_NOCTAL;
706 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
707 return extra_newl + NFA_WORD;
708 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
709 return extra_newl + NFA_NWORD;
710 case CLASS_az | CLASS_AZ | CLASS_underscore:
711 return extra_newl + NFA_HEAD;
712 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
713 return extra_newl + NFA_NHEAD;
714 case CLASS_az | CLASS_AZ:
715 return extra_newl + NFA_ALPHA;
716 case CLASS_not | CLASS_az | CLASS_AZ:
717 return extra_newl + NFA_NALPHA;
718 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200719 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200720 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200721 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200722 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200723 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200724 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200725 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200726 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200727 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728}
729
730/*
731 * Produce the bytes for equivalence class "c".
732 * Currently only handles latin1, latin9 and utf-8.
733 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
734 * equivalent to 'a OR b OR c'
735 *
736 * NOTE! When changing this function, also update reg_equi_class()
737 */
738 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100739nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200740{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200741#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
742#ifdef FEAT_MBYTE
743# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
744#else
745# define EMITMBC(c)
746#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200747
748#ifdef FEAT_MBYTE
749 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
750 || STRCMP(p_enc, "iso-8859-15") == 0)
751#endif
752 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200753#ifdef EBCDIC
754# define A_circumflex 0x62
755# define A_diaeresis 0x63
756# define A_grave 0x64
757# define A_acute 0x65
758# define A_virguilla 0x66
759# define A_ring 0x67
760# define C_cedilla 0x68
761# define E_acute 0x71
762# define E_circumflex 0x72
763# define E_diaeresis 0x73
764# define E_grave 0x74
765# define I_acute 0x75
766# define I_circumflex 0x76
767# define I_diaeresis 0x77
768# define I_grave 0x78
769# define N_virguilla 0x69
770# define O_circumflex 0xeb
771# define O_diaeresis 0xec
772# define O_grave 0xed
773# define O_acute 0xee
774# define O_virguilla 0xef
775# define O_slash 0x80
776# define U_circumflex 0xfb
777# define U_diaeresis 0xfc
778# define U_grave 0xfd
779# define U_acute 0xfe
780# define Y_acute 0xba
781# define a_grave 0x42
782# define a_acute 0x43
783# define a_circumflex 0x44
784# define a_virguilla 0x45
785# define a_diaeresis 0x46
786# define a_ring 0x47
787# define c_cedilla 0x48
788# define e_grave 0x51
789# define e_acute 0x52
790# define e_circumflex 0x53
791# define e_diaeresis 0x54
792# define i_grave 0x55
793# define i_acute 0x56
794# define i_circumflex 0x57
795# define i_diaeresis 0x58
796# define n_virguilla 0x49
797# define o_grave 0xcb
798# define o_acute 0xcc
799# define o_circumflex 0xcd
800# define o_virguilla 0xce
801# define o_diaeresis 0xcf
802# define o_slash 0x70
803# define u_grave 0xdb
804# define u_acute 0xdc
805# define u_circumflex 0xdd
806# define u_diaeresis 0xde
807# define y_acute 0x8d
808# define y_diaeresis 0xdf
809#else
810# define A_grave 0xc0
811# define A_acute 0xc1
812# define A_circumflex 0xc2
813# define A_virguilla 0xc3
814# define A_diaeresis 0xc4
815# define A_ring 0xc5
816# define C_cedilla 0xc7
817# define E_grave 0xc8
818# define E_acute 0xc9
819# define E_circumflex 0xca
820# define E_diaeresis 0xcb
821# define I_grave 0xcc
822# define I_acute 0xcd
823# define I_circumflex 0xce
824# define I_diaeresis 0xcf
825# define N_virguilla 0xd1
826# define O_grave 0xd2
827# define O_acute 0xd3
828# define O_circumflex 0xd4
829# define O_virguilla 0xd5
830# define O_diaeresis 0xd6
831# define O_slash 0xd8
832# define U_grave 0xd9
833# define U_acute 0xda
834# define U_circumflex 0xdb
835# define U_diaeresis 0xdc
836# define Y_acute 0xdd
837# define a_grave 0xe0
838# define a_acute 0xe1
839# define a_circumflex 0xe2
840# define a_virguilla 0xe3
841# define a_diaeresis 0xe4
842# define a_ring 0xe5
843# define c_cedilla 0xe7
844# define e_grave 0xe8
845# define e_acute 0xe9
846# define e_circumflex 0xea
847# define e_diaeresis 0xeb
848# define i_grave 0xec
849# define i_acute 0xed
850# define i_circumflex 0xee
851# define i_diaeresis 0xef
852# define n_virguilla 0xf1
853# define o_grave 0xf2
854# define o_acute 0xf3
855# define o_circumflex 0xf4
856# define o_virguilla 0xf5
857# define o_diaeresis 0xf6
858# define o_slash 0xf8
859# define u_grave 0xf9
860# define u_acute 0xfa
861# define u_circumflex 0xfb
862# define u_diaeresis 0xfc
863# define y_acute 0xfd
864# define y_diaeresis 0xff
865#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200866 switch (c)
867 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200868 case 'A': case A_grave: case A_acute: case A_circumflex:
869 case A_virguilla: case A_diaeresis: case A_ring:
870 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
871 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
872 CASEMBC(0x1ea2)
873 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
874 EMIT2(A_circumflex); EMIT2(A_virguilla);
875 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200876 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
877 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
878 EMITMBC(0x1ea2)
879 return OK;
880
881 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
882 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200883 return OK;
884
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200885 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
886 CASEMBC(0x10a) CASEMBC(0x10c)
887 EMIT2('C'); EMIT2(C_cedilla);
888 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200889 EMITMBC(0x10a) EMITMBC(0x10c)
890 return OK;
891
892 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200893 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200894 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
895 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200896 return OK;
897
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200898 case 'E': case E_grave: case E_acute: case E_circumflex:
899 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
900 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
901 CASEMBC(0x1eba) CASEMBC(0x1ebc)
902 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
903 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200904 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
905 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
906 EMITMBC(0x1ebc)
907 return OK;
908
909 case 'F': CASEMBC(0x1e1e)
910 EMIT2('F'); EMITMBC(0x1e1e)
911 return OK;
912
913 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200914 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
915 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200916 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
917 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
918 EMITMBC(0x1f4) EMITMBC(0x1e20)
919 return OK;
920
921 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200922 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200923 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
924 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200925 return OK;
926
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200927 case 'I': case I_grave: case I_acute: case I_circumflex:
928 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
929 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
930 CASEMBC(0x1cf) CASEMBC(0x1ec8)
931 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
932 EMIT2(I_circumflex); EMIT2(I_diaeresis);
933 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200934 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
935 EMITMBC(0x1cf) EMITMBC(0x1ec8)
936 return OK;
937
938 case 'J': CASEMBC(0x134)
939 EMIT2('J'); EMITMBC(0x134)
940 return OK;
941
942 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200943 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200944 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
945 EMITMBC(0x1e34)
946 return OK;
947
948 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200949 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200950 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
951 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
952 return OK;
953
954 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
955 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200956 return OK;
957
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200958 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
959 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
960 EMIT2('N'); EMIT2(N_virguilla);
961 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200962 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200963 return OK;
964
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200965 case 'O': case O_grave: case O_acute: case O_circumflex:
966 case O_virguilla: case O_diaeresis: case O_slash:
967 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
968 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
969 CASEMBC(0x1ec) CASEMBC(0x1ece)
970 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
971 EMIT2(O_circumflex); EMIT2(O_virguilla);
972 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200973 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
974 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
975 EMITMBC(0x1ec) EMITMBC(0x1ece)
976 return OK;
977
978 case 'P': case 0x1e54: case 0x1e56:
979 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
980 return OK;
981
982 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200983 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200984 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
985 EMITMBC(0x1e58) EMITMBC(0x1e5e)
986 return OK;
987
988 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200989 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200990 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
991 EMITMBC(0x160) EMITMBC(0x1e60)
992 return OK;
993
994 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200995 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200996 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
997 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200998 return OK;
999
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001000 case 'U': case U_grave: case U_acute: case U_diaeresis:
1001 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
1002 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
1003 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
1004 CASEMBC(0x1ee6)
1005 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
1006 EMIT2(U_diaeresis); EMIT2(U_circumflex);
1007 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001008 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
1009 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
1010 EMITMBC(0x1ee6)
1011 return OK;
1012
1013 case 'V': CASEMBC(0x1e7c)
1014 EMIT2('V'); EMITMBC(0x1e7c)
1015 return OK;
1016
1017 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001018 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001019 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
1020 EMITMBC(0x1e84) EMITMBC(0x1e86)
1021 return OK;
1022
1023 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
1024 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001025 return OK;
1026
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001027 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
1028 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
1029 CASEMBC(0x1ef8)
1030 EMIT2('Y'); EMIT2(Y_acute);
1031 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001032 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
1033 EMITMBC(0x1ef8)
1034 return OK;
1035
1036 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001037 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001038 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
1039 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001040 return OK;
1041
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001042 case 'a': case a_grave: case a_acute: case a_circumflex:
1043 case a_virguilla: case a_diaeresis: case a_ring:
1044 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
1045 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1046 CASEMBC(0x1ea3)
1047 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1048 EMIT2(a_circumflex); EMIT2(a_virguilla);
1049 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001050 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1051 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1052 EMITMBC(0x1ea3)
1053 return OK;
1054
1055 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1056 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001057 return OK;
1058
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001059 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1060 CASEMBC(0x10b) CASEMBC(0x10d)
1061 EMIT2('c'); EMIT2(c_cedilla);
1062 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001063 EMITMBC(0x10b) EMITMBC(0x10d)
1064 return OK;
1065
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001066 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001067 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001068 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1069 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001070 return OK;
1071
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001072 case 'e': case e_grave: case e_acute: case e_circumflex:
1073 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1074 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1075 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1076 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1077 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1078 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001079 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1080 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1081 return OK;
1082
1083 case 'f': CASEMBC(0x1e1f)
1084 EMIT2('f'); EMITMBC(0x1e1f)
1085 return OK;
1086
1087 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001088 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1089 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001090 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1091 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1092 EMITMBC(0x1f5) EMITMBC(0x1e21)
1093 return OK;
1094
1095 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001096 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001097 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1098 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001099 return OK;
1100
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001101 case 'i': case i_grave: case i_acute: case i_circumflex:
1102 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1103 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1104 CASEMBC(0x1ec9)
1105 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1106 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1107 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001108 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1109 EMITMBC(0x1ec9)
1110 return OK;
1111
1112 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1113 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1114 return OK;
1115
1116 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001117 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001118 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1119 EMITMBC(0x1e35)
1120 return OK;
1121
1122 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001123 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001124 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1125 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1126 return OK;
1127
1128 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1129 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001130 return OK;
1131
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001132 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1133 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1134 CASEMBC(0x1e49)
1135 EMIT2('n'); EMIT2(n_virguilla);
1136 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001137 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1138 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139 return OK;
1140
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001141 case 'o': case o_grave: case o_acute: case o_circumflex:
1142 case o_virguilla: case o_diaeresis: case o_slash:
1143 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1144 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1145 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1146 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1147 EMIT2(o_circumflex); EMIT2(o_virguilla);
1148 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001149 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1150 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1151 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1152 return OK;
1153
1154 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1155 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1156 return OK;
1157
1158 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001159 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001160 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1161 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1162 return OK;
1163
1164 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001165 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001166 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1167 EMITMBC(0x161) EMITMBC(0x1e61)
1168 return OK;
1169
1170 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001171 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001172 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1173 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001174 return OK;
1175
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001176 case 'u': case u_grave: case u_acute: case u_circumflex:
1177 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1178 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1179 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1180 CASEMBC(0x1ee7)
1181 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1182 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1183 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001184 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1185 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1186 EMITMBC(0x1ee7)
1187 return OK;
1188
1189 case 'v': CASEMBC(0x1e7d)
1190 EMIT2('v'); EMITMBC(0x1e7d)
1191 return OK;
1192
1193 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001194 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001195 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1196 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1197 return OK;
1198
1199 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1200 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 return OK;
1202
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001203 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1204 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1205 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1206 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1207 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001208 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1209 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210 return OK;
1211
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001212 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001213 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001214 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1215 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1216 return OK;
1217
1218 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219 }
1220 }
1221
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001222 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 return OK;
1224#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001225#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226}
1227
1228/*
1229 * Code to parse regular expression.
1230 *
1231 * We try to reuse parsing functions in regexp.c to
1232 * minimize surprise and keep the syntax consistent.
1233 */
1234
1235/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001236 * Parse the lowest level.
1237 *
1238 * An atom can be one of a long list of items. Many atoms match one character
1239 * in the text. It is often an ordinary character or a character class.
1240 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1241 * is only for syntax highlighting.
1242 *
1243 * atom ::= ordinary-atom
1244 * or \( pattern \)
1245 * or \%( pattern \)
1246 * or \z( pattern \)
1247 */
1248 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001249nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001250{
1251 int c;
1252 int charclass;
1253 int equiclass;
1254 int collclass;
1255 int got_coll_char;
1256 char_u *p;
1257 char_u *endp;
1258#ifdef FEAT_MBYTE
1259 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260#endif
1261 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262 int emit_range;
1263 int negated;
1264 int result;
1265 int startc = -1;
1266 int endc = -1;
1267 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001268 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001270 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271 switch (c)
1272 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001273 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001274 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001275
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001276 case Magic('^'):
1277 EMIT(NFA_BOL);
1278 break;
1279
1280 case Magic('$'):
1281 EMIT(NFA_EOL);
1282#if defined(FEAT_SYN_HL) || defined(PROTO)
1283 had_eol = TRUE;
1284#endif
1285 break;
1286
1287 case Magic('<'):
1288 EMIT(NFA_BOW);
1289 break;
1290
1291 case Magic('>'):
1292 EMIT(NFA_EOW);
1293 break;
1294
1295 case Magic('_'):
1296 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001297 if (c == NUL)
1298 EMSG_RET_FAIL(_(e_nul_found));
1299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001300 if (c == '^') /* "\_^" is start-of-line */
1301 {
1302 EMIT(NFA_BOL);
1303 break;
1304 }
1305 if (c == '$') /* "\_$" is end-of-line */
1306 {
1307 EMIT(NFA_EOL);
1308#if defined(FEAT_SYN_HL) || defined(PROTO)
1309 had_eol = TRUE;
1310#endif
1311 break;
1312 }
1313
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001314 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315
1316 /* "\_[" is collection plus newline */
1317 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001318 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319
1320 /* "\_x" is character class plus newline */
1321 /*FALLTHROUGH*/
1322
1323 /*
1324 * Character classes.
1325 */
1326 case Magic('.'):
1327 case Magic('i'):
1328 case Magic('I'):
1329 case Magic('k'):
1330 case Magic('K'):
1331 case Magic('f'):
1332 case Magic('F'):
1333 case Magic('p'):
1334 case Magic('P'):
1335 case Magic('s'):
1336 case Magic('S'):
1337 case Magic('d'):
1338 case Magic('D'):
1339 case Magic('x'):
1340 case Magic('X'):
1341 case Magic('o'):
1342 case Magic('O'):
1343 case Magic('w'):
1344 case Magic('W'):
1345 case Magic('h'):
1346 case Magic('H'):
1347 case Magic('a'):
1348 case Magic('A'):
1349 case Magic('l'):
1350 case Magic('L'):
1351 case Magic('u'):
1352 case Magic('U'):
1353 p = vim_strchr(classchars, no_Magic(c));
1354 if (p == NULL)
1355 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001356 if (extra == NFA_ADD_NL)
1357 {
1358 EMSGN(_(e_ill_char_class), c);
1359 rc_did_emsg = TRUE;
1360 return FAIL;
1361 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001362 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1363 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364 }
1365#ifdef FEAT_MBYTE
1366 /* When '.' is followed by a composing char ignore the dot, so that
1367 * the composing char is matched here. */
1368 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1369 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001370 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001371 c = getchr();
1372 goto nfa_do_multibyte;
1373 }
1374#endif
1375 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001376 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377 {
1378 EMIT(NFA_NEWL);
1379 EMIT(NFA_OR);
1380 regflags |= RF_HASNL;
1381 }
1382 break;
1383
1384 case Magic('n'):
1385 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001386 /* In a string "\n" matches a newline character. */
1387 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001388 else
1389 {
1390 /* In buffer text "\n" matches the end of a line. */
1391 EMIT(NFA_NEWL);
1392 regflags |= RF_HASNL;
1393 }
1394 break;
1395
1396 case Magic('('):
1397 if (nfa_reg(REG_PAREN) == FAIL)
1398 return FAIL; /* cascaded error */
1399 break;
1400
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001401 case Magic('|'):
1402 case Magic('&'):
1403 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001404 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405 return FAIL;
1406
1407 case Magic('='):
1408 case Magic('?'):
1409 case Magic('+'):
1410 case Magic('@'):
1411 case Magic('*'):
1412 case Magic('{'):
1413 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001414 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001415 return FAIL;
1416
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001417 case Magic('~'):
1418 {
1419 char_u *lp;
1420
1421 /* Previous substitute pattern.
1422 * Generated as "\%(pattern\)". */
1423 if (reg_prev_sub == NULL)
1424 {
1425 EMSG(_(e_nopresub));
1426 return FAIL;
1427 }
1428 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1429 {
1430 EMIT(PTR2CHAR(lp));
1431 if (lp != reg_prev_sub)
1432 EMIT(NFA_CONCAT);
1433 }
1434 EMIT(NFA_NOPEN);
1435 break;
1436 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437
Bram Moolenaar428e9872013-05-30 17:05:39 +02001438 case Magic('1'):
1439 case Magic('2'):
1440 case Magic('3'):
1441 case Magic('4'):
1442 case Magic('5'):
1443 case Magic('6'):
1444 case Magic('7'):
1445 case Magic('8'):
1446 case Magic('9'):
1447 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1448 nfa_has_backref = TRUE;
1449 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450
1451 case Magic('z'):
1452 c = no_Magic(getchr());
1453 switch (c)
1454 {
1455 case 's':
1456 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001457 if (re_mult_next("\\zs") == FAIL)
1458 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001459 break;
1460 case 'e':
1461 EMIT(NFA_ZEND);
1462 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001463 if (re_mult_next("\\ze") == FAIL)
1464 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001465 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001466#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 case '1':
1468 case '2':
1469 case '3':
1470 case '4':
1471 case '5':
1472 case '6':
1473 case '7':
1474 case '8':
1475 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001476 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001477 if (reg_do_extmatch != REX_USE)
1478 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001479 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1480 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001481 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001482 re_has_z = REX_USE;
1483 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001484 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001485 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001486 if (reg_do_extmatch != REX_SET)
1487 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001488 if (nfa_reg(REG_ZPAREN) == FAIL)
1489 return FAIL; /* cascaded error */
1490 re_has_z = REX_SET;
1491 break;
1492#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001494 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 no_Magic(c));
1496 return FAIL;
1497 }
1498 break;
1499
1500 case Magic('%'):
1501 c = no_Magic(getchr());
1502 switch (c)
1503 {
1504 /* () without a back reference */
1505 case '(':
1506 if (nfa_reg(REG_NPAREN) == FAIL)
1507 return FAIL;
1508 EMIT(NFA_NOPEN);
1509 break;
1510
1511 case 'd': /* %d123 decimal */
1512 case 'o': /* %o123 octal */
1513 case 'x': /* %xab hex 2 */
1514 case 'u': /* %uabcd hex 4 */
1515 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001516 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001517 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518
Bram Moolenaar47196582013-05-25 22:04:23 +02001519 switch (c)
1520 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001521 case 'd': nr = getdecchrs(); break;
1522 case 'o': nr = getoctchrs(); break;
1523 case 'x': nr = gethexchrs(2); break;
1524 case 'u': nr = gethexchrs(4); break;
1525 case 'U': nr = gethexchrs(8); break;
1526 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001527 }
1528
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001529 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001530 EMSG2_RET_FAIL(
1531 _("E678: Invalid character after %s%%[dxouU]"),
1532 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001533 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001534 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001535 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001536 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 break;
1538
1539 /* Catch \%^ and \%$ regardless of where they appear in the
1540 * pattern -- regardless of whether or not it makes sense. */
1541 case '^':
1542 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001543 break;
1544
1545 case '$':
1546 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001547 break;
1548
1549 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001550 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 break;
1552
1553 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001554 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 break;
1556
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001557 case 'C':
1558 EMIT(NFA_ANY_COMPOSING);
1559 break;
1560
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001562 {
1563 int n;
1564
1565 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001566 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001567 {
1568 if (c == NUL)
1569 EMSG2_RET_FAIL(_(e_missing_sb),
1570 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001571 /* recursive call! */
1572 if (nfa_regatom() == FAIL)
1573 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001574 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001575 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001576 if (n == 0)
1577 EMSG2_RET_FAIL(_(e_empty_sb),
1578 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001579 EMIT(NFA_OPT_CHARS);
1580 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001581
1582 /* Emit as "\%(\%[abc]\)" to be able to handle
1583 * "\%[abc]*" which would cause the empty string to be
1584 * matched an unlimited number of times. NFA_NOPEN is
1585 * added only once at a position, while NFA_SPLIT is
1586 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001587 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001588 * a lot. */
1589 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001590 break;
1591 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001592
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001594 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001595 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001596 int cmp = c;
1597
1598 if (c == '<' || c == '>')
1599 c = getchr();
1600 while (VIM_ISDIGIT(c))
1601 {
1602 n = n * 10 + (c - '0');
1603 c = getchr();
1604 }
1605 if (c == 'l' || c == 'c' || c == 'v')
1606 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001607 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001608 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001609 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001610 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001611 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001612 if (save_prev_at_start)
1613 at_start = TRUE;
1614 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001615 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001616 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001617 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001618 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001619 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001620 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001621 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001622 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001623 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001624 break;
1625 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001626 else if (c == '\'' && n == 0)
1627 {
1628 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001629 EMIT(cmp == '<' ? NFA_MARK_LT :
1630 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001631 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001632 break;
1633 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001634 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001635 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1636 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 return FAIL;
1638 }
1639 break;
1640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001641 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001642collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001643 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001644 * [abc] uses NFA_START_COLL - NFA_END_COLL
1645 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1646 * Each character is produced as a regular state, using
1647 * NFA_CONCAT to bind them together.
1648 * Besides normal characters there can be:
1649 * - character classes NFA_CLASS_*
1650 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 */
1652
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 p = regparse;
1654 endp = skip_anyof(p);
1655 if (*endp == ']')
1656 {
1657 /*
1658 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001659 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001660 * and perform the necessary substitutions in the NFA.
1661 */
1662 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001663 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001664 if (result != FAIL)
1665 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001666 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001667 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001668 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 EMIT(NFA_NEWL);
1670 EMIT(NFA_OR);
1671 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001672 else
1673 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001674 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001675 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 return OK;
1677 }
1678 /*
1679 * Failed to recognize a character class. Use the simple
1680 * version that turns [abc] into 'a' OR 'b' OR 'c'
1681 */
1682 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001683 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 if (*regparse == '^') /* negated range */
1685 {
1686 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001687 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001688 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001690 else
1691 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001692 if (*regparse == '-')
1693 {
1694 startc = '-';
1695 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001696 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001697 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698 }
1699 /* Emit the OR branches for each character in the [] */
1700 emit_range = FALSE;
1701 while (regparse < endp)
1702 {
1703 oldstartc = startc;
1704 startc = -1;
1705 got_coll_char = FALSE;
1706 if (*regparse == '[')
1707 {
1708 /* Check for [: :], [= =], [. .] */
1709 equiclass = collclass = 0;
1710 charclass = get_char_class(&regparse);
1711 if (charclass == CLASS_NONE)
1712 {
1713 equiclass = get_equi_class(&regparse);
1714 if (equiclass == 0)
1715 collclass = get_coll_element(&regparse);
1716 }
1717
1718 /* Character class like [:alpha:] */
1719 if (charclass != CLASS_NONE)
1720 {
1721 switch (charclass)
1722 {
1723 case CLASS_ALNUM:
1724 EMIT(NFA_CLASS_ALNUM);
1725 break;
1726 case CLASS_ALPHA:
1727 EMIT(NFA_CLASS_ALPHA);
1728 break;
1729 case CLASS_BLANK:
1730 EMIT(NFA_CLASS_BLANK);
1731 break;
1732 case CLASS_CNTRL:
1733 EMIT(NFA_CLASS_CNTRL);
1734 break;
1735 case CLASS_DIGIT:
1736 EMIT(NFA_CLASS_DIGIT);
1737 break;
1738 case CLASS_GRAPH:
1739 EMIT(NFA_CLASS_GRAPH);
1740 break;
1741 case CLASS_LOWER:
1742 EMIT(NFA_CLASS_LOWER);
1743 break;
1744 case CLASS_PRINT:
1745 EMIT(NFA_CLASS_PRINT);
1746 break;
1747 case CLASS_PUNCT:
1748 EMIT(NFA_CLASS_PUNCT);
1749 break;
1750 case CLASS_SPACE:
1751 EMIT(NFA_CLASS_SPACE);
1752 break;
1753 case CLASS_UPPER:
1754 EMIT(NFA_CLASS_UPPER);
1755 break;
1756 case CLASS_XDIGIT:
1757 EMIT(NFA_CLASS_XDIGIT);
1758 break;
1759 case CLASS_TAB:
1760 EMIT(NFA_CLASS_TAB);
1761 break;
1762 case CLASS_RETURN:
1763 EMIT(NFA_CLASS_RETURN);
1764 break;
1765 case CLASS_BACKSPACE:
1766 EMIT(NFA_CLASS_BACKSPACE);
1767 break;
1768 case CLASS_ESCAPE:
1769 EMIT(NFA_CLASS_ESCAPE);
1770 break;
1771 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001772 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 continue;
1774 }
1775 /* Try equivalence class [=a=] and the like */
1776 if (equiclass != 0)
1777 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001778 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001779 if (result == FAIL)
1780 {
1781 /* should never happen */
1782 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1783 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001784 continue;
1785 }
1786 /* Try collating class like [. .] */
1787 if (collclass != 0)
1788 {
1789 startc = collclass; /* allow [.a.]-x as a range */
1790 /* Will emit the proper atom at the end of the
1791 * while loop. */
1792 }
1793 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001794 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1795 * start character. */
1796 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 {
1798 emit_range = TRUE;
1799 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001800 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 continue; /* reading the end of the range */
1802 }
1803
1804 /* Now handle simple and escaped characters.
1805 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1806 * accepts "\t", "\e", etc., but only when the 'l' flag in
1807 * 'cpoptions' is not included.
1808 * Posix doesn't recognize backslash at all.
1809 */
1810 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001811 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812 && regparse + 1 <= endp
1813 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001814 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001815 && vim_strchr(REGEXP_ABBR, regparse[1])
1816 != NULL)
1817 )
1818 )
1819 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001820 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001822 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823 startc = reg_string ? NL : NFA_NEWL;
1824 else
1825 if (*regparse == 'd'
1826 || *regparse == 'o'
1827 || *regparse == 'x'
1828 || *regparse == 'u'
1829 || *regparse == 'U'
1830 )
1831 {
1832 /* TODO(RE) This needs more testing */
1833 startc = coll_get_char();
1834 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001835 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001836 }
1837 else
1838 {
1839 /* \r,\t,\e,\b */
1840 startc = backslash_trans(*regparse);
1841 }
1842 }
1843
1844 /* Normal printable char */
1845 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001846 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847
1848 /* Previous char was '-', so this char is end of range. */
1849 if (emit_range)
1850 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001851 endc = startc;
1852 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001853 if (startc > endc)
1854 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001855
1856 if (endc > startc + 2)
1857 {
1858 /* Emit a range instead of the sequence of
1859 * individual characters. */
1860 if (startc == 0)
1861 /* \x00 is translated to \x0a, start at \x01. */
1862 EMIT(1);
1863 else
1864 --post_ptr; /* remove NFA_CONCAT */
1865 EMIT(endc);
1866 EMIT(NFA_RANGE);
1867 EMIT(NFA_CONCAT);
1868 }
1869 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001871 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872 || (*mb_char2len)(endc) > 1))
1873 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001874 /* Emit the characters in the range.
1875 * "startc" was already emitted, so skip it.
1876 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 for (c = startc + 1; c <= endc; c++)
1878 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001879 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001880 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 }
1883 else
1884#endif
1885 {
1886#ifdef EBCDIC
1887 int alpha_only = FALSE;
1888
1889 /* for alphabetical range skip the gaps
1890 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1891 if (isalpha(startc) && isalpha(endc))
1892 alpha_only = TRUE;
1893#endif
1894 /* Emit the range. "startc" was already emitted, so
1895 * skip it. */
1896 for (c = startc + 1; c <= endc; c++)
1897#ifdef EBCDIC
1898 if (!alpha_only || isalpha(startc))
1899#endif
1900 {
1901 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001902 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001905 emit_range = FALSE;
1906 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 }
1908 else
1909 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001910 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001911 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 * Normally, simply emit startc. But if we get char
1913 * code=0 from a collating char, then replace it with
1914 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001916 * the backtracking engine. */
1917 if (startc == NFA_NEWL)
1918 {
1919 /* Line break can't be matched as part of the
1920 * collection, add an OR below. But not for negated
1921 * range. */
1922 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001923 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001924 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001926 {
1927 if (got_coll_char == TRUE && startc == 0)
1928 EMIT(0x0a);
1929 else
1930 EMIT(startc);
1931 EMIT(NFA_CONCAT);
1932 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 }
1934
Bram Moolenaar51a29832013-05-28 22:30:35 +02001935 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 } /* while (p < endp) */
1937
Bram Moolenaar51a29832013-05-28 22:30:35 +02001938 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 if (*regparse == '-') /* if last, '-' is just a char */
1940 {
1941 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001942 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001944
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 /* skip the trailing ] */
1946 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001947 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001948
1949 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001951 EMIT(NFA_END_NEG_COLL);
1952 else
1953 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001954
1955 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001956 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001957 {
1958 EMIT(reg_string ? NL : NFA_NEWL);
1959 EMIT(NFA_OR);
1960 }
1961
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001963 } /* if exists closing ] */
1964
1965 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001967 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001969 default:
1970 {
1971#ifdef FEAT_MBYTE
1972 int plen;
1973
1974nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001975 /* plen is length of current char with composing chars */
1976 if (enc_utf8 && ((*mb_char2len)(c)
1977 != (plen = (*mb_ptr2len)(old_regparse))
1978 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001979 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001980 int i = 0;
1981
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001982 /* A base character plus composing characters, or just one
1983 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001984 * This requires creating a separate atom as if enclosing
1985 * the characters in (), where NFA_COMPOSING is the ( and
1986 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001987 * building the postfix form, not the NFA itself;
1988 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001989 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001990 for (;;)
1991 {
1992 EMIT(c);
1993 if (i > 0)
1994 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001995 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001996 break;
1997 c = utf_ptr2char(old_regparse + i);
1998 }
1999 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002000 regparse = old_regparse + plen;
2001 }
2002 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003#endif
2004 {
2005 c = no_Magic(c);
2006 EMIT(c);
2007 }
2008 return OK;
2009 }
2010 }
2011
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002012 return OK;
2013}
2014
2015/*
2016 * Parse something followed by possible [*+=].
2017 *
2018 * A piece is an atom, possibly followed by a multi, an indication of how many
2019 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2020 * characters: "", "a", "aa", etc.
2021 *
2022 * piece ::= atom
2023 * or atom multi
2024 */
2025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002026nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002027{
2028 int i;
2029 int op;
2030 int ret;
2031 long minval, maxval;
2032 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002033 parse_state_T old_state;
2034 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002036 int old_post_pos;
2037 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 int quest;
2039
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002040 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2041 * next. */
2042 save_parse_state(&old_state);
2043
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002045 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046
2047 ret = nfa_regatom();
2048 if (ret == FAIL)
2049 return FAIL; /* cascaded error */
2050
2051 op = peekchr();
2052 if (re_multi_type(op) == NOT_MULTI)
2053 return OK;
2054
2055 skipchr();
2056 switch (op)
2057 {
2058 case Magic('*'):
2059 EMIT(NFA_STAR);
2060 break;
2061
2062 case Magic('+'):
2063 /*
2064 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2065 * first and only submatch would be "aaa". But the backtracking
2066 * engine interprets the plus as "try matching one more time", and
2067 * a* matches a second time at the end of the input, the empty
2068 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002069 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002071 * In order to be consistent with the old engine, we replace
2072 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002074 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002075 curchr = -1;
2076 if (nfa_regatom() == FAIL)
2077 return FAIL;
2078 EMIT(NFA_STAR);
2079 EMIT(NFA_CONCAT);
2080 skipchr(); /* skip the \+ */
2081 break;
2082
2083 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002084 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002086 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 switch(op)
2088 {
2089 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002090 /* \@= */
2091 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002092 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002093 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002094 /* \@! */
2095 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002096 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002097 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002098 op = no_Magic(getchr());
2099 if (op == '=')
2100 /* \@<= */
2101 i = NFA_PREV_ATOM_JUST_BEFORE;
2102 else if (op == '!')
2103 /* \@<! */
2104 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2105 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002106 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002107 /* \@> */
2108 i = NFA_PREV_ATOM_LIKE_PATTERN;
2109 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002110 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002111 if (i == 0)
2112 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002113 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2114 return FAIL;
2115 }
2116 EMIT(i);
2117 if (i == NFA_PREV_ATOM_JUST_BEFORE
2118 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2119 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002120 break;
2121
2122 case Magic('?'):
2123 case Magic('='):
2124 EMIT(NFA_QUEST);
2125 break;
2126
2127 case Magic('{'):
2128 /* a{2,5} will expand to 'aaa?a?a?'
2129 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2130 * version of '?'
2131 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2132 * parenthesis have the same id
2133 */
2134
2135 greedy = TRUE;
2136 c2 = peekchr();
2137 if (c2 == '-' || c2 == Magic('-'))
2138 {
2139 skipchr();
2140 greedy = FALSE;
2141 }
2142 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002143 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002144
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002145 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2146 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002147 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002148 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002149 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002150 /* \{}, \{0,} */
2151 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002152 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002153 /* \{-}, \{-0,} */
2154 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002155 break;
2156 }
2157
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002158 /* Special case: x{0} or x{-0} */
2159 if (maxval == 0)
2160 {
2161 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002162 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002163 /* NFA_EMPTY is 0-length and works everywhere */
2164 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 return OK;
2166 }
2167
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002168 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002169 * maximum is much larger than the minimum and when the maximum is
2170 * large. Bail out if we can use the other engine. */
2171 if ((nfa_re_flags & RE_AUTO)
2172 && (maxval > minval + 200 || maxval > 500))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002173 return FAIL;
2174
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002175 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002176 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002177 /* Save parse state after the repeated atom and the \{} */
2178 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2181 for (i = 0; i < maxval; i++)
2182 {
2183 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002184 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002185 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002186 if (nfa_regatom() == FAIL)
2187 return FAIL;
2188 /* after "minval" times, atoms are optional */
2189 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002190 {
2191 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002192 {
2193 if (greedy)
2194 EMIT(NFA_STAR);
2195 else
2196 EMIT(NFA_STAR_NONGREEDY);
2197 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002198 else
2199 EMIT(quest);
2200 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002201 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002202 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002203 if (i + 1 > minval && maxval == MAX_LIMIT)
2204 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002205 }
2206
2207 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002208 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002209 curchr = -1;
2210
2211 break;
2212
2213
2214 default:
2215 break;
2216 } /* end switch */
2217
2218 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002219 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221
2222 return OK;
2223}
2224
2225/*
2226 * Parse one or more pieces, concatenated. It matches a match for the
2227 * first piece, followed by a match for the second piece, etc. Example:
2228 * "f[0-9]b", first matches "f", then a digit and then "b".
2229 *
2230 * concat ::= piece
2231 * or piece piece
2232 * or piece piece piece
2233 * etc.
2234 */
2235 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002236nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237{
2238 int cont = TRUE;
2239 int first = TRUE;
2240
2241 while (cont)
2242 {
2243 switch (peekchr())
2244 {
2245 case NUL:
2246 case Magic('|'):
2247 case Magic('&'):
2248 case Magic(')'):
2249 cont = FALSE;
2250 break;
2251
2252 case Magic('Z'):
2253#ifdef FEAT_MBYTE
2254 regflags |= RF_ICOMBINE;
2255#endif
2256 skipchr_keepstart();
2257 break;
2258 case Magic('c'):
2259 regflags |= RF_ICASE;
2260 skipchr_keepstart();
2261 break;
2262 case Magic('C'):
2263 regflags |= RF_NOICASE;
2264 skipchr_keepstart();
2265 break;
2266 case Magic('v'):
2267 reg_magic = MAGIC_ALL;
2268 skipchr_keepstart();
2269 curchr = -1;
2270 break;
2271 case Magic('m'):
2272 reg_magic = MAGIC_ON;
2273 skipchr_keepstart();
2274 curchr = -1;
2275 break;
2276 case Magic('M'):
2277 reg_magic = MAGIC_OFF;
2278 skipchr_keepstart();
2279 curchr = -1;
2280 break;
2281 case Magic('V'):
2282 reg_magic = MAGIC_NONE;
2283 skipchr_keepstart();
2284 curchr = -1;
2285 break;
2286
2287 default:
2288 if (nfa_regpiece() == FAIL)
2289 return FAIL;
2290 if (first == FALSE)
2291 EMIT(NFA_CONCAT);
2292 else
2293 first = FALSE;
2294 break;
2295 }
2296 }
2297
2298 return OK;
2299}
2300
2301/*
2302 * Parse a branch, one or more concats, separated by "\&". It matches the
2303 * last concat, but only if all the preceding concats also match at the same
2304 * position. Examples:
2305 * "foobeep\&..." matches "foo" in "foobeep".
2306 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2307 *
2308 * branch ::= concat
2309 * or concat \& concat
2310 * or concat \& concat \& concat
2311 * etc.
2312 */
2313 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002314nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002315{
2316 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002317 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002318
Bram Moolenaar16299b52013-05-30 18:45:23 +02002319 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002320
2321 /* First branch, possibly the only one */
2322 if (nfa_regconcat() == FAIL)
2323 return FAIL;
2324
2325 ch = peekchr();
2326 /* Try next concats */
2327 while (ch == Magic('&'))
2328 {
2329 skipchr();
2330 EMIT(NFA_NOPEN);
2331 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002332 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002333 if (nfa_regconcat() == FAIL)
2334 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002335 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002336 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002337 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002338 EMIT(NFA_CONCAT);
2339 ch = peekchr();
2340 }
2341
Bram Moolenaar699c1202013-09-25 16:41:54 +02002342 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002343 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002344 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002345
2346 return OK;
2347}
2348
2349/*
2350 * Parse a pattern, one or more branches, separated by "\|". It matches
2351 * anything that matches one of the branches. Example: "foo\|beep" matches
2352 * "foo" and matches "beep". If more than one branch matches, the first one
2353 * is used.
2354 *
2355 * pattern ::= branch
2356 * or branch \| branch
2357 * or branch \| branch \| branch
2358 * etc.
2359 */
2360 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002361nfa_reg(
2362 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002363{
2364 int parno = 0;
2365
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002366 if (paren == REG_PAREN)
2367 {
2368 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370 parno = regnpar++;
2371 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002372#ifdef FEAT_SYN_HL
2373 else if (paren == REG_ZPAREN)
2374 {
2375 /* Make a ZOPEN node. */
2376 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002377 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002378 parno = regnzpar++;
2379 }
2380#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381
2382 if (nfa_regbranch() == FAIL)
2383 return FAIL; /* cascaded error */
2384
2385 while (peekchr() == Magic('|'))
2386 {
2387 skipchr();
2388 if (nfa_regbranch() == FAIL)
2389 return FAIL; /* cascaded error */
2390 EMIT(NFA_OR);
2391 }
2392
2393 /* Check for proper termination. */
2394 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2395 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396 if (paren == REG_NPAREN)
2397 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2398 else
2399 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2400 }
2401 else if (paren == REG_NOPAREN && peekchr() != NUL)
2402 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002403 if (peekchr() == Magic(')'))
2404 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2405 else
2406 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2407 }
2408 /*
2409 * Here we set the flag allowing back references to this set of
2410 * parentheses.
2411 */
2412 if (paren == REG_PAREN)
2413 {
2414 had_endbrace[parno] = TRUE; /* have seen the close paren */
2415 EMIT(NFA_MOPEN + parno);
2416 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002417#ifdef FEAT_SYN_HL
2418 else if (paren == REG_ZPAREN)
2419 EMIT(NFA_ZOPEN + parno);
2420#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421
2422 return OK;
2423}
2424
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425#ifdef DEBUG
2426static char_u code[50];
2427
2428 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002429nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430{
2431 int addnl = FALSE;
2432
2433 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2434 {
2435 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002436 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002437 }
2438
2439 STRCPY(code, "");
2440 switch (c)
2441 {
2442 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2443 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2444 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2445 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2446 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2447 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2448
Bram Moolenaar5714b802013-05-28 22:03:20 +02002449 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2450 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2451 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2452 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2453 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2454 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2455 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2456 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2457 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002458#ifdef FEAT_SYN_HL
2459 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2460 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2461 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2462 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2463 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2464 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2465 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2466 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2467 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2468#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002469 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2470
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471 case NFA_PREV_ATOM_NO_WIDTH:
2472 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002473 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2474 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002475 case NFA_PREV_ATOM_JUST_BEFORE:
2476 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2477 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2478 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002479 case NFA_PREV_ATOM_LIKE_PATTERN:
2480 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2481
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002482 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2483 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002484 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002485 case NFA_START_INVISIBLE_FIRST:
2486 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002487 case NFA_START_INVISIBLE_NEG:
2488 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002489 case NFA_START_INVISIBLE_NEG_FIRST:
2490 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002491 case NFA_START_INVISIBLE_BEFORE:
2492 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002493 case NFA_START_INVISIBLE_BEFORE_FIRST:
2494 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002495 case NFA_START_INVISIBLE_BEFORE_NEG:
2496 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002497 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2498 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002499 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002500 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002501 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002502 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2505 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002506 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002507
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002508 case NFA_MOPEN:
2509 case NFA_MOPEN1:
2510 case NFA_MOPEN2:
2511 case NFA_MOPEN3:
2512 case NFA_MOPEN4:
2513 case NFA_MOPEN5:
2514 case NFA_MOPEN6:
2515 case NFA_MOPEN7:
2516 case NFA_MOPEN8:
2517 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002518 STRCPY(code, "NFA_MOPEN(x)");
2519 code[10] = c - NFA_MOPEN + '0';
2520 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002521 case NFA_MCLOSE:
2522 case NFA_MCLOSE1:
2523 case NFA_MCLOSE2:
2524 case NFA_MCLOSE3:
2525 case NFA_MCLOSE4:
2526 case NFA_MCLOSE5:
2527 case NFA_MCLOSE6:
2528 case NFA_MCLOSE7:
2529 case NFA_MCLOSE8:
2530 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002531 STRCPY(code, "NFA_MCLOSE(x)");
2532 code[11] = c - NFA_MCLOSE + '0';
2533 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002534#ifdef FEAT_SYN_HL
2535 case NFA_ZOPEN:
2536 case NFA_ZOPEN1:
2537 case NFA_ZOPEN2:
2538 case NFA_ZOPEN3:
2539 case NFA_ZOPEN4:
2540 case NFA_ZOPEN5:
2541 case NFA_ZOPEN6:
2542 case NFA_ZOPEN7:
2543 case NFA_ZOPEN8:
2544 case NFA_ZOPEN9:
2545 STRCPY(code, "NFA_ZOPEN(x)");
2546 code[10] = c - NFA_ZOPEN + '0';
2547 break;
2548 case NFA_ZCLOSE:
2549 case NFA_ZCLOSE1:
2550 case NFA_ZCLOSE2:
2551 case NFA_ZCLOSE3:
2552 case NFA_ZCLOSE4:
2553 case NFA_ZCLOSE5:
2554 case NFA_ZCLOSE6:
2555 case NFA_ZCLOSE7:
2556 case NFA_ZCLOSE8:
2557 case NFA_ZCLOSE9:
2558 STRCPY(code, "NFA_ZCLOSE(x)");
2559 code[11] = c - NFA_ZCLOSE + '0';
2560 break;
2561#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2563 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2564 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2565 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002566 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2567 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002568 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2569 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2570 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2571 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2572 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2573 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2574 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2575 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2576 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2577 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2578 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2579 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2580 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2581 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002582 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002584 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002585 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2586 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2587 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002588 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002589 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002590
2591 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2592 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2593 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2594 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2595 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2596 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2597 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2598
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2600 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2601 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2602 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2603 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2604 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2605 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2606 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2607 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2608 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2609 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2610 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2611 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2612 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2613 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2614 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2615
2616 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2617 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2618 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2619 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2620 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2621 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2622 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2623 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2624 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2625 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2626 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2627 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2628 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2629 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2630 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2631 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2632 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2633 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2634 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2635 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2636 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2637 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2638 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2639 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2640 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2641 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2642 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002643 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2644 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2645 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2646 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647
2648 default:
2649 STRCPY(code, "CHAR(x)");
2650 code[5] = c;
2651 }
2652
2653 if (addnl == TRUE)
2654 STRCAT(code, " + NEWLINE ");
2655
2656}
2657
2658#ifdef ENABLE_LOG
2659static FILE *log_fd;
2660
2661/*
2662 * Print the postfix notation of the current regexp.
2663 */
2664 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002665nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002666{
2667 int *p;
2668 FILE *f;
2669
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002670 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002671 if (f != NULL)
2672 {
2673 fprintf(f, "\n-------------------------\n");
2674 if (retval == FAIL)
2675 fprintf(f, ">>> NFA engine failed ... \n");
2676 else if (retval == OK)
2677 fprintf(f, ">>> NFA engine succeeded !\n");
2678 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002679 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002680 {
2681 nfa_set_code(*p);
2682 fprintf(f, "%s, ", code);
2683 }
2684 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002685 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002686 fprintf(f, "%d ", *p);
2687 fprintf(f, "\n\n");
2688 fclose(f);
2689 }
2690}
2691
2692/*
2693 * Print the NFA starting with a root node "state".
2694 */
2695 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002696nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002698 garray_T indent;
2699
2700 ga_init2(&indent, 1, 64);
2701 ga_append(&indent, '\0');
2702 nfa_print_state2(debugf, state, &indent);
2703 ga_clear(&indent);
2704}
2705
2706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002707nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002708{
2709 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002710
2711 if (state == NULL)
2712 return;
2713
2714 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002715
2716 /* Output indent */
2717 p = (char_u *)indent->ga_data;
2718 if (indent->ga_len >= 3)
2719 {
2720 int last = indent->ga_len - 3;
2721 char_u save[2];
2722
2723 STRNCPY(save, &p[last], 2);
2724 STRNCPY(&p[last], "+-", 2);
2725 fprintf(debugf, " %s", p);
2726 STRNCPY(&p[last], save, 2);
2727 }
2728 else
2729 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730
2731 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002732 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002733 code,
2734 state->c,
2735 abs(state->id),
2736 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002737 if (state->id < 0)
2738 return;
2739
2740 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002741
2742 /* grow indent for state->out */
2743 indent->ga_len -= 1;
2744 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002745 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002746 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002747 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002748 ga_append(indent, '\0');
2749
2750 nfa_print_state2(debugf, state->out, indent);
2751
2752 /* replace last part of indent for state->out1 */
2753 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002754 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002755 ga_append(indent, '\0');
2756
2757 nfa_print_state2(debugf, state->out1, indent);
2758
2759 /* shrink indent */
2760 indent->ga_len -= 3;
2761 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762}
2763
2764/*
2765 * Print the NFA state machine.
2766 */
2767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002768nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002769{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002770 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002771
2772 if (debugf != NULL)
2773 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002774 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002775
Bram Moolenaar473de612013-06-08 18:19:48 +02002776 if (prog->reganch)
2777 fprintf(debugf, "reganch: %d\n", prog->reganch);
2778 if (prog->regstart != NUL)
2779 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2780 prog->regstart, prog->regstart);
2781 if (prog->match_text != NULL)
2782 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002783
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002784 fclose(debugf);
2785 }
2786}
2787#endif /* ENABLE_LOG */
2788#endif /* DEBUG */
2789
2790/*
2791 * Parse r.e. @expr and convert it into postfix form.
2792 * Return the postfix string on success, NULL otherwise.
2793 */
2794 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002795re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002796{
2797 if (nfa_reg(REG_NOPAREN) == FAIL)
2798 return NULL;
2799 EMIT(NFA_MOPEN);
2800 return post_start;
2801}
2802
2803/* NB. Some of the code below is inspired by Russ's. */
2804
2805/*
2806 * Represents an NFA state plus zero or one or two arrows exiting.
2807 * if c == MATCH, no arrows out; matching state.
2808 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2809 * If c < 256, labeled arrow with character c to out.
2810 */
2811
2812static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2813
2814/*
2815 * Allocate and initialize nfa_state_T.
2816 */
2817 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002818alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002819{
2820 nfa_state_T *s;
2821
2822 if (istate >= nstate)
2823 return NULL;
2824
2825 s = &state_ptr[istate++];
2826
2827 s->c = c;
2828 s->out = out;
2829 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002830 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831
2832 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002833 s->lastlist[0] = 0;
2834 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835
2836 return s;
2837}
2838
2839/*
2840 * A partially built NFA without the matching state filled in.
2841 * Frag_T.start points at the start state.
2842 * Frag_T.out is a list of places that need to be set to the
2843 * next state for this fragment.
2844 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002845
2846/* Since the out pointers in the list are always
2847 * uninitialized, we use the pointers themselves
2848 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002850union Ptrlist
2851{
2852 Ptrlist *next;
2853 nfa_state_T *s;
2854};
2855
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002856struct Frag
2857{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002858 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002859 Ptrlist *out;
2860};
2861typedef struct Frag Frag_T;
2862
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002863static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2864static Ptrlist *list1(nfa_state_T **outp);
2865static void patch(Ptrlist *l, nfa_state_T *s);
2866static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2867static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2868static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869
2870/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002871 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002872 */
2873 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002874frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002876 Frag_T n;
2877
2878 n.start = start;
2879 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002880 return n;
2881}
2882
2883/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884 * Create singleton list containing just outp.
2885 */
2886 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002887list1(
2888 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002889{
2890 Ptrlist *l;
2891
2892 l = (Ptrlist *)outp;
2893 l->next = NULL;
2894 return l;
2895}
2896
2897/*
2898 * Patch the list of states at out to point to start.
2899 */
2900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002901patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002902{
2903 Ptrlist *next;
2904
2905 for (; l; l = next)
2906 {
2907 next = l->next;
2908 l->s = s;
2909 }
2910}
2911
2912
2913/*
2914 * Join the two lists l1 and l2, returning the combination.
2915 */
2916 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002917append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918{
2919 Ptrlist *oldl1;
2920
2921 oldl1 = l1;
2922 while (l1->next)
2923 l1 = l1->next;
2924 l1->next = l2;
2925 return oldl1;
2926}
2927
2928/*
2929 * Stack used for transforming postfix form into NFA.
2930 */
2931static Frag_T empty;
2932
2933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002934st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002935{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002936#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002937 FILE *df;
2938 int *p2;
2939
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002940 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002941 if (df)
2942 {
2943 fprintf(df, "Error popping the stack!\n");
2944#ifdef DEBUG
2945 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2946#endif
2947 fprintf(df, "Postfix form is: ");
2948#ifdef DEBUG
2949 for (p2 = postfix; p2 < end; p2++)
2950 {
2951 nfa_set_code(*p2);
2952 fprintf(df, "%s, ", code);
2953 }
2954 nfa_set_code(*p);
2955 fprintf(df, "\nCurrent position is: ");
2956 for (p2 = postfix; p2 <= p; p2 ++)
2957 {
2958 nfa_set_code(*p2);
2959 fprintf(df, "%s, ", code);
2960 }
2961#else
2962 for (p2 = postfix; p2 < end; p2++)
2963 {
2964 fprintf(df, "%d, ", *p2);
2965 }
2966 fprintf(df, "\nCurrent position is: ");
2967 for (p2 = postfix; p2 <= p; p2 ++)
2968 {
2969 fprintf(df, "%d, ", *p2);
2970 }
2971#endif
2972 fprintf(df, "\n--------------------------\n");
2973 fclose(df);
2974 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002975#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976 EMSG(_("E874: (NFA) Could not pop the stack !"));
2977}
2978
2979/*
2980 * Push an item onto the stack.
2981 */
2982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002983st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002984{
2985 Frag_T *stackp = *p;
2986
2987 if (stackp >= stack_end)
2988 return;
2989 *stackp = s;
2990 *p = *p + 1;
2991}
2992
2993/*
2994 * Pop an item from the stack.
2995 */
2996 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002997st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002998{
2999 Frag_T *stackp;
3000
3001 *p = *p - 1;
3002 stackp = *p;
3003 if (stackp < stack)
3004 return empty;
3005 return **p;
3006}
3007
3008/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003009 * Estimate the maximum byte length of anything matching "state".
3010 * When unknown or unlimited return -1.
3011 */
3012 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003013nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003014{
3015 int l, r;
3016 nfa_state_T *state = startstate;
3017 int len = 0;
3018
3019 /* detect looping in a NFA_SPLIT */
3020 if (depth > 4)
3021 return -1;
3022
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003023 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003024 {
3025 switch (state->c)
3026 {
3027 case NFA_END_INVISIBLE:
3028 case NFA_END_INVISIBLE_NEG:
3029 /* the end, return what we have */
3030 return len;
3031
3032 case NFA_SPLIT:
3033 /* two alternatives, use the maximum */
3034 l = nfa_max_width(state->out, depth + 1);
3035 r = nfa_max_width(state->out1, depth + 1);
3036 if (l < 0 || r < 0)
3037 return -1;
3038 return len + (l > r ? l : r);
3039
3040 case NFA_ANY:
3041 case NFA_START_COLL:
3042 case NFA_START_NEG_COLL:
3043 /* matches some character, including composing chars */
3044#ifdef FEAT_MBYTE
3045 if (enc_utf8)
3046 len += MB_MAXBYTES;
3047 else if (has_mbyte)
3048 len += 2;
3049 else
3050#endif
3051 ++len;
3052 if (state->c != NFA_ANY)
3053 {
3054 /* skip over the characters */
3055 state = state->out1->out;
3056 continue;
3057 }
3058 break;
3059
3060 case NFA_DIGIT:
3061 case NFA_WHITE:
3062 case NFA_HEX:
3063 case NFA_OCTAL:
3064 /* ascii */
3065 ++len;
3066 break;
3067
3068 case NFA_IDENT:
3069 case NFA_SIDENT:
3070 case NFA_KWORD:
3071 case NFA_SKWORD:
3072 case NFA_FNAME:
3073 case NFA_SFNAME:
3074 case NFA_PRINT:
3075 case NFA_SPRINT:
3076 case NFA_NWHITE:
3077 case NFA_NDIGIT:
3078 case NFA_NHEX:
3079 case NFA_NOCTAL:
3080 case NFA_WORD:
3081 case NFA_NWORD:
3082 case NFA_HEAD:
3083 case NFA_NHEAD:
3084 case NFA_ALPHA:
3085 case NFA_NALPHA:
3086 case NFA_LOWER:
3087 case NFA_NLOWER:
3088 case NFA_UPPER:
3089 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003090 case NFA_LOWER_IC:
3091 case NFA_NLOWER_IC:
3092 case NFA_UPPER_IC:
3093 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003094 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003095 /* possibly non-ascii */
3096#ifdef FEAT_MBYTE
3097 if (has_mbyte)
3098 len += 3;
3099 else
3100#endif
3101 ++len;
3102 break;
3103
3104 case NFA_START_INVISIBLE:
3105 case NFA_START_INVISIBLE_NEG:
3106 case NFA_START_INVISIBLE_BEFORE:
3107 case NFA_START_INVISIBLE_BEFORE_NEG:
3108 /* zero-width, out1 points to the END state */
3109 state = state->out1->out;
3110 continue;
3111
3112 case NFA_BACKREF1:
3113 case NFA_BACKREF2:
3114 case NFA_BACKREF3:
3115 case NFA_BACKREF4:
3116 case NFA_BACKREF5:
3117 case NFA_BACKREF6:
3118 case NFA_BACKREF7:
3119 case NFA_BACKREF8:
3120 case NFA_BACKREF9:
3121#ifdef FEAT_SYN_HL
3122 case NFA_ZREF1:
3123 case NFA_ZREF2:
3124 case NFA_ZREF3:
3125 case NFA_ZREF4:
3126 case NFA_ZREF5:
3127 case NFA_ZREF6:
3128 case NFA_ZREF7:
3129 case NFA_ZREF8:
3130 case NFA_ZREF9:
3131#endif
3132 case NFA_NEWL:
3133 case NFA_SKIP:
3134 /* unknown width */
3135 return -1;
3136
3137 case NFA_BOL:
3138 case NFA_EOL:
3139 case NFA_BOF:
3140 case NFA_EOF:
3141 case NFA_BOW:
3142 case NFA_EOW:
3143 case NFA_MOPEN:
3144 case NFA_MOPEN1:
3145 case NFA_MOPEN2:
3146 case NFA_MOPEN3:
3147 case NFA_MOPEN4:
3148 case NFA_MOPEN5:
3149 case NFA_MOPEN6:
3150 case NFA_MOPEN7:
3151 case NFA_MOPEN8:
3152 case NFA_MOPEN9:
3153#ifdef FEAT_SYN_HL
3154 case NFA_ZOPEN:
3155 case NFA_ZOPEN1:
3156 case NFA_ZOPEN2:
3157 case NFA_ZOPEN3:
3158 case NFA_ZOPEN4:
3159 case NFA_ZOPEN5:
3160 case NFA_ZOPEN6:
3161 case NFA_ZOPEN7:
3162 case NFA_ZOPEN8:
3163 case NFA_ZOPEN9:
3164 case NFA_ZCLOSE:
3165 case NFA_ZCLOSE1:
3166 case NFA_ZCLOSE2:
3167 case NFA_ZCLOSE3:
3168 case NFA_ZCLOSE4:
3169 case NFA_ZCLOSE5:
3170 case NFA_ZCLOSE6:
3171 case NFA_ZCLOSE7:
3172 case NFA_ZCLOSE8:
3173 case NFA_ZCLOSE9:
3174#endif
3175 case NFA_MCLOSE:
3176 case NFA_MCLOSE1:
3177 case NFA_MCLOSE2:
3178 case NFA_MCLOSE3:
3179 case NFA_MCLOSE4:
3180 case NFA_MCLOSE5:
3181 case NFA_MCLOSE6:
3182 case NFA_MCLOSE7:
3183 case NFA_MCLOSE8:
3184 case NFA_MCLOSE9:
3185 case NFA_NOPEN:
3186 case NFA_NCLOSE:
3187
3188 case NFA_LNUM_GT:
3189 case NFA_LNUM_LT:
3190 case NFA_COL_GT:
3191 case NFA_COL_LT:
3192 case NFA_VCOL_GT:
3193 case NFA_VCOL_LT:
3194 case NFA_MARK_GT:
3195 case NFA_MARK_LT:
3196 case NFA_VISUAL:
3197 case NFA_LNUM:
3198 case NFA_CURSOR:
3199 case NFA_COL:
3200 case NFA_VCOL:
3201 case NFA_MARK:
3202
3203 case NFA_ZSTART:
3204 case NFA_ZEND:
3205 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003206 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003207 case NFA_START_PATTERN:
3208 case NFA_END_PATTERN:
3209 case NFA_COMPOSING:
3210 case NFA_END_COMPOSING:
3211 /* zero-width */
3212 break;
3213
3214 default:
3215 if (state->c < 0)
3216 /* don't know what this is */
3217 return -1;
3218 /* normal character */
3219 len += MB_CHAR2LEN(state->c);
3220 break;
3221 }
3222
3223 /* normal way to continue */
3224 state = state->out;
3225 }
3226
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003227 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003228 return -1;
3229}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003230
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003231/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232 * Convert a postfix form into its equivalent NFA.
3233 * Return the NFA start state on success, NULL otherwise.
3234 */
3235 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003236post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237{
3238 int *p;
3239 int mopen;
3240 int mclose;
3241 Frag_T *stack = NULL;
3242 Frag_T *stackp = NULL;
3243 Frag_T *stack_end = NULL;
3244 Frag_T e1;
3245 Frag_T e2;
3246 Frag_T e;
3247 nfa_state_T *s;
3248 nfa_state_T *s1;
3249 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003250 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251
3252 if (postfix == NULL)
3253 return NULL;
3254
Bram Moolenaar053bb602013-05-20 13:55:21 +02003255#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256#define POP() st_pop(&stackp, stack); \
3257 if (stackp < stack) \
3258 { \
3259 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003260 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 return NULL; \
3262 }
3263
3264 if (nfa_calc_size == FALSE)
3265 {
3266 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003267 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003269 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 }
3271
3272 for (p = postfix; p < end; ++p)
3273 {
3274 switch (*p)
3275 {
3276 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003277 /* Concatenation.
3278 * Pay attention: this operator does not exist in the r.e. itself
3279 * (it is implicit, really). It is added when r.e. is translated
3280 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 if (nfa_calc_size == TRUE)
3282 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003283 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 break;
3285 }
3286 e2 = POP();
3287 e1 = POP();
3288 patch(e1.out, e2.start);
3289 PUSH(frag(e1.start, e2.out));
3290 break;
3291
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 case NFA_OR:
3293 /* Alternation */
3294 if (nfa_calc_size == TRUE)
3295 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003296 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 break;
3298 }
3299 e2 = POP();
3300 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003301 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003303 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304 PUSH(frag(s, append(e1.out, e2.out)));
3305 break;
3306
3307 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003308 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003309 if (nfa_calc_size == TRUE)
3310 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003311 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 break;
3313 }
3314 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003315 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003317 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 patch(e.out, s);
3319 PUSH(frag(s, list1(&s->out1)));
3320 break;
3321
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003322 case NFA_STAR_NONGREEDY:
3323 /* Zero or more, prefer zero */
3324 if (nfa_calc_size == TRUE)
3325 {
3326 nstate++;
3327 break;
3328 }
3329 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003330 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003331 if (s == NULL)
3332 goto theend;
3333 patch(e.out, s);
3334 PUSH(frag(s, list1(&s->out)));
3335 break;
3336
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003337 case NFA_QUEST:
3338 /* one or zero atoms=> greedy match */
3339 if (nfa_calc_size == TRUE)
3340 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003341 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003342 break;
3343 }
3344 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003345 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003347 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 PUSH(frag(s, append(e.out, list1(&s->out1))));
3349 break;
3350
3351 case NFA_QUEST_NONGREEDY:
3352 /* zero or one atoms => non-greedy match */
3353 if (nfa_calc_size == TRUE)
3354 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003355 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003356 break;
3357 }
3358 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003359 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003361 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 PUSH(frag(s, append(e.out, list1(&s->out))));
3363 break;
3364
Bram Moolenaar417bad22013-06-07 14:08:30 +02003365 case NFA_END_COLL:
3366 case NFA_END_NEG_COLL:
3367 /* On the stack is the sequence starting with NFA_START_COLL or
3368 * NFA_START_NEG_COLL and all possible characters. Patch it to
3369 * add the output to the start. */
3370 if (nfa_calc_size == TRUE)
3371 {
3372 nstate++;
3373 break;
3374 }
3375 e = POP();
3376 s = alloc_state(NFA_END_COLL, NULL, NULL);
3377 if (s == NULL)
3378 goto theend;
3379 patch(e.out, s);
3380 e.start->out1 = s;
3381 PUSH(frag(e.start, list1(&s->out)));
3382 break;
3383
3384 case NFA_RANGE:
3385 /* Before this are two characters, the low and high end of a
3386 * range. Turn them into two states with MIN and MAX. */
3387 if (nfa_calc_size == TRUE)
3388 {
3389 /* nstate += 0; */
3390 break;
3391 }
3392 e2 = POP();
3393 e1 = POP();
3394 e2.start->val = e2.start->c;
3395 e2.start->c = NFA_RANGE_MAX;
3396 e1.start->val = e1.start->c;
3397 e1.start->c = NFA_RANGE_MIN;
3398 patch(e1.out, e2.start);
3399 PUSH(frag(e1.start, e2.out));
3400 break;
3401
Bram Moolenaar699c1202013-09-25 16:41:54 +02003402 case NFA_EMPTY:
3403 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404 if (nfa_calc_size == TRUE)
3405 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003406 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003407 break;
3408 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003409 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003410 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003411 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 PUSH(frag(s, list1(&s->out)));
3413 break;
3414
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003415 case NFA_OPT_CHARS:
3416 {
3417 int n;
3418
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003419 /* \%[abc] implemented as:
3420 * NFA_SPLIT
3421 * +-CHAR(a)
3422 * | +-NFA_SPLIT
3423 * | +-CHAR(b)
3424 * | | +-NFA_SPLIT
3425 * | | +-CHAR(c)
3426 * | | | +-next
3427 * | | +- next
3428 * | +- next
3429 * +- next
3430 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003431 n = *++p; /* get number of characters */
3432 if (nfa_calc_size == TRUE)
3433 {
3434 nstate += n;
3435 break;
3436 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003437 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003438 e1.out = NULL; /* stores list with out1's */
3439 s1 = NULL; /* previous NFA_SPLIT to connect to */
3440 while (n-- > 0)
3441 {
3442 e = POP(); /* get character */
3443 s = alloc_state(NFA_SPLIT, e.start, NULL);
3444 if (s == NULL)
3445 goto theend;
3446 if (e1.out == NULL)
3447 e1 = e;
3448 patch(e.out, s1);
3449 append(e1.out, list1(&s->out1));
3450 s1 = s;
3451 }
3452 PUSH(frag(s, e1.out));
3453 break;
3454 }
3455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003457 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003458 case NFA_PREV_ATOM_JUST_BEFORE:
3459 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003460 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003461 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003462 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3463 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003464 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003465 int start_state;
3466 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003467 int n = 0;
3468 nfa_state_T *zend;
3469 nfa_state_T *skip;
3470
Bram Moolenaardecd9542013-06-07 16:31:50 +02003471 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003472 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003473 case NFA_PREV_ATOM_NO_WIDTH:
3474 start_state = NFA_START_INVISIBLE;
3475 end_state = NFA_END_INVISIBLE;
3476 break;
3477 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3478 start_state = NFA_START_INVISIBLE_NEG;
3479 end_state = NFA_END_INVISIBLE_NEG;
3480 break;
3481 case NFA_PREV_ATOM_JUST_BEFORE:
3482 start_state = NFA_START_INVISIBLE_BEFORE;
3483 end_state = NFA_END_INVISIBLE;
3484 break;
3485 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3486 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3487 end_state = NFA_END_INVISIBLE_NEG;
3488 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003489 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003490 start_state = NFA_START_PATTERN;
3491 end_state = NFA_END_PATTERN;
3492 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003493 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003494
3495 if (before)
3496 n = *++p; /* get the count */
3497
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003498 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003499 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003500 * The \@<= operator: match for the preceding atom.
3501 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003503 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504
3505 if (nfa_calc_size == TRUE)
3506 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003507 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003508 break;
3509 }
3510 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003511 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003512 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003513 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514
Bram Moolenaar87953742013-06-05 18:52:40 +02003515 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003516 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003517 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003518 if (pattern)
3519 {
3520 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3521 skip = alloc_state(NFA_SKIP, NULL, NULL);
3522 zend = alloc_state(NFA_ZEND, s1, NULL);
3523 s1->out= skip;
3524 patch(e.out, zend);
3525 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003526 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003527 else
3528 {
3529 patch(e.out, s1);
3530 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003531 if (before)
3532 {
3533 if (n <= 0)
3534 /* See if we can guess the maximum width, it avoids a
3535 * lot of pointless tries. */
3536 n = nfa_max_width(e.start, 0);
3537 s->val = n; /* store the count */
3538 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003539 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003541 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003543#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003544 case NFA_COMPOSING: /* char with composing char */
3545#if 0
3546 /* TODO */
3547 if (regflags & RF_ICOMBINE)
3548 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003549 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003550 }
3551#endif
3552 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003553#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003554
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003555 case NFA_MOPEN: /* \( \) Submatch */
3556 case NFA_MOPEN1:
3557 case NFA_MOPEN2:
3558 case NFA_MOPEN3:
3559 case NFA_MOPEN4:
3560 case NFA_MOPEN5:
3561 case NFA_MOPEN6:
3562 case NFA_MOPEN7:
3563 case NFA_MOPEN8:
3564 case NFA_MOPEN9:
3565#ifdef FEAT_SYN_HL
3566 case NFA_ZOPEN: /* \z( \) Submatch */
3567 case NFA_ZOPEN1:
3568 case NFA_ZOPEN2:
3569 case NFA_ZOPEN3:
3570 case NFA_ZOPEN4:
3571 case NFA_ZOPEN5:
3572 case NFA_ZOPEN6:
3573 case NFA_ZOPEN7:
3574 case NFA_ZOPEN8:
3575 case NFA_ZOPEN9:
3576#endif
3577 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578 if (nfa_calc_size == TRUE)
3579 {
3580 nstate += 2;
3581 break;
3582 }
3583
3584 mopen = *p;
3585 switch (*p)
3586 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003587 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3588#ifdef FEAT_SYN_HL
3589 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3590 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3591 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3592 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3593 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3594 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3595 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3596 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3597 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3598 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3599#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003600#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003601 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003602#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003603 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003604 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 mclose = *p + NSUBEXP;
3606 break;
3607 }
3608
3609 /* Allow "NFA_MOPEN" as a valid postfix representation for
3610 * the empty regexp "". In this case, the NFA will be
3611 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3612 * empty groups of parenthesis, and empty mbyte chars */
3613 if (stackp == stack)
3614 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003615 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003616 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003617 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003618 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003620 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621 patch(list1(&s->out), s1);
3622 PUSH(frag(s, list1(&s1->out)));
3623 break;
3624 }
3625
3626 /* At least one node was emitted before NFA_MOPEN, so
3627 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3628 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003629 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003631 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632
Bram Moolenaar525666f2013-06-02 16:40:55 +02003633 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003635 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 patch(e.out, s1);
3637
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003638#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003639 if (mopen == NFA_COMPOSING)
3640 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003641 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003642#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643
3644 PUSH(frag(s, list1(&s1->out)));
3645 break;
3646
Bram Moolenaar5714b802013-05-28 22:03:20 +02003647 case NFA_BACKREF1:
3648 case NFA_BACKREF2:
3649 case NFA_BACKREF3:
3650 case NFA_BACKREF4:
3651 case NFA_BACKREF5:
3652 case NFA_BACKREF6:
3653 case NFA_BACKREF7:
3654 case NFA_BACKREF8:
3655 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003656#ifdef FEAT_SYN_HL
3657 case NFA_ZREF1:
3658 case NFA_ZREF2:
3659 case NFA_ZREF3:
3660 case NFA_ZREF4:
3661 case NFA_ZREF5:
3662 case NFA_ZREF6:
3663 case NFA_ZREF7:
3664 case NFA_ZREF8:
3665 case NFA_ZREF9:
3666#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003667 if (nfa_calc_size == TRUE)
3668 {
3669 nstate += 2;
3670 break;
3671 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003672 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003673 if (s == NULL)
3674 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003675 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003676 if (s1 == NULL)
3677 goto theend;
3678 patch(list1(&s->out), s1);
3679 PUSH(frag(s, list1(&s1->out)));
3680 break;
3681
Bram Moolenaar423532e2013-05-29 21:14:42 +02003682 case NFA_LNUM:
3683 case NFA_LNUM_GT:
3684 case NFA_LNUM_LT:
3685 case NFA_VCOL:
3686 case NFA_VCOL_GT:
3687 case NFA_VCOL_LT:
3688 case NFA_COL:
3689 case NFA_COL_GT:
3690 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003691 case NFA_MARK:
3692 case NFA_MARK_GT:
3693 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003694 {
3695 int n = *++p; /* lnum, col or mark name */
3696
Bram Moolenaar423532e2013-05-29 21:14:42 +02003697 if (nfa_calc_size == TRUE)
3698 {
3699 nstate += 1;
3700 break;
3701 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003702 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003703 if (s == NULL)
3704 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003705 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003706 PUSH(frag(s, list1(&s->out)));
3707 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003708 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003709
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003710 case NFA_ZSTART:
3711 case NFA_ZEND:
3712 default:
3713 /* Operands */
3714 if (nfa_calc_size == TRUE)
3715 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003716 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717 break;
3718 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003719 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003721 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 PUSH(frag(s, list1(&s->out)));
3723 break;
3724
3725 } /* switch(*p) */
3726
3727 } /* for(p = postfix; *p; ++p) */
3728
3729 if (nfa_calc_size == TRUE)
3730 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003731 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003732 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003733 }
3734
3735 e = POP();
3736 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003737 {
3738 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003740 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741
3742 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003743 {
3744 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003745 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003746 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003748 matchstate = &state_ptr[istate++]; /* the match state */
3749 matchstate->c = NFA_MATCH;
3750 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003751 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752
3753 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003754 ret = e.start;
3755
3756theend:
3757 vim_free(stack);
3758 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759
3760#undef POP1
3761#undef PUSH1
3762#undef POP2
3763#undef PUSH2
3764#undef POP
3765#undef PUSH
3766}
3767
Bram Moolenaara2947e22013-06-11 22:44:09 +02003768/*
3769 * After building the NFA program, inspect it to add optimization hints.
3770 */
3771 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003772nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003773{
3774 int i;
3775 int c;
3776
3777 for (i = 0; i < prog->nstate; ++i)
3778 {
3779 c = prog->state[i].c;
3780 if (c == NFA_START_INVISIBLE
3781 || c == NFA_START_INVISIBLE_NEG
3782 || c == NFA_START_INVISIBLE_BEFORE
3783 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3784 {
3785 int directly;
3786
3787 /* Do it directly when what follows is possibly the end of the
3788 * match. */
3789 if (match_follows(prog->state[i].out1->out, 0))
3790 directly = TRUE;
3791 else
3792 {
3793 int ch_invisible = failure_chance(prog->state[i].out, 0);
3794 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3795
3796 /* Postpone when the invisible match is expensive or has a
3797 * lower chance of failing. */
3798 if (c == NFA_START_INVISIBLE_BEFORE
3799 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3800 {
3801 /* "before" matches are very expensive when
3802 * unbounded, always prefer what follows then,
3803 * unless what follows will always match.
3804 * Otherwise strongly prefer what follows. */
3805 if (prog->state[i].val <= 0 && ch_follows > 0)
3806 directly = FALSE;
3807 else
3808 directly = ch_follows * 10 < ch_invisible;
3809 }
3810 else
3811 {
3812 /* normal invisible, first do the one with the
3813 * highest failure chance */
3814 directly = ch_follows < ch_invisible;
3815 }
3816 }
3817 if (directly)
3818 /* switch to the _FIRST state */
3819 ++prog->state[i].c;
3820 }
3821 }
3822}
3823
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003824/****************************************************************
3825 * NFA execution code.
3826 ****************************************************************/
3827
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003828typedef struct
3829{
3830 int in_use; /* number of subexpr with useful info */
3831
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003832 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003833 union
3834 {
3835 struct multipos
3836 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003837 linenr_T start_lnum;
3838 linenr_T end_lnum;
3839 colnr_T start_col;
3840 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003841 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003842 struct linepos
3843 {
3844 char_u *start;
3845 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003846 } line[NSUBEXP];
3847 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003848} regsub_T;
3849
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003850typedef struct
3851{
3852 regsub_T norm; /* \( .. \) matches */
3853#ifdef FEAT_SYN_HL
3854 regsub_T synt; /* \z( .. \) matches */
3855#endif
3856} regsubs_T;
3857
Bram Moolenaara2d95102013-06-04 14:23:05 +02003858/* nfa_pim_T stores a Postponed Invisible Match. */
3859typedef struct nfa_pim_S nfa_pim_T;
3860struct nfa_pim_S
3861{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003862 int result; /* NFA_PIM_*, see below */
3863 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003864 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003865 union
3866 {
3867 lpos_T pos;
3868 char_u *ptr;
3869 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003870};
3871
3872/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003873#define NFA_PIM_UNUSED 0 /* pim not used */
3874#define NFA_PIM_TODO 1 /* pim not done yet */
3875#define NFA_PIM_MATCH 2 /* pim executed, matches */
3876#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003877
3878
Bram Moolenaar963fee22013-05-26 21:47:28 +02003879/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003880typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881{
3882 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003883 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003884 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3885 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003886 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003887} nfa_thread_T;
3888
Bram Moolenaar963fee22013-05-26 21:47:28 +02003889/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003890typedef struct
3891{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003892 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003893 int n; /* nr of states currently in "t" */
3894 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003895 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003896 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003897} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003898
Bram Moolenaar5714b802013-05-28 22:03:20 +02003899#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003900static void log_subsexpr(regsubs_T *subs);
3901static void log_subexpr(regsub_T *sub);
3902static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003903
3904 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003905log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003906{
3907 log_subexpr(&subs->norm);
3908# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003909 if (nfa_has_zsubexpr)
3910 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003911# endif
3912}
3913
Bram Moolenaar5714b802013-05-28 22:03:20 +02003914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003915log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003916{
3917 int j;
3918
3919 for (j = 0; j < sub->in_use; j++)
3920 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003921 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003922 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003923 sub->list.multi[j].start_col,
3924 (int)sub->list.multi[j].start_lnum,
3925 sub->list.multi[j].end_col,
3926 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003927 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003928 {
3929 char *s = (char *)sub->list.line[j].start;
3930 char *e = (char *)sub->list.line[j].end;
3931
Bram Moolenaar87953742013-06-05 18:52:40 +02003932 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003933 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003934 s == NULL ? "NULL" : s,
3935 e == NULL ? "NULL" : e);
3936 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003937}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003938
3939 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003940pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003941{
3942 static char buf[30];
3943
3944 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3945 buf[0] = NUL;
3946 else
3947 {
3948 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3949 : (int)(pim->end.ptr - reginput));
3950 }
3951 return buf;
3952}
3953
Bram Moolenaar5714b802013-05-28 22:03:20 +02003954#endif
3955
Bram Moolenaar963fee22013-05-26 21:47:28 +02003956/* Used during execution: whether a match has been found. */
3957static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003958#ifdef FEAT_RELTIME
3959static proftime_T *nfa_time_limit;
3960static int nfa_time_count;
3961#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003962
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003963static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3964static void clear_sub(regsub_T *sub);
3965static void copy_sub(regsub_T *to, regsub_T *from);
3966static void copy_sub_off(regsub_T *to, regsub_T *from);
3967static void copy_ze_off(regsub_T *to, regsub_T *from);
3968static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3969static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3970static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3971static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3972static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3973static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3974static void addstate_here(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003975
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003976/*
3977 * Copy postponed invisible match info from "from" to "to".
3978 */
3979 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003980copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003981{
3982 to->result = from->result;
3983 to->state = from->state;
3984 copy_sub(&to->subs.norm, &from->subs.norm);
3985#ifdef FEAT_SYN_HL
3986 if (nfa_has_zsubexpr)
3987 copy_sub(&to->subs.synt, &from->subs.synt);
3988#endif
3989 to->end = from->end;
3990}
3991
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003992 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003993clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003994{
3995 if (REG_MULTI)
3996 /* Use 0xff to set lnum to -1 */
3997 vim_memset(sub->list.multi, 0xff,
3998 sizeof(struct multipos) * nfa_nsubexpr);
3999 else
4000 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4001 sub->in_use = 0;
4002}
4003
4004/*
4005 * Copy the submatches from "from" to "to".
4006 */
4007 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004008copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004009{
4010 to->in_use = from->in_use;
4011 if (from->in_use > 0)
4012 {
4013 /* Copy the match start and end positions. */
4014 if (REG_MULTI)
4015 mch_memmove(&to->list.multi[0],
4016 &from->list.multi[0],
4017 sizeof(struct multipos) * from->in_use);
4018 else
4019 mch_memmove(&to->list.line[0],
4020 &from->list.line[0],
4021 sizeof(struct linepos) * from->in_use);
4022 }
4023}
4024
4025/*
4026 * Like copy_sub() but exclude the main match.
4027 */
4028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004029copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004030{
4031 if (to->in_use < from->in_use)
4032 to->in_use = from->in_use;
4033 if (from->in_use > 1)
4034 {
4035 /* Copy the match start and end positions. */
4036 if (REG_MULTI)
4037 mch_memmove(&to->list.multi[1],
4038 &from->list.multi[1],
4039 sizeof(struct multipos) * (from->in_use - 1));
4040 else
4041 mch_memmove(&to->list.line[1],
4042 &from->list.line[1],
4043 sizeof(struct linepos) * (from->in_use - 1));
4044 }
4045}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046
Bram Moolenaar428e9872013-05-30 17:05:39 +02004047/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004048 * Like copy_sub() but only do the end of the main match if \ze is present.
4049 */
4050 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004051copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004052{
4053 if (nfa_has_zend)
4054 {
4055 if (REG_MULTI)
4056 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004057 if (from->list.multi[0].end_lnum >= 0)
4058 {
4059 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4060 to->list.multi[0].end_col = from->list.multi[0].end_col;
4061 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004062 }
4063 else
4064 {
4065 if (from->list.line[0].end != NULL)
4066 to->list.line[0].end = from->list.line[0].end;
4067 }
4068 }
4069}
4070
4071/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004072 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004073 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004074 */
4075 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004076sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004077{
4078 int i;
4079 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004080 linenr_T s1;
4081 linenr_T s2;
4082 char_u *sp1;
4083 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004084
4085 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4086 if (REG_MULTI)
4087 {
4088 for (i = 0; i < todo; ++i)
4089 {
4090 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004091 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004092 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004093 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004094 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004095 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004096 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004097 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004098 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004099 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004100 if (s1 != -1 && sub1->list.multi[i].start_col
4101 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004102 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004103
4104 if (nfa_has_backref)
4105 {
4106 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004107 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004108 else
4109 s1 = -1;
4110 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004111 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004112 else
4113 s2 = -1;
4114 if (s1 != s2)
4115 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004116 if (s1 != -1 && sub1->list.multi[i].end_col
4117 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004118 return FALSE;
4119 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004120 }
4121 }
4122 else
4123 {
4124 for (i = 0; i < todo; ++i)
4125 {
4126 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004127 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004128 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004129 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004130 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004131 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004132 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004133 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004134 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004135 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004136 if (nfa_has_backref)
4137 {
4138 if (i < sub1->in_use)
4139 sp1 = sub1->list.line[i].end;
4140 else
4141 sp1 = NULL;
4142 if (i < sub2->in_use)
4143 sp2 = sub2->list.line[i].end;
4144 else
4145 sp2 = NULL;
4146 if (sp1 != sp2)
4147 return FALSE;
4148 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004149 }
4150 }
4151
4152 return TRUE;
4153}
4154
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004155#ifdef ENABLE_LOG
4156 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004157report_state(char *action,
4158 regsub_T *sub,
4159 nfa_state_T *state,
4160 int lid,
4161 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004162{
4163 int col;
4164
4165 if (sub->in_use <= 0)
4166 col = -1;
4167 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004168 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004169 else
4170 col = (int)(sub->list.line[0].start - regline);
4171 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004172 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4173 action, abs(state->id), lid, state->c, code, col,
4174 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004175}
4176#endif
4177
Bram Moolenaar43e02982013-06-07 17:31:29 +02004178/*
4179 * Return TRUE if the same state is already in list "l" with the same
4180 * positions as "subs".
4181 */
4182 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004183has_state_with_pos(
4184 nfa_list_T *l, /* runtime state list */
4185 nfa_state_T *state, /* state to update */
4186 regsubs_T *subs, /* pointers to subexpressions */
4187 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004188{
4189 nfa_thread_T *thread;
4190 int i;
4191
4192 for (i = 0; i < l->n; ++i)
4193 {
4194 thread = &l->t[i];
4195 if (thread->state->id == state->id
4196 && sub_equal(&thread->subs.norm, &subs->norm)
4197#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004198 && (!nfa_has_zsubexpr
4199 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004200#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004201 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004202 return TRUE;
4203 }
4204 return FALSE;
4205}
4206
4207/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004208 * Return TRUE if "one" and "two" are equal. That includes when both are not
4209 * set.
4210 */
4211 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004212pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004213{
4214 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4215 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4216
4217 if (one_unused)
4218 /* one is unused: equal when two is also unused */
4219 return two_unused;
4220 if (two_unused)
4221 /* one is used and two is not: not equal */
4222 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004223 /* compare the state id */
4224 if (one->state->id != two->state->id)
4225 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004226 /* compare the position */
4227 if (REG_MULTI)
4228 return one->end.pos.lnum == two->end.pos.lnum
4229 && one->end.pos.col == two->end.pos.col;
4230 return one->end.ptr == two->end.ptr;
4231}
4232
4233/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004234 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4235 */
4236 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004237match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004238{
4239 nfa_state_T *state = startstate;
4240
4241 /* avoid too much recursion */
4242 if (depth > 10)
4243 return FALSE;
4244
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004245 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004246 {
4247 switch (state->c)
4248 {
4249 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004250 case NFA_MCLOSE:
4251 case NFA_END_INVISIBLE:
4252 case NFA_END_INVISIBLE_NEG:
4253 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004254 return TRUE;
4255
4256 case NFA_SPLIT:
4257 return match_follows(state->out, depth + 1)
4258 || match_follows(state->out1, depth + 1);
4259
4260 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004261 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004262 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004263 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004264 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004265 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004266 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004267 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004268 case NFA_COMPOSING:
4269 /* skip ahead to next state */
4270 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004271 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004272
4273 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004274 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004275 case NFA_IDENT:
4276 case NFA_SIDENT:
4277 case NFA_KWORD:
4278 case NFA_SKWORD:
4279 case NFA_FNAME:
4280 case NFA_SFNAME:
4281 case NFA_PRINT:
4282 case NFA_SPRINT:
4283 case NFA_WHITE:
4284 case NFA_NWHITE:
4285 case NFA_DIGIT:
4286 case NFA_NDIGIT:
4287 case NFA_HEX:
4288 case NFA_NHEX:
4289 case NFA_OCTAL:
4290 case NFA_NOCTAL:
4291 case NFA_WORD:
4292 case NFA_NWORD:
4293 case NFA_HEAD:
4294 case NFA_NHEAD:
4295 case NFA_ALPHA:
4296 case NFA_NALPHA:
4297 case NFA_LOWER:
4298 case NFA_NLOWER:
4299 case NFA_UPPER:
4300 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004301 case NFA_LOWER_IC:
4302 case NFA_NLOWER_IC:
4303 case NFA_UPPER_IC:
4304 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004305 case NFA_START_COLL:
4306 case NFA_START_NEG_COLL:
4307 case NFA_NEWL:
4308 /* state will advance input */
4309 return FALSE;
4310
4311 default:
4312 if (state->c > 0)
4313 /* state will advance input */
4314 return FALSE;
4315
4316 /* Others: zero-width or possibly zero-width, might still find
4317 * a match at the same position, keep looking. */
4318 break;
4319 }
4320 state = state->out;
4321 }
4322 return FALSE;
4323}
4324
4325
4326/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004327 * Return TRUE if "state" is already in list "l".
4328 */
4329 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004330state_in_list(
4331 nfa_list_T *l, /* runtime state list */
4332 nfa_state_T *state, /* state to update */
4333 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004334{
4335 if (state->lastlist[nfa_ll_index] == l->id)
4336 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004337 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004338 return TRUE;
4339 }
4340 return FALSE;
4341}
4342
Bram Moolenaard05bf562013-06-30 23:24:08 +02004343/*
4344 * Add "state" and possibly what follows to state list ".".
4345 * Returns "subs_arg", possibly copied into temp_subs.
4346 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004347 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004348addstate(
4349 nfa_list_T *l, /* runtime state list */
4350 nfa_state_T *state, /* state to update */
4351 regsubs_T *subs_arg, /* pointers to subexpressions */
4352 nfa_pim_T *pim, /* postponed look-behind match */
4353 int off) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004355 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004356 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004357 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004358 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004359 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004360 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004361 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004362 regsubs_T *subs = subs_arg;
4363 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004364#ifdef ENABLE_LOG
4365 int did_print = FALSE;
4366#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004368 switch (state->c)
4369 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004370 case NFA_NCLOSE:
4371 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004372 case NFA_MCLOSE1:
4373 case NFA_MCLOSE2:
4374 case NFA_MCLOSE3:
4375 case NFA_MCLOSE4:
4376 case NFA_MCLOSE5:
4377 case NFA_MCLOSE6:
4378 case NFA_MCLOSE7:
4379 case NFA_MCLOSE8:
4380 case NFA_MCLOSE9:
4381#ifdef FEAT_SYN_HL
4382 case NFA_ZCLOSE:
4383 case NFA_ZCLOSE1:
4384 case NFA_ZCLOSE2:
4385 case NFA_ZCLOSE3:
4386 case NFA_ZCLOSE4:
4387 case NFA_ZCLOSE5:
4388 case NFA_ZCLOSE6:
4389 case NFA_ZCLOSE7:
4390 case NFA_ZCLOSE8:
4391 case NFA_ZCLOSE9:
4392#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004393 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004394 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004395 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004396 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004397 /* These nodes are not added themselves but their "out" and/or
4398 * "out1" may be added below. */
4399 break;
4400
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004401 case NFA_BOL:
4402 case NFA_BOF:
4403 /* "^" won't match past end-of-line, don't bother trying.
4404 * Except when at the end of the line, or when we are going to the
4405 * next line for a look-behind match. */
4406 if (reginput > regline
4407 && *reginput != NUL
4408 && (nfa_endp == NULL
4409 || !REG_MULTI
4410 || reglnum == nfa_endp->se_u.pos.lnum))
4411 goto skip_add;
4412 /* FALLTHROUGH */
4413
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004414 case NFA_MOPEN1:
4415 case NFA_MOPEN2:
4416 case NFA_MOPEN3:
4417 case NFA_MOPEN4:
4418 case NFA_MOPEN5:
4419 case NFA_MOPEN6:
4420 case NFA_MOPEN7:
4421 case NFA_MOPEN8:
4422 case NFA_MOPEN9:
4423#ifdef FEAT_SYN_HL
4424 case NFA_ZOPEN:
4425 case NFA_ZOPEN1:
4426 case NFA_ZOPEN2:
4427 case NFA_ZOPEN3:
4428 case NFA_ZOPEN4:
4429 case NFA_ZOPEN5:
4430 case NFA_ZOPEN6:
4431 case NFA_ZOPEN7:
4432 case NFA_ZOPEN8:
4433 case NFA_ZOPEN9:
4434#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004435 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004436 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004437 /* These nodes need to be added so that we can bail out when it
4438 * was added to this list before at the same position to avoid an
4439 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004440
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004441 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004442 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004444 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004445 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004446 * when there is a PIM. For NFA_MATCH check the position,
4447 * lower position is preferred. */
4448 if (!nfa_has_backref && pim == NULL && !l->has_pim
4449 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004450 {
4451skip_add:
4452#ifdef ENABLE_LOG
4453 nfa_set_code(state->c);
4454 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4455 abs(state->id), l->id, state->c, code);
4456#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004457 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004458 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004459
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004460 /* Do not add the state again when it exists with the same
4461 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004462 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004463 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004464 }
4465
Bram Moolenaara0169122013-06-26 18:16:58 +02004466 /* When there are backreferences or PIMs the number of states may
4467 * be (a lot) bigger than anticipated. */
4468 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004469 {
4470 int newlen = l->len * 3 / 2 + 50;
4471
Bram Moolenaard05bf562013-06-30 23:24:08 +02004472 if (subs != &temp_subs)
4473 {
4474 /* "subs" may point into the current array, need to make a
4475 * copy before it becomes invalid. */
4476 copy_sub(&temp_subs.norm, &subs->norm);
4477#ifdef FEAT_SYN_HL
4478 if (nfa_has_zsubexpr)
4479 copy_sub(&temp_subs.synt, &subs->synt);
4480#endif
4481 subs = &temp_subs;
4482 }
4483
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004484 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004485 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4486 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004487 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004488
4489 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004490 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004491 thread = &l->t[l->n++];
4492 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004493 if (pim == NULL)
4494 thread->pim.result = NFA_PIM_UNUSED;
4495 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004496 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004497 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004498 l->has_pim = TRUE;
4499 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004500 copy_sub(&thread->subs.norm, &subs->norm);
4501#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004502 if (nfa_has_zsubexpr)
4503 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004504#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004505#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004506 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004507 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004508#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004509 }
4510
4511#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004512 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004513 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004514#endif
4515 switch (state->c)
4516 {
4517 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004518 break;
4519
4520 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004521 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004522 subs = addstate(l, state->out, subs, pim, off);
4523 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 break;
4525
Bram Moolenaar699c1202013-09-25 16:41:54 +02004526 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004527 case NFA_NOPEN:
4528 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004529 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004530 break;
4531
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004532 case NFA_MOPEN:
4533 case NFA_MOPEN1:
4534 case NFA_MOPEN2:
4535 case NFA_MOPEN3:
4536 case NFA_MOPEN4:
4537 case NFA_MOPEN5:
4538 case NFA_MOPEN6:
4539 case NFA_MOPEN7:
4540 case NFA_MOPEN8:
4541 case NFA_MOPEN9:
4542#ifdef FEAT_SYN_HL
4543 case NFA_ZOPEN:
4544 case NFA_ZOPEN1:
4545 case NFA_ZOPEN2:
4546 case NFA_ZOPEN3:
4547 case NFA_ZOPEN4:
4548 case NFA_ZOPEN5:
4549 case NFA_ZOPEN6:
4550 case NFA_ZOPEN7:
4551 case NFA_ZOPEN8:
4552 case NFA_ZOPEN9:
4553#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004555 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004556 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004558 sub = &subs->norm;
4559 }
4560#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004561 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004562 {
4563 subidx = state->c - NFA_ZOPEN;
4564 sub = &subs->synt;
4565 }
4566#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004567 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004568 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004569 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004570 sub = &subs->norm;
4571 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004572
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004573 /* avoid compiler warnings */
4574 save_ptr = NULL;
4575 save_lpos.lnum = 0;
4576 save_lpos.col = 0;
4577
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004578 /* Set the position (with "off" added) in the subexpression. Save
4579 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580 if (REG_MULTI)
4581 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004582 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004583 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004584 save_lpos.lnum = sub->list.multi[subidx].start_lnum;
4585 save_lpos.col = sub->list.multi[subidx].start_col;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004586 save_in_use = -1;
4587 }
4588 else
4589 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004590 save_in_use = sub->in_use;
4591 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004592 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004593 sub->list.multi[i].start_lnum = -1;
4594 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004595 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004596 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004597 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004598 if (off == -1)
4599 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004600 sub->list.multi[subidx].start_lnum = reglnum + 1;
4601 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004602 }
4603 else
4604 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004605 sub->list.multi[subidx].start_lnum = reglnum;
4606 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004607 (colnr_T)(reginput - regline + off);
4608 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004609 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004610 }
4611 else
4612 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004613 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004614 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004615 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004616 save_in_use = -1;
4617 }
4618 else
4619 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004620 save_in_use = sub->in_use;
4621 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004622 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004623 sub->list.line[i].start = NULL;
4624 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004625 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004626 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004627 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004628 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629 }
4630
Bram Moolenaard05bf562013-06-30 23:24:08 +02004631 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004632 /* "subs" may have changed, need to set "sub" again */
4633#ifdef FEAT_SYN_HL
4634 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4635 sub = &subs->synt;
4636 else
4637#endif
4638 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004639
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004640 if (save_in_use == -1)
4641 {
4642 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004643 {
4644 sub->list.multi[subidx].start_lnum = save_lpos.lnum;
4645 sub->list.multi[subidx].start_col = save_lpos.col;
4646 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004647 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004648 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004649 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004650 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004651 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004652 break;
4653
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004654 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004655 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004656 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004657 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004658 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004659 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004660 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004661 break;
4662 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004663 case NFA_MCLOSE1:
4664 case NFA_MCLOSE2:
4665 case NFA_MCLOSE3:
4666 case NFA_MCLOSE4:
4667 case NFA_MCLOSE5:
4668 case NFA_MCLOSE6:
4669 case NFA_MCLOSE7:
4670 case NFA_MCLOSE8:
4671 case NFA_MCLOSE9:
4672#ifdef FEAT_SYN_HL
4673 case NFA_ZCLOSE:
4674 case NFA_ZCLOSE1:
4675 case NFA_ZCLOSE2:
4676 case NFA_ZCLOSE3:
4677 case NFA_ZCLOSE4:
4678 case NFA_ZCLOSE5:
4679 case NFA_ZCLOSE6:
4680 case NFA_ZCLOSE7:
4681 case NFA_ZCLOSE8:
4682 case NFA_ZCLOSE9:
4683#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004684 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004686 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004687 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004688 sub = &subs->norm;
4689 }
4690#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004691 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004692 {
4693 subidx = state->c - NFA_ZCLOSE;
4694 sub = &subs->synt;
4695 }
4696#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004697 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004698 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004699 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004700 sub = &subs->norm;
4701 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004702
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004703 /* We don't fill in gaps here, there must have been an MOPEN that
4704 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004705 save_in_use = sub->in_use;
4706 if (sub->in_use <= subidx)
4707 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 if (REG_MULTI)
4709 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004710 save_lpos.lnum = sub->list.multi[subidx].end_lnum;
4711 save_lpos.col = sub->list.multi[subidx].end_col;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004712 if (off == -1)
4713 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004714 sub->list.multi[subidx].end_lnum = reglnum + 1;
4715 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004716 }
4717 else
4718 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004719 sub->list.multi[subidx].end_lnum = reglnum;
4720 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004721 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004722 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004723 /* avoid compiler warnings */
4724 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004725 }
4726 else
4727 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004728 save_ptr = sub->list.line[subidx].end;
4729 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004730 /* avoid compiler warnings */
4731 save_lpos.lnum = 0;
4732 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733 }
4734
Bram Moolenaard05bf562013-06-30 23:24:08 +02004735 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004736 /* "subs" may have changed, need to set "sub" again */
4737#ifdef FEAT_SYN_HL
4738 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4739 sub = &subs->synt;
4740 else
4741#endif
4742 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004743
4744 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004745 {
4746 sub->list.multi[subidx].end_lnum = save_lpos.lnum;
4747 sub->list.multi[subidx].end_col = save_lpos.col;
4748 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004749 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004750 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004751 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752 break;
4753 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004754 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004755}
4756
4757/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004758 * Like addstate(), but the new state(s) are put at position "*ip".
4759 * Used for zero-width matches, next state to use is the added one.
4760 * This makes sure the order of states to be tried does not change, which
4761 * matters for alternatives.
4762 */
4763 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004764addstate_here(
4765 nfa_list_T *l, /* runtime state list */
4766 nfa_state_T *state, /* state to update */
4767 regsubs_T *subs, /* pointers to subexpressions */
4768 nfa_pim_T *pim, /* postponed look-behind match */
4769 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004770{
4771 int tlen = l->n;
4772 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004773 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004774
4775 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004776 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004777
Bram Moolenaar4b417062013-05-25 20:19:50 +02004778 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004779 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004780 return;
4781
4782 /* re-order to put the new state at the current position */
4783 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004784 if (count == 0)
4785 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004786 if (count == 1)
4787 {
4788 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004789 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004790 }
4791 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004792 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004793 if (l->n + count - 1 >= l->len)
4794 {
4795 /* not enough space to move the new states, reallocate the list
4796 * and move the states to the right position */
4797 nfa_thread_T *newl;
4798
4799 l->len = l->len * 3 / 2 + 50;
4800 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4801 if (newl == NULL)
4802 return;
4803 mch_memmove(&(newl[0]),
4804 &(l->t[0]),
4805 sizeof(nfa_thread_T) * listidx);
4806 mch_memmove(&(newl[listidx]),
4807 &(l->t[l->n - count]),
4808 sizeof(nfa_thread_T) * count);
4809 mch_memmove(&(newl[listidx + count]),
4810 &(l->t[listidx + 1]),
4811 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4812 vim_free(l->t);
4813 l->t = newl;
4814 }
4815 else
4816 {
4817 /* make space for new states, then move them from the
4818 * end to the current position */
4819 mch_memmove(&(l->t[listidx + count]),
4820 &(l->t[listidx + 1]),
4821 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4822 mch_memmove(&(l->t[listidx]),
4823 &(l->t[l->n - 1]),
4824 sizeof(nfa_thread_T) * count);
4825 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004826 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004827 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004828 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004829}
4830
4831/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004832 * Check character class "class" against current character c.
4833 */
4834 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004835check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004836{
4837 switch (class)
4838 {
4839 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004840 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004841 return OK;
4842 break;
4843 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004844 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004845 return OK;
4846 break;
4847 case NFA_CLASS_BLANK:
4848 if (c == ' ' || c == '\t')
4849 return OK;
4850 break;
4851 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004852 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004853 return OK;
4854 break;
4855 case NFA_CLASS_DIGIT:
4856 if (VIM_ISDIGIT(c))
4857 return OK;
4858 break;
4859 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004860 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004861 return OK;
4862 break;
4863 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004864 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004865 return OK;
4866 break;
4867 case NFA_CLASS_PRINT:
4868 if (vim_isprintc(c))
4869 return OK;
4870 break;
4871 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004872 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004873 return OK;
4874 break;
4875 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004876 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004877 return OK;
4878 break;
4879 case NFA_CLASS_UPPER:
4880 if (MB_ISUPPER(c))
4881 return OK;
4882 break;
4883 case NFA_CLASS_XDIGIT:
4884 if (vim_isxdigit(c))
4885 return OK;
4886 break;
4887 case NFA_CLASS_TAB:
4888 if (c == '\t')
4889 return OK;
4890 break;
4891 case NFA_CLASS_RETURN:
4892 if (c == '\r')
4893 return OK;
4894 break;
4895 case NFA_CLASS_BACKSPACE:
4896 if (c == '\b')
4897 return OK;
4898 break;
4899 case NFA_CLASS_ESCAPE:
4900 if (c == '\033')
4901 return OK;
4902 break;
4903
4904 default:
4905 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004906 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004907 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004908 }
4909 return FAIL;
4910}
4911
Bram Moolenaar5714b802013-05-28 22:03:20 +02004912/*
4913 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004914 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004915 */
4916 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004917match_backref(
4918 regsub_T *sub, /* pointers to subexpressions */
4919 int subidx,
4920 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004921{
4922 int len;
4923
4924 if (sub->in_use <= subidx)
4925 {
4926retempty:
4927 /* backref was not set, match an empty string */
4928 *bytelen = 0;
4929 return TRUE;
4930 }
4931
4932 if (REG_MULTI)
4933 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004934 if (sub->list.multi[subidx].start_lnum < 0
4935 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004936 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004937 if (sub->list.multi[subidx].start_lnum == reglnum
4938 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004939 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004940 len = sub->list.multi[subidx].end_col
4941 - sub->list.multi[subidx].start_col;
4942 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004943 reginput, &len) == 0)
4944 {
4945 *bytelen = len;
4946 return TRUE;
4947 }
4948 }
4949 else
4950 {
4951 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004952 sub->list.multi[subidx].start_lnum,
4953 sub->list.multi[subidx].start_col,
4954 sub->list.multi[subidx].end_lnum,
4955 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004956 bytelen) == RA_MATCH)
4957 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004958 }
4959 }
4960 else
4961 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004962 if (sub->list.line[subidx].start == NULL
4963 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004964 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004965 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4966 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004967 {
4968 *bytelen = len;
4969 return TRUE;
4970 }
4971 }
4972 return FALSE;
4973}
4974
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004975#ifdef FEAT_SYN_HL
4976
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004977static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004978
4979/*
4980 * Check for a match with \z subexpression "subidx".
4981 * Return TRUE if it matches.
4982 */
4983 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004984match_zref(
4985 int subidx,
4986 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004987{
4988 int len;
4989
4990 cleanup_zsubexpr();
4991 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4992 {
4993 /* backref was not set, match an empty string */
4994 *bytelen = 0;
4995 return TRUE;
4996 }
4997
4998 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4999 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5000 {
5001 *bytelen = len;
5002 return TRUE;
5003 }
5004 return FALSE;
5005}
5006#endif
5007
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005008/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005009 * Save list IDs for all NFA states of "prog" into "list".
5010 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005011 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005012 */
5013 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005014nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005015{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005016 int i;
5017 nfa_state_T *p;
5018
5019 /* Order in the list is reverse, it's a bit faster that way. */
5020 p = &prog->state[0];
5021 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005022 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005023 list[i] = p->lastlist[1];
5024 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005025 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005026 }
5027}
5028
5029/*
5030 * Restore list IDs from "list" to all NFA states.
5031 */
5032 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005033nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005034{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005035 int i;
5036 nfa_state_T *p;
5037
5038 p = &prog->state[0];
5039 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005040 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005041 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005042 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005043 }
5044}
5045
Bram Moolenaar423532e2013-05-29 21:14:42 +02005046 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005047nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005048{
5049 if (op == 1) return pos > val;
5050 if (op == 2) return pos < val;
5051 return val == pos;
5052}
5053
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005054static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5055static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005056
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005057/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005058 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005059 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5060 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005061 */
5062 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005063recursive_regmatch(
5064 nfa_state_T *state,
5065 nfa_pim_T *pim,
5066 nfa_regprog_T *prog,
5067 regsubs_T *submatch,
5068 regsubs_T *m,
5069 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005070{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005071 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005072 int save_reglnum = reglnum;
5073 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005074 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005075 save_se_T *save_nfa_endp = nfa_endp;
5076 save_se_T endpos;
5077 save_se_T *endposp = NULL;
5078 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005079 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005080
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005081 if (pim != NULL)
5082 {
5083 /* start at the position where the postponed match was */
5084 if (REG_MULTI)
5085 reginput = regline + pim->end.pos.col;
5086 else
5087 reginput = pim->end.ptr;
5088 }
5089
Bram Moolenaardecd9542013-06-07 16:31:50 +02005090 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005091 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5092 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5093 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005094 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005095 /* The recursive match must end at the current position. When "pim" is
5096 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097 endposp = &endpos;
5098 if (REG_MULTI)
5099 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005100 if (pim == NULL)
5101 {
5102 endpos.se_u.pos.col = (int)(reginput - regline);
5103 endpos.se_u.pos.lnum = reglnum;
5104 }
5105 else
5106 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005107 }
5108 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005109 {
5110 if (pim == NULL)
5111 endpos.se_u.ptr = reginput;
5112 else
5113 endpos.se_u.ptr = pim->end.ptr;
5114 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005115
5116 /* Go back the specified number of bytes, or as far as the
5117 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005118 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005119 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005120 if (state->val <= 0)
5121 {
5122 if (REG_MULTI)
5123 {
5124 regline = reg_getline(--reglnum);
5125 if (regline == NULL)
5126 /* can't go before the first line */
5127 regline = reg_getline(++reglnum);
5128 }
5129 reginput = regline;
5130 }
5131 else
5132 {
5133 if (REG_MULTI && (int)(reginput - regline) < state->val)
5134 {
5135 /* Not enough bytes in this line, go to end of
5136 * previous line. */
5137 regline = reg_getline(--reglnum);
5138 if (regline == NULL)
5139 {
5140 /* can't go before the first line */
5141 regline = reg_getline(++reglnum);
5142 reginput = regline;
5143 }
5144 else
5145 reginput = regline + STRLEN(regline);
5146 }
5147 if ((int)(reginput - regline) >= state->val)
5148 {
5149 reginput -= state->val;
5150#ifdef FEAT_MBYTE
5151 if (has_mbyte)
5152 reginput -= mb_head_off(regline, reginput);
5153#endif
5154 }
5155 else
5156 reginput = regline;
5157 }
5158 }
5159
Bram Moolenaarf46da702013-06-02 22:37:42 +02005160#ifdef ENABLE_LOG
5161 if (log_fd != stderr)
5162 fclose(log_fd);
5163 log_fd = NULL;
5164#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005165 /* Have to clear the lastlist field of the NFA nodes, so that
5166 * nfa_regmatch() and addstate() can run properly after recursion. */
5167 if (nfa_ll_index == 1)
5168 {
5169 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5170 * values and clear them. */
5171 if (*listids == NULL)
5172 {
5173 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5174 if (*listids == NULL)
5175 {
5176 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5177 return 0;
5178 }
5179 }
5180 nfa_save_listids(prog, *listids);
5181 need_restore = TRUE;
5182 /* any value of nfa_listid will do */
5183 }
5184 else
5185 {
5186 /* First recursive nfa_regmatch() call, switch to the second lastlist
5187 * entry. Make sure nfa_listid is different from a previous recursive
5188 * call, because some states may still have this ID. */
5189 ++nfa_ll_index;
5190 if (nfa_listid <= nfa_alt_listid)
5191 nfa_listid = nfa_alt_listid;
5192 }
5193
5194 /* Call nfa_regmatch() to check if the current concat matches at this
5195 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005196 nfa_endp = endposp;
5197 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005198
5199 if (need_restore)
5200 nfa_restore_listids(prog, *listids);
5201 else
5202 {
5203 --nfa_ll_index;
5204 nfa_alt_listid = nfa_listid;
5205 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005206
5207 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005208 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005209 if (REG_MULTI)
5210 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005211 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005212 if (result != NFA_TOO_EXPENSIVE)
5213 {
5214 nfa_match = save_nfa_match;
5215 nfa_listid = save_nfa_listid;
5216 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005217 nfa_endp = save_nfa_endp;
5218
5219#ifdef ENABLE_LOG
5220 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5221 if (log_fd != NULL)
5222 {
5223 fprintf(log_fd, "****************************\n");
5224 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5225 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5226 fprintf(log_fd, "****************************\n");
5227 }
5228 else
5229 {
5230 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5231 log_fd = stderr;
5232 }
5233#endif
5234
5235 return result;
5236}
5237
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005238static int skip_to_start(int c, colnr_T *colp);
5239static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005240
5241/*
5242 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005243 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005244 * NFA_ANY: 1
5245 * specific character: 99
5246 */
5247 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005248failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005249{
5250 int c = state->c;
5251 int l, r;
5252
5253 /* detect looping */
5254 if (depth > 4)
5255 return 1;
5256
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005257 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005258 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005259 case NFA_SPLIT:
5260 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5261 /* avoid recursive stuff */
5262 return 1;
5263 /* two alternatives, use the lowest failure chance */
5264 l = failure_chance(state->out, depth + 1);
5265 r = failure_chance(state->out1, depth + 1);
5266 return l < r ? l : r;
5267
5268 case NFA_ANY:
5269 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005270 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005271
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005272 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005273 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005274 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005275 /* empty match works always */
5276 return 0;
5277
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005278 case NFA_START_INVISIBLE:
5279 case NFA_START_INVISIBLE_FIRST:
5280 case NFA_START_INVISIBLE_NEG:
5281 case NFA_START_INVISIBLE_NEG_FIRST:
5282 case NFA_START_INVISIBLE_BEFORE:
5283 case NFA_START_INVISIBLE_BEFORE_FIRST:
5284 case NFA_START_INVISIBLE_BEFORE_NEG:
5285 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5286 case NFA_START_PATTERN:
5287 /* recursive regmatch is expensive, use low failure chance */
5288 return 5;
5289
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005290 case NFA_BOL:
5291 case NFA_EOL:
5292 case NFA_BOF:
5293 case NFA_EOF:
5294 case NFA_NEWL:
5295 return 99;
5296
5297 case NFA_BOW:
5298 case NFA_EOW:
5299 return 90;
5300
5301 case NFA_MOPEN:
5302 case NFA_MOPEN1:
5303 case NFA_MOPEN2:
5304 case NFA_MOPEN3:
5305 case NFA_MOPEN4:
5306 case NFA_MOPEN5:
5307 case NFA_MOPEN6:
5308 case NFA_MOPEN7:
5309 case NFA_MOPEN8:
5310 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005311#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005312 case NFA_ZOPEN:
5313 case NFA_ZOPEN1:
5314 case NFA_ZOPEN2:
5315 case NFA_ZOPEN3:
5316 case NFA_ZOPEN4:
5317 case NFA_ZOPEN5:
5318 case NFA_ZOPEN6:
5319 case NFA_ZOPEN7:
5320 case NFA_ZOPEN8:
5321 case NFA_ZOPEN9:
5322 case NFA_ZCLOSE:
5323 case NFA_ZCLOSE1:
5324 case NFA_ZCLOSE2:
5325 case NFA_ZCLOSE3:
5326 case NFA_ZCLOSE4:
5327 case NFA_ZCLOSE5:
5328 case NFA_ZCLOSE6:
5329 case NFA_ZCLOSE7:
5330 case NFA_ZCLOSE8:
5331 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005332#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005333 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005334 case NFA_MCLOSE1:
5335 case NFA_MCLOSE2:
5336 case NFA_MCLOSE3:
5337 case NFA_MCLOSE4:
5338 case NFA_MCLOSE5:
5339 case NFA_MCLOSE6:
5340 case NFA_MCLOSE7:
5341 case NFA_MCLOSE8:
5342 case NFA_MCLOSE9:
5343 case NFA_NCLOSE:
5344 return failure_chance(state->out, depth + 1);
5345
5346 case NFA_BACKREF1:
5347 case NFA_BACKREF2:
5348 case NFA_BACKREF3:
5349 case NFA_BACKREF4:
5350 case NFA_BACKREF5:
5351 case NFA_BACKREF6:
5352 case NFA_BACKREF7:
5353 case NFA_BACKREF8:
5354 case NFA_BACKREF9:
5355#ifdef FEAT_SYN_HL
5356 case NFA_ZREF1:
5357 case NFA_ZREF2:
5358 case NFA_ZREF3:
5359 case NFA_ZREF4:
5360 case NFA_ZREF5:
5361 case NFA_ZREF6:
5362 case NFA_ZREF7:
5363 case NFA_ZREF8:
5364 case NFA_ZREF9:
5365#endif
5366 /* backreferences don't match in many places */
5367 return 94;
5368
5369 case NFA_LNUM_GT:
5370 case NFA_LNUM_LT:
5371 case NFA_COL_GT:
5372 case NFA_COL_LT:
5373 case NFA_VCOL_GT:
5374 case NFA_VCOL_LT:
5375 case NFA_MARK_GT:
5376 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005377 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005378 /* before/after positions don't match very often */
5379 return 85;
5380
5381 case NFA_LNUM:
5382 return 90;
5383
5384 case NFA_CURSOR:
5385 case NFA_COL:
5386 case NFA_VCOL:
5387 case NFA_MARK:
5388 /* specific positions rarely match */
5389 return 98;
5390
5391 case NFA_COMPOSING:
5392 return 95;
5393
5394 default:
5395 if (c > 0)
5396 /* character match fails often */
5397 return 95;
5398 }
5399
5400 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005401 return 50;
5402}
5403
Bram Moolenaarf46da702013-06-02 22:37:42 +02005404/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005405 * Skip until the char "c" we know a match must start with.
5406 */
5407 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005408skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005409{
5410 char_u *s;
5411
5412 /* Used often, do some work to avoid call overhead. */
5413 if (!ireg_ic
5414#ifdef FEAT_MBYTE
5415 && !has_mbyte
5416#endif
5417 )
5418 s = vim_strbyte(regline + *colp, c);
5419 else
5420 s = cstrchr(regline + *colp, c);
5421 if (s == NULL)
5422 return FAIL;
5423 *colp = (int)(s - regline);
5424 return OK;
5425}
5426
5427/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005428 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005429 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005430 * Returns zero for no match, 1 for a match.
5431 */
5432 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005433find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005434{
5435 colnr_T col = startcol;
5436 int c1, c2;
5437 int len1, len2;
5438 int match;
5439
5440 for (;;)
5441 {
5442 match = TRUE;
5443 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5444 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5445 {
5446 c1 = PTR2CHAR(match_text + len1);
5447 c2 = PTR2CHAR(regline + col + len2);
5448 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5449 {
5450 match = FALSE;
5451 break;
5452 }
5453 len2 += MB_CHAR2LEN(c2);
5454 }
5455 if (match
5456#ifdef FEAT_MBYTE
5457 /* check that no composing char follows */
5458 && !(enc_utf8
5459 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5460#endif
5461 )
5462 {
5463 cleanup_subexpr();
5464 if (REG_MULTI)
5465 {
5466 reg_startpos[0].lnum = reglnum;
5467 reg_startpos[0].col = col;
5468 reg_endpos[0].lnum = reglnum;
5469 reg_endpos[0].col = col + len2;
5470 }
5471 else
5472 {
5473 reg_startp[0] = regline + col;
5474 reg_endp[0] = regline + col + len2;
5475 }
5476 return 1L;
5477 }
5478
5479 /* Try finding regstart after the current match. */
5480 col += MB_CHAR2LEN(regstart); /* skip regstart */
5481 if (skip_to_start(regstart, &col) == FAIL)
5482 break;
5483 }
5484 return 0L;
5485}
5486
5487/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005488 * Main matching routine.
5489 *
5490 * Run NFA to determine whether it matches reginput.
5491 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005492 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005493 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005494 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005495 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005496 * Note: Caller must ensure that: start != NULL.
5497 */
5498 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005499nfa_regmatch(
5500 nfa_regprog_T *prog,
5501 nfa_state_T *start,
5502 regsubs_T *submatch,
5503 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005504{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005505 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005506 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005507 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005508 int go_to_nextline = FALSE;
5509 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005510 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005511 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005512 nfa_list_T *thislist;
5513 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005514 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005515 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005516 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005517 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005518 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005519 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005520#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005521 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005522#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005523
Bram Moolenaar41f12052013-08-25 17:01:42 +02005524 /* Some patterns may take a long time to match, especially when using
5525 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5526 fast_breakcheck();
5527 if (got_int)
5528 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005529#ifdef FEAT_RELTIME
5530 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5531 return FALSE;
5532#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005533
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005534#ifdef NFA_REGEXP_DEBUG_LOG
5535 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5536 if (debug == NULL)
5537 {
5538 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
5539 return FALSE;
5540 }
5541#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005542 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005543
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005544 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005545 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005546
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005547 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005548 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005549 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005550 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005551 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553
5554#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005555 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005556 if (log_fd != NULL)
5557 {
5558 fprintf(log_fd, "**********************************\n");
5559 nfa_set_code(start->c);
5560 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5561 abs(start->id), code);
5562 fprintf(log_fd, "**********************************\n");
5563 }
5564 else
5565 {
5566 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5567 log_fd = stderr;
5568 }
5569#endif
5570
5571 thislist = &list[0];
5572 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005573 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005574 nextlist = &list[1];
5575 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005576 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005577#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005578 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005579#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005580 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005581
5582 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5583 * it's the first MOPEN. */
5584 if (toplevel)
5585 {
5586 if (REG_MULTI)
5587 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005588 m->norm.list.multi[0].start_lnum = reglnum;
5589 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005590 }
5591 else
5592 m->norm.list.line[0].start = reginput;
5593 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005594 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005595 }
5596 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005597 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005599#define ADD_STATE_IF_MATCH(state) \
5600 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005601 add_state = state->out; \
5602 add_off = clen; \
5603 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005604
5605 /*
5606 * Run for each character.
5607 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005608 for (;;)
5609 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005610 int curc;
5611 int clen;
5612
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005613#ifdef FEAT_MBYTE
5614 if (has_mbyte)
5615 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005616 curc = (*mb_ptr2char)(reginput);
5617 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005618 }
5619 else
5620#endif
5621 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005622 curc = *reginput;
5623 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005625 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005626 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005627 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005628 go_to_nextline = FALSE;
5629 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005630
5631 /* swap lists */
5632 thislist = &list[flag];
5633 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005634 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005635 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005636 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005637 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5638 {
5639 /* too many states, retry with old engine */
5640 nfa_match = NFA_TOO_EXPENSIVE;
5641 goto theend;
5642 }
5643
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005644 thislist->id = nfa_listid;
5645 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005646
5647#ifdef ENABLE_LOG
5648 fprintf(log_fd, "------------------------------------------\n");
5649 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005650 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005651 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005652 {
5653 int i;
5654
5655 for (i = 0; i < thislist->n; i++)
5656 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5657 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005658 fprintf(log_fd, "\n");
5659#endif
5660
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005661#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005662 fprintf(debug, "\n-------------------\n");
5663#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005664 /*
5665 * If the state lists are empty we can stop.
5666 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005667 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005668 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005669
5670 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005671 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005672 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005673 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005674
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005675#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005676 nfa_set_code(t->state->c);
5677 fprintf(debug, "%s, ", code);
5678#endif
5679#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005680 {
5681 int col;
5682
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005683 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005684 col = -1;
5685 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005686 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005687 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005688 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005689 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005690 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5691 abs(t->state->id), (int)t->state->c, code, col,
5692 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005693 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005694#endif
5695
5696 /*
5697 * Handle the possible codes of the current state.
5698 * The most important is NFA_MATCH.
5699 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005700 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005701 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005702 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005703 switch (t->state->c)
5704 {
5705 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005706 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005707#ifdef FEAT_MBYTE
5708 /* If the match ends before a composing characters and
5709 * ireg_icombine is not set, that is not really a match. */
5710 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5711 break;
5712#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005713 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005714 copy_sub(&submatch->norm, &t->subs.norm);
5715#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005716 if (nfa_has_zsubexpr)
5717 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005718#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005719#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005720 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005721#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005722 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005723 * states at this position. When the list of states is going
5724 * to be empty quit without advancing, so that "reginput" is
5725 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005726 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005727 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005728 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005729 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005730
5731 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005732 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005733 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005734 /*
5735 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005736 * NFA_START_INVISIBLE_BEFORE node.
5737 * They surround a zero-width group, used with "\@=", "\&",
5738 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005739 * If we got here, it means that the current "invisible" group
5740 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005741 * nfa_regmatch(). For a look-behind match only when it ends
5742 * in the position in "nfa_endp".
5743 * Submatches are stored in *m, and used in the parent call.
5744 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005745#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005746 if (nfa_endp != NULL)
5747 {
5748 if (REG_MULTI)
5749 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5750 (int)reglnum,
5751 (int)nfa_endp->se_u.pos.lnum,
5752 (int)(reginput - regline),
5753 nfa_endp->se_u.pos.col);
5754 else
5755 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5756 (int)(reginput - regline),
5757 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005758 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005759#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005760 /* If "nfa_endp" is set it's only a match if it ends at
5761 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005762 if (nfa_endp != NULL && (REG_MULTI
5763 ? (reglnum != nfa_endp->se_u.pos.lnum
5764 || (int)(reginput - regline)
5765 != nfa_endp->se_u.pos.col)
5766 : reginput != nfa_endp->se_u.ptr))
5767 break;
5768
5769 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005770 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005771 {
5772 copy_sub(&m->norm, &t->subs.norm);
5773#ifdef FEAT_SYN_HL
5774 if (nfa_has_zsubexpr)
5775 copy_sub(&m->synt, &t->subs.synt);
5776#endif
5777 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005778#ifdef ENABLE_LOG
5779 fprintf(log_fd, "Match found:\n");
5780 log_subsexpr(m);
5781#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005782 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005783 /* See comment above at "goto nextchar". */
5784 if (nextlist->n == 0)
5785 clen = 0;
5786 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005787
5788 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005789 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005790 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005791 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005792 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005793 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005794 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005795 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005796 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005797#ifdef ENABLE_LOG
5798 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5799 failure_chance(t->state->out, 0),
5800 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005801#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005802 /* Do it directly if there already is a PIM or when
5803 * nfa_postprocess() detected it will work better. */
5804 if (t->pim.result != NFA_PIM_UNUSED
5805 || t->state->c == NFA_START_INVISIBLE_FIRST
5806 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5807 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5808 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005809 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005810 int in_use = m->norm.in_use;
5811
Bram Moolenaar699c1202013-09-25 16:41:54 +02005812 /* Copy submatch info for the recursive call, opposite
5813 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005814 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005815#ifdef FEAT_SYN_HL
5816 if (nfa_has_zsubexpr)
5817 copy_sub_off(&m->synt, &t->subs.synt);
5818#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005819
Bram Moolenaara2d95102013-06-04 14:23:05 +02005820 /*
5821 * First try matching the invisible match, then what
5822 * follows.
5823 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005824 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005825 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005826 if (result == NFA_TOO_EXPENSIVE)
5827 {
5828 nfa_match = result;
5829 goto theend;
5830 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005831
Bram Moolenaardecd9542013-06-07 16:31:50 +02005832 /* for \@! and \@<! it is a match when the result is
5833 * FALSE */
5834 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005835 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5836 || t->state->c
5837 == NFA_START_INVISIBLE_BEFORE_NEG
5838 || t->state->c
5839 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005840 {
5841 /* Copy submatch info from the recursive call */
5842 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005843#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005844 if (nfa_has_zsubexpr)
5845 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005846#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005847 /* If the pattern has \ze and it matched in the
5848 * sub pattern, use it. */
5849 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005850
Bram Moolenaara2d95102013-06-04 14:23:05 +02005851 /* t->state->out1 is the corresponding
5852 * END_INVISIBLE node; Add its out to the current
5853 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005854 add_here = TRUE;
5855 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005856 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005857 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005858 }
5859 else
5860 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005861 nfa_pim_T pim;
5862
Bram Moolenaara2d95102013-06-04 14:23:05 +02005863 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005864 * First try matching what follows. Only if a match
5865 * is found verify the invisible match matches. Add a
5866 * nfa_pim_T to the following states, it contains info
5867 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005868 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005869 pim.state = t->state;
5870 pim.result = NFA_PIM_TODO;
5871 pim.subs.norm.in_use = 0;
5872#ifdef FEAT_SYN_HL
5873 pim.subs.synt.in_use = 0;
5874#endif
5875 if (REG_MULTI)
5876 {
5877 pim.end.pos.col = (int)(reginput - regline);
5878 pim.end.pos.lnum = reglnum;
5879 }
5880 else
5881 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005882
5883 /* t->state->out1 is the corresponding END_INVISIBLE
5884 * node; Add its out to the current list (zero-width
5885 * match). */
5886 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005887 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005888 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005889 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005890 break;
5891
Bram Moolenaar87953742013-06-05 18:52:40 +02005892 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005893 {
5894 nfa_state_T *skip = NULL;
5895#ifdef ENABLE_LOG
5896 int skip_lid = 0;
5897#endif
5898
5899 /* There is no point in trying to match the pattern if the
5900 * output state is not going to be added to the list. */
5901 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5902 {
5903 skip = t->state->out1->out;
5904#ifdef ENABLE_LOG
5905 skip_lid = nextlist->id;
5906#endif
5907 }
5908 else if (state_in_list(nextlist,
5909 t->state->out1->out->out, &t->subs))
5910 {
5911 skip = t->state->out1->out->out;
5912#ifdef ENABLE_LOG
5913 skip_lid = nextlist->id;
5914#endif
5915 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005916 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005917 t->state->out1->out->out, &t->subs))
5918 {
5919 skip = t->state->out1->out->out;
5920#ifdef ENABLE_LOG
5921 skip_lid = thislist->id;
5922#endif
5923 }
5924 if (skip != NULL)
5925 {
5926#ifdef ENABLE_LOG
5927 nfa_set_code(skip->c);
5928 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5929 abs(skip->id), skip_lid, skip->c, code);
5930#endif
5931 break;
5932 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005933 /* Copy submatch info to the recursive call, opposite of what
5934 * happens afterwards. */
5935 copy_sub_off(&m->norm, &t->subs.norm);
5936#ifdef FEAT_SYN_HL
5937 if (nfa_has_zsubexpr)
5938 copy_sub_off(&m->synt, &t->subs.synt);
5939#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005940
Bram Moolenaar87953742013-06-05 18:52:40 +02005941 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005942 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005943 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005944 if (result == NFA_TOO_EXPENSIVE)
5945 {
5946 nfa_match = result;
5947 goto theend;
5948 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005949 if (result)
5950 {
5951 int bytelen;
5952
5953#ifdef ENABLE_LOG
5954 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5955 log_subsexpr(m);
5956#endif
5957 /* Copy submatch info from the recursive call */
5958 copy_sub_off(&t->subs.norm, &m->norm);
5959#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005960 if (nfa_has_zsubexpr)
5961 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005962#endif
5963 /* Now we need to skip over the matched text and then
5964 * continue with what follows. */
5965 if (REG_MULTI)
5966 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005967 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005968 - (int)(reginput - regline);
5969 else
5970 bytelen = (int)(m->norm.list.line[0].end - reginput);
5971
5972#ifdef ENABLE_LOG
5973 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5974#endif
5975 if (bytelen == 0)
5976 {
5977 /* empty match, output of corresponding
5978 * NFA_END_PATTERN/NFA_SKIP to be used at current
5979 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005980 add_here = TRUE;
5981 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005982 }
5983 else if (bytelen <= clen)
5984 {
5985 /* match current character, output of corresponding
5986 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005987 add_state = t->state->out1->out->out;
5988 add_off = clen;
5989 }
5990 else
5991 {
5992 /* skip over the matched characters, set character
5993 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005994 add_state = t->state->out1->out;
5995 add_off = bytelen;
5996 add_count = bytelen - clen;
5997 }
5998 }
5999 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006000 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006001
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006002 case NFA_BOL:
6003 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006004 {
6005 add_here = TRUE;
6006 add_state = t->state->out;
6007 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006008 break;
6009
6010 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006011 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006012 {
6013 add_here = TRUE;
6014 add_state = t->state->out;
6015 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006016 break;
6017
6018 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006019 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006020
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006021 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006022 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006023#ifdef FEAT_MBYTE
6024 else if (has_mbyte)
6025 {
6026 int this_class;
6027
6028 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006029 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006030 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006031 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006032 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006033 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006034 }
6035#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006036 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006037 || (reginput > regline
6038 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006039 result = FALSE;
6040 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006041 {
6042 add_here = TRUE;
6043 add_state = t->state->out;
6044 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006045 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006046
6047 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006048 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006049 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006050 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006051#ifdef FEAT_MBYTE
6052 else if (has_mbyte)
6053 {
6054 int this_class, prev_class;
6055
6056 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006057 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006058 prev_class = reg_prev_class();
6059 if (this_class == prev_class
6060 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006061 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006062 }
6063#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006064 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006065 || (reginput[0] != NUL
6066 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006067 result = FALSE;
6068 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006069 {
6070 add_here = TRUE;
6071 add_state = t->state->out;
6072 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006073 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006074
Bram Moolenaar4b780632013-05-31 22:14:52 +02006075 case NFA_BOF:
6076 if (reglnum == 0 && reginput == regline
6077 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006078 {
6079 add_here = TRUE;
6080 add_state = t->state->out;
6081 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006082 break;
6083
6084 case NFA_EOF:
6085 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006086 {
6087 add_here = TRUE;
6088 add_state = t->state->out;
6089 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006090 break;
6091
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006092#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006094 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006095 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006096 int len = 0;
6097 nfa_state_T *end;
6098 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006099 int cchars[MAX_MCO];
6100 int ccount = 0;
6101 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006102
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006103 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006104 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006105 if (utf_iscomposing(sta->c))
6106 {
6107 /* Only match composing character(s), ignore base
6108 * character. Used for ".{composing}" and "{composing}"
6109 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006110 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006111 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02006112 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006113 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006114 /* If \Z was present, then ignore composing characters.
6115 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006116 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006117 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006118 else
6119 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006120 while (sta->c != NFA_END_COMPOSING)
6121 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006122 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006123
6124 /* Check base character matches first, unless ignored. */
6125 else if (len > 0 || mc == sta->c)
6126 {
6127 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006128 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006129 len += mb_char2len(mc);
6130 sta = sta->out;
6131 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006132
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006133 /* We don't care about the order of composing characters.
6134 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006135 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006136 {
6137 mc = mb_ptr2char(reginput + len);
6138 cchars[ccount++] = mc;
6139 len += mb_char2len(mc);
6140 if (ccount == MAX_MCO)
6141 break;
6142 }
6143
6144 /* Check that each composing char in the pattern matches a
6145 * composing char in the text. We do not check if all
6146 * composing chars are matched. */
6147 result = OK;
6148 while (sta->c != NFA_END_COMPOSING)
6149 {
6150 for (j = 0; j < ccount; ++j)
6151 if (cchars[j] == sta->c)
6152 break;
6153 if (j == ccount)
6154 {
6155 result = FAIL;
6156 break;
6157 }
6158 sta = sta->out;
6159 }
6160 }
6161 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006162 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006163
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006164 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006165 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006166 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006167 }
6168#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006169
6170 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006171 if (curc == NUL && !reg_line_lbr && REG_MULTI
6172 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006173 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006174 go_to_nextline = TRUE;
6175 /* Pass -1 for the offset, which means taking the position
6176 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006177 add_state = t->state->out;
6178 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006179 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006180 else if (curc == '\n' && reg_line_lbr)
6181 {
6182 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006183 add_state = t->state->out;
6184 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006185 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006186 break;
6187
Bram Moolenaar417bad22013-06-07 14:08:30 +02006188 case NFA_START_COLL:
6189 case NFA_START_NEG_COLL:
6190 {
6191 /* What follows is a list of characters, until NFA_END_COLL.
6192 * One of them must match or none of them must match. */
6193 nfa_state_T *state;
6194 int result_if_matched;
6195 int c1, c2;
6196
6197 /* Never match EOL. If it's part of the collection it is added
6198 * as a separate state with an OR. */
6199 if (curc == NUL)
6200 break;
6201
6202 state = t->state->out;
6203 result_if_matched = (t->state->c == NFA_START_COLL);
6204 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006205 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006206 if (state->c == NFA_END_COLL)
6207 {
6208 result = !result_if_matched;
6209 break;
6210 }
6211 if (state->c == NFA_RANGE_MIN)
6212 {
6213 c1 = state->val;
6214 state = state->out; /* advance to NFA_RANGE_MAX */
6215 c2 = state->val;
6216#ifdef ENABLE_LOG
6217 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6218 curc, c1, c2);
6219#endif
6220 if (curc >= c1 && curc <= c2)
6221 {
6222 result = result_if_matched;
6223 break;
6224 }
6225 if (ireg_ic)
6226 {
6227 int curc_low = MB_TOLOWER(curc);
6228 int done = FALSE;
6229
6230 for ( ; c1 <= c2; ++c1)
6231 if (MB_TOLOWER(c1) == curc_low)
6232 {
6233 result = result_if_matched;
6234 done = TRUE;
6235 break;
6236 }
6237 if (done)
6238 break;
6239 }
6240 }
6241 else if (state->c < 0 ? check_char_class(state->c, curc)
6242 : (curc == state->c
6243 || (ireg_ic && MB_TOLOWER(curc)
6244 == MB_TOLOWER(state->c))))
6245 {
6246 result = result_if_matched;
6247 break;
6248 }
6249 state = state->out;
6250 }
6251 if (result)
6252 {
6253 /* next state is in out of the NFA_END_COLL, out1 of
6254 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006255 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006256 add_off = clen;
6257 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006258 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006259 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006260
6261 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006262 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006263 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006264 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006265 add_state = t->state->out;
6266 add_off = clen;
6267 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006268 break;
6269
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006270 case NFA_ANY_COMPOSING:
6271 /* On a composing character skip over it. Otherwise do
6272 * nothing. Always matches. */
6273#ifdef FEAT_MBYTE
6274 if (enc_utf8 && utf_iscomposing(curc))
6275 {
6276 add_off = clen;
6277 }
6278 else
6279#endif
6280 {
6281 add_here = TRUE;
6282 add_off = 0;
6283 }
6284 add_state = t->state->out;
6285 break;
6286
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006287 /*
6288 * Character classes like \a for alpha, \d for digit etc.
6289 */
6290 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006291 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006292 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006293 break;
6294
6295 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006296 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006297 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006298 break;
6299
6300 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006301 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006302 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006303 break;
6304
6305 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006306 result = !VIM_ISDIGIT(curc)
6307 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006308 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006309 break;
6310
6311 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006312 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006313 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006314 break;
6315
6316 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006317 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006318 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006319 break;
6320
6321 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006322 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006323 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006324 break;
6325
6326 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006327 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006328 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006329 break;
6330
6331 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006332 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006333 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006334 break;
6335
6336 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006337 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006338 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006339 break;
6340
6341 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006342 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006343 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006344 break;
6345
6346 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006347 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006348 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006349 break;
6350
6351 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006352 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006353 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006354 break;
6355
6356 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006357 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006358 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006359 break;
6360
6361 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006362 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006363 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006364 break;
6365
6366 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006367 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006368 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006369 break;
6370
6371 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006372 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006373 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006374 break;
6375
6376 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006377 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006378 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379 break;
6380
6381 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006382 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006383 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384 break;
6385
6386 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006387 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006388 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006389 break;
6390
6391 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006392 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006393 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006394 break;
6395
6396 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006397 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006398 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006399 break;
6400
6401 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006402 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006403 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006404 break;
6405
6406 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006407 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006408 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006409 break;
6410
6411 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006412 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006413 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414 break;
6415
6416 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006417 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006418 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006419 break;
6420
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006421 case NFA_LOWER_IC: /* [a-z] */
6422 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6423 ADD_STATE_IF_MATCH(t->state);
6424 break;
6425
6426 case NFA_NLOWER_IC: /* [^a-z] */
6427 result = curc != NUL
6428 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6429 ADD_STATE_IF_MATCH(t->state);
6430 break;
6431
6432 case NFA_UPPER_IC: /* [A-Z] */
6433 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6434 ADD_STATE_IF_MATCH(t->state);
6435 break;
6436
6437 case NFA_NUPPER_IC: /* ^[A-Z] */
6438 result = curc != NUL
6439 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6440 ADD_STATE_IF_MATCH(t->state);
6441 break;
6442
Bram Moolenaar5714b802013-05-28 22:03:20 +02006443 case NFA_BACKREF1:
6444 case NFA_BACKREF2:
6445 case NFA_BACKREF3:
6446 case NFA_BACKREF4:
6447 case NFA_BACKREF5:
6448 case NFA_BACKREF6:
6449 case NFA_BACKREF7:
6450 case NFA_BACKREF8:
6451 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006452#ifdef FEAT_SYN_HL
6453 case NFA_ZREF1:
6454 case NFA_ZREF2:
6455 case NFA_ZREF3:
6456 case NFA_ZREF4:
6457 case NFA_ZREF5:
6458 case NFA_ZREF6:
6459 case NFA_ZREF7:
6460 case NFA_ZREF8:
6461 case NFA_ZREF9:
6462#endif
6463 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006464 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006465 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006466 int bytelen;
6467
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006468 if (t->state->c <= NFA_BACKREF9)
6469 {
6470 subidx = t->state->c - NFA_BACKREF1 + 1;
6471 result = match_backref(&t->subs.norm, subidx, &bytelen);
6472 }
6473#ifdef FEAT_SYN_HL
6474 else
6475 {
6476 subidx = t->state->c - NFA_ZREF1 + 1;
6477 result = match_zref(subidx, &bytelen);
6478 }
6479#endif
6480
Bram Moolenaar5714b802013-05-28 22:03:20 +02006481 if (result)
6482 {
6483 if (bytelen == 0)
6484 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006485 /* empty match always works, output of NFA_SKIP to be
6486 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006487 add_here = TRUE;
6488 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006489 }
6490 else if (bytelen <= clen)
6491 {
6492 /* match current character, jump ahead to out of
6493 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006494 add_state = t->state->out->out;
6495 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006496 }
6497 else
6498 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006499 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006500 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006501 add_state = t->state->out;
6502 add_off = bytelen;
6503 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006504 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006505 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006506 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006507 }
6508 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006509 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006510 if (t->count - clen <= 0)
6511 {
6512 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006513 add_state = t->state->out;
6514 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006515 }
6516 else
6517 {
6518 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006519 add_state = t->state;
6520 add_off = 0;
6521 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006522 }
6523 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006524
Bram Moolenaar423532e2013-05-29 21:14:42 +02006525 case NFA_LNUM:
6526 case NFA_LNUM_GT:
6527 case NFA_LNUM_LT:
6528 result = (REG_MULTI &&
6529 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6530 (long_u)(reglnum + reg_firstlnum)));
6531 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006532 {
6533 add_here = TRUE;
6534 add_state = t->state->out;
6535 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006536 break;
6537
6538 case NFA_COL:
6539 case NFA_COL_GT:
6540 case NFA_COL_LT:
6541 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6542 (long_u)(reginput - regline) + 1);
6543 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006544 {
6545 add_here = TRUE;
6546 add_state = t->state->out;
6547 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006548 break;
6549
6550 case NFA_VCOL:
6551 case NFA_VCOL_GT:
6552 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006553 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006554 int op = t->state->c - NFA_VCOL;
6555 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaaref795d12015-01-18 16:46:32 +01006556 win_T *wp = reg_win == NULL ? curwin : reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006557
6558 /* Bail out quickly when there can't be a match, avoid the
6559 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006560 if (op != 1 && col > t->state->val
6561#ifdef FEAT_MBYTE
6562 * (has_mbyte ? MB_MAXBYTES : 1)
6563#endif
6564 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006565 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006566 result = FALSE;
6567 if (op == 1 && col - 1 > t->state->val && col > 100)
6568 {
6569 int ts = wp->w_buffer->b_p_ts;
6570
6571 /* Guess that a character won't use more columns than
6572 * 'tabstop', with a minimum of 4. */
6573 if (ts < 4)
6574 ts = 4;
6575 result = col > t->state->val * ts;
6576 }
6577 if (!result)
6578 result = nfa_re_num_cmp(t->state->val, op,
6579 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006580 if (result)
6581 {
6582 add_here = TRUE;
6583 add_state = t->state->out;
6584 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006585 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006586 break;
6587
Bram Moolenaar044aa292013-06-04 21:27:38 +02006588 case NFA_MARK:
6589 case NFA_MARK_GT:
6590 case NFA_MARK_LT:
6591 {
6592 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6593
6594 /* Compare the mark position to the match position. */
6595 result = (pos != NULL /* mark doesn't exist */
6596 && pos->lnum > 0 /* mark isn't set in reg_buf */
6597 && (pos->lnum == reglnum + reg_firstlnum
6598 ? (pos->col == (colnr_T)(reginput - regline)
6599 ? t->state->c == NFA_MARK
6600 : (pos->col < (colnr_T)(reginput - regline)
6601 ? t->state->c == NFA_MARK_GT
6602 : t->state->c == NFA_MARK_LT))
6603 : (pos->lnum < reglnum + reg_firstlnum
6604 ? t->state->c == NFA_MARK_GT
6605 : t->state->c == NFA_MARK_LT)));
6606 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006607 {
6608 add_here = TRUE;
6609 add_state = t->state->out;
6610 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006611 break;
6612 }
6613
Bram Moolenaar423532e2013-05-29 21:14:42 +02006614 case NFA_CURSOR:
6615 result = (reg_win != NULL
6616 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6617 && ((colnr_T)(reginput - regline)
6618 == reg_win->w_cursor.col));
6619 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006620 {
6621 add_here = TRUE;
6622 add_state = t->state->out;
6623 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006624 break;
6625
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006626 case NFA_VISUAL:
6627 result = reg_match_visual();
6628 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006629 {
6630 add_here = TRUE;
6631 add_state = t->state->out;
6632 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006633 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006634
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006635 case NFA_MOPEN1:
6636 case NFA_MOPEN2:
6637 case NFA_MOPEN3:
6638 case NFA_MOPEN4:
6639 case NFA_MOPEN5:
6640 case NFA_MOPEN6:
6641 case NFA_MOPEN7:
6642 case NFA_MOPEN8:
6643 case NFA_MOPEN9:
6644#ifdef FEAT_SYN_HL
6645 case NFA_ZOPEN:
6646 case NFA_ZOPEN1:
6647 case NFA_ZOPEN2:
6648 case NFA_ZOPEN3:
6649 case NFA_ZOPEN4:
6650 case NFA_ZOPEN5:
6651 case NFA_ZOPEN6:
6652 case NFA_ZOPEN7:
6653 case NFA_ZOPEN8:
6654 case NFA_ZOPEN9:
6655#endif
6656 case NFA_NOPEN:
6657 case NFA_ZSTART:
6658 /* These states are only added to be able to bail out when
6659 * they are added again, nothing is to be done. */
6660 break;
6661
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006662 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006663 {
6664 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006665
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006666#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006667 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006668 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006669#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006670 result = (c == curc);
6671
6672 if (!result && ireg_ic)
6673 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006674#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006675 /* If ireg_icombine is not set only skip over the character
6676 * itself. When it is set skip over composing characters. */
6677 if (result && enc_utf8 && !ireg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006678 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006679#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006680 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006681 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006682 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006683
6684 } /* switch (t->state->c) */
6685
6686 if (add_state != NULL)
6687 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006688 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006689 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006690
6691 if (t->pim.result == NFA_PIM_UNUSED)
6692 pim = NULL;
6693 else
6694 pim = &t->pim;
6695
6696 /* Handle the postponed invisible match if the match might end
6697 * without advancing and before the end of the line. */
6698 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006699 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006700 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006701 {
6702#ifdef ENABLE_LOG
6703 fprintf(log_fd, "\n");
6704 fprintf(log_fd, "==================================\n");
6705 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6706 fprintf(log_fd, "\n");
6707#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006708 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006709 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006710 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006711 /* for \@! and \@<! it is a match when the result is
6712 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006713 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006714 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6715 || pim->state->c
6716 == NFA_START_INVISIBLE_BEFORE_NEG
6717 || pim->state->c
6718 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006719 {
6720 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006721 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006722#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006723 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006724 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006725#endif
6726 }
6727 }
6728 else
6729 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006730 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006731#ifdef ENABLE_LOG
6732 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006733 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006734 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6735 fprintf(log_fd, "\n");
6736#endif
6737 }
6738
Bram Moolenaardecd9542013-06-07 16:31:50 +02006739 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006740 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006741 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6742 || pim->state->c
6743 == NFA_START_INVISIBLE_BEFORE_NEG
6744 || pim->state->c
6745 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006746 {
6747 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006748 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006749#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006750 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006751 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006752#endif
6753 }
6754 else
6755 /* look-behind match failed, don't add the state */
6756 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006757
6758 /* Postponed invisible match was handled, don't add it to
6759 * following states. */
6760 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006761 }
6762
Bram Moolenaara951e352013-10-06 15:46:11 +02006763 /* If "pim" points into l->t it will become invalid when
6764 * adding the state causes the list to be reallocated. Make a
6765 * local copy to avoid that. */
6766 if (pim == &t->pim)
6767 {
6768 copy_pim(&pim_copy, pim);
6769 pim = &pim_copy;
6770 }
6771
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006772 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006773 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006774 else
6775 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006776 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006777 if (add_count > 0)
6778 nextlist->t[nextlist->n - 1].count = add_count;
6779 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006780 }
6781
6782 } /* for (thislist = thislist; thislist->state; thislist++) */
6783
Bram Moolenaare23febd2013-05-26 18:40:14 +02006784 /* Look for the start of a match in the current position by adding the
6785 * start state to the list of states.
6786 * The first found match is the leftmost one, thus the order of states
6787 * matters!
6788 * Do not add the start state in recursive calls of nfa_regmatch(),
6789 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006790 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006791 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006792 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006793 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006794 && reglnum == 0
6795 && clen != 0
6796 && (ireg_maxcol == 0
6797 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006798 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006799 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006800 ? (reglnum < nfa_endp->se_u.pos.lnum
6801 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006802 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006803 < nfa_endp->se_u.pos.col))
6804 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006805 {
6806#ifdef ENABLE_LOG
6807 fprintf(log_fd, "(---) STARTSTATE\n");
6808#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006809 /* Inline optimized code for addstate() if we know the state is
6810 * the first MOPEN. */
6811 if (toplevel)
6812 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006813 int add = TRUE;
6814 int c;
6815
6816 if (prog->regstart != NUL && clen != 0)
6817 {
6818 if (nextlist->n == 0)
6819 {
6820 colnr_T col = (colnr_T)(reginput - regline) + clen;
6821
6822 /* Nextlist is empty, we can skip ahead to the
6823 * character that must appear at the start. */
6824 if (skip_to_start(prog->regstart, &col) == FAIL)
6825 break;
6826#ifdef ENABLE_LOG
6827 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6828 col - ((colnr_T)(reginput - regline) + clen));
6829#endif
6830 reginput = regline + col - clen;
6831 }
6832 else
6833 {
6834 /* Checking if the required start character matches is
6835 * cheaper than adding a state that won't match. */
6836 c = PTR2CHAR(reginput + clen);
6837 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6838 != MB_TOLOWER(prog->regstart)))
6839 {
6840#ifdef ENABLE_LOG
6841 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6842#endif
6843 add = FALSE;
6844 }
6845 }
6846 }
6847
6848 if (add)
6849 {
6850 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006851 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006852 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006853 else
6854 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006855 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006856 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006857 }
6858 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006859 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006860 }
6861
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006862#ifdef ENABLE_LOG
6863 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006864 {
6865 int i;
6866
6867 for (i = 0; i < thislist->n; i++)
6868 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6869 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006870 fprintf(log_fd, "\n");
6871#endif
6872
6873nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006874 /* Advance to the next character, or advance to the next line, or
6875 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006876 if (clen != 0)
6877 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006878 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6879 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006880 reg_nextline();
6881 else
6882 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006883
6884 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006885 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006886 if (got_int)
6887 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006888#ifdef FEAT_RELTIME
6889 /* Check for timeout once in a twenty times to avoid overhead. */
6890 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6891 {
6892 nfa_time_count = 0;
6893 if (profile_passed_limit(nfa_time_limit))
6894 break;
6895 }
6896#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006897 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006898
6899#ifdef ENABLE_LOG
6900 if (log_fd != stderr)
6901 fclose(log_fd);
6902 log_fd = NULL;
6903#endif
6904
6905theend:
6906 /* Free memory */
6907 vim_free(list[0].t);
6908 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006909 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006910#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006911#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006912 fclose(debug);
6913#endif
6914
Bram Moolenaar963fee22013-05-26 21:47:28 +02006915 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006916}
6917
6918/*
6919 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006920 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006921 */
6922 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006923nfa_regtry(
6924 nfa_regprog_T *prog,
6925 colnr_T col,
6926 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006927{
6928 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006929 regsubs_T subs, m;
6930 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006931 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006932#ifdef ENABLE_LOG
6933 FILE *f;
6934#endif
6935
6936 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006937#ifdef FEAT_RELTIME
6938 nfa_time_limit = tm;
6939 nfa_time_count = 0;
6940#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006941
6942#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006943 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006944 if (f != NULL)
6945 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006946 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006947#ifdef DEBUG
6948 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6949#endif
6950 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006951 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006952 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006953 fprintf(f, "\n\n");
6954 fclose(f);
6955 }
6956 else
6957 EMSG(_("Could not open temporary log file for writing "));
6958#endif
6959
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006960 clear_sub(&subs.norm);
6961 clear_sub(&m.norm);
6962#ifdef FEAT_SYN_HL
6963 clear_sub(&subs.synt);
6964 clear_sub(&m.synt);
6965#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006966
Bram Moolenaarfda37292014-11-05 14:27:36 +01006967 result = nfa_regmatch(prog, start, &subs, &m);
6968 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006969 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006970 else if (result == NFA_TOO_EXPENSIVE)
6971 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972
6973 cleanup_subexpr();
6974 if (REG_MULTI)
6975 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006976 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006977 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006978 reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6979 reg_startpos[i].col = subs.norm.list.multi[i].start_col;
6980
6981 reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6982 reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006983 }
6984
6985 if (reg_startpos[0].lnum < 0)
6986 {
6987 reg_startpos[0].lnum = 0;
6988 reg_startpos[0].col = col;
6989 }
6990 if (reg_endpos[0].lnum < 0)
6991 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006992 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006993 reg_endpos[0].lnum = reglnum;
6994 reg_endpos[0].col = (int)(reginput - regline);
6995 }
6996 else
6997 /* Use line number of "\ze". */
6998 reglnum = reg_endpos[0].lnum;
6999 }
7000 else
7001 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007002 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007003 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007004 reg_startp[i] = subs.norm.list.line[i].start;
7005 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007006 }
7007
7008 if (reg_startp[0] == NULL)
7009 reg_startp[0] = regline + col;
7010 if (reg_endp[0] == NULL)
7011 reg_endp[0] = reginput;
7012 }
7013
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007014#ifdef FEAT_SYN_HL
7015 /* Package any found \z(...\) matches for export. Default is none. */
7016 unref_extmatch(re_extmatch_out);
7017 re_extmatch_out = NULL;
7018
7019 if (prog->reghasz == REX_SET)
7020 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007021 cleanup_zsubexpr();
7022 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007023 /* Loop over \z1, \z2, etc. There is no \z0. */
7024 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007025 {
7026 if (REG_MULTI)
7027 {
7028 struct multipos *mpos = &subs.synt.list.multi[i];
7029
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007030 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007031 if (mpos->start_lnum >= 0
7032 && mpos->start_lnum == mpos->end_lnum
7033 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007034 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007035 vim_strnsave(reg_getline(mpos->start_lnum)
7036 + mpos->start_col,
7037 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007038 }
7039 else
7040 {
7041 struct linepos *lpos = &subs.synt.list.line[i];
7042
7043 if (lpos->start != NULL && lpos->end != NULL)
7044 re_extmatch_out->matches[i] =
7045 vim_strnsave(lpos->start,
7046 (int)(lpos->end - lpos->start));
7047 }
7048 }
7049 }
7050#endif
7051
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007052 return 1 + reglnum;
7053}
7054
7055/*
7056 * Match a regexp against a string ("line" points to the string) or multiple
7057 * lines ("line" is NULL, use reg_getline()).
7058 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007059 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007060 */
7061 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007062nfa_regexec_both(
7063 char_u *line,
7064 colnr_T startcol, /* column to start looking for match */
7065 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007066{
7067 nfa_regprog_T *prog;
7068 long retval = 0L;
7069 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007070 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007071
7072 if (REG_MULTI)
7073 {
7074 prog = (nfa_regprog_T *)reg_mmatch->regprog;
7075 line = reg_getline((linenr_T)0); /* relative to the cursor */
7076 reg_startpos = reg_mmatch->startpos;
7077 reg_endpos = reg_mmatch->endpos;
7078 }
7079 else
7080 {
7081 prog = (nfa_regprog_T *)reg_match->regprog;
7082 reg_startp = reg_match->startp;
7083 reg_endp = reg_match->endp;
7084 }
7085
7086 /* Be paranoid... */
7087 if (prog == NULL || line == NULL)
7088 {
7089 EMSG(_(e_null));
7090 goto theend;
7091 }
7092
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007093 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
7094 if (prog->regflags & RF_ICASE)
7095 ireg_ic = TRUE;
7096 else if (prog->regflags & RF_NOICASE)
7097 ireg_ic = FALSE;
7098
7099#ifdef FEAT_MBYTE
7100 /* If pattern contains "\Z" overrule value of ireg_icombine */
7101 if (prog->regflags & RF_ICOMBINE)
7102 ireg_icombine = TRUE;
7103#endif
7104
7105 regline = line;
7106 reglnum = 0; /* relative to line */
7107
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007108 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007109 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007110 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007111 nfa_listid = 1;
7112 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007113 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007114
Bram Moolenaard89616e2013-06-06 18:46:06 +02007115 if (prog->reganch && col > 0)
7116 return 0L;
7117
Bram Moolenaar473de612013-06-08 18:19:48 +02007118 need_clear_subexpr = TRUE;
7119#ifdef FEAT_SYN_HL
7120 /* Clear the external match subpointers if necessary. */
7121 if (prog->reghasz == REX_SET)
7122 {
7123 nfa_has_zsubexpr = TRUE;
7124 need_clear_zsubexpr = TRUE;
7125 }
7126 else
7127 nfa_has_zsubexpr = FALSE;
7128#endif
7129
Bram Moolenaard89616e2013-06-06 18:46:06 +02007130 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007131 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007132 /* Skip ahead until a character we know the match must start with.
7133 * When there is none there is no match. */
7134 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007135 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007136
Bram Moolenaar473de612013-06-08 18:19:48 +02007137 /* If match_text is set it contains the full text that must match.
7138 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007139 if (prog->match_text != NULL
7140#ifdef FEAT_MBYTE
7141 && !ireg_icombine
7142#endif
7143 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007144 return find_match_text(col, prog->regstart, prog->match_text);
7145 }
7146
Bram Moolenaard89616e2013-06-06 18:46:06 +02007147 /* If the start column is past the maximum column: no need to try. */
7148 if (ireg_maxcol > 0 && col >= ireg_maxcol)
7149 goto theend;
7150
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007151 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007152 for (i = 0; i < nstate; ++i)
7153 {
7154 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007155 prog->state[i].lastlist[0] = 0;
7156 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007157 }
7158
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007159 retval = nfa_regtry(prog, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007160
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007161 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007162
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007163theend:
7164 return retval;
7165}
7166
7167/*
7168 * Compile a regular expression into internal code for the NFA matcher.
7169 * Returns the program in allocated space. Returns NULL for an error.
7170 */
7171 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007172nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007173{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007174 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007175 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007176 int *postfix;
7177
7178 if (expr == NULL)
7179 return NULL;
7180
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007181 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007182 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007183
7184 init_class_tab();
7185
7186 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7187 return NULL;
7188
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007189 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007190 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007191 postfix = re2post();
7192 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007193 {
7194 /* TODO: only give this error for debugging? */
7195 if (post_ptr >= post_end)
7196 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007197 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007198 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007199
7200 /*
7201 * In order to build the NFA, we parse the input regexp twice:
7202 * 1. first pass to count size (so we can allocate space)
7203 * 2. second to emit code
7204 */
7205#ifdef ENABLE_LOG
7206 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007207 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208
7209 if (f != NULL)
7210 {
7211 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7212 fclose(f);
7213 }
7214 }
7215#endif
7216
7217 /*
7218 * PASS 1
7219 * Count number of NFA states in "nstate". Do not build the NFA.
7220 */
7221 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007222
Bram Moolenaar16619a22013-06-11 18:42:36 +02007223 /* allocate the regprog with space for the compiled regexp */
7224 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007225 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7226 if (prog == NULL)
7227 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228 state_ptr = prog->state;
7229
7230 /*
7231 * PASS 2
7232 * Build the NFA
7233 */
7234 prog->start = post2nfa(postfix, post_ptr, FALSE);
7235 if (prog->start == NULL)
7236 goto fail;
7237
7238 prog->regflags = regflags;
7239 prog->engine = &nfa_regengine;
7240 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007241 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007242 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007243 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007244
Bram Moolenaara2947e22013-06-11 22:44:09 +02007245 nfa_postprocess(prog);
7246
Bram Moolenaard89616e2013-06-06 18:46:06 +02007247 prog->reganch = nfa_get_reganch(prog->start, 0);
7248 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007249 prog->match_text = nfa_get_match_text(prog->start);
7250
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007251#ifdef ENABLE_LOG
7252 nfa_postfix_dump(expr, OK);
7253 nfa_dump(prog);
7254#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007255#ifdef FEAT_SYN_HL
7256 /* Remember whether this pattern has any \z specials in it. */
7257 prog->reghasz = re_has_z;
7258#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007259 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007260 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007261
7262out:
7263 vim_free(post_start);
7264 post_start = post_ptr = post_end = NULL;
7265 state_ptr = NULL;
7266 return (regprog_T *)prog;
7267
7268fail:
7269 vim_free(prog);
7270 prog = NULL;
7271#ifdef ENABLE_LOG
7272 nfa_postfix_dump(expr, FAIL);
7273#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007274 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007275 goto out;
7276}
7277
Bram Moolenaar473de612013-06-08 18:19:48 +02007278/*
7279 * Free a compiled regexp program, returned by nfa_regcomp().
7280 */
7281 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007282nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007283{
7284 if (prog != NULL)
7285 {
7286 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007287 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007288 vim_free(prog);
7289 }
7290}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007291
7292/*
7293 * Match a regexp against a string.
7294 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7295 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007296 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007297 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007298 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007299 */
7300 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007301nfa_regexec_nl(
7302 regmatch_T *rmp,
7303 char_u *line, /* string to match against */
7304 colnr_T col, /* column to start looking for match */
7305 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007306{
7307 reg_match = rmp;
7308 reg_mmatch = NULL;
7309 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007310 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007311 reg_buf = curbuf;
7312 reg_win = NULL;
7313 ireg_ic = rmp->rm_ic;
7314#ifdef FEAT_MBYTE
7315 ireg_icombine = FALSE;
7316#endif
7317 ireg_maxcol = 0;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007318 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007319}
7320
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007321
7322/*
7323 * Match a regexp against multiple lines.
7324 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7325 * Uses curbuf for line count and 'iskeyword'.
7326 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007327 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007328 * match otherwise.
7329 *
7330 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7331 *
7332 * ! Also NOTE : match may actually be in another line. e.g.:
7333 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7334 *
7335 * +-------------------------+
7336 * |a |
7337 * |b |
7338 * |c |
7339 * | |
7340 * +-------------------------+
7341 *
7342 * then nfa_regexec_multi() returns 3. while the original
7343 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7344 *
7345 * FIXME if this behavior is not compatible.
7346 */
7347 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007348nfa_regexec_multi(
7349 regmmatch_T *rmp,
7350 win_T *win, /* window in which to search or NULL */
7351 buf_T *buf, /* buffer in which to search */
7352 linenr_T lnum, /* nr of line to start looking for match */
7353 colnr_T col, /* column to start looking for match */
7354 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007355{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007356 reg_match = NULL;
7357 reg_mmatch = rmp;
7358 reg_buf = buf;
7359 reg_win = win;
7360 reg_firstlnum = lnum;
7361 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7362 reg_line_lbr = FALSE;
7363 ireg_ic = rmp->rmm_ic;
7364#ifdef FEAT_MBYTE
7365 ireg_icombine = FALSE;
7366#endif
7367 ireg_maxcol = rmp->rmm_maxcol;
7368
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007369 return nfa_regexec_both(NULL, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007370}
7371
7372#ifdef DEBUG
7373# undef ENABLE_LOG
7374#endif