blob: 865d67fdfe57aff51a215c9bfdf2217777a728e4 [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 Moolenaar7c29f382016-02-12 19:08:15 +01001131 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001133 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001134 switch (c)
1135 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001136 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001137 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001138
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139 case Magic('^'):
1140 EMIT(NFA_BOL);
1141 break;
1142
1143 case Magic('$'):
1144 EMIT(NFA_EOL);
1145#if defined(FEAT_SYN_HL) || defined(PROTO)
1146 had_eol = TRUE;
1147#endif
1148 break;
1149
1150 case Magic('<'):
1151 EMIT(NFA_BOW);
1152 break;
1153
1154 case Magic('>'):
1155 EMIT(NFA_EOW);
1156 break;
1157
1158 case Magic('_'):
1159 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001160 if (c == NUL)
1161 EMSG_RET_FAIL(_(e_nul_found));
1162
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001163 if (c == '^') /* "\_^" is start-of-line */
1164 {
1165 EMIT(NFA_BOL);
1166 break;
1167 }
1168 if (c == '$') /* "\_$" is end-of-line */
1169 {
1170 EMIT(NFA_EOL);
1171#if defined(FEAT_SYN_HL) || defined(PROTO)
1172 had_eol = TRUE;
1173#endif
1174 break;
1175 }
1176
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001177 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001178
1179 /* "\_[" is collection plus newline */
1180 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001181 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001182
1183 /* "\_x" is character class plus newline */
1184 /*FALLTHROUGH*/
1185
1186 /*
1187 * Character classes.
1188 */
1189 case Magic('.'):
1190 case Magic('i'):
1191 case Magic('I'):
1192 case Magic('k'):
1193 case Magic('K'):
1194 case Magic('f'):
1195 case Magic('F'):
1196 case Magic('p'):
1197 case Magic('P'):
1198 case Magic('s'):
1199 case Magic('S'):
1200 case Magic('d'):
1201 case Magic('D'):
1202 case Magic('x'):
1203 case Magic('X'):
1204 case Magic('o'):
1205 case Magic('O'):
1206 case Magic('w'):
1207 case Magic('W'):
1208 case Magic('h'):
1209 case Magic('H'):
1210 case Magic('a'):
1211 case Magic('A'):
1212 case Magic('l'):
1213 case Magic('L'):
1214 case Magic('u'):
1215 case Magic('U'):
1216 p = vim_strchr(classchars, no_Magic(c));
1217 if (p == NULL)
1218 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001219 if (extra == NFA_ADD_NL)
1220 {
1221 EMSGN(_(e_ill_char_class), c);
1222 rc_did_emsg = TRUE;
1223 return FAIL;
1224 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001225 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1226 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 }
1228#ifdef FEAT_MBYTE
1229 /* When '.' is followed by a composing char ignore the dot, so that
1230 * the composing char is matched here. */
1231 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1232 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001233 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 c = getchr();
1235 goto nfa_do_multibyte;
1236 }
1237#endif
1238 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001239 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001240 {
1241 EMIT(NFA_NEWL);
1242 EMIT(NFA_OR);
1243 regflags |= RF_HASNL;
1244 }
1245 break;
1246
1247 case Magic('n'):
1248 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001249 /* In a string "\n" matches a newline character. */
1250 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 else
1252 {
1253 /* In buffer text "\n" matches the end of a line. */
1254 EMIT(NFA_NEWL);
1255 regflags |= RF_HASNL;
1256 }
1257 break;
1258
1259 case Magic('('):
1260 if (nfa_reg(REG_PAREN) == FAIL)
1261 return FAIL; /* cascaded error */
1262 break;
1263
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 case Magic('|'):
1265 case Magic('&'):
1266 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001267 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001268 return FAIL;
1269
1270 case Magic('='):
1271 case Magic('?'):
1272 case Magic('+'):
1273 case Magic('@'):
1274 case Magic('*'):
1275 case Magic('{'):
1276 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001277 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001278 return FAIL;
1279
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001280 case Magic('~'):
1281 {
1282 char_u *lp;
1283
1284 /* Previous substitute pattern.
1285 * Generated as "\%(pattern\)". */
1286 if (reg_prev_sub == NULL)
1287 {
1288 EMSG(_(e_nopresub));
1289 return FAIL;
1290 }
1291 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1292 {
1293 EMIT(PTR2CHAR(lp));
1294 if (lp != reg_prev_sub)
1295 EMIT(NFA_CONCAT);
1296 }
1297 EMIT(NFA_NOPEN);
1298 break;
1299 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001300
Bram Moolenaar428e9872013-05-30 17:05:39 +02001301 case Magic('1'):
1302 case Magic('2'):
1303 case Magic('3'):
1304 case Magic('4'):
1305 case Magic('5'):
1306 case Magic('6'):
1307 case Magic('7'):
1308 case Magic('8'):
1309 case Magic('9'):
1310 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1311 nfa_has_backref = TRUE;
1312 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001313
1314 case Magic('z'):
1315 c = no_Magic(getchr());
1316 switch (c)
1317 {
1318 case 's':
1319 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001320 if (re_mult_next("\\zs") == FAIL)
1321 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001322 break;
1323 case 'e':
1324 EMIT(NFA_ZEND);
1325 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001326 if (re_mult_next("\\ze") == FAIL)
1327 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001328 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001329#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001330 case '1':
1331 case '2':
1332 case '3':
1333 case '4':
1334 case '5':
1335 case '6':
1336 case '7':
1337 case '8':
1338 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001339 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001340 if (reg_do_extmatch != REX_USE)
1341 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001342 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1343 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001344 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001345 re_has_z = REX_USE;
1346 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001347 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001348 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001349 if (reg_do_extmatch != REX_SET)
1350 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001351 if (nfa_reg(REG_ZPAREN) == FAIL)
1352 return FAIL; /* cascaded error */
1353 re_has_z = REX_SET;
1354 break;
1355#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001356 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001357 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 no_Magic(c));
1359 return FAIL;
1360 }
1361 break;
1362
1363 case Magic('%'):
1364 c = no_Magic(getchr());
1365 switch (c)
1366 {
1367 /* () without a back reference */
1368 case '(':
1369 if (nfa_reg(REG_NPAREN) == FAIL)
1370 return FAIL;
1371 EMIT(NFA_NOPEN);
1372 break;
1373
1374 case 'd': /* %d123 decimal */
1375 case 'o': /* %o123 octal */
1376 case 'x': /* %xab hex 2 */
1377 case 'u': /* %uabcd hex 4 */
1378 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001379 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001380 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001381
Bram Moolenaar47196582013-05-25 22:04:23 +02001382 switch (c)
1383 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001384 case 'd': nr = getdecchrs(); break;
1385 case 'o': nr = getoctchrs(); break;
1386 case 'x': nr = gethexchrs(2); break;
1387 case 'u': nr = gethexchrs(4); break;
1388 case 'U': nr = gethexchrs(8); break;
1389 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001390 }
1391
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001392 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001393 EMSG2_RET_FAIL(
1394 _("E678: Invalid character after %s%%[dxouU]"),
1395 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001396 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001397 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001398 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001399 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001400 break;
1401
1402 /* Catch \%^ and \%$ regardless of where they appear in the
1403 * pattern -- regardless of whether or not it makes sense. */
1404 case '^':
1405 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 break;
1407
1408 case '$':
1409 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001410 break;
1411
1412 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001413 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001414 break;
1415
1416 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001417 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001418 break;
1419
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001420 case 'C':
1421 EMIT(NFA_ANY_COMPOSING);
1422 break;
1423
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001424 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001425 {
1426 int n;
1427
1428 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001429 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001430 {
1431 if (c == NUL)
1432 EMSG2_RET_FAIL(_(e_missing_sb),
1433 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001434 /* recursive call! */
1435 if (nfa_regatom() == FAIL)
1436 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001437 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001438 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001439 if (n == 0)
1440 EMSG2_RET_FAIL(_(e_empty_sb),
1441 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001442 EMIT(NFA_OPT_CHARS);
1443 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001444
1445 /* Emit as "\%(\%[abc]\)" to be able to handle
1446 * "\%[abc]*" which would cause the empty string to be
1447 * matched an unlimited number of times. NFA_NOPEN is
1448 * added only once at a position, while NFA_SPLIT is
1449 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001450 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001451 * a lot. */
1452 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001453 break;
1454 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001456 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001457 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001458 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001459 int cmp = c;
1460
1461 if (c == '<' || c == '>')
1462 c = getchr();
1463 while (VIM_ISDIGIT(c))
1464 {
1465 n = n * 10 + (c - '0');
1466 c = getchr();
1467 }
1468 if (c == 'l' || c == 'c' || c == 'v')
1469 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001470 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001471 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001472 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001473 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001474 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001475 if (save_prev_at_start)
1476 at_start = TRUE;
1477 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001478 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001479 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001480 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001481 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001482 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001483 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001484 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001485 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001486 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001487 break;
1488 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001489 else if (c == '\'' && n == 0)
1490 {
1491 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001492 EMIT(cmp == '<' ? NFA_MARK_LT :
1493 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001494 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001495 break;
1496 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001497 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001498 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1499 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 return FAIL;
1501 }
1502 break;
1503
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001505collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001506 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001507 * [abc] uses NFA_START_COLL - NFA_END_COLL
1508 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1509 * Each character is produced as a regular state, using
1510 * NFA_CONCAT to bind them together.
1511 * Besides normal characters there can be:
1512 * - character classes NFA_CLASS_*
1513 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001514 */
1515
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516 p = regparse;
1517 endp = skip_anyof(p);
1518 if (*endp == ']')
1519 {
1520 /*
1521 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001522 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001523 * and perform the necessary substitutions in the NFA.
1524 */
1525 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001526 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001527 if (result != FAIL)
1528 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001529 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001531 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 EMIT(NFA_NEWL);
1533 EMIT(NFA_OR);
1534 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001535 else
1536 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001538 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 return OK;
1540 }
1541 /*
1542 * Failed to recognize a character class. Use the simple
1543 * version that turns [abc] into 'a' OR 'b' OR 'c'
1544 */
1545 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001546 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001547 if (*regparse == '^') /* negated range */
1548 {
1549 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001550 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001551 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001552 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001553 else
1554 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 if (*regparse == '-')
1556 {
1557 startc = '-';
1558 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001559 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001560 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561 }
1562 /* Emit the OR branches for each character in the [] */
1563 emit_range = FALSE;
1564 while (regparse < endp)
1565 {
1566 oldstartc = startc;
1567 startc = -1;
1568 got_coll_char = FALSE;
1569 if (*regparse == '[')
1570 {
1571 /* Check for [: :], [= =], [. .] */
1572 equiclass = collclass = 0;
1573 charclass = get_char_class(&regparse);
1574 if (charclass == CLASS_NONE)
1575 {
1576 equiclass = get_equi_class(&regparse);
1577 if (equiclass == 0)
1578 collclass = get_coll_element(&regparse);
1579 }
1580
1581 /* Character class like [:alpha:] */
1582 if (charclass != CLASS_NONE)
1583 {
1584 switch (charclass)
1585 {
1586 case CLASS_ALNUM:
1587 EMIT(NFA_CLASS_ALNUM);
1588 break;
1589 case CLASS_ALPHA:
1590 EMIT(NFA_CLASS_ALPHA);
1591 break;
1592 case CLASS_BLANK:
1593 EMIT(NFA_CLASS_BLANK);
1594 break;
1595 case CLASS_CNTRL:
1596 EMIT(NFA_CLASS_CNTRL);
1597 break;
1598 case CLASS_DIGIT:
1599 EMIT(NFA_CLASS_DIGIT);
1600 break;
1601 case CLASS_GRAPH:
1602 EMIT(NFA_CLASS_GRAPH);
1603 break;
1604 case CLASS_LOWER:
1605 EMIT(NFA_CLASS_LOWER);
1606 break;
1607 case CLASS_PRINT:
1608 EMIT(NFA_CLASS_PRINT);
1609 break;
1610 case CLASS_PUNCT:
1611 EMIT(NFA_CLASS_PUNCT);
1612 break;
1613 case CLASS_SPACE:
1614 EMIT(NFA_CLASS_SPACE);
1615 break;
1616 case CLASS_UPPER:
1617 EMIT(NFA_CLASS_UPPER);
1618 break;
1619 case CLASS_XDIGIT:
1620 EMIT(NFA_CLASS_XDIGIT);
1621 break;
1622 case CLASS_TAB:
1623 EMIT(NFA_CLASS_TAB);
1624 break;
1625 case CLASS_RETURN:
1626 EMIT(NFA_CLASS_RETURN);
1627 break;
1628 case CLASS_BACKSPACE:
1629 EMIT(NFA_CLASS_BACKSPACE);
1630 break;
1631 case CLASS_ESCAPE:
1632 EMIT(NFA_CLASS_ESCAPE);
1633 break;
1634 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001635 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001636 continue;
1637 }
1638 /* Try equivalence class [=a=] and the like */
1639 if (equiclass != 0)
1640 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001641 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 if (result == FAIL)
1643 {
1644 /* should never happen */
1645 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1646 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001647 continue;
1648 }
1649 /* Try collating class like [. .] */
1650 if (collclass != 0)
1651 {
1652 startc = collclass; /* allow [.a.]-x as a range */
1653 /* Will emit the proper atom at the end of the
1654 * while loop. */
1655 }
1656 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001657 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1658 * start character. */
1659 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001660 {
1661 emit_range = TRUE;
1662 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001663 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001664 continue; /* reading the end of the range */
1665 }
1666
1667 /* Now handle simple and escaped characters.
1668 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1669 * accepts "\t", "\e", etc., but only when the 'l' flag in
1670 * 'cpoptions' is not included.
1671 * Posix doesn't recognize backslash at all.
1672 */
1673 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001674 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001675 && regparse + 1 <= endp
1676 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001677 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 && vim_strchr(REGEXP_ABBR, regparse[1])
1679 != NULL)
1680 )
1681 )
1682 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001683 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001685 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001686 startc = reg_string ? NL : NFA_NEWL;
1687 else
1688 if (*regparse == 'd'
1689 || *regparse == 'o'
1690 || *regparse == 'x'
1691 || *regparse == 'u'
1692 || *regparse == 'U'
1693 )
1694 {
1695 /* TODO(RE) This needs more testing */
1696 startc = coll_get_char();
1697 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001698 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001699 }
1700 else
1701 {
1702 /* \r,\t,\e,\b */
1703 startc = backslash_trans(*regparse);
1704 }
1705 }
1706
1707 /* Normal printable char */
1708 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001709 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001710
1711 /* Previous char was '-', so this char is end of range. */
1712 if (emit_range)
1713 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001714 endc = startc;
1715 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001716 if (startc > endc)
1717 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001718
1719 if (endc > startc + 2)
1720 {
1721 /* Emit a range instead of the sequence of
1722 * individual characters. */
1723 if (startc == 0)
1724 /* \x00 is translated to \x0a, start at \x01. */
1725 EMIT(1);
1726 else
1727 --post_ptr; /* remove NFA_CONCAT */
1728 EMIT(endc);
1729 EMIT(NFA_RANGE);
1730 EMIT(NFA_CONCAT);
1731 }
1732 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001733#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001734 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735 || (*mb_char2len)(endc) > 1))
1736 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001737 /* Emit the characters in the range.
1738 * "startc" was already emitted, so skip it.
1739 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740 for (c = startc + 1; c <= endc; c++)
1741 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001742 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001743 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745 }
1746 else
1747#endif
1748 {
1749#ifdef EBCDIC
1750 int alpha_only = FALSE;
1751
1752 /* for alphabetical range skip the gaps
1753 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1754 if (isalpha(startc) && isalpha(endc))
1755 alpha_only = TRUE;
1756#endif
1757 /* Emit the range. "startc" was already emitted, so
1758 * skip it. */
1759 for (c = startc + 1; c <= endc; c++)
1760#ifdef EBCDIC
1761 if (!alpha_only || isalpha(startc))
1762#endif
1763 {
1764 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001765 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001767 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001768 emit_range = FALSE;
1769 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001770 }
1771 else
1772 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001773 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001774 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001775 * Normally, simply emit startc. But if we get char
1776 * code=0 from a collating char, then replace it with
1777 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001779 * the backtracking engine. */
1780 if (startc == NFA_NEWL)
1781 {
1782 /* Line break can't be matched as part of the
1783 * collection, add an OR below. But not for negated
1784 * range. */
1785 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001786 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001787 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001789 {
1790 if (got_coll_char == TRUE && startc == 0)
1791 EMIT(0x0a);
1792 else
1793 EMIT(startc);
1794 EMIT(NFA_CONCAT);
1795 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001796 }
1797
Bram Moolenaar51a29832013-05-28 22:30:35 +02001798 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 } /* while (p < endp) */
1800
Bram Moolenaar51a29832013-05-28 22:30:35 +02001801 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001802 if (*regparse == '-') /* if last, '-' is just a char */
1803 {
1804 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001805 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001806 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001807
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001808 /* skip the trailing ] */
1809 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001810 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001811
1812 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001813 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001814 EMIT(NFA_END_NEG_COLL);
1815 else
1816 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001817
1818 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001819 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001820 {
1821 EMIT(reg_string ? NL : NFA_NEWL);
1822 EMIT(NFA_OR);
1823 }
1824
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001825 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001826 } /* if exists closing ] */
1827
1828 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001830 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832 default:
1833 {
1834#ifdef FEAT_MBYTE
1835 int plen;
1836
1837nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001838 /* plen is length of current char with composing chars */
1839 if (enc_utf8 && ((*mb_char2len)(c)
1840 != (plen = (*mb_ptr2len)(old_regparse))
1841 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001842 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001843 int i = 0;
1844
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001845 /* A base character plus composing characters, or just one
1846 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001847 * This requires creating a separate atom as if enclosing
1848 * the characters in (), where NFA_COMPOSING is the ( and
1849 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001850 * building the postfix form, not the NFA itself;
1851 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001852 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001853 for (;;)
1854 {
1855 EMIT(c);
1856 if (i > 0)
1857 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001858 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001859 break;
1860 c = utf_ptr2char(old_regparse + i);
1861 }
1862 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001863 regparse = old_regparse + plen;
1864 }
1865 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866#endif
1867 {
1868 c = no_Magic(c);
1869 EMIT(c);
1870 }
1871 return OK;
1872 }
1873 }
1874
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001875 return OK;
1876}
1877
1878/*
1879 * Parse something followed by possible [*+=].
1880 *
1881 * A piece is an atom, possibly followed by a multi, an indication of how many
1882 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1883 * characters: "", "a", "aa", etc.
1884 *
1885 * piece ::= atom
1886 * or atom multi
1887 */
1888 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001889nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890{
1891 int i;
1892 int op;
1893 int ret;
1894 long minval, maxval;
1895 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001896 parse_state_T old_state;
1897 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001899 int old_post_pos;
1900 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 int quest;
1902
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001903 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1904 * next. */
1905 save_parse_state(&old_state);
1906
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001908 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909
1910 ret = nfa_regatom();
1911 if (ret == FAIL)
1912 return FAIL; /* cascaded error */
1913
1914 op = peekchr();
1915 if (re_multi_type(op) == NOT_MULTI)
1916 return OK;
1917
1918 skipchr();
1919 switch (op)
1920 {
1921 case Magic('*'):
1922 EMIT(NFA_STAR);
1923 break;
1924
1925 case Magic('+'):
1926 /*
1927 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1928 * first and only submatch would be "aaa". But the backtracking
1929 * engine interprets the plus as "try matching one more time", and
1930 * a* matches a second time at the end of the input, the empty
1931 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001932 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001934 * In order to be consistent with the old engine, we replace
1935 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001937 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938 curchr = -1;
1939 if (nfa_regatom() == FAIL)
1940 return FAIL;
1941 EMIT(NFA_STAR);
1942 EMIT(NFA_CONCAT);
1943 skipchr(); /* skip the \+ */
1944 break;
1945
1946 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001947 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001949 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950 switch(op)
1951 {
1952 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001953 /* \@= */
1954 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001955 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001956 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001957 /* \@! */
1958 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001959 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001961 op = no_Magic(getchr());
1962 if (op == '=')
1963 /* \@<= */
1964 i = NFA_PREV_ATOM_JUST_BEFORE;
1965 else if (op == '!')
1966 /* \@<! */
1967 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1968 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001969 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001970 /* \@> */
1971 i = NFA_PREV_ATOM_LIKE_PATTERN;
1972 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001973 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001974 if (i == 0)
1975 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001976 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1977 return FAIL;
1978 }
1979 EMIT(i);
1980 if (i == NFA_PREV_ATOM_JUST_BEFORE
1981 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1982 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983 break;
1984
1985 case Magic('?'):
1986 case Magic('='):
1987 EMIT(NFA_QUEST);
1988 break;
1989
1990 case Magic('{'):
1991 /* a{2,5} will expand to 'aaa?a?a?'
1992 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1993 * version of '?'
1994 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1995 * parenthesis have the same id
1996 */
1997
1998 greedy = TRUE;
1999 c2 = peekchr();
2000 if (c2 == '-' || c2 == Magic('-'))
2001 {
2002 skipchr();
2003 greedy = FALSE;
2004 }
2005 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002006 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002007
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2009 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002010 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002012 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002013 /* \{}, \{0,} */
2014 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002015 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002016 /* \{-}, \{-0,} */
2017 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018 break;
2019 }
2020
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002021 /* Special case: x{0} or x{-0} */
2022 if (maxval == 0)
2023 {
2024 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002025 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002026 /* NFA_EMPTY is 0-length and works everywhere */
2027 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002028 return OK;
2029 }
2030
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002031 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002032 * maximum is much larger than the minimum and when the maximum is
2033 * large. Bail out if we can use the other engine. */
2034 if ((nfa_re_flags & RE_AUTO)
2035 && (maxval > minval + 200 || maxval > 500))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002036 return FAIL;
2037
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002039 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002040 /* Save parse state after the repeated atom and the \{} */
2041 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2044 for (i = 0; i < maxval; i++)
2045 {
2046 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002047 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002048 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 if (nfa_regatom() == FAIL)
2050 return FAIL;
2051 /* after "minval" times, atoms are optional */
2052 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002053 {
2054 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002055 {
2056 if (greedy)
2057 EMIT(NFA_STAR);
2058 else
2059 EMIT(NFA_STAR_NONGREEDY);
2060 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002061 else
2062 EMIT(quest);
2063 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002064 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002066 if (i + 1 > minval && maxval == MAX_LIMIT)
2067 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 }
2069
2070 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002071 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 curchr = -1;
2073
2074 break;
2075
2076
2077 default:
2078 break;
2079 } /* end switch */
2080
2081 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002084
2085 return OK;
2086}
2087
2088/*
2089 * Parse one or more pieces, concatenated. It matches a match for the
2090 * first piece, followed by a match for the second piece, etc. Example:
2091 * "f[0-9]b", first matches "f", then a digit and then "b".
2092 *
2093 * concat ::= piece
2094 * or piece piece
2095 * or piece piece piece
2096 * etc.
2097 */
2098 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002099nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100{
2101 int cont = TRUE;
2102 int first = TRUE;
2103
2104 while (cont)
2105 {
2106 switch (peekchr())
2107 {
2108 case NUL:
2109 case Magic('|'):
2110 case Magic('&'):
2111 case Magic(')'):
2112 cont = FALSE;
2113 break;
2114
2115 case Magic('Z'):
2116#ifdef FEAT_MBYTE
2117 regflags |= RF_ICOMBINE;
2118#endif
2119 skipchr_keepstart();
2120 break;
2121 case Magic('c'):
2122 regflags |= RF_ICASE;
2123 skipchr_keepstart();
2124 break;
2125 case Magic('C'):
2126 regflags |= RF_NOICASE;
2127 skipchr_keepstart();
2128 break;
2129 case Magic('v'):
2130 reg_magic = MAGIC_ALL;
2131 skipchr_keepstart();
2132 curchr = -1;
2133 break;
2134 case Magic('m'):
2135 reg_magic = MAGIC_ON;
2136 skipchr_keepstart();
2137 curchr = -1;
2138 break;
2139 case Magic('M'):
2140 reg_magic = MAGIC_OFF;
2141 skipchr_keepstart();
2142 curchr = -1;
2143 break;
2144 case Magic('V'):
2145 reg_magic = MAGIC_NONE;
2146 skipchr_keepstart();
2147 curchr = -1;
2148 break;
2149
2150 default:
2151 if (nfa_regpiece() == FAIL)
2152 return FAIL;
2153 if (first == FALSE)
2154 EMIT(NFA_CONCAT);
2155 else
2156 first = FALSE;
2157 break;
2158 }
2159 }
2160
2161 return OK;
2162}
2163
2164/*
2165 * Parse a branch, one or more concats, separated by "\&". It matches the
2166 * last concat, but only if all the preceding concats also match at the same
2167 * position. Examples:
2168 * "foobeep\&..." matches "foo" in "foobeep".
2169 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2170 *
2171 * branch ::= concat
2172 * or concat \& concat
2173 * or concat \& concat \& concat
2174 * etc.
2175 */
2176 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002177nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002178{
2179 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002180 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181
Bram Moolenaar16299b52013-05-30 18:45:23 +02002182 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002183
2184 /* First branch, possibly the only one */
2185 if (nfa_regconcat() == FAIL)
2186 return FAIL;
2187
2188 ch = peekchr();
2189 /* Try next concats */
2190 while (ch == Magic('&'))
2191 {
2192 skipchr();
2193 EMIT(NFA_NOPEN);
2194 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002195 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002196 if (nfa_regconcat() == FAIL)
2197 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002198 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002199 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002200 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 EMIT(NFA_CONCAT);
2202 ch = peekchr();
2203 }
2204
Bram Moolenaar699c1202013-09-25 16:41:54 +02002205 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002206 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002207 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002208
2209 return OK;
2210}
2211
2212/*
2213 * Parse a pattern, one or more branches, separated by "\|". It matches
2214 * anything that matches one of the branches. Example: "foo\|beep" matches
2215 * "foo" and matches "beep". If more than one branch matches, the first one
2216 * is used.
2217 *
2218 * pattern ::= branch
2219 * or branch \| branch
2220 * or branch \| branch \| branch
2221 * etc.
2222 */
2223 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002224nfa_reg(
2225 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002226{
2227 int parno = 0;
2228
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229 if (paren == REG_PAREN)
2230 {
2231 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002232 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233 parno = regnpar++;
2234 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002235#ifdef FEAT_SYN_HL
2236 else if (paren == REG_ZPAREN)
2237 {
2238 /* Make a ZOPEN node. */
2239 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002240 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002241 parno = regnzpar++;
2242 }
2243#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002244
2245 if (nfa_regbranch() == FAIL)
2246 return FAIL; /* cascaded error */
2247
2248 while (peekchr() == Magic('|'))
2249 {
2250 skipchr();
2251 if (nfa_regbranch() == FAIL)
2252 return FAIL; /* cascaded error */
2253 EMIT(NFA_OR);
2254 }
2255
2256 /* Check for proper termination. */
2257 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2258 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259 if (paren == REG_NPAREN)
2260 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2261 else
2262 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2263 }
2264 else if (paren == REG_NOPAREN && peekchr() != NUL)
2265 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 if (peekchr() == Magic(')'))
2267 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2268 else
2269 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2270 }
2271 /*
2272 * Here we set the flag allowing back references to this set of
2273 * parentheses.
2274 */
2275 if (paren == REG_PAREN)
2276 {
2277 had_endbrace[parno] = TRUE; /* have seen the close paren */
2278 EMIT(NFA_MOPEN + parno);
2279 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002280#ifdef FEAT_SYN_HL
2281 else if (paren == REG_ZPAREN)
2282 EMIT(NFA_ZOPEN + parno);
2283#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002284
2285 return OK;
2286}
2287
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288#ifdef DEBUG
2289static char_u code[50];
2290
2291 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002292nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293{
2294 int addnl = FALSE;
2295
2296 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2297 {
2298 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002299 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300 }
2301
2302 STRCPY(code, "");
2303 switch (c)
2304 {
2305 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2306 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2307 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2308 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2309 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2310 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2311
Bram Moolenaar5714b802013-05-28 22:03:20 +02002312 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2313 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2314 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2315 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2316 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2317 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2318 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2319 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2320 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002321#ifdef FEAT_SYN_HL
2322 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2323 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2324 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2325 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2326 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2327 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2328 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2329 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2330 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2331#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002332 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2333
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 case NFA_PREV_ATOM_NO_WIDTH:
2335 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002336 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2337 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002338 case NFA_PREV_ATOM_JUST_BEFORE:
2339 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2340 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2341 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002342 case NFA_PREV_ATOM_LIKE_PATTERN:
2343 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2344
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002345 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2346 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002347 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002348 case NFA_START_INVISIBLE_FIRST:
2349 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002350 case NFA_START_INVISIBLE_NEG:
2351 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002352 case NFA_START_INVISIBLE_NEG_FIRST:
2353 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002354 case NFA_START_INVISIBLE_BEFORE:
2355 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002356 case NFA_START_INVISIBLE_BEFORE_FIRST:
2357 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002358 case NFA_START_INVISIBLE_BEFORE_NEG:
2359 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002360 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2361 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002362 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002363 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002364 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002365 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002366
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002367 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2368 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002369 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002371 case NFA_MOPEN:
2372 case NFA_MOPEN1:
2373 case NFA_MOPEN2:
2374 case NFA_MOPEN3:
2375 case NFA_MOPEN4:
2376 case NFA_MOPEN5:
2377 case NFA_MOPEN6:
2378 case NFA_MOPEN7:
2379 case NFA_MOPEN8:
2380 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381 STRCPY(code, "NFA_MOPEN(x)");
2382 code[10] = c - NFA_MOPEN + '0';
2383 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002384 case NFA_MCLOSE:
2385 case NFA_MCLOSE1:
2386 case NFA_MCLOSE2:
2387 case NFA_MCLOSE3:
2388 case NFA_MCLOSE4:
2389 case NFA_MCLOSE5:
2390 case NFA_MCLOSE6:
2391 case NFA_MCLOSE7:
2392 case NFA_MCLOSE8:
2393 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002394 STRCPY(code, "NFA_MCLOSE(x)");
2395 code[11] = c - NFA_MCLOSE + '0';
2396 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002397#ifdef FEAT_SYN_HL
2398 case NFA_ZOPEN:
2399 case NFA_ZOPEN1:
2400 case NFA_ZOPEN2:
2401 case NFA_ZOPEN3:
2402 case NFA_ZOPEN4:
2403 case NFA_ZOPEN5:
2404 case NFA_ZOPEN6:
2405 case NFA_ZOPEN7:
2406 case NFA_ZOPEN8:
2407 case NFA_ZOPEN9:
2408 STRCPY(code, "NFA_ZOPEN(x)");
2409 code[10] = c - NFA_ZOPEN + '0';
2410 break;
2411 case NFA_ZCLOSE:
2412 case NFA_ZCLOSE1:
2413 case NFA_ZCLOSE2:
2414 case NFA_ZCLOSE3:
2415 case NFA_ZCLOSE4:
2416 case NFA_ZCLOSE5:
2417 case NFA_ZCLOSE6:
2418 case NFA_ZCLOSE7:
2419 case NFA_ZCLOSE8:
2420 case NFA_ZCLOSE9:
2421 STRCPY(code, "NFA_ZCLOSE(x)");
2422 code[11] = c - NFA_ZCLOSE + '0';
2423 break;
2424#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2426 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2427 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2428 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002429 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2430 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002431 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2432 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2433 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2434 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2435 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2436 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2437 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2438 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2439 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2440 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2441 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2442 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2443 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2444 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002445 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002446
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002448 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2449 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2450 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002451 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002453
2454 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2455 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2456 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2457 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2458 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2459 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2460 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2461
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2463 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2464 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2465 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2466 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2467 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2468 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2469 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2470 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2471 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2472 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2473 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2474 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2475 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2476 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2477 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2478
2479 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2480 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2481 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2482 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2483 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2484 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2485 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2486 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2487 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2488 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2489 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2490 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2491 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2492 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2493 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2494 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2495 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2496 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2497 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2498 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2499 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2500 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2501 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2502 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2503 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2504 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2505 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002506 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2507 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2508 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2509 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002510
2511 default:
2512 STRCPY(code, "CHAR(x)");
2513 code[5] = c;
2514 }
2515
2516 if (addnl == TRUE)
2517 STRCAT(code, " + NEWLINE ");
2518
2519}
2520
2521#ifdef ENABLE_LOG
2522static FILE *log_fd;
2523
2524/*
2525 * Print the postfix notation of the current regexp.
2526 */
2527 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002528nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002529{
2530 int *p;
2531 FILE *f;
2532
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002533 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002534 if (f != NULL)
2535 {
2536 fprintf(f, "\n-------------------------\n");
2537 if (retval == FAIL)
2538 fprintf(f, ">>> NFA engine failed ... \n");
2539 else if (retval == OK)
2540 fprintf(f, ">>> NFA engine succeeded !\n");
2541 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002542 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 {
2544 nfa_set_code(*p);
2545 fprintf(f, "%s, ", code);
2546 }
2547 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002548 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002549 fprintf(f, "%d ", *p);
2550 fprintf(f, "\n\n");
2551 fclose(f);
2552 }
2553}
2554
2555/*
2556 * Print the NFA starting with a root node "state".
2557 */
2558 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002559nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002561 garray_T indent;
2562
2563 ga_init2(&indent, 1, 64);
2564 ga_append(&indent, '\0');
2565 nfa_print_state2(debugf, state, &indent);
2566 ga_clear(&indent);
2567}
2568
2569 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002570nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002571{
2572 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573
2574 if (state == NULL)
2575 return;
2576
2577 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002578
2579 /* Output indent */
2580 p = (char_u *)indent->ga_data;
2581 if (indent->ga_len >= 3)
2582 {
2583 int last = indent->ga_len - 3;
2584 char_u save[2];
2585
2586 STRNCPY(save, &p[last], 2);
2587 STRNCPY(&p[last], "+-", 2);
2588 fprintf(debugf, " %s", p);
2589 STRNCPY(&p[last], save, 2);
2590 }
2591 else
2592 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002593
2594 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002595 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002596 code,
2597 state->c,
2598 abs(state->id),
2599 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002600 if (state->id < 0)
2601 return;
2602
2603 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002604
2605 /* grow indent for state->out */
2606 indent->ga_len -= 1;
2607 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002608 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002609 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002610 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002611 ga_append(indent, '\0');
2612
2613 nfa_print_state2(debugf, state->out, indent);
2614
2615 /* replace last part of indent for state->out1 */
2616 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002617 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002618 ga_append(indent, '\0');
2619
2620 nfa_print_state2(debugf, state->out1, indent);
2621
2622 /* shrink indent */
2623 indent->ga_len -= 3;
2624 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002625}
2626
2627/*
2628 * Print the NFA state machine.
2629 */
2630 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002631nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002632{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002633 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002634
2635 if (debugf != NULL)
2636 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002637 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002638
Bram Moolenaar473de612013-06-08 18:19:48 +02002639 if (prog->reganch)
2640 fprintf(debugf, "reganch: %d\n", prog->reganch);
2641 if (prog->regstart != NUL)
2642 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2643 prog->regstart, prog->regstart);
2644 if (prog->match_text != NULL)
2645 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002646
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647 fclose(debugf);
2648 }
2649}
2650#endif /* ENABLE_LOG */
2651#endif /* DEBUG */
2652
2653/*
2654 * Parse r.e. @expr and convert it into postfix form.
2655 * Return the postfix string on success, NULL otherwise.
2656 */
2657 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002658re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659{
2660 if (nfa_reg(REG_NOPAREN) == FAIL)
2661 return NULL;
2662 EMIT(NFA_MOPEN);
2663 return post_start;
2664}
2665
2666/* NB. Some of the code below is inspired by Russ's. */
2667
2668/*
2669 * Represents an NFA state plus zero or one or two arrows exiting.
2670 * if c == MATCH, no arrows out; matching state.
2671 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2672 * If c < 256, labeled arrow with character c to out.
2673 */
2674
2675static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2676
2677/*
2678 * Allocate and initialize nfa_state_T.
2679 */
2680 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002681alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002682{
2683 nfa_state_T *s;
2684
2685 if (istate >= nstate)
2686 return NULL;
2687
2688 s = &state_ptr[istate++];
2689
2690 s->c = c;
2691 s->out = out;
2692 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002693 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002694
2695 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002696 s->lastlist[0] = 0;
2697 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002698
2699 return s;
2700}
2701
2702/*
2703 * A partially built NFA without the matching state filled in.
2704 * Frag_T.start points at the start state.
2705 * Frag_T.out is a list of places that need to be set to the
2706 * next state for this fragment.
2707 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002708
2709/* Since the out pointers in the list are always
2710 * uninitialized, we use the pointers themselves
2711 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002713union Ptrlist
2714{
2715 Ptrlist *next;
2716 nfa_state_T *s;
2717};
2718
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002719struct Frag
2720{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002721 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002722 Ptrlist *out;
2723};
2724typedef struct Frag Frag_T;
2725
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002726static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2727static Ptrlist *list1(nfa_state_T **outp);
2728static void patch(Ptrlist *l, nfa_state_T *s);
2729static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2730static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2731static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732
2733/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002734 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002735 */
2736 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002737frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002739 Frag_T n;
2740
2741 n.start = start;
2742 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002743 return n;
2744}
2745
2746/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002747 * Create singleton list containing just outp.
2748 */
2749 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002750list1(
2751 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752{
2753 Ptrlist *l;
2754
2755 l = (Ptrlist *)outp;
2756 l->next = NULL;
2757 return l;
2758}
2759
2760/*
2761 * Patch the list of states at out to point to start.
2762 */
2763 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002764patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765{
2766 Ptrlist *next;
2767
2768 for (; l; l = next)
2769 {
2770 next = l->next;
2771 l->s = s;
2772 }
2773}
2774
2775
2776/*
2777 * Join the two lists l1 and l2, returning the combination.
2778 */
2779 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002780append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002781{
2782 Ptrlist *oldl1;
2783
2784 oldl1 = l1;
2785 while (l1->next)
2786 l1 = l1->next;
2787 l1->next = l2;
2788 return oldl1;
2789}
2790
2791/*
2792 * Stack used for transforming postfix form into NFA.
2793 */
2794static Frag_T empty;
2795
2796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002797st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002799#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800 FILE *df;
2801 int *p2;
2802
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002803 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002804 if (df)
2805 {
2806 fprintf(df, "Error popping the stack!\n");
2807#ifdef DEBUG
2808 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2809#endif
2810 fprintf(df, "Postfix form is: ");
2811#ifdef DEBUG
2812 for (p2 = postfix; p2 < end; p2++)
2813 {
2814 nfa_set_code(*p2);
2815 fprintf(df, "%s, ", code);
2816 }
2817 nfa_set_code(*p);
2818 fprintf(df, "\nCurrent position is: ");
2819 for (p2 = postfix; p2 <= p; p2 ++)
2820 {
2821 nfa_set_code(*p2);
2822 fprintf(df, "%s, ", code);
2823 }
2824#else
2825 for (p2 = postfix; p2 < end; p2++)
2826 {
2827 fprintf(df, "%d, ", *p2);
2828 }
2829 fprintf(df, "\nCurrent position is: ");
2830 for (p2 = postfix; p2 <= p; p2 ++)
2831 {
2832 fprintf(df, "%d, ", *p2);
2833 }
2834#endif
2835 fprintf(df, "\n--------------------------\n");
2836 fclose(df);
2837 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002838#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002839 EMSG(_("E874: (NFA) Could not pop the stack !"));
2840}
2841
2842/*
2843 * Push an item onto the stack.
2844 */
2845 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002846st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002847{
2848 Frag_T *stackp = *p;
2849
2850 if (stackp >= stack_end)
2851 return;
2852 *stackp = s;
2853 *p = *p + 1;
2854}
2855
2856/*
2857 * Pop an item from the stack.
2858 */
2859 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002860st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002861{
2862 Frag_T *stackp;
2863
2864 *p = *p - 1;
2865 stackp = *p;
2866 if (stackp < stack)
2867 return empty;
2868 return **p;
2869}
2870
2871/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002872 * Estimate the maximum byte length of anything matching "state".
2873 * When unknown or unlimited return -1.
2874 */
2875 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002876nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002877{
2878 int l, r;
2879 nfa_state_T *state = startstate;
2880 int len = 0;
2881
2882 /* detect looping in a NFA_SPLIT */
2883 if (depth > 4)
2884 return -1;
2885
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002886 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002887 {
2888 switch (state->c)
2889 {
2890 case NFA_END_INVISIBLE:
2891 case NFA_END_INVISIBLE_NEG:
2892 /* the end, return what we have */
2893 return len;
2894
2895 case NFA_SPLIT:
2896 /* two alternatives, use the maximum */
2897 l = nfa_max_width(state->out, depth + 1);
2898 r = nfa_max_width(state->out1, depth + 1);
2899 if (l < 0 || r < 0)
2900 return -1;
2901 return len + (l > r ? l : r);
2902
2903 case NFA_ANY:
2904 case NFA_START_COLL:
2905 case NFA_START_NEG_COLL:
2906 /* matches some character, including composing chars */
2907#ifdef FEAT_MBYTE
2908 if (enc_utf8)
2909 len += MB_MAXBYTES;
2910 else if (has_mbyte)
2911 len += 2;
2912 else
2913#endif
2914 ++len;
2915 if (state->c != NFA_ANY)
2916 {
2917 /* skip over the characters */
2918 state = state->out1->out;
2919 continue;
2920 }
2921 break;
2922
2923 case NFA_DIGIT:
2924 case NFA_WHITE:
2925 case NFA_HEX:
2926 case NFA_OCTAL:
2927 /* ascii */
2928 ++len;
2929 break;
2930
2931 case NFA_IDENT:
2932 case NFA_SIDENT:
2933 case NFA_KWORD:
2934 case NFA_SKWORD:
2935 case NFA_FNAME:
2936 case NFA_SFNAME:
2937 case NFA_PRINT:
2938 case NFA_SPRINT:
2939 case NFA_NWHITE:
2940 case NFA_NDIGIT:
2941 case NFA_NHEX:
2942 case NFA_NOCTAL:
2943 case NFA_WORD:
2944 case NFA_NWORD:
2945 case NFA_HEAD:
2946 case NFA_NHEAD:
2947 case NFA_ALPHA:
2948 case NFA_NALPHA:
2949 case NFA_LOWER:
2950 case NFA_NLOWER:
2951 case NFA_UPPER:
2952 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002953 case NFA_LOWER_IC:
2954 case NFA_NLOWER_IC:
2955 case NFA_UPPER_IC:
2956 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002957 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002958 /* possibly non-ascii */
2959#ifdef FEAT_MBYTE
2960 if (has_mbyte)
2961 len += 3;
2962 else
2963#endif
2964 ++len;
2965 break;
2966
2967 case NFA_START_INVISIBLE:
2968 case NFA_START_INVISIBLE_NEG:
2969 case NFA_START_INVISIBLE_BEFORE:
2970 case NFA_START_INVISIBLE_BEFORE_NEG:
2971 /* zero-width, out1 points to the END state */
2972 state = state->out1->out;
2973 continue;
2974
2975 case NFA_BACKREF1:
2976 case NFA_BACKREF2:
2977 case NFA_BACKREF3:
2978 case NFA_BACKREF4:
2979 case NFA_BACKREF5:
2980 case NFA_BACKREF6:
2981 case NFA_BACKREF7:
2982 case NFA_BACKREF8:
2983 case NFA_BACKREF9:
2984#ifdef FEAT_SYN_HL
2985 case NFA_ZREF1:
2986 case NFA_ZREF2:
2987 case NFA_ZREF3:
2988 case NFA_ZREF4:
2989 case NFA_ZREF5:
2990 case NFA_ZREF6:
2991 case NFA_ZREF7:
2992 case NFA_ZREF8:
2993 case NFA_ZREF9:
2994#endif
2995 case NFA_NEWL:
2996 case NFA_SKIP:
2997 /* unknown width */
2998 return -1;
2999
3000 case NFA_BOL:
3001 case NFA_EOL:
3002 case NFA_BOF:
3003 case NFA_EOF:
3004 case NFA_BOW:
3005 case NFA_EOW:
3006 case NFA_MOPEN:
3007 case NFA_MOPEN1:
3008 case NFA_MOPEN2:
3009 case NFA_MOPEN3:
3010 case NFA_MOPEN4:
3011 case NFA_MOPEN5:
3012 case NFA_MOPEN6:
3013 case NFA_MOPEN7:
3014 case NFA_MOPEN8:
3015 case NFA_MOPEN9:
3016#ifdef FEAT_SYN_HL
3017 case NFA_ZOPEN:
3018 case NFA_ZOPEN1:
3019 case NFA_ZOPEN2:
3020 case NFA_ZOPEN3:
3021 case NFA_ZOPEN4:
3022 case NFA_ZOPEN5:
3023 case NFA_ZOPEN6:
3024 case NFA_ZOPEN7:
3025 case NFA_ZOPEN8:
3026 case NFA_ZOPEN9:
3027 case NFA_ZCLOSE:
3028 case NFA_ZCLOSE1:
3029 case NFA_ZCLOSE2:
3030 case NFA_ZCLOSE3:
3031 case NFA_ZCLOSE4:
3032 case NFA_ZCLOSE5:
3033 case NFA_ZCLOSE6:
3034 case NFA_ZCLOSE7:
3035 case NFA_ZCLOSE8:
3036 case NFA_ZCLOSE9:
3037#endif
3038 case NFA_MCLOSE:
3039 case NFA_MCLOSE1:
3040 case NFA_MCLOSE2:
3041 case NFA_MCLOSE3:
3042 case NFA_MCLOSE4:
3043 case NFA_MCLOSE5:
3044 case NFA_MCLOSE6:
3045 case NFA_MCLOSE7:
3046 case NFA_MCLOSE8:
3047 case NFA_MCLOSE9:
3048 case NFA_NOPEN:
3049 case NFA_NCLOSE:
3050
3051 case NFA_LNUM_GT:
3052 case NFA_LNUM_LT:
3053 case NFA_COL_GT:
3054 case NFA_COL_LT:
3055 case NFA_VCOL_GT:
3056 case NFA_VCOL_LT:
3057 case NFA_MARK_GT:
3058 case NFA_MARK_LT:
3059 case NFA_VISUAL:
3060 case NFA_LNUM:
3061 case NFA_CURSOR:
3062 case NFA_COL:
3063 case NFA_VCOL:
3064 case NFA_MARK:
3065
3066 case NFA_ZSTART:
3067 case NFA_ZEND:
3068 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003069 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003070 case NFA_START_PATTERN:
3071 case NFA_END_PATTERN:
3072 case NFA_COMPOSING:
3073 case NFA_END_COMPOSING:
3074 /* zero-width */
3075 break;
3076
3077 default:
3078 if (state->c < 0)
3079 /* don't know what this is */
3080 return -1;
3081 /* normal character */
3082 len += MB_CHAR2LEN(state->c);
3083 break;
3084 }
3085
3086 /* normal way to continue */
3087 state = state->out;
3088 }
3089
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003090 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003091 return -1;
3092}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003093
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003094/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003095 * Convert a postfix form into its equivalent NFA.
3096 * Return the NFA start state on success, NULL otherwise.
3097 */
3098 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003099post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003100{
3101 int *p;
3102 int mopen;
3103 int mclose;
3104 Frag_T *stack = NULL;
3105 Frag_T *stackp = NULL;
3106 Frag_T *stack_end = NULL;
3107 Frag_T e1;
3108 Frag_T e2;
3109 Frag_T e;
3110 nfa_state_T *s;
3111 nfa_state_T *s1;
3112 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003113 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114
3115 if (postfix == NULL)
3116 return NULL;
3117
Bram Moolenaar053bb602013-05-20 13:55:21 +02003118#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003119#define POP() st_pop(&stackp, stack); \
3120 if (stackp < stack) \
3121 { \
3122 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003123 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003124 return NULL; \
3125 }
3126
3127 if (nfa_calc_size == FALSE)
3128 {
3129 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003130 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003131 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003132 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003133 }
3134
3135 for (p = postfix; p < end; ++p)
3136 {
3137 switch (*p)
3138 {
3139 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003140 /* Concatenation.
3141 * Pay attention: this operator does not exist in the r.e. itself
3142 * (it is implicit, really). It is added when r.e. is translated
3143 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003144 if (nfa_calc_size == TRUE)
3145 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003146 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003147 break;
3148 }
3149 e2 = POP();
3150 e1 = POP();
3151 patch(e1.out, e2.start);
3152 PUSH(frag(e1.start, e2.out));
3153 break;
3154
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003155 case NFA_OR:
3156 /* Alternation */
3157 if (nfa_calc_size == TRUE)
3158 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003159 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003160 break;
3161 }
3162 e2 = POP();
3163 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003164 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003165 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003166 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003167 PUSH(frag(s, append(e1.out, e2.out)));
3168 break;
3169
3170 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003171 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003172 if (nfa_calc_size == TRUE)
3173 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003174 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003175 break;
3176 }
3177 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003178 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003180 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003181 patch(e.out, s);
3182 PUSH(frag(s, list1(&s->out1)));
3183 break;
3184
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003185 case NFA_STAR_NONGREEDY:
3186 /* Zero or more, prefer zero */
3187 if (nfa_calc_size == TRUE)
3188 {
3189 nstate++;
3190 break;
3191 }
3192 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003193 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003194 if (s == NULL)
3195 goto theend;
3196 patch(e.out, s);
3197 PUSH(frag(s, list1(&s->out)));
3198 break;
3199
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 case NFA_QUEST:
3201 /* one or zero atoms=> greedy match */
3202 if (nfa_calc_size == TRUE)
3203 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003204 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205 break;
3206 }
3207 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003208 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003210 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 PUSH(frag(s, append(e.out, list1(&s->out1))));
3212 break;
3213
3214 case NFA_QUEST_NONGREEDY:
3215 /* zero or one atoms => non-greedy match */
3216 if (nfa_calc_size == TRUE)
3217 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003218 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219 break;
3220 }
3221 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003222 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003224 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003225 PUSH(frag(s, append(e.out, list1(&s->out))));
3226 break;
3227
Bram Moolenaar417bad22013-06-07 14:08:30 +02003228 case NFA_END_COLL:
3229 case NFA_END_NEG_COLL:
3230 /* On the stack is the sequence starting with NFA_START_COLL or
3231 * NFA_START_NEG_COLL and all possible characters. Patch it to
3232 * add the output to the start. */
3233 if (nfa_calc_size == TRUE)
3234 {
3235 nstate++;
3236 break;
3237 }
3238 e = POP();
3239 s = alloc_state(NFA_END_COLL, NULL, NULL);
3240 if (s == NULL)
3241 goto theend;
3242 patch(e.out, s);
3243 e.start->out1 = s;
3244 PUSH(frag(e.start, list1(&s->out)));
3245 break;
3246
3247 case NFA_RANGE:
3248 /* Before this are two characters, the low and high end of a
3249 * range. Turn them into two states with MIN and MAX. */
3250 if (nfa_calc_size == TRUE)
3251 {
3252 /* nstate += 0; */
3253 break;
3254 }
3255 e2 = POP();
3256 e1 = POP();
3257 e2.start->val = e2.start->c;
3258 e2.start->c = NFA_RANGE_MAX;
3259 e1.start->val = e1.start->c;
3260 e1.start->c = NFA_RANGE_MIN;
3261 patch(e1.out, e2.start);
3262 PUSH(frag(e1.start, e2.out));
3263 break;
3264
Bram Moolenaar699c1202013-09-25 16:41:54 +02003265 case NFA_EMPTY:
3266 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003267 if (nfa_calc_size == TRUE)
3268 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003269 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 break;
3271 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003272 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003274 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 PUSH(frag(s, list1(&s->out)));
3276 break;
3277
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003278 case NFA_OPT_CHARS:
3279 {
3280 int n;
3281
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003282 /* \%[abc] implemented as:
3283 * NFA_SPLIT
3284 * +-CHAR(a)
3285 * | +-NFA_SPLIT
3286 * | +-CHAR(b)
3287 * | | +-NFA_SPLIT
3288 * | | +-CHAR(c)
3289 * | | | +-next
3290 * | | +- next
3291 * | +- next
3292 * +- next
3293 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003294 n = *++p; /* get number of characters */
3295 if (nfa_calc_size == TRUE)
3296 {
3297 nstate += n;
3298 break;
3299 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003300 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003301 e1.out = NULL; /* stores list with out1's */
3302 s1 = NULL; /* previous NFA_SPLIT to connect to */
3303 while (n-- > 0)
3304 {
3305 e = POP(); /* get character */
3306 s = alloc_state(NFA_SPLIT, e.start, NULL);
3307 if (s == NULL)
3308 goto theend;
3309 if (e1.out == NULL)
3310 e1 = e;
3311 patch(e.out, s1);
3312 append(e1.out, list1(&s->out1));
3313 s1 = s;
3314 }
3315 PUSH(frag(s, e1.out));
3316 break;
3317 }
3318
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003320 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003321 case NFA_PREV_ATOM_JUST_BEFORE:
3322 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003323 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003324 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003325 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3326 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003327 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003328 int start_state;
3329 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003330 int n = 0;
3331 nfa_state_T *zend;
3332 nfa_state_T *skip;
3333
Bram Moolenaardecd9542013-06-07 16:31:50 +02003334 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003335 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003336 case NFA_PREV_ATOM_NO_WIDTH:
3337 start_state = NFA_START_INVISIBLE;
3338 end_state = NFA_END_INVISIBLE;
3339 break;
3340 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3341 start_state = NFA_START_INVISIBLE_NEG;
3342 end_state = NFA_END_INVISIBLE_NEG;
3343 break;
3344 case NFA_PREV_ATOM_JUST_BEFORE:
3345 start_state = NFA_START_INVISIBLE_BEFORE;
3346 end_state = NFA_END_INVISIBLE;
3347 break;
3348 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3349 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3350 end_state = NFA_END_INVISIBLE_NEG;
3351 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003352 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003353 start_state = NFA_START_PATTERN;
3354 end_state = NFA_END_PATTERN;
3355 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003356 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003357
3358 if (before)
3359 n = *++p; /* get the count */
3360
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003361 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003362 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003363 * The \@<= operator: match for the preceding atom.
3364 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003366 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367
3368 if (nfa_calc_size == TRUE)
3369 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003370 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371 break;
3372 }
3373 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003374 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003375 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003376 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003377
Bram Moolenaar87953742013-06-05 18:52:40 +02003378 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003379 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003380 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003381 if (pattern)
3382 {
3383 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3384 skip = alloc_state(NFA_SKIP, NULL, NULL);
3385 zend = alloc_state(NFA_ZEND, s1, NULL);
3386 s1->out= skip;
3387 patch(e.out, zend);
3388 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003389 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003390 else
3391 {
3392 patch(e.out, s1);
3393 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003394 if (before)
3395 {
3396 if (n <= 0)
3397 /* See if we can guess the maximum width, it avoids a
3398 * lot of pointless tries. */
3399 n = nfa_max_width(e.start, 0);
3400 s->val = n; /* store the count */
3401 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003402 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003403 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003404 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003405
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003406#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003407 case NFA_COMPOSING: /* char with composing char */
3408#if 0
3409 /* TODO */
3410 if (regflags & RF_ICOMBINE)
3411 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003412 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003413 }
3414#endif
3415 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003416#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003417
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003418 case NFA_MOPEN: /* \( \) Submatch */
3419 case NFA_MOPEN1:
3420 case NFA_MOPEN2:
3421 case NFA_MOPEN3:
3422 case NFA_MOPEN4:
3423 case NFA_MOPEN5:
3424 case NFA_MOPEN6:
3425 case NFA_MOPEN7:
3426 case NFA_MOPEN8:
3427 case NFA_MOPEN9:
3428#ifdef FEAT_SYN_HL
3429 case NFA_ZOPEN: /* \z( \) Submatch */
3430 case NFA_ZOPEN1:
3431 case NFA_ZOPEN2:
3432 case NFA_ZOPEN3:
3433 case NFA_ZOPEN4:
3434 case NFA_ZOPEN5:
3435 case NFA_ZOPEN6:
3436 case NFA_ZOPEN7:
3437 case NFA_ZOPEN8:
3438 case NFA_ZOPEN9:
3439#endif
3440 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003441 if (nfa_calc_size == TRUE)
3442 {
3443 nstate += 2;
3444 break;
3445 }
3446
3447 mopen = *p;
3448 switch (*p)
3449 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003450 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3451#ifdef FEAT_SYN_HL
3452 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3453 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3454 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3455 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3456 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3457 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3458 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3459 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3460 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3461 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3462#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003463#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003464 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003465#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003467 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003468 mclose = *p + NSUBEXP;
3469 break;
3470 }
3471
3472 /* Allow "NFA_MOPEN" as a valid postfix representation for
3473 * the empty regexp "". In this case, the NFA will be
3474 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3475 * empty groups of parenthesis, and empty mbyte chars */
3476 if (stackp == stack)
3477 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003478 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003480 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003481 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003483 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484 patch(list1(&s->out), s1);
3485 PUSH(frag(s, list1(&s1->out)));
3486 break;
3487 }
3488
3489 /* At least one node was emitted before NFA_MOPEN, so
3490 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3491 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003492 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003494 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003495
Bram Moolenaar525666f2013-06-02 16:40:55 +02003496 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003497 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003498 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499 patch(e.out, s1);
3500
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003501#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003502 if (mopen == NFA_COMPOSING)
3503 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003505#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003506
3507 PUSH(frag(s, list1(&s1->out)));
3508 break;
3509
Bram Moolenaar5714b802013-05-28 22:03:20 +02003510 case NFA_BACKREF1:
3511 case NFA_BACKREF2:
3512 case NFA_BACKREF3:
3513 case NFA_BACKREF4:
3514 case NFA_BACKREF5:
3515 case NFA_BACKREF6:
3516 case NFA_BACKREF7:
3517 case NFA_BACKREF8:
3518 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003519#ifdef FEAT_SYN_HL
3520 case NFA_ZREF1:
3521 case NFA_ZREF2:
3522 case NFA_ZREF3:
3523 case NFA_ZREF4:
3524 case NFA_ZREF5:
3525 case NFA_ZREF6:
3526 case NFA_ZREF7:
3527 case NFA_ZREF8:
3528 case NFA_ZREF9:
3529#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003530 if (nfa_calc_size == TRUE)
3531 {
3532 nstate += 2;
3533 break;
3534 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003535 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003536 if (s == NULL)
3537 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003538 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003539 if (s1 == NULL)
3540 goto theend;
3541 patch(list1(&s->out), s1);
3542 PUSH(frag(s, list1(&s1->out)));
3543 break;
3544
Bram Moolenaar423532e2013-05-29 21:14:42 +02003545 case NFA_LNUM:
3546 case NFA_LNUM_GT:
3547 case NFA_LNUM_LT:
3548 case NFA_VCOL:
3549 case NFA_VCOL_GT:
3550 case NFA_VCOL_LT:
3551 case NFA_COL:
3552 case NFA_COL_GT:
3553 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003554 case NFA_MARK:
3555 case NFA_MARK_GT:
3556 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003557 {
3558 int n = *++p; /* lnum, col or mark name */
3559
Bram Moolenaar423532e2013-05-29 21:14:42 +02003560 if (nfa_calc_size == TRUE)
3561 {
3562 nstate += 1;
3563 break;
3564 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003565 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003566 if (s == NULL)
3567 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003568 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003569 PUSH(frag(s, list1(&s->out)));
3570 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003571 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003572
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003573 case NFA_ZSTART:
3574 case NFA_ZEND:
3575 default:
3576 /* Operands */
3577 if (nfa_calc_size == TRUE)
3578 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003579 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 break;
3581 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003582 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003583 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003584 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003585 PUSH(frag(s, list1(&s->out)));
3586 break;
3587
3588 } /* switch(*p) */
3589
3590 } /* for(p = postfix; *p; ++p) */
3591
3592 if (nfa_calc_size == TRUE)
3593 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003594 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003595 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596 }
3597
3598 e = POP();
3599 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003600 {
3601 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 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 +02003603 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003604
3605 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003606 {
3607 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003608 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003609 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003611 matchstate = &state_ptr[istate++]; /* the match state */
3612 matchstate->c = NFA_MATCH;
3613 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003614 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615
3616 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003617 ret = e.start;
3618
3619theend:
3620 vim_free(stack);
3621 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003622
3623#undef POP1
3624#undef PUSH1
3625#undef POP2
3626#undef PUSH2
3627#undef POP
3628#undef PUSH
3629}
3630
Bram Moolenaara2947e22013-06-11 22:44:09 +02003631/*
3632 * After building the NFA program, inspect it to add optimization hints.
3633 */
3634 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003635nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003636{
3637 int i;
3638 int c;
3639
3640 for (i = 0; i < prog->nstate; ++i)
3641 {
3642 c = prog->state[i].c;
3643 if (c == NFA_START_INVISIBLE
3644 || c == NFA_START_INVISIBLE_NEG
3645 || c == NFA_START_INVISIBLE_BEFORE
3646 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3647 {
3648 int directly;
3649
3650 /* Do it directly when what follows is possibly the end of the
3651 * match. */
3652 if (match_follows(prog->state[i].out1->out, 0))
3653 directly = TRUE;
3654 else
3655 {
3656 int ch_invisible = failure_chance(prog->state[i].out, 0);
3657 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3658
3659 /* Postpone when the invisible match is expensive or has a
3660 * lower chance of failing. */
3661 if (c == NFA_START_INVISIBLE_BEFORE
3662 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3663 {
3664 /* "before" matches are very expensive when
3665 * unbounded, always prefer what follows then,
3666 * unless what follows will always match.
3667 * Otherwise strongly prefer what follows. */
3668 if (prog->state[i].val <= 0 && ch_follows > 0)
3669 directly = FALSE;
3670 else
3671 directly = ch_follows * 10 < ch_invisible;
3672 }
3673 else
3674 {
3675 /* normal invisible, first do the one with the
3676 * highest failure chance */
3677 directly = ch_follows < ch_invisible;
3678 }
3679 }
3680 if (directly)
3681 /* switch to the _FIRST state */
3682 ++prog->state[i].c;
3683 }
3684 }
3685}
3686
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003687/****************************************************************
3688 * NFA execution code.
3689 ****************************************************************/
3690
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003691typedef struct
3692{
3693 int in_use; /* number of subexpr with useful info */
3694
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003695 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003696 union
3697 {
3698 struct multipos
3699 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003700 linenr_T start_lnum;
3701 linenr_T end_lnum;
3702 colnr_T start_col;
3703 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003704 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003705 struct linepos
3706 {
3707 char_u *start;
3708 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003709 } line[NSUBEXP];
3710 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003711} regsub_T;
3712
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003713typedef struct
3714{
3715 regsub_T norm; /* \( .. \) matches */
3716#ifdef FEAT_SYN_HL
3717 regsub_T synt; /* \z( .. \) matches */
3718#endif
3719} regsubs_T;
3720
Bram Moolenaara2d95102013-06-04 14:23:05 +02003721/* nfa_pim_T stores a Postponed Invisible Match. */
3722typedef struct nfa_pim_S nfa_pim_T;
3723struct nfa_pim_S
3724{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003725 int result; /* NFA_PIM_*, see below */
3726 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003727 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003728 union
3729 {
3730 lpos_T pos;
3731 char_u *ptr;
3732 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003733};
3734
3735/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003736#define NFA_PIM_UNUSED 0 /* pim not used */
3737#define NFA_PIM_TODO 1 /* pim not done yet */
3738#define NFA_PIM_MATCH 2 /* pim executed, matches */
3739#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003740
3741
Bram Moolenaar963fee22013-05-26 21:47:28 +02003742/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003743typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003744{
3745 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003746 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003747 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3748 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003749 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003750} nfa_thread_T;
3751
Bram Moolenaar963fee22013-05-26 21:47:28 +02003752/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003753typedef struct
3754{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003755 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003756 int n; /* nr of states currently in "t" */
3757 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003758 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003759 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003760} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003761
Bram Moolenaar5714b802013-05-28 22:03:20 +02003762#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003763static void log_subsexpr(regsubs_T *subs);
3764static void log_subexpr(regsub_T *sub);
3765static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003766
3767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003768log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003769{
3770 log_subexpr(&subs->norm);
3771# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003772 if (nfa_has_zsubexpr)
3773 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003774# endif
3775}
3776
Bram Moolenaar5714b802013-05-28 22:03:20 +02003777 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003778log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003779{
3780 int j;
3781
3782 for (j = 0; j < sub->in_use; j++)
3783 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003784 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003785 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003786 sub->list.multi[j].start_col,
3787 (int)sub->list.multi[j].start_lnum,
3788 sub->list.multi[j].end_col,
3789 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003790 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003791 {
3792 char *s = (char *)sub->list.line[j].start;
3793 char *e = (char *)sub->list.line[j].end;
3794
Bram Moolenaar87953742013-06-05 18:52:40 +02003795 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003796 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003797 s == NULL ? "NULL" : s,
3798 e == NULL ? "NULL" : e);
3799 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003800}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003801
3802 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003803pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003804{
3805 static char buf[30];
3806
3807 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3808 buf[0] = NUL;
3809 else
3810 {
3811 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3812 : (int)(pim->end.ptr - reginput));
3813 }
3814 return buf;
3815}
3816
Bram Moolenaar5714b802013-05-28 22:03:20 +02003817#endif
3818
Bram Moolenaar963fee22013-05-26 21:47:28 +02003819/* Used during execution: whether a match has been found. */
3820static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003821#ifdef FEAT_RELTIME
3822static proftime_T *nfa_time_limit;
3823static int nfa_time_count;
3824#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003825
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003826static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3827static void clear_sub(regsub_T *sub);
3828static void copy_sub(regsub_T *to, regsub_T *from);
3829static void copy_sub_off(regsub_T *to, regsub_T *from);
3830static void copy_ze_off(regsub_T *to, regsub_T *from);
3831static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3832static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3833static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3834static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3835static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3836static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3837static 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 +02003838
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003839/*
3840 * Copy postponed invisible match info from "from" to "to".
3841 */
3842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003843copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003844{
3845 to->result = from->result;
3846 to->state = from->state;
3847 copy_sub(&to->subs.norm, &from->subs.norm);
3848#ifdef FEAT_SYN_HL
3849 if (nfa_has_zsubexpr)
3850 copy_sub(&to->subs.synt, &from->subs.synt);
3851#endif
3852 to->end = from->end;
3853}
3854
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003856clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003857{
3858 if (REG_MULTI)
3859 /* Use 0xff to set lnum to -1 */
3860 vim_memset(sub->list.multi, 0xff,
3861 sizeof(struct multipos) * nfa_nsubexpr);
3862 else
3863 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3864 sub->in_use = 0;
3865}
3866
3867/*
3868 * Copy the submatches from "from" to "to".
3869 */
3870 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003871copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003872{
3873 to->in_use = from->in_use;
3874 if (from->in_use > 0)
3875 {
3876 /* Copy the match start and end positions. */
3877 if (REG_MULTI)
3878 mch_memmove(&to->list.multi[0],
3879 &from->list.multi[0],
3880 sizeof(struct multipos) * from->in_use);
3881 else
3882 mch_memmove(&to->list.line[0],
3883 &from->list.line[0],
3884 sizeof(struct linepos) * from->in_use);
3885 }
3886}
3887
3888/*
3889 * Like copy_sub() but exclude the main match.
3890 */
3891 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003892copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003893{
3894 if (to->in_use < from->in_use)
3895 to->in_use = from->in_use;
3896 if (from->in_use > 1)
3897 {
3898 /* Copy the match start and end positions. */
3899 if (REG_MULTI)
3900 mch_memmove(&to->list.multi[1],
3901 &from->list.multi[1],
3902 sizeof(struct multipos) * (from->in_use - 1));
3903 else
3904 mch_memmove(&to->list.line[1],
3905 &from->list.line[1],
3906 sizeof(struct linepos) * (from->in_use - 1));
3907 }
3908}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003909
Bram Moolenaar428e9872013-05-30 17:05:39 +02003910/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003911 * Like copy_sub() but only do the end of the main match if \ze is present.
3912 */
3913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003914copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003915{
3916 if (nfa_has_zend)
3917 {
3918 if (REG_MULTI)
3919 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003920 if (from->list.multi[0].end_lnum >= 0)
3921 {
3922 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3923 to->list.multi[0].end_col = from->list.multi[0].end_col;
3924 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02003925 }
3926 else
3927 {
3928 if (from->list.line[0].end != NULL)
3929 to->list.line[0].end = from->list.line[0].end;
3930 }
3931 }
3932}
3933
3934/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003935 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003936 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003937 */
3938 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003939sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003940{
3941 int i;
3942 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003943 linenr_T s1;
3944 linenr_T s2;
3945 char_u *sp1;
3946 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003947
3948 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3949 if (REG_MULTI)
3950 {
3951 for (i = 0; i < todo; ++i)
3952 {
3953 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003954 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003955 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003956 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003957 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003958 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003959 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003960 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003961 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003962 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003963 if (s1 != -1 && sub1->list.multi[i].start_col
3964 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003965 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02003966
3967 if (nfa_has_backref)
3968 {
3969 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003970 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02003971 else
3972 s1 = -1;
3973 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003974 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02003975 else
3976 s2 = -1;
3977 if (s1 != s2)
3978 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003979 if (s1 != -1 && sub1->list.multi[i].end_col
3980 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02003981 return FALSE;
3982 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003983 }
3984 }
3985 else
3986 {
3987 for (i = 0; i < todo; ++i)
3988 {
3989 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003990 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003991 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003992 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003993 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003994 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003995 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003996 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003997 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003998 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02003999 if (nfa_has_backref)
4000 {
4001 if (i < sub1->in_use)
4002 sp1 = sub1->list.line[i].end;
4003 else
4004 sp1 = NULL;
4005 if (i < sub2->in_use)
4006 sp2 = sub2->list.line[i].end;
4007 else
4008 sp2 = NULL;
4009 if (sp1 != sp2)
4010 return FALSE;
4011 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004012 }
4013 }
4014
4015 return TRUE;
4016}
4017
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004018#ifdef ENABLE_LOG
4019 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004020report_state(char *action,
4021 regsub_T *sub,
4022 nfa_state_T *state,
4023 int lid,
4024 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004025{
4026 int col;
4027
4028 if (sub->in_use <= 0)
4029 col = -1;
4030 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004031 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004032 else
4033 col = (int)(sub->list.line[0].start - regline);
4034 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004035 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4036 action, abs(state->id), lid, state->c, code, col,
4037 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004038}
4039#endif
4040
Bram Moolenaar43e02982013-06-07 17:31:29 +02004041/*
4042 * Return TRUE if the same state is already in list "l" with the same
4043 * positions as "subs".
4044 */
4045 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004046has_state_with_pos(
4047 nfa_list_T *l, /* runtime state list */
4048 nfa_state_T *state, /* state to update */
4049 regsubs_T *subs, /* pointers to subexpressions */
4050 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004051{
4052 nfa_thread_T *thread;
4053 int i;
4054
4055 for (i = 0; i < l->n; ++i)
4056 {
4057 thread = &l->t[i];
4058 if (thread->state->id == state->id
4059 && sub_equal(&thread->subs.norm, &subs->norm)
4060#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004061 && (!nfa_has_zsubexpr
4062 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004063#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004064 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004065 return TRUE;
4066 }
4067 return FALSE;
4068}
4069
4070/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004071 * Return TRUE if "one" and "two" are equal. That includes when both are not
4072 * set.
4073 */
4074 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004075pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004076{
4077 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4078 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4079
4080 if (one_unused)
4081 /* one is unused: equal when two is also unused */
4082 return two_unused;
4083 if (two_unused)
4084 /* one is used and two is not: not equal */
4085 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004086 /* compare the state id */
4087 if (one->state->id != two->state->id)
4088 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004089 /* compare the position */
4090 if (REG_MULTI)
4091 return one->end.pos.lnum == two->end.pos.lnum
4092 && one->end.pos.col == two->end.pos.col;
4093 return one->end.ptr == two->end.ptr;
4094}
4095
4096/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004097 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4098 */
4099 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004100match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004101{
4102 nfa_state_T *state = startstate;
4103
4104 /* avoid too much recursion */
4105 if (depth > 10)
4106 return FALSE;
4107
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004108 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004109 {
4110 switch (state->c)
4111 {
4112 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004113 case NFA_MCLOSE:
4114 case NFA_END_INVISIBLE:
4115 case NFA_END_INVISIBLE_NEG:
4116 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004117 return TRUE;
4118
4119 case NFA_SPLIT:
4120 return match_follows(state->out, depth + 1)
4121 || match_follows(state->out1, depth + 1);
4122
4123 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004124 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004125 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004126 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004127 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004128 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004129 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004130 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004131 case NFA_COMPOSING:
4132 /* skip ahead to next state */
4133 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004134 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004135
4136 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004137 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004138 case NFA_IDENT:
4139 case NFA_SIDENT:
4140 case NFA_KWORD:
4141 case NFA_SKWORD:
4142 case NFA_FNAME:
4143 case NFA_SFNAME:
4144 case NFA_PRINT:
4145 case NFA_SPRINT:
4146 case NFA_WHITE:
4147 case NFA_NWHITE:
4148 case NFA_DIGIT:
4149 case NFA_NDIGIT:
4150 case NFA_HEX:
4151 case NFA_NHEX:
4152 case NFA_OCTAL:
4153 case NFA_NOCTAL:
4154 case NFA_WORD:
4155 case NFA_NWORD:
4156 case NFA_HEAD:
4157 case NFA_NHEAD:
4158 case NFA_ALPHA:
4159 case NFA_NALPHA:
4160 case NFA_LOWER:
4161 case NFA_NLOWER:
4162 case NFA_UPPER:
4163 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004164 case NFA_LOWER_IC:
4165 case NFA_NLOWER_IC:
4166 case NFA_UPPER_IC:
4167 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004168 case NFA_START_COLL:
4169 case NFA_START_NEG_COLL:
4170 case NFA_NEWL:
4171 /* state will advance input */
4172 return FALSE;
4173
4174 default:
4175 if (state->c > 0)
4176 /* state will advance input */
4177 return FALSE;
4178
4179 /* Others: zero-width or possibly zero-width, might still find
4180 * a match at the same position, keep looking. */
4181 break;
4182 }
4183 state = state->out;
4184 }
4185 return FALSE;
4186}
4187
4188
4189/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004190 * Return TRUE if "state" is already in list "l".
4191 */
4192 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004193state_in_list(
4194 nfa_list_T *l, /* runtime state list */
4195 nfa_state_T *state, /* state to update */
4196 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004197{
4198 if (state->lastlist[nfa_ll_index] == l->id)
4199 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004200 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004201 return TRUE;
4202 }
4203 return FALSE;
4204}
4205
Bram Moolenaard05bf562013-06-30 23:24:08 +02004206/*
4207 * Add "state" and possibly what follows to state list ".".
4208 * Returns "subs_arg", possibly copied into temp_subs.
4209 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004210 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004211addstate(
4212 nfa_list_T *l, /* runtime state list */
4213 nfa_state_T *state, /* state to update */
4214 regsubs_T *subs_arg, /* pointers to subexpressions */
4215 nfa_pim_T *pim, /* postponed look-behind match */
4216 int off) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004217{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004218 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004219 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004220 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004221 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004222 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004223 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004224 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004225 regsubs_T *subs = subs_arg;
4226 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004227#ifdef ENABLE_LOG
4228 int did_print = FALSE;
4229#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004230
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004231 switch (state->c)
4232 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004233 case NFA_NCLOSE:
4234 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004235 case NFA_MCLOSE1:
4236 case NFA_MCLOSE2:
4237 case NFA_MCLOSE3:
4238 case NFA_MCLOSE4:
4239 case NFA_MCLOSE5:
4240 case NFA_MCLOSE6:
4241 case NFA_MCLOSE7:
4242 case NFA_MCLOSE8:
4243 case NFA_MCLOSE9:
4244#ifdef FEAT_SYN_HL
4245 case NFA_ZCLOSE:
4246 case NFA_ZCLOSE1:
4247 case NFA_ZCLOSE2:
4248 case NFA_ZCLOSE3:
4249 case NFA_ZCLOSE4:
4250 case NFA_ZCLOSE5:
4251 case NFA_ZCLOSE6:
4252 case NFA_ZCLOSE7:
4253 case NFA_ZCLOSE8:
4254 case NFA_ZCLOSE9:
4255#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004256 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004257 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004258 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004259 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004260 /* These nodes are not added themselves but their "out" and/or
4261 * "out1" may be added below. */
4262 break;
4263
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004264 case NFA_BOL:
4265 case NFA_BOF:
4266 /* "^" won't match past end-of-line, don't bother trying.
4267 * Except when at the end of the line, or when we are going to the
4268 * next line for a look-behind match. */
4269 if (reginput > regline
4270 && *reginput != NUL
4271 && (nfa_endp == NULL
4272 || !REG_MULTI
4273 || reglnum == nfa_endp->se_u.pos.lnum))
4274 goto skip_add;
4275 /* FALLTHROUGH */
4276
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004277 case NFA_MOPEN1:
4278 case NFA_MOPEN2:
4279 case NFA_MOPEN3:
4280 case NFA_MOPEN4:
4281 case NFA_MOPEN5:
4282 case NFA_MOPEN6:
4283 case NFA_MOPEN7:
4284 case NFA_MOPEN8:
4285 case NFA_MOPEN9:
4286#ifdef FEAT_SYN_HL
4287 case NFA_ZOPEN:
4288 case NFA_ZOPEN1:
4289 case NFA_ZOPEN2:
4290 case NFA_ZOPEN3:
4291 case NFA_ZOPEN4:
4292 case NFA_ZOPEN5:
4293 case NFA_ZOPEN6:
4294 case NFA_ZOPEN7:
4295 case NFA_ZOPEN8:
4296 case NFA_ZOPEN9:
4297#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004298 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004299 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004300 /* These nodes need to be added so that we can bail out when it
4301 * was added to this list before at the same position to avoid an
4302 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004303
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004305 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004306 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004307 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004308 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004309 * when there is a PIM. For NFA_MATCH check the position,
4310 * lower position is preferred. */
4311 if (!nfa_has_backref && pim == NULL && !l->has_pim
4312 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004313 {
4314skip_add:
4315#ifdef ENABLE_LOG
4316 nfa_set_code(state->c);
4317 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4318 abs(state->id), l->id, state->c, code);
4319#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004320 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004321 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004322
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004323 /* Do not add the state again when it exists with the same
4324 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004325 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004326 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004327 }
4328
Bram Moolenaara0169122013-06-26 18:16:58 +02004329 /* When there are backreferences or PIMs the number of states may
4330 * be (a lot) bigger than anticipated. */
4331 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004332 {
4333 int newlen = l->len * 3 / 2 + 50;
4334
Bram Moolenaard05bf562013-06-30 23:24:08 +02004335 if (subs != &temp_subs)
4336 {
4337 /* "subs" may point into the current array, need to make a
4338 * copy before it becomes invalid. */
4339 copy_sub(&temp_subs.norm, &subs->norm);
4340#ifdef FEAT_SYN_HL
4341 if (nfa_has_zsubexpr)
4342 copy_sub(&temp_subs.synt, &subs->synt);
4343#endif
4344 subs = &temp_subs;
4345 }
4346
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004347 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004348 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4349 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004351
4352 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004353 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004354 thread = &l->t[l->n++];
4355 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004356 if (pim == NULL)
4357 thread->pim.result = NFA_PIM_UNUSED;
4358 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004359 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004360 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004361 l->has_pim = TRUE;
4362 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004363 copy_sub(&thread->subs.norm, &subs->norm);
4364#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004365 if (nfa_has_zsubexpr)
4366 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004367#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004368#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004369 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004370 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004371#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004372 }
4373
4374#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004375 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004376 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004377#endif
4378 switch (state->c)
4379 {
4380 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004381 break;
4382
4383 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004384 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004385 subs = addstate(l, state->out, subs, pim, off);
4386 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004387 break;
4388
Bram Moolenaar699c1202013-09-25 16:41:54 +02004389 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004390 case NFA_NOPEN:
4391 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004392 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 break;
4394
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004395 case NFA_MOPEN:
4396 case NFA_MOPEN1:
4397 case NFA_MOPEN2:
4398 case NFA_MOPEN3:
4399 case NFA_MOPEN4:
4400 case NFA_MOPEN5:
4401 case NFA_MOPEN6:
4402 case NFA_MOPEN7:
4403 case NFA_MOPEN8:
4404 case NFA_MOPEN9:
4405#ifdef FEAT_SYN_HL
4406 case NFA_ZOPEN:
4407 case NFA_ZOPEN1:
4408 case NFA_ZOPEN2:
4409 case NFA_ZOPEN3:
4410 case NFA_ZOPEN4:
4411 case NFA_ZOPEN5:
4412 case NFA_ZOPEN6:
4413 case NFA_ZOPEN7:
4414 case NFA_ZOPEN8:
4415 case NFA_ZOPEN9:
4416#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004417 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004418 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004419 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004420 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004421 sub = &subs->norm;
4422 }
4423#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004424 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004425 {
4426 subidx = state->c - NFA_ZOPEN;
4427 sub = &subs->synt;
4428 }
4429#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004430 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004431 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004432 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004433 sub = &subs->norm;
4434 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004435
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004436 /* avoid compiler warnings */
4437 save_ptr = NULL;
4438 save_lpos.lnum = 0;
4439 save_lpos.col = 0;
4440
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004441 /* Set the position (with "off" added) in the subexpression. Save
4442 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 if (REG_MULTI)
4444 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004445 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004446 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004447 save_lpos.lnum = sub->list.multi[subidx].start_lnum;
4448 save_lpos.col = sub->list.multi[subidx].start_col;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004449 save_in_use = -1;
4450 }
4451 else
4452 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004453 save_in_use = sub->in_use;
4454 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004455 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004456 sub->list.multi[i].start_lnum = -1;
4457 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004458 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004459 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004460 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004461 if (off == -1)
4462 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004463 sub->list.multi[subidx].start_lnum = reglnum + 1;
4464 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004465 }
4466 else
4467 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004468 sub->list.multi[subidx].start_lnum = reglnum;
4469 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004470 (colnr_T)(reginput - regline + off);
4471 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004472 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004473 }
4474 else
4475 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004476 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004477 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004478 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004479 save_in_use = -1;
4480 }
4481 else
4482 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004483 save_in_use = sub->in_use;
4484 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004485 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004486 sub->list.line[i].start = NULL;
4487 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004488 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004489 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004490 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004491 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004492 }
4493
Bram Moolenaard05bf562013-06-30 23:24:08 +02004494 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004495 /* "subs" may have changed, need to set "sub" again */
4496#ifdef FEAT_SYN_HL
4497 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4498 sub = &subs->synt;
4499 else
4500#endif
4501 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004502
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004503 if (save_in_use == -1)
4504 {
4505 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004506 {
4507 sub->list.multi[subidx].start_lnum = save_lpos.lnum;
4508 sub->list.multi[subidx].start_col = save_lpos.col;
4509 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004510 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004511 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004512 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004513 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004514 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004515 break;
4516
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004517 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004518 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004519 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004520 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004521 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004522 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004523 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 break;
4525 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004526 case NFA_MCLOSE1:
4527 case NFA_MCLOSE2:
4528 case NFA_MCLOSE3:
4529 case NFA_MCLOSE4:
4530 case NFA_MCLOSE5:
4531 case NFA_MCLOSE6:
4532 case NFA_MCLOSE7:
4533 case NFA_MCLOSE8:
4534 case NFA_MCLOSE9:
4535#ifdef FEAT_SYN_HL
4536 case NFA_ZCLOSE:
4537 case NFA_ZCLOSE1:
4538 case NFA_ZCLOSE2:
4539 case NFA_ZCLOSE3:
4540 case NFA_ZCLOSE4:
4541 case NFA_ZCLOSE5:
4542 case NFA_ZCLOSE6:
4543 case NFA_ZCLOSE7:
4544 case NFA_ZCLOSE8:
4545 case NFA_ZCLOSE9:
4546#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004547 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004549 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004551 sub = &subs->norm;
4552 }
4553#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004554 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004555 {
4556 subidx = state->c - NFA_ZCLOSE;
4557 sub = &subs->synt;
4558 }
4559#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004560 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004561 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004562 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004563 sub = &subs->norm;
4564 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004565
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004566 /* We don't fill in gaps here, there must have been an MOPEN that
4567 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004568 save_in_use = sub->in_use;
4569 if (sub->in_use <= subidx)
4570 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004571 if (REG_MULTI)
4572 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004573 save_lpos.lnum = sub->list.multi[subidx].end_lnum;
4574 save_lpos.col = sub->list.multi[subidx].end_col;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004575 if (off == -1)
4576 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004577 sub->list.multi[subidx].end_lnum = reglnum + 1;
4578 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004579 }
4580 else
4581 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004582 sub->list.multi[subidx].end_lnum = reglnum;
4583 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004584 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004585 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004586 /* avoid compiler warnings */
4587 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588 }
4589 else
4590 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004591 save_ptr = sub->list.line[subidx].end;
4592 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004593 /* avoid compiler warnings */
4594 save_lpos.lnum = 0;
4595 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004596 }
4597
Bram Moolenaard05bf562013-06-30 23:24:08 +02004598 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004599 /* "subs" may have changed, need to set "sub" again */
4600#ifdef FEAT_SYN_HL
4601 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4602 sub = &subs->synt;
4603 else
4604#endif
4605 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004606
4607 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004608 {
4609 sub->list.multi[subidx].end_lnum = save_lpos.lnum;
4610 sub->list.multi[subidx].end_col = save_lpos.col;
4611 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004612 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004613 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004614 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004615 break;
4616 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004617 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004618}
4619
4620/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004621 * Like addstate(), but the new state(s) are put at position "*ip".
4622 * Used for zero-width matches, next state to use is the added one.
4623 * This makes sure the order of states to be tried does not change, which
4624 * matters for alternatives.
4625 */
4626 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004627addstate_here(
4628 nfa_list_T *l, /* runtime state list */
4629 nfa_state_T *state, /* state to update */
4630 regsubs_T *subs, /* pointers to subexpressions */
4631 nfa_pim_T *pim, /* postponed look-behind match */
4632 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004633{
4634 int tlen = l->n;
4635 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004636 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004637
4638 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004639 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004640
Bram Moolenaar4b417062013-05-25 20:19:50 +02004641 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004642 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004643 return;
4644
4645 /* re-order to put the new state at the current position */
4646 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004647 if (count == 0)
4648 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004649 if (count == 1)
4650 {
4651 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004652 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004653 }
4654 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004655 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004656 if (l->n + count - 1 >= l->len)
4657 {
4658 /* not enough space to move the new states, reallocate the list
4659 * and move the states to the right position */
4660 nfa_thread_T *newl;
4661
4662 l->len = l->len * 3 / 2 + 50;
4663 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4664 if (newl == NULL)
4665 return;
4666 mch_memmove(&(newl[0]),
4667 &(l->t[0]),
4668 sizeof(nfa_thread_T) * listidx);
4669 mch_memmove(&(newl[listidx]),
4670 &(l->t[l->n - count]),
4671 sizeof(nfa_thread_T) * count);
4672 mch_memmove(&(newl[listidx + count]),
4673 &(l->t[listidx + 1]),
4674 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4675 vim_free(l->t);
4676 l->t = newl;
4677 }
4678 else
4679 {
4680 /* make space for new states, then move them from the
4681 * end to the current position */
4682 mch_memmove(&(l->t[listidx + count]),
4683 &(l->t[listidx + 1]),
4684 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4685 mch_memmove(&(l->t[listidx]),
4686 &(l->t[l->n - 1]),
4687 sizeof(nfa_thread_T) * count);
4688 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004689 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004690 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004691 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004692}
4693
4694/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 * Check character class "class" against current character c.
4696 */
4697 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004698check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699{
4700 switch (class)
4701 {
4702 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004703 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004704 return OK;
4705 break;
4706 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004707 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 return OK;
4709 break;
4710 case NFA_CLASS_BLANK:
4711 if (c == ' ' || c == '\t')
4712 return OK;
4713 break;
4714 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004715 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004716 return OK;
4717 break;
4718 case NFA_CLASS_DIGIT:
4719 if (VIM_ISDIGIT(c))
4720 return OK;
4721 break;
4722 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004723 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 return OK;
4725 break;
4726 case NFA_CLASS_LOWER:
4727 if (MB_ISLOWER(c))
4728 return OK;
4729 break;
4730 case NFA_CLASS_PRINT:
4731 if (vim_isprintc(c))
4732 return OK;
4733 break;
4734 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004735 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736 return OK;
4737 break;
4738 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004739 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004740 return OK;
4741 break;
4742 case NFA_CLASS_UPPER:
4743 if (MB_ISUPPER(c))
4744 return OK;
4745 break;
4746 case NFA_CLASS_XDIGIT:
4747 if (vim_isxdigit(c))
4748 return OK;
4749 break;
4750 case NFA_CLASS_TAB:
4751 if (c == '\t')
4752 return OK;
4753 break;
4754 case NFA_CLASS_RETURN:
4755 if (c == '\r')
4756 return OK;
4757 break;
4758 case NFA_CLASS_BACKSPACE:
4759 if (c == '\b')
4760 return OK;
4761 break;
4762 case NFA_CLASS_ESCAPE:
4763 if (c == '\033')
4764 return OK;
4765 break;
4766
4767 default:
4768 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004769 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004770 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004771 }
4772 return FAIL;
4773}
4774
Bram Moolenaar5714b802013-05-28 22:03:20 +02004775/*
4776 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004777 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004778 */
4779 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004780match_backref(
4781 regsub_T *sub, /* pointers to subexpressions */
4782 int subidx,
4783 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004784{
4785 int len;
4786
4787 if (sub->in_use <= subidx)
4788 {
4789retempty:
4790 /* backref was not set, match an empty string */
4791 *bytelen = 0;
4792 return TRUE;
4793 }
4794
4795 if (REG_MULTI)
4796 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004797 if (sub->list.multi[subidx].start_lnum < 0
4798 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004799 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004800 if (sub->list.multi[subidx].start_lnum == reglnum
4801 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004802 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004803 len = sub->list.multi[subidx].end_col
4804 - sub->list.multi[subidx].start_col;
4805 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004806 reginput, &len) == 0)
4807 {
4808 *bytelen = len;
4809 return TRUE;
4810 }
4811 }
4812 else
4813 {
4814 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004815 sub->list.multi[subidx].start_lnum,
4816 sub->list.multi[subidx].start_col,
4817 sub->list.multi[subidx].end_lnum,
4818 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004819 bytelen) == RA_MATCH)
4820 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004821 }
4822 }
4823 else
4824 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004825 if (sub->list.line[subidx].start == NULL
4826 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004827 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004828 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4829 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004830 {
4831 *bytelen = len;
4832 return TRUE;
4833 }
4834 }
4835 return FALSE;
4836}
4837
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004838#ifdef FEAT_SYN_HL
4839
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004840static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004841
4842/*
4843 * Check for a match with \z subexpression "subidx".
4844 * Return TRUE if it matches.
4845 */
4846 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004847match_zref(
4848 int subidx,
4849 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004850{
4851 int len;
4852
4853 cleanup_zsubexpr();
4854 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4855 {
4856 /* backref was not set, match an empty string */
4857 *bytelen = 0;
4858 return TRUE;
4859 }
4860
4861 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4862 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4863 {
4864 *bytelen = len;
4865 return TRUE;
4866 }
4867 return FALSE;
4868}
4869#endif
4870
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004871/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004872 * Save list IDs for all NFA states of "prog" into "list".
4873 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004874 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004875 */
4876 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004877nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004879 int i;
4880 nfa_state_T *p;
4881
4882 /* Order in the list is reverse, it's a bit faster that way. */
4883 p = &prog->state[0];
4884 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004885 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004886 list[i] = p->lastlist[1];
4887 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004888 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004889 }
4890}
4891
4892/*
4893 * Restore list IDs from "list" to all NFA states.
4894 */
4895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004896nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004897{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004898 int i;
4899 nfa_state_T *p;
4900
4901 p = &prog->state[0];
4902 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004903 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004904 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004905 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004906 }
4907}
4908
Bram Moolenaar423532e2013-05-29 21:14:42 +02004909 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004910nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02004911{
4912 if (op == 1) return pos > val;
4913 if (op == 2) return pos < val;
4914 return val == pos;
4915}
4916
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004917static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
4918static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004919
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004921 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004922 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4923 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004924 */
4925 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004926recursive_regmatch(
4927 nfa_state_T *state,
4928 nfa_pim_T *pim,
4929 nfa_regprog_T *prog,
4930 regsubs_T *submatch,
4931 regsubs_T *m,
4932 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004933{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004934 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004935 int save_reglnum = reglnum;
4936 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004937 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004938 save_se_T *save_nfa_endp = nfa_endp;
4939 save_se_T endpos;
4940 save_se_T *endposp = NULL;
4941 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004942 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004943
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004944 if (pim != NULL)
4945 {
4946 /* start at the position where the postponed match was */
4947 if (REG_MULTI)
4948 reginput = regline + pim->end.pos.col;
4949 else
4950 reginput = pim->end.ptr;
4951 }
4952
Bram Moolenaardecd9542013-06-07 16:31:50 +02004953 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004954 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4955 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4956 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004957 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004958 /* The recursive match must end at the current position. When "pim" is
4959 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004960 endposp = &endpos;
4961 if (REG_MULTI)
4962 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004963 if (pim == NULL)
4964 {
4965 endpos.se_u.pos.col = (int)(reginput - regline);
4966 endpos.se_u.pos.lnum = reglnum;
4967 }
4968 else
4969 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004970 }
4971 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004972 {
4973 if (pim == NULL)
4974 endpos.se_u.ptr = reginput;
4975 else
4976 endpos.se_u.ptr = pim->end.ptr;
4977 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004978
4979 /* Go back the specified number of bytes, or as far as the
4980 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02004981 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004982 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004983 if (state->val <= 0)
4984 {
4985 if (REG_MULTI)
4986 {
4987 regline = reg_getline(--reglnum);
4988 if (regline == NULL)
4989 /* can't go before the first line */
4990 regline = reg_getline(++reglnum);
4991 }
4992 reginput = regline;
4993 }
4994 else
4995 {
4996 if (REG_MULTI && (int)(reginput - regline) < state->val)
4997 {
4998 /* Not enough bytes in this line, go to end of
4999 * previous line. */
5000 regline = reg_getline(--reglnum);
5001 if (regline == NULL)
5002 {
5003 /* can't go before the first line */
5004 regline = reg_getline(++reglnum);
5005 reginput = regline;
5006 }
5007 else
5008 reginput = regline + STRLEN(regline);
5009 }
5010 if ((int)(reginput - regline) >= state->val)
5011 {
5012 reginput -= state->val;
5013#ifdef FEAT_MBYTE
5014 if (has_mbyte)
5015 reginput -= mb_head_off(regline, reginput);
5016#endif
5017 }
5018 else
5019 reginput = regline;
5020 }
5021 }
5022
Bram Moolenaarf46da702013-06-02 22:37:42 +02005023#ifdef ENABLE_LOG
5024 if (log_fd != stderr)
5025 fclose(log_fd);
5026 log_fd = NULL;
5027#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005028 /* Have to clear the lastlist field of the NFA nodes, so that
5029 * nfa_regmatch() and addstate() can run properly after recursion. */
5030 if (nfa_ll_index == 1)
5031 {
5032 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5033 * values and clear them. */
5034 if (*listids == NULL)
5035 {
5036 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5037 if (*listids == NULL)
5038 {
5039 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5040 return 0;
5041 }
5042 }
5043 nfa_save_listids(prog, *listids);
5044 need_restore = TRUE;
5045 /* any value of nfa_listid will do */
5046 }
5047 else
5048 {
5049 /* First recursive nfa_regmatch() call, switch to the second lastlist
5050 * entry. Make sure nfa_listid is different from a previous recursive
5051 * call, because some states may still have this ID. */
5052 ++nfa_ll_index;
5053 if (nfa_listid <= nfa_alt_listid)
5054 nfa_listid = nfa_alt_listid;
5055 }
5056
5057 /* Call nfa_regmatch() to check if the current concat matches at this
5058 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005059 nfa_endp = endposp;
5060 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005061
5062 if (need_restore)
5063 nfa_restore_listids(prog, *listids);
5064 else
5065 {
5066 --nfa_ll_index;
5067 nfa_alt_listid = nfa_listid;
5068 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005069
5070 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005071 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005072 if (REG_MULTI)
5073 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005074 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005075 nfa_match = save_nfa_match;
5076 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005077 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005078
5079#ifdef ENABLE_LOG
5080 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5081 if (log_fd != NULL)
5082 {
5083 fprintf(log_fd, "****************************\n");
5084 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5085 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5086 fprintf(log_fd, "****************************\n");
5087 }
5088 else
5089 {
5090 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5091 log_fd = stderr;
5092 }
5093#endif
5094
5095 return result;
5096}
5097
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005098static int skip_to_start(int c, colnr_T *colp);
5099static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005100
5101/*
5102 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005103 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005104 * NFA_ANY: 1
5105 * specific character: 99
5106 */
5107 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005108failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005109{
5110 int c = state->c;
5111 int l, r;
5112
5113 /* detect looping */
5114 if (depth > 4)
5115 return 1;
5116
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005117 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005118 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005119 case NFA_SPLIT:
5120 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5121 /* avoid recursive stuff */
5122 return 1;
5123 /* two alternatives, use the lowest failure chance */
5124 l = failure_chance(state->out, depth + 1);
5125 r = failure_chance(state->out1, depth + 1);
5126 return l < r ? l : r;
5127
5128 case NFA_ANY:
5129 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005130 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005131
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005132 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005133 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005134 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005135 /* empty match works always */
5136 return 0;
5137
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005138 case NFA_START_INVISIBLE:
5139 case NFA_START_INVISIBLE_FIRST:
5140 case NFA_START_INVISIBLE_NEG:
5141 case NFA_START_INVISIBLE_NEG_FIRST:
5142 case NFA_START_INVISIBLE_BEFORE:
5143 case NFA_START_INVISIBLE_BEFORE_FIRST:
5144 case NFA_START_INVISIBLE_BEFORE_NEG:
5145 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5146 case NFA_START_PATTERN:
5147 /* recursive regmatch is expensive, use low failure chance */
5148 return 5;
5149
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005150 case NFA_BOL:
5151 case NFA_EOL:
5152 case NFA_BOF:
5153 case NFA_EOF:
5154 case NFA_NEWL:
5155 return 99;
5156
5157 case NFA_BOW:
5158 case NFA_EOW:
5159 return 90;
5160
5161 case NFA_MOPEN:
5162 case NFA_MOPEN1:
5163 case NFA_MOPEN2:
5164 case NFA_MOPEN3:
5165 case NFA_MOPEN4:
5166 case NFA_MOPEN5:
5167 case NFA_MOPEN6:
5168 case NFA_MOPEN7:
5169 case NFA_MOPEN8:
5170 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005171#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005172 case NFA_ZOPEN:
5173 case NFA_ZOPEN1:
5174 case NFA_ZOPEN2:
5175 case NFA_ZOPEN3:
5176 case NFA_ZOPEN4:
5177 case NFA_ZOPEN5:
5178 case NFA_ZOPEN6:
5179 case NFA_ZOPEN7:
5180 case NFA_ZOPEN8:
5181 case NFA_ZOPEN9:
5182 case NFA_ZCLOSE:
5183 case NFA_ZCLOSE1:
5184 case NFA_ZCLOSE2:
5185 case NFA_ZCLOSE3:
5186 case NFA_ZCLOSE4:
5187 case NFA_ZCLOSE5:
5188 case NFA_ZCLOSE6:
5189 case NFA_ZCLOSE7:
5190 case NFA_ZCLOSE8:
5191 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005192#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005193 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005194 case NFA_MCLOSE1:
5195 case NFA_MCLOSE2:
5196 case NFA_MCLOSE3:
5197 case NFA_MCLOSE4:
5198 case NFA_MCLOSE5:
5199 case NFA_MCLOSE6:
5200 case NFA_MCLOSE7:
5201 case NFA_MCLOSE8:
5202 case NFA_MCLOSE9:
5203 case NFA_NCLOSE:
5204 return failure_chance(state->out, depth + 1);
5205
5206 case NFA_BACKREF1:
5207 case NFA_BACKREF2:
5208 case NFA_BACKREF3:
5209 case NFA_BACKREF4:
5210 case NFA_BACKREF5:
5211 case NFA_BACKREF6:
5212 case NFA_BACKREF7:
5213 case NFA_BACKREF8:
5214 case NFA_BACKREF9:
5215#ifdef FEAT_SYN_HL
5216 case NFA_ZREF1:
5217 case NFA_ZREF2:
5218 case NFA_ZREF3:
5219 case NFA_ZREF4:
5220 case NFA_ZREF5:
5221 case NFA_ZREF6:
5222 case NFA_ZREF7:
5223 case NFA_ZREF8:
5224 case NFA_ZREF9:
5225#endif
5226 /* backreferences don't match in many places */
5227 return 94;
5228
5229 case NFA_LNUM_GT:
5230 case NFA_LNUM_LT:
5231 case NFA_COL_GT:
5232 case NFA_COL_LT:
5233 case NFA_VCOL_GT:
5234 case NFA_VCOL_LT:
5235 case NFA_MARK_GT:
5236 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005237 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005238 /* before/after positions don't match very often */
5239 return 85;
5240
5241 case NFA_LNUM:
5242 return 90;
5243
5244 case NFA_CURSOR:
5245 case NFA_COL:
5246 case NFA_VCOL:
5247 case NFA_MARK:
5248 /* specific positions rarely match */
5249 return 98;
5250
5251 case NFA_COMPOSING:
5252 return 95;
5253
5254 default:
5255 if (c > 0)
5256 /* character match fails often */
5257 return 95;
5258 }
5259
5260 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005261 return 50;
5262}
5263
Bram Moolenaarf46da702013-06-02 22:37:42 +02005264/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005265 * Skip until the char "c" we know a match must start with.
5266 */
5267 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005268skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005269{
5270 char_u *s;
5271
5272 /* Used often, do some work to avoid call overhead. */
5273 if (!ireg_ic
5274#ifdef FEAT_MBYTE
5275 && !has_mbyte
5276#endif
5277 )
5278 s = vim_strbyte(regline + *colp, c);
5279 else
5280 s = cstrchr(regline + *colp, c);
5281 if (s == NULL)
5282 return FAIL;
5283 *colp = (int)(s - regline);
5284 return OK;
5285}
5286
5287/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005288 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005289 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005290 * Returns zero for no match, 1 for a match.
5291 */
5292 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005293find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005294{
5295 colnr_T col = startcol;
5296 int c1, c2;
5297 int len1, len2;
5298 int match;
5299
5300 for (;;)
5301 {
5302 match = TRUE;
5303 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5304 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5305 {
5306 c1 = PTR2CHAR(match_text + len1);
5307 c2 = PTR2CHAR(regline + col + len2);
5308 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5309 {
5310 match = FALSE;
5311 break;
5312 }
5313 len2 += MB_CHAR2LEN(c2);
5314 }
5315 if (match
5316#ifdef FEAT_MBYTE
5317 /* check that no composing char follows */
5318 && !(enc_utf8
5319 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5320#endif
5321 )
5322 {
5323 cleanup_subexpr();
5324 if (REG_MULTI)
5325 {
5326 reg_startpos[0].lnum = reglnum;
5327 reg_startpos[0].col = col;
5328 reg_endpos[0].lnum = reglnum;
5329 reg_endpos[0].col = col + len2;
5330 }
5331 else
5332 {
5333 reg_startp[0] = regline + col;
5334 reg_endp[0] = regline + col + len2;
5335 }
5336 return 1L;
5337 }
5338
5339 /* Try finding regstart after the current match. */
5340 col += MB_CHAR2LEN(regstart); /* skip regstart */
5341 if (skip_to_start(regstart, &col) == FAIL)
5342 break;
5343 }
5344 return 0L;
5345}
5346
5347/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005348 * Main matching routine.
5349 *
5350 * Run NFA to determine whether it matches reginput.
5351 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005352 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005353 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005354 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005355 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005356 * Note: Caller must ensure that: start != NULL.
5357 */
5358 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005359nfa_regmatch(
5360 nfa_regprog_T *prog,
5361 nfa_state_T *start,
5362 regsubs_T *submatch,
5363 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005364{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005365 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005366 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005367 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005368 int go_to_nextline = FALSE;
5369 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005370 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005371 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005372 nfa_list_T *thislist;
5373 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005374 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005375 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005376 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005377 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005378 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005379 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005380#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005381 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005382
5383 if (debug == NULL)
5384 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005385 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005386 return FALSE;
5387 }
5388#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005389 /* Some patterns may take a long time to match, especially when using
5390 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5391 fast_breakcheck();
5392 if (got_int)
5393 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005394#ifdef FEAT_RELTIME
5395 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5396 return FALSE;
5397#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005398
Bram Moolenaar963fee22013-05-26 21:47:28 +02005399 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005400
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005401 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005402 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005403
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005404 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005405 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005406 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005407 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005408 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005409 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410
5411#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005412 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005413 if (log_fd != NULL)
5414 {
5415 fprintf(log_fd, "**********************************\n");
5416 nfa_set_code(start->c);
5417 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5418 abs(start->id), code);
5419 fprintf(log_fd, "**********************************\n");
5420 }
5421 else
5422 {
5423 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5424 log_fd = stderr;
5425 }
5426#endif
5427
5428 thislist = &list[0];
5429 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005430 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005431 nextlist = &list[1];
5432 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005433 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005434#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005435 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005436#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005437 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005438
5439 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5440 * it's the first MOPEN. */
5441 if (toplevel)
5442 {
5443 if (REG_MULTI)
5444 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005445 m->norm.list.multi[0].start_lnum = reglnum;
5446 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005447 }
5448 else
5449 m->norm.list.line[0].start = reginput;
5450 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005451 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005452 }
5453 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005454 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005455
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005456#define ADD_STATE_IF_MATCH(state) \
5457 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005458 add_state = state->out; \
5459 add_off = clen; \
5460 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005461
5462 /*
5463 * Run for each character.
5464 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005465 for (;;)
5466 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005467 int curc;
5468 int clen;
5469
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005470#ifdef FEAT_MBYTE
5471 if (has_mbyte)
5472 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005473 curc = (*mb_ptr2char)(reginput);
5474 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005475 }
5476 else
5477#endif
5478 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005479 curc = *reginput;
5480 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005481 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005482 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005483 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005484 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005485 go_to_nextline = FALSE;
5486 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005487
5488 /* swap lists */
5489 thislist = &list[flag];
5490 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005491 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005492 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005493 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005494 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5495 {
5496 /* too many states, retry with old engine */
5497 nfa_match = NFA_TOO_EXPENSIVE;
5498 goto theend;
5499 }
5500
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005501 thislist->id = nfa_listid;
5502 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503
5504#ifdef ENABLE_LOG
5505 fprintf(log_fd, "------------------------------------------\n");
5506 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005507 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005508 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005509 {
5510 int i;
5511
5512 for (i = 0; i < thislist->n; i++)
5513 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5514 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515 fprintf(log_fd, "\n");
5516#endif
5517
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005518#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005519 fprintf(debug, "\n-------------------\n");
5520#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005521 /*
5522 * If the state lists are empty we can stop.
5523 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005524 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005525 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526
5527 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005528 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005530 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005532#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533 nfa_set_code(t->state->c);
5534 fprintf(debug, "%s, ", code);
5535#endif
5536#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005537 {
5538 int col;
5539
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005540 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005541 col = -1;
5542 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005543 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005544 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005545 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005546 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005547 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5548 abs(t->state->id), (int)t->state->c, code, col,
5549 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005550 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005551#endif
5552
5553 /*
5554 * Handle the possible codes of the current state.
5555 * The most important is NFA_MATCH.
5556 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005557 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005558 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005559 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005560 switch (t->state->c)
5561 {
5562 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005563 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005564#ifdef FEAT_MBYTE
5565 /* If the match ends before a composing characters and
5566 * ireg_icombine is not set, that is not really a match. */
5567 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5568 break;
5569#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005570 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005571 copy_sub(&submatch->norm, &t->subs.norm);
5572#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005573 if (nfa_has_zsubexpr)
5574 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005575#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005576#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005577 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005578#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005579 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005580 * states at this position. When the list of states is going
5581 * to be empty quit without advancing, so that "reginput" is
5582 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005583 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005584 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005585 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005586 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005587
5588 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005589 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005590 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005591 /*
5592 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005593 * NFA_START_INVISIBLE_BEFORE node.
5594 * They surround a zero-width group, used with "\@=", "\&",
5595 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005596 * If we got here, it means that the current "invisible" group
5597 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005598 * nfa_regmatch(). For a look-behind match only when it ends
5599 * in the position in "nfa_endp".
5600 * Submatches are stored in *m, and used in the parent call.
5601 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005602#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005603 if (nfa_endp != NULL)
5604 {
5605 if (REG_MULTI)
5606 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5607 (int)reglnum,
5608 (int)nfa_endp->se_u.pos.lnum,
5609 (int)(reginput - regline),
5610 nfa_endp->se_u.pos.col);
5611 else
5612 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5613 (int)(reginput - regline),
5614 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005615 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005616#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005617 /* If "nfa_endp" is set it's only a match if it ends at
5618 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005619 if (nfa_endp != NULL && (REG_MULTI
5620 ? (reglnum != nfa_endp->se_u.pos.lnum
5621 || (int)(reginput - regline)
5622 != nfa_endp->se_u.pos.col)
5623 : reginput != nfa_endp->se_u.ptr))
5624 break;
5625
5626 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005627 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005628 {
5629 copy_sub(&m->norm, &t->subs.norm);
5630#ifdef FEAT_SYN_HL
5631 if (nfa_has_zsubexpr)
5632 copy_sub(&m->synt, &t->subs.synt);
5633#endif
5634 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005635#ifdef ENABLE_LOG
5636 fprintf(log_fd, "Match found:\n");
5637 log_subsexpr(m);
5638#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005639 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005640 /* See comment above at "goto nextchar". */
5641 if (nextlist->n == 0)
5642 clen = 0;
5643 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005644
5645 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005646 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005647 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005648 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005649 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005650 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005651 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005652 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005653 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005654#ifdef ENABLE_LOG
5655 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5656 failure_chance(t->state->out, 0),
5657 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005658#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005659 /* Do it directly if there already is a PIM or when
5660 * nfa_postprocess() detected it will work better. */
5661 if (t->pim.result != NFA_PIM_UNUSED
5662 || t->state->c == NFA_START_INVISIBLE_FIRST
5663 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5664 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5665 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005666 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005667 int in_use = m->norm.in_use;
5668
Bram Moolenaar699c1202013-09-25 16:41:54 +02005669 /* Copy submatch info for the recursive call, opposite
5670 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005671 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005672#ifdef FEAT_SYN_HL
5673 if (nfa_has_zsubexpr)
5674 copy_sub_off(&m->synt, &t->subs.synt);
5675#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005676
Bram Moolenaara2d95102013-06-04 14:23:05 +02005677 /*
5678 * First try matching the invisible match, then what
5679 * follows.
5680 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005681 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005682 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005683 if (result == NFA_TOO_EXPENSIVE)
5684 {
5685 nfa_match = result;
5686 goto theend;
5687 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005688
Bram Moolenaardecd9542013-06-07 16:31:50 +02005689 /* for \@! and \@<! it is a match when the result is
5690 * FALSE */
5691 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005692 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5693 || t->state->c
5694 == NFA_START_INVISIBLE_BEFORE_NEG
5695 || t->state->c
5696 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005697 {
5698 /* Copy submatch info from the recursive call */
5699 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005700#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005701 if (nfa_has_zsubexpr)
5702 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005703#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005704 /* If the pattern has \ze and it matched in the
5705 * sub pattern, use it. */
5706 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005707
Bram Moolenaara2d95102013-06-04 14:23:05 +02005708 /* t->state->out1 is the corresponding
5709 * END_INVISIBLE node; Add its out to the current
5710 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005711 add_here = TRUE;
5712 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005713 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005714 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005715 }
5716 else
5717 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005718 nfa_pim_T pim;
5719
Bram Moolenaara2d95102013-06-04 14:23:05 +02005720 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005721 * First try matching what follows. Only if a match
5722 * is found verify the invisible match matches. Add a
5723 * nfa_pim_T to the following states, it contains info
5724 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005725 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005726 pim.state = t->state;
5727 pim.result = NFA_PIM_TODO;
5728 pim.subs.norm.in_use = 0;
5729#ifdef FEAT_SYN_HL
5730 pim.subs.synt.in_use = 0;
5731#endif
5732 if (REG_MULTI)
5733 {
5734 pim.end.pos.col = (int)(reginput - regline);
5735 pim.end.pos.lnum = reglnum;
5736 }
5737 else
5738 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005739
5740 /* t->state->out1 is the corresponding END_INVISIBLE
5741 * node; Add its out to the current list (zero-width
5742 * match). */
5743 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005744 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005745 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005746 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005747 break;
5748
Bram Moolenaar87953742013-06-05 18:52:40 +02005749 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005750 {
5751 nfa_state_T *skip = NULL;
5752#ifdef ENABLE_LOG
5753 int skip_lid = 0;
5754#endif
5755
5756 /* There is no point in trying to match the pattern if the
5757 * output state is not going to be added to the list. */
5758 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5759 {
5760 skip = t->state->out1->out;
5761#ifdef ENABLE_LOG
5762 skip_lid = nextlist->id;
5763#endif
5764 }
5765 else if (state_in_list(nextlist,
5766 t->state->out1->out->out, &t->subs))
5767 {
5768 skip = t->state->out1->out->out;
5769#ifdef ENABLE_LOG
5770 skip_lid = nextlist->id;
5771#endif
5772 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005773 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005774 t->state->out1->out->out, &t->subs))
5775 {
5776 skip = t->state->out1->out->out;
5777#ifdef ENABLE_LOG
5778 skip_lid = thislist->id;
5779#endif
5780 }
5781 if (skip != NULL)
5782 {
5783#ifdef ENABLE_LOG
5784 nfa_set_code(skip->c);
5785 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5786 abs(skip->id), skip_lid, skip->c, code);
5787#endif
5788 break;
5789 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005790 /* Copy submatch info to the recursive call, opposite of what
5791 * happens afterwards. */
5792 copy_sub_off(&m->norm, &t->subs.norm);
5793#ifdef FEAT_SYN_HL
5794 if (nfa_has_zsubexpr)
5795 copy_sub_off(&m->synt, &t->subs.synt);
5796#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005797
Bram Moolenaar87953742013-06-05 18:52:40 +02005798 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005799 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005800 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005801 if (result == NFA_TOO_EXPENSIVE)
5802 {
5803 nfa_match = result;
5804 goto theend;
5805 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005806 if (result)
5807 {
5808 int bytelen;
5809
5810#ifdef ENABLE_LOG
5811 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5812 log_subsexpr(m);
5813#endif
5814 /* Copy submatch info from the recursive call */
5815 copy_sub_off(&t->subs.norm, &m->norm);
5816#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005817 if (nfa_has_zsubexpr)
5818 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005819#endif
5820 /* Now we need to skip over the matched text and then
5821 * continue with what follows. */
5822 if (REG_MULTI)
5823 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005824 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005825 - (int)(reginput - regline);
5826 else
5827 bytelen = (int)(m->norm.list.line[0].end - reginput);
5828
5829#ifdef ENABLE_LOG
5830 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5831#endif
5832 if (bytelen == 0)
5833 {
5834 /* empty match, output of corresponding
5835 * NFA_END_PATTERN/NFA_SKIP to be used at current
5836 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005837 add_here = TRUE;
5838 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005839 }
5840 else if (bytelen <= clen)
5841 {
5842 /* match current character, output of corresponding
5843 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005844 add_state = t->state->out1->out->out;
5845 add_off = clen;
5846 }
5847 else
5848 {
5849 /* skip over the matched characters, set character
5850 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005851 add_state = t->state->out1->out;
5852 add_off = bytelen;
5853 add_count = bytelen - clen;
5854 }
5855 }
5856 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005857 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005858
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005859 case NFA_BOL:
5860 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005861 {
5862 add_here = TRUE;
5863 add_state = t->state->out;
5864 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005865 break;
5866
5867 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005868 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005869 {
5870 add_here = TRUE;
5871 add_state = t->state->out;
5872 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005873 break;
5874
5875 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005876 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005877
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005878 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005879 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005880#ifdef FEAT_MBYTE
5881 else if (has_mbyte)
5882 {
5883 int this_class;
5884
5885 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005886 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005887 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005888 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005889 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005890 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005891 }
5892#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005893 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005894 || (reginput > regline
5895 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005896 result = FALSE;
5897 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005898 {
5899 add_here = TRUE;
5900 add_state = t->state->out;
5901 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005902 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005903
5904 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005905 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005906 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005907 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005908#ifdef FEAT_MBYTE
5909 else if (has_mbyte)
5910 {
5911 int this_class, prev_class;
5912
5913 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005914 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005915 prev_class = reg_prev_class();
5916 if (this_class == prev_class
5917 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005918 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005919 }
5920#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005921 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005922 || (reginput[0] != NUL
5923 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005924 result = FALSE;
5925 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005926 {
5927 add_here = TRUE;
5928 add_state = t->state->out;
5929 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005930 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005931
Bram Moolenaar4b780632013-05-31 22:14:52 +02005932 case NFA_BOF:
5933 if (reglnum == 0 && reginput == regline
5934 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005935 {
5936 add_here = TRUE;
5937 add_state = t->state->out;
5938 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005939 break;
5940
5941 case NFA_EOF:
5942 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005943 {
5944 add_here = TRUE;
5945 add_state = t->state->out;
5946 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005947 break;
5948
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005949#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005950 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005951 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005952 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005953 int len = 0;
5954 nfa_state_T *end;
5955 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005956 int cchars[MAX_MCO];
5957 int ccount = 0;
5958 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005959
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005960 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005961 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005962 if (utf_iscomposing(sta->c))
5963 {
5964 /* Only match composing character(s), ignore base
5965 * character. Used for ".{composing}" and "{composing}"
5966 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005967 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005968 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005969 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005970 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005971 /* If \Z was present, then ignore composing characters.
5972 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005973 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005974 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005975 else
5976 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005977 while (sta->c != NFA_END_COMPOSING)
5978 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005979 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005980
5981 /* Check base character matches first, unless ignored. */
5982 else if (len > 0 || mc == sta->c)
5983 {
5984 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005985 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005986 len += mb_char2len(mc);
5987 sta = sta->out;
5988 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005989
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005990 /* We don't care about the order of composing characters.
5991 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005992 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005993 {
5994 mc = mb_ptr2char(reginput + len);
5995 cchars[ccount++] = mc;
5996 len += mb_char2len(mc);
5997 if (ccount == MAX_MCO)
5998 break;
5999 }
6000
6001 /* Check that each composing char in the pattern matches a
6002 * composing char in the text. We do not check if all
6003 * composing chars are matched. */
6004 result = OK;
6005 while (sta->c != NFA_END_COMPOSING)
6006 {
6007 for (j = 0; j < ccount; ++j)
6008 if (cchars[j] == sta->c)
6009 break;
6010 if (j == ccount)
6011 {
6012 result = FAIL;
6013 break;
6014 }
6015 sta = sta->out;
6016 }
6017 }
6018 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006019 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006020
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006021 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006022 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006023 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006024 }
6025#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006026
6027 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006028 if (curc == NUL && !reg_line_lbr && REG_MULTI
6029 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006030 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006031 go_to_nextline = TRUE;
6032 /* Pass -1 for the offset, which means taking the position
6033 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006034 add_state = t->state->out;
6035 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006036 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006037 else if (curc == '\n' && reg_line_lbr)
6038 {
6039 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006040 add_state = t->state->out;
6041 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006042 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006043 break;
6044
Bram Moolenaar417bad22013-06-07 14:08:30 +02006045 case NFA_START_COLL:
6046 case NFA_START_NEG_COLL:
6047 {
6048 /* What follows is a list of characters, until NFA_END_COLL.
6049 * One of them must match or none of them must match. */
6050 nfa_state_T *state;
6051 int result_if_matched;
6052 int c1, c2;
6053
6054 /* Never match EOL. If it's part of the collection it is added
6055 * as a separate state with an OR. */
6056 if (curc == NUL)
6057 break;
6058
6059 state = t->state->out;
6060 result_if_matched = (t->state->c == NFA_START_COLL);
6061 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006062 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006063 if (state->c == NFA_END_COLL)
6064 {
6065 result = !result_if_matched;
6066 break;
6067 }
6068 if (state->c == NFA_RANGE_MIN)
6069 {
6070 c1 = state->val;
6071 state = state->out; /* advance to NFA_RANGE_MAX */
6072 c2 = state->val;
6073#ifdef ENABLE_LOG
6074 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6075 curc, c1, c2);
6076#endif
6077 if (curc >= c1 && curc <= c2)
6078 {
6079 result = result_if_matched;
6080 break;
6081 }
6082 if (ireg_ic)
6083 {
6084 int curc_low = MB_TOLOWER(curc);
6085 int done = FALSE;
6086
6087 for ( ; c1 <= c2; ++c1)
6088 if (MB_TOLOWER(c1) == curc_low)
6089 {
6090 result = result_if_matched;
6091 done = TRUE;
6092 break;
6093 }
6094 if (done)
6095 break;
6096 }
6097 }
6098 else if (state->c < 0 ? check_char_class(state->c, curc)
6099 : (curc == state->c
6100 || (ireg_ic && MB_TOLOWER(curc)
6101 == MB_TOLOWER(state->c))))
6102 {
6103 result = result_if_matched;
6104 break;
6105 }
6106 state = state->out;
6107 }
6108 if (result)
6109 {
6110 /* next state is in out of the NFA_END_COLL, out1 of
6111 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006112 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006113 add_off = clen;
6114 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006116 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117
6118 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006119 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006120 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006121 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006122 add_state = t->state->out;
6123 add_off = clen;
6124 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006125 break;
6126
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006127 case NFA_ANY_COMPOSING:
6128 /* On a composing character skip over it. Otherwise do
6129 * nothing. Always matches. */
6130#ifdef FEAT_MBYTE
6131 if (enc_utf8 && utf_iscomposing(curc))
6132 {
6133 add_off = clen;
6134 }
6135 else
6136#endif
6137 {
6138 add_here = TRUE;
6139 add_off = 0;
6140 }
6141 add_state = t->state->out;
6142 break;
6143
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006144 /*
6145 * Character classes like \a for alpha, \d for digit etc.
6146 */
6147 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006148 result = 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_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006153 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
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_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006158 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006159 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006160 break;
6161
6162 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006163 result = !VIM_ISDIGIT(curc)
6164 && vim_iswordp_buf(reginput, reg_buf);
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_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006169 result = 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_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006174 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
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_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006179 result = 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_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006184 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
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_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006189 result = 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_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006194 result = curc != NUL && !vim_iswhite(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_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006199 result = 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_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006204 result = curc != NUL && !ri_digit(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_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006209 result = 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_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006214 result = curc != NUL && !ri_hex(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_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006219 result = 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_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006224 result = curc != NUL && !ri_octal(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_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006229 result = 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_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006234 result = curc != NUL && !ri_word(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_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006239 result = 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_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006244 result = curc != NUL && !ri_head(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_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006249 result = 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_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006254 result = curc != NUL && !ri_alpha(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_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006259 result = 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_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006264 result = curc != NUL && !ri_lower(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_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006269 result = 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
6273 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006274 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006275 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006276 break;
6277
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006278 case NFA_LOWER_IC: /* [a-z] */
6279 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6280 ADD_STATE_IF_MATCH(t->state);
6281 break;
6282
6283 case NFA_NLOWER_IC: /* [^a-z] */
6284 result = curc != NUL
6285 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6286 ADD_STATE_IF_MATCH(t->state);
6287 break;
6288
6289 case NFA_UPPER_IC: /* [A-Z] */
6290 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6291 ADD_STATE_IF_MATCH(t->state);
6292 break;
6293
6294 case NFA_NUPPER_IC: /* ^[A-Z] */
6295 result = curc != NUL
6296 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6297 ADD_STATE_IF_MATCH(t->state);
6298 break;
6299
Bram Moolenaar5714b802013-05-28 22:03:20 +02006300 case NFA_BACKREF1:
6301 case NFA_BACKREF2:
6302 case NFA_BACKREF3:
6303 case NFA_BACKREF4:
6304 case NFA_BACKREF5:
6305 case NFA_BACKREF6:
6306 case NFA_BACKREF7:
6307 case NFA_BACKREF8:
6308 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006309#ifdef FEAT_SYN_HL
6310 case NFA_ZREF1:
6311 case NFA_ZREF2:
6312 case NFA_ZREF3:
6313 case NFA_ZREF4:
6314 case NFA_ZREF5:
6315 case NFA_ZREF6:
6316 case NFA_ZREF7:
6317 case NFA_ZREF8:
6318 case NFA_ZREF9:
6319#endif
6320 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006321 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006322 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006323 int bytelen;
6324
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006325 if (t->state->c <= NFA_BACKREF9)
6326 {
6327 subidx = t->state->c - NFA_BACKREF1 + 1;
6328 result = match_backref(&t->subs.norm, subidx, &bytelen);
6329 }
6330#ifdef FEAT_SYN_HL
6331 else
6332 {
6333 subidx = t->state->c - NFA_ZREF1 + 1;
6334 result = match_zref(subidx, &bytelen);
6335 }
6336#endif
6337
Bram Moolenaar5714b802013-05-28 22:03:20 +02006338 if (result)
6339 {
6340 if (bytelen == 0)
6341 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006342 /* empty match always works, output of NFA_SKIP to be
6343 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006344 add_here = TRUE;
6345 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006346 }
6347 else if (bytelen <= clen)
6348 {
6349 /* match current character, jump ahead to out of
6350 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006351 add_state = t->state->out->out;
6352 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006353 }
6354 else
6355 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006356 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006357 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006358 add_state = t->state->out;
6359 add_off = bytelen;
6360 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006361 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006362 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006363 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006364 }
6365 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006366 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006367 if (t->count - clen <= 0)
6368 {
6369 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006370 add_state = t->state->out;
6371 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006372 }
6373 else
6374 {
6375 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006376 add_state = t->state;
6377 add_off = 0;
6378 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006379 }
6380 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006381
Bram Moolenaar423532e2013-05-29 21:14:42 +02006382 case NFA_LNUM:
6383 case NFA_LNUM_GT:
6384 case NFA_LNUM_LT:
6385 result = (REG_MULTI &&
6386 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6387 (long_u)(reglnum + reg_firstlnum)));
6388 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006389 {
6390 add_here = TRUE;
6391 add_state = t->state->out;
6392 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006393 break;
6394
6395 case NFA_COL:
6396 case NFA_COL_GT:
6397 case NFA_COL_LT:
6398 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6399 (long_u)(reginput - regline) + 1);
6400 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006401 {
6402 add_here = TRUE;
6403 add_state = t->state->out;
6404 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006405 break;
6406
6407 case NFA_VCOL:
6408 case NFA_VCOL_GT:
6409 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006410 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006411 int op = t->state->c - NFA_VCOL;
6412 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaaref795d12015-01-18 16:46:32 +01006413 win_T *wp = reg_win == NULL ? curwin : reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006414
6415 /* Bail out quickly when there can't be a match, avoid the
6416 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006417 if (op != 1 && col > t->state->val
6418#ifdef FEAT_MBYTE
6419 * (has_mbyte ? MB_MAXBYTES : 1)
6420#endif
6421 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006422 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006423 result = FALSE;
6424 if (op == 1 && col - 1 > t->state->val && col > 100)
6425 {
6426 int ts = wp->w_buffer->b_p_ts;
6427
6428 /* Guess that a character won't use more columns than
6429 * 'tabstop', with a minimum of 4. */
6430 if (ts < 4)
6431 ts = 4;
6432 result = col > t->state->val * ts;
6433 }
6434 if (!result)
6435 result = nfa_re_num_cmp(t->state->val, op,
6436 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006437 if (result)
6438 {
6439 add_here = TRUE;
6440 add_state = t->state->out;
6441 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006442 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006443 break;
6444
Bram Moolenaar044aa292013-06-04 21:27:38 +02006445 case NFA_MARK:
6446 case NFA_MARK_GT:
6447 case NFA_MARK_LT:
6448 {
6449 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6450
6451 /* Compare the mark position to the match position. */
6452 result = (pos != NULL /* mark doesn't exist */
6453 && pos->lnum > 0 /* mark isn't set in reg_buf */
6454 && (pos->lnum == reglnum + reg_firstlnum
6455 ? (pos->col == (colnr_T)(reginput - regline)
6456 ? t->state->c == NFA_MARK
6457 : (pos->col < (colnr_T)(reginput - regline)
6458 ? t->state->c == NFA_MARK_GT
6459 : t->state->c == NFA_MARK_LT))
6460 : (pos->lnum < reglnum + reg_firstlnum
6461 ? t->state->c == NFA_MARK_GT
6462 : t->state->c == NFA_MARK_LT)));
6463 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006464 {
6465 add_here = TRUE;
6466 add_state = t->state->out;
6467 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006468 break;
6469 }
6470
Bram Moolenaar423532e2013-05-29 21:14:42 +02006471 case NFA_CURSOR:
6472 result = (reg_win != NULL
6473 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6474 && ((colnr_T)(reginput - regline)
6475 == reg_win->w_cursor.col));
6476 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006477 {
6478 add_here = TRUE;
6479 add_state = t->state->out;
6480 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006481 break;
6482
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006483 case NFA_VISUAL:
6484 result = reg_match_visual();
6485 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006486 {
6487 add_here = TRUE;
6488 add_state = t->state->out;
6489 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006490 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006491
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006492 case NFA_MOPEN1:
6493 case NFA_MOPEN2:
6494 case NFA_MOPEN3:
6495 case NFA_MOPEN4:
6496 case NFA_MOPEN5:
6497 case NFA_MOPEN6:
6498 case NFA_MOPEN7:
6499 case NFA_MOPEN8:
6500 case NFA_MOPEN9:
6501#ifdef FEAT_SYN_HL
6502 case NFA_ZOPEN:
6503 case NFA_ZOPEN1:
6504 case NFA_ZOPEN2:
6505 case NFA_ZOPEN3:
6506 case NFA_ZOPEN4:
6507 case NFA_ZOPEN5:
6508 case NFA_ZOPEN6:
6509 case NFA_ZOPEN7:
6510 case NFA_ZOPEN8:
6511 case NFA_ZOPEN9:
6512#endif
6513 case NFA_NOPEN:
6514 case NFA_ZSTART:
6515 /* These states are only added to be able to bail out when
6516 * they are added again, nothing is to be done. */
6517 break;
6518
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006519 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006520 {
6521 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006522
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006523#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006524 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006525 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006526#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006527 result = (c == curc);
6528
6529 if (!result && ireg_ic)
6530 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006531#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006532 /* If ireg_icombine is not set only skip over the character
6533 * itself. When it is set skip over composing characters. */
6534 if (result && enc_utf8 && !ireg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006535 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006536#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006537 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006538 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006539 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006540
6541 } /* switch (t->state->c) */
6542
6543 if (add_state != NULL)
6544 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006545 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006546 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006547
6548 if (t->pim.result == NFA_PIM_UNUSED)
6549 pim = NULL;
6550 else
6551 pim = &t->pim;
6552
6553 /* Handle the postponed invisible match if the match might end
6554 * without advancing and before the end of the line. */
6555 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006556 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006557 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006558 {
6559#ifdef ENABLE_LOG
6560 fprintf(log_fd, "\n");
6561 fprintf(log_fd, "==================================\n");
6562 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6563 fprintf(log_fd, "\n");
6564#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006565 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006566 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006567 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006568 /* for \@! and \@<! it is a match when the result is
6569 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006570 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006571 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6572 || pim->state->c
6573 == NFA_START_INVISIBLE_BEFORE_NEG
6574 || pim->state->c
6575 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006576 {
6577 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006578 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006579#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006580 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006581 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006582#endif
6583 }
6584 }
6585 else
6586 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006587 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006588#ifdef ENABLE_LOG
6589 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006590 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006591 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6592 fprintf(log_fd, "\n");
6593#endif
6594 }
6595
Bram Moolenaardecd9542013-06-07 16:31:50 +02006596 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006597 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006598 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6599 || pim->state->c
6600 == NFA_START_INVISIBLE_BEFORE_NEG
6601 || pim->state->c
6602 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006603 {
6604 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006605 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006606#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006607 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006608 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006609#endif
6610 }
6611 else
6612 /* look-behind match failed, don't add the state */
6613 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006614
6615 /* Postponed invisible match was handled, don't add it to
6616 * following states. */
6617 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006618 }
6619
Bram Moolenaara951e352013-10-06 15:46:11 +02006620 /* If "pim" points into l->t it will become invalid when
6621 * adding the state causes the list to be reallocated. Make a
6622 * local copy to avoid that. */
6623 if (pim == &t->pim)
6624 {
6625 copy_pim(&pim_copy, pim);
6626 pim = &pim_copy;
6627 }
6628
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006629 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006630 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006631 else
6632 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006633 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006634 if (add_count > 0)
6635 nextlist->t[nextlist->n - 1].count = add_count;
6636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006637 }
6638
6639 } /* for (thislist = thislist; thislist->state; thislist++) */
6640
Bram Moolenaare23febd2013-05-26 18:40:14 +02006641 /* Look for the start of a match in the current position by adding the
6642 * start state to the list of states.
6643 * The first found match is the leftmost one, thus the order of states
6644 * matters!
6645 * Do not add the start state in recursive calls of nfa_regmatch(),
6646 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006647 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006648 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006649 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006650 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006651 && reglnum == 0
6652 && clen != 0
6653 && (ireg_maxcol == 0
6654 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006655 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006656 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006657 ? (reglnum < nfa_endp->se_u.pos.lnum
6658 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006659 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006660 < nfa_endp->se_u.pos.col))
6661 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006662 {
6663#ifdef ENABLE_LOG
6664 fprintf(log_fd, "(---) STARTSTATE\n");
6665#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006666 /* Inline optimized code for addstate() if we know the state is
6667 * the first MOPEN. */
6668 if (toplevel)
6669 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006670 int add = TRUE;
6671 int c;
6672
6673 if (prog->regstart != NUL && clen != 0)
6674 {
6675 if (nextlist->n == 0)
6676 {
6677 colnr_T col = (colnr_T)(reginput - regline) + clen;
6678
6679 /* Nextlist is empty, we can skip ahead to the
6680 * character that must appear at the start. */
6681 if (skip_to_start(prog->regstart, &col) == FAIL)
6682 break;
6683#ifdef ENABLE_LOG
6684 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6685 col - ((colnr_T)(reginput - regline) + clen));
6686#endif
6687 reginput = regline + col - clen;
6688 }
6689 else
6690 {
6691 /* Checking if the required start character matches is
6692 * cheaper than adding a state that won't match. */
6693 c = PTR2CHAR(reginput + clen);
6694 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6695 != MB_TOLOWER(prog->regstart)))
6696 {
6697#ifdef ENABLE_LOG
6698 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6699#endif
6700 add = FALSE;
6701 }
6702 }
6703 }
6704
6705 if (add)
6706 {
6707 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006708 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006709 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006710 else
6711 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006712 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006713 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006714 }
6715 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006716 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006717 }
6718
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006719#ifdef ENABLE_LOG
6720 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006721 {
6722 int i;
6723
6724 for (i = 0; i < thislist->n; i++)
6725 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6726 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006727 fprintf(log_fd, "\n");
6728#endif
6729
6730nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006731 /* Advance to the next character, or advance to the next line, or
6732 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006733 if (clen != 0)
6734 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006735 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6736 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006737 reg_nextline();
6738 else
6739 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006740
6741 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006742 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006743 if (got_int)
6744 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006745#ifdef FEAT_RELTIME
6746 /* Check for timeout once in a twenty times to avoid overhead. */
6747 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6748 {
6749 nfa_time_count = 0;
6750 if (profile_passed_limit(nfa_time_limit))
6751 break;
6752 }
6753#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006754 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006755
6756#ifdef ENABLE_LOG
6757 if (log_fd != stderr)
6758 fclose(log_fd);
6759 log_fd = NULL;
6760#endif
6761
6762theend:
6763 /* Free memory */
6764 vim_free(list[0].t);
6765 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006766 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006767#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006768#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006769 fclose(debug);
6770#endif
6771
Bram Moolenaar963fee22013-05-26 21:47:28 +02006772 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006773}
6774
6775/*
6776 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006777 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006778 */
6779 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006780nfa_regtry(
6781 nfa_regprog_T *prog,
6782 colnr_T col,
6783 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006784{
6785 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006786 regsubs_T subs, m;
6787 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006788 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006789#ifdef ENABLE_LOG
6790 FILE *f;
6791#endif
6792
6793 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006794#ifdef FEAT_RELTIME
6795 nfa_time_limit = tm;
6796 nfa_time_count = 0;
6797#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006798
6799#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006800 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006801 if (f != NULL)
6802 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006803 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006804#ifdef DEBUG
6805 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6806#endif
6807 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006808 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006809 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006810 fprintf(f, "\n\n");
6811 fclose(f);
6812 }
6813 else
6814 EMSG(_("Could not open temporary log file for writing "));
6815#endif
6816
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006817 clear_sub(&subs.norm);
6818 clear_sub(&m.norm);
6819#ifdef FEAT_SYN_HL
6820 clear_sub(&subs.synt);
6821 clear_sub(&m.synt);
6822#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006823
Bram Moolenaarfda37292014-11-05 14:27:36 +01006824 result = nfa_regmatch(prog, start, &subs, &m);
6825 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006826 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006827 else if (result == NFA_TOO_EXPENSIVE)
6828 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006829
6830 cleanup_subexpr();
6831 if (REG_MULTI)
6832 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006833 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006834 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006835 reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6836 reg_startpos[i].col = subs.norm.list.multi[i].start_col;
6837
6838 reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6839 reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006840 }
6841
6842 if (reg_startpos[0].lnum < 0)
6843 {
6844 reg_startpos[0].lnum = 0;
6845 reg_startpos[0].col = col;
6846 }
6847 if (reg_endpos[0].lnum < 0)
6848 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006849 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006850 reg_endpos[0].lnum = reglnum;
6851 reg_endpos[0].col = (int)(reginput - regline);
6852 }
6853 else
6854 /* Use line number of "\ze". */
6855 reglnum = reg_endpos[0].lnum;
6856 }
6857 else
6858 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006859 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006860 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006861 reg_startp[i] = subs.norm.list.line[i].start;
6862 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006863 }
6864
6865 if (reg_startp[0] == NULL)
6866 reg_startp[0] = regline + col;
6867 if (reg_endp[0] == NULL)
6868 reg_endp[0] = reginput;
6869 }
6870
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006871#ifdef FEAT_SYN_HL
6872 /* Package any found \z(...\) matches for export. Default is none. */
6873 unref_extmatch(re_extmatch_out);
6874 re_extmatch_out = NULL;
6875
6876 if (prog->reghasz == REX_SET)
6877 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006878 cleanup_zsubexpr();
6879 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01006880 /* Loop over \z1, \z2, etc. There is no \z0. */
6881 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006882 {
6883 if (REG_MULTI)
6884 {
6885 struct multipos *mpos = &subs.synt.list.multi[i];
6886
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006887 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006888 if (mpos->start_lnum >= 0
6889 && mpos->start_lnum == mpos->end_lnum
6890 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006891 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006892 vim_strnsave(reg_getline(mpos->start_lnum)
6893 + mpos->start_col,
6894 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006895 }
6896 else
6897 {
6898 struct linepos *lpos = &subs.synt.list.line[i];
6899
6900 if (lpos->start != NULL && lpos->end != NULL)
6901 re_extmatch_out->matches[i] =
6902 vim_strnsave(lpos->start,
6903 (int)(lpos->end - lpos->start));
6904 }
6905 }
6906 }
6907#endif
6908
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006909 return 1 + reglnum;
6910}
6911
6912/*
6913 * Match a regexp against a string ("line" points to the string) or multiple
6914 * lines ("line" is NULL, use reg_getline()).
6915 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01006916 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006917 */
6918 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006919nfa_regexec_both(
6920 char_u *line,
6921 colnr_T startcol, /* column to start looking for match */
6922 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006923{
6924 nfa_regprog_T *prog;
6925 long retval = 0L;
6926 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006927 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006928
6929 if (REG_MULTI)
6930 {
6931 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6932 line = reg_getline((linenr_T)0); /* relative to the cursor */
6933 reg_startpos = reg_mmatch->startpos;
6934 reg_endpos = reg_mmatch->endpos;
6935 }
6936 else
6937 {
6938 prog = (nfa_regprog_T *)reg_match->regprog;
6939 reg_startp = reg_match->startp;
6940 reg_endp = reg_match->endp;
6941 }
6942
6943 /* Be paranoid... */
6944 if (prog == NULL || line == NULL)
6945 {
6946 EMSG(_(e_null));
6947 goto theend;
6948 }
6949
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006950 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6951 if (prog->regflags & RF_ICASE)
6952 ireg_ic = TRUE;
6953 else if (prog->regflags & RF_NOICASE)
6954 ireg_ic = FALSE;
6955
6956#ifdef FEAT_MBYTE
6957 /* If pattern contains "\Z" overrule value of ireg_icombine */
6958 if (prog->regflags & RF_ICOMBINE)
6959 ireg_icombine = TRUE;
6960#endif
6961
6962 regline = line;
6963 reglnum = 0; /* relative to line */
6964
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006965 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006966 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006967 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006968 nfa_listid = 1;
6969 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006970 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006971
Bram Moolenaard89616e2013-06-06 18:46:06 +02006972 if (prog->reganch && col > 0)
6973 return 0L;
6974
Bram Moolenaar473de612013-06-08 18:19:48 +02006975 need_clear_subexpr = TRUE;
6976#ifdef FEAT_SYN_HL
6977 /* Clear the external match subpointers if necessary. */
6978 if (prog->reghasz == REX_SET)
6979 {
6980 nfa_has_zsubexpr = TRUE;
6981 need_clear_zsubexpr = TRUE;
6982 }
6983 else
6984 nfa_has_zsubexpr = FALSE;
6985#endif
6986
Bram Moolenaard89616e2013-06-06 18:46:06 +02006987 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006988 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006989 /* Skip ahead until a character we know the match must start with.
6990 * When there is none there is no match. */
6991 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006992 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006993
Bram Moolenaar473de612013-06-08 18:19:48 +02006994 /* If match_text is set it contains the full text that must match.
6995 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006996 if (prog->match_text != NULL
6997#ifdef FEAT_MBYTE
6998 && !ireg_icombine
6999#endif
7000 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007001 return find_match_text(col, prog->regstart, prog->match_text);
7002 }
7003
Bram Moolenaard89616e2013-06-06 18:46:06 +02007004 /* If the start column is past the maximum column: no need to try. */
7005 if (ireg_maxcol > 0 && col >= ireg_maxcol)
7006 goto theend;
7007
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007008 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007009 for (i = 0; i < nstate; ++i)
7010 {
7011 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007012 prog->state[i].lastlist[0] = 0;
7013 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 }
7015
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007016 retval = nfa_regtry(prog, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007017
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007018 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007019
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007020theend:
7021 return retval;
7022}
7023
7024/*
7025 * Compile a regular expression into internal code for the NFA matcher.
7026 * Returns the program in allocated space. Returns NULL for an error.
7027 */
7028 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007029nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007030{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007031 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007032 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007033 int *postfix;
7034
7035 if (expr == NULL)
7036 return NULL;
7037
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007038 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007039 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007040
7041 init_class_tab();
7042
7043 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7044 return NULL;
7045
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007046 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007047 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007048 postfix = re2post();
7049 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007050 {
7051 /* TODO: only give this error for debugging? */
7052 if (post_ptr >= post_end)
7053 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007054 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007055 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007056
7057 /*
7058 * In order to build the NFA, we parse the input regexp twice:
7059 * 1. first pass to count size (so we can allocate space)
7060 * 2. second to emit code
7061 */
7062#ifdef ENABLE_LOG
7063 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007064 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007065
7066 if (f != NULL)
7067 {
7068 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7069 fclose(f);
7070 }
7071 }
7072#endif
7073
7074 /*
7075 * PASS 1
7076 * Count number of NFA states in "nstate". Do not build the NFA.
7077 */
7078 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007079
Bram Moolenaar16619a22013-06-11 18:42:36 +02007080 /* allocate the regprog with space for the compiled regexp */
7081 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007082 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7083 if (prog == NULL)
7084 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007085 state_ptr = prog->state;
7086
7087 /*
7088 * PASS 2
7089 * Build the NFA
7090 */
7091 prog->start = post2nfa(postfix, post_ptr, FALSE);
7092 if (prog->start == NULL)
7093 goto fail;
7094
7095 prog->regflags = regflags;
7096 prog->engine = &nfa_regengine;
7097 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007098 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007099 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007100 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007101
Bram Moolenaara2947e22013-06-11 22:44:09 +02007102 nfa_postprocess(prog);
7103
Bram Moolenaard89616e2013-06-06 18:46:06 +02007104 prog->reganch = nfa_get_reganch(prog->start, 0);
7105 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007106 prog->match_text = nfa_get_match_text(prog->start);
7107
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007108#ifdef ENABLE_LOG
7109 nfa_postfix_dump(expr, OK);
7110 nfa_dump(prog);
7111#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007112#ifdef FEAT_SYN_HL
7113 /* Remember whether this pattern has any \z specials in it. */
7114 prog->reghasz = re_has_z;
7115#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007116 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007117 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007118
7119out:
7120 vim_free(post_start);
7121 post_start = post_ptr = post_end = NULL;
7122 state_ptr = NULL;
7123 return (regprog_T *)prog;
7124
7125fail:
7126 vim_free(prog);
7127 prog = NULL;
7128#ifdef ENABLE_LOG
7129 nfa_postfix_dump(expr, FAIL);
7130#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007131 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007132 goto out;
7133}
7134
Bram Moolenaar473de612013-06-08 18:19:48 +02007135/*
7136 * Free a compiled regexp program, returned by nfa_regcomp().
7137 */
7138 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007139nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007140{
7141 if (prog != NULL)
7142 {
7143 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007144 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007145 vim_free(prog);
7146 }
7147}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007148
7149/*
7150 * Match a regexp against a string.
7151 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7152 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007153 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007154 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007155 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007156 */
7157 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007158nfa_regexec_nl(
7159 regmatch_T *rmp,
7160 char_u *line, /* string to match against */
7161 colnr_T col, /* column to start looking for match */
7162 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007163{
7164 reg_match = rmp;
7165 reg_mmatch = NULL;
7166 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007167 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168 reg_buf = curbuf;
7169 reg_win = NULL;
7170 ireg_ic = rmp->rm_ic;
7171#ifdef FEAT_MBYTE
7172 ireg_icombine = FALSE;
7173#endif
7174 ireg_maxcol = 0;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007175 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007176}
7177
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007178
7179/*
7180 * Match a regexp against multiple lines.
7181 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7182 * Uses curbuf for line count and 'iskeyword'.
7183 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007184 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007185 * match otherwise.
7186 *
7187 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7188 *
7189 * ! Also NOTE : match may actually be in another line. e.g.:
7190 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7191 *
7192 * +-------------------------+
7193 * |a |
7194 * |b |
7195 * |c |
7196 * | |
7197 * +-------------------------+
7198 *
7199 * then nfa_regexec_multi() returns 3. while the original
7200 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7201 *
7202 * FIXME if this behavior is not compatible.
7203 */
7204 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007205nfa_regexec_multi(
7206 regmmatch_T *rmp,
7207 win_T *win, /* window in which to search or NULL */
7208 buf_T *buf, /* buffer in which to search */
7209 linenr_T lnum, /* nr of line to start looking for match */
7210 colnr_T col, /* column to start looking for match */
7211 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007212{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007213 reg_match = NULL;
7214 reg_mmatch = rmp;
7215 reg_buf = buf;
7216 reg_win = win;
7217 reg_firstlnum = lnum;
7218 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7219 reg_line_lbr = FALSE;
7220 ireg_ic = rmp->rmm_ic;
7221#ifdef FEAT_MBYTE
7222 ireg_icombine = FALSE;
7223#endif
7224 ireg_maxcol = rmp->rmm_maxcol;
7225
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007226 return nfa_regexec_both(NULL, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007227}
7228
7229#ifdef DEBUG
7230# undef ENABLE_LOG
7231#endif