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