blob: c490acb710af4778dc61291509c943cf29431aef [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaare0ad3652015-01-27 12:59:55 +0100247/* re_flags passed to nfa_regcomp() */
248static int nfa_re_flags;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200251static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaar428e9872013-05-30 17:05:39 +0200253/* NFA regexp \1 .. \9 encountered. */
254static int nfa_has_backref;
255
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257/* NFA regexp has \z( ), set zsubexpr. */
258static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200259#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200260
Bram Moolenaar963fee22013-05-26 21:47:28 +0200261/* Number of sub expressions actually being used during execution. 1 if only
262 * the whole match (subexpr 0) is used. */
263static int nfa_nsubexpr;
264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265static int *post_start; /* holds the postfix form of r.e. */
266static int *post_end;
267static int *post_ptr;
268
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200269static int nstate; /* Number of states in the NFA. Also used when
270 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200271static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar307aa162013-06-02 16:34:21 +0200273/* If not NULL match must end at this position */
274static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200276/* listid is global, so that it increases on recursive calls to
277 * nfa_regmatch(), which means we don't have to clear the lastlist field of
278 * all the states. */
279static int nfa_listid;
280static int nfa_alt_listid;
281
282/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
283static int nfa_ll_index = 0;
284
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100285static int nfa_regcomp_start(char_u *expr, int re_flags);
286static int nfa_get_reganch(nfa_state_T *start, int depth);
287static int nfa_get_regstart(nfa_state_T *start, int depth);
288static char_u *nfa_get_match_text(nfa_state_T *start);
289static int realloc_post_list(void);
290static int nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl);
291static int nfa_emit_equi_class(int c);
292static int nfa_regatom(void);
293static int nfa_regpiece(void);
294static int nfa_regconcat(void);
295static int nfa_regbranch(void);
296static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100298static void nfa_set_code(int c);
299static void nfa_postfix_dump(char_u *expr, int retval);
300static void nfa_print_state(FILE *debugf, nfa_state_T *state);
301static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
302static void nfa_dump(nfa_regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100304static int *re2post(void);
305static nfa_state_T *alloc_state(int c, nfa_state_T *out, nfa_state_T *out1);
306static void st_error(int *postfix, int *end, int *p);
307static int nfa_max_width(nfa_state_T *startstate, int depth);
308static nfa_state_T *post2nfa(int *postfix, int *end, int nfa_calc_size);
309static void nfa_postprocess(nfa_regprog_T *prog);
310static int check_char_class(int class, int c);
311static void nfa_save_listids(nfa_regprog_T *prog, int *list);
312static void nfa_restore_listids(nfa_regprog_T *prog, int *list);
313static int nfa_re_num_cmp(long_u val, int op, long_u pos);
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 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200631 if (*(p + 2) == '7')
632 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200633 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 break;
635 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200636 return FAIL;
637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 case 'a':
639 if (*(p + 2) == 'z')
640 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200641 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200642 break;
643 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 if (*(p + 2) == 'f')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200649 return FAIL;
650
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200651 case 'A':
652 if (*(p + 2) == 'Z')
653 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200654 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200655 break;
656 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200657 if (*(p + 2) == 'F')
658 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200659 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660 break;
661 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200662 return FAIL;
663
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200664 default:
665 return FAIL;
666 }
667 p += 3;
668 }
669 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
670 {
671 newl = TRUE;
672 p += 2;
673 }
674 else if (*p == '_')
675 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677 p ++;
678 }
679 else if (*p == '\n')
680 {
681 newl = TRUE;
682 p ++;
683 }
684 else
685 return FAIL;
686 } /* while (p < end) */
687
688 if (p != end)
689 return FAIL;
690
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200691 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200692 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200693
694 switch (config)
695 {
696 case CLASS_o9:
697 return extra_newl + NFA_DIGIT;
698 case CLASS_not | CLASS_o9:
699 return extra_newl + NFA_NDIGIT;
700 case CLASS_af | CLASS_AF | CLASS_o9:
701 return extra_newl + NFA_HEX;
702 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
703 return extra_newl + NFA_NHEX;
704 case CLASS_o7:
705 return extra_newl + NFA_OCTAL;
706 case CLASS_not | CLASS_o7:
707 return extra_newl + NFA_NOCTAL;
708 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
709 return extra_newl + NFA_WORD;
710 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
711 return extra_newl + NFA_NWORD;
712 case CLASS_az | CLASS_AZ | CLASS_underscore:
713 return extra_newl + NFA_HEAD;
714 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
715 return extra_newl + NFA_NHEAD;
716 case CLASS_az | CLASS_AZ:
717 return extra_newl + NFA_ALPHA;
718 case CLASS_not | CLASS_az | CLASS_AZ:
719 return extra_newl + NFA_NALPHA;
720 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200721 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200722 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200723 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200724 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200725 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200726 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200727 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200729 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730}
731
732/*
733 * Produce the bytes for equivalence class "c".
734 * Currently only handles latin1, latin9 and utf-8.
735 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
736 * equivalent to 'a OR b OR c'
737 *
738 * NOTE! When changing this function, also update reg_equi_class()
739 */
740 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100741nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200742{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200743#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
744#ifdef FEAT_MBYTE
745# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
746#else
747# define EMITMBC(c)
748#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749
750#ifdef FEAT_MBYTE
751 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
752 || STRCMP(p_enc, "iso-8859-15") == 0)
753#endif
754 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200755#ifdef EBCDIC
756# define A_circumflex 0x62
757# define A_diaeresis 0x63
758# define A_grave 0x64
759# define A_acute 0x65
760# define A_virguilla 0x66
761# define A_ring 0x67
762# define C_cedilla 0x68
763# define E_acute 0x71
764# define E_circumflex 0x72
765# define E_diaeresis 0x73
766# define E_grave 0x74
767# define I_acute 0x75
768# define I_circumflex 0x76
769# define I_diaeresis 0x77
770# define I_grave 0x78
771# define N_virguilla 0x69
772# define O_circumflex 0xeb
773# define O_diaeresis 0xec
774# define O_grave 0xed
775# define O_acute 0xee
776# define O_virguilla 0xef
777# define O_slash 0x80
778# define U_circumflex 0xfb
779# define U_diaeresis 0xfc
780# define U_grave 0xfd
781# define U_acute 0xfe
782# define Y_acute 0xba
783# define a_grave 0x42
784# define a_acute 0x43
785# define a_circumflex 0x44
786# define a_virguilla 0x45
787# define a_diaeresis 0x46
788# define a_ring 0x47
789# define c_cedilla 0x48
790# define e_grave 0x51
791# define e_acute 0x52
792# define e_circumflex 0x53
793# define e_diaeresis 0x54
794# define i_grave 0x55
795# define i_acute 0x56
796# define i_circumflex 0x57
797# define i_diaeresis 0x58
798# define n_virguilla 0x49
799# define o_grave 0xcb
800# define o_acute 0xcc
801# define o_circumflex 0xcd
802# define o_virguilla 0xce
803# define o_diaeresis 0xcf
804# define o_slash 0x70
805# define u_grave 0xdb
806# define u_acute 0xdc
807# define u_circumflex 0xdd
808# define u_diaeresis 0xde
809# define y_acute 0x8d
810# define y_diaeresis 0xdf
811#else
812# define A_grave 0xc0
813# define A_acute 0xc1
814# define A_circumflex 0xc2
815# define A_virguilla 0xc3
816# define A_diaeresis 0xc4
817# define A_ring 0xc5
818# define C_cedilla 0xc7
819# define E_grave 0xc8
820# define E_acute 0xc9
821# define E_circumflex 0xca
822# define E_diaeresis 0xcb
823# define I_grave 0xcc
824# define I_acute 0xcd
825# define I_circumflex 0xce
826# define I_diaeresis 0xcf
827# define N_virguilla 0xd1
828# define O_grave 0xd2
829# define O_acute 0xd3
830# define O_circumflex 0xd4
831# define O_virguilla 0xd5
832# define O_diaeresis 0xd6
833# define O_slash 0xd8
834# define U_grave 0xd9
835# define U_acute 0xda
836# define U_circumflex 0xdb
837# define U_diaeresis 0xdc
838# define Y_acute 0xdd
839# define a_grave 0xe0
840# define a_acute 0xe1
841# define a_circumflex 0xe2
842# define a_virguilla 0xe3
843# define a_diaeresis 0xe4
844# define a_ring 0xe5
845# define c_cedilla 0xe7
846# define e_grave 0xe8
847# define e_acute 0xe9
848# define e_circumflex 0xea
849# define e_diaeresis 0xeb
850# define i_grave 0xec
851# define i_acute 0xed
852# define i_circumflex 0xee
853# define i_diaeresis 0xef
854# define n_virguilla 0xf1
855# define o_grave 0xf2
856# define o_acute 0xf3
857# define o_circumflex 0xf4
858# define o_virguilla 0xf5
859# define o_diaeresis 0xf6
860# define o_slash 0xf8
861# define u_grave 0xf9
862# define u_acute 0xfa
863# define u_circumflex 0xfb
864# define u_diaeresis 0xfc
865# define y_acute 0xfd
866# define y_diaeresis 0xff
867#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 switch (c)
869 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 case 'A': case A_grave: case A_acute: case A_circumflex:
871 case A_virguilla: case A_diaeresis: case A_ring:
872 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
873 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
874 CASEMBC(0x1ea2)
875 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
876 EMIT2(A_circumflex); EMIT2(A_virguilla);
877 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200878 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
879 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
880 EMITMBC(0x1ea2)
881 return OK;
882
883 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
884 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200885 return OK;
886
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200887 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
888 CASEMBC(0x10a) CASEMBC(0x10c)
889 EMIT2('C'); EMIT2(C_cedilla);
890 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200891 EMITMBC(0x10a) EMITMBC(0x10c)
892 return OK;
893
894 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200895 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200896 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
897 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200898 return OK;
899
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200900 case 'E': case E_grave: case E_acute: case E_circumflex:
901 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
902 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
903 CASEMBC(0x1eba) CASEMBC(0x1ebc)
904 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
905 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200906 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
907 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
908 EMITMBC(0x1ebc)
909 return OK;
910
911 case 'F': CASEMBC(0x1e1e)
912 EMIT2('F'); EMITMBC(0x1e1e)
913 return OK;
914
915 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200916 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
917 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200918 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
919 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
920 EMITMBC(0x1f4) EMITMBC(0x1e20)
921 return OK;
922
923 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200924 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200925 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
926 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200927 return OK;
928
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 case 'I': case I_grave: case I_acute: case I_circumflex:
930 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
931 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
932 CASEMBC(0x1cf) CASEMBC(0x1ec8)
933 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
934 EMIT2(I_circumflex); EMIT2(I_diaeresis);
935 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
937 EMITMBC(0x1cf) EMITMBC(0x1ec8)
938 return OK;
939
940 case 'J': CASEMBC(0x134)
941 EMIT2('J'); EMITMBC(0x134)
942 return OK;
943
944 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200945 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200946 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
947 EMITMBC(0x1e34)
948 return OK;
949
950 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200951 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200952 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
953 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
954 return OK;
955
956 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
957 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200958 return OK;
959
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200960 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
961 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
962 EMIT2('N'); EMIT2(N_virguilla);
963 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200964 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 return OK;
966
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200967 case 'O': case O_grave: case O_acute: case O_circumflex:
968 case O_virguilla: case O_diaeresis: case O_slash:
969 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
970 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
971 CASEMBC(0x1ec) CASEMBC(0x1ece)
972 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
973 EMIT2(O_circumflex); EMIT2(O_virguilla);
974 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200975 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
976 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
977 EMITMBC(0x1ec) EMITMBC(0x1ece)
978 return OK;
979
980 case 'P': case 0x1e54: case 0x1e56:
981 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
982 return OK;
983
984 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200985 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200986 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
987 EMITMBC(0x1e58) EMITMBC(0x1e5e)
988 return OK;
989
990 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200991 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200992 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
993 EMITMBC(0x160) EMITMBC(0x1e60)
994 return OK;
995
996 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200997 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200998 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
999 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return OK;
1001
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001002 case 'U': case U_grave: case U_acute: case U_diaeresis:
1003 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
1004 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
1005 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
1006 CASEMBC(0x1ee6)
1007 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
1008 EMIT2(U_diaeresis); EMIT2(U_circumflex);
1009 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
1011 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
1012 EMITMBC(0x1ee6)
1013 return OK;
1014
1015 case 'V': CASEMBC(0x1e7c)
1016 EMIT2('V'); EMITMBC(0x1e7c)
1017 return OK;
1018
1019 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001020 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001021 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
1022 EMITMBC(0x1e84) EMITMBC(0x1e86)
1023 return OK;
1024
1025 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
1026 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001027 return OK;
1028
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001029 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
1030 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
1031 CASEMBC(0x1ef8)
1032 EMIT2('Y'); EMIT2(Y_acute);
1033 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001034 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
1035 EMITMBC(0x1ef8)
1036 return OK;
1037
1038 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001039 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001040 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
1041 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 case 'a': case a_grave: case a_acute: case a_circumflex:
1045 case a_virguilla: case a_diaeresis: case a_ring:
1046 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
1047 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1048 CASEMBC(0x1ea3)
1049 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1050 EMIT2(a_circumflex); EMIT2(a_virguilla);
1051 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001052 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1053 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1054 EMITMBC(0x1ea3)
1055 return OK;
1056
1057 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1058 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001059 return OK;
1060
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001061 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1062 CASEMBC(0x10b) CASEMBC(0x10d)
1063 EMIT2('c'); EMIT2(c_cedilla);
1064 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001065 EMITMBC(0x10b) EMITMBC(0x10d)
1066 return OK;
1067
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001068 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001069 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001070 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1071 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001072 return OK;
1073
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001074 case 'e': case e_grave: case e_acute: case e_circumflex:
1075 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1076 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1077 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1078 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1079 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1080 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001081 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1082 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1083 return OK;
1084
1085 case 'f': CASEMBC(0x1e1f)
1086 EMIT2('f'); EMITMBC(0x1e1f)
1087 return OK;
1088
1089 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001090 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1091 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1093 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1094 EMITMBC(0x1f5) EMITMBC(0x1e21)
1095 return OK;
1096
1097 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001098 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001099 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1100 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001101 return OK;
1102
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001103 case 'i': case i_grave: case i_acute: case i_circumflex:
1104 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1105 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1106 CASEMBC(0x1ec9)
1107 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1108 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1109 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001110 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1111 EMITMBC(0x1ec9)
1112 return OK;
1113
1114 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1115 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1116 return OK;
1117
1118 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001120 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1121 EMITMBC(0x1e35)
1122 return OK;
1123
1124 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001125 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001126 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1127 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1128 return OK;
1129
1130 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1131 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 return OK;
1133
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001134 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1135 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1136 CASEMBC(0x1e49)
1137 EMIT2('n'); EMIT2(n_virguilla);
1138 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001139 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1140 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 return OK;
1142
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001143 case 'o': case o_grave: case o_acute: case o_circumflex:
1144 case o_virguilla: case o_diaeresis: case o_slash:
1145 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1146 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1147 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1148 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1149 EMIT2(o_circumflex); EMIT2(o_virguilla);
1150 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001151 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1152 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1153 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1154 return OK;
1155
1156 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1157 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1158 return OK;
1159
1160 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001161 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001162 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1163 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1164 return OK;
1165
1166 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001167 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1169 EMITMBC(0x161) EMITMBC(0x1e61)
1170 return OK;
1171
1172 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001173 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001174 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1175 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001176 return OK;
1177
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001178 case 'u': case u_grave: case u_acute: case u_circumflex:
1179 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1180 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1181 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1182 CASEMBC(0x1ee7)
1183 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1184 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1185 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001186 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1187 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1188 EMITMBC(0x1ee7)
1189 return OK;
1190
1191 case 'v': CASEMBC(0x1e7d)
1192 EMIT2('v'); EMITMBC(0x1e7d)
1193 return OK;
1194
1195 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001196 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001197 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1198 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1199 return OK;
1200
1201 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1202 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 return OK;
1204
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001205 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1206 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1207 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1208 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1209 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001210 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1211 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 return OK;
1213
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001214 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001215 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001216 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1217 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1218 return OK;
1219
1220 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 }
1222 }
1223
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001224 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 return OK;
1226#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001227#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228}
1229
1230/*
1231 * Code to parse regular expression.
1232 *
1233 * We try to reuse parsing functions in regexp.c to
1234 * minimize surprise and keep the syntax consistent.
1235 */
1236
1237/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 * Parse the lowest level.
1239 *
1240 * An atom can be one of a long list of items. Many atoms match one character
1241 * in the text. It is often an ordinary character or a character class.
1242 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1243 * is only for syntax highlighting.
1244 *
1245 * atom ::= ordinary-atom
1246 * or \( pattern \)
1247 * or \%( pattern \)
1248 * or \z( pattern \)
1249 */
1250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001251nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001252{
1253 int c;
1254 int charclass;
1255 int equiclass;
1256 int collclass;
1257 int got_coll_char;
1258 char_u *p;
1259 char_u *endp;
1260#ifdef FEAT_MBYTE
1261 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262#endif
1263 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 int emit_range;
1265 int negated;
1266 int result;
1267 int startc = -1;
1268 int endc = -1;
1269 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001270 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001272 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 switch (c)
1274 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001275 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001276 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001277
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001278 case Magic('^'):
1279 EMIT(NFA_BOL);
1280 break;
1281
1282 case Magic('$'):
1283 EMIT(NFA_EOL);
1284#if defined(FEAT_SYN_HL) || defined(PROTO)
1285 had_eol = TRUE;
1286#endif
1287 break;
1288
1289 case Magic('<'):
1290 EMIT(NFA_BOW);
1291 break;
1292
1293 case Magic('>'):
1294 EMIT(NFA_EOW);
1295 break;
1296
1297 case Magic('_'):
1298 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001299 if (c == NUL)
1300 EMSG_RET_FAIL(_(e_nul_found));
1301
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001302 if (c == '^') /* "\_^" is start-of-line */
1303 {
1304 EMIT(NFA_BOL);
1305 break;
1306 }
1307 if (c == '$') /* "\_$" is end-of-line */
1308 {
1309 EMIT(NFA_EOL);
1310#if defined(FEAT_SYN_HL) || defined(PROTO)
1311 had_eol = TRUE;
1312#endif
1313 break;
1314 }
1315
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001316 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317
1318 /* "\_[" is collection plus newline */
1319 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001320 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321
1322 /* "\_x" is character class plus newline */
1323 /*FALLTHROUGH*/
1324
1325 /*
1326 * Character classes.
1327 */
1328 case Magic('.'):
1329 case Magic('i'):
1330 case Magic('I'):
1331 case Magic('k'):
1332 case Magic('K'):
1333 case Magic('f'):
1334 case Magic('F'):
1335 case Magic('p'):
1336 case Magic('P'):
1337 case Magic('s'):
1338 case Magic('S'):
1339 case Magic('d'):
1340 case Magic('D'):
1341 case Magic('x'):
1342 case Magic('X'):
1343 case Magic('o'):
1344 case Magic('O'):
1345 case Magic('w'):
1346 case Magic('W'):
1347 case Magic('h'):
1348 case Magic('H'):
1349 case Magic('a'):
1350 case Magic('A'):
1351 case Magic('l'):
1352 case Magic('L'):
1353 case Magic('u'):
1354 case Magic('U'):
1355 p = vim_strchr(classchars, no_Magic(c));
1356 if (p == NULL)
1357 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001358 if (extra == NFA_ADD_NL)
1359 {
1360 EMSGN(_(e_ill_char_class), c);
1361 rc_did_emsg = TRUE;
1362 return FAIL;
1363 }
Bram Moolenaarde330112017-01-08 20:50:52 +01001364 IEMSGN("INTERNAL: Unknown character class char: %ld", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001365 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 }
1367#ifdef FEAT_MBYTE
1368 /* When '.' is followed by a composing char ignore the dot, so that
1369 * the composing char is matched here. */
1370 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1371 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001372 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001373 c = getchr();
1374 goto nfa_do_multibyte;
1375 }
1376#endif
1377 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001378 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001379 {
1380 EMIT(NFA_NEWL);
1381 EMIT(NFA_OR);
1382 regflags |= RF_HASNL;
1383 }
1384 break;
1385
1386 case Magic('n'):
1387 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001388 /* In a string "\n" matches a newline character. */
1389 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 else
1391 {
1392 /* In buffer text "\n" matches the end of a line. */
1393 EMIT(NFA_NEWL);
1394 regflags |= RF_HASNL;
1395 }
1396 break;
1397
1398 case Magic('('):
1399 if (nfa_reg(REG_PAREN) == FAIL)
1400 return FAIL; /* cascaded error */
1401 break;
1402
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 case Magic('|'):
1404 case Magic('&'):
1405 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001406 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 return FAIL;
1408
1409 case Magic('='):
1410 case Magic('?'):
1411 case Magic('+'):
1412 case Magic('@'):
1413 case Magic('*'):
1414 case Magic('{'):
1415 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001416 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 return FAIL;
1418
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001419 case Magic('~'):
1420 {
1421 char_u *lp;
1422
1423 /* Previous substitute pattern.
1424 * Generated as "\%(pattern\)". */
1425 if (reg_prev_sub == NULL)
1426 {
1427 EMSG(_(e_nopresub));
1428 return FAIL;
1429 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001430 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001431 {
1432 EMIT(PTR2CHAR(lp));
1433 if (lp != reg_prev_sub)
1434 EMIT(NFA_CONCAT);
1435 }
1436 EMIT(NFA_NOPEN);
1437 break;
1438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439
Bram Moolenaar428e9872013-05-30 17:05:39 +02001440 case Magic('1'):
1441 case Magic('2'):
1442 case Magic('3'):
1443 case Magic('4'):
1444 case Magic('5'):
1445 case Magic('6'):
1446 case Magic('7'):
1447 case Magic('8'):
1448 case Magic('9'):
1449 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1450 nfa_has_backref = TRUE;
1451 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452
1453 case Magic('z'):
1454 c = no_Magic(getchr());
1455 switch (c)
1456 {
1457 case 's':
1458 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001459 if (re_mult_next("\\zs") == FAIL)
1460 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461 break;
1462 case 'e':
1463 EMIT(NFA_ZEND);
1464 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001465 if (re_mult_next("\\ze") == FAIL)
1466 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001467 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001468#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001469 case '1':
1470 case '2':
1471 case '3':
1472 case '4':
1473 case '5':
1474 case '6':
1475 case '7':
1476 case '8':
1477 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001478 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001479 if (reg_do_extmatch != REX_USE)
1480 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001481 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1482 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001483 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001484 re_has_z = REX_USE;
1485 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001487 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001488 if (reg_do_extmatch != REX_SET)
1489 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001490 if (nfa_reg(REG_ZPAREN) == FAIL)
1491 return FAIL; /* cascaded error */
1492 re_has_z = REX_SET;
1493 break;
1494#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001496 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 no_Magic(c));
1498 return FAIL;
1499 }
1500 break;
1501
1502 case Magic('%'):
1503 c = no_Magic(getchr());
1504 switch (c)
1505 {
1506 /* () without a back reference */
1507 case '(':
1508 if (nfa_reg(REG_NPAREN) == FAIL)
1509 return FAIL;
1510 EMIT(NFA_NOPEN);
1511 break;
1512
1513 case 'd': /* %d123 decimal */
1514 case 'o': /* %o123 octal */
1515 case 'x': /* %xab hex 2 */
1516 case 'u': /* %uabcd hex 4 */
1517 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001518 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001519 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520
Bram Moolenaar47196582013-05-25 22:04:23 +02001521 switch (c)
1522 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001523 case 'd': nr = getdecchrs(); break;
1524 case 'o': nr = getoctchrs(); break;
1525 case 'x': nr = gethexchrs(2); break;
1526 case 'u': nr = gethexchrs(4); break;
1527 case 'U': nr = gethexchrs(8); break;
1528 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001529 }
1530
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001531 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001532 EMSG2_RET_FAIL(
1533 _("E678: Invalid character after %s%%[dxouU]"),
1534 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001535 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001536 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001537 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001538 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 break;
1540
1541 /* Catch \%^ and \%$ regardless of where they appear in the
1542 * pattern -- regardless of whether or not it makes sense. */
1543 case '^':
1544 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 break;
1546
1547 case '$':
1548 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549 break;
1550
1551 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001552 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 break;
1554
1555 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001556 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001557 break;
1558
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001559 case 'C':
1560 EMIT(NFA_ANY_COMPOSING);
1561 break;
1562
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001564 {
1565 int n;
1566
1567 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001568 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001569 {
1570 if (c == NUL)
1571 EMSG2_RET_FAIL(_(e_missing_sb),
1572 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001573 /* recursive call! */
1574 if (nfa_regatom() == FAIL)
1575 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001576 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001577 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001578 if (n == 0)
1579 EMSG2_RET_FAIL(_(e_empty_sb),
1580 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001581 EMIT(NFA_OPT_CHARS);
1582 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001583
1584 /* Emit as "\%(\%[abc]\)" to be able to handle
1585 * "\%[abc]*" which would cause the empty string to be
1586 * matched an unlimited number of times. NFA_NOPEN is
1587 * added only once at a position, while NFA_SPLIT is
1588 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001589 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001590 * a lot. */
1591 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001592 break;
1593 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001594
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001595 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001596 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001597 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001598 int cmp = c;
1599
1600 if (c == '<' || c == '>')
1601 c = getchr();
1602 while (VIM_ISDIGIT(c))
1603 {
1604 n = n * 10 + (c - '0');
1605 c = getchr();
1606 }
1607 if (c == 'l' || c == 'c' || c == 'v')
1608 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001609 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001610 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001611 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001612 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001613 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001614 if (save_prev_at_start)
1615 at_start = TRUE;
1616 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001617 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001618 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001619 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001620 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001621 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001622 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001623 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001624 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001625 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001626 break;
1627 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001628 else if (c == '\'' && n == 0)
1629 {
1630 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001631 EMIT(cmp == '<' ? NFA_MARK_LT :
1632 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001633 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001634 break;
1635 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001636 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001637 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1638 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 return FAIL;
1640 }
1641 break;
1642
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001643 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001644collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001646 * [abc] uses NFA_START_COLL - NFA_END_COLL
1647 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1648 * Each character is produced as a regular state, using
1649 * NFA_CONCAT to bind them together.
1650 * Besides normal characters there can be:
1651 * - character classes NFA_CLASS_*
1652 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 */
1654
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001655 p = regparse;
1656 endp = skip_anyof(p);
1657 if (*endp == ']')
1658 {
1659 /*
1660 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001661 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001662 * and perform the necessary substitutions in the NFA.
1663 */
1664 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001665 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 if (result != FAIL)
1667 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001668 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001670 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001671 EMIT(NFA_NEWL);
1672 EMIT(NFA_OR);
1673 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001674 else
1675 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001677 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 return OK;
1679 }
1680 /*
1681 * Failed to recognize a character class. Use the simple
1682 * version that turns [abc] into 'a' OR 'b' OR 'c'
1683 */
1684 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001685 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001686 if (*regparse == '^') /* negated range */
1687 {
1688 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001689 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001690 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001692 else
1693 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694 if (*regparse == '-')
1695 {
1696 startc = '-';
1697 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001698 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001699 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001700 }
1701 /* Emit the OR branches for each character in the [] */
1702 emit_range = FALSE;
1703 while (regparse < endp)
1704 {
1705 oldstartc = startc;
1706 startc = -1;
1707 got_coll_char = FALSE;
1708 if (*regparse == '[')
1709 {
1710 /* Check for [: :], [= =], [. .] */
1711 equiclass = collclass = 0;
1712 charclass = get_char_class(&regparse);
1713 if (charclass == CLASS_NONE)
1714 {
1715 equiclass = get_equi_class(&regparse);
1716 if (equiclass == 0)
1717 collclass = get_coll_element(&regparse);
1718 }
1719
1720 /* Character class like [:alpha:] */
1721 if (charclass != CLASS_NONE)
1722 {
1723 switch (charclass)
1724 {
1725 case CLASS_ALNUM:
1726 EMIT(NFA_CLASS_ALNUM);
1727 break;
1728 case CLASS_ALPHA:
1729 EMIT(NFA_CLASS_ALPHA);
1730 break;
1731 case CLASS_BLANK:
1732 EMIT(NFA_CLASS_BLANK);
1733 break;
1734 case CLASS_CNTRL:
1735 EMIT(NFA_CLASS_CNTRL);
1736 break;
1737 case CLASS_DIGIT:
1738 EMIT(NFA_CLASS_DIGIT);
1739 break;
1740 case CLASS_GRAPH:
1741 EMIT(NFA_CLASS_GRAPH);
1742 break;
1743 case CLASS_LOWER:
1744 EMIT(NFA_CLASS_LOWER);
1745 break;
1746 case CLASS_PRINT:
1747 EMIT(NFA_CLASS_PRINT);
1748 break;
1749 case CLASS_PUNCT:
1750 EMIT(NFA_CLASS_PUNCT);
1751 break;
1752 case CLASS_SPACE:
1753 EMIT(NFA_CLASS_SPACE);
1754 break;
1755 case CLASS_UPPER:
1756 EMIT(NFA_CLASS_UPPER);
1757 break;
1758 case CLASS_XDIGIT:
1759 EMIT(NFA_CLASS_XDIGIT);
1760 break;
1761 case CLASS_TAB:
1762 EMIT(NFA_CLASS_TAB);
1763 break;
1764 case CLASS_RETURN:
1765 EMIT(NFA_CLASS_RETURN);
1766 break;
1767 case CLASS_BACKSPACE:
1768 EMIT(NFA_CLASS_BACKSPACE);
1769 break;
1770 case CLASS_ESCAPE:
1771 EMIT(NFA_CLASS_ESCAPE);
1772 break;
1773 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001774 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001775 continue;
1776 }
1777 /* Try equivalence class [=a=] and the like */
1778 if (equiclass != 0)
1779 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001780 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001781 if (result == FAIL)
1782 {
1783 /* should never happen */
1784 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1785 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786 continue;
1787 }
1788 /* Try collating class like [. .] */
1789 if (collclass != 0)
1790 {
1791 startc = collclass; /* allow [.a.]-x as a range */
1792 /* Will emit the proper atom at the end of the
1793 * while loop. */
1794 }
1795 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001796 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1797 * start character. */
1798 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 {
1800 emit_range = TRUE;
1801 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001802 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001803 continue; /* reading the end of the range */
1804 }
1805
1806 /* Now handle simple and escaped characters.
1807 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1808 * accepts "\t", "\e", etc., but only when the 'l' flag in
1809 * 'cpoptions' is not included.
1810 * Posix doesn't recognize backslash at all.
1811 */
1812 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001813 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001814 && regparse + 1 <= endp
1815 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001816 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001817 && vim_strchr(REGEXP_ABBR, regparse[1])
1818 != NULL)
1819 )
1820 )
1821 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001822 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001824 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001825 startc = reg_string ? NL : NFA_NEWL;
1826 else
1827 if (*regparse == 'd'
1828 || *regparse == 'o'
1829 || *regparse == 'x'
1830 || *regparse == 'u'
1831 || *regparse == 'U'
1832 )
1833 {
1834 /* TODO(RE) This needs more testing */
1835 startc = coll_get_char();
1836 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001837 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 }
1839 else
1840 {
1841 /* \r,\t,\e,\b */
1842 startc = backslash_trans(*regparse);
1843 }
1844 }
1845
1846 /* Normal printable char */
1847 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001848 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001849
1850 /* Previous char was '-', so this char is end of range. */
1851 if (emit_range)
1852 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001853 endc = startc;
1854 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 if (startc > endc)
1856 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001857
1858 if (endc > startc + 2)
1859 {
1860 /* Emit a range instead of the sequence of
1861 * individual characters. */
1862 if (startc == 0)
1863 /* \x00 is translated to \x0a, start at \x01. */
1864 EMIT(1);
1865 else
1866 --post_ptr; /* remove NFA_CONCAT */
1867 EMIT(endc);
1868 EMIT(NFA_RANGE);
1869 EMIT(NFA_CONCAT);
1870 }
1871 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001873 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 || (*mb_char2len)(endc) > 1))
1875 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001876 /* Emit the characters in the range.
1877 * "startc" was already emitted, so skip it.
1878 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001879 for (c = startc + 1; c <= endc; c++)
1880 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001881 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001882 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001883 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884 }
1885 else
1886#endif
1887 {
1888#ifdef EBCDIC
1889 int alpha_only = FALSE;
1890
1891 /* for alphabetical range skip the gaps
1892 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1893 if (isalpha(startc) && isalpha(endc))
1894 alpha_only = TRUE;
1895#endif
1896 /* Emit the range. "startc" was already emitted, so
1897 * skip it. */
1898 for (c = startc + 1; c <= endc; c++)
1899#ifdef EBCDIC
1900 if (!alpha_only || isalpha(startc))
1901#endif
1902 {
1903 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001904 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001907 emit_range = FALSE;
1908 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 }
1910 else
1911 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001912 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001913 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914 * Normally, simply emit startc. But if we get char
1915 * code=0 from a collating char, then replace it with
1916 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001917 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001918 * the backtracking engine. */
1919 if (startc == NFA_NEWL)
1920 {
1921 /* Line break can't be matched as part of the
1922 * collection, add an OR below. But not for negated
1923 * range. */
1924 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001925 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001926 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001928 {
1929 if (got_coll_char == TRUE && startc == 0)
1930 EMIT(0x0a);
1931 else
1932 EMIT(startc);
1933 EMIT(NFA_CONCAT);
1934 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 }
1936
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001937 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938 } /* while (p < endp) */
1939
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001940 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 if (*regparse == '-') /* if last, '-' is just a char */
1942 {
1943 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001944 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001946
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001947 /* skip the trailing ] */
1948 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001949 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001950
1951 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001953 EMIT(NFA_END_NEG_COLL);
1954 else
1955 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001956
1957 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001958 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001959 {
1960 EMIT(reg_string ? NL : NFA_NEWL);
1961 EMIT(NFA_OR);
1962 }
1963
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001965 } /* if exists closing ] */
1966
1967 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001969 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001971 default:
1972 {
1973#ifdef FEAT_MBYTE
1974 int plen;
1975
1976nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001977 /* plen is length of current char with composing chars */
1978 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001979 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001980 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001981 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001982 int i = 0;
1983
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001984 /* A base character plus composing characters, or just one
1985 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001986 * This requires creating a separate atom as if enclosing
1987 * the characters in (), where NFA_COMPOSING is the ( and
1988 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989 * building the postfix form, not the NFA itself;
1990 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001991 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001992 for (;;)
1993 {
1994 EMIT(c);
1995 if (i > 0)
1996 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001997 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001998 break;
1999 c = utf_ptr2char(old_regparse + i);
2000 }
2001 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002 regparse = old_regparse + plen;
2003 }
2004 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002005#endif
2006 {
2007 c = no_Magic(c);
2008 EMIT(c);
2009 }
2010 return OK;
2011 }
2012 }
2013
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014 return OK;
2015}
2016
2017/*
2018 * Parse something followed by possible [*+=].
2019 *
2020 * A piece is an atom, possibly followed by a multi, an indication of how many
2021 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2022 * characters: "", "a", "aa", etc.
2023 *
2024 * piece ::= atom
2025 * or atom multi
2026 */
2027 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002028nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029{
2030 int i;
2031 int op;
2032 int ret;
2033 long minval, maxval;
2034 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002035 parse_state_T old_state;
2036 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002038 int old_post_pos;
2039 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040 int quest;
2041
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002042 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2043 * next. */
2044 save_parse_state(&old_state);
2045
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002047 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002048
2049 ret = nfa_regatom();
2050 if (ret == FAIL)
2051 return FAIL; /* cascaded error */
2052
2053 op = peekchr();
2054 if (re_multi_type(op) == NOT_MULTI)
2055 return OK;
2056
2057 skipchr();
2058 switch (op)
2059 {
2060 case Magic('*'):
2061 EMIT(NFA_STAR);
2062 break;
2063
2064 case Magic('+'):
2065 /*
2066 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2067 * first and only submatch would be "aaa". But the backtracking
2068 * engine interprets the plus as "try matching one more time", and
2069 * a* matches a second time at the end of the input, the empty
2070 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002071 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002073 * In order to be consistent with the old engine, we replace
2074 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002075 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002076 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 curchr = -1;
2078 if (nfa_regatom() == FAIL)
2079 return FAIL;
2080 EMIT(NFA_STAR);
2081 EMIT(NFA_CONCAT);
2082 skipchr(); /* skip the \+ */
2083 break;
2084
2085 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002086 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002088 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002089 switch(op)
2090 {
2091 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002092 /* \@= */
2093 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002094 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002095 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002096 /* \@! */
2097 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002098 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002099 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002100 op = no_Magic(getchr());
2101 if (op == '=')
2102 /* \@<= */
2103 i = NFA_PREV_ATOM_JUST_BEFORE;
2104 else if (op == '!')
2105 /* \@<! */
2106 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2107 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002108 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002109 /* \@> */
2110 i = NFA_PREV_ATOM_LIKE_PATTERN;
2111 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002113 if (i == 0)
2114 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002115 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2116 return FAIL;
2117 }
2118 EMIT(i);
2119 if (i == NFA_PREV_ATOM_JUST_BEFORE
2120 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2121 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122 break;
2123
2124 case Magic('?'):
2125 case Magic('='):
2126 EMIT(NFA_QUEST);
2127 break;
2128
2129 case Magic('{'):
2130 /* a{2,5} will expand to 'aaa?a?a?'
2131 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2132 * version of '?'
2133 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2134 * parenthesis have the same id
2135 */
2136
2137 greedy = TRUE;
2138 c2 = peekchr();
2139 if (c2 == '-' || c2 == Magic('-'))
2140 {
2141 skipchr();
2142 greedy = FALSE;
2143 }
2144 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002145 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002146
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2148 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002149 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002151 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002152 /* \{}, \{0,} */
2153 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002154 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002155 /* \{-}, \{-0,} */
2156 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002157 break;
2158 }
2159
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002160 /* Special case: x{0} or x{-0} */
2161 if (maxval == 0)
2162 {
2163 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002164 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002165 /* NFA_EMPTY is 0-length and works everywhere */
2166 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002167 return OK;
2168 }
2169
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002170 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002171 * maximum is much larger than the minimum and when the maximum is
2172 * large. Bail out if we can use the other engine. */
2173 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002174 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002175 return FAIL;
2176
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002177 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002178 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002179 /* Save parse state after the repeated atom and the \{} */
2180 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2183 for (i = 0; i < maxval; i++)
2184 {
2185 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002186 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002187 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002188 if (nfa_regatom() == FAIL)
2189 return FAIL;
2190 /* after "minval" times, atoms are optional */
2191 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002192 {
2193 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002194 {
2195 if (greedy)
2196 EMIT(NFA_STAR);
2197 else
2198 EMIT(NFA_STAR_NONGREEDY);
2199 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002200 else
2201 EMIT(quest);
2202 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002203 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002204 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002205 if (i + 1 > minval && maxval == MAX_LIMIT)
2206 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207 }
2208
2209 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002210 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002211 curchr = -1;
2212
2213 break;
2214
2215
2216 default:
2217 break;
2218 } /* end switch */
2219
2220 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002222 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002223
2224 return OK;
2225}
2226
2227/*
2228 * Parse one or more pieces, concatenated. It matches a match for the
2229 * first piece, followed by a match for the second piece, etc. Example:
2230 * "f[0-9]b", first matches "f", then a digit and then "b".
2231 *
2232 * concat ::= piece
2233 * or piece piece
2234 * or piece piece piece
2235 * etc.
2236 */
2237 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002238nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002239{
2240 int cont = TRUE;
2241 int first = TRUE;
2242
2243 while (cont)
2244 {
2245 switch (peekchr())
2246 {
2247 case NUL:
2248 case Magic('|'):
2249 case Magic('&'):
2250 case Magic(')'):
2251 cont = FALSE;
2252 break;
2253
2254 case Magic('Z'):
2255#ifdef FEAT_MBYTE
2256 regflags |= RF_ICOMBINE;
2257#endif
2258 skipchr_keepstart();
2259 break;
2260 case Magic('c'):
2261 regflags |= RF_ICASE;
2262 skipchr_keepstart();
2263 break;
2264 case Magic('C'):
2265 regflags |= RF_NOICASE;
2266 skipchr_keepstart();
2267 break;
2268 case Magic('v'):
2269 reg_magic = MAGIC_ALL;
2270 skipchr_keepstart();
2271 curchr = -1;
2272 break;
2273 case Magic('m'):
2274 reg_magic = MAGIC_ON;
2275 skipchr_keepstart();
2276 curchr = -1;
2277 break;
2278 case Magic('M'):
2279 reg_magic = MAGIC_OFF;
2280 skipchr_keepstart();
2281 curchr = -1;
2282 break;
2283 case Magic('V'):
2284 reg_magic = MAGIC_NONE;
2285 skipchr_keepstart();
2286 curchr = -1;
2287 break;
2288
2289 default:
2290 if (nfa_regpiece() == FAIL)
2291 return FAIL;
2292 if (first == FALSE)
2293 EMIT(NFA_CONCAT);
2294 else
2295 first = FALSE;
2296 break;
2297 }
2298 }
2299
2300 return OK;
2301}
2302
2303/*
2304 * Parse a branch, one or more concats, separated by "\&". It matches the
2305 * last concat, but only if all the preceding concats also match at the same
2306 * position. Examples:
2307 * "foobeep\&..." matches "foo" in "foobeep".
2308 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2309 *
2310 * branch ::= concat
2311 * or concat \& concat
2312 * or concat \& concat \& concat
2313 * etc.
2314 */
2315 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002316nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317{
2318 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002319 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002320
Bram Moolenaar16299b52013-05-30 18:45:23 +02002321 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002322
2323 /* First branch, possibly the only one */
2324 if (nfa_regconcat() == FAIL)
2325 return FAIL;
2326
2327 ch = peekchr();
2328 /* Try next concats */
2329 while (ch == Magic('&'))
2330 {
2331 skipchr();
2332 EMIT(NFA_NOPEN);
2333 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002334 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002335 if (nfa_regconcat() == FAIL)
2336 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002337 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002338 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002339 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340 EMIT(NFA_CONCAT);
2341 ch = peekchr();
2342 }
2343
Bram Moolenaar699c1202013-09-25 16:41:54 +02002344 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002345 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002346 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002347
2348 return OK;
2349}
2350
2351/*
2352 * Parse a pattern, one or more branches, separated by "\|". It matches
2353 * anything that matches one of the branches. Example: "foo\|beep" matches
2354 * "foo" and matches "beep". If more than one branch matches, the first one
2355 * is used.
2356 *
2357 * pattern ::= branch
2358 * or branch \| branch
2359 * or branch \| branch \| branch
2360 * etc.
2361 */
2362 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002363nfa_reg(
2364 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365{
2366 int parno = 0;
2367
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002368 if (paren == REG_PAREN)
2369 {
2370 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 parno = regnpar++;
2373 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002374#ifdef FEAT_SYN_HL
2375 else if (paren == REG_ZPAREN)
2376 {
2377 /* Make a ZOPEN node. */
2378 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002379 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002380 parno = regnzpar++;
2381 }
2382#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383
2384 if (nfa_regbranch() == FAIL)
2385 return FAIL; /* cascaded error */
2386
2387 while (peekchr() == Magic('|'))
2388 {
2389 skipchr();
2390 if (nfa_regbranch() == FAIL)
2391 return FAIL; /* cascaded error */
2392 EMIT(NFA_OR);
2393 }
2394
2395 /* Check for proper termination. */
2396 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2397 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398 if (paren == REG_NPAREN)
2399 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2400 else
2401 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2402 }
2403 else if (paren == REG_NOPAREN && peekchr() != NUL)
2404 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002405 if (peekchr() == Magic(')'))
2406 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2407 else
2408 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2409 }
2410 /*
2411 * Here we set the flag allowing back references to this set of
2412 * parentheses.
2413 */
2414 if (paren == REG_PAREN)
2415 {
2416 had_endbrace[parno] = TRUE; /* have seen the close paren */
2417 EMIT(NFA_MOPEN + parno);
2418 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002419#ifdef FEAT_SYN_HL
2420 else if (paren == REG_ZPAREN)
2421 EMIT(NFA_ZOPEN + parno);
2422#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002423
2424 return OK;
2425}
2426
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002427#ifdef DEBUG
2428static char_u code[50];
2429
2430 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002431nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432{
2433 int addnl = FALSE;
2434
2435 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2436 {
2437 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002438 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 }
2440
2441 STRCPY(code, "");
2442 switch (c)
2443 {
2444 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2445 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2446 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2447 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2448 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2449 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2450
Bram Moolenaar5714b802013-05-28 22:03:20 +02002451 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2452 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2453 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2454 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2455 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2456 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2457 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2458 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2459 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002460#ifdef FEAT_SYN_HL
2461 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2462 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2463 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2464 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2465 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2466 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2467 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2468 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2469 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2470#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002471 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2472
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 case NFA_PREV_ATOM_NO_WIDTH:
2474 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002475 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2476 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002477 case NFA_PREV_ATOM_JUST_BEFORE:
2478 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2479 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2480 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002481 case NFA_PREV_ATOM_LIKE_PATTERN:
2482 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2483
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002484 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2485 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002487 case NFA_START_INVISIBLE_FIRST:
2488 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002489 case NFA_START_INVISIBLE_NEG:
2490 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002491 case NFA_START_INVISIBLE_NEG_FIRST:
2492 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002493 case NFA_START_INVISIBLE_BEFORE:
2494 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002495 case NFA_START_INVISIBLE_BEFORE_FIRST:
2496 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002497 case NFA_START_INVISIBLE_BEFORE_NEG:
2498 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002499 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2500 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002501 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002502 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002503 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002504 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2507 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002508 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002509
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002510 case NFA_MOPEN:
2511 case NFA_MOPEN1:
2512 case NFA_MOPEN2:
2513 case NFA_MOPEN3:
2514 case NFA_MOPEN4:
2515 case NFA_MOPEN5:
2516 case NFA_MOPEN6:
2517 case NFA_MOPEN7:
2518 case NFA_MOPEN8:
2519 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520 STRCPY(code, "NFA_MOPEN(x)");
2521 code[10] = c - NFA_MOPEN + '0';
2522 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002523 case NFA_MCLOSE:
2524 case NFA_MCLOSE1:
2525 case NFA_MCLOSE2:
2526 case NFA_MCLOSE3:
2527 case NFA_MCLOSE4:
2528 case NFA_MCLOSE5:
2529 case NFA_MCLOSE6:
2530 case NFA_MCLOSE7:
2531 case NFA_MCLOSE8:
2532 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002533 STRCPY(code, "NFA_MCLOSE(x)");
2534 code[11] = c - NFA_MCLOSE + '0';
2535 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002536#ifdef FEAT_SYN_HL
2537 case NFA_ZOPEN:
2538 case NFA_ZOPEN1:
2539 case NFA_ZOPEN2:
2540 case NFA_ZOPEN3:
2541 case NFA_ZOPEN4:
2542 case NFA_ZOPEN5:
2543 case NFA_ZOPEN6:
2544 case NFA_ZOPEN7:
2545 case NFA_ZOPEN8:
2546 case NFA_ZOPEN9:
2547 STRCPY(code, "NFA_ZOPEN(x)");
2548 code[10] = c - NFA_ZOPEN + '0';
2549 break;
2550 case NFA_ZCLOSE:
2551 case NFA_ZCLOSE1:
2552 case NFA_ZCLOSE2:
2553 case NFA_ZCLOSE3:
2554 case NFA_ZCLOSE4:
2555 case NFA_ZCLOSE5:
2556 case NFA_ZCLOSE6:
2557 case NFA_ZCLOSE7:
2558 case NFA_ZCLOSE8:
2559 case NFA_ZCLOSE9:
2560 STRCPY(code, "NFA_ZCLOSE(x)");
2561 code[11] = c - NFA_ZCLOSE + '0';
2562 break;
2563#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002564 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2565 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2566 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2567 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002568 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2569 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002570 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2571 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2572 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2573 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2574 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2575 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2576 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2577 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2578 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2579 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2580 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2581 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2582 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2583 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002584 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002585
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002586 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002587 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2588 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2589 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002590 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002591 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002592
2593 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2594 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2595 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2596 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2597 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2598 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2599 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002601 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2602 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2603 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2604 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2605 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2606 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2607 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2608 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2609 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2610 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2611 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2612 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2613 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2614 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2615 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2616 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2617
2618 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2619 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2620 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2621 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2622 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2623 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2624 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2625 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2626 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2627 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2628 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2629 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2630 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2631 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2632 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2633 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2634 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2635 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2636 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2637 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2638 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2639 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2640 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2641 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2642 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2643 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2644 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002645 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2646 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2647 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2648 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649
2650 default:
2651 STRCPY(code, "CHAR(x)");
2652 code[5] = c;
2653 }
2654
2655 if (addnl == TRUE)
2656 STRCAT(code, " + NEWLINE ");
2657
2658}
2659
2660#ifdef ENABLE_LOG
2661static FILE *log_fd;
2662
2663/*
2664 * Print the postfix notation of the current regexp.
2665 */
2666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002667nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002668{
2669 int *p;
2670 FILE *f;
2671
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002672 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002673 if (f != NULL)
2674 {
2675 fprintf(f, "\n-------------------------\n");
2676 if (retval == FAIL)
2677 fprintf(f, ">>> NFA engine failed ... \n");
2678 else if (retval == OK)
2679 fprintf(f, ">>> NFA engine succeeded !\n");
2680 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002681 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002682 {
2683 nfa_set_code(*p);
2684 fprintf(f, "%s, ", code);
2685 }
2686 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002687 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002688 fprintf(f, "%d ", *p);
2689 fprintf(f, "\n\n");
2690 fclose(f);
2691 }
2692}
2693
2694/*
2695 * Print the NFA starting with a root node "state".
2696 */
2697 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002698nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002700 garray_T indent;
2701
2702 ga_init2(&indent, 1, 64);
2703 ga_append(&indent, '\0');
2704 nfa_print_state2(debugf, state, &indent);
2705 ga_clear(&indent);
2706}
2707
2708 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002709nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002710{
2711 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712
2713 if (state == NULL)
2714 return;
2715
2716 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002717
2718 /* Output indent */
2719 p = (char_u *)indent->ga_data;
2720 if (indent->ga_len >= 3)
2721 {
2722 int last = indent->ga_len - 3;
2723 char_u save[2];
2724
2725 STRNCPY(save, &p[last], 2);
2726 STRNCPY(&p[last], "+-", 2);
2727 fprintf(debugf, " %s", p);
2728 STRNCPY(&p[last], save, 2);
2729 }
2730 else
2731 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732
2733 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002734 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002735 code,
2736 state->c,
2737 abs(state->id),
2738 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739 if (state->id < 0)
2740 return;
2741
2742 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002743
2744 /* grow indent for state->out */
2745 indent->ga_len -= 1;
2746 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002747 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002748 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002749 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002750 ga_append(indent, '\0');
2751
2752 nfa_print_state2(debugf, state->out, indent);
2753
2754 /* replace last part of indent for state->out1 */
2755 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002756 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002757 ga_append(indent, '\0');
2758
2759 nfa_print_state2(debugf, state->out1, indent);
2760
2761 /* shrink indent */
2762 indent->ga_len -= 3;
2763 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002764}
2765
2766/*
2767 * Print the NFA state machine.
2768 */
2769 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002770nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002771{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002772 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002773
2774 if (debugf != NULL)
2775 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002776 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002777
Bram Moolenaar473de612013-06-08 18:19:48 +02002778 if (prog->reganch)
2779 fprintf(debugf, "reganch: %d\n", prog->reganch);
2780 if (prog->regstart != NUL)
2781 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2782 prog->regstart, prog->regstart);
2783 if (prog->match_text != NULL)
2784 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002785
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002786 fclose(debugf);
2787 }
2788}
2789#endif /* ENABLE_LOG */
2790#endif /* DEBUG */
2791
2792/*
2793 * Parse r.e. @expr and convert it into postfix form.
2794 * Return the postfix string on success, NULL otherwise.
2795 */
2796 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002797re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798{
2799 if (nfa_reg(REG_NOPAREN) == FAIL)
2800 return NULL;
2801 EMIT(NFA_MOPEN);
2802 return post_start;
2803}
2804
2805/* NB. Some of the code below is inspired by Russ's. */
2806
2807/*
2808 * Represents an NFA state plus zero or one or two arrows exiting.
2809 * if c == MATCH, no arrows out; matching state.
2810 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2811 * If c < 256, labeled arrow with character c to out.
2812 */
2813
2814static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2815
2816/*
2817 * Allocate and initialize nfa_state_T.
2818 */
2819 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002820alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821{
2822 nfa_state_T *s;
2823
2824 if (istate >= nstate)
2825 return NULL;
2826
2827 s = &state_ptr[istate++];
2828
2829 s->c = c;
2830 s->out = out;
2831 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002832 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002833
2834 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002835 s->lastlist[0] = 0;
2836 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002837
2838 return s;
2839}
2840
2841/*
2842 * A partially built NFA without the matching state filled in.
2843 * Frag_T.start points at the start state.
2844 * Frag_T.out is a list of places that need to be set to the
2845 * next state for this fragment.
2846 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002847
2848/* Since the out pointers in the list are always
2849 * uninitialized, we use the pointers themselves
2850 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002852union Ptrlist
2853{
2854 Ptrlist *next;
2855 nfa_state_T *s;
2856};
2857
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002858struct Frag
2859{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002860 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002861 Ptrlist *out;
2862};
2863typedef struct Frag Frag_T;
2864
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002865static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2866static Ptrlist *list1(nfa_state_T **outp);
2867static void patch(Ptrlist *l, nfa_state_T *s);
2868static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2869static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2870static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002871
2872/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002873 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874 */
2875 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002876frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002878 Frag_T n;
2879
2880 n.start = start;
2881 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 return n;
2883}
2884
2885/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 * Create singleton list containing just outp.
2887 */
2888 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002889list1(
2890 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891{
2892 Ptrlist *l;
2893
2894 l = (Ptrlist *)outp;
2895 l->next = NULL;
2896 return l;
2897}
2898
2899/*
2900 * Patch the list of states at out to point to start.
2901 */
2902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002903patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904{
2905 Ptrlist *next;
2906
2907 for (; l; l = next)
2908 {
2909 next = l->next;
2910 l->s = s;
2911 }
2912}
2913
2914
2915/*
2916 * Join the two lists l1 and l2, returning the combination.
2917 */
2918 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002919append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002920{
2921 Ptrlist *oldl1;
2922
2923 oldl1 = l1;
2924 while (l1->next)
2925 l1 = l1->next;
2926 l1->next = l2;
2927 return oldl1;
2928}
2929
2930/*
2931 * Stack used for transforming postfix form into NFA.
2932 */
2933static Frag_T empty;
2934
2935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002936st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002937{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002938#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002939 FILE *df;
2940 int *p2;
2941
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002942 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002943 if (df)
2944 {
2945 fprintf(df, "Error popping the stack!\n");
2946#ifdef DEBUG
2947 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2948#endif
2949 fprintf(df, "Postfix form is: ");
2950#ifdef DEBUG
2951 for (p2 = postfix; p2 < end; p2++)
2952 {
2953 nfa_set_code(*p2);
2954 fprintf(df, "%s, ", code);
2955 }
2956 nfa_set_code(*p);
2957 fprintf(df, "\nCurrent position is: ");
2958 for (p2 = postfix; p2 <= p; p2 ++)
2959 {
2960 nfa_set_code(*p2);
2961 fprintf(df, "%s, ", code);
2962 }
2963#else
2964 for (p2 = postfix; p2 < end; p2++)
2965 {
2966 fprintf(df, "%d, ", *p2);
2967 }
2968 fprintf(df, "\nCurrent position is: ");
2969 for (p2 = postfix; p2 <= p; p2 ++)
2970 {
2971 fprintf(df, "%d, ", *p2);
2972 }
2973#endif
2974 fprintf(df, "\n--------------------------\n");
2975 fclose(df);
2976 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002977#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002978 EMSG(_("E874: (NFA) Could not pop the stack !"));
2979}
2980
2981/*
2982 * Push an item onto the stack.
2983 */
2984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002985st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002986{
2987 Frag_T *stackp = *p;
2988
2989 if (stackp >= stack_end)
2990 return;
2991 *stackp = s;
2992 *p = *p + 1;
2993}
2994
2995/*
2996 * Pop an item from the stack.
2997 */
2998 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002999st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000{
3001 Frag_T *stackp;
3002
3003 *p = *p - 1;
3004 stackp = *p;
3005 if (stackp < stack)
3006 return empty;
3007 return **p;
3008}
3009
3010/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003011 * Estimate the maximum byte length of anything matching "state".
3012 * When unknown or unlimited return -1.
3013 */
3014 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003015nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003016{
3017 int l, r;
3018 nfa_state_T *state = startstate;
3019 int len = 0;
3020
3021 /* detect looping in a NFA_SPLIT */
3022 if (depth > 4)
3023 return -1;
3024
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003025 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003026 {
3027 switch (state->c)
3028 {
3029 case NFA_END_INVISIBLE:
3030 case NFA_END_INVISIBLE_NEG:
3031 /* the end, return what we have */
3032 return len;
3033
3034 case NFA_SPLIT:
3035 /* two alternatives, use the maximum */
3036 l = nfa_max_width(state->out, depth + 1);
3037 r = nfa_max_width(state->out1, depth + 1);
3038 if (l < 0 || r < 0)
3039 return -1;
3040 return len + (l > r ? l : r);
3041
3042 case NFA_ANY:
3043 case NFA_START_COLL:
3044 case NFA_START_NEG_COLL:
3045 /* matches some character, including composing chars */
3046#ifdef FEAT_MBYTE
3047 if (enc_utf8)
3048 len += MB_MAXBYTES;
3049 else if (has_mbyte)
3050 len += 2;
3051 else
3052#endif
3053 ++len;
3054 if (state->c != NFA_ANY)
3055 {
3056 /* skip over the characters */
3057 state = state->out1->out;
3058 continue;
3059 }
3060 break;
3061
3062 case NFA_DIGIT:
3063 case NFA_WHITE:
3064 case NFA_HEX:
3065 case NFA_OCTAL:
3066 /* ascii */
3067 ++len;
3068 break;
3069
3070 case NFA_IDENT:
3071 case NFA_SIDENT:
3072 case NFA_KWORD:
3073 case NFA_SKWORD:
3074 case NFA_FNAME:
3075 case NFA_SFNAME:
3076 case NFA_PRINT:
3077 case NFA_SPRINT:
3078 case NFA_NWHITE:
3079 case NFA_NDIGIT:
3080 case NFA_NHEX:
3081 case NFA_NOCTAL:
3082 case NFA_WORD:
3083 case NFA_NWORD:
3084 case NFA_HEAD:
3085 case NFA_NHEAD:
3086 case NFA_ALPHA:
3087 case NFA_NALPHA:
3088 case NFA_LOWER:
3089 case NFA_NLOWER:
3090 case NFA_UPPER:
3091 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003092 case NFA_LOWER_IC:
3093 case NFA_NLOWER_IC:
3094 case NFA_UPPER_IC:
3095 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003096 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003097 /* possibly non-ascii */
3098#ifdef FEAT_MBYTE
3099 if (has_mbyte)
3100 len += 3;
3101 else
3102#endif
3103 ++len;
3104 break;
3105
3106 case NFA_START_INVISIBLE:
3107 case NFA_START_INVISIBLE_NEG:
3108 case NFA_START_INVISIBLE_BEFORE:
3109 case NFA_START_INVISIBLE_BEFORE_NEG:
3110 /* zero-width, out1 points to the END state */
3111 state = state->out1->out;
3112 continue;
3113
3114 case NFA_BACKREF1:
3115 case NFA_BACKREF2:
3116 case NFA_BACKREF3:
3117 case NFA_BACKREF4:
3118 case NFA_BACKREF5:
3119 case NFA_BACKREF6:
3120 case NFA_BACKREF7:
3121 case NFA_BACKREF8:
3122 case NFA_BACKREF9:
3123#ifdef FEAT_SYN_HL
3124 case NFA_ZREF1:
3125 case NFA_ZREF2:
3126 case NFA_ZREF3:
3127 case NFA_ZREF4:
3128 case NFA_ZREF5:
3129 case NFA_ZREF6:
3130 case NFA_ZREF7:
3131 case NFA_ZREF8:
3132 case NFA_ZREF9:
3133#endif
3134 case NFA_NEWL:
3135 case NFA_SKIP:
3136 /* unknown width */
3137 return -1;
3138
3139 case NFA_BOL:
3140 case NFA_EOL:
3141 case NFA_BOF:
3142 case NFA_EOF:
3143 case NFA_BOW:
3144 case NFA_EOW:
3145 case NFA_MOPEN:
3146 case NFA_MOPEN1:
3147 case NFA_MOPEN2:
3148 case NFA_MOPEN3:
3149 case NFA_MOPEN4:
3150 case NFA_MOPEN5:
3151 case NFA_MOPEN6:
3152 case NFA_MOPEN7:
3153 case NFA_MOPEN8:
3154 case NFA_MOPEN9:
3155#ifdef FEAT_SYN_HL
3156 case NFA_ZOPEN:
3157 case NFA_ZOPEN1:
3158 case NFA_ZOPEN2:
3159 case NFA_ZOPEN3:
3160 case NFA_ZOPEN4:
3161 case NFA_ZOPEN5:
3162 case NFA_ZOPEN6:
3163 case NFA_ZOPEN7:
3164 case NFA_ZOPEN8:
3165 case NFA_ZOPEN9:
3166 case NFA_ZCLOSE:
3167 case NFA_ZCLOSE1:
3168 case NFA_ZCLOSE2:
3169 case NFA_ZCLOSE3:
3170 case NFA_ZCLOSE4:
3171 case NFA_ZCLOSE5:
3172 case NFA_ZCLOSE6:
3173 case NFA_ZCLOSE7:
3174 case NFA_ZCLOSE8:
3175 case NFA_ZCLOSE9:
3176#endif
3177 case NFA_MCLOSE:
3178 case NFA_MCLOSE1:
3179 case NFA_MCLOSE2:
3180 case NFA_MCLOSE3:
3181 case NFA_MCLOSE4:
3182 case NFA_MCLOSE5:
3183 case NFA_MCLOSE6:
3184 case NFA_MCLOSE7:
3185 case NFA_MCLOSE8:
3186 case NFA_MCLOSE9:
3187 case NFA_NOPEN:
3188 case NFA_NCLOSE:
3189
3190 case NFA_LNUM_GT:
3191 case NFA_LNUM_LT:
3192 case NFA_COL_GT:
3193 case NFA_COL_LT:
3194 case NFA_VCOL_GT:
3195 case NFA_VCOL_LT:
3196 case NFA_MARK_GT:
3197 case NFA_MARK_LT:
3198 case NFA_VISUAL:
3199 case NFA_LNUM:
3200 case NFA_CURSOR:
3201 case NFA_COL:
3202 case NFA_VCOL:
3203 case NFA_MARK:
3204
3205 case NFA_ZSTART:
3206 case NFA_ZEND:
3207 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003208 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003209 case NFA_START_PATTERN:
3210 case NFA_END_PATTERN:
3211 case NFA_COMPOSING:
3212 case NFA_END_COMPOSING:
3213 /* zero-width */
3214 break;
3215
3216 default:
3217 if (state->c < 0)
3218 /* don't know what this is */
3219 return -1;
3220 /* normal character */
3221 len += MB_CHAR2LEN(state->c);
3222 break;
3223 }
3224
3225 /* normal way to continue */
3226 state = state->out;
3227 }
3228
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003229 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003230 return -1;
3231}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003232
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003233/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 * Convert a postfix form into its equivalent NFA.
3235 * Return the NFA start state on success, NULL otherwise.
3236 */
3237 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003238post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239{
3240 int *p;
3241 int mopen;
3242 int mclose;
3243 Frag_T *stack = NULL;
3244 Frag_T *stackp = NULL;
3245 Frag_T *stack_end = NULL;
3246 Frag_T e1;
3247 Frag_T e2;
3248 Frag_T e;
3249 nfa_state_T *s;
3250 nfa_state_T *s1;
3251 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003252 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253
3254 if (postfix == NULL)
3255 return NULL;
3256
Bram Moolenaar053bb602013-05-20 13:55:21 +02003257#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003258#define POP() st_pop(&stackp, stack); \
3259 if (stackp < stack) \
3260 { \
3261 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003262 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263 return NULL; \
3264 }
3265
3266 if (nfa_calc_size == FALSE)
3267 {
3268 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003269 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003271 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003272 }
3273
3274 for (p = postfix; p < end; ++p)
3275 {
3276 switch (*p)
3277 {
3278 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003279 /* Concatenation.
3280 * Pay attention: this operator does not exist in the r.e. itself
3281 * (it is implicit, really). It is added when r.e. is translated
3282 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 if (nfa_calc_size == TRUE)
3284 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003285 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 break;
3287 }
3288 e2 = POP();
3289 e1 = POP();
3290 patch(e1.out, e2.start);
3291 PUSH(frag(e1.start, e2.out));
3292 break;
3293
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 case NFA_OR:
3295 /* Alternation */
3296 if (nfa_calc_size == TRUE)
3297 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003298 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 break;
3300 }
3301 e2 = POP();
3302 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003303 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003305 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306 PUSH(frag(s, append(e1.out, e2.out)));
3307 break;
3308
3309 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003310 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311 if (nfa_calc_size == TRUE)
3312 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003313 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 break;
3315 }
3316 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003317 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003319 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 patch(e.out, s);
3321 PUSH(frag(s, list1(&s->out1)));
3322 break;
3323
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003324 case NFA_STAR_NONGREEDY:
3325 /* Zero or more, prefer zero */
3326 if (nfa_calc_size == TRUE)
3327 {
3328 nstate++;
3329 break;
3330 }
3331 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003332 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003333 if (s == NULL)
3334 goto theend;
3335 patch(e.out, s);
3336 PUSH(frag(s, list1(&s->out)));
3337 break;
3338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339 case NFA_QUEST:
3340 /* one or zero atoms=> greedy match */
3341 if (nfa_calc_size == TRUE)
3342 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003343 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003344 break;
3345 }
3346 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003347 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003349 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350 PUSH(frag(s, append(e.out, list1(&s->out1))));
3351 break;
3352
3353 case NFA_QUEST_NONGREEDY:
3354 /* zero or one atoms => non-greedy match */
3355 if (nfa_calc_size == TRUE)
3356 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003357 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 break;
3359 }
3360 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003361 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003363 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 PUSH(frag(s, append(e.out, list1(&s->out))));
3365 break;
3366
Bram Moolenaar417bad22013-06-07 14:08:30 +02003367 case NFA_END_COLL:
3368 case NFA_END_NEG_COLL:
3369 /* On the stack is the sequence starting with NFA_START_COLL or
3370 * NFA_START_NEG_COLL and all possible characters. Patch it to
3371 * add the output to the start. */
3372 if (nfa_calc_size == TRUE)
3373 {
3374 nstate++;
3375 break;
3376 }
3377 e = POP();
3378 s = alloc_state(NFA_END_COLL, NULL, NULL);
3379 if (s == NULL)
3380 goto theend;
3381 patch(e.out, s);
3382 e.start->out1 = s;
3383 PUSH(frag(e.start, list1(&s->out)));
3384 break;
3385
3386 case NFA_RANGE:
3387 /* Before this are two characters, the low and high end of a
3388 * range. Turn them into two states with MIN and MAX. */
3389 if (nfa_calc_size == TRUE)
3390 {
3391 /* nstate += 0; */
3392 break;
3393 }
3394 e2 = POP();
3395 e1 = POP();
3396 e2.start->val = e2.start->c;
3397 e2.start->c = NFA_RANGE_MAX;
3398 e1.start->val = e1.start->c;
3399 e1.start->c = NFA_RANGE_MIN;
3400 patch(e1.out, e2.start);
3401 PUSH(frag(e1.start, e2.out));
3402 break;
3403
Bram Moolenaar699c1202013-09-25 16:41:54 +02003404 case NFA_EMPTY:
3405 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003406 if (nfa_calc_size == TRUE)
3407 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003408 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003409 break;
3410 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003411 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003413 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003414 PUSH(frag(s, list1(&s->out)));
3415 break;
3416
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003417 case NFA_OPT_CHARS:
3418 {
3419 int n;
3420
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003421 /* \%[abc] implemented as:
3422 * NFA_SPLIT
3423 * +-CHAR(a)
3424 * | +-NFA_SPLIT
3425 * | +-CHAR(b)
3426 * | | +-NFA_SPLIT
3427 * | | +-CHAR(c)
3428 * | | | +-next
3429 * | | +- next
3430 * | +- next
3431 * +- next
3432 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003433 n = *++p; /* get number of characters */
3434 if (nfa_calc_size == TRUE)
3435 {
3436 nstate += n;
3437 break;
3438 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003439 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003440 e1.out = NULL; /* stores list with out1's */
3441 s1 = NULL; /* previous NFA_SPLIT to connect to */
3442 while (n-- > 0)
3443 {
3444 e = POP(); /* get character */
3445 s = alloc_state(NFA_SPLIT, e.start, NULL);
3446 if (s == NULL)
3447 goto theend;
3448 if (e1.out == NULL)
3449 e1 = e;
3450 patch(e.out, s1);
3451 append(e1.out, list1(&s->out1));
3452 s1 = s;
3453 }
3454 PUSH(frag(s, e1.out));
3455 break;
3456 }
3457
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003459 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003460 case NFA_PREV_ATOM_JUST_BEFORE:
3461 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003462 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003463 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003464 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3465 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003466 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003467 int start_state;
3468 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003469 int n = 0;
3470 nfa_state_T *zend;
3471 nfa_state_T *skip;
3472
Bram Moolenaardecd9542013-06-07 16:31:50 +02003473 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003474 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003475 case NFA_PREV_ATOM_NO_WIDTH:
3476 start_state = NFA_START_INVISIBLE;
3477 end_state = NFA_END_INVISIBLE;
3478 break;
3479 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3480 start_state = NFA_START_INVISIBLE_NEG;
3481 end_state = NFA_END_INVISIBLE_NEG;
3482 break;
3483 case NFA_PREV_ATOM_JUST_BEFORE:
3484 start_state = NFA_START_INVISIBLE_BEFORE;
3485 end_state = NFA_END_INVISIBLE;
3486 break;
3487 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3488 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3489 end_state = NFA_END_INVISIBLE_NEG;
3490 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003491 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003492 start_state = NFA_START_PATTERN;
3493 end_state = NFA_END_PATTERN;
3494 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003495 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003496
3497 if (before)
3498 n = *++p; /* get the count */
3499
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003500 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003501 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003502 * The \@<= operator: match for the preceding atom.
3503 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003505 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003506
3507 if (nfa_calc_size == TRUE)
3508 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003509 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 break;
3511 }
3512 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003513 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003515 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003516
Bram Moolenaar87953742013-06-05 18:52:40 +02003517 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003519 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003520 if (pattern)
3521 {
3522 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3523 skip = alloc_state(NFA_SKIP, NULL, NULL);
3524 zend = alloc_state(NFA_ZEND, s1, NULL);
3525 s1->out= skip;
3526 patch(e.out, zend);
3527 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003528 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003529 else
3530 {
3531 patch(e.out, s1);
3532 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003533 if (before)
3534 {
3535 if (n <= 0)
3536 /* See if we can guess the maximum width, it avoids a
3537 * lot of pointless tries. */
3538 n = nfa_max_width(e.start, 0);
3539 s->val = n; /* store the count */
3540 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003541 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003543 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003544
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003545#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003546 case NFA_COMPOSING: /* char with composing char */
3547#if 0
3548 /* TODO */
3549 if (regflags & RF_ICOMBINE)
3550 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003551 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003552 }
3553#endif
3554 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003555#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003556
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003557 case NFA_MOPEN: /* \( \) Submatch */
3558 case NFA_MOPEN1:
3559 case NFA_MOPEN2:
3560 case NFA_MOPEN3:
3561 case NFA_MOPEN4:
3562 case NFA_MOPEN5:
3563 case NFA_MOPEN6:
3564 case NFA_MOPEN7:
3565 case NFA_MOPEN8:
3566 case NFA_MOPEN9:
3567#ifdef FEAT_SYN_HL
3568 case NFA_ZOPEN: /* \z( \) Submatch */
3569 case NFA_ZOPEN1:
3570 case NFA_ZOPEN2:
3571 case NFA_ZOPEN3:
3572 case NFA_ZOPEN4:
3573 case NFA_ZOPEN5:
3574 case NFA_ZOPEN6:
3575 case NFA_ZOPEN7:
3576 case NFA_ZOPEN8:
3577 case NFA_ZOPEN9:
3578#endif
3579 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 if (nfa_calc_size == TRUE)
3581 {
3582 nstate += 2;
3583 break;
3584 }
3585
3586 mopen = *p;
3587 switch (*p)
3588 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003589 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3590#ifdef FEAT_SYN_HL
3591 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3592 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3593 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3594 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3595 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3596 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3597 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3598 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3599 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3600 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3601#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003602#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003603 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003604#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003606 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003607 mclose = *p + NSUBEXP;
3608 break;
3609 }
3610
3611 /* Allow "NFA_MOPEN" as a valid postfix representation for
3612 * the empty regexp "". In this case, the NFA will be
3613 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3614 * empty groups of parenthesis, and empty mbyte chars */
3615 if (stackp == stack)
3616 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003617 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003618 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003619 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003620 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003622 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 patch(list1(&s->out), s1);
3624 PUSH(frag(s, list1(&s1->out)));
3625 break;
3626 }
3627
3628 /* At least one node was emitted before NFA_MOPEN, so
3629 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3630 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003631 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003633 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634
Bram Moolenaar525666f2013-06-02 16:40:55 +02003635 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003637 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003638 patch(e.out, s1);
3639
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003640#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003641 if (mopen == NFA_COMPOSING)
3642 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003644#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003645
3646 PUSH(frag(s, list1(&s1->out)));
3647 break;
3648
Bram Moolenaar5714b802013-05-28 22:03:20 +02003649 case NFA_BACKREF1:
3650 case NFA_BACKREF2:
3651 case NFA_BACKREF3:
3652 case NFA_BACKREF4:
3653 case NFA_BACKREF5:
3654 case NFA_BACKREF6:
3655 case NFA_BACKREF7:
3656 case NFA_BACKREF8:
3657 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003658#ifdef FEAT_SYN_HL
3659 case NFA_ZREF1:
3660 case NFA_ZREF2:
3661 case NFA_ZREF3:
3662 case NFA_ZREF4:
3663 case NFA_ZREF5:
3664 case NFA_ZREF6:
3665 case NFA_ZREF7:
3666 case NFA_ZREF8:
3667 case NFA_ZREF9:
3668#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003669 if (nfa_calc_size == TRUE)
3670 {
3671 nstate += 2;
3672 break;
3673 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003674 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003675 if (s == NULL)
3676 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003677 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003678 if (s1 == NULL)
3679 goto theend;
3680 patch(list1(&s->out), s1);
3681 PUSH(frag(s, list1(&s1->out)));
3682 break;
3683
Bram Moolenaar423532e2013-05-29 21:14:42 +02003684 case NFA_LNUM:
3685 case NFA_LNUM_GT:
3686 case NFA_LNUM_LT:
3687 case NFA_VCOL:
3688 case NFA_VCOL_GT:
3689 case NFA_VCOL_LT:
3690 case NFA_COL:
3691 case NFA_COL_GT:
3692 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003693 case NFA_MARK:
3694 case NFA_MARK_GT:
3695 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003696 {
3697 int n = *++p; /* lnum, col or mark name */
3698
Bram Moolenaar423532e2013-05-29 21:14:42 +02003699 if (nfa_calc_size == TRUE)
3700 {
3701 nstate += 1;
3702 break;
3703 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003704 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003705 if (s == NULL)
3706 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003707 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003708 PUSH(frag(s, list1(&s->out)));
3709 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003710 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003711
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712 case NFA_ZSTART:
3713 case NFA_ZEND:
3714 default:
3715 /* Operands */
3716 if (nfa_calc_size == TRUE)
3717 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003718 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003719 break;
3720 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003721 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003723 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 PUSH(frag(s, list1(&s->out)));
3725 break;
3726
3727 } /* switch(*p) */
3728
3729 } /* for(p = postfix; *p; ++p) */
3730
3731 if (nfa_calc_size == TRUE)
3732 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003733 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003734 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003735 }
3736
3737 e = POP();
3738 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003739 {
3740 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003742 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003743
3744 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003745 {
3746 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003748 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750 matchstate = &state_ptr[istate++]; /* the match state */
3751 matchstate->c = NFA_MATCH;
3752 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003753 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754
3755 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003756 ret = e.start;
3757
3758theend:
3759 vim_free(stack);
3760 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003761
3762#undef POP1
3763#undef PUSH1
3764#undef POP2
3765#undef PUSH2
3766#undef POP
3767#undef PUSH
3768}
3769
Bram Moolenaara2947e22013-06-11 22:44:09 +02003770/*
3771 * After building the NFA program, inspect it to add optimization hints.
3772 */
3773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003774nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003775{
3776 int i;
3777 int c;
3778
3779 for (i = 0; i < prog->nstate; ++i)
3780 {
3781 c = prog->state[i].c;
3782 if (c == NFA_START_INVISIBLE
3783 || c == NFA_START_INVISIBLE_NEG
3784 || c == NFA_START_INVISIBLE_BEFORE
3785 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3786 {
3787 int directly;
3788
3789 /* Do it directly when what follows is possibly the end of the
3790 * match. */
3791 if (match_follows(prog->state[i].out1->out, 0))
3792 directly = TRUE;
3793 else
3794 {
3795 int ch_invisible = failure_chance(prog->state[i].out, 0);
3796 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3797
3798 /* Postpone when the invisible match is expensive or has a
3799 * lower chance of failing. */
3800 if (c == NFA_START_INVISIBLE_BEFORE
3801 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3802 {
3803 /* "before" matches are very expensive when
3804 * unbounded, always prefer what follows then,
3805 * unless what follows will always match.
3806 * Otherwise strongly prefer what follows. */
3807 if (prog->state[i].val <= 0 && ch_follows > 0)
3808 directly = FALSE;
3809 else
3810 directly = ch_follows * 10 < ch_invisible;
3811 }
3812 else
3813 {
3814 /* normal invisible, first do the one with the
3815 * highest failure chance */
3816 directly = ch_follows < ch_invisible;
3817 }
3818 }
3819 if (directly)
3820 /* switch to the _FIRST state */
3821 ++prog->state[i].c;
3822 }
3823 }
3824}
3825
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003826/****************************************************************
3827 * NFA execution code.
3828 ****************************************************************/
3829
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003830typedef struct
3831{
3832 int in_use; /* number of subexpr with useful info */
3833
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003834 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003835 union
3836 {
3837 struct multipos
3838 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003839 linenr_T start_lnum;
3840 linenr_T end_lnum;
3841 colnr_T start_col;
3842 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003843 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003844 struct linepos
3845 {
3846 char_u *start;
3847 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003848 } line[NSUBEXP];
3849 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003850} regsub_T;
3851
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003852typedef struct
3853{
3854 regsub_T norm; /* \( .. \) matches */
3855#ifdef FEAT_SYN_HL
3856 regsub_T synt; /* \z( .. \) matches */
3857#endif
3858} regsubs_T;
3859
Bram Moolenaara2d95102013-06-04 14:23:05 +02003860/* nfa_pim_T stores a Postponed Invisible Match. */
3861typedef struct nfa_pim_S nfa_pim_T;
3862struct nfa_pim_S
3863{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003864 int result; /* NFA_PIM_*, see below */
3865 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003866 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003867 union
3868 {
3869 lpos_T pos;
3870 char_u *ptr;
3871 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003872};
3873
3874/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003875#define NFA_PIM_UNUSED 0 /* pim not used */
3876#define NFA_PIM_TODO 1 /* pim not done yet */
3877#define NFA_PIM_MATCH 2 /* pim executed, matches */
3878#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003879
3880
Bram Moolenaar963fee22013-05-26 21:47:28 +02003881/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003882typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003883{
3884 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003885 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003886 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3887 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003888 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003889} nfa_thread_T;
3890
Bram Moolenaar963fee22013-05-26 21:47:28 +02003891/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003892typedef struct
3893{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003894 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003895 int n; /* nr of states currently in "t" */
3896 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003897 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003898 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003899} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900
Bram Moolenaar5714b802013-05-28 22:03:20 +02003901#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003902static void log_subsexpr(regsubs_T *subs);
3903static void log_subexpr(regsub_T *sub);
3904static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003905
3906 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003907log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003908{
3909 log_subexpr(&subs->norm);
3910# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003911 if (nfa_has_zsubexpr)
3912 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003913# endif
3914}
3915
Bram Moolenaar5714b802013-05-28 22:03:20 +02003916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003917log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003918{
3919 int j;
3920
3921 for (j = 0; j < sub->in_use; j++)
3922 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003923 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003924 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003925 sub->list.multi[j].start_col,
3926 (int)sub->list.multi[j].start_lnum,
3927 sub->list.multi[j].end_col,
3928 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003929 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003930 {
3931 char *s = (char *)sub->list.line[j].start;
3932 char *e = (char *)sub->list.line[j].end;
3933
Bram Moolenaar87953742013-06-05 18:52:40 +02003934 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003935 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003936 s == NULL ? "NULL" : s,
3937 e == NULL ? "NULL" : e);
3938 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003939}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003940
3941 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003942pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003943{
3944 static char buf[30];
3945
3946 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3947 buf[0] = NUL;
3948 else
3949 {
3950 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3951 : (int)(pim->end.ptr - reginput));
3952 }
3953 return buf;
3954}
3955
Bram Moolenaar5714b802013-05-28 22:03:20 +02003956#endif
3957
Bram Moolenaar963fee22013-05-26 21:47:28 +02003958/* Used during execution: whether a match has been found. */
3959static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003960#ifdef FEAT_RELTIME
3961static proftime_T *nfa_time_limit;
3962static int nfa_time_count;
3963#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003964
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003965static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3966static void clear_sub(regsub_T *sub);
3967static void copy_sub(regsub_T *to, regsub_T *from);
3968static void copy_sub_off(regsub_T *to, regsub_T *from);
3969static void copy_ze_off(regsub_T *to, regsub_T *from);
3970static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3971static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3972static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3973static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3974static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3975static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3976static 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 +02003977
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003978/*
3979 * Copy postponed invisible match info from "from" to "to".
3980 */
3981 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003982copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003983{
3984 to->result = from->result;
3985 to->state = from->state;
3986 copy_sub(&to->subs.norm, &from->subs.norm);
3987#ifdef FEAT_SYN_HL
3988 if (nfa_has_zsubexpr)
3989 copy_sub(&to->subs.synt, &from->subs.synt);
3990#endif
3991 to->end = from->end;
3992}
3993
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003994 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003995clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003996{
3997 if (REG_MULTI)
3998 /* Use 0xff to set lnum to -1 */
3999 vim_memset(sub->list.multi, 0xff,
4000 sizeof(struct multipos) * nfa_nsubexpr);
4001 else
4002 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4003 sub->in_use = 0;
4004}
4005
4006/*
4007 * Copy the submatches from "from" to "to".
4008 */
4009 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004010copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004011{
4012 to->in_use = from->in_use;
4013 if (from->in_use > 0)
4014 {
4015 /* Copy the match start and end positions. */
4016 if (REG_MULTI)
4017 mch_memmove(&to->list.multi[0],
4018 &from->list.multi[0],
4019 sizeof(struct multipos) * from->in_use);
4020 else
4021 mch_memmove(&to->list.line[0],
4022 &from->list.line[0],
4023 sizeof(struct linepos) * from->in_use);
4024 }
4025}
4026
4027/*
4028 * Like copy_sub() but exclude the main match.
4029 */
4030 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004031copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004032{
4033 if (to->in_use < from->in_use)
4034 to->in_use = from->in_use;
4035 if (from->in_use > 1)
4036 {
4037 /* Copy the match start and end positions. */
4038 if (REG_MULTI)
4039 mch_memmove(&to->list.multi[1],
4040 &from->list.multi[1],
4041 sizeof(struct multipos) * (from->in_use - 1));
4042 else
4043 mch_memmove(&to->list.line[1],
4044 &from->list.line[1],
4045 sizeof(struct linepos) * (from->in_use - 1));
4046 }
4047}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004048
Bram Moolenaar428e9872013-05-30 17:05:39 +02004049/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004050 * Like copy_sub() but only do the end of the main match if \ze is present.
4051 */
4052 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004053copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004054{
4055 if (nfa_has_zend)
4056 {
4057 if (REG_MULTI)
4058 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004059 if (from->list.multi[0].end_lnum >= 0)
4060 {
4061 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4062 to->list.multi[0].end_col = from->list.multi[0].end_col;
4063 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004064 }
4065 else
4066 {
4067 if (from->list.line[0].end != NULL)
4068 to->list.line[0].end = from->list.line[0].end;
4069 }
4070 }
4071}
4072
4073/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004074 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004075 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004076 */
4077 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004078sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004079{
4080 int i;
4081 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004082 linenr_T s1;
4083 linenr_T s2;
4084 char_u *sp1;
4085 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004086
4087 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4088 if (REG_MULTI)
4089 {
4090 for (i = 0; i < todo; ++i)
4091 {
4092 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004093 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004094 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004095 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004096 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004097 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004098 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004099 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004100 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004101 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004102 if (s1 != -1 && sub1->list.multi[i].start_col
4103 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004104 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004105
4106 if (nfa_has_backref)
4107 {
4108 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004109 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004110 else
4111 s1 = -1;
4112 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004113 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004114 else
4115 s2 = -1;
4116 if (s1 != s2)
4117 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004118 if (s1 != -1 && sub1->list.multi[i].end_col
4119 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004120 return FALSE;
4121 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004122 }
4123 }
4124 else
4125 {
4126 for (i = 0; i < todo; ++i)
4127 {
4128 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004129 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004130 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004131 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004132 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004133 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004134 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004135 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004136 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004137 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004138 if (nfa_has_backref)
4139 {
4140 if (i < sub1->in_use)
4141 sp1 = sub1->list.line[i].end;
4142 else
4143 sp1 = NULL;
4144 if (i < sub2->in_use)
4145 sp2 = sub2->list.line[i].end;
4146 else
4147 sp2 = NULL;
4148 if (sp1 != sp2)
4149 return FALSE;
4150 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004151 }
4152 }
4153
4154 return TRUE;
4155}
4156
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004157#ifdef ENABLE_LOG
4158 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004159report_state(char *action,
4160 regsub_T *sub,
4161 nfa_state_T *state,
4162 int lid,
4163 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004164{
4165 int col;
4166
4167 if (sub->in_use <= 0)
4168 col = -1;
4169 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004170 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004171 else
4172 col = (int)(sub->list.line[0].start - regline);
4173 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004174 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4175 action, abs(state->id), lid, state->c, code, col,
4176 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004177}
4178#endif
4179
Bram Moolenaar43e02982013-06-07 17:31:29 +02004180/*
4181 * Return TRUE if the same state is already in list "l" with the same
4182 * positions as "subs".
4183 */
4184 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004185has_state_with_pos(
4186 nfa_list_T *l, /* runtime state list */
4187 nfa_state_T *state, /* state to update */
4188 regsubs_T *subs, /* pointers to subexpressions */
4189 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004190{
4191 nfa_thread_T *thread;
4192 int i;
4193
4194 for (i = 0; i < l->n; ++i)
4195 {
4196 thread = &l->t[i];
4197 if (thread->state->id == state->id
4198 && sub_equal(&thread->subs.norm, &subs->norm)
4199#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004200 && (!nfa_has_zsubexpr
4201 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004202#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004203 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004204 return TRUE;
4205 }
4206 return FALSE;
4207}
4208
4209/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004210 * Return TRUE if "one" and "two" are equal. That includes when both are not
4211 * set.
4212 */
4213 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004214pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004215{
4216 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4217 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4218
4219 if (one_unused)
4220 /* one is unused: equal when two is also unused */
4221 return two_unused;
4222 if (two_unused)
4223 /* one is used and two is not: not equal */
4224 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004225 /* compare the state id */
4226 if (one->state->id != two->state->id)
4227 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004228 /* compare the position */
4229 if (REG_MULTI)
4230 return one->end.pos.lnum == two->end.pos.lnum
4231 && one->end.pos.col == two->end.pos.col;
4232 return one->end.ptr == two->end.ptr;
4233}
4234
4235/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004236 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4237 */
4238 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004239match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004240{
4241 nfa_state_T *state = startstate;
4242
4243 /* avoid too much recursion */
4244 if (depth > 10)
4245 return FALSE;
4246
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004247 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004248 {
4249 switch (state->c)
4250 {
4251 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004252 case NFA_MCLOSE:
4253 case NFA_END_INVISIBLE:
4254 case NFA_END_INVISIBLE_NEG:
4255 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004256 return TRUE;
4257
4258 case NFA_SPLIT:
4259 return match_follows(state->out, depth + 1)
4260 || match_follows(state->out1, depth + 1);
4261
4262 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004263 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004264 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004265 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004266 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004267 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004268 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004269 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004270 case NFA_COMPOSING:
4271 /* skip ahead to next state */
4272 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004273 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004274
4275 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004276 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004277 case NFA_IDENT:
4278 case NFA_SIDENT:
4279 case NFA_KWORD:
4280 case NFA_SKWORD:
4281 case NFA_FNAME:
4282 case NFA_SFNAME:
4283 case NFA_PRINT:
4284 case NFA_SPRINT:
4285 case NFA_WHITE:
4286 case NFA_NWHITE:
4287 case NFA_DIGIT:
4288 case NFA_NDIGIT:
4289 case NFA_HEX:
4290 case NFA_NHEX:
4291 case NFA_OCTAL:
4292 case NFA_NOCTAL:
4293 case NFA_WORD:
4294 case NFA_NWORD:
4295 case NFA_HEAD:
4296 case NFA_NHEAD:
4297 case NFA_ALPHA:
4298 case NFA_NALPHA:
4299 case NFA_LOWER:
4300 case NFA_NLOWER:
4301 case NFA_UPPER:
4302 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004303 case NFA_LOWER_IC:
4304 case NFA_NLOWER_IC:
4305 case NFA_UPPER_IC:
4306 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004307 case NFA_START_COLL:
4308 case NFA_START_NEG_COLL:
4309 case NFA_NEWL:
4310 /* state will advance input */
4311 return FALSE;
4312
4313 default:
4314 if (state->c > 0)
4315 /* state will advance input */
4316 return FALSE;
4317
4318 /* Others: zero-width or possibly zero-width, might still find
4319 * a match at the same position, keep looking. */
4320 break;
4321 }
4322 state = state->out;
4323 }
4324 return FALSE;
4325}
4326
4327
4328/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004329 * Return TRUE if "state" is already in list "l".
4330 */
4331 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004332state_in_list(
4333 nfa_list_T *l, /* runtime state list */
4334 nfa_state_T *state, /* state to update */
4335 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004336{
4337 if (state->lastlist[nfa_ll_index] == l->id)
4338 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004339 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004340 return TRUE;
4341 }
4342 return FALSE;
4343}
4344
Bram Moolenaar16b35782016-09-09 20:29:50 +02004345/* Offset used for "off" by addstate_here(). */
4346#define ADDSTATE_HERE_OFFSET 10
4347
Bram Moolenaard05bf562013-06-30 23:24:08 +02004348/*
4349 * Add "state" and possibly what follows to state list ".".
4350 * Returns "subs_arg", possibly copied into temp_subs.
4351 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004352 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004353addstate(
4354 nfa_list_T *l, /* runtime state list */
4355 nfa_state_T *state, /* state to update */
4356 regsubs_T *subs_arg, /* pointers to subexpressions */
4357 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004358 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004359{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004360 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004361 int off = off_arg;
4362 int add_here = FALSE;
4363 int listindex = 0;
4364 int k;
4365 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004366 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004367 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004368 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004369 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004370 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004371 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004372 regsubs_T *subs = subs_arg;
4373 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004374#ifdef ENABLE_LOG
4375 int did_print = FALSE;
4376#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004377
Bram Moolenaar16b35782016-09-09 20:29:50 +02004378 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4379 {
4380 add_here = TRUE;
4381 off = 0;
4382 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4383 }
4384
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004385 switch (state->c)
4386 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004387 case NFA_NCLOSE:
4388 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004389 case NFA_MCLOSE1:
4390 case NFA_MCLOSE2:
4391 case NFA_MCLOSE3:
4392 case NFA_MCLOSE4:
4393 case NFA_MCLOSE5:
4394 case NFA_MCLOSE6:
4395 case NFA_MCLOSE7:
4396 case NFA_MCLOSE8:
4397 case NFA_MCLOSE9:
4398#ifdef FEAT_SYN_HL
4399 case NFA_ZCLOSE:
4400 case NFA_ZCLOSE1:
4401 case NFA_ZCLOSE2:
4402 case NFA_ZCLOSE3:
4403 case NFA_ZCLOSE4:
4404 case NFA_ZCLOSE5:
4405 case NFA_ZCLOSE6:
4406 case NFA_ZCLOSE7:
4407 case NFA_ZCLOSE8:
4408 case NFA_ZCLOSE9:
4409#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004410 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004411 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004412 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004413 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004414 /* These nodes are not added themselves but their "out" and/or
4415 * "out1" may be added below. */
4416 break;
4417
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004418 case NFA_BOL:
4419 case NFA_BOF:
4420 /* "^" won't match past end-of-line, don't bother trying.
4421 * Except when at the end of the line, or when we are going to the
4422 * next line for a look-behind match. */
4423 if (reginput > regline
4424 && *reginput != NUL
4425 && (nfa_endp == NULL
4426 || !REG_MULTI
4427 || reglnum == nfa_endp->se_u.pos.lnum))
4428 goto skip_add;
4429 /* FALLTHROUGH */
4430
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004431 case NFA_MOPEN1:
4432 case NFA_MOPEN2:
4433 case NFA_MOPEN3:
4434 case NFA_MOPEN4:
4435 case NFA_MOPEN5:
4436 case NFA_MOPEN6:
4437 case NFA_MOPEN7:
4438 case NFA_MOPEN8:
4439 case NFA_MOPEN9:
4440#ifdef FEAT_SYN_HL
4441 case NFA_ZOPEN:
4442 case NFA_ZOPEN1:
4443 case NFA_ZOPEN2:
4444 case NFA_ZOPEN3:
4445 case NFA_ZOPEN4:
4446 case NFA_ZOPEN5:
4447 case NFA_ZOPEN6:
4448 case NFA_ZOPEN7:
4449 case NFA_ZOPEN8:
4450 case NFA_ZOPEN9:
4451#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004452 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004453 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004454 /* These nodes need to be added so that we can bail out when it
4455 * was added to this list before at the same position to avoid an
4456 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004457
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004458 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004459 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004460 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004461 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004462 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004463 * when there is a PIM. For NFA_MATCH check the position,
4464 * lower position is preferred. */
4465 if (!nfa_has_backref && pim == NULL && !l->has_pim
4466 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004467 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004468 /* When called from addstate_here() do insert before
4469 * existing states. */
4470 if (add_here)
4471 {
4472 for (k = 0; k < l->n && k < listindex; ++k)
4473 if (l->t[k].state->id == state->id)
4474 {
4475 found = TRUE;
4476 break;
4477 }
4478 }
4479 if (!add_here || found)
4480 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004481skip_add:
4482#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004483 nfa_set_code(state->c);
4484 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4485 abs(state->id), l->id, state->c, code,
4486 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004487#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004488 return subs;
4489 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004490 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004491
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004492 /* Do not add the state again when it exists with the same
4493 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004494 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004495 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004496 }
4497
Bram Moolenaara0169122013-06-26 18:16:58 +02004498 /* When there are backreferences or PIMs the number of states may
4499 * be (a lot) bigger than anticipated. */
4500 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004501 {
4502 int newlen = l->len * 3 / 2 + 50;
4503
Bram Moolenaard05bf562013-06-30 23:24:08 +02004504 if (subs != &temp_subs)
4505 {
4506 /* "subs" may point into the current array, need to make a
4507 * copy before it becomes invalid. */
4508 copy_sub(&temp_subs.norm, &subs->norm);
4509#ifdef FEAT_SYN_HL
4510 if (nfa_has_zsubexpr)
4511 copy_sub(&temp_subs.synt, &subs->synt);
4512#endif
4513 subs = &temp_subs;
4514 }
4515
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004516 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004517 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4518 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004520
4521 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004522 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004523 thread = &l->t[l->n++];
4524 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004525 if (pim == NULL)
4526 thread->pim.result = NFA_PIM_UNUSED;
4527 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004528 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004529 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004530 l->has_pim = TRUE;
4531 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004532 copy_sub(&thread->subs.norm, &subs->norm);
4533#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004534 if (nfa_has_zsubexpr)
4535 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004536#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004537#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004538 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004539 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004540#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004541 }
4542
4543#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004544 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004545 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004546#endif
4547 switch (state->c)
4548 {
4549 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550 break;
4551
4552 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004553 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004554 subs = addstate(l, state->out, subs, pim, off_arg);
4555 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004556 break;
4557
Bram Moolenaar699c1202013-09-25 16:41:54 +02004558 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 case NFA_NOPEN:
4560 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004561 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004562 break;
4563
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004564 case NFA_MOPEN:
4565 case NFA_MOPEN1:
4566 case NFA_MOPEN2:
4567 case NFA_MOPEN3:
4568 case NFA_MOPEN4:
4569 case NFA_MOPEN5:
4570 case NFA_MOPEN6:
4571 case NFA_MOPEN7:
4572 case NFA_MOPEN8:
4573 case NFA_MOPEN9:
4574#ifdef FEAT_SYN_HL
4575 case NFA_ZOPEN:
4576 case NFA_ZOPEN1:
4577 case NFA_ZOPEN2:
4578 case NFA_ZOPEN3:
4579 case NFA_ZOPEN4:
4580 case NFA_ZOPEN5:
4581 case NFA_ZOPEN6:
4582 case NFA_ZOPEN7:
4583 case NFA_ZOPEN8:
4584 case NFA_ZOPEN9:
4585#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004586 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004587 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004588 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004589 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004590 sub = &subs->norm;
4591 }
4592#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004593 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004594 {
4595 subidx = state->c - NFA_ZOPEN;
4596 sub = &subs->synt;
4597 }
4598#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004599 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004600 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004601 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004602 sub = &subs->norm;
4603 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004604
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004605 /* avoid compiler warnings */
4606 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004607 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004608
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004609 /* Set the position (with "off" added) in the subexpression. Save
4610 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611 if (REG_MULTI)
4612 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004613 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004614 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004615 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004616 save_in_use = -1;
4617 }
4618 else
4619 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004620 save_in_use = sub->in_use;
4621 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004622 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004623 sub->list.multi[i].start_lnum = -1;
4624 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004625 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004626 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004627 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004628 if (off == -1)
4629 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004630 sub->list.multi[subidx].start_lnum = reglnum + 1;
4631 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004632 }
4633 else
4634 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004635 sub->list.multi[subidx].start_lnum = reglnum;
4636 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004637 (colnr_T)(reginput - regline + off);
4638 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004639 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004640 }
4641 else
4642 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004643 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004644 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004645 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004646 save_in_use = -1;
4647 }
4648 else
4649 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004650 save_in_use = sub->in_use;
4651 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004652 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004653 sub->list.line[i].start = NULL;
4654 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004655 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004656 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004657 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004658 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004659 }
4660
Bram Moolenaar16b35782016-09-09 20:29:50 +02004661 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004662 /* "subs" may have changed, need to set "sub" again */
4663#ifdef FEAT_SYN_HL
4664 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4665 sub = &subs->synt;
4666 else
4667#endif
4668 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004669
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004670 if (save_in_use == -1)
4671 {
4672 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004673 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004674 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004675 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004676 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004678 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004679 break;
4680
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004681 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004682 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004683 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004684 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004686 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004687 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 break;
4689 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004690 case NFA_MCLOSE1:
4691 case NFA_MCLOSE2:
4692 case NFA_MCLOSE3:
4693 case NFA_MCLOSE4:
4694 case NFA_MCLOSE5:
4695 case NFA_MCLOSE6:
4696 case NFA_MCLOSE7:
4697 case NFA_MCLOSE8:
4698 case NFA_MCLOSE9:
4699#ifdef FEAT_SYN_HL
4700 case NFA_ZCLOSE:
4701 case NFA_ZCLOSE1:
4702 case NFA_ZCLOSE2:
4703 case NFA_ZCLOSE3:
4704 case NFA_ZCLOSE4:
4705 case NFA_ZCLOSE5:
4706 case NFA_ZCLOSE6:
4707 case NFA_ZCLOSE7:
4708 case NFA_ZCLOSE8:
4709 case NFA_ZCLOSE9:
4710#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004712 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004713 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004714 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004715 sub = &subs->norm;
4716 }
4717#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004718 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004719 {
4720 subidx = state->c - NFA_ZCLOSE;
4721 sub = &subs->synt;
4722 }
4723#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004724 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004725 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004726 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004727 sub = &subs->norm;
4728 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004729
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004730 /* We don't fill in gaps here, there must have been an MOPEN that
4731 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004732 save_in_use = sub->in_use;
4733 if (sub->in_use <= subidx)
4734 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004735 if (REG_MULTI)
4736 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004737 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004738 if (off == -1)
4739 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004740 sub->list.multi[subidx].end_lnum = reglnum + 1;
4741 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004742 }
4743 else
4744 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004745 sub->list.multi[subidx].end_lnum = reglnum;
4746 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004747 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004748 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004749 /* avoid compiler warnings */
4750 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004751 }
4752 else
4753 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004754 save_ptr = sub->list.line[subidx].end;
4755 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004756 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004757 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004758 }
4759
Bram Moolenaar16b35782016-09-09 20:29:50 +02004760 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004761 /* "subs" may have changed, need to set "sub" again */
4762#ifdef FEAT_SYN_HL
4763 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4764 sub = &subs->synt;
4765 else
4766#endif
4767 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004768
4769 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004770 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004771 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004772 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004773 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004774 break;
4775 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004776 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004777}
4778
4779/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004780 * Like addstate(), but the new state(s) are put at position "*ip".
4781 * Used for zero-width matches, next state to use is the added one.
4782 * This makes sure the order of states to be tried does not change, which
4783 * matters for alternatives.
4784 */
4785 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004786addstate_here(
4787 nfa_list_T *l, /* runtime state list */
4788 nfa_state_T *state, /* state to update */
4789 regsubs_T *subs, /* pointers to subexpressions */
4790 nfa_pim_T *pim, /* postponed look-behind match */
4791 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004792{
4793 int tlen = l->n;
4794 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004795 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004796
Bram Moolenaar16b35782016-09-09 20:29:50 +02004797 /* First add the state(s) at the end, so that we know how many there are.
4798 * Pass the listidx as offset (avoids adding another argument to
4799 * addstate(). */
4800 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004801
Bram Moolenaar4b417062013-05-25 20:19:50 +02004802 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004803 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004804 return;
4805
4806 /* re-order to put the new state at the current position */
4807 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004808 if (count == 0)
4809 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004810 if (count == 1)
4811 {
4812 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004813 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004814 }
4815 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004816 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004817 if (l->n + count - 1 >= l->len)
4818 {
4819 /* not enough space to move the new states, reallocate the list
4820 * and move the states to the right position */
4821 nfa_thread_T *newl;
4822
4823 l->len = l->len * 3 / 2 + 50;
4824 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4825 if (newl == NULL)
4826 return;
4827 mch_memmove(&(newl[0]),
4828 &(l->t[0]),
4829 sizeof(nfa_thread_T) * listidx);
4830 mch_memmove(&(newl[listidx]),
4831 &(l->t[l->n - count]),
4832 sizeof(nfa_thread_T) * count);
4833 mch_memmove(&(newl[listidx + count]),
4834 &(l->t[listidx + 1]),
4835 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4836 vim_free(l->t);
4837 l->t = newl;
4838 }
4839 else
4840 {
4841 /* make space for new states, then move them from the
4842 * end to the current position */
4843 mch_memmove(&(l->t[listidx + count]),
4844 &(l->t[listidx + 1]),
4845 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4846 mch_memmove(&(l->t[listidx]),
4847 &(l->t[l->n - 1]),
4848 sizeof(nfa_thread_T) * count);
4849 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004850 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004851 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004852 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004853}
4854
4855/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004856 * Check character class "class" against current character c.
4857 */
4858 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004859check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004860{
4861 switch (class)
4862 {
4863 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004864 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004865 return OK;
4866 break;
4867 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004868 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004869 return OK;
4870 break;
4871 case NFA_CLASS_BLANK:
4872 if (c == ' ' || c == '\t')
4873 return OK;
4874 break;
4875 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004876 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004877 return OK;
4878 break;
4879 case NFA_CLASS_DIGIT:
4880 if (VIM_ISDIGIT(c))
4881 return OK;
4882 break;
4883 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004884 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004885 return OK;
4886 break;
4887 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004888 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004889 return OK;
4890 break;
4891 case NFA_CLASS_PRINT:
4892 if (vim_isprintc(c))
4893 return OK;
4894 break;
4895 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004896 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004897 return OK;
4898 break;
4899 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004900 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901 return OK;
4902 break;
4903 case NFA_CLASS_UPPER:
4904 if (MB_ISUPPER(c))
4905 return OK;
4906 break;
4907 case NFA_CLASS_XDIGIT:
4908 if (vim_isxdigit(c))
4909 return OK;
4910 break;
4911 case NFA_CLASS_TAB:
4912 if (c == '\t')
4913 return OK;
4914 break;
4915 case NFA_CLASS_RETURN:
4916 if (c == '\r')
4917 return OK;
4918 break;
4919 case NFA_CLASS_BACKSPACE:
4920 if (c == '\b')
4921 return OK;
4922 break;
4923 case NFA_CLASS_ESCAPE:
4924 if (c == '\033')
4925 return OK;
4926 break;
4927
4928 default:
4929 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004930 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004931 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004932 }
4933 return FAIL;
4934}
4935
Bram Moolenaar5714b802013-05-28 22:03:20 +02004936/*
4937 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004938 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004939 */
4940 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004941match_backref(
4942 regsub_T *sub, /* pointers to subexpressions */
4943 int subidx,
4944 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004945{
4946 int len;
4947
4948 if (sub->in_use <= subidx)
4949 {
4950retempty:
4951 /* backref was not set, match an empty string */
4952 *bytelen = 0;
4953 return TRUE;
4954 }
4955
4956 if (REG_MULTI)
4957 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004958 if (sub->list.multi[subidx].start_lnum < 0
4959 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004960 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004961 if (sub->list.multi[subidx].start_lnum == reglnum
4962 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004963 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004964 len = sub->list.multi[subidx].end_col
4965 - sub->list.multi[subidx].start_col;
4966 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004967 reginput, &len) == 0)
4968 {
4969 *bytelen = len;
4970 return TRUE;
4971 }
4972 }
4973 else
4974 {
4975 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004976 sub->list.multi[subidx].start_lnum,
4977 sub->list.multi[subidx].start_col,
4978 sub->list.multi[subidx].end_lnum,
4979 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004980 bytelen) == RA_MATCH)
4981 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004982 }
4983 }
4984 else
4985 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004986 if (sub->list.line[subidx].start == NULL
4987 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004988 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004989 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4990 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004991 {
4992 *bytelen = len;
4993 return TRUE;
4994 }
4995 }
4996 return FALSE;
4997}
4998
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004999#ifdef FEAT_SYN_HL
5000
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005001static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005002
5003/*
5004 * Check for a match with \z subexpression "subidx".
5005 * Return TRUE if it matches.
5006 */
5007 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005008match_zref(
5009 int subidx,
5010 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005011{
5012 int len;
5013
5014 cleanup_zsubexpr();
5015 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5016 {
5017 /* backref was not set, match an empty string */
5018 *bytelen = 0;
5019 return TRUE;
5020 }
5021
5022 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
5023 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5024 {
5025 *bytelen = len;
5026 return TRUE;
5027 }
5028 return FALSE;
5029}
5030#endif
5031
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005032/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005033 * Save list IDs for all NFA states of "prog" into "list".
5034 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005035 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005036 */
5037 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005038nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005039{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005040 int i;
5041 nfa_state_T *p;
5042
5043 /* Order in the list is reverse, it's a bit faster that way. */
5044 p = &prog->state[0];
5045 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005046 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005047 list[i] = p->lastlist[1];
5048 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005049 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005050 }
5051}
5052
5053/*
5054 * Restore list IDs from "list" to all NFA states.
5055 */
5056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005057nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005058{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005059 int i;
5060 nfa_state_T *p;
5061
5062 p = &prog->state[0];
5063 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005064 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005065 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005066 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005067 }
5068}
5069
Bram Moolenaar423532e2013-05-29 21:14:42 +02005070 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005071nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005072{
5073 if (op == 1) return pos > val;
5074 if (op == 2) return pos < val;
5075 return val == pos;
5076}
5077
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005078static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5079static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005080
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005081/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005082 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005083 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5084 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005085 */
5086 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005087recursive_regmatch(
5088 nfa_state_T *state,
5089 nfa_pim_T *pim,
5090 nfa_regprog_T *prog,
5091 regsubs_T *submatch,
5092 regsubs_T *m,
5093 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005094{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005095 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005096 int save_reglnum = reglnum;
5097 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005098 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005099 save_se_T *save_nfa_endp = nfa_endp;
5100 save_se_T endpos;
5101 save_se_T *endposp = NULL;
5102 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005103 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005104
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005105 if (pim != NULL)
5106 {
5107 /* start at the position where the postponed match was */
5108 if (REG_MULTI)
5109 reginput = regline + pim->end.pos.col;
5110 else
5111 reginput = pim->end.ptr;
5112 }
5113
Bram Moolenaardecd9542013-06-07 16:31:50 +02005114 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005115 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5116 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5117 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005118 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005119 /* The recursive match must end at the current position. When "pim" is
5120 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005121 endposp = &endpos;
5122 if (REG_MULTI)
5123 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005124 if (pim == NULL)
5125 {
5126 endpos.se_u.pos.col = (int)(reginput - regline);
5127 endpos.se_u.pos.lnum = reglnum;
5128 }
5129 else
5130 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005131 }
5132 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005133 {
5134 if (pim == NULL)
5135 endpos.se_u.ptr = reginput;
5136 else
5137 endpos.se_u.ptr = pim->end.ptr;
5138 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005139
5140 /* Go back the specified number of bytes, or as far as the
5141 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005142 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005143 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005144 if (state->val <= 0)
5145 {
5146 if (REG_MULTI)
5147 {
5148 regline = reg_getline(--reglnum);
5149 if (regline == NULL)
5150 /* can't go before the first line */
5151 regline = reg_getline(++reglnum);
5152 }
5153 reginput = regline;
5154 }
5155 else
5156 {
5157 if (REG_MULTI && (int)(reginput - regline) < state->val)
5158 {
5159 /* Not enough bytes in this line, go to end of
5160 * previous line. */
5161 regline = reg_getline(--reglnum);
5162 if (regline == NULL)
5163 {
5164 /* can't go before the first line */
5165 regline = reg_getline(++reglnum);
5166 reginput = regline;
5167 }
5168 else
5169 reginput = regline + STRLEN(regline);
5170 }
5171 if ((int)(reginput - regline) >= state->val)
5172 {
5173 reginput -= state->val;
5174#ifdef FEAT_MBYTE
5175 if (has_mbyte)
5176 reginput -= mb_head_off(regline, reginput);
5177#endif
5178 }
5179 else
5180 reginput = regline;
5181 }
5182 }
5183
Bram Moolenaarf46da702013-06-02 22:37:42 +02005184#ifdef ENABLE_LOG
5185 if (log_fd != stderr)
5186 fclose(log_fd);
5187 log_fd = NULL;
5188#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005189 /* Have to clear the lastlist field of the NFA nodes, so that
5190 * nfa_regmatch() and addstate() can run properly after recursion. */
5191 if (nfa_ll_index == 1)
5192 {
5193 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5194 * values and clear them. */
5195 if (*listids == NULL)
5196 {
5197 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5198 if (*listids == NULL)
5199 {
5200 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5201 return 0;
5202 }
5203 }
5204 nfa_save_listids(prog, *listids);
5205 need_restore = TRUE;
5206 /* any value of nfa_listid will do */
5207 }
5208 else
5209 {
5210 /* First recursive nfa_regmatch() call, switch to the second lastlist
5211 * entry. Make sure nfa_listid is different from a previous recursive
5212 * call, because some states may still have this ID. */
5213 ++nfa_ll_index;
5214 if (nfa_listid <= nfa_alt_listid)
5215 nfa_listid = nfa_alt_listid;
5216 }
5217
5218 /* Call nfa_regmatch() to check if the current concat matches at this
5219 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005220 nfa_endp = endposp;
5221 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005222
5223 if (need_restore)
5224 nfa_restore_listids(prog, *listids);
5225 else
5226 {
5227 --nfa_ll_index;
5228 nfa_alt_listid = nfa_listid;
5229 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005230
5231 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005232 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005233 if (REG_MULTI)
5234 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005235 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005236 if (result != NFA_TOO_EXPENSIVE)
5237 {
5238 nfa_match = save_nfa_match;
5239 nfa_listid = save_nfa_listid;
5240 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005241 nfa_endp = save_nfa_endp;
5242
5243#ifdef ENABLE_LOG
5244 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5245 if (log_fd != NULL)
5246 {
5247 fprintf(log_fd, "****************************\n");
5248 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5249 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5250 fprintf(log_fd, "****************************\n");
5251 }
5252 else
5253 {
5254 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5255 log_fd = stderr;
5256 }
5257#endif
5258
5259 return result;
5260}
5261
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005262static int skip_to_start(int c, colnr_T *colp);
5263static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005264
5265/*
5266 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005267 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005268 * NFA_ANY: 1
5269 * specific character: 99
5270 */
5271 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005272failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005273{
5274 int c = state->c;
5275 int l, r;
5276
5277 /* detect looping */
5278 if (depth > 4)
5279 return 1;
5280
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005281 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005282 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005283 case NFA_SPLIT:
5284 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5285 /* avoid recursive stuff */
5286 return 1;
5287 /* two alternatives, use the lowest failure chance */
5288 l = failure_chance(state->out, depth + 1);
5289 r = failure_chance(state->out1, depth + 1);
5290 return l < r ? l : r;
5291
5292 case NFA_ANY:
5293 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005294 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005295
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005296 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005297 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005298 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005299 /* empty match works always */
5300 return 0;
5301
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005302 case NFA_START_INVISIBLE:
5303 case NFA_START_INVISIBLE_FIRST:
5304 case NFA_START_INVISIBLE_NEG:
5305 case NFA_START_INVISIBLE_NEG_FIRST:
5306 case NFA_START_INVISIBLE_BEFORE:
5307 case NFA_START_INVISIBLE_BEFORE_FIRST:
5308 case NFA_START_INVISIBLE_BEFORE_NEG:
5309 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5310 case NFA_START_PATTERN:
5311 /* recursive regmatch is expensive, use low failure chance */
5312 return 5;
5313
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005314 case NFA_BOL:
5315 case NFA_EOL:
5316 case NFA_BOF:
5317 case NFA_EOF:
5318 case NFA_NEWL:
5319 return 99;
5320
5321 case NFA_BOW:
5322 case NFA_EOW:
5323 return 90;
5324
5325 case NFA_MOPEN:
5326 case NFA_MOPEN1:
5327 case NFA_MOPEN2:
5328 case NFA_MOPEN3:
5329 case NFA_MOPEN4:
5330 case NFA_MOPEN5:
5331 case NFA_MOPEN6:
5332 case NFA_MOPEN7:
5333 case NFA_MOPEN8:
5334 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005335#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005336 case NFA_ZOPEN:
5337 case NFA_ZOPEN1:
5338 case NFA_ZOPEN2:
5339 case NFA_ZOPEN3:
5340 case NFA_ZOPEN4:
5341 case NFA_ZOPEN5:
5342 case NFA_ZOPEN6:
5343 case NFA_ZOPEN7:
5344 case NFA_ZOPEN8:
5345 case NFA_ZOPEN9:
5346 case NFA_ZCLOSE:
5347 case NFA_ZCLOSE1:
5348 case NFA_ZCLOSE2:
5349 case NFA_ZCLOSE3:
5350 case NFA_ZCLOSE4:
5351 case NFA_ZCLOSE5:
5352 case NFA_ZCLOSE6:
5353 case NFA_ZCLOSE7:
5354 case NFA_ZCLOSE8:
5355 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005356#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005357 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005358 case NFA_MCLOSE1:
5359 case NFA_MCLOSE2:
5360 case NFA_MCLOSE3:
5361 case NFA_MCLOSE4:
5362 case NFA_MCLOSE5:
5363 case NFA_MCLOSE6:
5364 case NFA_MCLOSE7:
5365 case NFA_MCLOSE8:
5366 case NFA_MCLOSE9:
5367 case NFA_NCLOSE:
5368 return failure_chance(state->out, depth + 1);
5369
5370 case NFA_BACKREF1:
5371 case NFA_BACKREF2:
5372 case NFA_BACKREF3:
5373 case NFA_BACKREF4:
5374 case NFA_BACKREF5:
5375 case NFA_BACKREF6:
5376 case NFA_BACKREF7:
5377 case NFA_BACKREF8:
5378 case NFA_BACKREF9:
5379#ifdef FEAT_SYN_HL
5380 case NFA_ZREF1:
5381 case NFA_ZREF2:
5382 case NFA_ZREF3:
5383 case NFA_ZREF4:
5384 case NFA_ZREF5:
5385 case NFA_ZREF6:
5386 case NFA_ZREF7:
5387 case NFA_ZREF8:
5388 case NFA_ZREF9:
5389#endif
5390 /* backreferences don't match in many places */
5391 return 94;
5392
5393 case NFA_LNUM_GT:
5394 case NFA_LNUM_LT:
5395 case NFA_COL_GT:
5396 case NFA_COL_LT:
5397 case NFA_VCOL_GT:
5398 case NFA_VCOL_LT:
5399 case NFA_MARK_GT:
5400 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005401 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005402 /* before/after positions don't match very often */
5403 return 85;
5404
5405 case NFA_LNUM:
5406 return 90;
5407
5408 case NFA_CURSOR:
5409 case NFA_COL:
5410 case NFA_VCOL:
5411 case NFA_MARK:
5412 /* specific positions rarely match */
5413 return 98;
5414
5415 case NFA_COMPOSING:
5416 return 95;
5417
5418 default:
5419 if (c > 0)
5420 /* character match fails often */
5421 return 95;
5422 }
5423
5424 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005425 return 50;
5426}
5427
Bram Moolenaarf46da702013-06-02 22:37:42 +02005428/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005429 * Skip until the char "c" we know a match must start with.
5430 */
5431 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005432skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005433{
5434 char_u *s;
5435
5436 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005437 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005438#ifdef FEAT_MBYTE
5439 && !has_mbyte
5440#endif
5441 )
5442 s = vim_strbyte(regline + *colp, c);
5443 else
5444 s = cstrchr(regline + *colp, c);
5445 if (s == NULL)
5446 return FAIL;
5447 *colp = (int)(s - regline);
5448 return OK;
5449}
5450
5451/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005452 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005453 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005454 * Returns zero for no match, 1 for a match.
5455 */
5456 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005457find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005458{
5459 colnr_T col = startcol;
5460 int c1, c2;
5461 int len1, len2;
5462 int match;
5463
5464 for (;;)
5465 {
5466 match = TRUE;
5467 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5468 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5469 {
5470 c1 = PTR2CHAR(match_text + len1);
5471 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005472 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005473 {
5474 match = FALSE;
5475 break;
5476 }
5477 len2 += MB_CHAR2LEN(c2);
5478 }
5479 if (match
5480#ifdef FEAT_MBYTE
5481 /* check that no composing char follows */
5482 && !(enc_utf8
5483 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5484#endif
5485 )
5486 {
5487 cleanup_subexpr();
5488 if (REG_MULTI)
5489 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005490 rex.reg_startpos[0].lnum = reglnum;
5491 rex.reg_startpos[0].col = col;
5492 rex.reg_endpos[0].lnum = reglnum;
5493 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005494 }
5495 else
5496 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005497 rex.reg_startp[0] = regline + col;
5498 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005499 }
5500 return 1L;
5501 }
5502
5503 /* Try finding regstart after the current match. */
5504 col += MB_CHAR2LEN(regstart); /* skip regstart */
5505 if (skip_to_start(regstart, &col) == FAIL)
5506 break;
5507 }
5508 return 0L;
5509}
5510
5511/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512 * Main matching routine.
5513 *
5514 * Run NFA to determine whether it matches reginput.
5515 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005516 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005517 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005519 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005520 * Note: Caller must ensure that: start != NULL.
5521 */
5522 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005523nfa_regmatch(
5524 nfa_regprog_T *prog,
5525 nfa_state_T *start,
5526 regsubs_T *submatch,
5527 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005528{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005530 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005532 int go_to_nextline = FALSE;
5533 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005534 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005535 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005536 nfa_list_T *thislist;
5537 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005539 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005540 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005541 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005542 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005543 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005544#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005545 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005547
Bram Moolenaar41f12052013-08-25 17:01:42 +02005548 /* Some patterns may take a long time to match, especially when using
5549 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5550 fast_breakcheck();
5551 if (got_int)
5552 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005553#ifdef FEAT_RELTIME
5554 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5555 return FALSE;
5556#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005557
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005558#ifdef NFA_REGEXP_DEBUG_LOG
5559 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5560 if (debug == NULL)
5561 {
5562 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
5563 return FALSE;
5564 }
5565#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005566 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005567
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005568 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005569 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005570
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005571 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005572 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005573 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005574 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005575 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005576 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005577
5578#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005579 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005580 if (log_fd != NULL)
5581 {
5582 fprintf(log_fd, "**********************************\n");
5583 nfa_set_code(start->c);
5584 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5585 abs(start->id), code);
5586 fprintf(log_fd, "**********************************\n");
5587 }
5588 else
5589 {
5590 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5591 log_fd = stderr;
5592 }
5593#endif
5594
5595 thislist = &list[0];
5596 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005597 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598 nextlist = &list[1];
5599 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005600 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005601#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005602 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005603#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005604 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005605
5606 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5607 * it's the first MOPEN. */
5608 if (toplevel)
5609 {
5610 if (REG_MULTI)
5611 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005612 m->norm.list.multi[0].start_lnum = reglnum;
5613 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005614 }
5615 else
5616 m->norm.list.line[0].start = reginput;
5617 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005618 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005619 }
5620 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005621 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005622
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005623#define ADD_STATE_IF_MATCH(state) \
5624 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005625 add_state = state->out; \
5626 add_off = clen; \
5627 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005628
5629 /*
5630 * Run for each character.
5631 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005632 for (;;)
5633 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005634 int curc;
5635 int clen;
5636
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637#ifdef FEAT_MBYTE
5638 if (has_mbyte)
5639 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005640 curc = (*mb_ptr2char)(reginput);
5641 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005642 }
5643 else
5644#endif
5645 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005646 curc = *reginput;
5647 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005648 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005649 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005650 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005651 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005652 go_to_nextline = FALSE;
5653 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005654
5655 /* swap lists */
5656 thislist = &list[flag];
5657 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005658 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005659 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005660 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005661 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5662 {
5663 /* too many states, retry with old engine */
5664 nfa_match = NFA_TOO_EXPENSIVE;
5665 goto theend;
5666 }
5667
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005668 thislist->id = nfa_listid;
5669 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005670
5671#ifdef ENABLE_LOG
5672 fprintf(log_fd, "------------------------------------------\n");
5673 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005674 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005675 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005676 {
5677 int i;
5678
5679 for (i = 0; i < thislist->n; i++)
5680 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5681 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005682 fprintf(log_fd, "\n");
5683#endif
5684
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005685#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005686 fprintf(debug, "\n-------------------\n");
5687#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005688 /*
5689 * If the state lists are empty we can stop.
5690 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005691 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005692 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005693
5694 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005695 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005697 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005699#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005700 nfa_set_code(t->state->c);
5701 fprintf(debug, "%s, ", code);
5702#endif
5703#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005704 {
5705 int col;
5706
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005707 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005708 col = -1;
5709 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005710 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005711 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005712 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005713 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005714 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5715 abs(t->state->id), (int)t->state->c, code, col,
5716 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005717 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005718#endif
5719
5720 /*
5721 * Handle the possible codes of the current state.
5722 * The most important is NFA_MATCH.
5723 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005724 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005725 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005726 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005727 switch (t->state->c)
5728 {
5729 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005730 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005731#ifdef FEAT_MBYTE
5732 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005733 * rex.reg_icombine is not set, that is not really a match. */
5734 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005735 break;
5736#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005737 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005738 copy_sub(&submatch->norm, &t->subs.norm);
5739#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005740 if (nfa_has_zsubexpr)
5741 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005742#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005743#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005744 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005745#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005746 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005747 * states at this position. When the list of states is going
5748 * to be empty quit without advancing, so that "reginput" is
5749 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005750 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005751 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005752 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005753 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005754
5755 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005756 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005757 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005758 /*
5759 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005760 * NFA_START_INVISIBLE_BEFORE node.
5761 * They surround a zero-width group, used with "\@=", "\&",
5762 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005763 * If we got here, it means that the current "invisible" group
5764 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005765 * nfa_regmatch(). For a look-behind match only when it ends
5766 * in the position in "nfa_endp".
5767 * Submatches are stored in *m, and used in the parent call.
5768 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005769#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005770 if (nfa_endp != NULL)
5771 {
5772 if (REG_MULTI)
5773 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5774 (int)reglnum,
5775 (int)nfa_endp->se_u.pos.lnum,
5776 (int)(reginput - regline),
5777 nfa_endp->se_u.pos.col);
5778 else
5779 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5780 (int)(reginput - regline),
5781 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005782 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005783#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005784 /* If "nfa_endp" is set it's only a match if it ends at
5785 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005786 if (nfa_endp != NULL && (REG_MULTI
5787 ? (reglnum != nfa_endp->se_u.pos.lnum
5788 || (int)(reginput - regline)
5789 != nfa_endp->se_u.pos.col)
5790 : reginput != nfa_endp->se_u.ptr))
5791 break;
5792
5793 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005794 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005795 {
5796 copy_sub(&m->norm, &t->subs.norm);
5797#ifdef FEAT_SYN_HL
5798 if (nfa_has_zsubexpr)
5799 copy_sub(&m->synt, &t->subs.synt);
5800#endif
5801 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005802#ifdef ENABLE_LOG
5803 fprintf(log_fd, "Match found:\n");
5804 log_subsexpr(m);
5805#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005806 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005807 /* See comment above at "goto nextchar". */
5808 if (nextlist->n == 0)
5809 clen = 0;
5810 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005811
5812 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005813 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005814 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005815 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005816 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005817 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005818 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005819 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005820 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005821#ifdef ENABLE_LOG
5822 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5823 failure_chance(t->state->out, 0),
5824 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005825#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005826 /* Do it directly if there already is a PIM or when
5827 * nfa_postprocess() detected it will work better. */
5828 if (t->pim.result != NFA_PIM_UNUSED
5829 || t->state->c == NFA_START_INVISIBLE_FIRST
5830 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5831 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5832 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005833 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005834 int in_use = m->norm.in_use;
5835
Bram Moolenaar699c1202013-09-25 16:41:54 +02005836 /* Copy submatch info for the recursive call, opposite
5837 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005838 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005839#ifdef FEAT_SYN_HL
5840 if (nfa_has_zsubexpr)
5841 copy_sub_off(&m->synt, &t->subs.synt);
5842#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005843
Bram Moolenaara2d95102013-06-04 14:23:05 +02005844 /*
5845 * First try matching the invisible match, then what
5846 * follows.
5847 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005848 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005849 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005850 if (result == NFA_TOO_EXPENSIVE)
5851 {
5852 nfa_match = result;
5853 goto theend;
5854 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005855
Bram Moolenaardecd9542013-06-07 16:31:50 +02005856 /* for \@! and \@<! it is a match when the result is
5857 * FALSE */
5858 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005859 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5860 || t->state->c
5861 == NFA_START_INVISIBLE_BEFORE_NEG
5862 || t->state->c
5863 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005864 {
5865 /* Copy submatch info from the recursive call */
5866 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005867#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005868 if (nfa_has_zsubexpr)
5869 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005870#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005871 /* If the pattern has \ze and it matched in the
5872 * sub pattern, use it. */
5873 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005874
Bram Moolenaara2d95102013-06-04 14:23:05 +02005875 /* t->state->out1 is the corresponding
5876 * END_INVISIBLE node; Add its out to the current
5877 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005878 add_here = TRUE;
5879 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005880 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005881 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005882 }
5883 else
5884 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005885 nfa_pim_T pim;
5886
Bram Moolenaara2d95102013-06-04 14:23:05 +02005887 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005888 * First try matching what follows. Only if a match
5889 * is found verify the invisible match matches. Add a
5890 * nfa_pim_T to the following states, it contains info
5891 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005892 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005893 pim.state = t->state;
5894 pim.result = NFA_PIM_TODO;
5895 pim.subs.norm.in_use = 0;
5896#ifdef FEAT_SYN_HL
5897 pim.subs.synt.in_use = 0;
5898#endif
5899 if (REG_MULTI)
5900 {
5901 pim.end.pos.col = (int)(reginput - regline);
5902 pim.end.pos.lnum = reglnum;
5903 }
5904 else
5905 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005906
5907 /* t->state->out1 is the corresponding END_INVISIBLE
5908 * node; Add its out to the current list (zero-width
5909 * match). */
5910 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005911 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005912 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005913 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005914 break;
5915
Bram Moolenaar87953742013-06-05 18:52:40 +02005916 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005917 {
5918 nfa_state_T *skip = NULL;
5919#ifdef ENABLE_LOG
5920 int skip_lid = 0;
5921#endif
5922
5923 /* There is no point in trying to match the pattern if the
5924 * output state is not going to be added to the list. */
5925 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5926 {
5927 skip = t->state->out1->out;
5928#ifdef ENABLE_LOG
5929 skip_lid = nextlist->id;
5930#endif
5931 }
5932 else if (state_in_list(nextlist,
5933 t->state->out1->out->out, &t->subs))
5934 {
5935 skip = t->state->out1->out->out;
5936#ifdef ENABLE_LOG
5937 skip_lid = nextlist->id;
5938#endif
5939 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005940 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005941 t->state->out1->out->out, &t->subs))
5942 {
5943 skip = t->state->out1->out->out;
5944#ifdef ENABLE_LOG
5945 skip_lid = thislist->id;
5946#endif
5947 }
5948 if (skip != NULL)
5949 {
5950#ifdef ENABLE_LOG
5951 nfa_set_code(skip->c);
5952 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5953 abs(skip->id), skip_lid, skip->c, code);
5954#endif
5955 break;
5956 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005957 /* Copy submatch info to the recursive call, opposite of what
5958 * happens afterwards. */
5959 copy_sub_off(&m->norm, &t->subs.norm);
5960#ifdef FEAT_SYN_HL
5961 if (nfa_has_zsubexpr)
5962 copy_sub_off(&m->synt, &t->subs.synt);
5963#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005964
Bram Moolenaar87953742013-06-05 18:52:40 +02005965 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005966 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005967 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005968 if (result == NFA_TOO_EXPENSIVE)
5969 {
5970 nfa_match = result;
5971 goto theend;
5972 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005973 if (result)
5974 {
5975 int bytelen;
5976
5977#ifdef ENABLE_LOG
5978 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5979 log_subsexpr(m);
5980#endif
5981 /* Copy submatch info from the recursive call */
5982 copy_sub_off(&t->subs.norm, &m->norm);
5983#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005984 if (nfa_has_zsubexpr)
5985 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005986#endif
5987 /* Now we need to skip over the matched text and then
5988 * continue with what follows. */
5989 if (REG_MULTI)
5990 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005991 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005992 - (int)(reginput - regline);
5993 else
5994 bytelen = (int)(m->norm.list.line[0].end - reginput);
5995
5996#ifdef ENABLE_LOG
5997 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5998#endif
5999 if (bytelen == 0)
6000 {
6001 /* empty match, output of corresponding
6002 * NFA_END_PATTERN/NFA_SKIP to be used at current
6003 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006004 add_here = TRUE;
6005 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006006 }
6007 else if (bytelen <= clen)
6008 {
6009 /* match current character, output of corresponding
6010 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006011 add_state = t->state->out1->out->out;
6012 add_off = clen;
6013 }
6014 else
6015 {
6016 /* skip over the matched characters, set character
6017 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006018 add_state = t->state->out1->out;
6019 add_off = bytelen;
6020 add_count = bytelen - clen;
6021 }
6022 }
6023 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006024 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006025
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006026 case NFA_BOL:
6027 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006028 {
6029 add_here = TRUE;
6030 add_state = t->state->out;
6031 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006032 break;
6033
6034 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006035 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006036 {
6037 add_here = TRUE;
6038 add_state = t->state->out;
6039 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006040 break;
6041
6042 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006043 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006044
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006045 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006046 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006047#ifdef FEAT_MBYTE
6048 else if (has_mbyte)
6049 {
6050 int this_class;
6051
6052 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006053 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006055 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006056 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006057 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006058 }
6059#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006060 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006061 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006062 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006063 result = FALSE;
6064 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006065 {
6066 add_here = TRUE;
6067 add_state = t->state->out;
6068 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006069 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006070
6071 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006072 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006073 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006074 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006075#ifdef FEAT_MBYTE
6076 else if (has_mbyte)
6077 {
6078 int this_class, prev_class;
6079
6080 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006081 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006082 prev_class = reg_prev_class();
6083 if (this_class == prev_class
6084 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006085 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006086 }
6087#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006088 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006089 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006090 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006091 result = FALSE;
6092 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006093 {
6094 add_here = TRUE;
6095 add_state = t->state->out;
6096 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006097 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006098
Bram Moolenaar4b780632013-05-31 22:14:52 +02006099 case NFA_BOF:
6100 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006101 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006102 {
6103 add_here = TRUE;
6104 add_state = t->state->out;
6105 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006106 break;
6107
6108 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006109 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006110 {
6111 add_here = TRUE;
6112 add_state = t->state->out;
6113 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006114 break;
6115
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006116#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006118 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006119 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006120 int len = 0;
6121 nfa_state_T *end;
6122 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006123 int cchars[MAX_MCO];
6124 int ccount = 0;
6125 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006126
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006127 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006128 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006129 if (utf_iscomposing(sta->c))
6130 {
6131 /* Only match composing character(s), ignore base
6132 * character. Used for ".{composing}" and "{composing}"
6133 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006134 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006135 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006136 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006137 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006138 /* If \Z was present, then ignore composing characters.
6139 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006140 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006141 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006142 else
6143 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006144 while (sta->c != NFA_END_COMPOSING)
6145 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006146 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006147
6148 /* Check base character matches first, unless ignored. */
6149 else if (len > 0 || mc == sta->c)
6150 {
6151 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006152 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006153 len += mb_char2len(mc);
6154 sta = sta->out;
6155 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006156
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006157 /* We don't care about the order of composing characters.
6158 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006159 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006160 {
6161 mc = mb_ptr2char(reginput + len);
6162 cchars[ccount++] = mc;
6163 len += mb_char2len(mc);
6164 if (ccount == MAX_MCO)
6165 break;
6166 }
6167
6168 /* Check that each composing char in the pattern matches a
6169 * composing char in the text. We do not check if all
6170 * composing chars are matched. */
6171 result = OK;
6172 while (sta->c != NFA_END_COMPOSING)
6173 {
6174 for (j = 0; j < ccount; ++j)
6175 if (cchars[j] == sta->c)
6176 break;
6177 if (j == ccount)
6178 {
6179 result = FAIL;
6180 break;
6181 }
6182 sta = sta->out;
6183 }
6184 }
6185 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006186 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006187
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006188 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006189 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006190 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006191 }
6192#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006193
6194 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006195 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6196 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006197 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006198 go_to_nextline = TRUE;
6199 /* Pass -1 for the offset, which means taking the position
6200 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006201 add_state = t->state->out;
6202 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006203 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006204 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006205 {
6206 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006207 add_state = t->state->out;
6208 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006209 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006210 break;
6211
Bram Moolenaar417bad22013-06-07 14:08:30 +02006212 case NFA_START_COLL:
6213 case NFA_START_NEG_COLL:
6214 {
6215 /* What follows is a list of characters, until NFA_END_COLL.
6216 * One of them must match or none of them must match. */
6217 nfa_state_T *state;
6218 int result_if_matched;
6219 int c1, c2;
6220
6221 /* Never match EOL. If it's part of the collection it is added
6222 * as a separate state with an OR. */
6223 if (curc == NUL)
6224 break;
6225
6226 state = t->state->out;
6227 result_if_matched = (t->state->c == NFA_START_COLL);
6228 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006229 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006230 if (state->c == NFA_END_COLL)
6231 {
6232 result = !result_if_matched;
6233 break;
6234 }
6235 if (state->c == NFA_RANGE_MIN)
6236 {
6237 c1 = state->val;
6238 state = state->out; /* advance to NFA_RANGE_MAX */
6239 c2 = state->val;
6240#ifdef ENABLE_LOG
6241 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6242 curc, c1, c2);
6243#endif
6244 if (curc >= c1 && curc <= c2)
6245 {
6246 result = result_if_matched;
6247 break;
6248 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006249 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006250 {
6251 int curc_low = MB_TOLOWER(curc);
6252 int done = FALSE;
6253
6254 for ( ; c1 <= c2; ++c1)
6255 if (MB_TOLOWER(c1) == curc_low)
6256 {
6257 result = result_if_matched;
6258 done = TRUE;
6259 break;
6260 }
6261 if (done)
6262 break;
6263 }
6264 }
6265 else if (state->c < 0 ? check_char_class(state->c, curc)
6266 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006267 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006268 == MB_TOLOWER(state->c))))
6269 {
6270 result = result_if_matched;
6271 break;
6272 }
6273 state = state->out;
6274 }
6275 if (result)
6276 {
6277 /* next state is in out of the NFA_END_COLL, out1 of
6278 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006279 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006280 add_off = clen;
6281 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006282 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006283 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006284
6285 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006286 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006287 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006288 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006289 add_state = t->state->out;
6290 add_off = clen;
6291 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006292 break;
6293
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006294 case NFA_ANY_COMPOSING:
6295 /* On a composing character skip over it. Otherwise do
6296 * nothing. Always matches. */
6297#ifdef FEAT_MBYTE
6298 if (enc_utf8 && utf_iscomposing(curc))
6299 {
6300 add_off = clen;
6301 }
6302 else
6303#endif
6304 {
6305 add_here = TRUE;
6306 add_off = 0;
6307 }
6308 add_state = t->state->out;
6309 break;
6310
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006311 /*
6312 * Character classes like \a for alpha, \d for digit etc.
6313 */
6314 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006315 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006316 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006317 break;
6318
6319 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006320 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006321 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006322 break;
6323
6324 case NFA_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006325 result = vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006326 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006327 break;
6328
6329 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006330 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006331 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006332 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006333 break;
6334
6335 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006336 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006337 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006338 break;
6339
6340 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006341 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006342 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006343 break;
6344
6345 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006346 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006347 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006348 break;
6349
6350 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006351 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006352 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006353 break;
6354
6355 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006356 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006357 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006358 break;
6359
6360 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006361 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006362 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006363 break;
6364
6365 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006366 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006367 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006368 break;
6369
6370 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006371 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006372 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006373 break;
6374
6375 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006376 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006377 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006378 break;
6379
6380 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006381 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006382 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006383 break;
6384
6385 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006386 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006387 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006388 break;
6389
6390 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006391 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006392 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006393 break;
6394
6395 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006396 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006397 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006398 break;
6399
6400 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006401 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006402 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006403 break;
6404
6405 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006406 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006407 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006408 break;
6409
6410 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006411 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006412 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006413 break;
6414
6415 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006416 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006417 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006418 break;
6419
6420 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006421 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006422 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006423 break;
6424
6425 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006426 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006427 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006428 break;
6429
6430 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006431 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006432 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006433 break;
6434
6435 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006436 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006437 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006438 break;
6439
6440 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006441 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006442 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006443 break;
6444
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006445 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006446 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006447 ADD_STATE_IF_MATCH(t->state);
6448 break;
6449
6450 case NFA_NLOWER_IC: /* [^a-z] */
6451 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006452 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006453 ADD_STATE_IF_MATCH(t->state);
6454 break;
6455
6456 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006457 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006458 ADD_STATE_IF_MATCH(t->state);
6459 break;
6460
6461 case NFA_NUPPER_IC: /* ^[A-Z] */
6462 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006463 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006464 ADD_STATE_IF_MATCH(t->state);
6465 break;
6466
Bram Moolenaar5714b802013-05-28 22:03:20 +02006467 case NFA_BACKREF1:
6468 case NFA_BACKREF2:
6469 case NFA_BACKREF3:
6470 case NFA_BACKREF4:
6471 case NFA_BACKREF5:
6472 case NFA_BACKREF6:
6473 case NFA_BACKREF7:
6474 case NFA_BACKREF8:
6475 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006476#ifdef FEAT_SYN_HL
6477 case NFA_ZREF1:
6478 case NFA_ZREF2:
6479 case NFA_ZREF3:
6480 case NFA_ZREF4:
6481 case NFA_ZREF5:
6482 case NFA_ZREF6:
6483 case NFA_ZREF7:
6484 case NFA_ZREF8:
6485 case NFA_ZREF9:
6486#endif
6487 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006488 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006489 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006490 int bytelen;
6491
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006492 if (t->state->c <= NFA_BACKREF9)
6493 {
6494 subidx = t->state->c - NFA_BACKREF1 + 1;
6495 result = match_backref(&t->subs.norm, subidx, &bytelen);
6496 }
6497#ifdef FEAT_SYN_HL
6498 else
6499 {
6500 subidx = t->state->c - NFA_ZREF1 + 1;
6501 result = match_zref(subidx, &bytelen);
6502 }
6503#endif
6504
Bram Moolenaar5714b802013-05-28 22:03:20 +02006505 if (result)
6506 {
6507 if (bytelen == 0)
6508 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006509 /* empty match always works, output of NFA_SKIP to be
6510 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006511 add_here = TRUE;
6512 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006513 }
6514 else if (bytelen <= clen)
6515 {
6516 /* match current character, jump ahead to out of
6517 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006518 add_state = t->state->out->out;
6519 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006520 }
6521 else
6522 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006523 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006524 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006525 add_state = t->state->out;
6526 add_off = bytelen;
6527 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006528 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006529 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006530 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006531 }
6532 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006533 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006534 if (t->count - clen <= 0)
6535 {
6536 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006537 add_state = t->state->out;
6538 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006539 }
6540 else
6541 {
6542 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006543 add_state = t->state;
6544 add_off = 0;
6545 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006546 }
6547 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006548
Bram Moolenaar423532e2013-05-29 21:14:42 +02006549 case NFA_LNUM:
6550 case NFA_LNUM_GT:
6551 case NFA_LNUM_LT:
6552 result = (REG_MULTI &&
6553 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006554 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006555 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006556 {
6557 add_here = TRUE;
6558 add_state = t->state->out;
6559 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006560 break;
6561
6562 case NFA_COL:
6563 case NFA_COL_GT:
6564 case NFA_COL_LT:
6565 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6566 (long_u)(reginput - regline) + 1);
6567 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006568 {
6569 add_here = TRUE;
6570 add_state = t->state->out;
6571 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006572 break;
6573
6574 case NFA_VCOL:
6575 case NFA_VCOL_GT:
6576 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006577 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006578 int op = t->state->c - NFA_VCOL;
6579 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006580 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006581
6582 /* Bail out quickly when there can't be a match, avoid the
6583 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006584 if (op != 1 && col > t->state->val
6585#ifdef FEAT_MBYTE
6586 * (has_mbyte ? MB_MAXBYTES : 1)
6587#endif
6588 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006589 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006590 result = FALSE;
6591 if (op == 1 && col - 1 > t->state->val && col > 100)
6592 {
6593 int ts = wp->w_buffer->b_p_ts;
6594
6595 /* Guess that a character won't use more columns than
6596 * 'tabstop', with a minimum of 4. */
6597 if (ts < 4)
6598 ts = 4;
6599 result = col > t->state->val * ts;
6600 }
6601 if (!result)
6602 result = nfa_re_num_cmp(t->state->val, op,
6603 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006604 if (result)
6605 {
6606 add_here = TRUE;
6607 add_state = t->state->out;
6608 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006609 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006610 break;
6611
Bram Moolenaar044aa292013-06-04 21:27:38 +02006612 case NFA_MARK:
6613 case NFA_MARK_GT:
6614 case NFA_MARK_LT:
6615 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006616 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006617
6618 /* Compare the mark position to the match position. */
6619 result = (pos != NULL /* mark doesn't exist */
6620 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006621 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006622 ? (pos->col == (colnr_T)(reginput - regline)
6623 ? t->state->c == NFA_MARK
6624 : (pos->col < (colnr_T)(reginput - regline)
6625 ? t->state->c == NFA_MARK_GT
6626 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006627 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006628 ? t->state->c == NFA_MARK_GT
6629 : t->state->c == NFA_MARK_LT)));
6630 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006631 {
6632 add_here = TRUE;
6633 add_state = t->state->out;
6634 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006635 break;
6636 }
6637
Bram Moolenaar423532e2013-05-29 21:14:42 +02006638 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006639 result = (rex.reg_win != NULL
6640 && (reglnum + rex.reg_firstlnum
6641 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006642 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006643 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006644 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006645 {
6646 add_here = TRUE;
6647 add_state = t->state->out;
6648 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006649 break;
6650
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006651 case NFA_VISUAL:
6652 result = reg_match_visual();
6653 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006654 {
6655 add_here = TRUE;
6656 add_state = t->state->out;
6657 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006658 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006659
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006660 case NFA_MOPEN1:
6661 case NFA_MOPEN2:
6662 case NFA_MOPEN3:
6663 case NFA_MOPEN4:
6664 case NFA_MOPEN5:
6665 case NFA_MOPEN6:
6666 case NFA_MOPEN7:
6667 case NFA_MOPEN8:
6668 case NFA_MOPEN9:
6669#ifdef FEAT_SYN_HL
6670 case NFA_ZOPEN:
6671 case NFA_ZOPEN1:
6672 case NFA_ZOPEN2:
6673 case NFA_ZOPEN3:
6674 case NFA_ZOPEN4:
6675 case NFA_ZOPEN5:
6676 case NFA_ZOPEN6:
6677 case NFA_ZOPEN7:
6678 case NFA_ZOPEN8:
6679 case NFA_ZOPEN9:
6680#endif
6681 case NFA_NOPEN:
6682 case NFA_ZSTART:
6683 /* These states are only added to be able to bail out when
6684 * they are added again, nothing is to be done. */
6685 break;
6686
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006687 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006688 {
6689 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006690
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006691#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006692 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006693 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006694#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006695 result = (c == curc);
6696
Bram Moolenaar6100d022016-10-02 16:51:57 +02006697 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006698 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006699#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006700 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006701 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006702 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006703 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006704#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006705 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006706 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006707 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006708
6709 } /* switch (t->state->c) */
6710
6711 if (add_state != NULL)
6712 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006713 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006714 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006715
6716 if (t->pim.result == NFA_PIM_UNUSED)
6717 pim = NULL;
6718 else
6719 pim = &t->pim;
6720
6721 /* Handle the postponed invisible match if the match might end
6722 * without advancing and before the end of the line. */
6723 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006724 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006725 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006726 {
6727#ifdef ENABLE_LOG
6728 fprintf(log_fd, "\n");
6729 fprintf(log_fd, "==================================\n");
6730 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6731 fprintf(log_fd, "\n");
6732#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006733 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006734 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006735 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006736 /* for \@! and \@<! it is a match when the result is
6737 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006738 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006739 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6740 || pim->state->c
6741 == NFA_START_INVISIBLE_BEFORE_NEG
6742 || pim->state->c
6743 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006744 {
6745 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006746 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006747#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006748 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006749 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006750#endif
6751 }
6752 }
6753 else
6754 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006755 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006756#ifdef ENABLE_LOG
6757 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006758 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006759 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6760 fprintf(log_fd, "\n");
6761#endif
6762 }
6763
Bram Moolenaardecd9542013-06-07 16:31:50 +02006764 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006765 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006766 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6767 || pim->state->c
6768 == NFA_START_INVISIBLE_BEFORE_NEG
6769 || pim->state->c
6770 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006771 {
6772 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006773 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006774#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006775 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006776 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006777#endif
6778 }
6779 else
6780 /* look-behind match failed, don't add the state */
6781 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006782
6783 /* Postponed invisible match was handled, don't add it to
6784 * following states. */
6785 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006786 }
6787
Bram Moolenaara951e352013-10-06 15:46:11 +02006788 /* If "pim" points into l->t it will become invalid when
6789 * adding the state causes the list to be reallocated. Make a
6790 * local copy to avoid that. */
6791 if (pim == &t->pim)
6792 {
6793 copy_pim(&pim_copy, pim);
6794 pim = &pim_copy;
6795 }
6796
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006797 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006798 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006799 else
6800 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006801 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006802 if (add_count > 0)
6803 nextlist->t[nextlist->n - 1].count = add_count;
6804 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006805 }
6806
6807 } /* for (thislist = thislist; thislist->state; thislist++) */
6808
Bram Moolenaare23febd2013-05-26 18:40:14 +02006809 /* Look for the start of a match in the current position by adding the
6810 * start state to the list of states.
6811 * The first found match is the leftmost one, thus the order of states
6812 * matters!
6813 * Do not add the start state in recursive calls of nfa_regmatch(),
6814 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006815 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006816 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006817 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006818 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006819 && reglnum == 0
6820 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006821 && (rex.reg_maxcol == 0
6822 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006823 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006824 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006825 ? (reglnum < nfa_endp->se_u.pos.lnum
6826 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006827 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006828 < nfa_endp->se_u.pos.col))
6829 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006830 {
6831#ifdef ENABLE_LOG
6832 fprintf(log_fd, "(---) STARTSTATE\n");
6833#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006834 /* Inline optimized code for addstate() if we know the state is
6835 * the first MOPEN. */
6836 if (toplevel)
6837 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006838 int add = TRUE;
6839 int c;
6840
6841 if (prog->regstart != NUL && clen != 0)
6842 {
6843 if (nextlist->n == 0)
6844 {
6845 colnr_T col = (colnr_T)(reginput - regline) + clen;
6846
6847 /* Nextlist is empty, we can skip ahead to the
6848 * character that must appear at the start. */
6849 if (skip_to_start(prog->regstart, &col) == FAIL)
6850 break;
6851#ifdef ENABLE_LOG
6852 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6853 col - ((colnr_T)(reginput - regline) + clen));
6854#endif
6855 reginput = regline + col - clen;
6856 }
6857 else
6858 {
6859 /* Checking if the required start character matches is
6860 * cheaper than adding a state that won't match. */
6861 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006862 if (c != prog->regstart && (!rex.reg_ic
6863 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006864 {
6865#ifdef ENABLE_LOG
6866 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6867#endif
6868 add = FALSE;
6869 }
6870 }
6871 }
6872
6873 if (add)
6874 {
6875 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006876 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006877 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006878 else
6879 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006880 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006881 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006882 }
6883 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006884 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006885 }
6886
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006887#ifdef ENABLE_LOG
6888 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006889 {
6890 int i;
6891
6892 for (i = 0; i < thislist->n; i++)
6893 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6894 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006895 fprintf(log_fd, "\n");
6896#endif
6897
6898nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006899 /* Advance to the next character, or advance to the next line, or
6900 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006901 if (clen != 0)
6902 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006903 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6904 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006905 reg_nextline();
6906 else
6907 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006908
6909 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006910 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006911 if (got_int)
6912 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006913#ifdef FEAT_RELTIME
6914 /* Check for timeout once in a twenty times to avoid overhead. */
6915 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6916 {
6917 nfa_time_count = 0;
6918 if (profile_passed_limit(nfa_time_limit))
6919 break;
6920 }
6921#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006922 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006923
6924#ifdef ENABLE_LOG
6925 if (log_fd != stderr)
6926 fclose(log_fd);
6927 log_fd = NULL;
6928#endif
6929
6930theend:
6931 /* Free memory */
6932 vim_free(list[0].t);
6933 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006934 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006935#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006936#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006937 fclose(debug);
6938#endif
6939
Bram Moolenaar963fee22013-05-26 21:47:28 +02006940 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006941}
6942
6943/*
6944 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006945 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006946 */
6947 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006948nfa_regtry(
6949 nfa_regprog_T *prog,
6950 colnr_T col,
6951 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006952{
6953 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006954 regsubs_T subs, m;
6955 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006956 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006957#ifdef ENABLE_LOG
6958 FILE *f;
6959#endif
6960
6961 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006962#ifdef FEAT_RELTIME
6963 nfa_time_limit = tm;
6964 nfa_time_count = 0;
6965#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006966
6967#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006968 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006969 if (f != NULL)
6970 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006971 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972#ifdef DEBUG
6973 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6974#endif
6975 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006976 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006977 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006978 fprintf(f, "\n\n");
6979 fclose(f);
6980 }
6981 else
6982 EMSG(_("Could not open temporary log file for writing "));
6983#endif
6984
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006985 clear_sub(&subs.norm);
6986 clear_sub(&m.norm);
6987#ifdef FEAT_SYN_HL
6988 clear_sub(&subs.synt);
6989 clear_sub(&m.synt);
6990#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006991
Bram Moolenaarfda37292014-11-05 14:27:36 +01006992 result = nfa_regmatch(prog, start, &subs, &m);
6993 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006994 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006995 else if (result == NFA_TOO_EXPENSIVE)
6996 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997
6998 cleanup_subexpr();
6999 if (REG_MULTI)
7000 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007001 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007002 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007003 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7004 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007005
Bram Moolenaar6100d022016-10-02 16:51:57 +02007006 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7007 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007008 }
7009
Bram Moolenaar6100d022016-10-02 16:51:57 +02007010 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007011 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007012 rex.reg_startpos[0].lnum = 0;
7013 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007015 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007016 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007017 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007018 rex.reg_endpos[0].lnum = reglnum;
7019 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007020 }
7021 else
7022 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007023 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007024 }
7025 else
7026 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007027 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007029 rex.reg_startp[i] = subs.norm.list.line[i].start;
7030 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007031 }
7032
Bram Moolenaar6100d022016-10-02 16:51:57 +02007033 if (rex.reg_startp[0] == NULL)
7034 rex.reg_startp[0] = regline + col;
7035 if (rex.reg_endp[0] == NULL)
7036 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007037 }
7038
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007039#ifdef FEAT_SYN_HL
7040 /* Package any found \z(...\) matches for export. Default is none. */
7041 unref_extmatch(re_extmatch_out);
7042 re_extmatch_out = NULL;
7043
7044 if (prog->reghasz == REX_SET)
7045 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007046 cleanup_zsubexpr();
7047 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007048 /* Loop over \z1, \z2, etc. There is no \z0. */
7049 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007050 {
7051 if (REG_MULTI)
7052 {
7053 struct multipos *mpos = &subs.synt.list.multi[i];
7054
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007055 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007056 if (mpos->start_lnum >= 0
7057 && mpos->start_lnum == mpos->end_lnum
7058 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007059 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007060 vim_strnsave(reg_getline(mpos->start_lnum)
7061 + mpos->start_col,
7062 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007063 }
7064 else
7065 {
7066 struct linepos *lpos = &subs.synt.list.line[i];
7067
7068 if (lpos->start != NULL && lpos->end != NULL)
7069 re_extmatch_out->matches[i] =
7070 vim_strnsave(lpos->start,
7071 (int)(lpos->end - lpos->start));
7072 }
7073 }
7074 }
7075#endif
7076
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007077 return 1 + reglnum;
7078}
7079
7080/*
7081 * Match a regexp against a string ("line" points to the string) or multiple
7082 * lines ("line" is NULL, use reg_getline()).
7083 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007084 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007085 */
7086 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007087nfa_regexec_both(
7088 char_u *line,
7089 colnr_T startcol, /* column to start looking for match */
7090 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007091{
7092 nfa_regprog_T *prog;
7093 long retval = 0L;
7094 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007095 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007096
7097 if (REG_MULTI)
7098 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007099 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007100 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007101 rex.reg_startpos = rex.reg_mmatch->startpos;
7102 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007103 }
7104 else
7105 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007106 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7107 rex.reg_startp = rex.reg_match->startp;
7108 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007109 }
7110
7111 /* Be paranoid... */
7112 if (prog == NULL || line == NULL)
7113 {
7114 EMSG(_(e_null));
7115 goto theend;
7116 }
7117
Bram Moolenaar6100d022016-10-02 16:51:57 +02007118 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007119 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007120 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007121 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007122 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007123
7124#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007125 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007126 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007127 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007128#endif
7129
7130 regline = line;
7131 reglnum = 0; /* relative to line */
7132
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007133 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007134 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007135 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007136 nfa_listid = 1;
7137 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007138 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007139
Bram Moolenaard89616e2013-06-06 18:46:06 +02007140 if (prog->reganch && col > 0)
7141 return 0L;
7142
Bram Moolenaar473de612013-06-08 18:19:48 +02007143 need_clear_subexpr = TRUE;
7144#ifdef FEAT_SYN_HL
7145 /* Clear the external match subpointers if necessary. */
7146 if (prog->reghasz == REX_SET)
7147 {
7148 nfa_has_zsubexpr = TRUE;
7149 need_clear_zsubexpr = TRUE;
7150 }
7151 else
7152 nfa_has_zsubexpr = FALSE;
7153#endif
7154
Bram Moolenaard89616e2013-06-06 18:46:06 +02007155 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007156 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007157 /* Skip ahead until a character we know the match must start with.
7158 * When there is none there is no match. */
7159 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007160 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007161
Bram Moolenaar473de612013-06-08 18:19:48 +02007162 /* If match_text is set it contains the full text that must match.
7163 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007164 if (prog->match_text != NULL
7165#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007166 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007167#endif
7168 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007169 return find_match_text(col, prog->regstart, prog->match_text);
7170 }
7171
Bram Moolenaard89616e2013-06-06 18:46:06 +02007172 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007173 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007174 goto theend;
7175
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007176 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007177 for (i = 0; i < nstate; ++i)
7178 {
7179 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007180 prog->state[i].lastlist[0] = 0;
7181 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007182 }
7183
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007184 retval = nfa_regtry(prog, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007185
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007186 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007187
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007188theend:
7189 return retval;
7190}
7191
7192/*
7193 * Compile a regular expression into internal code for the NFA matcher.
7194 * Returns the program in allocated space. Returns NULL for an error.
7195 */
7196 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007197nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007198{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007199 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007200 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007201 int *postfix;
7202
7203 if (expr == NULL)
7204 return NULL;
7205
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007206 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007207 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208
7209 init_class_tab();
7210
7211 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7212 return NULL;
7213
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007214 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007215 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007216 postfix = re2post();
7217 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007218 {
7219 /* TODO: only give this error for debugging? */
7220 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007221 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007222 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007223 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007224
7225 /*
7226 * In order to build the NFA, we parse the input regexp twice:
7227 * 1. first pass to count size (so we can allocate space)
7228 * 2. second to emit code
7229 */
7230#ifdef ENABLE_LOG
7231 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007232 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007233
7234 if (f != NULL)
7235 {
7236 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7237 fclose(f);
7238 }
7239 }
7240#endif
7241
7242 /*
7243 * PASS 1
7244 * Count number of NFA states in "nstate". Do not build the NFA.
7245 */
7246 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007247
Bram Moolenaar16619a22013-06-11 18:42:36 +02007248 /* allocate the regprog with space for the compiled regexp */
7249 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007250 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7251 if (prog == NULL)
7252 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007253 state_ptr = prog->state;
7254
7255 /*
7256 * PASS 2
7257 * Build the NFA
7258 */
7259 prog->start = post2nfa(postfix, post_ptr, FALSE);
7260 if (prog->start == NULL)
7261 goto fail;
7262
7263 prog->regflags = regflags;
7264 prog->engine = &nfa_regengine;
7265 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007266 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007267 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007268 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007269
Bram Moolenaara2947e22013-06-11 22:44:09 +02007270 nfa_postprocess(prog);
7271
Bram Moolenaard89616e2013-06-06 18:46:06 +02007272 prog->reganch = nfa_get_reganch(prog->start, 0);
7273 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007274 prog->match_text = nfa_get_match_text(prog->start);
7275
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007276#ifdef ENABLE_LOG
7277 nfa_postfix_dump(expr, OK);
7278 nfa_dump(prog);
7279#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007280#ifdef FEAT_SYN_HL
7281 /* Remember whether this pattern has any \z specials in it. */
7282 prog->reghasz = re_has_z;
7283#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007284 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007285 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007286
7287out:
7288 vim_free(post_start);
7289 post_start = post_ptr = post_end = NULL;
7290 state_ptr = NULL;
7291 return (regprog_T *)prog;
7292
7293fail:
7294 vim_free(prog);
7295 prog = NULL;
7296#ifdef ENABLE_LOG
7297 nfa_postfix_dump(expr, FAIL);
7298#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007299 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007300 goto out;
7301}
7302
Bram Moolenaar473de612013-06-08 18:19:48 +02007303/*
7304 * Free a compiled regexp program, returned by nfa_regcomp().
7305 */
7306 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007307nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007308{
7309 if (prog != NULL)
7310 {
7311 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007312 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007313 vim_free(prog);
7314 }
7315}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007316
7317/*
7318 * Match a regexp against a string.
7319 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7320 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007321 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007322 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007323 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007324 */
7325 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007326nfa_regexec_nl(
7327 regmatch_T *rmp,
7328 char_u *line, /* string to match against */
7329 colnr_T col, /* column to start looking for match */
7330 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007331{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007332 rex.reg_match = rmp;
7333 rex.reg_mmatch = NULL;
7334 rex.reg_maxline = 0;
7335 rex.reg_line_lbr = line_lbr;
7336 rex.reg_buf = curbuf;
7337 rex.reg_win = NULL;
7338 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007339#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007340 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007341#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007342 rex.reg_maxcol = 0;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007343 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007344}
7345
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007346
7347/*
7348 * Match a regexp against multiple lines.
7349 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7350 * Uses curbuf for line count and 'iskeyword'.
7351 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007352 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007353 * match otherwise.
7354 *
7355 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7356 *
7357 * ! Also NOTE : match may actually be in another line. e.g.:
7358 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7359 *
7360 * +-------------------------+
7361 * |a |
7362 * |b |
7363 * |c |
7364 * | |
7365 * +-------------------------+
7366 *
7367 * then nfa_regexec_multi() returns 3. while the original
7368 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7369 *
7370 * FIXME if this behavior is not compatible.
7371 */
7372 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007373nfa_regexec_multi(
7374 regmmatch_T *rmp,
7375 win_T *win, /* window in which to search or NULL */
7376 buf_T *buf, /* buffer in which to search */
7377 linenr_T lnum, /* nr of line to start looking for match */
7378 colnr_T col, /* column to start looking for match */
7379 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007380{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007381 rex.reg_match = NULL;
7382 rex.reg_mmatch = rmp;
7383 rex.reg_buf = buf;
7384 rex.reg_win = win;
7385 rex.reg_firstlnum = lnum;
7386 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7387 rex.reg_line_lbr = FALSE;
7388 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007389#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007390 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007391#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007392 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007393
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007394 return nfa_regexec_both(NULL, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007395}
7396
7397#ifdef DEBUG
7398# undef ENABLE_LOG
7399#endif