blob: 8bcf3bc413196f5630d0b2298dee9f33a1aa2eb5 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
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 {
753 switch (c)
754 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200755 case 'A': case 0300: case 0301: case 0302:
756 case 0303: case 0304: case 0305:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200757 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
758 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
759 EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302);
760 EMIT2(0303); EMIT2(0304); EMIT2(0305);
761 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
762 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
763 EMITMBC(0x1ea2)
764 return OK;
765
766 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
767 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200768 return OK;
769
Bram Moolenaar417bad22013-06-07 14:08:30 +0200770 case 'C': case 0307:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200771 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
772 EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108)
773 EMITMBC(0x10a) EMITMBC(0x10c)
774 return OK;
775
776 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
777 CASEMBC(0x1e0e) CASEMBC(0x1e10)
778 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
779 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200780 return OK;
781
Bram Moolenaar417bad22013-06-07 14:08:30 +0200782 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200783 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
784 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
785 EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312);
786 EMIT2(0313);
787 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
788 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
789 EMITMBC(0x1ebc)
790 return OK;
791
792 case 'F': CASEMBC(0x1e1e)
793 EMIT2('F'); EMITMBC(0x1e1e)
794 return OK;
795
796 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
797 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
798 CASEMBC(0x1e20)
799 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
800 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
801 EMITMBC(0x1f4) EMITMBC(0x1e20)
802 return OK;
803
804 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
805 CASEMBC(0x1e26) CASEMBC(0x1e28)
806 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
807 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 return OK;
809
Bram Moolenaar417bad22013-06-07 14:08:30 +0200810 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200811 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
812 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
813 EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316);
814 EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a)
815 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
816 EMITMBC(0x1cf) EMITMBC(0x1ec8)
817 return OK;
818
819 case 'J': CASEMBC(0x134)
820 EMIT2('J'); EMITMBC(0x134)
821 return OK;
822
823 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
824 CASEMBC(0x1e34)
825 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
826 EMITMBC(0x1e34)
827 return OK;
828
829 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
830 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
831 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
832 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
833 return OK;
834
835 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
836 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200837 return OK;
838
Bram Moolenaar417bad22013-06-07 14:08:30 +0200839 case 'N': case 0321:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200840 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
841 CASEMBC(0x1e48)
842 EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145)
843 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200844 return OK;
845
Bram Moolenaar417bad22013-06-07 14:08:30 +0200846 case 'O': case 0322: case 0323: case 0324: case 0325:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200847 case 0326: case 0330:
848 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
849 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
850 EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324);
851 EMIT2(0325); EMIT2(0326); EMIT2(0330);
852 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
853 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
854 EMITMBC(0x1ec) EMITMBC(0x1ece)
855 return OK;
856
857 case 'P': case 0x1e54: case 0x1e56:
858 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
859 return OK;
860
861 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
862 CASEMBC(0x1e58) CASEMBC(0x1e5e)
863 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
864 EMITMBC(0x1e58) EMITMBC(0x1e5e)
865 return OK;
866
867 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
868 CASEMBC(0x160) CASEMBC(0x1e60)
869 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
870 EMITMBC(0x160) EMITMBC(0x1e60)
871 return OK;
872
873 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
874 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
875 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
876 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200877 return OK;
878
Bram Moolenaar417bad22013-06-07 14:08:30 +0200879 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200880 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
881 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
882 CASEMBC(0x1ee6)
883 EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333);
884 EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a)
885 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
886 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
887 EMITMBC(0x1ee6)
888 return OK;
889
890 case 'V': CASEMBC(0x1e7c)
891 EMIT2('V'); EMITMBC(0x1e7c)
892 return OK;
893
894 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
895 CASEMBC(0x1e84) CASEMBC(0x1e86)
896 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
897 EMITMBC(0x1e84) EMITMBC(0x1e86)
898 return OK;
899
900 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
901 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200902 return OK;
903
Bram Moolenaar417bad22013-06-07 14:08:30 +0200904 case 'Y': case 0335:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200905 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
906 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
907 EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178)
908 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
909 EMITMBC(0x1ef8)
910 return OK;
911
912 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
913 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
914 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
915 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200916 return OK;
917
Bram Moolenaar417bad22013-06-07 14:08:30 +0200918 case 'a': case 0340: case 0341: case 0342:
919 case 0343: case 0344: case 0345:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200920 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
921 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
922 EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342);
923 EMIT2(0343); EMIT2(0344); EMIT2(0345);
924 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
925 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
926 EMITMBC(0x1ea3)
927 return OK;
928
929 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
930 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200931 return OK;
932
Bram Moolenaar417bad22013-06-07 14:08:30 +0200933 case 'c': case 0347:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200934 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
935 EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109)
936 EMITMBC(0x10b) EMITMBC(0x10d)
937 return OK;
938
Bram Moolenaar2c61ec62015-07-10 19:16:34 +0200939 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
940 CASEMBC(0x1e0f) CASEMBC(0x1e11)
941 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
942 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200943 return OK;
944
Bram Moolenaar417bad22013-06-07 14:08:30 +0200945 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200946 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
947 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
948 EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352);
949 EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115)
950 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
951 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
952 return OK;
953
954 case 'f': CASEMBC(0x1e1f)
955 EMIT2('f'); EMITMBC(0x1e1f)
956 return OK;
957
958 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
959 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
960 CASEMBC(0x1e21)
961 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
962 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
963 EMITMBC(0x1f5) EMITMBC(0x1e21)
964 return OK;
965
966 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
967 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
968 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
969 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200970 return OK;
971
Bram Moolenaar417bad22013-06-07 14:08:30 +0200972 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200973 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
974 CASEMBC(0x1d0) CASEMBC(0x1ec9)
975 EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356);
976 EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b)
977 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
978 EMITMBC(0x1ec9)
979 return OK;
980
981 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
982 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
983 return OK;
984
985 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
986 CASEMBC(0x1e35)
987 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
988 EMITMBC(0x1e35)
989 return OK;
990
991 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
992 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
993 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
994 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
995 return OK;
996
997 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
998 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200999 return OK;
1000
Bram Moolenaar417bad22013-06-07 14:08:30 +02001001 case 'n': case 0361:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001002 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1003 CASEMBC(0x1e45) CASEMBC(0x1e49)
1004 EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146)
1005 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1006 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001007 return OK;
1008
Bram Moolenaar417bad22013-06-07 14:08:30 +02001009 case 'o': case 0362: case 0363: case 0364: case 0365:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 case 0366: case 0370:
1011 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1012 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
1013 EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364);
1014 EMIT2(0365); EMIT2(0366); EMIT2(0370);
1015 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1016 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1017 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1018 return OK;
1019
1020 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1021 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1022 return OK;
1023
1024 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1025 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1026 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1027 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1028 return OK;
1029
1030 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1031 CASEMBC(0x161) CASEMBC(0x1e61)
1032 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1033 EMITMBC(0x161) EMITMBC(0x1e61)
1034 return OK;
1035
1036 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1037 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1038 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1039 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001040 return OK;
1041
Bram Moolenaar417bad22013-06-07 14:08:30 +02001042 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001043 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1044 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1045 CASEMBC(0x1ee7)
1046 EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373);
1047 EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b)
1048 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1049 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1050 EMITMBC(0x1ee7)
1051 return OK;
1052
1053 case 'v': CASEMBC(0x1e7d)
1054 EMIT2('v'); EMITMBC(0x1e7d)
1055 return OK;
1056
1057 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1058 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1059 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1060 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1061 return OK;
1062
1063 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1064 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001065 return OK;
1066
Bram Moolenaar417bad22013-06-07 14:08:30 +02001067 case 'y': case 0375: case 0377:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001068 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1069 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1070 EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177)
1071 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1072 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001073 return OK;
1074
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001075 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1076 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1077 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1078 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1079 return OK;
1080
1081 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001082 }
1083 }
1084
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001085 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 return OK;
1087#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001088#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001089}
1090
1091/*
1092 * Code to parse regular expression.
1093 *
1094 * We try to reuse parsing functions in regexp.c to
1095 * minimize surprise and keep the syntax consistent.
1096 */
1097
1098/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001099 * Parse the lowest level.
1100 *
1101 * An atom can be one of a long list of items. Many atoms match one character
1102 * in the text. It is often an ordinary character or a character class.
1103 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1104 * is only for syntax highlighting.
1105 *
1106 * atom ::= ordinary-atom
1107 * or \( pattern \)
1108 * or \%( pattern \)
1109 * or \z( pattern \)
1110 */
1111 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001112nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001113{
1114 int c;
1115 int charclass;
1116 int equiclass;
1117 int collclass;
1118 int got_coll_char;
1119 char_u *p;
1120 char_u *endp;
1121#ifdef FEAT_MBYTE
1122 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001123#endif
1124 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001125 int emit_range;
1126 int negated;
1127 int result;
1128 int startc = -1;
1129 int endc = -1;
1130 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001131
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001133 switch (c)
1134 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001135 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001136 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001137
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001138 case Magic('^'):
1139 EMIT(NFA_BOL);
1140 break;
1141
1142 case Magic('$'):
1143 EMIT(NFA_EOL);
1144#if defined(FEAT_SYN_HL) || defined(PROTO)
1145 had_eol = TRUE;
1146#endif
1147 break;
1148
1149 case Magic('<'):
1150 EMIT(NFA_BOW);
1151 break;
1152
1153 case Magic('>'):
1154 EMIT(NFA_EOW);
1155 break;
1156
1157 case Magic('_'):
1158 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001159 if (c == NUL)
1160 EMSG_RET_FAIL(_(e_nul_found));
1161
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001162 if (c == '^') /* "\_^" is start-of-line */
1163 {
1164 EMIT(NFA_BOL);
1165 break;
1166 }
1167 if (c == '$') /* "\_$" is end-of-line */
1168 {
1169 EMIT(NFA_EOL);
1170#if defined(FEAT_SYN_HL) || defined(PROTO)
1171 had_eol = TRUE;
1172#endif
1173 break;
1174 }
1175
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001176 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001177
1178 /* "\_[" is collection plus newline */
1179 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001180 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001181
1182 /* "\_x" is character class plus newline */
1183 /*FALLTHROUGH*/
1184
1185 /*
1186 * Character classes.
1187 */
1188 case Magic('.'):
1189 case Magic('i'):
1190 case Magic('I'):
1191 case Magic('k'):
1192 case Magic('K'):
1193 case Magic('f'):
1194 case Magic('F'):
1195 case Magic('p'):
1196 case Magic('P'):
1197 case Magic('s'):
1198 case Magic('S'):
1199 case Magic('d'):
1200 case Magic('D'):
1201 case Magic('x'):
1202 case Magic('X'):
1203 case Magic('o'):
1204 case Magic('O'):
1205 case Magic('w'):
1206 case Magic('W'):
1207 case Magic('h'):
1208 case Magic('H'):
1209 case Magic('a'):
1210 case Magic('A'):
1211 case Magic('l'):
1212 case Magic('L'):
1213 case Magic('u'):
1214 case Magic('U'):
1215 p = vim_strchr(classchars, no_Magic(c));
1216 if (p == NULL)
1217 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001218 if (extra == NFA_ADD_NL)
1219 {
1220 EMSGN(_(e_ill_char_class), c);
1221 rc_did_emsg = TRUE;
1222 return FAIL;
1223 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001224 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1225 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226 }
1227#ifdef FEAT_MBYTE
1228 /* When '.' is followed by a composing char ignore the dot, so that
1229 * the composing char is matched here. */
1230 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1231 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001232 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001233 c = getchr();
1234 goto nfa_do_multibyte;
1235 }
1236#endif
1237 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001238 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001239 {
1240 EMIT(NFA_NEWL);
1241 EMIT(NFA_OR);
1242 regflags |= RF_HASNL;
1243 }
1244 break;
1245
1246 case Magic('n'):
1247 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001248 /* In a string "\n" matches a newline character. */
1249 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001250 else
1251 {
1252 /* In buffer text "\n" matches the end of a line. */
1253 EMIT(NFA_NEWL);
1254 regflags |= RF_HASNL;
1255 }
1256 break;
1257
1258 case Magic('('):
1259 if (nfa_reg(REG_PAREN) == FAIL)
1260 return FAIL; /* cascaded error */
1261 break;
1262
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263 case Magic('|'):
1264 case Magic('&'):
1265 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001266 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267 return FAIL;
1268
1269 case Magic('='):
1270 case Magic('?'):
1271 case Magic('+'):
1272 case Magic('@'):
1273 case Magic('*'):
1274 case Magic('{'):
1275 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001276 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001277 return FAIL;
1278
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001279 case Magic('~'):
1280 {
1281 char_u *lp;
1282
1283 /* Previous substitute pattern.
1284 * Generated as "\%(pattern\)". */
1285 if (reg_prev_sub == NULL)
1286 {
1287 EMSG(_(e_nopresub));
1288 return FAIL;
1289 }
1290 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1291 {
1292 EMIT(PTR2CHAR(lp));
1293 if (lp != reg_prev_sub)
1294 EMIT(NFA_CONCAT);
1295 }
1296 EMIT(NFA_NOPEN);
1297 break;
1298 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001299
Bram Moolenaar428e9872013-05-30 17:05:39 +02001300 case Magic('1'):
1301 case Magic('2'):
1302 case Magic('3'):
1303 case Magic('4'):
1304 case Magic('5'):
1305 case Magic('6'):
1306 case Magic('7'):
1307 case Magic('8'):
1308 case Magic('9'):
1309 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1310 nfa_has_backref = TRUE;
1311 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001312
1313 case Magic('z'):
1314 c = no_Magic(getchr());
1315 switch (c)
1316 {
1317 case 's':
1318 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001319 if (re_mult_next("\\zs") == FAIL)
1320 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321 break;
1322 case 'e':
1323 EMIT(NFA_ZEND);
1324 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001325 if (re_mult_next("\\ze") == FAIL)
1326 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001327 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001328#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001329 case '1':
1330 case '2':
1331 case '3':
1332 case '4':
1333 case '5':
1334 case '6':
1335 case '7':
1336 case '8':
1337 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001338 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001339 if (reg_do_extmatch != REX_USE)
1340 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001341 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1342 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001343 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001344 re_has_z = REX_USE;
1345 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001346 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001347 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001348 if (reg_do_extmatch != REX_SET)
1349 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001350 if (nfa_reg(REG_ZPAREN) == FAIL)
1351 return FAIL; /* cascaded error */
1352 re_has_z = REX_SET;
1353 break;
1354#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001356 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001357 no_Magic(c));
1358 return FAIL;
1359 }
1360 break;
1361
1362 case Magic('%'):
1363 c = no_Magic(getchr());
1364 switch (c)
1365 {
1366 /* () without a back reference */
1367 case '(':
1368 if (nfa_reg(REG_NPAREN) == FAIL)
1369 return FAIL;
1370 EMIT(NFA_NOPEN);
1371 break;
1372
1373 case 'd': /* %d123 decimal */
1374 case 'o': /* %o123 octal */
1375 case 'x': /* %xab hex 2 */
1376 case 'u': /* %uabcd hex 4 */
1377 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001378 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001379 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001380
Bram Moolenaar47196582013-05-25 22:04:23 +02001381 switch (c)
1382 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001383 case 'd': nr = getdecchrs(); break;
1384 case 'o': nr = getoctchrs(); break;
1385 case 'x': nr = gethexchrs(2); break;
1386 case 'u': nr = gethexchrs(4); break;
1387 case 'U': nr = gethexchrs(8); break;
1388 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001389 }
1390
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001391 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001392 EMSG2_RET_FAIL(
1393 _("E678: Invalid character after %s%%[dxouU]"),
1394 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001395 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001396 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001397 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001398 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399 break;
1400
1401 /* Catch \%^ and \%$ regardless of where they appear in the
1402 * pattern -- regardless of whether or not it makes sense. */
1403 case '^':
1404 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405 break;
1406
1407 case '$':
1408 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001409 break;
1410
1411 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001412 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 break;
1414
1415 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001416 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 break;
1418
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001419 case 'C':
1420 EMIT(NFA_ANY_COMPOSING);
1421 break;
1422
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001424 {
1425 int n;
1426
1427 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001428 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001429 {
1430 if (c == NUL)
1431 EMSG2_RET_FAIL(_(e_missing_sb),
1432 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001433 /* recursive call! */
1434 if (nfa_regatom() == FAIL)
1435 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001436 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001437 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001438 if (n == 0)
1439 EMSG2_RET_FAIL(_(e_empty_sb),
1440 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001441 EMIT(NFA_OPT_CHARS);
1442 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001443
1444 /* Emit as "\%(\%[abc]\)" to be able to handle
1445 * "\%[abc]*" which would cause the empty string to be
1446 * matched an unlimited number of times. NFA_NOPEN is
1447 * added only once at a position, while NFA_SPLIT is
1448 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001449 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001450 * a lot. */
1451 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001452 break;
1453 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001454
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001455 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001456 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001457 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001458 int cmp = c;
1459
1460 if (c == '<' || c == '>')
1461 c = getchr();
1462 while (VIM_ISDIGIT(c))
1463 {
1464 n = n * 10 + (c - '0');
1465 c = getchr();
1466 }
1467 if (c == 'l' || c == 'c' || c == 'v')
1468 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001469 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001470 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001471 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001472 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001473 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001474 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001475 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001476 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001477 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001478 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001479 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001480 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001481 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001482 break;
1483 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001484 else if (c == '\'' && n == 0)
1485 {
1486 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001487 EMIT(cmp == '<' ? NFA_MARK_LT :
1488 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001489 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001490 break;
1491 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001492 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001493 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1494 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 return FAIL;
1496 }
1497 break;
1498
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001500collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001502 * [abc] uses NFA_START_COLL - NFA_END_COLL
1503 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1504 * Each character is produced as a regular state, using
1505 * NFA_CONCAT to bind them together.
1506 * Besides normal characters there can be:
1507 * - character classes NFA_CLASS_*
1508 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 */
1510
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 p = regparse;
1512 endp = skip_anyof(p);
1513 if (*endp == ']')
1514 {
1515 /*
1516 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001517 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 * and perform the necessary substitutions in the NFA.
1519 */
1520 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001521 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001522 if (result != FAIL)
1523 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001524 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001525 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001526 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001527 EMIT(NFA_NEWL);
1528 EMIT(NFA_OR);
1529 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001530 else
1531 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001533 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001534 return OK;
1535 }
1536 /*
1537 * Failed to recognize a character class. Use the simple
1538 * version that turns [abc] into 'a' OR 'b' OR 'c'
1539 */
1540 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001541 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 if (*regparse == '^') /* negated range */
1543 {
1544 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001545 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001546 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001547 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001548 else
1549 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 if (*regparse == '-')
1551 {
1552 startc = '-';
1553 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001554 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001555 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001556 }
1557 /* Emit the OR branches for each character in the [] */
1558 emit_range = FALSE;
1559 while (regparse < endp)
1560 {
1561 oldstartc = startc;
1562 startc = -1;
1563 got_coll_char = FALSE;
1564 if (*regparse == '[')
1565 {
1566 /* Check for [: :], [= =], [. .] */
1567 equiclass = collclass = 0;
1568 charclass = get_char_class(&regparse);
1569 if (charclass == CLASS_NONE)
1570 {
1571 equiclass = get_equi_class(&regparse);
1572 if (equiclass == 0)
1573 collclass = get_coll_element(&regparse);
1574 }
1575
1576 /* Character class like [:alpha:] */
1577 if (charclass != CLASS_NONE)
1578 {
1579 switch (charclass)
1580 {
1581 case CLASS_ALNUM:
1582 EMIT(NFA_CLASS_ALNUM);
1583 break;
1584 case CLASS_ALPHA:
1585 EMIT(NFA_CLASS_ALPHA);
1586 break;
1587 case CLASS_BLANK:
1588 EMIT(NFA_CLASS_BLANK);
1589 break;
1590 case CLASS_CNTRL:
1591 EMIT(NFA_CLASS_CNTRL);
1592 break;
1593 case CLASS_DIGIT:
1594 EMIT(NFA_CLASS_DIGIT);
1595 break;
1596 case CLASS_GRAPH:
1597 EMIT(NFA_CLASS_GRAPH);
1598 break;
1599 case CLASS_LOWER:
1600 EMIT(NFA_CLASS_LOWER);
1601 break;
1602 case CLASS_PRINT:
1603 EMIT(NFA_CLASS_PRINT);
1604 break;
1605 case CLASS_PUNCT:
1606 EMIT(NFA_CLASS_PUNCT);
1607 break;
1608 case CLASS_SPACE:
1609 EMIT(NFA_CLASS_SPACE);
1610 break;
1611 case CLASS_UPPER:
1612 EMIT(NFA_CLASS_UPPER);
1613 break;
1614 case CLASS_XDIGIT:
1615 EMIT(NFA_CLASS_XDIGIT);
1616 break;
1617 case CLASS_TAB:
1618 EMIT(NFA_CLASS_TAB);
1619 break;
1620 case CLASS_RETURN:
1621 EMIT(NFA_CLASS_RETURN);
1622 break;
1623 case CLASS_BACKSPACE:
1624 EMIT(NFA_CLASS_BACKSPACE);
1625 break;
1626 case CLASS_ESCAPE:
1627 EMIT(NFA_CLASS_ESCAPE);
1628 break;
1629 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001630 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001631 continue;
1632 }
1633 /* Try equivalence class [=a=] and the like */
1634 if (equiclass != 0)
1635 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001636 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 if (result == FAIL)
1638 {
1639 /* should never happen */
1640 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1641 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 continue;
1643 }
1644 /* Try collating class like [. .] */
1645 if (collclass != 0)
1646 {
1647 startc = collclass; /* allow [.a.]-x as a range */
1648 /* Will emit the proper atom at the end of the
1649 * while loop. */
1650 }
1651 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001652 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1653 * start character. */
1654 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001655 {
1656 emit_range = TRUE;
1657 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001658 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 continue; /* reading the end of the range */
1660 }
1661
1662 /* Now handle simple and escaped characters.
1663 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1664 * accepts "\t", "\e", etc., but only when the 'l' flag in
1665 * 'cpoptions' is not included.
1666 * Posix doesn't recognize backslash at all.
1667 */
1668 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001669 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001670 && regparse + 1 <= endp
1671 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001672 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001673 && vim_strchr(REGEXP_ABBR, regparse[1])
1674 != NULL)
1675 )
1676 )
1677 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001678 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001679
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001680 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 startc = reg_string ? NL : NFA_NEWL;
1682 else
1683 if (*regparse == 'd'
1684 || *regparse == 'o'
1685 || *regparse == 'x'
1686 || *regparse == 'u'
1687 || *regparse == 'U'
1688 )
1689 {
1690 /* TODO(RE) This needs more testing */
1691 startc = coll_get_char();
1692 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001693 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694 }
1695 else
1696 {
1697 /* \r,\t,\e,\b */
1698 startc = backslash_trans(*regparse);
1699 }
1700 }
1701
1702 /* Normal printable char */
1703 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001704 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001705
1706 /* Previous char was '-', so this char is end of range. */
1707 if (emit_range)
1708 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001709 endc = startc;
1710 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001711 if (startc > endc)
1712 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001713
1714 if (endc > startc + 2)
1715 {
1716 /* Emit a range instead of the sequence of
1717 * individual characters. */
1718 if (startc == 0)
1719 /* \x00 is translated to \x0a, start at \x01. */
1720 EMIT(1);
1721 else
1722 --post_ptr; /* remove NFA_CONCAT */
1723 EMIT(endc);
1724 EMIT(NFA_RANGE);
1725 EMIT(NFA_CONCAT);
1726 }
1727 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001728#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001729 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001730 || (*mb_char2len)(endc) > 1))
1731 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001732 /* Emit the characters in the range.
1733 * "startc" was already emitted, so skip it.
1734 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735 for (c = startc + 1; c <= endc; c++)
1736 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001737 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001738 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740 }
1741 else
1742#endif
1743 {
1744#ifdef EBCDIC
1745 int alpha_only = FALSE;
1746
1747 /* for alphabetical range skip the gaps
1748 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1749 if (isalpha(startc) && isalpha(endc))
1750 alpha_only = TRUE;
1751#endif
1752 /* Emit the range. "startc" was already emitted, so
1753 * skip it. */
1754 for (c = startc + 1; c <= endc; c++)
1755#ifdef EBCDIC
1756 if (!alpha_only || isalpha(startc))
1757#endif
1758 {
1759 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001760 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001761 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001762 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001763 emit_range = FALSE;
1764 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765 }
1766 else
1767 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001768 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001770 * Normally, simply emit startc. But if we get char
1771 * code=0 from a collating char, then replace it with
1772 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001774 * the backtracking engine. */
1775 if (startc == NFA_NEWL)
1776 {
1777 /* Line break can't be matched as part of the
1778 * collection, add an OR below. But not for negated
1779 * range. */
1780 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001781 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001782 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001784 {
1785 if (got_coll_char == TRUE && startc == 0)
1786 EMIT(0x0a);
1787 else
1788 EMIT(startc);
1789 EMIT(NFA_CONCAT);
1790 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001791 }
1792
Bram Moolenaar51a29832013-05-28 22:30:35 +02001793 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794 } /* while (p < endp) */
1795
Bram Moolenaar51a29832013-05-28 22:30:35 +02001796 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 if (*regparse == '-') /* if last, '-' is just a char */
1798 {
1799 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001800 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001802
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001803 /* skip the trailing ] */
1804 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001805 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001806
1807 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001808 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001809 EMIT(NFA_END_NEG_COLL);
1810 else
1811 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001812
1813 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001814 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001815 {
1816 EMIT(reg_string ? NL : NFA_NEWL);
1817 EMIT(NFA_OR);
1818 }
1819
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001821 } /* if exists closing ] */
1822
1823 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001824 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001825 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001826
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001827 default:
1828 {
1829#ifdef FEAT_MBYTE
1830 int plen;
1831
1832nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001833 /* plen is length of current char with composing chars */
1834 if (enc_utf8 && ((*mb_char2len)(c)
1835 != (plen = (*mb_ptr2len)(old_regparse))
1836 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001837 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001838 int i = 0;
1839
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001840 /* A base character plus composing characters, or just one
1841 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001842 * This requires creating a separate atom as if enclosing
1843 * the characters in (), where NFA_COMPOSING is the ( and
1844 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001845 * building the postfix form, not the NFA itself;
1846 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001847 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001848 for (;;)
1849 {
1850 EMIT(c);
1851 if (i > 0)
1852 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001853 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001854 break;
1855 c = utf_ptr2char(old_regparse + i);
1856 }
1857 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858 regparse = old_regparse + plen;
1859 }
1860 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001861#endif
1862 {
1863 c = no_Magic(c);
1864 EMIT(c);
1865 }
1866 return OK;
1867 }
1868 }
1869
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870 return OK;
1871}
1872
1873/*
1874 * Parse something followed by possible [*+=].
1875 *
1876 * A piece is an atom, possibly followed by a multi, an indication of how many
1877 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1878 * characters: "", "a", "aa", etc.
1879 *
1880 * piece ::= atom
1881 * or atom multi
1882 */
1883 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001884nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885{
1886 int i;
1887 int op;
1888 int ret;
1889 long minval, maxval;
1890 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001891 parse_state_T old_state;
1892 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001893 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001894 int old_post_pos;
1895 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 int quest;
1897
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001898 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1899 * next. */
1900 save_parse_state(&old_state);
1901
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001903 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904
1905 ret = nfa_regatom();
1906 if (ret == FAIL)
1907 return FAIL; /* cascaded error */
1908
1909 op = peekchr();
1910 if (re_multi_type(op) == NOT_MULTI)
1911 return OK;
1912
1913 skipchr();
1914 switch (op)
1915 {
1916 case Magic('*'):
1917 EMIT(NFA_STAR);
1918 break;
1919
1920 case Magic('+'):
1921 /*
1922 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1923 * first and only submatch would be "aaa". But the backtracking
1924 * engine interprets the plus as "try matching one more time", and
1925 * a* matches a second time at the end of the input, the empty
1926 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001927 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001929 * In order to be consistent with the old engine, we replace
1930 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001932 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 curchr = -1;
1934 if (nfa_regatom() == FAIL)
1935 return FAIL;
1936 EMIT(NFA_STAR);
1937 EMIT(NFA_CONCAT);
1938 skipchr(); /* skip the \+ */
1939 break;
1940
1941 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001942 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001944 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 switch(op)
1946 {
1947 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001948 /* \@= */
1949 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001951 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001952 /* \@! */
1953 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001954 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001955 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001956 op = no_Magic(getchr());
1957 if (op == '=')
1958 /* \@<= */
1959 i = NFA_PREV_ATOM_JUST_BEFORE;
1960 else if (op == '!')
1961 /* \@<! */
1962 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1963 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001965 /* \@> */
1966 i = NFA_PREV_ATOM_LIKE_PATTERN;
1967 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001969 if (i == 0)
1970 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001971 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1972 return FAIL;
1973 }
1974 EMIT(i);
1975 if (i == NFA_PREV_ATOM_JUST_BEFORE
1976 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1977 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001978 break;
1979
1980 case Magic('?'):
1981 case Magic('='):
1982 EMIT(NFA_QUEST);
1983 break;
1984
1985 case Magic('{'):
1986 /* a{2,5} will expand to 'aaa?a?a?'
1987 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1988 * version of '?'
1989 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1990 * parenthesis have the same id
1991 */
1992
1993 greedy = TRUE;
1994 c2 = peekchr();
1995 if (c2 == '-' || c2 == Magic('-'))
1996 {
1997 skipchr();
1998 greedy = FALSE;
1999 }
2000 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002001 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002002
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2004 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002005 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002006 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002007 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002008 /* \{}, \{0,} */
2009 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002010 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002011 /* \{-}, \{-0,} */
2012 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002013 break;
2014 }
2015
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002016 /* Special case: x{0} or x{-0} */
2017 if (maxval == 0)
2018 {
2019 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002020 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002021 /* NFA_EMPTY is 0-length and works everywhere */
2022 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 return OK;
2024 }
2025
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002026 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002027 * maximum is much larger than the minimum and when the maximum is
2028 * large. Bail out if we can use the other engine. */
2029 if ((nfa_re_flags & RE_AUTO)
2030 && (maxval > minval + 200 || maxval > 500))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002031 return FAIL;
2032
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002034 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002035 /* Save parse state after the repeated atom and the \{} */
2036 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2039 for (i = 0; i < maxval; i++)
2040 {
2041 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002042 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002043 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 if (nfa_regatom() == FAIL)
2045 return FAIL;
2046 /* after "minval" times, atoms are optional */
2047 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002048 {
2049 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002050 {
2051 if (greedy)
2052 EMIT(NFA_STAR);
2053 else
2054 EMIT(NFA_STAR_NONGREEDY);
2055 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002056 else
2057 EMIT(quest);
2058 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002059 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002060 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002061 if (i + 1 > minval && maxval == MAX_LIMIT)
2062 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063 }
2064
2065 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002066 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 curchr = -1;
2068
2069 break;
2070
2071
2072 default:
2073 break;
2074 } /* end switch */
2075
2076 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002078 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002079
2080 return OK;
2081}
2082
2083/*
2084 * Parse one or more pieces, concatenated. It matches a match for the
2085 * first piece, followed by a match for the second piece, etc. Example:
2086 * "f[0-9]b", first matches "f", then a digit and then "b".
2087 *
2088 * concat ::= piece
2089 * or piece piece
2090 * or piece piece piece
2091 * etc.
2092 */
2093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002094nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002095{
2096 int cont = TRUE;
2097 int first = TRUE;
2098
2099 while (cont)
2100 {
2101 switch (peekchr())
2102 {
2103 case NUL:
2104 case Magic('|'):
2105 case Magic('&'):
2106 case Magic(')'):
2107 cont = FALSE;
2108 break;
2109
2110 case Magic('Z'):
2111#ifdef FEAT_MBYTE
2112 regflags |= RF_ICOMBINE;
2113#endif
2114 skipchr_keepstart();
2115 break;
2116 case Magic('c'):
2117 regflags |= RF_ICASE;
2118 skipchr_keepstart();
2119 break;
2120 case Magic('C'):
2121 regflags |= RF_NOICASE;
2122 skipchr_keepstart();
2123 break;
2124 case Magic('v'):
2125 reg_magic = MAGIC_ALL;
2126 skipchr_keepstart();
2127 curchr = -1;
2128 break;
2129 case Magic('m'):
2130 reg_magic = MAGIC_ON;
2131 skipchr_keepstart();
2132 curchr = -1;
2133 break;
2134 case Magic('M'):
2135 reg_magic = MAGIC_OFF;
2136 skipchr_keepstart();
2137 curchr = -1;
2138 break;
2139 case Magic('V'):
2140 reg_magic = MAGIC_NONE;
2141 skipchr_keepstart();
2142 curchr = -1;
2143 break;
2144
2145 default:
2146 if (nfa_regpiece() == FAIL)
2147 return FAIL;
2148 if (first == FALSE)
2149 EMIT(NFA_CONCAT);
2150 else
2151 first = FALSE;
2152 break;
2153 }
2154 }
2155
2156 return OK;
2157}
2158
2159/*
2160 * Parse a branch, one or more concats, separated by "\&". It matches the
2161 * last concat, but only if all the preceding concats also match at the same
2162 * position. Examples:
2163 * "foobeep\&..." matches "foo" in "foobeep".
2164 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2165 *
2166 * branch ::= concat
2167 * or concat \& concat
2168 * or concat \& concat \& concat
2169 * etc.
2170 */
2171 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002172nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173{
2174 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002175 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002176
Bram Moolenaar16299b52013-05-30 18:45:23 +02002177 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002178
2179 /* First branch, possibly the only one */
2180 if (nfa_regconcat() == FAIL)
2181 return FAIL;
2182
2183 ch = peekchr();
2184 /* Try next concats */
2185 while (ch == Magic('&'))
2186 {
2187 skipchr();
2188 EMIT(NFA_NOPEN);
2189 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002190 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002191 if (nfa_regconcat() == FAIL)
2192 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002193 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002194 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002195 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002196 EMIT(NFA_CONCAT);
2197 ch = peekchr();
2198 }
2199
Bram Moolenaar699c1202013-09-25 16:41:54 +02002200 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002201 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002202 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002203
2204 return OK;
2205}
2206
2207/*
2208 * Parse a pattern, one or more branches, separated by "\|". It matches
2209 * anything that matches one of the branches. Example: "foo\|beep" matches
2210 * "foo" and matches "beep". If more than one branch matches, the first one
2211 * is used.
2212 *
2213 * pattern ::= branch
2214 * or branch \| branch
2215 * or branch \| branch \| branch
2216 * etc.
2217 */
2218 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002219nfa_reg(
2220 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221{
2222 int parno = 0;
2223
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 if (paren == REG_PAREN)
2225 {
2226 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002227 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228 parno = regnpar++;
2229 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002230#ifdef FEAT_SYN_HL
2231 else if (paren == REG_ZPAREN)
2232 {
2233 /* Make a ZOPEN node. */
2234 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002235 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002236 parno = regnzpar++;
2237 }
2238#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002239
2240 if (nfa_regbranch() == FAIL)
2241 return FAIL; /* cascaded error */
2242
2243 while (peekchr() == Magic('|'))
2244 {
2245 skipchr();
2246 if (nfa_regbranch() == FAIL)
2247 return FAIL; /* cascaded error */
2248 EMIT(NFA_OR);
2249 }
2250
2251 /* Check for proper termination. */
2252 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2253 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002254 if (paren == REG_NPAREN)
2255 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2256 else
2257 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2258 }
2259 else if (paren == REG_NOPAREN && peekchr() != NUL)
2260 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261 if (peekchr() == Magic(')'))
2262 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2263 else
2264 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2265 }
2266 /*
2267 * Here we set the flag allowing back references to this set of
2268 * parentheses.
2269 */
2270 if (paren == REG_PAREN)
2271 {
2272 had_endbrace[parno] = TRUE; /* have seen the close paren */
2273 EMIT(NFA_MOPEN + parno);
2274 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002275#ifdef FEAT_SYN_HL
2276 else if (paren == REG_ZPAREN)
2277 EMIT(NFA_ZOPEN + parno);
2278#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279
2280 return OK;
2281}
2282
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283#ifdef DEBUG
2284static char_u code[50];
2285
2286 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002287nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288{
2289 int addnl = FALSE;
2290
2291 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2292 {
2293 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002294 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 }
2296
2297 STRCPY(code, "");
2298 switch (c)
2299 {
2300 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2301 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2302 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2303 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2304 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2305 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2306
Bram Moolenaar5714b802013-05-28 22:03:20 +02002307 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2308 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2309 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2310 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2311 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2312 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2313 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2314 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2315 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002316#ifdef FEAT_SYN_HL
2317 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2318 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2319 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2320 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2321 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2322 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2323 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2324 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2325 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2326#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002327 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2328
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329 case NFA_PREV_ATOM_NO_WIDTH:
2330 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002331 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2332 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002333 case NFA_PREV_ATOM_JUST_BEFORE:
2334 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2335 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2336 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002337 case NFA_PREV_ATOM_LIKE_PATTERN:
2338 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2339
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002340 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2341 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002342 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002343 case NFA_START_INVISIBLE_FIRST:
2344 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002345 case NFA_START_INVISIBLE_NEG:
2346 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002347 case NFA_START_INVISIBLE_NEG_FIRST:
2348 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002349 case NFA_START_INVISIBLE_BEFORE:
2350 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002351 case NFA_START_INVISIBLE_BEFORE_FIRST:
2352 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002353 case NFA_START_INVISIBLE_BEFORE_NEG:
2354 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002355 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2356 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002357 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002359 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002360 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002361
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2363 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002364 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002366 case NFA_MOPEN:
2367 case NFA_MOPEN1:
2368 case NFA_MOPEN2:
2369 case NFA_MOPEN3:
2370 case NFA_MOPEN4:
2371 case NFA_MOPEN5:
2372 case NFA_MOPEN6:
2373 case NFA_MOPEN7:
2374 case NFA_MOPEN8:
2375 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002376 STRCPY(code, "NFA_MOPEN(x)");
2377 code[10] = c - NFA_MOPEN + '0';
2378 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002379 case NFA_MCLOSE:
2380 case NFA_MCLOSE1:
2381 case NFA_MCLOSE2:
2382 case NFA_MCLOSE3:
2383 case NFA_MCLOSE4:
2384 case NFA_MCLOSE5:
2385 case NFA_MCLOSE6:
2386 case NFA_MCLOSE7:
2387 case NFA_MCLOSE8:
2388 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389 STRCPY(code, "NFA_MCLOSE(x)");
2390 code[11] = c - NFA_MCLOSE + '0';
2391 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002392#ifdef FEAT_SYN_HL
2393 case NFA_ZOPEN:
2394 case NFA_ZOPEN1:
2395 case NFA_ZOPEN2:
2396 case NFA_ZOPEN3:
2397 case NFA_ZOPEN4:
2398 case NFA_ZOPEN5:
2399 case NFA_ZOPEN6:
2400 case NFA_ZOPEN7:
2401 case NFA_ZOPEN8:
2402 case NFA_ZOPEN9:
2403 STRCPY(code, "NFA_ZOPEN(x)");
2404 code[10] = c - NFA_ZOPEN + '0';
2405 break;
2406 case NFA_ZCLOSE:
2407 case NFA_ZCLOSE1:
2408 case NFA_ZCLOSE2:
2409 case NFA_ZCLOSE3:
2410 case NFA_ZCLOSE4:
2411 case NFA_ZCLOSE5:
2412 case NFA_ZCLOSE6:
2413 case NFA_ZCLOSE7:
2414 case NFA_ZCLOSE8:
2415 case NFA_ZCLOSE9:
2416 STRCPY(code, "NFA_ZCLOSE(x)");
2417 code[11] = c - NFA_ZCLOSE + '0';
2418 break;
2419#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002420 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2421 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2422 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2423 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002424 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2425 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002426 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2427 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2428 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2429 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2430 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2431 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2432 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2433 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2434 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2435 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2436 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2437 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2438 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2439 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002440 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002441
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002443 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2444 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2445 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002446 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002448
2449 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2450 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2451 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2452 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2453 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2454 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2455 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2456
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002457 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2458 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2459 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2460 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2461 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2462 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2463 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2464 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2465 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2466 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2467 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2468 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2469 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2470 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2471 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2472 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2473
2474 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2475 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2476 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2477 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2478 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2479 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2480 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2481 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2482 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2483 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2484 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2485 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2486 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2487 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2488 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2489 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2490 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2491 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2492 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2493 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2494 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2495 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2496 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2497 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2498 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2499 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2500 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002501 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2502 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2503 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2504 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505
2506 default:
2507 STRCPY(code, "CHAR(x)");
2508 code[5] = c;
2509 }
2510
2511 if (addnl == TRUE)
2512 STRCAT(code, " + NEWLINE ");
2513
2514}
2515
2516#ifdef ENABLE_LOG
2517static FILE *log_fd;
2518
2519/*
2520 * Print the postfix notation of the current regexp.
2521 */
2522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002523nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002524{
2525 int *p;
2526 FILE *f;
2527
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002528 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002529 if (f != NULL)
2530 {
2531 fprintf(f, "\n-------------------------\n");
2532 if (retval == FAIL)
2533 fprintf(f, ">>> NFA engine failed ... \n");
2534 else if (retval == OK)
2535 fprintf(f, ">>> NFA engine succeeded !\n");
2536 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002537 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538 {
2539 nfa_set_code(*p);
2540 fprintf(f, "%s, ", code);
2541 }
2542 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002543 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544 fprintf(f, "%d ", *p);
2545 fprintf(f, "\n\n");
2546 fclose(f);
2547 }
2548}
2549
2550/*
2551 * Print the NFA starting with a root node "state".
2552 */
2553 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002554nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002555{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002556 garray_T indent;
2557
2558 ga_init2(&indent, 1, 64);
2559 ga_append(&indent, '\0');
2560 nfa_print_state2(debugf, state, &indent);
2561 ga_clear(&indent);
2562}
2563
2564 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002565nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002566{
2567 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002568
2569 if (state == NULL)
2570 return;
2571
2572 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002573
2574 /* Output indent */
2575 p = (char_u *)indent->ga_data;
2576 if (indent->ga_len >= 3)
2577 {
2578 int last = indent->ga_len - 3;
2579 char_u save[2];
2580
2581 STRNCPY(save, &p[last], 2);
2582 STRNCPY(&p[last], "+-", 2);
2583 fprintf(debugf, " %s", p);
2584 STRNCPY(&p[last], save, 2);
2585 }
2586 else
2587 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002588
2589 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002590 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002591 code,
2592 state->c,
2593 abs(state->id),
2594 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002595 if (state->id < 0)
2596 return;
2597
2598 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002599
2600 /* grow indent for state->out */
2601 indent->ga_len -= 1;
2602 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002603 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002604 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002605 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002606 ga_append(indent, '\0');
2607
2608 nfa_print_state2(debugf, state->out, indent);
2609
2610 /* replace last part of indent for state->out1 */
2611 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002612 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002613 ga_append(indent, '\0');
2614
2615 nfa_print_state2(debugf, state->out1, indent);
2616
2617 /* shrink indent */
2618 indent->ga_len -= 3;
2619 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002620}
2621
2622/*
2623 * Print the NFA state machine.
2624 */
2625 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002626nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002627{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002628 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002629
2630 if (debugf != NULL)
2631 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002632 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002633
Bram Moolenaar473de612013-06-08 18:19:48 +02002634 if (prog->reganch)
2635 fprintf(debugf, "reganch: %d\n", prog->reganch);
2636 if (prog->regstart != NUL)
2637 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2638 prog->regstart, prog->regstart);
2639 if (prog->match_text != NULL)
2640 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002641
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002642 fclose(debugf);
2643 }
2644}
2645#endif /* ENABLE_LOG */
2646#endif /* DEBUG */
2647
2648/*
2649 * Parse r.e. @expr and convert it into postfix form.
2650 * Return the postfix string on success, NULL otherwise.
2651 */
2652 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002653re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654{
2655 if (nfa_reg(REG_NOPAREN) == FAIL)
2656 return NULL;
2657 EMIT(NFA_MOPEN);
2658 return post_start;
2659}
2660
2661/* NB. Some of the code below is inspired by Russ's. */
2662
2663/*
2664 * Represents an NFA state plus zero or one or two arrows exiting.
2665 * if c == MATCH, no arrows out; matching state.
2666 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2667 * If c < 256, labeled arrow with character c to out.
2668 */
2669
2670static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2671
2672/*
2673 * Allocate and initialize nfa_state_T.
2674 */
2675 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002676alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002677{
2678 nfa_state_T *s;
2679
2680 if (istate >= nstate)
2681 return NULL;
2682
2683 s = &state_ptr[istate++];
2684
2685 s->c = c;
2686 s->out = out;
2687 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002688 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002689
2690 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002691 s->lastlist[0] = 0;
2692 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002693
2694 return s;
2695}
2696
2697/*
2698 * A partially built NFA without the matching state filled in.
2699 * Frag_T.start points at the start state.
2700 * Frag_T.out is a list of places that need to be set to the
2701 * next state for this fragment.
2702 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002703
2704/* Since the out pointers in the list are always
2705 * uninitialized, we use the pointers themselves
2706 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002707typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002708union Ptrlist
2709{
2710 Ptrlist *next;
2711 nfa_state_T *s;
2712};
2713
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714struct Frag
2715{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002716 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002717 Ptrlist *out;
2718};
2719typedef struct Frag Frag_T;
2720
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002721static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2722static Ptrlist *list1(nfa_state_T **outp);
2723static void patch(Ptrlist *l, nfa_state_T *s);
2724static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2725static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2726static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002727
2728/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002729 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730 */
2731 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002732frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002733{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002734 Frag_T n;
2735
2736 n.start = start;
2737 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 return n;
2739}
2740
2741/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 * Create singleton list containing just outp.
2743 */
2744 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002745list1(
2746 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002747{
2748 Ptrlist *l;
2749
2750 l = (Ptrlist *)outp;
2751 l->next = NULL;
2752 return l;
2753}
2754
2755/*
2756 * Patch the list of states at out to point to start.
2757 */
2758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002759patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002760{
2761 Ptrlist *next;
2762
2763 for (; l; l = next)
2764 {
2765 next = l->next;
2766 l->s = s;
2767 }
2768}
2769
2770
2771/*
2772 * Join the two lists l1 and l2, returning the combination.
2773 */
2774 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002775append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776{
2777 Ptrlist *oldl1;
2778
2779 oldl1 = l1;
2780 while (l1->next)
2781 l1 = l1->next;
2782 l1->next = l2;
2783 return oldl1;
2784}
2785
2786/*
2787 * Stack used for transforming postfix form into NFA.
2788 */
2789static Frag_T empty;
2790
2791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002792st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002793{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002794#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002795 FILE *df;
2796 int *p2;
2797
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002798 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002799 if (df)
2800 {
2801 fprintf(df, "Error popping the stack!\n");
2802#ifdef DEBUG
2803 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2804#endif
2805 fprintf(df, "Postfix form is: ");
2806#ifdef DEBUG
2807 for (p2 = postfix; p2 < end; p2++)
2808 {
2809 nfa_set_code(*p2);
2810 fprintf(df, "%s, ", code);
2811 }
2812 nfa_set_code(*p);
2813 fprintf(df, "\nCurrent position is: ");
2814 for (p2 = postfix; p2 <= p; p2 ++)
2815 {
2816 nfa_set_code(*p2);
2817 fprintf(df, "%s, ", code);
2818 }
2819#else
2820 for (p2 = postfix; p2 < end; p2++)
2821 {
2822 fprintf(df, "%d, ", *p2);
2823 }
2824 fprintf(df, "\nCurrent position is: ");
2825 for (p2 = postfix; p2 <= p; p2 ++)
2826 {
2827 fprintf(df, "%d, ", *p2);
2828 }
2829#endif
2830 fprintf(df, "\n--------------------------\n");
2831 fclose(df);
2832 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002833#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834 EMSG(_("E874: (NFA) Could not pop the stack !"));
2835}
2836
2837/*
2838 * Push an item onto the stack.
2839 */
2840 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002841st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842{
2843 Frag_T *stackp = *p;
2844
2845 if (stackp >= stack_end)
2846 return;
2847 *stackp = s;
2848 *p = *p + 1;
2849}
2850
2851/*
2852 * Pop an item from the stack.
2853 */
2854 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002855st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002856{
2857 Frag_T *stackp;
2858
2859 *p = *p - 1;
2860 stackp = *p;
2861 if (stackp < stack)
2862 return empty;
2863 return **p;
2864}
2865
2866/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002867 * Estimate the maximum byte length of anything matching "state".
2868 * When unknown or unlimited return -1.
2869 */
2870 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002871nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002872{
2873 int l, r;
2874 nfa_state_T *state = startstate;
2875 int len = 0;
2876
2877 /* detect looping in a NFA_SPLIT */
2878 if (depth > 4)
2879 return -1;
2880
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002881 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002882 {
2883 switch (state->c)
2884 {
2885 case NFA_END_INVISIBLE:
2886 case NFA_END_INVISIBLE_NEG:
2887 /* the end, return what we have */
2888 return len;
2889
2890 case NFA_SPLIT:
2891 /* two alternatives, use the maximum */
2892 l = nfa_max_width(state->out, depth + 1);
2893 r = nfa_max_width(state->out1, depth + 1);
2894 if (l < 0 || r < 0)
2895 return -1;
2896 return len + (l > r ? l : r);
2897
2898 case NFA_ANY:
2899 case NFA_START_COLL:
2900 case NFA_START_NEG_COLL:
2901 /* matches some character, including composing chars */
2902#ifdef FEAT_MBYTE
2903 if (enc_utf8)
2904 len += MB_MAXBYTES;
2905 else if (has_mbyte)
2906 len += 2;
2907 else
2908#endif
2909 ++len;
2910 if (state->c != NFA_ANY)
2911 {
2912 /* skip over the characters */
2913 state = state->out1->out;
2914 continue;
2915 }
2916 break;
2917
2918 case NFA_DIGIT:
2919 case NFA_WHITE:
2920 case NFA_HEX:
2921 case NFA_OCTAL:
2922 /* ascii */
2923 ++len;
2924 break;
2925
2926 case NFA_IDENT:
2927 case NFA_SIDENT:
2928 case NFA_KWORD:
2929 case NFA_SKWORD:
2930 case NFA_FNAME:
2931 case NFA_SFNAME:
2932 case NFA_PRINT:
2933 case NFA_SPRINT:
2934 case NFA_NWHITE:
2935 case NFA_NDIGIT:
2936 case NFA_NHEX:
2937 case NFA_NOCTAL:
2938 case NFA_WORD:
2939 case NFA_NWORD:
2940 case NFA_HEAD:
2941 case NFA_NHEAD:
2942 case NFA_ALPHA:
2943 case NFA_NALPHA:
2944 case NFA_LOWER:
2945 case NFA_NLOWER:
2946 case NFA_UPPER:
2947 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002948 case NFA_LOWER_IC:
2949 case NFA_NLOWER_IC:
2950 case NFA_UPPER_IC:
2951 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002952 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002953 /* possibly non-ascii */
2954#ifdef FEAT_MBYTE
2955 if (has_mbyte)
2956 len += 3;
2957 else
2958#endif
2959 ++len;
2960 break;
2961
2962 case NFA_START_INVISIBLE:
2963 case NFA_START_INVISIBLE_NEG:
2964 case NFA_START_INVISIBLE_BEFORE:
2965 case NFA_START_INVISIBLE_BEFORE_NEG:
2966 /* zero-width, out1 points to the END state */
2967 state = state->out1->out;
2968 continue;
2969
2970 case NFA_BACKREF1:
2971 case NFA_BACKREF2:
2972 case NFA_BACKREF3:
2973 case NFA_BACKREF4:
2974 case NFA_BACKREF5:
2975 case NFA_BACKREF6:
2976 case NFA_BACKREF7:
2977 case NFA_BACKREF8:
2978 case NFA_BACKREF9:
2979#ifdef FEAT_SYN_HL
2980 case NFA_ZREF1:
2981 case NFA_ZREF2:
2982 case NFA_ZREF3:
2983 case NFA_ZREF4:
2984 case NFA_ZREF5:
2985 case NFA_ZREF6:
2986 case NFA_ZREF7:
2987 case NFA_ZREF8:
2988 case NFA_ZREF9:
2989#endif
2990 case NFA_NEWL:
2991 case NFA_SKIP:
2992 /* unknown width */
2993 return -1;
2994
2995 case NFA_BOL:
2996 case NFA_EOL:
2997 case NFA_BOF:
2998 case NFA_EOF:
2999 case NFA_BOW:
3000 case NFA_EOW:
3001 case NFA_MOPEN:
3002 case NFA_MOPEN1:
3003 case NFA_MOPEN2:
3004 case NFA_MOPEN3:
3005 case NFA_MOPEN4:
3006 case NFA_MOPEN5:
3007 case NFA_MOPEN6:
3008 case NFA_MOPEN7:
3009 case NFA_MOPEN8:
3010 case NFA_MOPEN9:
3011#ifdef FEAT_SYN_HL
3012 case NFA_ZOPEN:
3013 case NFA_ZOPEN1:
3014 case NFA_ZOPEN2:
3015 case NFA_ZOPEN3:
3016 case NFA_ZOPEN4:
3017 case NFA_ZOPEN5:
3018 case NFA_ZOPEN6:
3019 case NFA_ZOPEN7:
3020 case NFA_ZOPEN8:
3021 case NFA_ZOPEN9:
3022 case NFA_ZCLOSE:
3023 case NFA_ZCLOSE1:
3024 case NFA_ZCLOSE2:
3025 case NFA_ZCLOSE3:
3026 case NFA_ZCLOSE4:
3027 case NFA_ZCLOSE5:
3028 case NFA_ZCLOSE6:
3029 case NFA_ZCLOSE7:
3030 case NFA_ZCLOSE8:
3031 case NFA_ZCLOSE9:
3032#endif
3033 case NFA_MCLOSE:
3034 case NFA_MCLOSE1:
3035 case NFA_MCLOSE2:
3036 case NFA_MCLOSE3:
3037 case NFA_MCLOSE4:
3038 case NFA_MCLOSE5:
3039 case NFA_MCLOSE6:
3040 case NFA_MCLOSE7:
3041 case NFA_MCLOSE8:
3042 case NFA_MCLOSE9:
3043 case NFA_NOPEN:
3044 case NFA_NCLOSE:
3045
3046 case NFA_LNUM_GT:
3047 case NFA_LNUM_LT:
3048 case NFA_COL_GT:
3049 case NFA_COL_LT:
3050 case NFA_VCOL_GT:
3051 case NFA_VCOL_LT:
3052 case NFA_MARK_GT:
3053 case NFA_MARK_LT:
3054 case NFA_VISUAL:
3055 case NFA_LNUM:
3056 case NFA_CURSOR:
3057 case NFA_COL:
3058 case NFA_VCOL:
3059 case NFA_MARK:
3060
3061 case NFA_ZSTART:
3062 case NFA_ZEND:
3063 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003064 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003065 case NFA_START_PATTERN:
3066 case NFA_END_PATTERN:
3067 case NFA_COMPOSING:
3068 case NFA_END_COMPOSING:
3069 /* zero-width */
3070 break;
3071
3072 default:
3073 if (state->c < 0)
3074 /* don't know what this is */
3075 return -1;
3076 /* normal character */
3077 len += MB_CHAR2LEN(state->c);
3078 break;
3079 }
3080
3081 /* normal way to continue */
3082 state = state->out;
3083 }
3084
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003085 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003086 return -1;
3087}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003088
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003089/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003090 * Convert a postfix form into its equivalent NFA.
3091 * Return the NFA start state on success, NULL otherwise.
3092 */
3093 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003094post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003095{
3096 int *p;
3097 int mopen;
3098 int mclose;
3099 Frag_T *stack = NULL;
3100 Frag_T *stackp = NULL;
3101 Frag_T *stack_end = NULL;
3102 Frag_T e1;
3103 Frag_T e2;
3104 Frag_T e;
3105 nfa_state_T *s;
3106 nfa_state_T *s1;
3107 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003108 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003109
3110 if (postfix == NULL)
3111 return NULL;
3112
Bram Moolenaar053bb602013-05-20 13:55:21 +02003113#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114#define POP() st_pop(&stackp, stack); \
3115 if (stackp < stack) \
3116 { \
3117 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003118 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003119 return NULL; \
3120 }
3121
3122 if (nfa_calc_size == FALSE)
3123 {
3124 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003125 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003126 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003127 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003128 }
3129
3130 for (p = postfix; p < end; ++p)
3131 {
3132 switch (*p)
3133 {
3134 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003135 /* Concatenation.
3136 * Pay attention: this operator does not exist in the r.e. itself
3137 * (it is implicit, really). It is added when r.e. is translated
3138 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003139 if (nfa_calc_size == TRUE)
3140 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003141 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003142 break;
3143 }
3144 e2 = POP();
3145 e1 = POP();
3146 patch(e1.out, e2.start);
3147 PUSH(frag(e1.start, e2.out));
3148 break;
3149
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003150 case NFA_OR:
3151 /* Alternation */
3152 if (nfa_calc_size == TRUE)
3153 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003154 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003155 break;
3156 }
3157 e2 = POP();
3158 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003159 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003160 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003161 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003162 PUSH(frag(s, append(e1.out, e2.out)));
3163 break;
3164
3165 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003166 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003167 if (nfa_calc_size == TRUE)
3168 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003169 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003170 break;
3171 }
3172 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003173 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003174 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003175 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003176 patch(e.out, s);
3177 PUSH(frag(s, list1(&s->out1)));
3178 break;
3179
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003180 case NFA_STAR_NONGREEDY:
3181 /* Zero or more, prefer zero */
3182 if (nfa_calc_size == TRUE)
3183 {
3184 nstate++;
3185 break;
3186 }
3187 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003188 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003189 if (s == NULL)
3190 goto theend;
3191 patch(e.out, s);
3192 PUSH(frag(s, list1(&s->out)));
3193 break;
3194
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 case NFA_QUEST:
3196 /* one or zero atoms=> greedy match */
3197 if (nfa_calc_size == TRUE)
3198 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003199 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 break;
3201 }
3202 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003203 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003205 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206 PUSH(frag(s, append(e.out, list1(&s->out1))));
3207 break;
3208
3209 case NFA_QUEST_NONGREEDY:
3210 /* zero or one atoms => non-greedy match */
3211 if (nfa_calc_size == TRUE)
3212 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003213 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003214 break;
3215 }
3216 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003217 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003219 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 PUSH(frag(s, append(e.out, list1(&s->out))));
3221 break;
3222
Bram Moolenaar417bad22013-06-07 14:08:30 +02003223 case NFA_END_COLL:
3224 case NFA_END_NEG_COLL:
3225 /* On the stack is the sequence starting with NFA_START_COLL or
3226 * NFA_START_NEG_COLL and all possible characters. Patch it to
3227 * add the output to the start. */
3228 if (nfa_calc_size == TRUE)
3229 {
3230 nstate++;
3231 break;
3232 }
3233 e = POP();
3234 s = alloc_state(NFA_END_COLL, NULL, NULL);
3235 if (s == NULL)
3236 goto theend;
3237 patch(e.out, s);
3238 e.start->out1 = s;
3239 PUSH(frag(e.start, list1(&s->out)));
3240 break;
3241
3242 case NFA_RANGE:
3243 /* Before this are two characters, the low and high end of a
3244 * range. Turn them into two states with MIN and MAX. */
3245 if (nfa_calc_size == TRUE)
3246 {
3247 /* nstate += 0; */
3248 break;
3249 }
3250 e2 = POP();
3251 e1 = POP();
3252 e2.start->val = e2.start->c;
3253 e2.start->c = NFA_RANGE_MAX;
3254 e1.start->val = e1.start->c;
3255 e1.start->c = NFA_RANGE_MIN;
3256 patch(e1.out, e2.start);
3257 PUSH(frag(e1.start, e2.out));
3258 break;
3259
Bram Moolenaar699c1202013-09-25 16:41:54 +02003260 case NFA_EMPTY:
3261 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003262 if (nfa_calc_size == TRUE)
3263 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003264 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003265 break;
3266 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003267 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003269 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 PUSH(frag(s, list1(&s->out)));
3271 break;
3272
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003273 case NFA_OPT_CHARS:
3274 {
3275 int n;
3276
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003277 /* \%[abc] implemented as:
3278 * NFA_SPLIT
3279 * +-CHAR(a)
3280 * | +-NFA_SPLIT
3281 * | +-CHAR(b)
3282 * | | +-NFA_SPLIT
3283 * | | +-CHAR(c)
3284 * | | | +-next
3285 * | | +- next
3286 * | +- next
3287 * +- next
3288 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003289 n = *++p; /* get number of characters */
3290 if (nfa_calc_size == TRUE)
3291 {
3292 nstate += n;
3293 break;
3294 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003295 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003296 e1.out = NULL; /* stores list with out1's */
3297 s1 = NULL; /* previous NFA_SPLIT to connect to */
3298 while (n-- > 0)
3299 {
3300 e = POP(); /* get character */
3301 s = alloc_state(NFA_SPLIT, e.start, NULL);
3302 if (s == NULL)
3303 goto theend;
3304 if (e1.out == NULL)
3305 e1 = e;
3306 patch(e.out, s1);
3307 append(e1.out, list1(&s->out1));
3308 s1 = s;
3309 }
3310 PUSH(frag(s, e1.out));
3311 break;
3312 }
3313
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003315 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003316 case NFA_PREV_ATOM_JUST_BEFORE:
3317 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003318 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003319 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003320 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3321 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003322 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003323 int start_state;
3324 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003325 int n = 0;
3326 nfa_state_T *zend;
3327 nfa_state_T *skip;
3328
Bram Moolenaardecd9542013-06-07 16:31:50 +02003329 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003330 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003331 case NFA_PREV_ATOM_NO_WIDTH:
3332 start_state = NFA_START_INVISIBLE;
3333 end_state = NFA_END_INVISIBLE;
3334 break;
3335 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3336 start_state = NFA_START_INVISIBLE_NEG;
3337 end_state = NFA_END_INVISIBLE_NEG;
3338 break;
3339 case NFA_PREV_ATOM_JUST_BEFORE:
3340 start_state = NFA_START_INVISIBLE_BEFORE;
3341 end_state = NFA_END_INVISIBLE;
3342 break;
3343 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3344 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3345 end_state = NFA_END_INVISIBLE_NEG;
3346 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003347 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003348 start_state = NFA_START_PATTERN;
3349 end_state = NFA_END_PATTERN;
3350 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003351 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003352
3353 if (before)
3354 n = *++p; /* get the count */
3355
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003356 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003357 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003358 * The \@<= operator: match for the preceding atom.
3359 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003361 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362
3363 if (nfa_calc_size == TRUE)
3364 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003365 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003366 break;
3367 }
3368 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003369 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003371 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372
Bram Moolenaar87953742013-06-05 18:52:40 +02003373 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003374 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003375 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003376 if (pattern)
3377 {
3378 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3379 skip = alloc_state(NFA_SKIP, NULL, NULL);
3380 zend = alloc_state(NFA_ZEND, s1, NULL);
3381 s1->out= skip;
3382 patch(e.out, zend);
3383 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003384 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003385 else
3386 {
3387 patch(e.out, s1);
3388 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003389 if (before)
3390 {
3391 if (n <= 0)
3392 /* See if we can guess the maximum width, it avoids a
3393 * lot of pointless tries. */
3394 n = nfa_max_width(e.start, 0);
3395 s->val = n; /* store the count */
3396 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003397 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003398 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003399 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003401#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003402 case NFA_COMPOSING: /* char with composing char */
3403#if 0
3404 /* TODO */
3405 if (regflags & RF_ICOMBINE)
3406 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003407 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003408 }
3409#endif
3410 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003411#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003412
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003413 case NFA_MOPEN: /* \( \) Submatch */
3414 case NFA_MOPEN1:
3415 case NFA_MOPEN2:
3416 case NFA_MOPEN3:
3417 case NFA_MOPEN4:
3418 case NFA_MOPEN5:
3419 case NFA_MOPEN6:
3420 case NFA_MOPEN7:
3421 case NFA_MOPEN8:
3422 case NFA_MOPEN9:
3423#ifdef FEAT_SYN_HL
3424 case NFA_ZOPEN: /* \z( \) Submatch */
3425 case NFA_ZOPEN1:
3426 case NFA_ZOPEN2:
3427 case NFA_ZOPEN3:
3428 case NFA_ZOPEN4:
3429 case NFA_ZOPEN5:
3430 case NFA_ZOPEN6:
3431 case NFA_ZOPEN7:
3432 case NFA_ZOPEN8:
3433 case NFA_ZOPEN9:
3434#endif
3435 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003436 if (nfa_calc_size == TRUE)
3437 {
3438 nstate += 2;
3439 break;
3440 }
3441
3442 mopen = *p;
3443 switch (*p)
3444 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003445 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3446#ifdef FEAT_SYN_HL
3447 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3448 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3449 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3450 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3451 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3452 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3453 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3454 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3455 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3456 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3457#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003458#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003459 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003460#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003462 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003463 mclose = *p + NSUBEXP;
3464 break;
3465 }
3466
3467 /* Allow "NFA_MOPEN" as a valid postfix representation for
3468 * the empty regexp "". In this case, the NFA will be
3469 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3470 * empty groups of parenthesis, and empty mbyte chars */
3471 if (stackp == stack)
3472 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003473 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003475 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003476 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003477 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003478 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 patch(list1(&s->out), s1);
3480 PUSH(frag(s, list1(&s1->out)));
3481 break;
3482 }
3483
3484 /* At least one node was emitted before NFA_MOPEN, so
3485 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3486 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003487 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003488 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003489 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003490
Bram Moolenaar525666f2013-06-02 16:40:55 +02003491 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003492 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003493 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 patch(e.out, s1);
3495
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003496#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003497 if (mopen == NFA_COMPOSING)
3498 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003500#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003501
3502 PUSH(frag(s, list1(&s1->out)));
3503 break;
3504
Bram Moolenaar5714b802013-05-28 22:03:20 +02003505 case NFA_BACKREF1:
3506 case NFA_BACKREF2:
3507 case NFA_BACKREF3:
3508 case NFA_BACKREF4:
3509 case NFA_BACKREF5:
3510 case NFA_BACKREF6:
3511 case NFA_BACKREF7:
3512 case NFA_BACKREF8:
3513 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003514#ifdef FEAT_SYN_HL
3515 case NFA_ZREF1:
3516 case NFA_ZREF2:
3517 case NFA_ZREF3:
3518 case NFA_ZREF4:
3519 case NFA_ZREF5:
3520 case NFA_ZREF6:
3521 case NFA_ZREF7:
3522 case NFA_ZREF8:
3523 case NFA_ZREF9:
3524#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003525 if (nfa_calc_size == TRUE)
3526 {
3527 nstate += 2;
3528 break;
3529 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003530 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003531 if (s == NULL)
3532 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003533 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003534 if (s1 == NULL)
3535 goto theend;
3536 patch(list1(&s->out), s1);
3537 PUSH(frag(s, list1(&s1->out)));
3538 break;
3539
Bram Moolenaar423532e2013-05-29 21:14:42 +02003540 case NFA_LNUM:
3541 case NFA_LNUM_GT:
3542 case NFA_LNUM_LT:
3543 case NFA_VCOL:
3544 case NFA_VCOL_GT:
3545 case NFA_VCOL_LT:
3546 case NFA_COL:
3547 case NFA_COL_GT:
3548 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003549 case NFA_MARK:
3550 case NFA_MARK_GT:
3551 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003552 {
3553 int n = *++p; /* lnum, col or mark name */
3554
Bram Moolenaar423532e2013-05-29 21:14:42 +02003555 if (nfa_calc_size == TRUE)
3556 {
3557 nstate += 1;
3558 break;
3559 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003560 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003561 if (s == NULL)
3562 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003563 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003564 PUSH(frag(s, list1(&s->out)));
3565 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003566 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003567
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003568 case NFA_ZSTART:
3569 case NFA_ZEND:
3570 default:
3571 /* Operands */
3572 if (nfa_calc_size == TRUE)
3573 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003574 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003575 break;
3576 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003577 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003579 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 PUSH(frag(s, list1(&s->out)));
3581 break;
3582
3583 } /* switch(*p) */
3584
3585 } /* for(p = postfix; *p; ++p) */
3586
3587 if (nfa_calc_size == TRUE)
3588 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003589 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003590 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003591 }
3592
3593 e = POP();
3594 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003595 {
3596 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003597 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 +02003598 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599
3600 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003601 {
3602 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003603 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003604 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003606 matchstate = &state_ptr[istate++]; /* the match state */
3607 matchstate->c = NFA_MATCH;
3608 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003609 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610
3611 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003612 ret = e.start;
3613
3614theend:
3615 vim_free(stack);
3616 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617
3618#undef POP1
3619#undef PUSH1
3620#undef POP2
3621#undef PUSH2
3622#undef POP
3623#undef PUSH
3624}
3625
Bram Moolenaara2947e22013-06-11 22:44:09 +02003626/*
3627 * After building the NFA program, inspect it to add optimization hints.
3628 */
3629 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003630nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003631{
3632 int i;
3633 int c;
3634
3635 for (i = 0; i < prog->nstate; ++i)
3636 {
3637 c = prog->state[i].c;
3638 if (c == NFA_START_INVISIBLE
3639 || c == NFA_START_INVISIBLE_NEG
3640 || c == NFA_START_INVISIBLE_BEFORE
3641 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3642 {
3643 int directly;
3644
3645 /* Do it directly when what follows is possibly the end of the
3646 * match. */
3647 if (match_follows(prog->state[i].out1->out, 0))
3648 directly = TRUE;
3649 else
3650 {
3651 int ch_invisible = failure_chance(prog->state[i].out, 0);
3652 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3653
3654 /* Postpone when the invisible match is expensive or has a
3655 * lower chance of failing. */
3656 if (c == NFA_START_INVISIBLE_BEFORE
3657 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3658 {
3659 /* "before" matches are very expensive when
3660 * unbounded, always prefer what follows then,
3661 * unless what follows will always match.
3662 * Otherwise strongly prefer what follows. */
3663 if (prog->state[i].val <= 0 && ch_follows > 0)
3664 directly = FALSE;
3665 else
3666 directly = ch_follows * 10 < ch_invisible;
3667 }
3668 else
3669 {
3670 /* normal invisible, first do the one with the
3671 * highest failure chance */
3672 directly = ch_follows < ch_invisible;
3673 }
3674 }
3675 if (directly)
3676 /* switch to the _FIRST state */
3677 ++prog->state[i].c;
3678 }
3679 }
3680}
3681
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003682/****************************************************************
3683 * NFA execution code.
3684 ****************************************************************/
3685
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003686typedef struct
3687{
3688 int in_use; /* number of subexpr with useful info */
3689
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003690 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003691 union
3692 {
3693 struct multipos
3694 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003695 linenr_T start_lnum;
3696 linenr_T end_lnum;
3697 colnr_T start_col;
3698 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003699 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003700 struct linepos
3701 {
3702 char_u *start;
3703 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003704 } line[NSUBEXP];
3705 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003706} regsub_T;
3707
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003708typedef struct
3709{
3710 regsub_T norm; /* \( .. \) matches */
3711#ifdef FEAT_SYN_HL
3712 regsub_T synt; /* \z( .. \) matches */
3713#endif
3714} regsubs_T;
3715
Bram Moolenaara2d95102013-06-04 14:23:05 +02003716/* nfa_pim_T stores a Postponed Invisible Match. */
3717typedef struct nfa_pim_S nfa_pim_T;
3718struct nfa_pim_S
3719{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003720 int result; /* NFA_PIM_*, see below */
3721 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003722 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003723 union
3724 {
3725 lpos_T pos;
3726 char_u *ptr;
3727 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003728};
3729
3730/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003731#define NFA_PIM_UNUSED 0 /* pim not used */
3732#define NFA_PIM_TODO 1 /* pim not done yet */
3733#define NFA_PIM_MATCH 2 /* pim executed, matches */
3734#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003735
3736
Bram Moolenaar963fee22013-05-26 21:47:28 +02003737/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003738typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739{
3740 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003741 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003742 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3743 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003744 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003745} nfa_thread_T;
3746
Bram Moolenaar963fee22013-05-26 21:47:28 +02003747/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003748typedef struct
3749{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003750 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003751 int n; /* nr of states currently in "t" */
3752 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003753 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003754 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003755} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003756
Bram Moolenaar5714b802013-05-28 22:03:20 +02003757#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003758static void log_subsexpr(regsubs_T *subs);
3759static void log_subexpr(regsub_T *sub);
3760static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003761
3762 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003763log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003764{
3765 log_subexpr(&subs->norm);
3766# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003767 if (nfa_has_zsubexpr)
3768 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003769# endif
3770}
3771
Bram Moolenaar5714b802013-05-28 22:03:20 +02003772 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003773log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003774{
3775 int j;
3776
3777 for (j = 0; j < sub->in_use; j++)
3778 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003779 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003780 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003781 sub->list.multi[j].start_col,
3782 (int)sub->list.multi[j].start_lnum,
3783 sub->list.multi[j].end_col,
3784 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003785 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003786 {
3787 char *s = (char *)sub->list.line[j].start;
3788 char *e = (char *)sub->list.line[j].end;
3789
Bram Moolenaar87953742013-06-05 18:52:40 +02003790 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003791 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003792 s == NULL ? "NULL" : s,
3793 e == NULL ? "NULL" : e);
3794 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003795}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003796
3797 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003798pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003799{
3800 static char buf[30];
3801
3802 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3803 buf[0] = NUL;
3804 else
3805 {
3806 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3807 : (int)(pim->end.ptr - reginput));
3808 }
3809 return buf;
3810}
3811
Bram Moolenaar5714b802013-05-28 22:03:20 +02003812#endif
3813
Bram Moolenaar963fee22013-05-26 21:47:28 +02003814/* Used during execution: whether a match has been found. */
3815static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003816#ifdef FEAT_RELTIME
3817static proftime_T *nfa_time_limit;
3818static int nfa_time_count;
3819#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003820
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003821static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3822static void clear_sub(regsub_T *sub);
3823static void copy_sub(regsub_T *to, regsub_T *from);
3824static void copy_sub_off(regsub_T *to, regsub_T *from);
3825static void copy_ze_off(regsub_T *to, regsub_T *from);
3826static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3827static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3828static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3829static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3830static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3831static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3832static 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 +02003833
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003834/*
3835 * Copy postponed invisible match info from "from" to "to".
3836 */
3837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003838copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003839{
3840 to->result = from->result;
3841 to->state = from->state;
3842 copy_sub(&to->subs.norm, &from->subs.norm);
3843#ifdef FEAT_SYN_HL
3844 if (nfa_has_zsubexpr)
3845 copy_sub(&to->subs.synt, &from->subs.synt);
3846#endif
3847 to->end = from->end;
3848}
3849
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003851clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003852{
3853 if (REG_MULTI)
3854 /* Use 0xff to set lnum to -1 */
3855 vim_memset(sub->list.multi, 0xff,
3856 sizeof(struct multipos) * nfa_nsubexpr);
3857 else
3858 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3859 sub->in_use = 0;
3860}
3861
3862/*
3863 * Copy the submatches from "from" to "to".
3864 */
3865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003866copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003867{
3868 to->in_use = from->in_use;
3869 if (from->in_use > 0)
3870 {
3871 /* Copy the match start and end positions. */
3872 if (REG_MULTI)
3873 mch_memmove(&to->list.multi[0],
3874 &from->list.multi[0],
3875 sizeof(struct multipos) * from->in_use);
3876 else
3877 mch_memmove(&to->list.line[0],
3878 &from->list.line[0],
3879 sizeof(struct linepos) * from->in_use);
3880 }
3881}
3882
3883/*
3884 * Like copy_sub() but exclude the main match.
3885 */
3886 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003887copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003888{
3889 if (to->in_use < from->in_use)
3890 to->in_use = from->in_use;
3891 if (from->in_use > 1)
3892 {
3893 /* Copy the match start and end positions. */
3894 if (REG_MULTI)
3895 mch_memmove(&to->list.multi[1],
3896 &from->list.multi[1],
3897 sizeof(struct multipos) * (from->in_use - 1));
3898 else
3899 mch_memmove(&to->list.line[1],
3900 &from->list.line[1],
3901 sizeof(struct linepos) * (from->in_use - 1));
3902 }
3903}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003904
Bram Moolenaar428e9872013-05-30 17:05:39 +02003905/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003906 * Like copy_sub() but only do the end of the main match if \ze is present.
3907 */
3908 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003909copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003910{
3911 if (nfa_has_zend)
3912 {
3913 if (REG_MULTI)
3914 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003915 if (from->list.multi[0].end_lnum >= 0)
3916 {
3917 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3918 to->list.multi[0].end_col = from->list.multi[0].end_col;
3919 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02003920 }
3921 else
3922 {
3923 if (from->list.line[0].end != NULL)
3924 to->list.line[0].end = from->list.line[0].end;
3925 }
3926 }
3927}
3928
3929/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003930 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003931 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003932 */
3933 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003934sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003935{
3936 int i;
3937 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003938 linenr_T s1;
3939 linenr_T s2;
3940 char_u *sp1;
3941 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003942
3943 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3944 if (REG_MULTI)
3945 {
3946 for (i = 0; i < todo; ++i)
3947 {
3948 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003949 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003950 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003951 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003952 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003953 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003954 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003955 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003956 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003957 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003958 if (s1 != -1 && sub1->list.multi[i].start_col
3959 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003960 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02003961
3962 if (nfa_has_backref)
3963 {
3964 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003965 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02003966 else
3967 s1 = -1;
3968 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003969 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02003970 else
3971 s2 = -1;
3972 if (s1 != s2)
3973 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003974 if (s1 != -1 && sub1->list.multi[i].end_col
3975 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02003976 return FALSE;
3977 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003978 }
3979 }
3980 else
3981 {
3982 for (i = 0; i < todo; ++i)
3983 {
3984 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003985 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003986 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003987 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003988 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003989 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003990 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003991 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003992 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003993 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02003994 if (nfa_has_backref)
3995 {
3996 if (i < sub1->in_use)
3997 sp1 = sub1->list.line[i].end;
3998 else
3999 sp1 = NULL;
4000 if (i < sub2->in_use)
4001 sp2 = sub2->list.line[i].end;
4002 else
4003 sp2 = NULL;
4004 if (sp1 != sp2)
4005 return FALSE;
4006 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004007 }
4008 }
4009
4010 return TRUE;
4011}
4012
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004013#ifdef ENABLE_LOG
4014 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004015report_state(char *action,
4016 regsub_T *sub,
4017 nfa_state_T *state,
4018 int lid,
4019 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004020{
4021 int col;
4022
4023 if (sub->in_use <= 0)
4024 col = -1;
4025 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004026 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004027 else
4028 col = (int)(sub->list.line[0].start - regline);
4029 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004030 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4031 action, abs(state->id), lid, state->c, code, col,
4032 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004033}
4034#endif
4035
Bram Moolenaar43e02982013-06-07 17:31:29 +02004036/*
4037 * Return TRUE if the same state is already in list "l" with the same
4038 * positions as "subs".
4039 */
4040 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004041has_state_with_pos(
4042 nfa_list_T *l, /* runtime state list */
4043 nfa_state_T *state, /* state to update */
4044 regsubs_T *subs, /* pointers to subexpressions */
4045 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004046{
4047 nfa_thread_T *thread;
4048 int i;
4049
4050 for (i = 0; i < l->n; ++i)
4051 {
4052 thread = &l->t[i];
4053 if (thread->state->id == state->id
4054 && sub_equal(&thread->subs.norm, &subs->norm)
4055#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004056 && (!nfa_has_zsubexpr
4057 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004058#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004059 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004060 return TRUE;
4061 }
4062 return FALSE;
4063}
4064
4065/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004066 * Return TRUE if "one" and "two" are equal. That includes when both are not
4067 * set.
4068 */
4069 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004070pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004071{
4072 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4073 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4074
4075 if (one_unused)
4076 /* one is unused: equal when two is also unused */
4077 return two_unused;
4078 if (two_unused)
4079 /* one is used and two is not: not equal */
4080 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004081 /* compare the state id */
4082 if (one->state->id != two->state->id)
4083 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004084 /* compare the position */
4085 if (REG_MULTI)
4086 return one->end.pos.lnum == two->end.pos.lnum
4087 && one->end.pos.col == two->end.pos.col;
4088 return one->end.ptr == two->end.ptr;
4089}
4090
4091/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004092 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4093 */
4094 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004095match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004096{
4097 nfa_state_T *state = startstate;
4098
4099 /* avoid too much recursion */
4100 if (depth > 10)
4101 return FALSE;
4102
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004103 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004104 {
4105 switch (state->c)
4106 {
4107 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004108 case NFA_MCLOSE:
4109 case NFA_END_INVISIBLE:
4110 case NFA_END_INVISIBLE_NEG:
4111 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004112 return TRUE;
4113
4114 case NFA_SPLIT:
4115 return match_follows(state->out, depth + 1)
4116 || match_follows(state->out1, depth + 1);
4117
4118 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004119 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004120 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004121 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004122 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004123 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004124 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004125 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004126 case NFA_COMPOSING:
4127 /* skip ahead to next state */
4128 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004129 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004130
4131 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004132 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004133 case NFA_IDENT:
4134 case NFA_SIDENT:
4135 case NFA_KWORD:
4136 case NFA_SKWORD:
4137 case NFA_FNAME:
4138 case NFA_SFNAME:
4139 case NFA_PRINT:
4140 case NFA_SPRINT:
4141 case NFA_WHITE:
4142 case NFA_NWHITE:
4143 case NFA_DIGIT:
4144 case NFA_NDIGIT:
4145 case NFA_HEX:
4146 case NFA_NHEX:
4147 case NFA_OCTAL:
4148 case NFA_NOCTAL:
4149 case NFA_WORD:
4150 case NFA_NWORD:
4151 case NFA_HEAD:
4152 case NFA_NHEAD:
4153 case NFA_ALPHA:
4154 case NFA_NALPHA:
4155 case NFA_LOWER:
4156 case NFA_NLOWER:
4157 case NFA_UPPER:
4158 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004159 case NFA_LOWER_IC:
4160 case NFA_NLOWER_IC:
4161 case NFA_UPPER_IC:
4162 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004163 case NFA_START_COLL:
4164 case NFA_START_NEG_COLL:
4165 case NFA_NEWL:
4166 /* state will advance input */
4167 return FALSE;
4168
4169 default:
4170 if (state->c > 0)
4171 /* state will advance input */
4172 return FALSE;
4173
4174 /* Others: zero-width or possibly zero-width, might still find
4175 * a match at the same position, keep looking. */
4176 break;
4177 }
4178 state = state->out;
4179 }
4180 return FALSE;
4181}
4182
4183
4184/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004185 * Return TRUE if "state" is already in list "l".
4186 */
4187 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004188state_in_list(
4189 nfa_list_T *l, /* runtime state list */
4190 nfa_state_T *state, /* state to update */
4191 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004192{
4193 if (state->lastlist[nfa_ll_index] == l->id)
4194 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004195 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004196 return TRUE;
4197 }
4198 return FALSE;
4199}
4200
Bram Moolenaard05bf562013-06-30 23:24:08 +02004201/*
4202 * Add "state" and possibly what follows to state list ".".
4203 * Returns "subs_arg", possibly copied into temp_subs.
4204 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004205 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004206addstate(
4207 nfa_list_T *l, /* runtime state list */
4208 nfa_state_T *state, /* state to update */
4209 regsubs_T *subs_arg, /* pointers to subexpressions */
4210 nfa_pim_T *pim, /* postponed look-behind match */
4211 int off) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004212{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004213 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004214 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004215 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004216 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004217 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004218 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004219 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004220 regsubs_T *subs = subs_arg;
4221 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004222#ifdef ENABLE_LOG
4223 int did_print = FALSE;
4224#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004226 switch (state->c)
4227 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004228 case NFA_NCLOSE:
4229 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004230 case NFA_MCLOSE1:
4231 case NFA_MCLOSE2:
4232 case NFA_MCLOSE3:
4233 case NFA_MCLOSE4:
4234 case NFA_MCLOSE5:
4235 case NFA_MCLOSE6:
4236 case NFA_MCLOSE7:
4237 case NFA_MCLOSE8:
4238 case NFA_MCLOSE9:
4239#ifdef FEAT_SYN_HL
4240 case NFA_ZCLOSE:
4241 case NFA_ZCLOSE1:
4242 case NFA_ZCLOSE2:
4243 case NFA_ZCLOSE3:
4244 case NFA_ZCLOSE4:
4245 case NFA_ZCLOSE5:
4246 case NFA_ZCLOSE6:
4247 case NFA_ZCLOSE7:
4248 case NFA_ZCLOSE8:
4249 case NFA_ZCLOSE9:
4250#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004251 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004252 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004253 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004254 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004255 /* These nodes are not added themselves but their "out" and/or
4256 * "out1" may be added below. */
4257 break;
4258
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004259 case NFA_BOL:
4260 case NFA_BOF:
4261 /* "^" won't match past end-of-line, don't bother trying.
4262 * Except when at the end of the line, or when we are going to the
4263 * next line for a look-behind match. */
4264 if (reginput > regline
4265 && *reginput != NUL
4266 && (nfa_endp == NULL
4267 || !REG_MULTI
4268 || reglnum == nfa_endp->se_u.pos.lnum))
4269 goto skip_add;
4270 /* FALLTHROUGH */
4271
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004272 case NFA_MOPEN1:
4273 case NFA_MOPEN2:
4274 case NFA_MOPEN3:
4275 case NFA_MOPEN4:
4276 case NFA_MOPEN5:
4277 case NFA_MOPEN6:
4278 case NFA_MOPEN7:
4279 case NFA_MOPEN8:
4280 case NFA_MOPEN9:
4281#ifdef FEAT_SYN_HL
4282 case NFA_ZOPEN:
4283 case NFA_ZOPEN1:
4284 case NFA_ZOPEN2:
4285 case NFA_ZOPEN3:
4286 case NFA_ZOPEN4:
4287 case NFA_ZOPEN5:
4288 case NFA_ZOPEN6:
4289 case NFA_ZOPEN7:
4290 case NFA_ZOPEN8:
4291 case NFA_ZOPEN9:
4292#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004293 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004294 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004295 /* These nodes need to be added so that we can bail out when it
4296 * was added to this list before at the same position to avoid an
4297 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004298
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004299 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004300 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004301 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004302 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004303 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004304 * when there is a PIM. For NFA_MATCH check the position,
4305 * lower position is preferred. */
4306 if (!nfa_has_backref && pim == NULL && !l->has_pim
4307 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004308 {
4309skip_add:
4310#ifdef ENABLE_LOG
4311 nfa_set_code(state->c);
4312 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4313 abs(state->id), l->id, state->c, code);
4314#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004315 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004316 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004317
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004318 /* Do not add the state again when it exists with the same
4319 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004320 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004321 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004322 }
4323
Bram Moolenaara0169122013-06-26 18:16:58 +02004324 /* When there are backreferences or PIMs the number of states may
4325 * be (a lot) bigger than anticipated. */
4326 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004327 {
4328 int newlen = l->len * 3 / 2 + 50;
4329
Bram Moolenaard05bf562013-06-30 23:24:08 +02004330 if (subs != &temp_subs)
4331 {
4332 /* "subs" may point into the current array, need to make a
4333 * copy before it becomes invalid. */
4334 copy_sub(&temp_subs.norm, &subs->norm);
4335#ifdef FEAT_SYN_HL
4336 if (nfa_has_zsubexpr)
4337 copy_sub(&temp_subs.synt, &subs->synt);
4338#endif
4339 subs = &temp_subs;
4340 }
4341
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004342 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004343 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4344 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004345 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004346
4347 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004348 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004349 thread = &l->t[l->n++];
4350 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004351 if (pim == NULL)
4352 thread->pim.result = NFA_PIM_UNUSED;
4353 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004354 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004355 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004356 l->has_pim = TRUE;
4357 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004358 copy_sub(&thread->subs.norm, &subs->norm);
4359#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004360 if (nfa_has_zsubexpr)
4361 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004362#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004363#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004364 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004365 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004366#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367 }
4368
4369#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004370 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004371 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004372#endif
4373 switch (state->c)
4374 {
4375 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004376 break;
4377
4378 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004379 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004380 subs = addstate(l, state->out, subs, pim, off);
4381 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004382 break;
4383
Bram Moolenaar699c1202013-09-25 16:41:54 +02004384 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004385 case NFA_NOPEN:
4386 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004387 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388 break;
4389
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004390 case NFA_MOPEN:
4391 case NFA_MOPEN1:
4392 case NFA_MOPEN2:
4393 case NFA_MOPEN3:
4394 case NFA_MOPEN4:
4395 case NFA_MOPEN5:
4396 case NFA_MOPEN6:
4397 case NFA_MOPEN7:
4398 case NFA_MOPEN8:
4399 case NFA_MOPEN9:
4400#ifdef FEAT_SYN_HL
4401 case NFA_ZOPEN:
4402 case NFA_ZOPEN1:
4403 case NFA_ZOPEN2:
4404 case NFA_ZOPEN3:
4405 case NFA_ZOPEN4:
4406 case NFA_ZOPEN5:
4407 case NFA_ZOPEN6:
4408 case NFA_ZOPEN7:
4409 case NFA_ZOPEN8:
4410 case NFA_ZOPEN9:
4411#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004412 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004413 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004414 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004415 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004416 sub = &subs->norm;
4417 }
4418#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004419 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004420 {
4421 subidx = state->c - NFA_ZOPEN;
4422 sub = &subs->synt;
4423 }
4424#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004425 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004426 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004427 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004428 sub = &subs->norm;
4429 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004430
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004431 /* avoid compiler warnings */
4432 save_ptr = NULL;
4433 save_lpos.lnum = 0;
4434 save_lpos.col = 0;
4435
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004436 /* Set the position (with "off" added) in the subexpression. Save
4437 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004438 if (REG_MULTI)
4439 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004440 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004441 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004442 save_lpos.lnum = sub->list.multi[subidx].start_lnum;
4443 save_lpos.col = sub->list.multi[subidx].start_col;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004444 save_in_use = -1;
4445 }
4446 else
4447 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004448 save_in_use = sub->in_use;
4449 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004450 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004451 sub->list.multi[i].start_lnum = -1;
4452 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004453 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004454 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004455 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004456 if (off == -1)
4457 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004458 sub->list.multi[subidx].start_lnum = reglnum + 1;
4459 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004460 }
4461 else
4462 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004463 sub->list.multi[subidx].start_lnum = reglnum;
4464 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004465 (colnr_T)(reginput - regline + off);
4466 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004467 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004468 }
4469 else
4470 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004471 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004472 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004473 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004474 save_in_use = -1;
4475 }
4476 else
4477 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004478 save_in_use = sub->in_use;
4479 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004480 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004481 sub->list.line[i].start = NULL;
4482 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004483 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004484 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004485 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004486 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004487 }
4488
Bram Moolenaard05bf562013-06-30 23:24:08 +02004489 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004490 /* "subs" may have changed, need to set "sub" again */
4491#ifdef FEAT_SYN_HL
4492 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4493 sub = &subs->synt;
4494 else
4495#endif
4496 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004497
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004498 if (save_in_use == -1)
4499 {
4500 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004501 {
4502 sub->list.multi[subidx].start_lnum = save_lpos.lnum;
4503 sub->list.multi[subidx].start_col = save_lpos.col;
4504 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004505 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004506 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004507 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004508 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004509 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004510 break;
4511
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004512 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004513 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004514 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004515 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004516 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004517 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004518 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 break;
4520 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004521 case NFA_MCLOSE1:
4522 case NFA_MCLOSE2:
4523 case NFA_MCLOSE3:
4524 case NFA_MCLOSE4:
4525 case NFA_MCLOSE5:
4526 case NFA_MCLOSE6:
4527 case NFA_MCLOSE7:
4528 case NFA_MCLOSE8:
4529 case NFA_MCLOSE9:
4530#ifdef FEAT_SYN_HL
4531 case NFA_ZCLOSE:
4532 case NFA_ZCLOSE1:
4533 case NFA_ZCLOSE2:
4534 case NFA_ZCLOSE3:
4535 case NFA_ZCLOSE4:
4536 case NFA_ZCLOSE5:
4537 case NFA_ZCLOSE6:
4538 case NFA_ZCLOSE7:
4539 case NFA_ZCLOSE8:
4540 case NFA_ZCLOSE9:
4541#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004542 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004543 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004544 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004545 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004546 sub = &subs->norm;
4547 }
4548#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004549 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004550 {
4551 subidx = state->c - NFA_ZCLOSE;
4552 sub = &subs->synt;
4553 }
4554#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004555 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004556 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004557 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004558 sub = &subs->norm;
4559 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004561 /* We don't fill in gaps here, there must have been an MOPEN that
4562 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004563 save_in_use = sub->in_use;
4564 if (sub->in_use <= subidx)
4565 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004566 if (REG_MULTI)
4567 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004568 save_lpos.lnum = sub->list.multi[subidx].end_lnum;
4569 save_lpos.col = sub->list.multi[subidx].end_col;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004570 if (off == -1)
4571 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004572 sub->list.multi[subidx].end_lnum = reglnum + 1;
4573 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004574 }
4575 else
4576 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004577 sub->list.multi[subidx].end_lnum = reglnum;
4578 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004579 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004580 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004581 /* avoid compiler warnings */
4582 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004583 }
4584 else
4585 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004586 save_ptr = sub->list.line[subidx].end;
4587 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004588 /* avoid compiler warnings */
4589 save_lpos.lnum = 0;
4590 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004591 }
4592
Bram Moolenaard05bf562013-06-30 23:24:08 +02004593 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004594 /* "subs" may have changed, need to set "sub" again */
4595#ifdef FEAT_SYN_HL
4596 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4597 sub = &subs->synt;
4598 else
4599#endif
4600 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004601
4602 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004603 {
4604 sub->list.multi[subidx].end_lnum = save_lpos.lnum;
4605 sub->list.multi[subidx].end_col = save_lpos.col;
4606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004607 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004608 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004609 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004610 break;
4611 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004612 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004613}
4614
4615/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004616 * Like addstate(), but the new state(s) are put at position "*ip".
4617 * Used for zero-width matches, next state to use is the added one.
4618 * This makes sure the order of states to be tried does not change, which
4619 * matters for alternatives.
4620 */
4621 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004622addstate_here(
4623 nfa_list_T *l, /* runtime state list */
4624 nfa_state_T *state, /* state to update */
4625 regsubs_T *subs, /* pointers to subexpressions */
4626 nfa_pim_T *pim, /* postponed look-behind match */
4627 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004628{
4629 int tlen = l->n;
4630 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004631 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004632
4633 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004634 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004635
Bram Moolenaar4b417062013-05-25 20:19:50 +02004636 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004637 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004638 return;
4639
4640 /* re-order to put the new state at the current position */
4641 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004642 if (count == 0)
4643 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004644 if (count == 1)
4645 {
4646 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004647 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004648 }
4649 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004650 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004651 if (l->n + count - 1 >= l->len)
4652 {
4653 /* not enough space to move the new states, reallocate the list
4654 * and move the states to the right position */
4655 nfa_thread_T *newl;
4656
4657 l->len = l->len * 3 / 2 + 50;
4658 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4659 if (newl == NULL)
4660 return;
4661 mch_memmove(&(newl[0]),
4662 &(l->t[0]),
4663 sizeof(nfa_thread_T) * listidx);
4664 mch_memmove(&(newl[listidx]),
4665 &(l->t[l->n - count]),
4666 sizeof(nfa_thread_T) * count);
4667 mch_memmove(&(newl[listidx + count]),
4668 &(l->t[listidx + 1]),
4669 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4670 vim_free(l->t);
4671 l->t = newl;
4672 }
4673 else
4674 {
4675 /* make space for new states, then move them from the
4676 * end to the current position */
4677 mch_memmove(&(l->t[listidx + count]),
4678 &(l->t[listidx + 1]),
4679 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4680 mch_memmove(&(l->t[listidx]),
4681 &(l->t[l->n - 1]),
4682 sizeof(nfa_thread_T) * count);
4683 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004684 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004685 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004686 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004687}
4688
4689/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004690 * Check character class "class" against current character c.
4691 */
4692 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004693check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004694{
4695 switch (class)
4696 {
4697 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004698 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699 return OK;
4700 break;
4701 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004702 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004703 return OK;
4704 break;
4705 case NFA_CLASS_BLANK:
4706 if (c == ' ' || c == '\t')
4707 return OK;
4708 break;
4709 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004710 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 return OK;
4712 break;
4713 case NFA_CLASS_DIGIT:
4714 if (VIM_ISDIGIT(c))
4715 return OK;
4716 break;
4717 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004718 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004719 return OK;
4720 break;
4721 case NFA_CLASS_LOWER:
4722 if (MB_ISLOWER(c))
4723 return OK;
4724 break;
4725 case NFA_CLASS_PRINT:
4726 if (vim_isprintc(c))
4727 return OK;
4728 break;
4729 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004730 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004731 return OK;
4732 break;
4733 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004734 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004735 return OK;
4736 break;
4737 case NFA_CLASS_UPPER:
4738 if (MB_ISUPPER(c))
4739 return OK;
4740 break;
4741 case NFA_CLASS_XDIGIT:
4742 if (vim_isxdigit(c))
4743 return OK;
4744 break;
4745 case NFA_CLASS_TAB:
4746 if (c == '\t')
4747 return OK;
4748 break;
4749 case NFA_CLASS_RETURN:
4750 if (c == '\r')
4751 return OK;
4752 break;
4753 case NFA_CLASS_BACKSPACE:
4754 if (c == '\b')
4755 return OK;
4756 break;
4757 case NFA_CLASS_ESCAPE:
4758 if (c == '\033')
4759 return OK;
4760 break;
4761
4762 default:
4763 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004764 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004765 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004766 }
4767 return FAIL;
4768}
4769
Bram Moolenaar5714b802013-05-28 22:03:20 +02004770/*
4771 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004772 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004773 */
4774 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004775match_backref(
4776 regsub_T *sub, /* pointers to subexpressions */
4777 int subidx,
4778 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004779{
4780 int len;
4781
4782 if (sub->in_use <= subidx)
4783 {
4784retempty:
4785 /* backref was not set, match an empty string */
4786 *bytelen = 0;
4787 return TRUE;
4788 }
4789
4790 if (REG_MULTI)
4791 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004792 if (sub->list.multi[subidx].start_lnum < 0
4793 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004794 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004795 if (sub->list.multi[subidx].start_lnum == reglnum
4796 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004797 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004798 len = sub->list.multi[subidx].end_col
4799 - sub->list.multi[subidx].start_col;
4800 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004801 reginput, &len) == 0)
4802 {
4803 *bytelen = len;
4804 return TRUE;
4805 }
4806 }
4807 else
4808 {
4809 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004810 sub->list.multi[subidx].start_lnum,
4811 sub->list.multi[subidx].start_col,
4812 sub->list.multi[subidx].end_lnum,
4813 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004814 bytelen) == RA_MATCH)
4815 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004816 }
4817 }
4818 else
4819 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004820 if (sub->list.line[subidx].start == NULL
4821 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004822 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004823 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4824 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004825 {
4826 *bytelen = len;
4827 return TRUE;
4828 }
4829 }
4830 return FALSE;
4831}
4832
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004833#ifdef FEAT_SYN_HL
4834
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004835static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004836
4837/*
4838 * Check for a match with \z subexpression "subidx".
4839 * Return TRUE if it matches.
4840 */
4841 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004842match_zref(
4843 int subidx,
4844 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004845{
4846 int len;
4847
4848 cleanup_zsubexpr();
4849 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4850 {
4851 /* backref was not set, match an empty string */
4852 *bytelen = 0;
4853 return TRUE;
4854 }
4855
4856 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4857 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4858 {
4859 *bytelen = len;
4860 return TRUE;
4861 }
4862 return FALSE;
4863}
4864#endif
4865
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004867 * Save list IDs for all NFA states of "prog" into "list".
4868 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004869 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004870 */
4871 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004872nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004873{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004874 int i;
4875 nfa_state_T *p;
4876
4877 /* Order in the list is reverse, it's a bit faster that way. */
4878 p = &prog->state[0];
4879 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004880 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004881 list[i] = p->lastlist[1];
4882 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004883 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004884 }
4885}
4886
4887/*
4888 * Restore list IDs from "list" to all NFA states.
4889 */
4890 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004891nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004892{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004893 int i;
4894 nfa_state_T *p;
4895
4896 p = &prog->state[0];
4897 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004898 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004899 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004900 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901 }
4902}
4903
Bram Moolenaar423532e2013-05-29 21:14:42 +02004904 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004905nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02004906{
4907 if (op == 1) return pos > val;
4908 if (op == 2) return pos < val;
4909 return val == pos;
4910}
4911
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004912static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
4913static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004914
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004915/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004916 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004917 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4918 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004919 */
4920 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004921recursive_regmatch(
4922 nfa_state_T *state,
4923 nfa_pim_T *pim,
4924 nfa_regprog_T *prog,
4925 regsubs_T *submatch,
4926 regsubs_T *m,
4927 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004928{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004929 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004930 int save_reglnum = reglnum;
4931 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004932 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004933 save_se_T *save_nfa_endp = nfa_endp;
4934 save_se_T endpos;
4935 save_se_T *endposp = NULL;
4936 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004937 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004938
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004939 if (pim != NULL)
4940 {
4941 /* start at the position where the postponed match was */
4942 if (REG_MULTI)
4943 reginput = regline + pim->end.pos.col;
4944 else
4945 reginput = pim->end.ptr;
4946 }
4947
Bram Moolenaardecd9542013-06-07 16:31:50 +02004948 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004949 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4950 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4951 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004952 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004953 /* The recursive match must end at the current position. When "pim" is
4954 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004955 endposp = &endpos;
4956 if (REG_MULTI)
4957 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004958 if (pim == NULL)
4959 {
4960 endpos.se_u.pos.col = (int)(reginput - regline);
4961 endpos.se_u.pos.lnum = reglnum;
4962 }
4963 else
4964 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004965 }
4966 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004967 {
4968 if (pim == NULL)
4969 endpos.se_u.ptr = reginput;
4970 else
4971 endpos.se_u.ptr = pim->end.ptr;
4972 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004973
4974 /* Go back the specified number of bytes, or as far as the
4975 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02004976 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004977 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004978 if (state->val <= 0)
4979 {
4980 if (REG_MULTI)
4981 {
4982 regline = reg_getline(--reglnum);
4983 if (regline == NULL)
4984 /* can't go before the first line */
4985 regline = reg_getline(++reglnum);
4986 }
4987 reginput = regline;
4988 }
4989 else
4990 {
4991 if (REG_MULTI && (int)(reginput - regline) < state->val)
4992 {
4993 /* Not enough bytes in this line, go to end of
4994 * previous line. */
4995 regline = reg_getline(--reglnum);
4996 if (regline == NULL)
4997 {
4998 /* can't go before the first line */
4999 regline = reg_getline(++reglnum);
5000 reginput = regline;
5001 }
5002 else
5003 reginput = regline + STRLEN(regline);
5004 }
5005 if ((int)(reginput - regline) >= state->val)
5006 {
5007 reginput -= state->val;
5008#ifdef FEAT_MBYTE
5009 if (has_mbyte)
5010 reginput -= mb_head_off(regline, reginput);
5011#endif
5012 }
5013 else
5014 reginput = regline;
5015 }
5016 }
5017
Bram Moolenaarf46da702013-06-02 22:37:42 +02005018#ifdef ENABLE_LOG
5019 if (log_fd != stderr)
5020 fclose(log_fd);
5021 log_fd = NULL;
5022#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005023 /* Have to clear the lastlist field of the NFA nodes, so that
5024 * nfa_regmatch() and addstate() can run properly after recursion. */
5025 if (nfa_ll_index == 1)
5026 {
5027 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5028 * values and clear them. */
5029 if (*listids == NULL)
5030 {
5031 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5032 if (*listids == NULL)
5033 {
5034 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5035 return 0;
5036 }
5037 }
5038 nfa_save_listids(prog, *listids);
5039 need_restore = TRUE;
5040 /* any value of nfa_listid will do */
5041 }
5042 else
5043 {
5044 /* First recursive nfa_regmatch() call, switch to the second lastlist
5045 * entry. Make sure nfa_listid is different from a previous recursive
5046 * call, because some states may still have this ID. */
5047 ++nfa_ll_index;
5048 if (nfa_listid <= nfa_alt_listid)
5049 nfa_listid = nfa_alt_listid;
5050 }
5051
5052 /* Call nfa_regmatch() to check if the current concat matches at this
5053 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005054 nfa_endp = endposp;
5055 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005056
5057 if (need_restore)
5058 nfa_restore_listids(prog, *listids);
5059 else
5060 {
5061 --nfa_ll_index;
5062 nfa_alt_listid = nfa_listid;
5063 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005064
5065 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005066 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005067 if (REG_MULTI)
5068 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005069 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005070 nfa_match = save_nfa_match;
5071 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005072 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005073
5074#ifdef ENABLE_LOG
5075 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5076 if (log_fd != NULL)
5077 {
5078 fprintf(log_fd, "****************************\n");
5079 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5080 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5081 fprintf(log_fd, "****************************\n");
5082 }
5083 else
5084 {
5085 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5086 log_fd = stderr;
5087 }
5088#endif
5089
5090 return result;
5091}
5092
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005093static int skip_to_start(int c, colnr_T *colp);
5094static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005095
5096/*
5097 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005098 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005099 * NFA_ANY: 1
5100 * specific character: 99
5101 */
5102 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005103failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005104{
5105 int c = state->c;
5106 int l, r;
5107
5108 /* detect looping */
5109 if (depth > 4)
5110 return 1;
5111
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005112 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005113 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005114 case NFA_SPLIT:
5115 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5116 /* avoid recursive stuff */
5117 return 1;
5118 /* two alternatives, use the lowest failure chance */
5119 l = failure_chance(state->out, depth + 1);
5120 r = failure_chance(state->out1, depth + 1);
5121 return l < r ? l : r;
5122
5123 case NFA_ANY:
5124 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005125 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005126
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005127 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005128 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005129 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005130 /* empty match works always */
5131 return 0;
5132
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005133 case NFA_START_INVISIBLE:
5134 case NFA_START_INVISIBLE_FIRST:
5135 case NFA_START_INVISIBLE_NEG:
5136 case NFA_START_INVISIBLE_NEG_FIRST:
5137 case NFA_START_INVISIBLE_BEFORE:
5138 case NFA_START_INVISIBLE_BEFORE_FIRST:
5139 case NFA_START_INVISIBLE_BEFORE_NEG:
5140 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5141 case NFA_START_PATTERN:
5142 /* recursive regmatch is expensive, use low failure chance */
5143 return 5;
5144
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005145 case NFA_BOL:
5146 case NFA_EOL:
5147 case NFA_BOF:
5148 case NFA_EOF:
5149 case NFA_NEWL:
5150 return 99;
5151
5152 case NFA_BOW:
5153 case NFA_EOW:
5154 return 90;
5155
5156 case NFA_MOPEN:
5157 case NFA_MOPEN1:
5158 case NFA_MOPEN2:
5159 case NFA_MOPEN3:
5160 case NFA_MOPEN4:
5161 case NFA_MOPEN5:
5162 case NFA_MOPEN6:
5163 case NFA_MOPEN7:
5164 case NFA_MOPEN8:
5165 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005166#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005167 case NFA_ZOPEN:
5168 case NFA_ZOPEN1:
5169 case NFA_ZOPEN2:
5170 case NFA_ZOPEN3:
5171 case NFA_ZOPEN4:
5172 case NFA_ZOPEN5:
5173 case NFA_ZOPEN6:
5174 case NFA_ZOPEN7:
5175 case NFA_ZOPEN8:
5176 case NFA_ZOPEN9:
5177 case NFA_ZCLOSE:
5178 case NFA_ZCLOSE1:
5179 case NFA_ZCLOSE2:
5180 case NFA_ZCLOSE3:
5181 case NFA_ZCLOSE4:
5182 case NFA_ZCLOSE5:
5183 case NFA_ZCLOSE6:
5184 case NFA_ZCLOSE7:
5185 case NFA_ZCLOSE8:
5186 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005187#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005188 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005189 case NFA_MCLOSE1:
5190 case NFA_MCLOSE2:
5191 case NFA_MCLOSE3:
5192 case NFA_MCLOSE4:
5193 case NFA_MCLOSE5:
5194 case NFA_MCLOSE6:
5195 case NFA_MCLOSE7:
5196 case NFA_MCLOSE8:
5197 case NFA_MCLOSE9:
5198 case NFA_NCLOSE:
5199 return failure_chance(state->out, depth + 1);
5200
5201 case NFA_BACKREF1:
5202 case NFA_BACKREF2:
5203 case NFA_BACKREF3:
5204 case NFA_BACKREF4:
5205 case NFA_BACKREF5:
5206 case NFA_BACKREF6:
5207 case NFA_BACKREF7:
5208 case NFA_BACKREF8:
5209 case NFA_BACKREF9:
5210#ifdef FEAT_SYN_HL
5211 case NFA_ZREF1:
5212 case NFA_ZREF2:
5213 case NFA_ZREF3:
5214 case NFA_ZREF4:
5215 case NFA_ZREF5:
5216 case NFA_ZREF6:
5217 case NFA_ZREF7:
5218 case NFA_ZREF8:
5219 case NFA_ZREF9:
5220#endif
5221 /* backreferences don't match in many places */
5222 return 94;
5223
5224 case NFA_LNUM_GT:
5225 case NFA_LNUM_LT:
5226 case NFA_COL_GT:
5227 case NFA_COL_LT:
5228 case NFA_VCOL_GT:
5229 case NFA_VCOL_LT:
5230 case NFA_MARK_GT:
5231 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005232 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005233 /* before/after positions don't match very often */
5234 return 85;
5235
5236 case NFA_LNUM:
5237 return 90;
5238
5239 case NFA_CURSOR:
5240 case NFA_COL:
5241 case NFA_VCOL:
5242 case NFA_MARK:
5243 /* specific positions rarely match */
5244 return 98;
5245
5246 case NFA_COMPOSING:
5247 return 95;
5248
5249 default:
5250 if (c > 0)
5251 /* character match fails often */
5252 return 95;
5253 }
5254
5255 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005256 return 50;
5257}
5258
Bram Moolenaarf46da702013-06-02 22:37:42 +02005259/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005260 * Skip until the char "c" we know a match must start with.
5261 */
5262 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005263skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005264{
5265 char_u *s;
5266
5267 /* Used often, do some work to avoid call overhead. */
5268 if (!ireg_ic
5269#ifdef FEAT_MBYTE
5270 && !has_mbyte
5271#endif
5272 )
5273 s = vim_strbyte(regline + *colp, c);
5274 else
5275 s = cstrchr(regline + *colp, c);
5276 if (s == NULL)
5277 return FAIL;
5278 *colp = (int)(s - regline);
5279 return OK;
5280}
5281
5282/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005283 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005284 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005285 * Returns zero for no match, 1 for a match.
5286 */
5287 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005288find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005289{
5290 colnr_T col = startcol;
5291 int c1, c2;
5292 int len1, len2;
5293 int match;
5294
5295 for (;;)
5296 {
5297 match = TRUE;
5298 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5299 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5300 {
5301 c1 = PTR2CHAR(match_text + len1);
5302 c2 = PTR2CHAR(regline + col + len2);
5303 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5304 {
5305 match = FALSE;
5306 break;
5307 }
5308 len2 += MB_CHAR2LEN(c2);
5309 }
5310 if (match
5311#ifdef FEAT_MBYTE
5312 /* check that no composing char follows */
5313 && !(enc_utf8
5314 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5315#endif
5316 )
5317 {
5318 cleanup_subexpr();
5319 if (REG_MULTI)
5320 {
5321 reg_startpos[0].lnum = reglnum;
5322 reg_startpos[0].col = col;
5323 reg_endpos[0].lnum = reglnum;
5324 reg_endpos[0].col = col + len2;
5325 }
5326 else
5327 {
5328 reg_startp[0] = regline + col;
5329 reg_endp[0] = regline + col + len2;
5330 }
5331 return 1L;
5332 }
5333
5334 /* Try finding regstart after the current match. */
5335 col += MB_CHAR2LEN(regstart); /* skip regstart */
5336 if (skip_to_start(regstart, &col) == FAIL)
5337 break;
5338 }
5339 return 0L;
5340}
5341
5342/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005343 * Main matching routine.
5344 *
5345 * Run NFA to determine whether it matches reginput.
5346 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005347 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005348 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005349 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005350 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005351 * Note: Caller must ensure that: start != NULL.
5352 */
5353 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005354nfa_regmatch(
5355 nfa_regprog_T *prog,
5356 nfa_state_T *start,
5357 regsubs_T *submatch,
5358 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005359{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005360 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005361 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005362 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005363 int go_to_nextline = FALSE;
5364 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005365 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005366 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005367 nfa_list_T *thislist;
5368 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005369 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005370 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005371 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005372 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005373 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005374 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005375#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005376 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005377
5378 if (debug == NULL)
5379 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005380 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005381 return FALSE;
5382 }
5383#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005384 /* Some patterns may take a long time to match, especially when using
5385 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5386 fast_breakcheck();
5387 if (got_int)
5388 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005389#ifdef FEAT_RELTIME
5390 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5391 return FALSE;
5392#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005393
Bram Moolenaar963fee22013-05-26 21:47:28 +02005394 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005395
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005396 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005397 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005398
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005399 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005400 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005401 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005402 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005403 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005404 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005405
5406#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005407 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005408 if (log_fd != NULL)
5409 {
5410 fprintf(log_fd, "**********************************\n");
5411 nfa_set_code(start->c);
5412 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5413 abs(start->id), code);
5414 fprintf(log_fd, "**********************************\n");
5415 }
5416 else
5417 {
5418 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5419 log_fd = stderr;
5420 }
5421#endif
5422
5423 thislist = &list[0];
5424 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005425 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005426 nextlist = &list[1];
5427 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005428 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005429#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005430 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005431#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005432 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005433
5434 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5435 * it's the first MOPEN. */
5436 if (toplevel)
5437 {
5438 if (REG_MULTI)
5439 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005440 m->norm.list.multi[0].start_lnum = reglnum;
5441 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005442 }
5443 else
5444 m->norm.list.line[0].start = reginput;
5445 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005446 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005447 }
5448 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005449 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005450
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005451#define ADD_STATE_IF_MATCH(state) \
5452 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005453 add_state = state->out; \
5454 add_off = clen; \
5455 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005456
5457 /*
5458 * Run for each character.
5459 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005460 for (;;)
5461 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005462 int curc;
5463 int clen;
5464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005465#ifdef FEAT_MBYTE
5466 if (has_mbyte)
5467 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005468 curc = (*mb_ptr2char)(reginput);
5469 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005470 }
5471 else
5472#endif
5473 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005474 curc = *reginput;
5475 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005477 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005478 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005479 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005480 go_to_nextline = FALSE;
5481 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005482
5483 /* swap lists */
5484 thislist = &list[flag];
5485 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005486 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005487 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005488 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005489 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5490 {
5491 /* too many states, retry with old engine */
5492 nfa_match = NFA_TOO_EXPENSIVE;
5493 goto theend;
5494 }
5495
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005496 thislist->id = nfa_listid;
5497 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005498
5499#ifdef ENABLE_LOG
5500 fprintf(log_fd, "------------------------------------------\n");
5501 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005502 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005504 {
5505 int i;
5506
5507 for (i = 0; i < thislist->n; i++)
5508 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5509 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005510 fprintf(log_fd, "\n");
5511#endif
5512
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005513#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005514 fprintf(debug, "\n-------------------\n");
5515#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005516 /*
5517 * If the state lists are empty we can stop.
5518 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005519 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005520 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005521
5522 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005523 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005524 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005525 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005527#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005528 nfa_set_code(t->state->c);
5529 fprintf(debug, "%s, ", code);
5530#endif
5531#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005532 {
5533 int col;
5534
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005535 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005536 col = -1;
5537 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005538 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005539 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005540 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005541 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005542 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5543 abs(t->state->id), (int)t->state->c, code, col,
5544 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005545 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546#endif
5547
5548 /*
5549 * Handle the possible codes of the current state.
5550 * The most important is NFA_MATCH.
5551 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005552 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005553 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005554 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005555 switch (t->state->c)
5556 {
5557 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005558 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005559#ifdef FEAT_MBYTE
5560 /* If the match ends before a composing characters and
5561 * ireg_icombine is not set, that is not really a match. */
5562 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5563 break;
5564#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005565 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005566 copy_sub(&submatch->norm, &t->subs.norm);
5567#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005568 if (nfa_has_zsubexpr)
5569 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005570#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005571#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005572 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005573#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005574 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005575 * states at this position. When the list of states is going
5576 * to be empty quit without advancing, so that "reginput" is
5577 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005578 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005579 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005580 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005581 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582
5583 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005584 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005585 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005586 /*
5587 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005588 * NFA_START_INVISIBLE_BEFORE node.
5589 * They surround a zero-width group, used with "\@=", "\&",
5590 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 * If we got here, it means that the current "invisible" group
5592 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005593 * nfa_regmatch(). For a look-behind match only when it ends
5594 * in the position in "nfa_endp".
5595 * Submatches are stored in *m, and used in the parent call.
5596 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005597#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005598 if (nfa_endp != NULL)
5599 {
5600 if (REG_MULTI)
5601 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5602 (int)reglnum,
5603 (int)nfa_endp->se_u.pos.lnum,
5604 (int)(reginput - regline),
5605 nfa_endp->se_u.pos.col);
5606 else
5607 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5608 (int)(reginput - regline),
5609 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005610 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005611#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005612 /* If "nfa_endp" is set it's only a match if it ends at
5613 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005614 if (nfa_endp != NULL && (REG_MULTI
5615 ? (reglnum != nfa_endp->se_u.pos.lnum
5616 || (int)(reginput - regline)
5617 != nfa_endp->se_u.pos.col)
5618 : reginput != nfa_endp->se_u.ptr))
5619 break;
5620
5621 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005622 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005623 {
5624 copy_sub(&m->norm, &t->subs.norm);
5625#ifdef FEAT_SYN_HL
5626 if (nfa_has_zsubexpr)
5627 copy_sub(&m->synt, &t->subs.synt);
5628#endif
5629 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005630#ifdef ENABLE_LOG
5631 fprintf(log_fd, "Match found:\n");
5632 log_subsexpr(m);
5633#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005634 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005635 /* See comment above at "goto nextchar". */
5636 if (nextlist->n == 0)
5637 clen = 0;
5638 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005639
5640 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005641 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005642 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005643 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005644 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005645 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005646 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005647 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005648 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005649#ifdef ENABLE_LOG
5650 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5651 failure_chance(t->state->out, 0),
5652 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005653#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005654 /* Do it directly if there already is a PIM or when
5655 * nfa_postprocess() detected it will work better. */
5656 if (t->pim.result != NFA_PIM_UNUSED
5657 || t->state->c == NFA_START_INVISIBLE_FIRST
5658 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5659 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5660 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005661 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005662 int in_use = m->norm.in_use;
5663
Bram Moolenaar699c1202013-09-25 16:41:54 +02005664 /* Copy submatch info for the recursive call, opposite
5665 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005666 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005667#ifdef FEAT_SYN_HL
5668 if (nfa_has_zsubexpr)
5669 copy_sub_off(&m->synt, &t->subs.synt);
5670#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005671
Bram Moolenaara2d95102013-06-04 14:23:05 +02005672 /*
5673 * First try matching the invisible match, then what
5674 * follows.
5675 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005676 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005677 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005678 if (result == NFA_TOO_EXPENSIVE)
5679 {
5680 nfa_match = result;
5681 goto theend;
5682 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005683
Bram Moolenaardecd9542013-06-07 16:31:50 +02005684 /* for \@! and \@<! it is a match when the result is
5685 * FALSE */
5686 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005687 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5688 || t->state->c
5689 == NFA_START_INVISIBLE_BEFORE_NEG
5690 || t->state->c
5691 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005692 {
5693 /* Copy submatch info from the recursive call */
5694 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005695#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005696 if (nfa_has_zsubexpr)
5697 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005698#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005699 /* If the pattern has \ze and it matched in the
5700 * sub pattern, use it. */
5701 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005702
Bram Moolenaara2d95102013-06-04 14:23:05 +02005703 /* t->state->out1 is the corresponding
5704 * END_INVISIBLE node; Add its out to the current
5705 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005706 add_here = TRUE;
5707 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005708 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005709 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005710 }
5711 else
5712 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005713 nfa_pim_T pim;
5714
Bram Moolenaara2d95102013-06-04 14:23:05 +02005715 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005716 * First try matching what follows. Only if a match
5717 * is found verify the invisible match matches. Add a
5718 * nfa_pim_T to the following states, it contains info
5719 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005720 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005721 pim.state = t->state;
5722 pim.result = NFA_PIM_TODO;
5723 pim.subs.norm.in_use = 0;
5724#ifdef FEAT_SYN_HL
5725 pim.subs.synt.in_use = 0;
5726#endif
5727 if (REG_MULTI)
5728 {
5729 pim.end.pos.col = (int)(reginput - regline);
5730 pim.end.pos.lnum = reglnum;
5731 }
5732 else
5733 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005734
5735 /* t->state->out1 is the corresponding END_INVISIBLE
5736 * node; Add its out to the current list (zero-width
5737 * match). */
5738 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005739 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005740 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005741 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005742 break;
5743
Bram Moolenaar87953742013-06-05 18:52:40 +02005744 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005745 {
5746 nfa_state_T *skip = NULL;
5747#ifdef ENABLE_LOG
5748 int skip_lid = 0;
5749#endif
5750
5751 /* There is no point in trying to match the pattern if the
5752 * output state is not going to be added to the list. */
5753 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5754 {
5755 skip = t->state->out1->out;
5756#ifdef ENABLE_LOG
5757 skip_lid = nextlist->id;
5758#endif
5759 }
5760 else if (state_in_list(nextlist,
5761 t->state->out1->out->out, &t->subs))
5762 {
5763 skip = t->state->out1->out->out;
5764#ifdef ENABLE_LOG
5765 skip_lid = nextlist->id;
5766#endif
5767 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005768 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005769 t->state->out1->out->out, &t->subs))
5770 {
5771 skip = t->state->out1->out->out;
5772#ifdef ENABLE_LOG
5773 skip_lid = thislist->id;
5774#endif
5775 }
5776 if (skip != NULL)
5777 {
5778#ifdef ENABLE_LOG
5779 nfa_set_code(skip->c);
5780 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5781 abs(skip->id), skip_lid, skip->c, code);
5782#endif
5783 break;
5784 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005785 /* Copy submatch info to the recursive call, opposite of what
5786 * happens afterwards. */
5787 copy_sub_off(&m->norm, &t->subs.norm);
5788#ifdef FEAT_SYN_HL
5789 if (nfa_has_zsubexpr)
5790 copy_sub_off(&m->synt, &t->subs.synt);
5791#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005792
Bram Moolenaar87953742013-06-05 18:52:40 +02005793 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005794 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005795 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005796 if (result == NFA_TOO_EXPENSIVE)
5797 {
5798 nfa_match = result;
5799 goto theend;
5800 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005801 if (result)
5802 {
5803 int bytelen;
5804
5805#ifdef ENABLE_LOG
5806 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5807 log_subsexpr(m);
5808#endif
5809 /* Copy submatch info from the recursive call */
5810 copy_sub_off(&t->subs.norm, &m->norm);
5811#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005812 if (nfa_has_zsubexpr)
5813 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005814#endif
5815 /* Now we need to skip over the matched text and then
5816 * continue with what follows. */
5817 if (REG_MULTI)
5818 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005819 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005820 - (int)(reginput - regline);
5821 else
5822 bytelen = (int)(m->norm.list.line[0].end - reginput);
5823
5824#ifdef ENABLE_LOG
5825 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5826#endif
5827 if (bytelen == 0)
5828 {
5829 /* empty match, output of corresponding
5830 * NFA_END_PATTERN/NFA_SKIP to be used at current
5831 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005832 add_here = TRUE;
5833 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005834 }
5835 else if (bytelen <= clen)
5836 {
5837 /* match current character, output of corresponding
5838 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005839 add_state = t->state->out1->out->out;
5840 add_off = clen;
5841 }
5842 else
5843 {
5844 /* skip over the matched characters, set character
5845 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005846 add_state = t->state->out1->out;
5847 add_off = bytelen;
5848 add_count = bytelen - clen;
5849 }
5850 }
5851 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005852 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005853
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005854 case NFA_BOL:
5855 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005856 {
5857 add_here = TRUE;
5858 add_state = t->state->out;
5859 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005860 break;
5861
5862 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005863 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005864 {
5865 add_here = TRUE;
5866 add_state = t->state->out;
5867 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005868 break;
5869
5870 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005871 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005872
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005873 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005874 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005875#ifdef FEAT_MBYTE
5876 else if (has_mbyte)
5877 {
5878 int this_class;
5879
5880 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005881 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005882 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005883 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005884 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005885 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005886 }
5887#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005888 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005889 || (reginput > regline
5890 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005891 result = FALSE;
5892 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005893 {
5894 add_here = TRUE;
5895 add_state = t->state->out;
5896 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005897 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005898
5899 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005900 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005901 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005902 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005903#ifdef FEAT_MBYTE
5904 else if (has_mbyte)
5905 {
5906 int this_class, prev_class;
5907
5908 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005909 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005910 prev_class = reg_prev_class();
5911 if (this_class == prev_class
5912 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005913 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005914 }
5915#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005916 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005917 || (reginput[0] != NUL
5918 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005919 result = FALSE;
5920 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005921 {
5922 add_here = TRUE;
5923 add_state = t->state->out;
5924 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005925 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005926
Bram Moolenaar4b780632013-05-31 22:14:52 +02005927 case NFA_BOF:
5928 if (reglnum == 0 && reginput == regline
5929 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005930 {
5931 add_here = TRUE;
5932 add_state = t->state->out;
5933 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005934 break;
5935
5936 case NFA_EOF:
5937 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005938 {
5939 add_here = TRUE;
5940 add_state = t->state->out;
5941 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005942 break;
5943
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005944#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005945 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005946 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005947 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005948 int len = 0;
5949 nfa_state_T *end;
5950 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005951 int cchars[MAX_MCO];
5952 int ccount = 0;
5953 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005954
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005955 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005956 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005957 if (utf_iscomposing(sta->c))
5958 {
5959 /* Only match composing character(s), ignore base
5960 * character. Used for ".{composing}" and "{composing}"
5961 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005962 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005963 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005964 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005965 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005966 /* If \Z was present, then ignore composing characters.
5967 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005968 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005969 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005970 else
5971 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005972 while (sta->c != NFA_END_COMPOSING)
5973 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005974 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005975
5976 /* Check base character matches first, unless ignored. */
5977 else if (len > 0 || mc == sta->c)
5978 {
5979 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005980 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005981 len += mb_char2len(mc);
5982 sta = sta->out;
5983 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005984
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005985 /* We don't care about the order of composing characters.
5986 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005987 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005988 {
5989 mc = mb_ptr2char(reginput + len);
5990 cchars[ccount++] = mc;
5991 len += mb_char2len(mc);
5992 if (ccount == MAX_MCO)
5993 break;
5994 }
5995
5996 /* Check that each composing char in the pattern matches a
5997 * composing char in the text. We do not check if all
5998 * composing chars are matched. */
5999 result = OK;
6000 while (sta->c != NFA_END_COMPOSING)
6001 {
6002 for (j = 0; j < ccount; ++j)
6003 if (cchars[j] == sta->c)
6004 break;
6005 if (j == ccount)
6006 {
6007 result = FAIL;
6008 break;
6009 }
6010 sta = sta->out;
6011 }
6012 }
6013 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006014 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006015
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006016 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006017 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006018 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006019 }
6020#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006021
6022 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006023 if (curc == NUL && !reg_line_lbr && REG_MULTI
6024 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006025 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006026 go_to_nextline = TRUE;
6027 /* Pass -1 for the offset, which means taking the position
6028 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006029 add_state = t->state->out;
6030 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006031 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006032 else if (curc == '\n' && reg_line_lbr)
6033 {
6034 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006035 add_state = t->state->out;
6036 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006037 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006038 break;
6039
Bram Moolenaar417bad22013-06-07 14:08:30 +02006040 case NFA_START_COLL:
6041 case NFA_START_NEG_COLL:
6042 {
6043 /* What follows is a list of characters, until NFA_END_COLL.
6044 * One of them must match or none of them must match. */
6045 nfa_state_T *state;
6046 int result_if_matched;
6047 int c1, c2;
6048
6049 /* Never match EOL. If it's part of the collection it is added
6050 * as a separate state with an OR. */
6051 if (curc == NUL)
6052 break;
6053
6054 state = t->state->out;
6055 result_if_matched = (t->state->c == NFA_START_COLL);
6056 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006057 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006058 if (state->c == NFA_END_COLL)
6059 {
6060 result = !result_if_matched;
6061 break;
6062 }
6063 if (state->c == NFA_RANGE_MIN)
6064 {
6065 c1 = state->val;
6066 state = state->out; /* advance to NFA_RANGE_MAX */
6067 c2 = state->val;
6068#ifdef ENABLE_LOG
6069 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6070 curc, c1, c2);
6071#endif
6072 if (curc >= c1 && curc <= c2)
6073 {
6074 result = result_if_matched;
6075 break;
6076 }
6077 if (ireg_ic)
6078 {
6079 int curc_low = MB_TOLOWER(curc);
6080 int done = FALSE;
6081
6082 for ( ; c1 <= c2; ++c1)
6083 if (MB_TOLOWER(c1) == curc_low)
6084 {
6085 result = result_if_matched;
6086 done = TRUE;
6087 break;
6088 }
6089 if (done)
6090 break;
6091 }
6092 }
6093 else if (state->c < 0 ? check_char_class(state->c, curc)
6094 : (curc == state->c
6095 || (ireg_ic && MB_TOLOWER(curc)
6096 == MB_TOLOWER(state->c))))
6097 {
6098 result = result_if_matched;
6099 break;
6100 }
6101 state = state->out;
6102 }
6103 if (result)
6104 {
6105 /* next state is in out of the NFA_END_COLL, out1 of
6106 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006107 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006108 add_off = clen;
6109 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006110 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006111 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006112
6113 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006114 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006115 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006116 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006117 add_state = t->state->out;
6118 add_off = clen;
6119 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006120 break;
6121
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006122 case NFA_ANY_COMPOSING:
6123 /* On a composing character skip over it. Otherwise do
6124 * nothing. Always matches. */
6125#ifdef FEAT_MBYTE
6126 if (enc_utf8 && utf_iscomposing(curc))
6127 {
6128 add_off = clen;
6129 }
6130 else
6131#endif
6132 {
6133 add_here = TRUE;
6134 add_off = 0;
6135 }
6136 add_state = t->state->out;
6137 break;
6138
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006139 /*
6140 * Character classes like \a for alpha, \d for digit etc.
6141 */
6142 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006143 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006144 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006145 break;
6146
6147 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006148 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006149 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006150 break;
6151
6152 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006153 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006154 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006155 break;
6156
6157 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006158 result = !VIM_ISDIGIT(curc)
6159 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006160 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006161 break;
6162
6163 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006164 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006165 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006166 break;
6167
6168 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006169 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006170 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171 break;
6172
6173 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006174 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006175 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006176 break;
6177
6178 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006179 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006180 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006181 break;
6182
6183 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006184 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006185 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006186 break;
6187
6188 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006189 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006190 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006191 break;
6192
6193 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006194 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006195 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006196 break;
6197
6198 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006199 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006200 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006201 break;
6202
6203 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006204 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006205 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006206 break;
6207
6208 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006209 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006210 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006211 break;
6212
6213 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006214 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006215 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006216 break;
6217
6218 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006219 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006220 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006221 break;
6222
6223 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006224 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006225 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006226 break;
6227
6228 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006229 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006230 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006231 break;
6232
6233 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006234 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006235 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006236 break;
6237
6238 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006239 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006240 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006241 break;
6242
6243 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006244 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006245 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006246 break;
6247
6248 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006249 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006250 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006251 break;
6252
6253 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006254 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006255 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006256 break;
6257
6258 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006259 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006260 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006261 break;
6262
6263 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006264 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006265 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006266 break;
6267
6268 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006269 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006270 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006271 break;
6272
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006273 case NFA_LOWER_IC: /* [a-z] */
6274 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6275 ADD_STATE_IF_MATCH(t->state);
6276 break;
6277
6278 case NFA_NLOWER_IC: /* [^a-z] */
6279 result = curc != NUL
6280 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6281 ADD_STATE_IF_MATCH(t->state);
6282 break;
6283
6284 case NFA_UPPER_IC: /* [A-Z] */
6285 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6286 ADD_STATE_IF_MATCH(t->state);
6287 break;
6288
6289 case NFA_NUPPER_IC: /* ^[A-Z] */
6290 result = curc != NUL
6291 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6292 ADD_STATE_IF_MATCH(t->state);
6293 break;
6294
Bram Moolenaar5714b802013-05-28 22:03:20 +02006295 case NFA_BACKREF1:
6296 case NFA_BACKREF2:
6297 case NFA_BACKREF3:
6298 case NFA_BACKREF4:
6299 case NFA_BACKREF5:
6300 case NFA_BACKREF6:
6301 case NFA_BACKREF7:
6302 case NFA_BACKREF8:
6303 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006304#ifdef FEAT_SYN_HL
6305 case NFA_ZREF1:
6306 case NFA_ZREF2:
6307 case NFA_ZREF3:
6308 case NFA_ZREF4:
6309 case NFA_ZREF5:
6310 case NFA_ZREF6:
6311 case NFA_ZREF7:
6312 case NFA_ZREF8:
6313 case NFA_ZREF9:
6314#endif
6315 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006316 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006317 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006318 int bytelen;
6319
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006320 if (t->state->c <= NFA_BACKREF9)
6321 {
6322 subidx = t->state->c - NFA_BACKREF1 + 1;
6323 result = match_backref(&t->subs.norm, subidx, &bytelen);
6324 }
6325#ifdef FEAT_SYN_HL
6326 else
6327 {
6328 subidx = t->state->c - NFA_ZREF1 + 1;
6329 result = match_zref(subidx, &bytelen);
6330 }
6331#endif
6332
Bram Moolenaar5714b802013-05-28 22:03:20 +02006333 if (result)
6334 {
6335 if (bytelen == 0)
6336 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006337 /* empty match always works, output of NFA_SKIP to be
6338 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006339 add_here = TRUE;
6340 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006341 }
6342 else if (bytelen <= clen)
6343 {
6344 /* match current character, jump ahead to out of
6345 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006346 add_state = t->state->out->out;
6347 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006348 }
6349 else
6350 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006351 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006352 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006353 add_state = t->state->out;
6354 add_off = bytelen;
6355 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006356 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006357 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006358 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006359 }
6360 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006361 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006362 if (t->count - clen <= 0)
6363 {
6364 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006365 add_state = t->state->out;
6366 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006367 }
6368 else
6369 {
6370 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006371 add_state = t->state;
6372 add_off = 0;
6373 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006374 }
6375 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006376
Bram Moolenaar423532e2013-05-29 21:14:42 +02006377 case NFA_LNUM:
6378 case NFA_LNUM_GT:
6379 case NFA_LNUM_LT:
6380 result = (REG_MULTI &&
6381 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6382 (long_u)(reglnum + reg_firstlnum)));
6383 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006384 {
6385 add_here = TRUE;
6386 add_state = t->state->out;
6387 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006388 break;
6389
6390 case NFA_COL:
6391 case NFA_COL_GT:
6392 case NFA_COL_LT:
6393 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6394 (long_u)(reginput - regline) + 1);
6395 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006396 {
6397 add_here = TRUE;
6398 add_state = t->state->out;
6399 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006400 break;
6401
6402 case NFA_VCOL:
6403 case NFA_VCOL_GT:
6404 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006405 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006406 int op = t->state->c - NFA_VCOL;
6407 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaaref795d12015-01-18 16:46:32 +01006408 win_T *wp = reg_win == NULL ? curwin : reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006409
6410 /* Bail out quickly when there can't be a match, avoid the
6411 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006412 if (op != 1 && col > t->state->val
6413#ifdef FEAT_MBYTE
6414 * (has_mbyte ? MB_MAXBYTES : 1)
6415#endif
6416 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006417 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006418 result = FALSE;
6419 if (op == 1 && col - 1 > t->state->val && col > 100)
6420 {
6421 int ts = wp->w_buffer->b_p_ts;
6422
6423 /* Guess that a character won't use more columns than
6424 * 'tabstop', with a minimum of 4. */
6425 if (ts < 4)
6426 ts = 4;
6427 result = col > t->state->val * ts;
6428 }
6429 if (!result)
6430 result = nfa_re_num_cmp(t->state->val, op,
6431 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006432 if (result)
6433 {
6434 add_here = TRUE;
6435 add_state = t->state->out;
6436 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006437 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006438 break;
6439
Bram Moolenaar044aa292013-06-04 21:27:38 +02006440 case NFA_MARK:
6441 case NFA_MARK_GT:
6442 case NFA_MARK_LT:
6443 {
6444 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6445
6446 /* Compare the mark position to the match position. */
6447 result = (pos != NULL /* mark doesn't exist */
6448 && pos->lnum > 0 /* mark isn't set in reg_buf */
6449 && (pos->lnum == reglnum + reg_firstlnum
6450 ? (pos->col == (colnr_T)(reginput - regline)
6451 ? t->state->c == NFA_MARK
6452 : (pos->col < (colnr_T)(reginput - regline)
6453 ? t->state->c == NFA_MARK_GT
6454 : t->state->c == NFA_MARK_LT))
6455 : (pos->lnum < reglnum + reg_firstlnum
6456 ? t->state->c == NFA_MARK_GT
6457 : t->state->c == NFA_MARK_LT)));
6458 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006459 {
6460 add_here = TRUE;
6461 add_state = t->state->out;
6462 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006463 break;
6464 }
6465
Bram Moolenaar423532e2013-05-29 21:14:42 +02006466 case NFA_CURSOR:
6467 result = (reg_win != NULL
6468 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6469 && ((colnr_T)(reginput - regline)
6470 == reg_win->w_cursor.col));
6471 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006472 {
6473 add_here = TRUE;
6474 add_state = t->state->out;
6475 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006476 break;
6477
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006478 case NFA_VISUAL:
6479 result = reg_match_visual();
6480 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006481 {
6482 add_here = TRUE;
6483 add_state = t->state->out;
6484 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006485 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006486
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006487 case NFA_MOPEN1:
6488 case NFA_MOPEN2:
6489 case NFA_MOPEN3:
6490 case NFA_MOPEN4:
6491 case NFA_MOPEN5:
6492 case NFA_MOPEN6:
6493 case NFA_MOPEN7:
6494 case NFA_MOPEN8:
6495 case NFA_MOPEN9:
6496#ifdef FEAT_SYN_HL
6497 case NFA_ZOPEN:
6498 case NFA_ZOPEN1:
6499 case NFA_ZOPEN2:
6500 case NFA_ZOPEN3:
6501 case NFA_ZOPEN4:
6502 case NFA_ZOPEN5:
6503 case NFA_ZOPEN6:
6504 case NFA_ZOPEN7:
6505 case NFA_ZOPEN8:
6506 case NFA_ZOPEN9:
6507#endif
6508 case NFA_NOPEN:
6509 case NFA_ZSTART:
6510 /* These states are only added to be able to bail out when
6511 * they are added again, nothing is to be done. */
6512 break;
6513
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006514 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006515 {
6516 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006517
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006518#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006519 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006520 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006521#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006522 result = (c == curc);
6523
6524 if (!result && ireg_ic)
6525 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006526#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006527 /* If ireg_icombine is not set only skip over the character
6528 * itself. When it is set skip over composing characters. */
6529 if (result && enc_utf8 && !ireg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006530 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006531#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006532 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006533 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006534 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006535
6536 } /* switch (t->state->c) */
6537
6538 if (add_state != NULL)
6539 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006540 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006541 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006542
6543 if (t->pim.result == NFA_PIM_UNUSED)
6544 pim = NULL;
6545 else
6546 pim = &t->pim;
6547
6548 /* Handle the postponed invisible match if the match might end
6549 * without advancing and before the end of the line. */
6550 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006551 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006552 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006553 {
6554#ifdef ENABLE_LOG
6555 fprintf(log_fd, "\n");
6556 fprintf(log_fd, "==================================\n");
6557 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6558 fprintf(log_fd, "\n");
6559#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006560 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006561 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006562 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006563 /* for \@! and \@<! it is a match when the result is
6564 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006565 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006566 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6567 || pim->state->c
6568 == NFA_START_INVISIBLE_BEFORE_NEG
6569 || pim->state->c
6570 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006571 {
6572 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006573 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006574#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006575 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006576 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006577#endif
6578 }
6579 }
6580 else
6581 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006582 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006583#ifdef ENABLE_LOG
6584 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006585 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006586 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6587 fprintf(log_fd, "\n");
6588#endif
6589 }
6590
Bram Moolenaardecd9542013-06-07 16:31:50 +02006591 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006592 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006593 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6594 || pim->state->c
6595 == NFA_START_INVISIBLE_BEFORE_NEG
6596 || pim->state->c
6597 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006598 {
6599 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006600 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006601#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006602 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006603 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006604#endif
6605 }
6606 else
6607 /* look-behind match failed, don't add the state */
6608 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006609
6610 /* Postponed invisible match was handled, don't add it to
6611 * following states. */
6612 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006613 }
6614
Bram Moolenaara951e352013-10-06 15:46:11 +02006615 /* If "pim" points into l->t it will become invalid when
6616 * adding the state causes the list to be reallocated. Make a
6617 * local copy to avoid that. */
6618 if (pim == &t->pim)
6619 {
6620 copy_pim(&pim_copy, pim);
6621 pim = &pim_copy;
6622 }
6623
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006624 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006625 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006626 else
6627 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006628 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006629 if (add_count > 0)
6630 nextlist->t[nextlist->n - 1].count = add_count;
6631 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006632 }
6633
6634 } /* for (thislist = thislist; thislist->state; thislist++) */
6635
Bram Moolenaare23febd2013-05-26 18:40:14 +02006636 /* Look for the start of a match in the current position by adding the
6637 * start state to the list of states.
6638 * The first found match is the leftmost one, thus the order of states
6639 * matters!
6640 * Do not add the start state in recursive calls of nfa_regmatch(),
6641 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006642 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006643 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006644 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006645 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006646 && reglnum == 0
6647 && clen != 0
6648 && (ireg_maxcol == 0
6649 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006650 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006651 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006652 ? (reglnum < nfa_endp->se_u.pos.lnum
6653 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006654 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006655 < nfa_endp->se_u.pos.col))
6656 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006657 {
6658#ifdef ENABLE_LOG
6659 fprintf(log_fd, "(---) STARTSTATE\n");
6660#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006661 /* Inline optimized code for addstate() if we know the state is
6662 * the first MOPEN. */
6663 if (toplevel)
6664 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006665 int add = TRUE;
6666 int c;
6667
6668 if (prog->regstart != NUL && clen != 0)
6669 {
6670 if (nextlist->n == 0)
6671 {
6672 colnr_T col = (colnr_T)(reginput - regline) + clen;
6673
6674 /* Nextlist is empty, we can skip ahead to the
6675 * character that must appear at the start. */
6676 if (skip_to_start(prog->regstart, &col) == FAIL)
6677 break;
6678#ifdef ENABLE_LOG
6679 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6680 col - ((colnr_T)(reginput - regline) + clen));
6681#endif
6682 reginput = regline + col - clen;
6683 }
6684 else
6685 {
6686 /* Checking if the required start character matches is
6687 * cheaper than adding a state that won't match. */
6688 c = PTR2CHAR(reginput + clen);
6689 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6690 != MB_TOLOWER(prog->regstart)))
6691 {
6692#ifdef ENABLE_LOG
6693 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6694#endif
6695 add = FALSE;
6696 }
6697 }
6698 }
6699
6700 if (add)
6701 {
6702 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006703 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006704 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006705 else
6706 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006707 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006708 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006709 }
6710 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006711 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006712 }
6713
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006714#ifdef ENABLE_LOG
6715 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006716 {
6717 int i;
6718
6719 for (i = 0; i < thislist->n; i++)
6720 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6721 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006722 fprintf(log_fd, "\n");
6723#endif
6724
6725nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006726 /* Advance to the next character, or advance to the next line, or
6727 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006728 if (clen != 0)
6729 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006730 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6731 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006732 reg_nextline();
6733 else
6734 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006735
6736 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006737 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006738 if (got_int)
6739 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006740#ifdef FEAT_RELTIME
6741 /* Check for timeout once in a twenty times to avoid overhead. */
6742 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6743 {
6744 nfa_time_count = 0;
6745 if (profile_passed_limit(nfa_time_limit))
6746 break;
6747 }
6748#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006749 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006750
6751#ifdef ENABLE_LOG
6752 if (log_fd != stderr)
6753 fclose(log_fd);
6754 log_fd = NULL;
6755#endif
6756
6757theend:
6758 /* Free memory */
6759 vim_free(list[0].t);
6760 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006761 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006762#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006763#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006764 fclose(debug);
6765#endif
6766
Bram Moolenaar963fee22013-05-26 21:47:28 +02006767 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006768}
6769
6770/*
6771 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006772 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006773 */
6774 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006775nfa_regtry(
6776 nfa_regprog_T *prog,
6777 colnr_T col,
6778 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006779{
6780 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006781 regsubs_T subs, m;
6782 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006783 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006784#ifdef ENABLE_LOG
6785 FILE *f;
6786#endif
6787
6788 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006789#ifdef FEAT_RELTIME
6790 nfa_time_limit = tm;
6791 nfa_time_count = 0;
6792#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006793
6794#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006795 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006796 if (f != NULL)
6797 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006798 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006799#ifdef DEBUG
6800 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6801#endif
6802 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006803 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006804 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006805 fprintf(f, "\n\n");
6806 fclose(f);
6807 }
6808 else
6809 EMSG(_("Could not open temporary log file for writing "));
6810#endif
6811
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006812 clear_sub(&subs.norm);
6813 clear_sub(&m.norm);
6814#ifdef FEAT_SYN_HL
6815 clear_sub(&subs.synt);
6816 clear_sub(&m.synt);
6817#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006818
Bram Moolenaarfda37292014-11-05 14:27:36 +01006819 result = nfa_regmatch(prog, start, &subs, &m);
6820 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006821 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006822 else if (result == NFA_TOO_EXPENSIVE)
6823 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006824
6825 cleanup_subexpr();
6826 if (REG_MULTI)
6827 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006828 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006829 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006830 reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6831 reg_startpos[i].col = subs.norm.list.multi[i].start_col;
6832
6833 reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6834 reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006835 }
6836
6837 if (reg_startpos[0].lnum < 0)
6838 {
6839 reg_startpos[0].lnum = 0;
6840 reg_startpos[0].col = col;
6841 }
6842 if (reg_endpos[0].lnum < 0)
6843 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006844 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006845 reg_endpos[0].lnum = reglnum;
6846 reg_endpos[0].col = (int)(reginput - regline);
6847 }
6848 else
6849 /* Use line number of "\ze". */
6850 reglnum = reg_endpos[0].lnum;
6851 }
6852 else
6853 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006854 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006855 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006856 reg_startp[i] = subs.norm.list.line[i].start;
6857 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006858 }
6859
6860 if (reg_startp[0] == NULL)
6861 reg_startp[0] = regline + col;
6862 if (reg_endp[0] == NULL)
6863 reg_endp[0] = reginput;
6864 }
6865
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006866#ifdef FEAT_SYN_HL
6867 /* Package any found \z(...\) matches for export. Default is none. */
6868 unref_extmatch(re_extmatch_out);
6869 re_extmatch_out = NULL;
6870
6871 if (prog->reghasz == REX_SET)
6872 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006873 cleanup_zsubexpr();
6874 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01006875 /* Loop over \z1, \z2, etc. There is no \z0. */
6876 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006877 {
6878 if (REG_MULTI)
6879 {
6880 struct multipos *mpos = &subs.synt.list.multi[i];
6881
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006882 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006883 if (mpos->start_lnum >= 0
6884 && mpos->start_lnum == mpos->end_lnum
6885 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006886 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006887 vim_strnsave(reg_getline(mpos->start_lnum)
6888 + mpos->start_col,
6889 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006890 }
6891 else
6892 {
6893 struct linepos *lpos = &subs.synt.list.line[i];
6894
6895 if (lpos->start != NULL && lpos->end != NULL)
6896 re_extmatch_out->matches[i] =
6897 vim_strnsave(lpos->start,
6898 (int)(lpos->end - lpos->start));
6899 }
6900 }
6901 }
6902#endif
6903
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006904 return 1 + reglnum;
6905}
6906
6907/*
6908 * Match a regexp against a string ("line" points to the string) or multiple
6909 * lines ("line" is NULL, use reg_getline()).
6910 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01006911 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006912 */
6913 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006914nfa_regexec_both(
6915 char_u *line,
6916 colnr_T startcol, /* column to start looking for match */
6917 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006918{
6919 nfa_regprog_T *prog;
6920 long retval = 0L;
6921 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006922 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006923
6924 if (REG_MULTI)
6925 {
6926 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6927 line = reg_getline((linenr_T)0); /* relative to the cursor */
6928 reg_startpos = reg_mmatch->startpos;
6929 reg_endpos = reg_mmatch->endpos;
6930 }
6931 else
6932 {
6933 prog = (nfa_regprog_T *)reg_match->regprog;
6934 reg_startp = reg_match->startp;
6935 reg_endp = reg_match->endp;
6936 }
6937
6938 /* Be paranoid... */
6939 if (prog == NULL || line == NULL)
6940 {
6941 EMSG(_(e_null));
6942 goto theend;
6943 }
6944
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006945 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6946 if (prog->regflags & RF_ICASE)
6947 ireg_ic = TRUE;
6948 else if (prog->regflags & RF_NOICASE)
6949 ireg_ic = FALSE;
6950
6951#ifdef FEAT_MBYTE
6952 /* If pattern contains "\Z" overrule value of ireg_icombine */
6953 if (prog->regflags & RF_ICOMBINE)
6954 ireg_icombine = TRUE;
6955#endif
6956
6957 regline = line;
6958 reglnum = 0; /* relative to line */
6959
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006960 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006961 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006962 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006963 nfa_listid = 1;
6964 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006965 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006966
Bram Moolenaard89616e2013-06-06 18:46:06 +02006967 if (prog->reganch && col > 0)
6968 return 0L;
6969
Bram Moolenaar473de612013-06-08 18:19:48 +02006970 need_clear_subexpr = TRUE;
6971#ifdef FEAT_SYN_HL
6972 /* Clear the external match subpointers if necessary. */
6973 if (prog->reghasz == REX_SET)
6974 {
6975 nfa_has_zsubexpr = TRUE;
6976 need_clear_zsubexpr = TRUE;
6977 }
6978 else
6979 nfa_has_zsubexpr = FALSE;
6980#endif
6981
Bram Moolenaard89616e2013-06-06 18:46:06 +02006982 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006983 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006984 /* Skip ahead until a character we know the match must start with.
6985 * When there is none there is no match. */
6986 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006987 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006988
Bram Moolenaar473de612013-06-08 18:19:48 +02006989 /* If match_text is set it contains the full text that must match.
6990 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006991 if (prog->match_text != NULL
6992#ifdef FEAT_MBYTE
6993 && !ireg_icombine
6994#endif
6995 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006996 return find_match_text(col, prog->regstart, prog->match_text);
6997 }
6998
Bram Moolenaard89616e2013-06-06 18:46:06 +02006999 /* If the start column is past the maximum column: no need to try. */
7000 if (ireg_maxcol > 0 && col >= ireg_maxcol)
7001 goto theend;
7002
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007003 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007004 for (i = 0; i < nstate; ++i)
7005 {
7006 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007007 prog->state[i].lastlist[0] = 0;
7008 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007009 }
7010
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007011 retval = nfa_regtry(prog, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007012
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007013 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007014
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007015theend:
7016 return retval;
7017}
7018
7019/*
7020 * Compile a regular expression into internal code for the NFA matcher.
7021 * Returns the program in allocated space. Returns NULL for an error.
7022 */
7023 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007024nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007025{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007026 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007027 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028 int *postfix;
7029
7030 if (expr == NULL)
7031 return NULL;
7032
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007033 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007034 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007035
7036 init_class_tab();
7037
7038 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7039 return NULL;
7040
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007041 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007042 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007043 postfix = re2post();
7044 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007045 {
7046 /* TODO: only give this error for debugging? */
7047 if (post_ptr >= post_end)
7048 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007049 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007050 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007051
7052 /*
7053 * In order to build the NFA, we parse the input regexp twice:
7054 * 1. first pass to count size (so we can allocate space)
7055 * 2. second to emit code
7056 */
7057#ifdef ENABLE_LOG
7058 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007059 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007060
7061 if (f != NULL)
7062 {
7063 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7064 fclose(f);
7065 }
7066 }
7067#endif
7068
7069 /*
7070 * PASS 1
7071 * Count number of NFA states in "nstate". Do not build the NFA.
7072 */
7073 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007074
Bram Moolenaar16619a22013-06-11 18:42:36 +02007075 /* allocate the regprog with space for the compiled regexp */
7076 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007077 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7078 if (prog == NULL)
7079 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007080 state_ptr = prog->state;
7081
7082 /*
7083 * PASS 2
7084 * Build the NFA
7085 */
7086 prog->start = post2nfa(postfix, post_ptr, FALSE);
7087 if (prog->start == NULL)
7088 goto fail;
7089
7090 prog->regflags = regflags;
7091 prog->engine = &nfa_regengine;
7092 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007093 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007094 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007095 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007096
Bram Moolenaara2947e22013-06-11 22:44:09 +02007097 nfa_postprocess(prog);
7098
Bram Moolenaard89616e2013-06-06 18:46:06 +02007099 prog->reganch = nfa_get_reganch(prog->start, 0);
7100 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007101 prog->match_text = nfa_get_match_text(prog->start);
7102
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007103#ifdef ENABLE_LOG
7104 nfa_postfix_dump(expr, OK);
7105 nfa_dump(prog);
7106#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007107#ifdef FEAT_SYN_HL
7108 /* Remember whether this pattern has any \z specials in it. */
7109 prog->reghasz = re_has_z;
7110#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007111 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007112 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007113
7114out:
7115 vim_free(post_start);
7116 post_start = post_ptr = post_end = NULL;
7117 state_ptr = NULL;
7118 return (regprog_T *)prog;
7119
7120fail:
7121 vim_free(prog);
7122 prog = NULL;
7123#ifdef ENABLE_LOG
7124 nfa_postfix_dump(expr, FAIL);
7125#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007126 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007127 goto out;
7128}
7129
Bram Moolenaar473de612013-06-08 18:19:48 +02007130/*
7131 * Free a compiled regexp program, returned by nfa_regcomp().
7132 */
7133 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007134nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007135{
7136 if (prog != NULL)
7137 {
7138 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007139 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007140 vim_free(prog);
7141 }
7142}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007143
7144/*
7145 * Match a regexp against a string.
7146 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7147 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007148 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007149 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007150 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151 */
7152 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007153nfa_regexec_nl(
7154 regmatch_T *rmp,
7155 char_u *line, /* string to match against */
7156 colnr_T col, /* column to start looking for match */
7157 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007158{
7159 reg_match = rmp;
7160 reg_mmatch = NULL;
7161 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007162 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007163 reg_buf = curbuf;
7164 reg_win = NULL;
7165 ireg_ic = rmp->rm_ic;
7166#ifdef FEAT_MBYTE
7167 ireg_icombine = FALSE;
7168#endif
7169 ireg_maxcol = 0;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007170 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007171}
7172
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007173
7174/*
7175 * Match a regexp against multiple lines.
7176 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7177 * Uses curbuf for line count and 'iskeyword'.
7178 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007179 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007180 * match otherwise.
7181 *
7182 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7183 *
7184 * ! Also NOTE : match may actually be in another line. e.g.:
7185 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7186 *
7187 * +-------------------------+
7188 * |a |
7189 * |b |
7190 * |c |
7191 * | |
7192 * +-------------------------+
7193 *
7194 * then nfa_regexec_multi() returns 3. while the original
7195 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7196 *
7197 * FIXME if this behavior is not compatible.
7198 */
7199 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007200nfa_regexec_multi(
7201 regmmatch_T *rmp,
7202 win_T *win, /* window in which to search or NULL */
7203 buf_T *buf, /* buffer in which to search */
7204 linenr_T lnum, /* nr of line to start looking for match */
7205 colnr_T col, /* column to start looking for match */
7206 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007207{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208 reg_match = NULL;
7209 reg_mmatch = rmp;
7210 reg_buf = buf;
7211 reg_win = win;
7212 reg_firstlnum = lnum;
7213 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7214 reg_line_lbr = FALSE;
7215 ireg_ic = rmp->rmm_ic;
7216#ifdef FEAT_MBYTE
7217 ireg_icombine = FALSE;
7218#endif
7219 ireg_maxcol = rmp->rmm_maxcol;
7220
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007221 return nfa_regexec_both(NULL, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007222}
7223
7224#ifdef DEBUG
7225# undef ENABLE_LOG
7226#endif