blob: 7a335336d394a1ba7eab9471bf9dd0e9e7cb1e2f [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 Moolenaarfbc0d2e2013-05-19 19:40:29 +020032enum
33{
34 NFA_SPLIT = -1024,
35 NFA_MATCH,
36 NFA_SKIP_CHAR, /* matches a 0-length char */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020037
Bram Moolenaar417bad22013-06-07 14:08:30 +020038 NFA_START_COLL, /* [abc] start */
39 NFA_END_COLL, /* [abc] end */
40 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020041 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
42 NFA_RANGE, /* range of the two previous items
43 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020044 NFA_RANGE_MIN, /* low end of a range */
45 NFA_RANGE_MAX, /* high end of a range */
46
Bram Moolenaare7766ee2013-06-08 22:30:03 +020047 NFA_CONCAT, /* concatenate two previous items (postfix
48 * only) */
49 NFA_OR, /* \| (postfix only) */
50 NFA_STAR, /* greedy * (posfix only) */
51 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
52 NFA_QUEST, /* greedy \? (postfix only) */
53 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020054
55 NFA_BOL, /* ^ Begin line */
56 NFA_EOL, /* $ End line */
57 NFA_BOW, /* \< Begin word */
58 NFA_EOW, /* \> End word */
59 NFA_BOF, /* \%^ Begin file */
60 NFA_EOF, /* \%$ End file */
61 NFA_NEWL,
62 NFA_ZSTART, /* Used for \zs */
63 NFA_ZEND, /* Used for \ze */
64 NFA_NOPEN, /* Start of subexpression marked with \%( */
65 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
66 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020067 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020068 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020069 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020070 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020071 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020072 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020073 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020074 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020075 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020076 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_COMPOSING, /* Next nodes in NFA are part of the
79 composing multibyte char */
80 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020081 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020082
83 /* The following are used only in the postfix form, not in the NFA */
84 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
85 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
86 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
87 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
88 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
89
Bram Moolenaar5714b802013-05-28 22:03:20 +020090 NFA_BACKREF1, /* \1 */
91 NFA_BACKREF2, /* \2 */
92 NFA_BACKREF3, /* \3 */
93 NFA_BACKREF4, /* \4 */
94 NFA_BACKREF5, /* \5 */
95 NFA_BACKREF6, /* \6 */
96 NFA_BACKREF7, /* \7 */
97 NFA_BACKREF8, /* \8 */
98 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020099#ifdef FEAT_SYN_HL
100 NFA_ZREF1, /* \z1 */
101 NFA_ZREF2, /* \z2 */
102 NFA_ZREF3, /* \z3 */
103 NFA_ZREF4, /* \z4 */
104 NFA_ZREF5, /* \z5 */
105 NFA_ZREF6, /* \z6 */
106 NFA_ZREF7, /* \z7 */
107 NFA_ZREF8, /* \z8 */
108 NFA_ZREF9, /* \z9 */
109#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200110 NFA_SKIP, /* Skip characters */
111
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200112 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200113 NFA_MOPEN1,
114 NFA_MOPEN2,
115 NFA_MOPEN3,
116 NFA_MOPEN4,
117 NFA_MOPEN5,
118 NFA_MOPEN6,
119 NFA_MOPEN7,
120 NFA_MOPEN8,
121 NFA_MOPEN9,
122
123 NFA_MCLOSE,
124 NFA_MCLOSE1,
125 NFA_MCLOSE2,
126 NFA_MCLOSE3,
127 NFA_MCLOSE4,
128 NFA_MCLOSE5,
129 NFA_MCLOSE6,
130 NFA_MCLOSE7,
131 NFA_MCLOSE8,
132 NFA_MCLOSE9,
133
134#ifdef FEAT_SYN_HL
135 NFA_ZOPEN,
136 NFA_ZOPEN1,
137 NFA_ZOPEN2,
138 NFA_ZOPEN3,
139 NFA_ZOPEN4,
140 NFA_ZOPEN5,
141 NFA_ZOPEN6,
142 NFA_ZOPEN7,
143 NFA_ZOPEN8,
144 NFA_ZOPEN9,
145
146 NFA_ZCLOSE,
147 NFA_ZCLOSE1,
148 NFA_ZCLOSE2,
149 NFA_ZCLOSE3,
150 NFA_ZCLOSE4,
151 NFA_ZCLOSE5,
152 NFA_ZCLOSE6,
153 NFA_ZCLOSE7,
154 NFA_ZCLOSE8,
155 NFA_ZCLOSE9,
156#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200157
158 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200159 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200160 NFA_IDENT, /* Match identifier char */
161 NFA_SIDENT, /* Match identifier char but no digit */
162 NFA_KWORD, /* Match keyword char */
163 NFA_SKWORD, /* Match word char but no digit */
164 NFA_FNAME, /* Match file name char */
165 NFA_SFNAME, /* Match file name char but no digit */
166 NFA_PRINT, /* Match printable char */
167 NFA_SPRINT, /* Match printable char but no digit */
168 NFA_WHITE, /* Match whitespace char */
169 NFA_NWHITE, /* Match non-whitespace char */
170 NFA_DIGIT, /* Match digit char */
171 NFA_NDIGIT, /* Match non-digit char */
172 NFA_HEX, /* Match hex char */
173 NFA_NHEX, /* Match non-hex char */
174 NFA_OCTAL, /* Match octal char */
175 NFA_NOCTAL, /* Match non-octal char */
176 NFA_WORD, /* Match word char */
177 NFA_NWORD, /* Match non-word char */
178 NFA_HEAD, /* Match head char */
179 NFA_NHEAD, /* Match non-head char */
180 NFA_ALPHA, /* Match alpha char */
181 NFA_NALPHA, /* Match non-alpha char */
182 NFA_LOWER, /* Match lowercase char */
183 NFA_NLOWER, /* Match non-lowercase char */
184 NFA_UPPER, /* Match uppercase char */
185 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200186
187 NFA_CURSOR, /* Match cursor pos */
188 NFA_LNUM, /* Match line number */
189 NFA_LNUM_GT, /* Match > line number */
190 NFA_LNUM_LT, /* Match < line number */
191 NFA_COL, /* Match cursor column */
192 NFA_COL_GT, /* Match > cursor column */
193 NFA_COL_LT, /* Match < cursor column */
194 NFA_VCOL, /* Match cursor virtual column */
195 NFA_VCOL_GT, /* Match > cursor virtual column */
196 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200197 NFA_MARK, /* Match mark */
198 NFA_MARK_GT, /* Match > mark */
199 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200200 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200201
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200202 NFA_FIRST_NL = NFA_ANY + ADD_NL,
203 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
204
205 /* Character classes [:alnum:] etc */
206 NFA_CLASS_ALNUM,
207 NFA_CLASS_ALPHA,
208 NFA_CLASS_BLANK,
209 NFA_CLASS_CNTRL,
210 NFA_CLASS_DIGIT,
211 NFA_CLASS_GRAPH,
212 NFA_CLASS_LOWER,
213 NFA_CLASS_PRINT,
214 NFA_CLASS_PUNCT,
215 NFA_CLASS_SPACE,
216 NFA_CLASS_UPPER,
217 NFA_CLASS_XDIGIT,
218 NFA_CLASS_TAB,
219 NFA_CLASS_RETURN,
220 NFA_CLASS_BACKSPACE,
221 NFA_CLASS_ESCAPE
222};
223
224/* Keep in sync with classchars. */
225static int nfa_classcodes[] = {
226 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
227 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
228 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
229 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
230 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
231 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
232 NFA_UPPER, NFA_NUPPER
233};
234
235static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
236
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200238static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200239
Bram Moolenaar428e9872013-05-30 17:05:39 +0200240/* NFA regexp \1 .. \9 encountered. */
241static int nfa_has_backref;
242
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200243#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200244/* NFA regexp has \z( ), set zsubexpr. */
245static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200246#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200247
Bram Moolenaar963fee22013-05-26 21:47:28 +0200248/* Number of sub expressions actually being used during execution. 1 if only
249 * the whole match (subexpr 0) is used. */
250static int nfa_nsubexpr;
251
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252static int *post_start; /* holds the postfix form of r.e. */
253static int *post_end;
254static int *post_ptr;
255
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200256static int nstate; /* Number of states in the NFA. Also used when
257 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200258static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259
Bram Moolenaar307aa162013-06-02 16:34:21 +0200260/* If not NULL match must end at this position */
261static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200263/* listid is global, so that it increases on recursive calls to
264 * nfa_regmatch(), which means we don't have to clear the lastlist field of
265 * all the states. */
266static int nfa_listid;
267static int nfa_alt_listid;
268
269/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
270static int nfa_ll_index = 0;
271
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200272static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200273static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
274static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200275static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200277static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200278static int nfa_regatom __ARGS((void));
279static int nfa_regpiece __ARGS((void));
280static int nfa_regconcat __ARGS((void));
281static int nfa_regbranch __ARGS((void));
282static int nfa_reg __ARGS((int paren));
283#ifdef DEBUG
284static void nfa_set_code __ARGS((int c));
285static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200286static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
287static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200288static void nfa_dump __ARGS((nfa_regprog_T *prog));
289#endif
290static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200291static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200293static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200294static int check_char_class __ARGS((int class, int c));
295static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200296static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
297static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200298static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200299static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
301static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200302static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
304static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200305static int match_follows __ARGS((nfa_state_T *startstate, int depth));
306static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307
308/* helper functions used when doing re2post() ... regatom() parsing */
309#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200310 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311 return FAIL; \
312 *post_ptr++ = c; \
313 } while (0)
314
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200315/*
316 * Initialize internal variables before NFA compilation.
317 * Return OK on success, FAIL otherwise.
318 */
319 static int
320nfa_regcomp_start(expr, re_flags)
321 char_u *expr;
322 int re_flags; /* see vim_regcomp() */
323{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200324 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200325 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326
327 nstate = 0;
328 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200329 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200330 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200331
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200332 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200333 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200334 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200335
336 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200337 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339 post_start = (int *)lalloc(postfix_size, TRUE);
340 if (post_start == NULL)
341 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200342 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200343 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200345 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200347 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200348 regcomp_start(expr, re_flags);
349
350 return OK;
351}
352
353/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200354 * Figure out if the NFA state list starts with an anchor, must match at start
355 * of the line.
356 */
357 static int
358nfa_get_reganch(start, depth)
359 nfa_state_T *start;
360 int depth;
361{
362 nfa_state_T *p = start;
363
364 if (depth > 4)
365 return 0;
366
367 while (p != NULL)
368 {
369 switch (p->c)
370 {
371 case NFA_BOL:
372 case NFA_BOF:
373 return 1; /* yes! */
374
375 case NFA_ZSTART:
376 case NFA_ZEND:
377 case NFA_CURSOR:
378 case NFA_VISUAL:
379
380 case NFA_MOPEN:
381 case NFA_MOPEN1:
382 case NFA_MOPEN2:
383 case NFA_MOPEN3:
384 case NFA_MOPEN4:
385 case NFA_MOPEN5:
386 case NFA_MOPEN6:
387 case NFA_MOPEN7:
388 case NFA_MOPEN8:
389 case NFA_MOPEN9:
390 case NFA_NOPEN:
391#ifdef FEAT_SYN_HL
392 case NFA_ZOPEN:
393 case NFA_ZOPEN1:
394 case NFA_ZOPEN2:
395 case NFA_ZOPEN3:
396 case NFA_ZOPEN4:
397 case NFA_ZOPEN5:
398 case NFA_ZOPEN6:
399 case NFA_ZOPEN7:
400 case NFA_ZOPEN8:
401 case NFA_ZOPEN9:
402#endif
403 p = p->out;
404 break;
405
406 case NFA_SPLIT:
407 return nfa_get_reganch(p->out, depth + 1)
408 && nfa_get_reganch(p->out1, depth + 1);
409
410 default:
411 return 0; /* noooo */
412 }
413 }
414 return 0;
415}
416
417/*
418 * Figure out if the NFA state list starts with a character which must match
419 * at start of the match.
420 */
421 static int
422nfa_get_regstart(start, depth)
423 nfa_state_T *start;
424 int depth;
425{
426 nfa_state_T *p = start;
427
428 if (depth > 4)
429 return 0;
430
431 while (p != NULL)
432 {
433 switch (p->c)
434 {
435 /* all kinds of zero-width matches */
436 case NFA_BOL:
437 case NFA_BOF:
438 case NFA_BOW:
439 case NFA_EOW:
440 case NFA_ZSTART:
441 case NFA_ZEND:
442 case NFA_CURSOR:
443 case NFA_VISUAL:
444 case NFA_LNUM:
445 case NFA_LNUM_GT:
446 case NFA_LNUM_LT:
447 case NFA_COL:
448 case NFA_COL_GT:
449 case NFA_COL_LT:
450 case NFA_VCOL:
451 case NFA_VCOL_GT:
452 case NFA_VCOL_LT:
453 case NFA_MARK:
454 case NFA_MARK_GT:
455 case NFA_MARK_LT:
456
457 case NFA_MOPEN:
458 case NFA_MOPEN1:
459 case NFA_MOPEN2:
460 case NFA_MOPEN3:
461 case NFA_MOPEN4:
462 case NFA_MOPEN5:
463 case NFA_MOPEN6:
464 case NFA_MOPEN7:
465 case NFA_MOPEN8:
466 case NFA_MOPEN9:
467 case NFA_NOPEN:
468#ifdef FEAT_SYN_HL
469 case NFA_ZOPEN:
470 case NFA_ZOPEN1:
471 case NFA_ZOPEN2:
472 case NFA_ZOPEN3:
473 case NFA_ZOPEN4:
474 case NFA_ZOPEN5:
475 case NFA_ZOPEN6:
476 case NFA_ZOPEN7:
477 case NFA_ZOPEN8:
478 case NFA_ZOPEN9:
479#endif
480 p = p->out;
481 break;
482
483 case NFA_SPLIT:
484 {
485 int c1 = nfa_get_regstart(p->out, depth + 1);
486 int c2 = nfa_get_regstart(p->out1, depth + 1);
487
488 if (c1 == c2)
489 return c1; /* yes! */
490 return 0;
491 }
492
493 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200494 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200495 return p->c; /* yes! */
496 return 0;
497 }
498 }
499 return 0;
500}
501
502/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200503 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200504 * else. If so return a string in allocated memory with what must match after
505 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200506 */
507 static char_u *
508nfa_get_match_text(start)
509 nfa_state_T *start;
510{
511 nfa_state_T *p = start;
512 int len = 0;
513 char_u *ret;
514 char_u *s;
515
516 if (p->c != NFA_MOPEN)
517 return NULL; /* just in case */
518 p = p->out;
519 while (p->c > 0)
520 {
521 len += MB_CHAR2LEN(p->c);
522 p = p->out;
523 }
524 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
525 return NULL;
526
527 ret = alloc(len);
528 if (ret != NULL)
529 {
530 len = 0;
531 p = start->out->out; /* skip first char, it goes into regstart */
532 s = ret;
533 while (p->c > 0)
534 {
535#ifdef FEAT_MBYTE
536 if (has_mbyte)
537 s += (*mb_char2bytes)(p->c, s);
538 else
539#endif
540 *s++ = p->c;
541 p = p->out;
542 }
543 *s = NUL;
544 }
545 return ret;
546}
547
548/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200549 * Allocate more space for post_start. Called when
550 * running above the estimated number of states.
551 */
552 static int
553realloc_post_list()
554{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200555 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200556 int new_max = nstate_max + 1000;
557 int *new_start;
558 int *old_start;
559
560 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
561 if (new_start == NULL)
562 return FAIL;
563 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200564 old_start = post_start;
565 post_start = new_start;
566 post_ptr = new_start + (post_ptr - old_start);
567 post_end = post_start + new_max;
568 vim_free(old_start);
569 return OK;
570}
571
572/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200573 * Search between "start" and "end" and try to recognize a
574 * character class in expanded form. For example [0-9].
575 * On success, return the id the character class to be emitted.
576 * On failure, return 0 (=FAIL)
577 * Start points to the first char of the range, while end should point
578 * to the closing brace.
579 */
580 static int
581nfa_recognize_char_class(start, end, extra_newl)
582 char_u *start;
583 char_u *end;
584 int extra_newl;
585{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200586# define CLASS_not 0x80
587# define CLASS_af 0x40
588# define CLASS_AF 0x20
589# define CLASS_az 0x10
590# define CLASS_AZ 0x08
591# define CLASS_o7 0x04
592# define CLASS_o9 0x02
593# define CLASS_underscore 0x01
594
595 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200596 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200597 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598
599 if (extra_newl == TRUE)
600 newl = TRUE;
601
602 if (*end != ']')
603 return FAIL;
604 p = start;
605 if (*p == '^')
606 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200607 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200608 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609 }
610
611 while (p < end)
612 {
613 if (p + 2 < end && *(p + 1) == '-')
614 {
615 switch (*p)
616 {
617 case '0':
618 if (*(p + 2) == '9')
619 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200620 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200621 break;
622 }
623 else
624 if (*(p + 2) == '7')
625 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200626 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200627 break;
628 }
629 case 'a':
630 if (*(p + 2) == 'z')
631 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200632 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200633 break;
634 }
635 else
636 if (*(p + 2) == 'f')
637 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200638 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200639 break;
640 }
641 case 'A':
642 if (*(p + 2) == 'Z')
643 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200644 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200645 break;
646 }
647 else
648 if (*(p + 2) == 'F')
649 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200650 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200651 break;
652 }
653 /* FALLTHROUGH */
654 default:
655 return FAIL;
656 }
657 p += 3;
658 }
659 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
660 {
661 newl = TRUE;
662 p += 2;
663 }
664 else if (*p == '_')
665 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200666 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200667 p ++;
668 }
669 else if (*p == '\n')
670 {
671 newl = TRUE;
672 p ++;
673 }
674 else
675 return FAIL;
676 } /* while (p < end) */
677
678 if (p != end)
679 return FAIL;
680
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200681 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200682 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200683
684 switch (config)
685 {
686 case CLASS_o9:
687 return extra_newl + NFA_DIGIT;
688 case CLASS_not | CLASS_o9:
689 return extra_newl + NFA_NDIGIT;
690 case CLASS_af | CLASS_AF | CLASS_o9:
691 return extra_newl + NFA_HEX;
692 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
693 return extra_newl + NFA_NHEX;
694 case CLASS_o7:
695 return extra_newl + NFA_OCTAL;
696 case CLASS_not | CLASS_o7:
697 return extra_newl + NFA_NOCTAL;
698 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
699 return extra_newl + NFA_WORD;
700 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
701 return extra_newl + NFA_NWORD;
702 case CLASS_az | CLASS_AZ | CLASS_underscore:
703 return extra_newl + NFA_HEAD;
704 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
705 return extra_newl + NFA_NHEAD;
706 case CLASS_az | CLASS_AZ:
707 return extra_newl + NFA_ALPHA;
708 case CLASS_not | CLASS_az | CLASS_AZ:
709 return extra_newl + NFA_NALPHA;
710 case CLASS_az:
711 return extra_newl + NFA_LOWER;
712 case CLASS_not | CLASS_az:
713 return extra_newl + NFA_NLOWER;
714 case CLASS_AZ:
715 return extra_newl + NFA_UPPER;
716 case CLASS_not | CLASS_AZ:
717 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200718 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200719 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720}
721
722/*
723 * Produce the bytes for equivalence class "c".
724 * Currently only handles latin1, latin9 and utf-8.
725 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
726 * equivalent to 'a OR b OR c'
727 *
728 * NOTE! When changing this function, also update reg_equi_class()
729 */
730 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200731nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200733{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200734#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735
736#ifdef FEAT_MBYTE
737 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
738 || STRCMP(p_enc, "iso-8859-15") == 0)
739#endif
740 {
741 switch (c)
742 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200743 case 'A': case 0300: case 0301: case 0302:
744 case 0303: case 0304: case 0305:
745 EMIT2('A'); EMIT2(0300); EMIT2(0301);
746 EMIT2(0302); EMIT2(0303); EMIT2(0304);
747 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200748 return OK;
749
Bram Moolenaar417bad22013-06-07 14:08:30 +0200750 case 'C': case 0307:
751 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200752 return OK;
753
Bram Moolenaar417bad22013-06-07 14:08:30 +0200754 case 'E': case 0310: case 0311: case 0312: case 0313:
755 EMIT2('E'); EMIT2(0310); EMIT2(0311);
756 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200757 return OK;
758
Bram Moolenaar417bad22013-06-07 14:08:30 +0200759 case 'I': case 0314: case 0315: case 0316: case 0317:
760 EMIT2('I'); EMIT2(0314); EMIT2(0315);
761 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200762 return OK;
763
Bram Moolenaar417bad22013-06-07 14:08:30 +0200764 case 'N': case 0321:
765 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200766 return OK;
767
Bram Moolenaar417bad22013-06-07 14:08:30 +0200768 case 'O': case 0322: case 0323: case 0324: case 0325:
769 case 0326:
770 EMIT2('O'); EMIT2(0322); EMIT2(0323);
771 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200772 return OK;
773
Bram Moolenaar417bad22013-06-07 14:08:30 +0200774 case 'U': case 0331: case 0332: case 0333: case 0334:
775 EMIT2('U'); EMIT2(0331); EMIT2(0332);
776 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200777 return OK;
778
Bram Moolenaar417bad22013-06-07 14:08:30 +0200779 case 'Y': case 0335:
780 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200781 return OK;
782
Bram Moolenaar417bad22013-06-07 14:08:30 +0200783 case 'a': case 0340: case 0341: case 0342:
784 case 0343: case 0344: case 0345:
785 EMIT2('a'); EMIT2(0340); EMIT2(0341);
786 EMIT2(0342); EMIT2(0343); EMIT2(0344);
787 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 return OK;
789
Bram Moolenaar417bad22013-06-07 14:08:30 +0200790 case 'c': case 0347:
791 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200792 return OK;
793
Bram Moolenaar417bad22013-06-07 14:08:30 +0200794 case 'e': case 0350: case 0351: case 0352: case 0353:
795 EMIT2('e'); EMIT2(0350); EMIT2(0351);
796 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200797 return OK;
798
Bram Moolenaar417bad22013-06-07 14:08:30 +0200799 case 'i': case 0354: case 0355: case 0356: case 0357:
800 EMIT2('i'); EMIT2(0354); EMIT2(0355);
801 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200802 return OK;
803
Bram Moolenaar417bad22013-06-07 14:08:30 +0200804 case 'n': case 0361:
805 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200806 return OK;
807
Bram Moolenaar417bad22013-06-07 14:08:30 +0200808 case 'o': case 0362: case 0363: case 0364: case 0365:
809 case 0366:
810 EMIT2('o'); EMIT2(0362); EMIT2(0363);
811 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 return OK;
813
Bram Moolenaar417bad22013-06-07 14:08:30 +0200814 case 'u': case 0371: case 0372: case 0373: case 0374:
815 EMIT2('u'); EMIT2(0371); EMIT2(0372);
816 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 return OK;
818
Bram Moolenaar417bad22013-06-07 14:08:30 +0200819 case 'y': case 0375: case 0377:
820 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 return OK;
822
823 default:
824 return FAIL;
825 }
826 }
827
828 EMIT(c);
829 return OK;
830#undef EMIT2
831}
832
833/*
834 * Code to parse regular expression.
835 *
836 * We try to reuse parsing functions in regexp.c to
837 * minimize surprise and keep the syntax consistent.
838 */
839
840/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200841 * Parse the lowest level.
842 *
843 * An atom can be one of a long list of items. Many atoms match one character
844 * in the text. It is often an ordinary character or a character class.
845 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
846 * is only for syntax highlighting.
847 *
848 * atom ::= ordinary-atom
849 * or \( pattern \)
850 * or \%( pattern \)
851 * or \z( pattern \)
852 */
853 static int
854nfa_regatom()
855{
856 int c;
857 int charclass;
858 int equiclass;
859 int collclass;
860 int got_coll_char;
861 char_u *p;
862 char_u *endp;
863#ifdef FEAT_MBYTE
864 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200865#endif
866 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867 int emit_range;
868 int negated;
869 int result;
870 int startc = -1;
871 int endc = -1;
872 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200873
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200874 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 switch (c)
876 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200877 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200878 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
879
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 case Magic('^'):
881 EMIT(NFA_BOL);
882 break;
883
884 case Magic('$'):
885 EMIT(NFA_EOL);
886#if defined(FEAT_SYN_HL) || defined(PROTO)
887 had_eol = TRUE;
888#endif
889 break;
890
891 case Magic('<'):
892 EMIT(NFA_BOW);
893 break;
894
895 case Magic('>'):
896 EMIT(NFA_EOW);
897 break;
898
899 case Magic('_'):
900 c = no_Magic(getchr());
901 if (c == '^') /* "\_^" is start-of-line */
902 {
903 EMIT(NFA_BOL);
904 break;
905 }
906 if (c == '$') /* "\_$" is end-of-line */
907 {
908 EMIT(NFA_EOL);
909#if defined(FEAT_SYN_HL) || defined(PROTO)
910 had_eol = TRUE;
911#endif
912 break;
913 }
914
915 extra = ADD_NL;
916
917 /* "\_[" is collection plus newline */
918 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200919 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200920
921 /* "\_x" is character class plus newline */
922 /*FALLTHROUGH*/
923
924 /*
925 * Character classes.
926 */
927 case Magic('.'):
928 case Magic('i'):
929 case Magic('I'):
930 case Magic('k'):
931 case Magic('K'):
932 case Magic('f'):
933 case Magic('F'):
934 case Magic('p'):
935 case Magic('P'):
936 case Magic('s'):
937 case Magic('S'):
938 case Magic('d'):
939 case Magic('D'):
940 case Magic('x'):
941 case Magic('X'):
942 case Magic('o'):
943 case Magic('O'):
944 case Magic('w'):
945 case Magic('W'):
946 case Magic('h'):
947 case Magic('H'):
948 case Magic('a'):
949 case Magic('A'):
950 case Magic('l'):
951 case Magic('L'):
952 case Magic('u'):
953 case Magic('U'):
954 p = vim_strchr(classchars, no_Magic(c));
955 if (p == NULL)
956 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200957 EMSGN("INTERNAL: Unknown character class char: %ld", c);
958 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200959 }
960#ifdef FEAT_MBYTE
961 /* When '.' is followed by a composing char ignore the dot, so that
962 * the composing char is matched here. */
963 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
964 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200965 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200966 c = getchr();
967 goto nfa_do_multibyte;
968 }
969#endif
970 EMIT(nfa_classcodes[p - classchars]);
971 if (extra == ADD_NL)
972 {
973 EMIT(NFA_NEWL);
974 EMIT(NFA_OR);
975 regflags |= RF_HASNL;
976 }
977 break;
978
979 case Magic('n'):
980 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200981 /* In a string "\n" matches a newline character. */
982 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200983 else
984 {
985 /* In buffer text "\n" matches the end of a line. */
986 EMIT(NFA_NEWL);
987 regflags |= RF_HASNL;
988 }
989 break;
990
991 case Magic('('):
992 if (nfa_reg(REG_PAREN) == FAIL)
993 return FAIL; /* cascaded error */
994 break;
995
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200996 case Magic('|'):
997 case Magic('&'):
998 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200999 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return FAIL;
1001
1002 case Magic('='):
1003 case Magic('?'):
1004 case Magic('+'):
1005 case Magic('@'):
1006 case Magic('*'):
1007 case Magic('{'):
1008 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001009 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001010 return FAIL;
1011
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001012 case Magic('~'):
1013 {
1014 char_u *lp;
1015
1016 /* Previous substitute pattern.
1017 * Generated as "\%(pattern\)". */
1018 if (reg_prev_sub == NULL)
1019 {
1020 EMSG(_(e_nopresub));
1021 return FAIL;
1022 }
1023 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1024 {
1025 EMIT(PTR2CHAR(lp));
1026 if (lp != reg_prev_sub)
1027 EMIT(NFA_CONCAT);
1028 }
1029 EMIT(NFA_NOPEN);
1030 break;
1031 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001032
Bram Moolenaar428e9872013-05-30 17:05:39 +02001033 case Magic('1'):
1034 case Magic('2'):
1035 case Magic('3'):
1036 case Magic('4'):
1037 case Magic('5'):
1038 case Magic('6'):
1039 case Magic('7'):
1040 case Magic('8'):
1041 case Magic('9'):
1042 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1043 nfa_has_backref = TRUE;
1044 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001045
1046 case Magic('z'):
1047 c = no_Magic(getchr());
1048 switch (c)
1049 {
1050 case 's':
1051 EMIT(NFA_ZSTART);
1052 break;
1053 case 'e':
1054 EMIT(NFA_ZEND);
1055 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001056 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001057#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001058 case '1':
1059 case '2':
1060 case '3':
1061 case '4':
1062 case '5':
1063 case '6':
1064 case '7':
1065 case '8':
1066 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001067 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001068 if (reg_do_extmatch != REX_USE)
1069 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001070 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1071 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001072 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001073 re_has_z = REX_USE;
1074 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001075 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001076 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001077 if (reg_do_extmatch != REX_SET)
1078 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001079 if (nfa_reg(REG_ZPAREN) == FAIL)
1080 return FAIL; /* cascaded error */
1081 re_has_z = REX_SET;
1082 break;
1083#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001084 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001085 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 no_Magic(c));
1087 return FAIL;
1088 }
1089 break;
1090
1091 case Magic('%'):
1092 c = no_Magic(getchr());
1093 switch (c)
1094 {
1095 /* () without a back reference */
1096 case '(':
1097 if (nfa_reg(REG_NPAREN) == FAIL)
1098 return FAIL;
1099 EMIT(NFA_NOPEN);
1100 break;
1101
1102 case 'd': /* %d123 decimal */
1103 case 'o': /* %o123 octal */
1104 case 'x': /* %xab hex 2 */
1105 case 'u': /* %uabcd hex 4 */
1106 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001107 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001108 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001109
Bram Moolenaar47196582013-05-25 22:04:23 +02001110 switch (c)
1111 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001112 case 'd': nr = getdecchrs(); break;
1113 case 'o': nr = getoctchrs(); break;
1114 case 'x': nr = gethexchrs(2); break;
1115 case 'u': nr = gethexchrs(4); break;
1116 case 'U': nr = gethexchrs(8); break;
1117 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001118 }
1119
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001120 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001121 EMSG2_RET_FAIL(
1122 _("E678: Invalid character after %s%%[dxouU]"),
1123 reg_magic == MAGIC_ALL);
1124 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001125 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001126 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001127 break;
1128
1129 /* Catch \%^ and \%$ regardless of where they appear in the
1130 * pattern -- regardless of whether or not it makes sense. */
1131 case '^':
1132 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001133 break;
1134
1135 case '$':
1136 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001137 break;
1138
1139 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001140 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 break;
1142
1143 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001144 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001145 break;
1146
1147 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001148 {
1149 int n;
1150
1151 /* \%[abc] */
1152 for (n = 0; (c = getchr()) != ']'; ++n)
1153 {
1154 if (c == NUL)
1155 EMSG2_RET_FAIL(_(e_missing_sb),
1156 reg_magic == MAGIC_ALL);
1157 EMIT(c);
1158 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001159 if (n == 0)
1160 EMSG2_RET_FAIL(_(e_empty_sb),
1161 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001162 EMIT(NFA_OPT_CHARS);
1163 EMIT(n);
1164 break;
1165 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001166
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001167 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001168 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001169 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001170 int cmp = c;
1171
1172 if (c == '<' || c == '>')
1173 c = getchr();
1174 while (VIM_ISDIGIT(c))
1175 {
1176 n = n * 10 + (c - '0');
1177 c = getchr();
1178 }
1179 if (c == 'l' || c == 'c' || c == 'v')
1180 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001181 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001182 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001183 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001184 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001185 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001186 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001187 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001188 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001189 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001190 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001191 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001192 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001193 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001194 break;
1195 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001196 else if (c == '\'' && n == 0)
1197 {
1198 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001199 EMIT(cmp == '<' ? NFA_MARK_LT :
1200 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001201 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001202 break;
1203 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001204 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001205 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1206 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001207 return FAIL;
1208 }
1209 break;
1210
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001211 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001212collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001214 * [abc] uses NFA_START_COLL - NFA_END_COLL
1215 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1216 * Each character is produced as a regular state, using
1217 * NFA_CONCAT to bind them together.
1218 * Besides normal characters there can be:
1219 * - character classes NFA_CLASS_*
1220 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 */
1222
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 p = regparse;
1224 endp = skip_anyof(p);
1225 if (*endp == ']')
1226 {
1227 /*
1228 * Try to reverse engineer character classes. For example,
1229 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1230 * and perform the necessary substitutions in the NFA.
1231 */
1232 result = nfa_recognize_char_class(regparse, endp,
1233 extra == ADD_NL);
1234 if (result != FAIL)
1235 {
1236 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1237 EMIT(result);
1238 else /* must be char class + newline */
1239 {
1240 EMIT(result - ADD_NL);
1241 EMIT(NFA_NEWL);
1242 EMIT(NFA_OR);
1243 }
1244 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001245 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246 return OK;
1247 }
1248 /*
1249 * Failed to recognize a character class. Use the simple
1250 * version that turns [abc] into 'a' OR 'b' OR 'c'
1251 */
1252 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254 if (*regparse == '^') /* negated range */
1255 {
1256 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001257 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001258 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001260 else
1261 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262 if (*regparse == '-')
1263 {
1264 startc = '-';
1265 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001266 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001267 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001268 }
1269 /* Emit the OR branches for each character in the [] */
1270 emit_range = FALSE;
1271 while (regparse < endp)
1272 {
1273 oldstartc = startc;
1274 startc = -1;
1275 got_coll_char = FALSE;
1276 if (*regparse == '[')
1277 {
1278 /* Check for [: :], [= =], [. .] */
1279 equiclass = collclass = 0;
1280 charclass = get_char_class(&regparse);
1281 if (charclass == CLASS_NONE)
1282 {
1283 equiclass = get_equi_class(&regparse);
1284 if (equiclass == 0)
1285 collclass = get_coll_element(&regparse);
1286 }
1287
1288 /* Character class like [:alpha:] */
1289 if (charclass != CLASS_NONE)
1290 {
1291 switch (charclass)
1292 {
1293 case CLASS_ALNUM:
1294 EMIT(NFA_CLASS_ALNUM);
1295 break;
1296 case CLASS_ALPHA:
1297 EMIT(NFA_CLASS_ALPHA);
1298 break;
1299 case CLASS_BLANK:
1300 EMIT(NFA_CLASS_BLANK);
1301 break;
1302 case CLASS_CNTRL:
1303 EMIT(NFA_CLASS_CNTRL);
1304 break;
1305 case CLASS_DIGIT:
1306 EMIT(NFA_CLASS_DIGIT);
1307 break;
1308 case CLASS_GRAPH:
1309 EMIT(NFA_CLASS_GRAPH);
1310 break;
1311 case CLASS_LOWER:
1312 EMIT(NFA_CLASS_LOWER);
1313 break;
1314 case CLASS_PRINT:
1315 EMIT(NFA_CLASS_PRINT);
1316 break;
1317 case CLASS_PUNCT:
1318 EMIT(NFA_CLASS_PUNCT);
1319 break;
1320 case CLASS_SPACE:
1321 EMIT(NFA_CLASS_SPACE);
1322 break;
1323 case CLASS_UPPER:
1324 EMIT(NFA_CLASS_UPPER);
1325 break;
1326 case CLASS_XDIGIT:
1327 EMIT(NFA_CLASS_XDIGIT);
1328 break;
1329 case CLASS_TAB:
1330 EMIT(NFA_CLASS_TAB);
1331 break;
1332 case CLASS_RETURN:
1333 EMIT(NFA_CLASS_RETURN);
1334 break;
1335 case CLASS_BACKSPACE:
1336 EMIT(NFA_CLASS_BACKSPACE);
1337 break;
1338 case CLASS_ESCAPE:
1339 EMIT(NFA_CLASS_ESCAPE);
1340 break;
1341 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001342 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 continue;
1344 }
1345 /* Try equivalence class [=a=] and the like */
1346 if (equiclass != 0)
1347 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001348 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001349 if (result == FAIL)
1350 {
1351 /* should never happen */
1352 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1353 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001354 continue;
1355 }
1356 /* Try collating class like [. .] */
1357 if (collclass != 0)
1358 {
1359 startc = collclass; /* allow [.a.]-x as a range */
1360 /* Will emit the proper atom at the end of the
1361 * while loop. */
1362 }
1363 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001364 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1365 * start character. */
1366 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001367 {
1368 emit_range = TRUE;
1369 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001370 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001371 continue; /* reading the end of the range */
1372 }
1373
1374 /* Now handle simple and escaped characters.
1375 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1376 * accepts "\t", "\e", etc., but only when the 'l' flag in
1377 * 'cpoptions' is not included.
1378 * Posix doesn't recognize backslash at all.
1379 */
1380 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001381 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001382 && regparse + 1 <= endp
1383 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001384 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385 && vim_strchr(REGEXP_ABBR, regparse[1])
1386 != NULL)
1387 )
1388 )
1389 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001390 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001391
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001392 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001393 startc = reg_string ? NL : NFA_NEWL;
1394 else
1395 if (*regparse == 'd'
1396 || *regparse == 'o'
1397 || *regparse == 'x'
1398 || *regparse == 'u'
1399 || *regparse == 'U'
1400 )
1401 {
1402 /* TODO(RE) This needs more testing */
1403 startc = coll_get_char();
1404 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001405 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 }
1407 else
1408 {
1409 /* \r,\t,\e,\b */
1410 startc = backslash_trans(*regparse);
1411 }
1412 }
1413
1414 /* Normal printable char */
1415 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001416 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417
1418 /* Previous char was '-', so this char is end of range. */
1419 if (emit_range)
1420 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001421 endc = startc;
1422 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423 if (startc > endc)
1424 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001425
1426 if (endc > startc + 2)
1427 {
1428 /* Emit a range instead of the sequence of
1429 * individual characters. */
1430 if (startc == 0)
1431 /* \x00 is translated to \x0a, start at \x01. */
1432 EMIT(1);
1433 else
1434 --post_ptr; /* remove NFA_CONCAT */
1435 EMIT(endc);
1436 EMIT(NFA_RANGE);
1437 EMIT(NFA_CONCAT);
1438 }
1439 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001441 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 || (*mb_char2len)(endc) > 1))
1443 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001444 /* Emit the characters in the range.
1445 * "startc" was already emitted, so skip it.
1446 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 for (c = startc + 1; c <= endc; c++)
1448 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001449 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001450 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001451 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 }
1453 else
1454#endif
1455 {
1456#ifdef EBCDIC
1457 int alpha_only = FALSE;
1458
1459 /* for alphabetical range skip the gaps
1460 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1461 if (isalpha(startc) && isalpha(endc))
1462 alpha_only = TRUE;
1463#endif
1464 /* Emit the range. "startc" was already emitted, so
1465 * skip it. */
1466 for (c = startc + 1; c <= endc; c++)
1467#ifdef EBCDIC
1468 if (!alpha_only || isalpha(startc))
1469#endif
1470 {
1471 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001472 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001473 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001475 emit_range = FALSE;
1476 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 }
1478 else
1479 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001480 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001481 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001482 * Normally, simply emit startc. But if we get char
1483 * code=0 from a collating char, then replace it with
1484 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001486 * the backtracking engine. */
1487 if (startc == NFA_NEWL)
1488 {
1489 /* Line break can't be matched as part of the
1490 * collection, add an OR below. But not for negated
1491 * range. */
1492 if (!negated)
1493 extra = ADD_NL;
1494 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001496 {
1497 if (got_coll_char == TRUE && startc == 0)
1498 EMIT(0x0a);
1499 else
1500 EMIT(startc);
1501 EMIT(NFA_CONCAT);
1502 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001503 }
1504
Bram Moolenaar51a29832013-05-28 22:30:35 +02001505 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001506 } /* while (p < endp) */
1507
Bram Moolenaar51a29832013-05-28 22:30:35 +02001508 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 if (*regparse == '-') /* if last, '-' is just a char */
1510 {
1511 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001512 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001513 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001514 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001515
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516 /* skip the trailing ] */
1517 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001518 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001519
1520 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001522 EMIT(NFA_END_NEG_COLL);
1523 else
1524 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001525
1526 /* \_[] also matches \n but it's not negated */
1527 if (extra == ADD_NL)
1528 {
1529 EMIT(reg_string ? NL : NFA_NEWL);
1530 EMIT(NFA_OR);
1531 }
1532
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001533 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001534 } /* if exists closing ] */
1535
1536 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001538 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540 default:
1541 {
1542#ifdef FEAT_MBYTE
1543 int plen;
1544
1545nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001546 /* plen is length of current char with composing chars */
1547 if (enc_utf8 && ((*mb_char2len)(c)
1548 != (plen = (*mb_ptr2len)(old_regparse))
1549 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001551 int i = 0;
1552
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001553 /* A base character plus composing characters, or just one
1554 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001555 * This requires creating a separate atom as if enclosing
1556 * the characters in (), where NFA_COMPOSING is the ( and
1557 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001558 * building the postfix form, not the NFA itself;
1559 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001560 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001561 for (;;)
1562 {
1563 EMIT(c);
1564 if (i > 0)
1565 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001566 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001567 break;
1568 c = utf_ptr2char(old_regparse + i);
1569 }
1570 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001571 regparse = old_regparse + plen;
1572 }
1573 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001574#endif
1575 {
1576 c = no_Magic(c);
1577 EMIT(c);
1578 }
1579 return OK;
1580 }
1581 }
1582
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001583 return OK;
1584}
1585
1586/*
1587 * Parse something followed by possible [*+=].
1588 *
1589 * A piece is an atom, possibly followed by a multi, an indication of how many
1590 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1591 * characters: "", "a", "aa", etc.
1592 *
1593 * piece ::= atom
1594 * or atom multi
1595 */
1596 static int
1597nfa_regpiece()
1598{
1599 int i;
1600 int op;
1601 int ret;
1602 long minval, maxval;
1603 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001604 parse_state_T old_state;
1605 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001606 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001607 int old_post_pos;
1608 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001609 int quest;
1610
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001611 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1612 * next. */
1613 save_parse_state(&old_state);
1614
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001615 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001616 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001617
1618 ret = nfa_regatom();
1619 if (ret == FAIL)
1620 return FAIL; /* cascaded error */
1621
1622 op = peekchr();
1623 if (re_multi_type(op) == NOT_MULTI)
1624 return OK;
1625
1626 skipchr();
1627 switch (op)
1628 {
1629 case Magic('*'):
1630 EMIT(NFA_STAR);
1631 break;
1632
1633 case Magic('+'):
1634 /*
1635 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1636 * first and only submatch would be "aaa". But the backtracking
1637 * engine interprets the plus as "try matching one more time", and
1638 * a* matches a second time at the end of the input, the empty
1639 * string.
1640 * The submatch will the empty string.
1641 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001642 * In order to be consistent with the old engine, we replace
1643 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001644 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001645 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001646 curchr = -1;
1647 if (nfa_regatom() == FAIL)
1648 return FAIL;
1649 EMIT(NFA_STAR);
1650 EMIT(NFA_CONCAT);
1651 skipchr(); /* skip the \+ */
1652 break;
1653
1654 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001655 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001656 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001657 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 switch(op)
1659 {
1660 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001661 /* \@= */
1662 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001664 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001665 /* \@! */
1666 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001667 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001669 op = no_Magic(getchr());
1670 if (op == '=')
1671 /* \@<= */
1672 i = NFA_PREV_ATOM_JUST_BEFORE;
1673 else if (op == '!')
1674 /* \@<! */
1675 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1676 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001678 /* \@> */
1679 i = NFA_PREV_ATOM_LIKE_PATTERN;
1680 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001682 if (i == 0)
1683 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001684 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1685 return FAIL;
1686 }
1687 EMIT(i);
1688 if (i == NFA_PREV_ATOM_JUST_BEFORE
1689 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1690 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 break;
1692
1693 case Magic('?'):
1694 case Magic('='):
1695 EMIT(NFA_QUEST);
1696 break;
1697
1698 case Magic('{'):
1699 /* a{2,5} will expand to 'aaa?a?a?'
1700 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1701 * version of '?'
1702 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1703 * parenthesis have the same id
1704 */
1705
1706 greedy = TRUE;
1707 c2 = peekchr();
1708 if (c2 == '-' || c2 == Magic('-'))
1709 {
1710 skipchr();
1711 greedy = FALSE;
1712 }
1713 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001714 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001715
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001716 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1717 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001718 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001719 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001720 if (greedy)
1721 /* \{}, \{0,} */
1722 EMIT(NFA_STAR);
1723 else
1724 /* \{-}, \{-0,} */
1725 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001726 break;
1727 }
1728
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001729 /* Special case: x{0} or x{-0} */
1730 if (maxval == 0)
1731 {
1732 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001733 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001734 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1735 EMIT(NFA_SKIP_CHAR);
1736 return OK;
1737 }
1738
1739 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001740 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001741 /* Save parse state after the repeated atom and the \{} */
1742 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1745 for (i = 0; i < maxval; i++)
1746 {
1747 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001748 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001749 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001750 if (nfa_regatom() == FAIL)
1751 return FAIL;
1752 /* after "minval" times, atoms are optional */
1753 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001754 {
1755 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001756 {
1757 if (greedy)
1758 EMIT(NFA_STAR);
1759 else
1760 EMIT(NFA_STAR_NONGREEDY);
1761 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001762 else
1763 EMIT(quest);
1764 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001765 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001767 if (i + 1 > minval && maxval == MAX_LIMIT)
1768 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 }
1770
1771 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001772 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 curchr = -1;
1774
1775 break;
1776
1777
1778 default:
1779 break;
1780 } /* end switch */
1781
1782 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001784 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001785
1786 return OK;
1787}
1788
1789/*
1790 * Parse one or more pieces, concatenated. It matches a match for the
1791 * first piece, followed by a match for the second piece, etc. Example:
1792 * "f[0-9]b", first matches "f", then a digit and then "b".
1793 *
1794 * concat ::= piece
1795 * or piece piece
1796 * or piece piece piece
1797 * etc.
1798 */
1799 static int
1800nfa_regconcat()
1801{
1802 int cont = TRUE;
1803 int first = TRUE;
1804
1805 while (cont)
1806 {
1807 switch (peekchr())
1808 {
1809 case NUL:
1810 case Magic('|'):
1811 case Magic('&'):
1812 case Magic(')'):
1813 cont = FALSE;
1814 break;
1815
1816 case Magic('Z'):
1817#ifdef FEAT_MBYTE
1818 regflags |= RF_ICOMBINE;
1819#endif
1820 skipchr_keepstart();
1821 break;
1822 case Magic('c'):
1823 regflags |= RF_ICASE;
1824 skipchr_keepstart();
1825 break;
1826 case Magic('C'):
1827 regflags |= RF_NOICASE;
1828 skipchr_keepstart();
1829 break;
1830 case Magic('v'):
1831 reg_magic = MAGIC_ALL;
1832 skipchr_keepstart();
1833 curchr = -1;
1834 break;
1835 case Magic('m'):
1836 reg_magic = MAGIC_ON;
1837 skipchr_keepstart();
1838 curchr = -1;
1839 break;
1840 case Magic('M'):
1841 reg_magic = MAGIC_OFF;
1842 skipchr_keepstart();
1843 curchr = -1;
1844 break;
1845 case Magic('V'):
1846 reg_magic = MAGIC_NONE;
1847 skipchr_keepstart();
1848 curchr = -1;
1849 break;
1850
1851 default:
1852 if (nfa_regpiece() == FAIL)
1853 return FAIL;
1854 if (first == FALSE)
1855 EMIT(NFA_CONCAT);
1856 else
1857 first = FALSE;
1858 break;
1859 }
1860 }
1861
1862 return OK;
1863}
1864
1865/*
1866 * Parse a branch, one or more concats, separated by "\&". It matches the
1867 * last concat, but only if all the preceding concats also match at the same
1868 * position. Examples:
1869 * "foobeep\&..." matches "foo" in "foobeep".
1870 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1871 *
1872 * branch ::= concat
1873 * or concat \& concat
1874 * or concat \& concat \& concat
1875 * etc.
1876 */
1877 static int
1878nfa_regbranch()
1879{
1880 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001881 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882
Bram Moolenaar16299b52013-05-30 18:45:23 +02001883 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884
1885 /* First branch, possibly the only one */
1886 if (nfa_regconcat() == FAIL)
1887 return FAIL;
1888
1889 ch = peekchr();
1890 /* Try next concats */
1891 while (ch == Magic('&'))
1892 {
1893 skipchr();
1894 EMIT(NFA_NOPEN);
1895 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001896 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 if (nfa_regconcat() == FAIL)
1898 return FAIL;
1899 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001900 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 EMIT(NFA_SKIP_CHAR);
1902 EMIT(NFA_CONCAT);
1903 ch = peekchr();
1904 }
1905
1906 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001907 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 EMIT(NFA_SKIP_CHAR);
1909
1910 return OK;
1911}
1912
1913/*
1914 * Parse a pattern, one or more branches, separated by "\|". It matches
1915 * anything that matches one of the branches. Example: "foo\|beep" matches
1916 * "foo" and matches "beep". If more than one branch matches, the first one
1917 * is used.
1918 *
1919 * pattern ::= branch
1920 * or branch \| branch
1921 * or branch \| branch \| branch
1922 * etc.
1923 */
1924 static int
1925nfa_reg(paren)
1926 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1927{
1928 int parno = 0;
1929
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 if (paren == REG_PAREN)
1931 {
1932 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 parno = regnpar++;
1935 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001936#ifdef FEAT_SYN_HL
1937 else if (paren == REG_ZPAREN)
1938 {
1939 /* Make a ZOPEN node. */
1940 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001941 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001942 parno = regnzpar++;
1943 }
1944#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945
1946 if (nfa_regbranch() == FAIL)
1947 return FAIL; /* cascaded error */
1948
1949 while (peekchr() == Magic('|'))
1950 {
1951 skipchr();
1952 if (nfa_regbranch() == FAIL)
1953 return FAIL; /* cascaded error */
1954 EMIT(NFA_OR);
1955 }
1956
1957 /* Check for proper termination. */
1958 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1959 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 if (paren == REG_NPAREN)
1961 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1962 else
1963 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1964 }
1965 else if (paren == REG_NOPAREN && peekchr() != NUL)
1966 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001967 if (peekchr() == Magic(')'))
1968 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1969 else
1970 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1971 }
1972 /*
1973 * Here we set the flag allowing back references to this set of
1974 * parentheses.
1975 */
1976 if (paren == REG_PAREN)
1977 {
1978 had_endbrace[parno] = TRUE; /* have seen the close paren */
1979 EMIT(NFA_MOPEN + parno);
1980 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001981#ifdef FEAT_SYN_HL
1982 else if (paren == REG_ZPAREN)
1983 EMIT(NFA_ZOPEN + parno);
1984#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001985
1986 return OK;
1987}
1988
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989#ifdef DEBUG
1990static char_u code[50];
1991
1992 static void
1993nfa_set_code(c)
1994 int c;
1995{
1996 int addnl = FALSE;
1997
1998 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1999 {
2000 addnl = TRUE;
2001 c -= ADD_NL;
2002 }
2003
2004 STRCPY(code, "");
2005 switch (c)
2006 {
2007 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2008 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2009 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2010 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2011 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2012 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2013
Bram Moolenaar5714b802013-05-28 22:03:20 +02002014 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2015 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2016 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2017 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2018 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2019 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2020 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2021 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2022 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002023#ifdef FEAT_SYN_HL
2024 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2025 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2026 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2027 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2028 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2029 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2030 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2031 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2032 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2033#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002034 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2035
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002036 case NFA_PREV_ATOM_NO_WIDTH:
2037 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002038 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2039 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002040 case NFA_PREV_ATOM_JUST_BEFORE:
2041 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2042 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2043 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002044 case NFA_PREV_ATOM_LIKE_PATTERN:
2045 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2046
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002047 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2048 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002050 case NFA_START_INVISIBLE_FIRST:
2051 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002052 case NFA_START_INVISIBLE_NEG:
2053 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002054 case NFA_START_INVISIBLE_NEG_FIRST:
2055 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002056 case NFA_START_INVISIBLE_BEFORE:
2057 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002058 case NFA_START_INVISIBLE_BEFORE_FIRST:
2059 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002060 case NFA_START_INVISIBLE_BEFORE_NEG:
2061 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002062 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2063 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002064 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002066 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002067 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2070 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002071 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002073 case NFA_MOPEN:
2074 case NFA_MOPEN1:
2075 case NFA_MOPEN2:
2076 case NFA_MOPEN3:
2077 case NFA_MOPEN4:
2078 case NFA_MOPEN5:
2079 case NFA_MOPEN6:
2080 case NFA_MOPEN7:
2081 case NFA_MOPEN8:
2082 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 STRCPY(code, "NFA_MOPEN(x)");
2084 code[10] = c - NFA_MOPEN + '0';
2085 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002086 case NFA_MCLOSE:
2087 case NFA_MCLOSE1:
2088 case NFA_MCLOSE2:
2089 case NFA_MCLOSE3:
2090 case NFA_MCLOSE4:
2091 case NFA_MCLOSE5:
2092 case NFA_MCLOSE6:
2093 case NFA_MCLOSE7:
2094 case NFA_MCLOSE8:
2095 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002096 STRCPY(code, "NFA_MCLOSE(x)");
2097 code[11] = c - NFA_MCLOSE + '0';
2098 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002099#ifdef FEAT_SYN_HL
2100 case NFA_ZOPEN:
2101 case NFA_ZOPEN1:
2102 case NFA_ZOPEN2:
2103 case NFA_ZOPEN3:
2104 case NFA_ZOPEN4:
2105 case NFA_ZOPEN5:
2106 case NFA_ZOPEN6:
2107 case NFA_ZOPEN7:
2108 case NFA_ZOPEN8:
2109 case NFA_ZOPEN9:
2110 STRCPY(code, "NFA_ZOPEN(x)");
2111 code[10] = c - NFA_ZOPEN + '0';
2112 break;
2113 case NFA_ZCLOSE:
2114 case NFA_ZCLOSE1:
2115 case NFA_ZCLOSE2:
2116 case NFA_ZCLOSE3:
2117 case NFA_ZCLOSE4:
2118 case NFA_ZCLOSE5:
2119 case NFA_ZCLOSE6:
2120 case NFA_ZCLOSE7:
2121 case NFA_ZCLOSE8:
2122 case NFA_ZCLOSE9:
2123 STRCPY(code, "NFA_ZCLOSE(x)");
2124 code[11] = c - NFA_ZCLOSE + '0';
2125 break;
2126#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002127 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2128 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2129 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2130 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002131 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2132 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002133 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2134 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2135 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2136 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2137 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2138 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2139 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2140 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2141 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2142 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2143 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2144 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2145 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2146 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2147
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002148 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002149 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2150 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2151 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002152 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2153 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002154
2155 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2156 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2157 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2158 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2159 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2160 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2161 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2162
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2164 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2165 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2166 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2167 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2168 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2169 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2170 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2171 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2172 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2173 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2174 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2175 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2176 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2177 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2178 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2179
2180 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2181 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2182 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2183 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2184 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2185 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2186 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2187 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2188 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2189 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2190 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2191 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2192 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2193 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2194 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2195 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2196 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2197 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2198 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2199 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2200 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2201 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2202 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2203 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2204 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2205 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2206 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2207
2208 default:
2209 STRCPY(code, "CHAR(x)");
2210 code[5] = c;
2211 }
2212
2213 if (addnl == TRUE)
2214 STRCAT(code, " + NEWLINE ");
2215
2216}
2217
2218#ifdef ENABLE_LOG
2219static FILE *log_fd;
2220
2221/*
2222 * Print the postfix notation of the current regexp.
2223 */
2224 static void
2225nfa_postfix_dump(expr, retval)
2226 char_u *expr;
2227 int retval;
2228{
2229 int *p;
2230 FILE *f;
2231
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002232 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233 if (f != NULL)
2234 {
2235 fprintf(f, "\n-------------------------\n");
2236 if (retval == FAIL)
2237 fprintf(f, ">>> NFA engine failed ... \n");
2238 else if (retval == OK)
2239 fprintf(f, ">>> NFA engine succeeded !\n");
2240 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002241 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002242 {
2243 nfa_set_code(*p);
2244 fprintf(f, "%s, ", code);
2245 }
2246 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002247 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002248 fprintf(f, "%d ", *p);
2249 fprintf(f, "\n\n");
2250 fclose(f);
2251 }
2252}
2253
2254/*
2255 * Print the NFA starting with a root node "state".
2256 */
2257 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002258nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259 FILE *debugf;
2260 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002262 garray_T indent;
2263
2264 ga_init2(&indent, 1, 64);
2265 ga_append(&indent, '\0');
2266 nfa_print_state2(debugf, state, &indent);
2267 ga_clear(&indent);
2268}
2269
2270 static void
2271nfa_print_state2(debugf, state, indent)
2272 FILE *debugf;
2273 nfa_state_T *state;
2274 garray_T *indent;
2275{
2276 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277
2278 if (state == NULL)
2279 return;
2280
2281 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002282
2283 /* Output indent */
2284 p = (char_u *)indent->ga_data;
2285 if (indent->ga_len >= 3)
2286 {
2287 int last = indent->ga_len - 3;
2288 char_u save[2];
2289
2290 STRNCPY(save, &p[last], 2);
2291 STRNCPY(&p[last], "+-", 2);
2292 fprintf(debugf, " %s", p);
2293 STRNCPY(&p[last], save, 2);
2294 }
2295 else
2296 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002297
2298 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002299 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002300 code,
2301 state->c,
2302 abs(state->id),
2303 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 if (state->id < 0)
2305 return;
2306
2307 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002308
2309 /* grow indent for state->out */
2310 indent->ga_len -= 1;
2311 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002312 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002313 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002314 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002315 ga_append(indent, '\0');
2316
2317 nfa_print_state2(debugf, state->out, indent);
2318
2319 /* replace last part of indent for state->out1 */
2320 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002321 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002322 ga_append(indent, '\0');
2323
2324 nfa_print_state2(debugf, state->out1, indent);
2325
2326 /* shrink indent */
2327 indent->ga_len -= 3;
2328 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329}
2330
2331/*
2332 * Print the NFA state machine.
2333 */
2334 static void
2335nfa_dump(prog)
2336 nfa_regprog_T *prog;
2337{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002338 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339
2340 if (debugf != NULL)
2341 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002342 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002343
Bram Moolenaar473de612013-06-08 18:19:48 +02002344 if (prog->reganch)
2345 fprintf(debugf, "reganch: %d\n", prog->reganch);
2346 if (prog->regstart != NUL)
2347 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2348 prog->regstart, prog->regstart);
2349 if (prog->match_text != NULL)
2350 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352 fclose(debugf);
2353 }
2354}
2355#endif /* ENABLE_LOG */
2356#endif /* DEBUG */
2357
2358/*
2359 * Parse r.e. @expr and convert it into postfix form.
2360 * Return the postfix string on success, NULL otherwise.
2361 */
2362 static int *
2363re2post()
2364{
2365 if (nfa_reg(REG_NOPAREN) == FAIL)
2366 return NULL;
2367 EMIT(NFA_MOPEN);
2368 return post_start;
2369}
2370
2371/* NB. Some of the code below is inspired by Russ's. */
2372
2373/*
2374 * Represents an NFA state plus zero or one or two arrows exiting.
2375 * if c == MATCH, no arrows out; matching state.
2376 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2377 * If c < 256, labeled arrow with character c to out.
2378 */
2379
2380static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2381
2382/*
2383 * Allocate and initialize nfa_state_T.
2384 */
2385 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002386alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387 int c;
2388 nfa_state_T *out;
2389 nfa_state_T *out1;
2390{
2391 nfa_state_T *s;
2392
2393 if (istate >= nstate)
2394 return NULL;
2395
2396 s = &state_ptr[istate++];
2397
2398 s->c = c;
2399 s->out = out;
2400 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002401 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002402
2403 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002404 s->lastlist[0] = 0;
2405 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002406
2407 return s;
2408}
2409
2410/*
2411 * A partially built NFA without the matching state filled in.
2412 * Frag_T.start points at the start state.
2413 * Frag_T.out is a list of places that need to be set to the
2414 * next state for this fragment.
2415 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002416
2417/* Since the out pointers in the list are always
2418 * uninitialized, we use the pointers themselves
2419 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002420typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002421union Ptrlist
2422{
2423 Ptrlist *next;
2424 nfa_state_T *s;
2425};
2426
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002427struct Frag
2428{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002429 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 Ptrlist *out;
2431};
2432typedef struct Frag Frag_T;
2433
2434static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2435static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2436static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2437static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2438static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2439static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2440
2441/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002442 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 */
2444 static Frag_T
2445frag(start, out)
2446 nfa_state_T *start;
2447 Ptrlist *out;
2448{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002449 Frag_T n;
2450
2451 n.start = start;
2452 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002453 return n;
2454}
2455
2456/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002457 * Create singleton list containing just outp.
2458 */
2459 static Ptrlist *
2460list1(outp)
2461 nfa_state_T **outp;
2462{
2463 Ptrlist *l;
2464
2465 l = (Ptrlist *)outp;
2466 l->next = NULL;
2467 return l;
2468}
2469
2470/*
2471 * Patch the list of states at out to point to start.
2472 */
2473 static void
2474patch(l, s)
2475 Ptrlist *l;
2476 nfa_state_T *s;
2477{
2478 Ptrlist *next;
2479
2480 for (; l; l = next)
2481 {
2482 next = l->next;
2483 l->s = s;
2484 }
2485}
2486
2487
2488/*
2489 * Join the two lists l1 and l2, returning the combination.
2490 */
2491 static Ptrlist *
2492append(l1, l2)
2493 Ptrlist *l1;
2494 Ptrlist *l2;
2495{
2496 Ptrlist *oldl1;
2497
2498 oldl1 = l1;
2499 while (l1->next)
2500 l1 = l1->next;
2501 l1->next = l2;
2502 return oldl1;
2503}
2504
2505/*
2506 * Stack used for transforming postfix form into NFA.
2507 */
2508static Frag_T empty;
2509
2510 static void
2511st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002512 int *postfix UNUSED;
2513 int *end UNUSED;
2514 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002516#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002517 FILE *df;
2518 int *p2;
2519
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002520 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521 if (df)
2522 {
2523 fprintf(df, "Error popping the stack!\n");
2524#ifdef DEBUG
2525 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2526#endif
2527 fprintf(df, "Postfix form is: ");
2528#ifdef DEBUG
2529 for (p2 = postfix; p2 < end; p2++)
2530 {
2531 nfa_set_code(*p2);
2532 fprintf(df, "%s, ", code);
2533 }
2534 nfa_set_code(*p);
2535 fprintf(df, "\nCurrent position is: ");
2536 for (p2 = postfix; p2 <= p; p2 ++)
2537 {
2538 nfa_set_code(*p2);
2539 fprintf(df, "%s, ", code);
2540 }
2541#else
2542 for (p2 = postfix; p2 < end; p2++)
2543 {
2544 fprintf(df, "%d, ", *p2);
2545 }
2546 fprintf(df, "\nCurrent position is: ");
2547 for (p2 = postfix; p2 <= p; p2 ++)
2548 {
2549 fprintf(df, "%d, ", *p2);
2550 }
2551#endif
2552 fprintf(df, "\n--------------------------\n");
2553 fclose(df);
2554 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002555#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002556 EMSG(_("E874: (NFA) Could not pop the stack !"));
2557}
2558
2559/*
2560 * Push an item onto the stack.
2561 */
2562 static void
2563st_push(s, p, stack_end)
2564 Frag_T s;
2565 Frag_T **p;
2566 Frag_T *stack_end;
2567{
2568 Frag_T *stackp = *p;
2569
2570 if (stackp >= stack_end)
2571 return;
2572 *stackp = s;
2573 *p = *p + 1;
2574}
2575
2576/*
2577 * Pop an item from the stack.
2578 */
2579 static Frag_T
2580st_pop(p, stack)
2581 Frag_T **p;
2582 Frag_T *stack;
2583{
2584 Frag_T *stackp;
2585
2586 *p = *p - 1;
2587 stackp = *p;
2588 if (stackp < stack)
2589 return empty;
2590 return **p;
2591}
2592
2593/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002594 * Estimate the maximum byte length of anything matching "state".
2595 * When unknown or unlimited return -1.
2596 */
2597 static int
2598nfa_max_width(startstate, depth)
2599 nfa_state_T *startstate;
2600 int depth;
2601{
2602 int l, r;
2603 nfa_state_T *state = startstate;
2604 int len = 0;
2605
2606 /* detect looping in a NFA_SPLIT */
2607 if (depth > 4)
2608 return -1;
2609
2610 for (;;)
2611 {
2612 switch (state->c)
2613 {
2614 case NFA_END_INVISIBLE:
2615 case NFA_END_INVISIBLE_NEG:
2616 /* the end, return what we have */
2617 return len;
2618
2619 case NFA_SPLIT:
2620 /* two alternatives, use the maximum */
2621 l = nfa_max_width(state->out, depth + 1);
2622 r = nfa_max_width(state->out1, depth + 1);
2623 if (l < 0 || r < 0)
2624 return -1;
2625 return len + (l > r ? l : r);
2626
2627 case NFA_ANY:
2628 case NFA_START_COLL:
2629 case NFA_START_NEG_COLL:
2630 /* matches some character, including composing chars */
2631#ifdef FEAT_MBYTE
2632 if (enc_utf8)
2633 len += MB_MAXBYTES;
2634 else if (has_mbyte)
2635 len += 2;
2636 else
2637#endif
2638 ++len;
2639 if (state->c != NFA_ANY)
2640 {
2641 /* skip over the characters */
2642 state = state->out1->out;
2643 continue;
2644 }
2645 break;
2646
2647 case NFA_DIGIT:
2648 case NFA_WHITE:
2649 case NFA_HEX:
2650 case NFA_OCTAL:
2651 /* ascii */
2652 ++len;
2653 break;
2654
2655 case NFA_IDENT:
2656 case NFA_SIDENT:
2657 case NFA_KWORD:
2658 case NFA_SKWORD:
2659 case NFA_FNAME:
2660 case NFA_SFNAME:
2661 case NFA_PRINT:
2662 case NFA_SPRINT:
2663 case NFA_NWHITE:
2664 case NFA_NDIGIT:
2665 case NFA_NHEX:
2666 case NFA_NOCTAL:
2667 case NFA_WORD:
2668 case NFA_NWORD:
2669 case NFA_HEAD:
2670 case NFA_NHEAD:
2671 case NFA_ALPHA:
2672 case NFA_NALPHA:
2673 case NFA_LOWER:
2674 case NFA_NLOWER:
2675 case NFA_UPPER:
2676 case NFA_NUPPER:
2677 /* possibly non-ascii */
2678#ifdef FEAT_MBYTE
2679 if (has_mbyte)
2680 len += 3;
2681 else
2682#endif
2683 ++len;
2684 break;
2685
2686 case NFA_START_INVISIBLE:
2687 case NFA_START_INVISIBLE_NEG:
2688 case NFA_START_INVISIBLE_BEFORE:
2689 case NFA_START_INVISIBLE_BEFORE_NEG:
2690 /* zero-width, out1 points to the END state */
2691 state = state->out1->out;
2692 continue;
2693
2694 case NFA_BACKREF1:
2695 case NFA_BACKREF2:
2696 case NFA_BACKREF3:
2697 case NFA_BACKREF4:
2698 case NFA_BACKREF5:
2699 case NFA_BACKREF6:
2700 case NFA_BACKREF7:
2701 case NFA_BACKREF8:
2702 case NFA_BACKREF9:
2703#ifdef FEAT_SYN_HL
2704 case NFA_ZREF1:
2705 case NFA_ZREF2:
2706 case NFA_ZREF3:
2707 case NFA_ZREF4:
2708 case NFA_ZREF5:
2709 case NFA_ZREF6:
2710 case NFA_ZREF7:
2711 case NFA_ZREF8:
2712 case NFA_ZREF9:
2713#endif
2714 case NFA_NEWL:
2715 case NFA_SKIP:
2716 /* unknown width */
2717 return -1;
2718
2719 case NFA_BOL:
2720 case NFA_EOL:
2721 case NFA_BOF:
2722 case NFA_EOF:
2723 case NFA_BOW:
2724 case NFA_EOW:
2725 case NFA_MOPEN:
2726 case NFA_MOPEN1:
2727 case NFA_MOPEN2:
2728 case NFA_MOPEN3:
2729 case NFA_MOPEN4:
2730 case NFA_MOPEN5:
2731 case NFA_MOPEN6:
2732 case NFA_MOPEN7:
2733 case NFA_MOPEN8:
2734 case NFA_MOPEN9:
2735#ifdef FEAT_SYN_HL
2736 case NFA_ZOPEN:
2737 case NFA_ZOPEN1:
2738 case NFA_ZOPEN2:
2739 case NFA_ZOPEN3:
2740 case NFA_ZOPEN4:
2741 case NFA_ZOPEN5:
2742 case NFA_ZOPEN6:
2743 case NFA_ZOPEN7:
2744 case NFA_ZOPEN8:
2745 case NFA_ZOPEN9:
2746 case NFA_ZCLOSE:
2747 case NFA_ZCLOSE1:
2748 case NFA_ZCLOSE2:
2749 case NFA_ZCLOSE3:
2750 case NFA_ZCLOSE4:
2751 case NFA_ZCLOSE5:
2752 case NFA_ZCLOSE6:
2753 case NFA_ZCLOSE7:
2754 case NFA_ZCLOSE8:
2755 case NFA_ZCLOSE9:
2756#endif
2757 case NFA_MCLOSE:
2758 case NFA_MCLOSE1:
2759 case NFA_MCLOSE2:
2760 case NFA_MCLOSE3:
2761 case NFA_MCLOSE4:
2762 case NFA_MCLOSE5:
2763 case NFA_MCLOSE6:
2764 case NFA_MCLOSE7:
2765 case NFA_MCLOSE8:
2766 case NFA_MCLOSE9:
2767 case NFA_NOPEN:
2768 case NFA_NCLOSE:
2769
2770 case NFA_LNUM_GT:
2771 case NFA_LNUM_LT:
2772 case NFA_COL_GT:
2773 case NFA_COL_LT:
2774 case NFA_VCOL_GT:
2775 case NFA_VCOL_LT:
2776 case NFA_MARK_GT:
2777 case NFA_MARK_LT:
2778 case NFA_VISUAL:
2779 case NFA_LNUM:
2780 case NFA_CURSOR:
2781 case NFA_COL:
2782 case NFA_VCOL:
2783 case NFA_MARK:
2784
2785 case NFA_ZSTART:
2786 case NFA_ZEND:
2787 case NFA_OPT_CHARS:
2788 case NFA_SKIP_CHAR:
2789 case NFA_START_PATTERN:
2790 case NFA_END_PATTERN:
2791 case NFA_COMPOSING:
2792 case NFA_END_COMPOSING:
2793 /* zero-width */
2794 break;
2795
2796 default:
2797 if (state->c < 0)
2798 /* don't know what this is */
2799 return -1;
2800 /* normal character */
2801 len += MB_CHAR2LEN(state->c);
2802 break;
2803 }
2804
2805 /* normal way to continue */
2806 state = state->out;
2807 }
2808
2809 /* unrecognized */
2810 return -1;
2811}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02002812
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002813/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002814 * Convert a postfix form into its equivalent NFA.
2815 * Return the NFA start state on success, NULL otherwise.
2816 */
2817 static nfa_state_T *
2818post2nfa(postfix, end, nfa_calc_size)
2819 int *postfix;
2820 int *end;
2821 int nfa_calc_size;
2822{
2823 int *p;
2824 int mopen;
2825 int mclose;
2826 Frag_T *stack = NULL;
2827 Frag_T *stackp = NULL;
2828 Frag_T *stack_end = NULL;
2829 Frag_T e1;
2830 Frag_T e2;
2831 Frag_T e;
2832 nfa_state_T *s;
2833 nfa_state_T *s1;
2834 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002835 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836
2837 if (postfix == NULL)
2838 return NULL;
2839
Bram Moolenaar053bb602013-05-20 13:55:21 +02002840#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002841#define POP() st_pop(&stackp, stack); \
2842 if (stackp < stack) \
2843 { \
2844 st_error(postfix, end, p); \
2845 return NULL; \
2846 }
2847
2848 if (nfa_calc_size == FALSE)
2849 {
2850 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002851 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002852 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002853 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854 }
2855
2856 for (p = postfix; p < end; ++p)
2857 {
2858 switch (*p)
2859 {
2860 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002861 /* Concatenation.
2862 * Pay attention: this operator does not exist in the r.e. itself
2863 * (it is implicit, really). It is added when r.e. is translated
2864 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002865 if (nfa_calc_size == TRUE)
2866 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002867 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002868 break;
2869 }
2870 e2 = POP();
2871 e1 = POP();
2872 patch(e1.out, e2.start);
2873 PUSH(frag(e1.start, e2.out));
2874 break;
2875
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876 case NFA_OR:
2877 /* Alternation */
2878 if (nfa_calc_size == TRUE)
2879 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002880 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881 break;
2882 }
2883 e2 = POP();
2884 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002885 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002887 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888 PUSH(frag(s, append(e1.out, e2.out)));
2889 break;
2890
2891 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002892 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 if (nfa_calc_size == TRUE)
2894 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002895 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 break;
2897 }
2898 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002899 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002900 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002901 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002902 patch(e.out, s);
2903 PUSH(frag(s, list1(&s->out1)));
2904 break;
2905
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002906 case NFA_STAR_NONGREEDY:
2907 /* Zero or more, prefer zero */
2908 if (nfa_calc_size == TRUE)
2909 {
2910 nstate++;
2911 break;
2912 }
2913 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002914 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002915 if (s == NULL)
2916 goto theend;
2917 patch(e.out, s);
2918 PUSH(frag(s, list1(&s->out)));
2919 break;
2920
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921 case NFA_QUEST:
2922 /* one or zero atoms=> greedy match */
2923 if (nfa_calc_size == TRUE)
2924 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002925 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 break;
2927 }
2928 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002929 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002930 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002931 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002932 PUSH(frag(s, append(e.out, list1(&s->out1))));
2933 break;
2934
2935 case NFA_QUEST_NONGREEDY:
2936 /* zero or one atoms => non-greedy match */
2937 if (nfa_calc_size == TRUE)
2938 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002939 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 break;
2941 }
2942 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002943 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002944 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002945 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002946 PUSH(frag(s, append(e.out, list1(&s->out))));
2947 break;
2948
Bram Moolenaar417bad22013-06-07 14:08:30 +02002949 case NFA_END_COLL:
2950 case NFA_END_NEG_COLL:
2951 /* On the stack is the sequence starting with NFA_START_COLL or
2952 * NFA_START_NEG_COLL and all possible characters. Patch it to
2953 * add the output to the start. */
2954 if (nfa_calc_size == TRUE)
2955 {
2956 nstate++;
2957 break;
2958 }
2959 e = POP();
2960 s = alloc_state(NFA_END_COLL, NULL, NULL);
2961 if (s == NULL)
2962 goto theend;
2963 patch(e.out, s);
2964 e.start->out1 = s;
2965 PUSH(frag(e.start, list1(&s->out)));
2966 break;
2967
2968 case NFA_RANGE:
2969 /* Before this are two characters, the low and high end of a
2970 * range. Turn them into two states with MIN and MAX. */
2971 if (nfa_calc_size == TRUE)
2972 {
2973 /* nstate += 0; */
2974 break;
2975 }
2976 e2 = POP();
2977 e1 = POP();
2978 e2.start->val = e2.start->c;
2979 e2.start->c = NFA_RANGE_MAX;
2980 e1.start->val = e1.start->c;
2981 e1.start->c = NFA_RANGE_MIN;
2982 patch(e1.out, e2.start);
2983 PUSH(frag(e1.start, e2.out));
2984 break;
2985
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002986 case NFA_SKIP_CHAR:
2987 /* Symbol of 0-length, Used in a repetition
2988 * with max/min count of 0 */
2989 if (nfa_calc_size == TRUE)
2990 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002991 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992 break;
2993 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002994 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002995 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002996 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002997 PUSH(frag(s, list1(&s->out)));
2998 break;
2999
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003000 case NFA_OPT_CHARS:
3001 {
3002 int n;
3003
3004 /* \%[abc] */
3005 n = *++p; /* get number of characters */
3006 if (nfa_calc_size == TRUE)
3007 {
3008 nstate += n;
3009 break;
3010 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003011 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003012 e1.out = NULL; /* stores list with out1's */
3013 s1 = NULL; /* previous NFA_SPLIT to connect to */
3014 while (n-- > 0)
3015 {
3016 e = POP(); /* get character */
3017 s = alloc_state(NFA_SPLIT, e.start, NULL);
3018 if (s == NULL)
3019 goto theend;
3020 if (e1.out == NULL)
3021 e1 = e;
3022 patch(e.out, s1);
3023 append(e1.out, list1(&s->out1));
3024 s1 = s;
3025 }
3026 PUSH(frag(s, e1.out));
3027 break;
3028 }
3029
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003030 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003031 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003032 case NFA_PREV_ATOM_JUST_BEFORE:
3033 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003034 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003035 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003036 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3037 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003038 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003039 int start_state;
3040 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003041 int n = 0;
3042 nfa_state_T *zend;
3043 nfa_state_T *skip;
3044
Bram Moolenaardecd9542013-06-07 16:31:50 +02003045 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003046 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003047 case NFA_PREV_ATOM_NO_WIDTH:
3048 start_state = NFA_START_INVISIBLE;
3049 end_state = NFA_END_INVISIBLE;
3050 break;
3051 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3052 start_state = NFA_START_INVISIBLE_NEG;
3053 end_state = NFA_END_INVISIBLE_NEG;
3054 break;
3055 case NFA_PREV_ATOM_JUST_BEFORE:
3056 start_state = NFA_START_INVISIBLE_BEFORE;
3057 end_state = NFA_END_INVISIBLE;
3058 break;
3059 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3060 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3061 end_state = NFA_END_INVISIBLE_NEG;
3062 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003063 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003064 start_state = NFA_START_PATTERN;
3065 end_state = NFA_END_PATTERN;
3066 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003067 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003068
3069 if (before)
3070 n = *++p; /* get the count */
3071
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003072 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003073 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003074 * The \@<= operator: match for the preceding atom.
3075 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003076 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003077 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003078
3079 if (nfa_calc_size == TRUE)
3080 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003081 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003082 break;
3083 }
3084 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003085 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003086 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003087 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003088
Bram Moolenaar87953742013-06-05 18:52:40 +02003089 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003090 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003091 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003092 if (pattern)
3093 {
3094 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3095 skip = alloc_state(NFA_SKIP, NULL, NULL);
3096 zend = alloc_state(NFA_ZEND, s1, NULL);
3097 s1->out= skip;
3098 patch(e.out, zend);
3099 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003100 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003101 else
3102 {
3103 patch(e.out, s1);
3104 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003105 if (before)
3106 {
3107 if (n <= 0)
3108 /* See if we can guess the maximum width, it avoids a
3109 * lot of pointless tries. */
3110 n = nfa_max_width(e.start, 0);
3111 s->val = n; /* store the count */
3112 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003113 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003115 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003116
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003117#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003118 case NFA_COMPOSING: /* char with composing char */
3119#if 0
3120 /* TODO */
3121 if (regflags & RF_ICOMBINE)
3122 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003123 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003124 }
3125#endif
3126 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003127#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003128
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003129 case NFA_MOPEN: /* \( \) Submatch */
3130 case NFA_MOPEN1:
3131 case NFA_MOPEN2:
3132 case NFA_MOPEN3:
3133 case NFA_MOPEN4:
3134 case NFA_MOPEN5:
3135 case NFA_MOPEN6:
3136 case NFA_MOPEN7:
3137 case NFA_MOPEN8:
3138 case NFA_MOPEN9:
3139#ifdef FEAT_SYN_HL
3140 case NFA_ZOPEN: /* \z( \) Submatch */
3141 case NFA_ZOPEN1:
3142 case NFA_ZOPEN2:
3143 case NFA_ZOPEN3:
3144 case NFA_ZOPEN4:
3145 case NFA_ZOPEN5:
3146 case NFA_ZOPEN6:
3147 case NFA_ZOPEN7:
3148 case NFA_ZOPEN8:
3149 case NFA_ZOPEN9:
3150#endif
3151 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003152 if (nfa_calc_size == TRUE)
3153 {
3154 nstate += 2;
3155 break;
3156 }
3157
3158 mopen = *p;
3159 switch (*p)
3160 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003161 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3162#ifdef FEAT_SYN_HL
3163 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3164 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3165 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3166 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3167 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3168 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3169 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3170 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3171 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3172 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3173#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003174#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003175 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003176#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003177 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003178 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 mclose = *p + NSUBEXP;
3180 break;
3181 }
3182
3183 /* Allow "NFA_MOPEN" as a valid postfix representation for
3184 * the empty regexp "". In this case, the NFA will be
3185 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3186 * empty groups of parenthesis, and empty mbyte chars */
3187 if (stackp == stack)
3188 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003189 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003190 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003191 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003192 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003194 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 patch(list1(&s->out), s1);
3196 PUSH(frag(s, list1(&s1->out)));
3197 break;
3198 }
3199
3200 /* At least one node was emitted before NFA_MOPEN, so
3201 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3202 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003203 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003205 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206
Bram Moolenaar525666f2013-06-02 16:40:55 +02003207 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003208 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003209 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003210 patch(e.out, s1);
3211
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003212#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003213 if (mopen == NFA_COMPOSING)
3214 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003215 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003216#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217
3218 PUSH(frag(s, list1(&s1->out)));
3219 break;
3220
Bram Moolenaar5714b802013-05-28 22:03:20 +02003221 case NFA_BACKREF1:
3222 case NFA_BACKREF2:
3223 case NFA_BACKREF3:
3224 case NFA_BACKREF4:
3225 case NFA_BACKREF5:
3226 case NFA_BACKREF6:
3227 case NFA_BACKREF7:
3228 case NFA_BACKREF8:
3229 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003230#ifdef FEAT_SYN_HL
3231 case NFA_ZREF1:
3232 case NFA_ZREF2:
3233 case NFA_ZREF3:
3234 case NFA_ZREF4:
3235 case NFA_ZREF5:
3236 case NFA_ZREF6:
3237 case NFA_ZREF7:
3238 case NFA_ZREF8:
3239 case NFA_ZREF9:
3240#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003241 if (nfa_calc_size == TRUE)
3242 {
3243 nstate += 2;
3244 break;
3245 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003246 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003247 if (s == NULL)
3248 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003249 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003250 if (s1 == NULL)
3251 goto theend;
3252 patch(list1(&s->out), s1);
3253 PUSH(frag(s, list1(&s1->out)));
3254 break;
3255
Bram Moolenaar423532e2013-05-29 21:14:42 +02003256 case NFA_LNUM:
3257 case NFA_LNUM_GT:
3258 case NFA_LNUM_LT:
3259 case NFA_VCOL:
3260 case NFA_VCOL_GT:
3261 case NFA_VCOL_LT:
3262 case NFA_COL:
3263 case NFA_COL_GT:
3264 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003265 case NFA_MARK:
3266 case NFA_MARK_GT:
3267 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003268 {
3269 int n = *++p; /* lnum, col or mark name */
3270
Bram Moolenaar423532e2013-05-29 21:14:42 +02003271 if (nfa_calc_size == TRUE)
3272 {
3273 nstate += 1;
3274 break;
3275 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003276 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003277 if (s == NULL)
3278 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003279 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003280 PUSH(frag(s, list1(&s->out)));
3281 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003282 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003283
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 case NFA_ZSTART:
3285 case NFA_ZEND:
3286 default:
3287 /* Operands */
3288 if (nfa_calc_size == TRUE)
3289 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003290 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 break;
3292 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003293 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003295 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 PUSH(frag(s, list1(&s->out)));
3297 break;
3298
3299 } /* switch(*p) */
3300
3301 } /* for(p = postfix; *p; ++p) */
3302
3303 if (nfa_calc_size == TRUE)
3304 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003305 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003306 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307 }
3308
3309 e = POP();
3310 if (stackp != stack)
3311 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3312
3313 if (istate >= nstate)
3314 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3315
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 matchstate = &state_ptr[istate++]; /* the match state */
3317 matchstate->c = NFA_MATCH;
3318 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003319 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320
3321 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003322 ret = e.start;
3323
3324theend:
3325 vim_free(stack);
3326 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003327
3328#undef POP1
3329#undef PUSH1
3330#undef POP2
3331#undef PUSH2
3332#undef POP
3333#undef PUSH
3334}
3335
Bram Moolenaara2947e22013-06-11 22:44:09 +02003336/*
3337 * After building the NFA program, inspect it to add optimization hints.
3338 */
3339 static void
3340nfa_postprocess(prog)
3341 nfa_regprog_T *prog;
3342{
3343 int i;
3344 int c;
3345
3346 for (i = 0; i < prog->nstate; ++i)
3347 {
3348 c = prog->state[i].c;
3349 if (c == NFA_START_INVISIBLE
3350 || c == NFA_START_INVISIBLE_NEG
3351 || c == NFA_START_INVISIBLE_BEFORE
3352 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3353 {
3354 int directly;
3355
3356 /* Do it directly when what follows is possibly the end of the
3357 * match. */
3358 if (match_follows(prog->state[i].out1->out, 0))
3359 directly = TRUE;
3360 else
3361 {
3362 int ch_invisible = failure_chance(prog->state[i].out, 0);
3363 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3364
3365 /* Postpone when the invisible match is expensive or has a
3366 * lower chance of failing. */
3367 if (c == NFA_START_INVISIBLE_BEFORE
3368 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3369 {
3370 /* "before" matches are very expensive when
3371 * unbounded, always prefer what follows then,
3372 * unless what follows will always match.
3373 * Otherwise strongly prefer what follows. */
3374 if (prog->state[i].val <= 0 && ch_follows > 0)
3375 directly = FALSE;
3376 else
3377 directly = ch_follows * 10 < ch_invisible;
3378 }
3379 else
3380 {
3381 /* normal invisible, first do the one with the
3382 * highest failure chance */
3383 directly = ch_follows < ch_invisible;
3384 }
3385 }
3386 if (directly)
3387 /* switch to the _FIRST state */
3388 ++prog->state[i].c;
3389 }
3390 }
3391}
3392
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003393/****************************************************************
3394 * NFA execution code.
3395 ****************************************************************/
3396
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003397typedef struct
3398{
3399 int in_use; /* number of subexpr with useful info */
3400
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003401 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003402 union
3403 {
3404 struct multipos
3405 {
3406 lpos_T start;
3407 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003408 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003409 struct linepos
3410 {
3411 char_u *start;
3412 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003413 } line[NSUBEXP];
3414 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003415} regsub_T;
3416
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003417typedef struct
3418{
3419 regsub_T norm; /* \( .. \) matches */
3420#ifdef FEAT_SYN_HL
3421 regsub_T synt; /* \z( .. \) matches */
3422#endif
3423} regsubs_T;
3424
Bram Moolenaara2d95102013-06-04 14:23:05 +02003425/* nfa_pim_T stores a Postponed Invisible Match. */
3426typedef struct nfa_pim_S nfa_pim_T;
3427struct nfa_pim_S
3428{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003429 int result; /* NFA_PIM_*, see below */
3430 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003431 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003432 union
3433 {
3434 lpos_T pos;
3435 char_u *ptr;
3436 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003437};
3438
3439/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003440#define NFA_PIM_UNUSED 0 /* pim not used */
3441#define NFA_PIM_TODO 1 /* pim not done yet */
3442#define NFA_PIM_MATCH 2 /* pim executed, matches */
3443#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003444
3445
Bram Moolenaar963fee22013-05-26 21:47:28 +02003446/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003447typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448{
3449 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003450 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003451 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3452 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003453 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003454} nfa_thread_T;
3455
Bram Moolenaar963fee22013-05-26 21:47:28 +02003456/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003457typedef struct
3458{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003459 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003460 int n; /* nr of states currently in "t" */
3461 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003462 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003463} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464
Bram Moolenaar5714b802013-05-28 22:03:20 +02003465#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003466static void log_subsexpr __ARGS((regsubs_T *subs));
3467static void log_subexpr __ARGS((regsub_T *sub));
3468
3469 static void
3470log_subsexpr(subs)
3471 regsubs_T *subs;
3472{
3473 log_subexpr(&subs->norm);
3474# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003475 if (nfa_has_zsubexpr)
3476 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003477# endif
3478}
3479
Bram Moolenaar5714b802013-05-28 22:03:20 +02003480 static void
3481log_subexpr(sub)
3482 regsub_T *sub;
3483{
3484 int j;
3485
3486 for (j = 0; j < sub->in_use; j++)
3487 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003488 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003489 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003490 sub->list.multi[j].start.col,
3491 (int)sub->list.multi[j].start.lnum,
3492 sub->list.multi[j].end.col,
3493 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003494 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003495 {
3496 char *s = (char *)sub->list.line[j].start;
3497 char *e = (char *)sub->list.line[j].end;
3498
Bram Moolenaar87953742013-06-05 18:52:40 +02003499 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003500 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003501 s == NULL ? "NULL" : s,
3502 e == NULL ? "NULL" : e);
3503 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003504}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003505
3506 static char *
3507pim_info(nfa_pim_T *pim)
3508{
3509 static char buf[30];
3510
3511 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3512 buf[0] = NUL;
3513 else
3514 {
3515 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3516 : (int)(pim->end.ptr - reginput));
3517 }
3518 return buf;
3519}
3520
Bram Moolenaar5714b802013-05-28 22:03:20 +02003521#endif
3522
Bram Moolenaar963fee22013-05-26 21:47:28 +02003523/* Used during execution: whether a match has been found. */
3524static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003525
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003526static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003527static void clear_sub __ARGS((regsub_T *sub));
3528static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3529static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003530static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003531static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
3532static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003533static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003534static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003535
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003536/*
3537 * Copy postponed invisible match info from "from" to "to".
3538 */
3539 static void
3540copy_pim(to, from)
3541 nfa_pim_T *to;
3542 nfa_pim_T *from;
3543{
3544 to->result = from->result;
3545 to->state = from->state;
3546 copy_sub(&to->subs.norm, &from->subs.norm);
3547#ifdef FEAT_SYN_HL
3548 if (nfa_has_zsubexpr)
3549 copy_sub(&to->subs.synt, &from->subs.synt);
3550#endif
3551 to->end = from->end;
3552}
3553
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003554 static void
3555clear_sub(sub)
3556 regsub_T *sub;
3557{
3558 if (REG_MULTI)
3559 /* Use 0xff to set lnum to -1 */
3560 vim_memset(sub->list.multi, 0xff,
3561 sizeof(struct multipos) * nfa_nsubexpr);
3562 else
3563 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3564 sub->in_use = 0;
3565}
3566
3567/*
3568 * Copy the submatches from "from" to "to".
3569 */
3570 static void
3571copy_sub(to, from)
3572 regsub_T *to;
3573 regsub_T *from;
3574{
3575 to->in_use = from->in_use;
3576 if (from->in_use > 0)
3577 {
3578 /* Copy the match start and end positions. */
3579 if (REG_MULTI)
3580 mch_memmove(&to->list.multi[0],
3581 &from->list.multi[0],
3582 sizeof(struct multipos) * from->in_use);
3583 else
3584 mch_memmove(&to->list.line[0],
3585 &from->list.line[0],
3586 sizeof(struct linepos) * from->in_use);
3587 }
3588}
3589
3590/*
3591 * Like copy_sub() but exclude the main match.
3592 */
3593 static void
3594copy_sub_off(to, from)
3595 regsub_T *to;
3596 regsub_T *from;
3597{
3598 if (to->in_use < from->in_use)
3599 to->in_use = from->in_use;
3600 if (from->in_use > 1)
3601 {
3602 /* Copy the match start and end positions. */
3603 if (REG_MULTI)
3604 mch_memmove(&to->list.multi[1],
3605 &from->list.multi[1],
3606 sizeof(struct multipos) * (from->in_use - 1));
3607 else
3608 mch_memmove(&to->list.line[1],
3609 &from->list.line[1],
3610 sizeof(struct linepos) * (from->in_use - 1));
3611 }
3612}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613
Bram Moolenaar428e9872013-05-30 17:05:39 +02003614/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003615 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003616 */
3617 static int
3618sub_equal(sub1, sub2)
3619 regsub_T *sub1;
3620 regsub_T *sub2;
3621{
3622 int i;
3623 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003624 linenr_T s1;
3625 linenr_T s2;
3626 char_u *sp1;
3627 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003628
3629 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3630 if (REG_MULTI)
3631 {
3632 for (i = 0; i < todo; ++i)
3633 {
3634 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003635 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003636 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003637 s1 = 0;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003638 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003639 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003640 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003641 s2 = 0;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003642 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003643 return FALSE;
3644 if (s1 != 0 && sub1->list.multi[i].start.col
3645 != sub2->list.multi[i].start.col)
3646 return FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003647 }
3648 }
3649 else
3650 {
3651 for (i = 0; i < todo; ++i)
3652 {
3653 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003654 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003655 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003656 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003657 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003658 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003659 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003660 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003661 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003662 return FALSE;
3663 }
3664 }
3665
3666 return TRUE;
3667}
3668
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003669#ifdef ENABLE_LOG
3670 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003671report_state(char *action,
3672 regsub_T *sub,
3673 nfa_state_T *state,
3674 int lid,
3675 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003676{
3677 int col;
3678
3679 if (sub->in_use <= 0)
3680 col = -1;
3681 else if (REG_MULTI)
3682 col = sub->list.multi[0].start.col;
3683 else
3684 col = (int)(sub->list.line[0].start - regline);
3685 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003686 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3687 action, abs(state->id), lid, state->c, code, col,
3688 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003689}
3690#endif
3691
Bram Moolenaar43e02982013-06-07 17:31:29 +02003692/*
3693 * Return TRUE if the same state is already in list "l" with the same
3694 * positions as "subs".
3695 */
3696 static int
3697has_state_with_pos(l, state, subs)
3698 nfa_list_T *l; /* runtime state list */
3699 nfa_state_T *state; /* state to update */
3700 regsubs_T *subs; /* pointers to subexpressions */
3701{
3702 nfa_thread_T *thread;
3703 int i;
3704
3705 for (i = 0; i < l->n; ++i)
3706 {
3707 thread = &l->t[i];
3708 if (thread->state->id == state->id
3709 && sub_equal(&thread->subs.norm, &subs->norm)
3710#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003711 && (!nfa_has_zsubexpr
3712 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003713#endif
3714 )
3715 return TRUE;
3716 }
3717 return FALSE;
3718}
3719
3720/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003721 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3722 */
3723 static int
3724match_follows(startstate, depth)
3725 nfa_state_T *startstate;
3726 int depth;
3727{
3728 nfa_state_T *state = startstate;
3729
3730 /* avoid too much recursion */
3731 if (depth > 10)
3732 return FALSE;
3733
3734 for (;;)
3735 {
3736 switch (state->c)
3737 {
3738 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003739 case NFA_MCLOSE:
3740 case NFA_END_INVISIBLE:
3741 case NFA_END_INVISIBLE_NEG:
3742 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003743 return TRUE;
3744
3745 case NFA_SPLIT:
3746 return match_follows(state->out, depth + 1)
3747 || match_follows(state->out1, depth + 1);
3748
3749 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003750 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003751 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003752 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003753 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003754 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003755 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003756 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003757 case NFA_COMPOSING:
3758 /* skip ahead to next state */
3759 state = state->out1->out;
3760 break;
3761
3762 case NFA_ANY:
3763 case NFA_IDENT:
3764 case NFA_SIDENT:
3765 case NFA_KWORD:
3766 case NFA_SKWORD:
3767 case NFA_FNAME:
3768 case NFA_SFNAME:
3769 case NFA_PRINT:
3770 case NFA_SPRINT:
3771 case NFA_WHITE:
3772 case NFA_NWHITE:
3773 case NFA_DIGIT:
3774 case NFA_NDIGIT:
3775 case NFA_HEX:
3776 case NFA_NHEX:
3777 case NFA_OCTAL:
3778 case NFA_NOCTAL:
3779 case NFA_WORD:
3780 case NFA_NWORD:
3781 case NFA_HEAD:
3782 case NFA_NHEAD:
3783 case NFA_ALPHA:
3784 case NFA_NALPHA:
3785 case NFA_LOWER:
3786 case NFA_NLOWER:
3787 case NFA_UPPER:
3788 case NFA_NUPPER:
3789 case NFA_START_COLL:
3790 case NFA_START_NEG_COLL:
3791 case NFA_NEWL:
3792 /* state will advance input */
3793 return FALSE;
3794
3795 default:
3796 if (state->c > 0)
3797 /* state will advance input */
3798 return FALSE;
3799
3800 /* Others: zero-width or possibly zero-width, might still find
3801 * a match at the same position, keep looking. */
3802 break;
3803 }
3804 state = state->out;
3805 }
3806 return FALSE;
3807}
3808
3809
3810/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003811 * Return TRUE if "state" is already in list "l".
3812 */
3813 static int
3814state_in_list(l, state, subs)
3815 nfa_list_T *l; /* runtime state list */
3816 nfa_state_T *state; /* state to update */
3817 regsubs_T *subs; /* pointers to subexpressions */
3818{
3819 if (state->lastlist[nfa_ll_index] == l->id)
3820 {
3821 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3822 return TRUE;
3823 }
3824 return FALSE;
3825}
3826
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003827 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003828addstate(l, state, subs, pim, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003829 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003830 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003831 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003832 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003833 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003834{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003835 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003836 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003837 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003838 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003839 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003840 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003841 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003842#ifdef ENABLE_LOG
3843 int did_print = FALSE;
3844#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003845
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846 switch (state->c)
3847 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003848 case NFA_NCLOSE:
3849 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003850 case NFA_MCLOSE1:
3851 case NFA_MCLOSE2:
3852 case NFA_MCLOSE3:
3853 case NFA_MCLOSE4:
3854 case NFA_MCLOSE5:
3855 case NFA_MCLOSE6:
3856 case NFA_MCLOSE7:
3857 case NFA_MCLOSE8:
3858 case NFA_MCLOSE9:
3859#ifdef FEAT_SYN_HL
3860 case NFA_ZCLOSE:
3861 case NFA_ZCLOSE1:
3862 case NFA_ZCLOSE2:
3863 case NFA_ZCLOSE3:
3864 case NFA_ZCLOSE4:
3865 case NFA_ZCLOSE5:
3866 case NFA_ZCLOSE6:
3867 case NFA_ZCLOSE7:
3868 case NFA_ZCLOSE8:
3869 case NFA_ZCLOSE9:
3870#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003871 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003872 case NFA_SPLIT:
3873 case NFA_NOPEN:
3874 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003875 /* These nodes are not added themselves but their "out" and/or
3876 * "out1" may be added below. */
3877 break;
3878
3879 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003880 case NFA_MOPEN1:
3881 case NFA_MOPEN2:
3882 case NFA_MOPEN3:
3883 case NFA_MOPEN4:
3884 case NFA_MOPEN5:
3885 case NFA_MOPEN6:
3886 case NFA_MOPEN7:
3887 case NFA_MOPEN8:
3888 case NFA_MOPEN9:
3889#ifdef FEAT_SYN_HL
3890 case NFA_ZOPEN:
3891 case NFA_ZOPEN1:
3892 case NFA_ZOPEN2:
3893 case NFA_ZOPEN3:
3894 case NFA_ZOPEN4:
3895 case NFA_ZOPEN5:
3896 case NFA_ZOPEN6:
3897 case NFA_ZOPEN7:
3898 case NFA_ZOPEN8:
3899 case NFA_ZOPEN9:
3900#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003901 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003902 /* These nodes do not need to be added, but we need to bail out
3903 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003904 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003905 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003906 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003907 break;
3908
Bram Moolenaar307aa162013-06-02 16:34:21 +02003909 case NFA_BOL:
3910 case NFA_BOF:
3911 /* "^" won't match past end-of-line, don't bother trying.
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003912 * Except when at the end of the line, or when we are going to the
3913 * next line for a look-behind match. */
Bram Moolenaar307aa162013-06-02 16:34:21 +02003914 if (reginput > regline
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003915 && *reginput != NUL
Bram Moolenaar307aa162013-06-02 16:34:21 +02003916 && (nfa_endp == NULL
3917 || !REG_MULTI
3918 || reglnum == nfa_endp->se_u.pos.lnum))
3919 goto skip_add;
3920 /* FALLTHROUGH */
3921
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003923 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003924 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003925 /* This state is already in the list, don't add it again,
3926 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003927 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003928 {
3929skip_add:
3930#ifdef ENABLE_LOG
3931 nfa_set_code(state->c);
3932 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3933 abs(state->id), l->id, state->c, code);
3934#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003935 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003936 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003937
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003938 /* Do not add the state again when it exists with the same
3939 * positions. */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003940 if (has_state_with_pos(l, state, subs))
3941 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003942 }
3943
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003944 /* When there are backreferences the number of states may be (a
3945 * lot) bigger than anticipated. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003946 if (nfa_has_backref && l->n == l->len)
3947 {
3948 int newlen = l->len * 3 / 2 + 50;
3949
3950 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3951 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003952 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003953
3954 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003955 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003956 thread = &l->t[l->n++];
3957 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003958 if (pim == NULL)
3959 thread->pim.result = NFA_PIM_UNUSED;
3960 else
3961 copy_pim(&thread->pim, pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003962 copy_sub(&thread->subs.norm, &subs->norm);
3963#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003964 if (nfa_has_zsubexpr)
3965 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003966#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003967#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003968 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003969 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003970#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003971 }
3972
3973#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003974 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003975 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003976#endif
3977 switch (state->c)
3978 {
3979 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003980 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003981 break;
3982
3983 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003984 /* order matters here */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003985 addstate(l, state->out, subs, pim, off);
3986 addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003987 break;
3988
Bram Moolenaar5714b802013-05-28 22:03:20 +02003989 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003990 case NFA_NOPEN:
3991 case NFA_NCLOSE:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003992 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003993 break;
3994
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003995 case NFA_MOPEN:
3996 case NFA_MOPEN1:
3997 case NFA_MOPEN2:
3998 case NFA_MOPEN3:
3999 case NFA_MOPEN4:
4000 case NFA_MOPEN5:
4001 case NFA_MOPEN6:
4002 case NFA_MOPEN7:
4003 case NFA_MOPEN8:
4004 case NFA_MOPEN9:
4005#ifdef FEAT_SYN_HL
4006 case NFA_ZOPEN:
4007 case NFA_ZOPEN1:
4008 case NFA_ZOPEN2:
4009 case NFA_ZOPEN3:
4010 case NFA_ZOPEN4:
4011 case NFA_ZOPEN5:
4012 case NFA_ZOPEN6:
4013 case NFA_ZOPEN7:
4014 case NFA_ZOPEN8:
4015 case NFA_ZOPEN9:
4016#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004017 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004018 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004019 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004020 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004021 sub = &subs->norm;
4022 }
4023#ifdef FEAT_SYN_HL
4024 else if (state->c >= NFA_ZOPEN)
4025 {
4026 subidx = state->c - NFA_ZOPEN;
4027 sub = &subs->synt;
4028 }
4029#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004030 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004031 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004032 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004033 sub = &subs->norm;
4034 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004035
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004036 /* Set the position (with "off" added) in the subexpression. Save
4037 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02004038 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004039 if (REG_MULTI)
4040 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004041 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004042 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004043 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004044 save_in_use = -1;
4045 }
4046 else
4047 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004048 save_in_use = sub->in_use;
4049 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004050 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004051 sub->list.multi[i].start.lnum = -1;
4052 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004053 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004054 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004055 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004056 if (off == -1)
4057 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004058 sub->list.multi[subidx].start.lnum = reglnum + 1;
4059 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004060 }
4061 else
4062 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004063 sub->list.multi[subidx].start.lnum = reglnum;
4064 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004065 (colnr_T)(reginput - regline + off);
4066 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067 }
4068 else
4069 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004070 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004071 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004072 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004073 save_in_use = -1;
4074 }
4075 else
4076 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004077 save_in_use = sub->in_use;
4078 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004079 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004080 sub->list.line[i].start = NULL;
4081 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004082 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004083 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004084 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004085 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004086 }
4087
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004088 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004090 if (save_in_use == -1)
4091 {
4092 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004093 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004094 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004095 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004096 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004097 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004098 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004099 break;
4100
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004101 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004102 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004103 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004104 /* Do not overwrite the position set by \ze. If no \ze
4105 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004106 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004107 break;
4108 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004109 case NFA_MCLOSE1:
4110 case NFA_MCLOSE2:
4111 case NFA_MCLOSE3:
4112 case NFA_MCLOSE4:
4113 case NFA_MCLOSE5:
4114 case NFA_MCLOSE6:
4115 case NFA_MCLOSE7:
4116 case NFA_MCLOSE8:
4117 case NFA_MCLOSE9:
4118#ifdef FEAT_SYN_HL
4119 case NFA_ZCLOSE:
4120 case NFA_ZCLOSE1:
4121 case NFA_ZCLOSE2:
4122 case NFA_ZCLOSE3:
4123 case NFA_ZCLOSE4:
4124 case NFA_ZCLOSE5:
4125 case NFA_ZCLOSE6:
4126 case NFA_ZCLOSE7:
4127 case NFA_ZCLOSE8:
4128 case NFA_ZCLOSE9:
4129#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004130 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004132 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004133 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004134 sub = &subs->norm;
4135 }
4136#ifdef FEAT_SYN_HL
4137 else if (state->c >= NFA_ZCLOSE)
4138 {
4139 subidx = state->c - NFA_ZCLOSE;
4140 sub = &subs->synt;
4141 }
4142#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004143 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004144 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004145 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004146 sub = &subs->norm;
4147 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004148
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004149 /* We don't fill in gaps here, there must have been an MOPEN that
4150 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004151 save_in_use = sub->in_use;
4152 if (sub->in_use <= subidx)
4153 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004154 if (REG_MULTI)
4155 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004156 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004157 if (off == -1)
4158 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004159 sub->list.multi[subidx].end.lnum = reglnum + 1;
4160 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004161 }
4162 else
4163 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004164 sub->list.multi[subidx].end.lnum = reglnum;
4165 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004166 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004167 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004168 }
4169 else
4170 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004171 save_ptr = sub->list.line[subidx].end;
4172 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004173 }
4174
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004175 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004176
4177 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004178 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004179 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004180 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004181 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182 break;
4183 }
4184}
4185
4186/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004187 * Like addstate(), but the new state(s) are put at position "*ip".
4188 * Used for zero-width matches, next state to use is the added one.
4189 * This makes sure the order of states to be tried does not change, which
4190 * matters for alternatives.
4191 */
4192 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004193addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004194 nfa_list_T *l; /* runtime state list */
4195 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004196 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004197 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004198 int *ip;
4199{
4200 int tlen = l->n;
4201 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004202 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004203
4204 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004205 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004206
Bram Moolenaar4b417062013-05-25 20:19:50 +02004207 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004208 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004209 return;
4210
4211 /* re-order to put the new state at the current position */
4212 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004213 if (count == 1)
4214 {
4215 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004216 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004217 }
4218 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004219 {
4220 /* make space for new states, then move them from the
4221 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004222 mch_memmove(&(l->t[listidx + count]),
4223 &(l->t[listidx + 1]),
4224 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4225 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02004226 &(l->t[l->n - 1]),
4227 sizeof(nfa_thread_T) * count);
4228 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004229 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004230 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004231}
4232
4233/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004234 * Check character class "class" against current character c.
4235 */
4236 static int
4237check_char_class(class, c)
4238 int class;
4239 int c;
4240{
4241 switch (class)
4242 {
4243 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004244 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004245 return OK;
4246 break;
4247 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004248 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004249 return OK;
4250 break;
4251 case NFA_CLASS_BLANK:
4252 if (c == ' ' || c == '\t')
4253 return OK;
4254 break;
4255 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004256 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257 return OK;
4258 break;
4259 case NFA_CLASS_DIGIT:
4260 if (VIM_ISDIGIT(c))
4261 return OK;
4262 break;
4263 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004264 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004265 return OK;
4266 break;
4267 case NFA_CLASS_LOWER:
4268 if (MB_ISLOWER(c))
4269 return OK;
4270 break;
4271 case NFA_CLASS_PRINT:
4272 if (vim_isprintc(c))
4273 return OK;
4274 break;
4275 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004276 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004277 return OK;
4278 break;
4279 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004280 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004281 return OK;
4282 break;
4283 case NFA_CLASS_UPPER:
4284 if (MB_ISUPPER(c))
4285 return OK;
4286 break;
4287 case NFA_CLASS_XDIGIT:
4288 if (vim_isxdigit(c))
4289 return OK;
4290 break;
4291 case NFA_CLASS_TAB:
4292 if (c == '\t')
4293 return OK;
4294 break;
4295 case NFA_CLASS_RETURN:
4296 if (c == '\r')
4297 return OK;
4298 break;
4299 case NFA_CLASS_BACKSPACE:
4300 if (c == '\b')
4301 return OK;
4302 break;
4303 case NFA_CLASS_ESCAPE:
4304 if (c == '\033')
4305 return OK;
4306 break;
4307
4308 default:
4309 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004310 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4311 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004312 }
4313 return FAIL;
4314}
4315
Bram Moolenaar5714b802013-05-28 22:03:20 +02004316static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
4317
4318/*
4319 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004320 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004321 */
4322 static int
4323match_backref(sub, subidx, bytelen)
4324 regsub_T *sub; /* pointers to subexpressions */
4325 int subidx;
4326 int *bytelen; /* out: length of match in bytes */
4327{
4328 int len;
4329
4330 if (sub->in_use <= subidx)
4331 {
4332retempty:
4333 /* backref was not set, match an empty string */
4334 *bytelen = 0;
4335 return TRUE;
4336 }
4337
4338 if (REG_MULTI)
4339 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004340 if (sub->list.multi[subidx].start.lnum < 0
4341 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004342 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004343 if (sub->list.multi[subidx].start.lnum == reglnum
4344 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004345 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004346 len = sub->list.multi[subidx].end.col
4347 - sub->list.multi[subidx].start.col;
4348 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4349 reginput, &len) == 0)
4350 {
4351 *bytelen = len;
4352 return TRUE;
4353 }
4354 }
4355 else
4356 {
4357 if (match_with_backref(
4358 sub->list.multi[subidx].start.lnum,
4359 sub->list.multi[subidx].start.col,
4360 sub->list.multi[subidx].end.lnum,
4361 sub->list.multi[subidx].end.col,
4362 bytelen) == RA_MATCH)
4363 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004364 }
4365 }
4366 else
4367 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004368 if (sub->list.line[subidx].start == NULL
4369 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004370 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004371 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4372 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004373 {
4374 *bytelen = len;
4375 return TRUE;
4376 }
4377 }
4378 return FALSE;
4379}
4380
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004381#ifdef FEAT_SYN_HL
4382
4383static int match_zref __ARGS((int subidx, int *bytelen));
4384
4385/*
4386 * Check for a match with \z subexpression "subidx".
4387 * Return TRUE if it matches.
4388 */
4389 static int
4390match_zref(subidx, bytelen)
4391 int subidx;
4392 int *bytelen; /* out: length of match in bytes */
4393{
4394 int len;
4395
4396 cleanup_zsubexpr();
4397 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4398 {
4399 /* backref was not set, match an empty string */
4400 *bytelen = 0;
4401 return TRUE;
4402 }
4403
4404 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4405 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4406 {
4407 *bytelen = len;
4408 return TRUE;
4409 }
4410 return FALSE;
4411}
4412#endif
4413
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004414/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004415 * Save list IDs for all NFA states of "prog" into "list".
4416 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004417 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004418 */
4419 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004420nfa_save_listids(prog, list)
4421 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004422 int *list;
4423{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004424 int i;
4425 nfa_state_T *p;
4426
4427 /* Order in the list is reverse, it's a bit faster that way. */
4428 p = &prog->state[0];
4429 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004430 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004431 list[i] = p->lastlist[1];
4432 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004433 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004434 }
4435}
4436
4437/*
4438 * Restore list IDs from "list" to all NFA states.
4439 */
4440 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004441nfa_restore_listids(prog, list)
4442 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 int *list;
4444{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004445 int i;
4446 nfa_state_T *p;
4447
4448 p = &prog->state[0];
4449 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004450 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004451 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004452 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004453 }
4454}
4455
Bram Moolenaar423532e2013-05-29 21:14:42 +02004456 static int
4457nfa_re_num_cmp(val, op, pos)
4458 long_u val;
4459 int op;
4460 long_u pos;
4461{
4462 if (op == 1) return pos > val;
4463 if (op == 2) return pos < val;
4464 return val == pos;
4465}
4466
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004467static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004468static int nfa_regmatch __ARGS((nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m));
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004469
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004470/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004471 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004472 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4473 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004474 */
4475 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004476recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004477 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004478 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004479 nfa_regprog_T *prog;
4480 regsubs_T *submatch;
4481 regsubs_T *m;
4482 int **listids;
4483{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004484 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004485 int save_reglnum = reglnum;
4486 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004487 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004488 save_se_T *save_nfa_endp = nfa_endp;
4489 save_se_T endpos;
4490 save_se_T *endposp = NULL;
4491 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004492 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004493
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004494 if (pim != NULL)
4495 {
4496 /* start at the position where the postponed match was */
4497 if (REG_MULTI)
4498 reginput = regline + pim->end.pos.col;
4499 else
4500 reginput = pim->end.ptr;
4501 }
4502
Bram Moolenaardecd9542013-06-07 16:31:50 +02004503 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004504 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4505 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4506 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004507 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004508 /* The recursive match must end at the current position. When "pim" is
4509 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004510 endposp = &endpos;
4511 if (REG_MULTI)
4512 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004513 if (pim == NULL)
4514 {
4515 endpos.se_u.pos.col = (int)(reginput - regline);
4516 endpos.se_u.pos.lnum = reglnum;
4517 }
4518 else
4519 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004520 }
4521 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004522 {
4523 if (pim == NULL)
4524 endpos.se_u.ptr = reginput;
4525 else
4526 endpos.se_u.ptr = pim->end.ptr;
4527 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004528
4529 /* Go back the specified number of bytes, or as far as the
4530 * start of the previous line, to try matching "\@<=" or
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004531 * not matching "\@<!". This is very ineffecient, limit the number of
4532 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004533 if (state->val <= 0)
4534 {
4535 if (REG_MULTI)
4536 {
4537 regline = reg_getline(--reglnum);
4538 if (regline == NULL)
4539 /* can't go before the first line */
4540 regline = reg_getline(++reglnum);
4541 }
4542 reginput = regline;
4543 }
4544 else
4545 {
4546 if (REG_MULTI && (int)(reginput - regline) < state->val)
4547 {
4548 /* Not enough bytes in this line, go to end of
4549 * previous line. */
4550 regline = reg_getline(--reglnum);
4551 if (regline == NULL)
4552 {
4553 /* can't go before the first line */
4554 regline = reg_getline(++reglnum);
4555 reginput = regline;
4556 }
4557 else
4558 reginput = regline + STRLEN(regline);
4559 }
4560 if ((int)(reginput - regline) >= state->val)
4561 {
4562 reginput -= state->val;
4563#ifdef FEAT_MBYTE
4564 if (has_mbyte)
4565 reginput -= mb_head_off(regline, reginput);
4566#endif
4567 }
4568 else
4569 reginput = regline;
4570 }
4571 }
4572
Bram Moolenaarf46da702013-06-02 22:37:42 +02004573#ifdef ENABLE_LOG
4574 if (log_fd != stderr)
4575 fclose(log_fd);
4576 log_fd = NULL;
4577#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004578 /* Have to clear the lastlist field of the NFA nodes, so that
4579 * nfa_regmatch() and addstate() can run properly after recursion. */
4580 if (nfa_ll_index == 1)
4581 {
4582 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4583 * values and clear them. */
4584 if (*listids == NULL)
4585 {
4586 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4587 if (*listids == NULL)
4588 {
4589 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4590 return 0;
4591 }
4592 }
4593 nfa_save_listids(prog, *listids);
4594 need_restore = TRUE;
4595 /* any value of nfa_listid will do */
4596 }
4597 else
4598 {
4599 /* First recursive nfa_regmatch() call, switch to the second lastlist
4600 * entry. Make sure nfa_listid is different from a previous recursive
4601 * call, because some states may still have this ID. */
4602 ++nfa_ll_index;
4603 if (nfa_listid <= nfa_alt_listid)
4604 nfa_listid = nfa_alt_listid;
4605 }
4606
4607 /* Call nfa_regmatch() to check if the current concat matches at this
4608 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004609 nfa_endp = endposp;
4610 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004611
4612 if (need_restore)
4613 nfa_restore_listids(prog, *listids);
4614 else
4615 {
4616 --nfa_ll_index;
4617 nfa_alt_listid = nfa_listid;
4618 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004619
4620 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004621 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02004622 if (REG_MULTI)
4623 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004624 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004625 nfa_match = save_nfa_match;
4626 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004627 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004628
4629#ifdef ENABLE_LOG
4630 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4631 if (log_fd != NULL)
4632 {
4633 fprintf(log_fd, "****************************\n");
4634 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4635 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4636 fprintf(log_fd, "****************************\n");
4637 }
4638 else
4639 {
4640 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4641 log_fd = stderr;
4642 }
4643#endif
4644
4645 return result;
4646}
4647
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004648static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004649static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004650
4651/*
4652 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004653 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004654 * NFA_ANY: 1
4655 * specific character: 99
4656 */
4657 static int
4658failure_chance(state, depth)
4659 nfa_state_T *state;
4660 int depth;
4661{
4662 int c = state->c;
4663 int l, r;
4664
4665 /* detect looping */
4666 if (depth > 4)
4667 return 1;
4668
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004669 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004670 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004671 case NFA_SPLIT:
4672 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4673 /* avoid recursive stuff */
4674 return 1;
4675 /* two alternatives, use the lowest failure chance */
4676 l = failure_chance(state->out, depth + 1);
4677 r = failure_chance(state->out1, depth + 1);
4678 return l < r ? l : r;
4679
4680 case NFA_ANY:
4681 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004682 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004683
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004684 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004685 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004686 /* empty match works always */
4687 return 0;
4688
4689 case NFA_BOL:
4690 case NFA_EOL:
4691 case NFA_BOF:
4692 case NFA_EOF:
4693 case NFA_NEWL:
4694 return 99;
4695
4696 case NFA_BOW:
4697 case NFA_EOW:
4698 return 90;
4699
4700 case NFA_MOPEN:
4701 case NFA_MOPEN1:
4702 case NFA_MOPEN2:
4703 case NFA_MOPEN3:
4704 case NFA_MOPEN4:
4705 case NFA_MOPEN5:
4706 case NFA_MOPEN6:
4707 case NFA_MOPEN7:
4708 case NFA_MOPEN8:
4709 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004710#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004711 case NFA_ZOPEN:
4712 case NFA_ZOPEN1:
4713 case NFA_ZOPEN2:
4714 case NFA_ZOPEN3:
4715 case NFA_ZOPEN4:
4716 case NFA_ZOPEN5:
4717 case NFA_ZOPEN6:
4718 case NFA_ZOPEN7:
4719 case NFA_ZOPEN8:
4720 case NFA_ZOPEN9:
4721 case NFA_ZCLOSE:
4722 case NFA_ZCLOSE1:
4723 case NFA_ZCLOSE2:
4724 case NFA_ZCLOSE3:
4725 case NFA_ZCLOSE4:
4726 case NFA_ZCLOSE5:
4727 case NFA_ZCLOSE6:
4728 case NFA_ZCLOSE7:
4729 case NFA_ZCLOSE8:
4730 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004731#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004732 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004733 case NFA_MCLOSE1:
4734 case NFA_MCLOSE2:
4735 case NFA_MCLOSE3:
4736 case NFA_MCLOSE4:
4737 case NFA_MCLOSE5:
4738 case NFA_MCLOSE6:
4739 case NFA_MCLOSE7:
4740 case NFA_MCLOSE8:
4741 case NFA_MCLOSE9:
4742 case NFA_NCLOSE:
4743 return failure_chance(state->out, depth + 1);
4744
4745 case NFA_BACKREF1:
4746 case NFA_BACKREF2:
4747 case NFA_BACKREF3:
4748 case NFA_BACKREF4:
4749 case NFA_BACKREF5:
4750 case NFA_BACKREF6:
4751 case NFA_BACKREF7:
4752 case NFA_BACKREF8:
4753 case NFA_BACKREF9:
4754#ifdef FEAT_SYN_HL
4755 case NFA_ZREF1:
4756 case NFA_ZREF2:
4757 case NFA_ZREF3:
4758 case NFA_ZREF4:
4759 case NFA_ZREF5:
4760 case NFA_ZREF6:
4761 case NFA_ZREF7:
4762 case NFA_ZREF8:
4763 case NFA_ZREF9:
4764#endif
4765 /* backreferences don't match in many places */
4766 return 94;
4767
4768 case NFA_LNUM_GT:
4769 case NFA_LNUM_LT:
4770 case NFA_COL_GT:
4771 case NFA_COL_LT:
4772 case NFA_VCOL_GT:
4773 case NFA_VCOL_LT:
4774 case NFA_MARK_GT:
4775 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004776 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004777 /* before/after positions don't match very often */
4778 return 85;
4779
4780 case NFA_LNUM:
4781 return 90;
4782
4783 case NFA_CURSOR:
4784 case NFA_COL:
4785 case NFA_VCOL:
4786 case NFA_MARK:
4787 /* specific positions rarely match */
4788 return 98;
4789
4790 case NFA_COMPOSING:
4791 return 95;
4792
4793 default:
4794 if (c > 0)
4795 /* character match fails often */
4796 return 95;
4797 }
4798
4799 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004800 return 50;
4801}
4802
Bram Moolenaarf46da702013-06-02 22:37:42 +02004803/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004804 * Skip until the char "c" we know a match must start with.
4805 */
4806 static int
4807skip_to_start(c, colp)
4808 int c;
4809 colnr_T *colp;
4810{
4811 char_u *s;
4812
4813 /* Used often, do some work to avoid call overhead. */
4814 if (!ireg_ic
4815#ifdef FEAT_MBYTE
4816 && !has_mbyte
4817#endif
4818 )
4819 s = vim_strbyte(regline + *colp, c);
4820 else
4821 s = cstrchr(regline + *colp, c);
4822 if (s == NULL)
4823 return FAIL;
4824 *colp = (int)(s - regline);
4825 return OK;
4826}
4827
4828/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004829 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004830 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004831 * Returns zero for no match, 1 for a match.
4832 */
4833 static long
4834find_match_text(startcol, regstart, match_text)
4835 colnr_T startcol;
4836 int regstart;
4837 char_u *match_text;
4838{
4839 colnr_T col = startcol;
4840 int c1, c2;
4841 int len1, len2;
4842 int match;
4843
4844 for (;;)
4845 {
4846 match = TRUE;
4847 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4848 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4849 {
4850 c1 = PTR2CHAR(match_text + len1);
4851 c2 = PTR2CHAR(regline + col + len2);
4852 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4853 {
4854 match = FALSE;
4855 break;
4856 }
4857 len2 += MB_CHAR2LEN(c2);
4858 }
4859 if (match
4860#ifdef FEAT_MBYTE
4861 /* check that no composing char follows */
4862 && !(enc_utf8
4863 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4864#endif
4865 )
4866 {
4867 cleanup_subexpr();
4868 if (REG_MULTI)
4869 {
4870 reg_startpos[0].lnum = reglnum;
4871 reg_startpos[0].col = col;
4872 reg_endpos[0].lnum = reglnum;
4873 reg_endpos[0].col = col + len2;
4874 }
4875 else
4876 {
4877 reg_startp[0] = regline + col;
4878 reg_endp[0] = regline + col + len2;
4879 }
4880 return 1L;
4881 }
4882
4883 /* Try finding regstart after the current match. */
4884 col += MB_CHAR2LEN(regstart); /* skip regstart */
4885 if (skip_to_start(regstart, &col) == FAIL)
4886 break;
4887 }
4888 return 0L;
4889}
4890
4891/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004892 * Main matching routine.
4893 *
4894 * Run NFA to determine whether it matches reginput.
4895 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004896 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004897 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004898 * Return TRUE if there is a match, FALSE otherwise.
4899 * Note: Caller must ensure that: start != NULL.
4900 */
4901 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004902nfa_regmatch(prog, start, submatch, m)
4903 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004904 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004905 regsubs_T *submatch;
4906 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004907{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004908 int result;
4909 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004910 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004911 int go_to_nextline = FALSE;
4912 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004913 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004914 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004915 nfa_list_T *thislist;
4916 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004917 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004918 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004919 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004920 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02004921 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004922 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004923#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004924 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925
4926 if (debug == NULL)
4927 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004928 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004929 return FALSE;
4930 }
4931#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004932 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004933
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004934 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004935 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004936 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004937 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004938 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004939 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004940 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004941 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004942
4943#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004944 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004945 if (log_fd != NULL)
4946 {
4947 fprintf(log_fd, "**********************************\n");
4948 nfa_set_code(start->c);
4949 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4950 abs(start->id), code);
4951 fprintf(log_fd, "**********************************\n");
4952 }
4953 else
4954 {
4955 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4956 log_fd = stderr;
4957 }
4958#endif
4959
4960 thislist = &list[0];
4961 thislist->n = 0;
4962 nextlist = &list[1];
4963 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004964#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004965 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004966#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004967 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004968
4969 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4970 * it's the first MOPEN. */
4971 if (toplevel)
4972 {
4973 if (REG_MULTI)
4974 {
4975 m->norm.list.multi[0].start.lnum = reglnum;
4976 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4977 }
4978 else
4979 m->norm.list.line[0].start = reginput;
4980 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004981 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004982 }
4983 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004984 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004985
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004986#define ADD_STATE_IF_MATCH(state) \
4987 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004988 add_state = state->out; \
4989 add_off = clen; \
4990 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004991
4992 /*
4993 * Run for each character.
4994 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004995 for (;;)
4996 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004997 int curc;
4998 int clen;
4999
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005000#ifdef FEAT_MBYTE
5001 if (has_mbyte)
5002 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005003 curc = (*mb_ptr2char)(reginput);
5004 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005005 }
5006 else
5007#endif
5008 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005009 curc = *reginput;
5010 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005011 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005012 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005013 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005014 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005015 go_to_nextline = FALSE;
5016 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005017
5018 /* swap lists */
5019 thislist = &list[flag];
5020 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005021 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005022 ++nfa_listid;
5023 thislist->id = nfa_listid;
5024 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005025
5026#ifdef ENABLE_LOG
5027 fprintf(log_fd, "------------------------------------------\n");
5028 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005029 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005031 {
5032 int i;
5033
5034 for (i = 0; i < thislist->n; i++)
5035 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5036 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005037 fprintf(log_fd, "\n");
5038#endif
5039
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005040#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005041 fprintf(debug, "\n-------------------\n");
5042#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005043 /*
5044 * If the state lists are empty we can stop.
5045 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005046 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005047 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005048
5049 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005050 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005052 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005053
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005054#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055 nfa_set_code(t->state->c);
5056 fprintf(debug, "%s, ", code);
5057#endif
5058#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005059 {
5060 int col;
5061
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005062 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005063 col = -1;
5064 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005065 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005066 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005067 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005068 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005069 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5070 abs(t->state->id), (int)t->state->c, code, col,
5071 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005072 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005073#endif
5074
5075 /*
5076 * Handle the possible codes of the current state.
5077 * The most important is NFA_MATCH.
5078 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005079 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005080 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005081 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005082 switch (t->state->c)
5083 {
5084 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005085 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005086 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005087 copy_sub(&submatch->norm, &t->subs.norm);
5088#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005089 if (nfa_has_zsubexpr)
5090 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005091#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005092#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005093 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005094#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005095 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005096 * states at this position. When the list of states is going
5097 * to be empty quit without advancing, so that "reginput" is
5098 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005099 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005100 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005101 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005102 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005103
5104 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005105 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005106 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005107 /*
5108 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005109 * NFA_START_INVISIBLE_BEFORE node.
5110 * They surround a zero-width group, used with "\@=", "\&",
5111 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005112 * If we got here, it means that the current "invisible" group
5113 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005114 * nfa_regmatch(). For a look-behind match only when it ends
5115 * in the position in "nfa_endp".
5116 * Submatches are stored in *m, and used in the parent call.
5117 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005118#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005119 if (nfa_endp != NULL)
5120 {
5121 if (REG_MULTI)
5122 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5123 (int)reglnum,
5124 (int)nfa_endp->se_u.pos.lnum,
5125 (int)(reginput - regline),
5126 nfa_endp->se_u.pos.col);
5127 else
5128 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5129 (int)(reginput - regline),
5130 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005131 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005132#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005133 /* If "nfa_endp" is set it's only a match if it ends at
5134 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005135 if (nfa_endp != NULL && (REG_MULTI
5136 ? (reglnum != nfa_endp->se_u.pos.lnum
5137 || (int)(reginput - regline)
5138 != nfa_endp->se_u.pos.col)
5139 : reginput != nfa_endp->se_u.ptr))
5140 break;
5141
5142 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005143 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005144 {
5145 copy_sub(&m->norm, &t->subs.norm);
5146#ifdef FEAT_SYN_HL
5147 if (nfa_has_zsubexpr)
5148 copy_sub(&m->synt, &t->subs.synt);
5149#endif
5150 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005151#ifdef ENABLE_LOG
5152 fprintf(log_fd, "Match found:\n");
5153 log_subsexpr(m);
5154#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005155 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005156 break;
5157
5158 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005159 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005160 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005161 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005162 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005163 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005164 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005165 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005166 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005167#ifdef ENABLE_LOG
5168 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5169 failure_chance(t->state->out, 0),
5170 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005171#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005172 /* Do it directly if there already is a PIM or when
5173 * nfa_postprocess() detected it will work better. */
5174 if (t->pim.result != NFA_PIM_UNUSED
5175 || t->state->c == NFA_START_INVISIBLE_FIRST
5176 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5177 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5178 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005179 {
5180 /*
5181 * First try matching the invisible match, then what
5182 * follows.
5183 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005184 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005185 submatch, m, &listids);
5186
Bram Moolenaardecd9542013-06-07 16:31:50 +02005187 /* for \@! and \@<! it is a match when the result is
5188 * FALSE */
5189 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005190 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5191 || t->state->c
5192 == NFA_START_INVISIBLE_BEFORE_NEG
5193 || t->state->c
5194 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005195 {
5196 /* Copy submatch info from the recursive call */
5197 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005198#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005199 if (nfa_has_zsubexpr)
5200 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005201#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005202
Bram Moolenaara2d95102013-06-04 14:23:05 +02005203 /* t->state->out1 is the corresponding
5204 * END_INVISIBLE node; Add its out to the current
5205 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005206 add_here = TRUE;
5207 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005208 }
5209 }
5210 else
5211 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005212 nfa_pim_T pim;
5213
Bram Moolenaara2d95102013-06-04 14:23:05 +02005214 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005215 * First try matching what follows. Only if a match
5216 * is found verify the invisible match matches. Add a
5217 * nfa_pim_T to the following states, it contains info
5218 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005219 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005220 pim.state = t->state;
5221 pim.result = NFA_PIM_TODO;
5222 pim.subs.norm.in_use = 0;
5223#ifdef FEAT_SYN_HL
5224 pim.subs.synt.in_use = 0;
5225#endif
5226 if (REG_MULTI)
5227 {
5228 pim.end.pos.col = (int)(reginput - regline);
5229 pim.end.pos.lnum = reglnum;
5230 }
5231 else
5232 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005233
5234 /* t->state->out1 is the corresponding END_INVISIBLE
5235 * node; Add its out to the current list (zero-width
5236 * match). */
5237 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005238 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005239 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005240 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005241 break;
5242
Bram Moolenaar87953742013-06-05 18:52:40 +02005243 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005244 {
5245 nfa_state_T *skip = NULL;
5246#ifdef ENABLE_LOG
5247 int skip_lid = 0;
5248#endif
5249
5250 /* There is no point in trying to match the pattern if the
5251 * output state is not going to be added to the list. */
5252 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5253 {
5254 skip = t->state->out1->out;
5255#ifdef ENABLE_LOG
5256 skip_lid = nextlist->id;
5257#endif
5258 }
5259 else if (state_in_list(nextlist,
5260 t->state->out1->out->out, &t->subs))
5261 {
5262 skip = t->state->out1->out->out;
5263#ifdef ENABLE_LOG
5264 skip_lid = nextlist->id;
5265#endif
5266 }
5267 else if(state_in_list(thislist,
5268 t->state->out1->out->out, &t->subs))
5269 {
5270 skip = t->state->out1->out->out;
5271#ifdef ENABLE_LOG
5272 skip_lid = thislist->id;
5273#endif
5274 }
5275 if (skip != NULL)
5276 {
5277#ifdef ENABLE_LOG
5278 nfa_set_code(skip->c);
5279 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5280 abs(skip->id), skip_lid, skip->c, code);
5281#endif
5282 break;
5283 }
5284
Bram Moolenaar87953742013-06-05 18:52:40 +02005285 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005286 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005287 submatch, m, &listids);
5288 if (result)
5289 {
5290 int bytelen;
5291
5292#ifdef ENABLE_LOG
5293 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5294 log_subsexpr(m);
5295#endif
5296 /* Copy submatch info from the recursive call */
5297 copy_sub_off(&t->subs.norm, &m->norm);
5298#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005299 if (nfa_has_zsubexpr)
5300 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005301#endif
5302 /* Now we need to skip over the matched text and then
5303 * continue with what follows. */
5304 if (REG_MULTI)
5305 /* TODO: multi-line match */
5306 bytelen = m->norm.list.multi[0].end.col
5307 - (int)(reginput - regline);
5308 else
5309 bytelen = (int)(m->norm.list.line[0].end - reginput);
5310
5311#ifdef ENABLE_LOG
5312 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5313#endif
5314 if (bytelen == 0)
5315 {
5316 /* empty match, output of corresponding
5317 * NFA_END_PATTERN/NFA_SKIP to be used at current
5318 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005319 add_here = TRUE;
5320 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005321 }
5322 else if (bytelen <= clen)
5323 {
5324 /* match current character, output of corresponding
5325 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005326 add_state = t->state->out1->out->out;
5327 add_off = clen;
5328 }
5329 else
5330 {
5331 /* skip over the matched characters, set character
5332 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005333 add_state = t->state->out1->out;
5334 add_off = bytelen;
5335 add_count = bytelen - clen;
5336 }
5337 }
5338 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005339 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005341 case NFA_BOL:
5342 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005343 {
5344 add_here = TRUE;
5345 add_state = t->state->out;
5346 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005347 break;
5348
5349 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005350 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005351 {
5352 add_here = TRUE;
5353 add_state = t->state->out;
5354 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005355 break;
5356
5357 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005358 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005359
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005360 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005361 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005362#ifdef FEAT_MBYTE
5363 else if (has_mbyte)
5364 {
5365 int this_class;
5366
5367 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005368 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005369 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005370 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005371 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005372 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005373 }
5374#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005375 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005376 || (reginput > regline
5377 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005378 result = FALSE;
5379 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005380 {
5381 add_here = TRUE;
5382 add_state = t->state->out;
5383 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005384 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005385
5386 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005387 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005388 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005389 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005390#ifdef FEAT_MBYTE
5391 else if (has_mbyte)
5392 {
5393 int this_class, prev_class;
5394
5395 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005396 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005397 prev_class = reg_prev_class();
5398 if (this_class == prev_class
5399 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005400 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005401 }
5402#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005403 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005404 || (reginput[0] != NUL
5405 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005406 result = FALSE;
5407 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005408 {
5409 add_here = TRUE;
5410 add_state = t->state->out;
5411 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005412 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005413
Bram Moolenaar4b780632013-05-31 22:14:52 +02005414 case NFA_BOF:
5415 if (reglnum == 0 && reginput == regline
5416 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005417 {
5418 add_here = TRUE;
5419 add_state = t->state->out;
5420 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005421 break;
5422
5423 case NFA_EOF:
5424 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005425 {
5426 add_here = TRUE;
5427 add_state = t->state->out;
5428 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005429 break;
5430
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005431#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005432 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005433 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005434 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005435 int len = 0;
5436 nfa_state_T *end;
5437 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005438 int cchars[MAX_MCO];
5439 int ccount = 0;
5440 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005441
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005442 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005443 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005444 if (utf_iscomposing(sta->c))
5445 {
5446 /* Only match composing character(s), ignore base
5447 * character. Used for ".{composing}" and "{composing}"
5448 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005449 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005450 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005451 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005452 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005453 /* If \Z was present, then ignore composing characters.
5454 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005455 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005456 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005457 else
5458 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005459 while (sta->c != NFA_END_COMPOSING)
5460 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005461 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005462
5463 /* Check base character matches first, unless ignored. */
5464 else if (len > 0 || mc == sta->c)
5465 {
5466 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005467 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005468 len += mb_char2len(mc);
5469 sta = sta->out;
5470 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005471
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005472 /* We don't care about the order of composing characters.
5473 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005474 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005475 {
5476 mc = mb_ptr2char(reginput + len);
5477 cchars[ccount++] = mc;
5478 len += mb_char2len(mc);
5479 if (ccount == MAX_MCO)
5480 break;
5481 }
5482
5483 /* Check that each composing char in the pattern matches a
5484 * composing char in the text. We do not check if all
5485 * composing chars are matched. */
5486 result = OK;
5487 while (sta->c != NFA_END_COMPOSING)
5488 {
5489 for (j = 0; j < ccount; ++j)
5490 if (cchars[j] == sta->c)
5491 break;
5492 if (j == ccount)
5493 {
5494 result = FAIL;
5495 break;
5496 }
5497 sta = sta->out;
5498 }
5499 }
5500 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005501 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005502
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005503 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005504 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005505 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005506 }
5507#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005508
5509 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005510 if (curc == NUL && !reg_line_lbr && REG_MULTI
5511 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005513 go_to_nextline = TRUE;
5514 /* Pass -1 for the offset, which means taking the position
5515 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005516 add_state = t->state->out;
5517 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005519 else if (curc == '\n' && reg_line_lbr)
5520 {
5521 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005522 add_state = t->state->out;
5523 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005524 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005525 break;
5526
Bram Moolenaar417bad22013-06-07 14:08:30 +02005527 case NFA_START_COLL:
5528 case NFA_START_NEG_COLL:
5529 {
5530 /* What follows is a list of characters, until NFA_END_COLL.
5531 * One of them must match or none of them must match. */
5532 nfa_state_T *state;
5533 int result_if_matched;
5534 int c1, c2;
5535
5536 /* Never match EOL. If it's part of the collection it is added
5537 * as a separate state with an OR. */
5538 if (curc == NUL)
5539 break;
5540
5541 state = t->state->out;
5542 result_if_matched = (t->state->c == NFA_START_COLL);
5543 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005544 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005545 if (state->c == NFA_END_COLL)
5546 {
5547 result = !result_if_matched;
5548 break;
5549 }
5550 if (state->c == NFA_RANGE_MIN)
5551 {
5552 c1 = state->val;
5553 state = state->out; /* advance to NFA_RANGE_MAX */
5554 c2 = state->val;
5555#ifdef ENABLE_LOG
5556 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5557 curc, c1, c2);
5558#endif
5559 if (curc >= c1 && curc <= c2)
5560 {
5561 result = result_if_matched;
5562 break;
5563 }
5564 if (ireg_ic)
5565 {
5566 int curc_low = MB_TOLOWER(curc);
5567 int done = FALSE;
5568
5569 for ( ; c1 <= c2; ++c1)
5570 if (MB_TOLOWER(c1) == curc_low)
5571 {
5572 result = result_if_matched;
5573 done = TRUE;
5574 break;
5575 }
5576 if (done)
5577 break;
5578 }
5579 }
5580 else if (state->c < 0 ? check_char_class(state->c, curc)
5581 : (curc == state->c
5582 || (ireg_ic && MB_TOLOWER(curc)
5583 == MB_TOLOWER(state->c))))
5584 {
5585 result = result_if_matched;
5586 break;
5587 }
5588 state = state->out;
5589 }
5590 if (result)
5591 {
5592 /* next state is in out of the NFA_END_COLL, out1 of
5593 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005594 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005595 add_off = clen;
5596 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005598 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599
5600 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005601 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005602 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005603 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005604 add_state = t->state->out;
5605 add_off = clen;
5606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005607 break;
5608
5609 /*
5610 * Character classes like \a for alpha, \d for digit etc.
5611 */
5612 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005613 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005614 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005615 break;
5616
5617 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005618 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005619 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005620 break;
5621
5622 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005623 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005624 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005625 break;
5626
5627 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005628 result = !VIM_ISDIGIT(curc)
5629 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005630 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005631 break;
5632
5633 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005634 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005635 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005636 break;
5637
5638 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005639 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005640 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641 break;
5642
5643 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005644 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005645 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005646 break;
5647
5648 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005649 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005650 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005651 break;
5652
5653 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005654 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005655 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005656 break;
5657
5658 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005659 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005660 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005661 break;
5662
5663 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005664 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005665 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005666 break;
5667
5668 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005669 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005670 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005671 break;
5672
5673 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005674 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005675 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005676 break;
5677
5678 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005679 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005680 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005681 break;
5682
5683 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005684 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005685 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005686 break;
5687
5688 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005689 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005690 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005691 break;
5692
5693 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005694 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005695 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696 break;
5697
5698 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005699 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005700 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005701 break;
5702
5703 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005704 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005705 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005706 break;
5707
5708 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005709 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005710 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005711 break;
5712
5713 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005714 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005715 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005716 break;
5717
5718 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005719 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005720 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005721 break;
5722
5723 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005724 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005725 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005726 break;
5727
5728 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005729 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005730 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005731 break;
5732
5733 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005734 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005735 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005736 break;
5737
5738 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005739 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005740 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005741 break;
5742
Bram Moolenaar5714b802013-05-28 22:03:20 +02005743 case NFA_BACKREF1:
5744 case NFA_BACKREF2:
5745 case NFA_BACKREF3:
5746 case NFA_BACKREF4:
5747 case NFA_BACKREF5:
5748 case NFA_BACKREF6:
5749 case NFA_BACKREF7:
5750 case NFA_BACKREF8:
5751 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005752#ifdef FEAT_SYN_HL
5753 case NFA_ZREF1:
5754 case NFA_ZREF2:
5755 case NFA_ZREF3:
5756 case NFA_ZREF4:
5757 case NFA_ZREF5:
5758 case NFA_ZREF6:
5759 case NFA_ZREF7:
5760 case NFA_ZREF8:
5761 case NFA_ZREF9:
5762#endif
5763 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005764 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005765 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005766 int bytelen;
5767
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005768 if (t->state->c <= NFA_BACKREF9)
5769 {
5770 subidx = t->state->c - NFA_BACKREF1 + 1;
5771 result = match_backref(&t->subs.norm, subidx, &bytelen);
5772 }
5773#ifdef FEAT_SYN_HL
5774 else
5775 {
5776 subidx = t->state->c - NFA_ZREF1 + 1;
5777 result = match_zref(subidx, &bytelen);
5778 }
5779#endif
5780
Bram Moolenaar5714b802013-05-28 22:03:20 +02005781 if (result)
5782 {
5783 if (bytelen == 0)
5784 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005785 /* empty match always works, output of NFA_SKIP to be
5786 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005787 add_here = TRUE;
5788 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005789 }
5790 else if (bytelen <= clen)
5791 {
5792 /* match current character, jump ahead to out of
5793 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005794 add_state = t->state->out->out;
5795 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005796 }
5797 else
5798 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005799 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005800 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005801 add_state = t->state->out;
5802 add_off = bytelen;
5803 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005804 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005805 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005806 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005807 }
5808 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005809 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005810 if (t->count - clen <= 0)
5811 {
5812 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005813 add_state = t->state->out;
5814 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005815 }
5816 else
5817 {
5818 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005819 add_state = t->state;
5820 add_off = 0;
5821 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005822 }
5823 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005824
Bram Moolenaar423532e2013-05-29 21:14:42 +02005825 case NFA_LNUM:
5826 case NFA_LNUM_GT:
5827 case NFA_LNUM_LT:
5828 result = (REG_MULTI &&
5829 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5830 (long_u)(reglnum + reg_firstlnum)));
5831 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005832 {
5833 add_here = TRUE;
5834 add_state = t->state->out;
5835 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005836 break;
5837
5838 case NFA_COL:
5839 case NFA_COL_GT:
5840 case NFA_COL_LT:
5841 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5842 (long_u)(reginput - regline) + 1);
5843 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005844 {
5845 add_here = TRUE;
5846 add_state = t->state->out;
5847 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005848 break;
5849
5850 case NFA_VCOL:
5851 case NFA_VCOL_GT:
5852 case NFA_VCOL_LT:
5853 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5854 (long_u)win_linetabsize(
5855 reg_win == NULL ? curwin : reg_win,
5856 regline, (colnr_T)(reginput - regline)) + 1);
5857 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005858 {
5859 add_here = TRUE;
5860 add_state = t->state->out;
5861 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005862 break;
5863
Bram Moolenaar044aa292013-06-04 21:27:38 +02005864 case NFA_MARK:
5865 case NFA_MARK_GT:
5866 case NFA_MARK_LT:
5867 {
5868 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5869
5870 /* Compare the mark position to the match position. */
5871 result = (pos != NULL /* mark doesn't exist */
5872 && pos->lnum > 0 /* mark isn't set in reg_buf */
5873 && (pos->lnum == reglnum + reg_firstlnum
5874 ? (pos->col == (colnr_T)(reginput - regline)
5875 ? t->state->c == NFA_MARK
5876 : (pos->col < (colnr_T)(reginput - regline)
5877 ? t->state->c == NFA_MARK_GT
5878 : t->state->c == NFA_MARK_LT))
5879 : (pos->lnum < reglnum + reg_firstlnum
5880 ? t->state->c == NFA_MARK_GT
5881 : t->state->c == NFA_MARK_LT)));
5882 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005883 {
5884 add_here = TRUE;
5885 add_state = t->state->out;
5886 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005887 break;
5888 }
5889
Bram Moolenaar423532e2013-05-29 21:14:42 +02005890 case NFA_CURSOR:
5891 result = (reg_win != NULL
5892 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5893 && ((colnr_T)(reginput - regline)
5894 == reg_win->w_cursor.col));
5895 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005896 {
5897 add_here = TRUE;
5898 add_state = t->state->out;
5899 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005900 break;
5901
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005902 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005903#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005904 result = reg_match_visual();
5905 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005906 {
5907 add_here = TRUE;
5908 add_state = t->state->out;
5909 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005910#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005911 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005912
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005913 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005914 {
5915 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005916
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005917 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005918 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005919 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005920 result = (c == curc);
5921
5922 if (!result && ireg_ic)
5923 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005924#ifdef FEAT_MBYTE
5925 /* If there is a composing character which is not being
5926 * ignored there can be no match. Match with composing
5927 * character uses NFA_COMPOSING above. */
5928 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005929 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005930 result = FALSE;
5931#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005932 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005933 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005934 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005935
5936 } /* switch (t->state->c) */
5937
5938 if (add_state != NULL)
5939 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005940 nfa_pim_T *pim;
5941
5942 if (t->pim.result == NFA_PIM_UNUSED)
5943 pim = NULL;
5944 else
5945 pim = &t->pim;
5946
5947 /* Handle the postponed invisible match if the match might end
5948 * without advancing and before the end of the line. */
5949 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005950 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005951 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005952 {
5953#ifdef ENABLE_LOG
5954 fprintf(log_fd, "\n");
5955 fprintf(log_fd, "==================================\n");
5956 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5957 fprintf(log_fd, "\n");
5958#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005959 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005960 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005961 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005962 /* for \@! and \@<! it is a match when the result is
5963 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005964 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005965 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
5966 || pim->state->c
5967 == NFA_START_INVISIBLE_BEFORE_NEG
5968 || pim->state->c
5969 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005970 {
5971 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005972 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005973#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005974 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005975 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005976#endif
5977 }
5978 }
5979 else
5980 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005981 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005982#ifdef ENABLE_LOG
5983 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005984 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005985 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5986 fprintf(log_fd, "\n");
5987#endif
5988 }
5989
Bram Moolenaardecd9542013-06-07 16:31:50 +02005990 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005991 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005992 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
5993 || pim->state->c
5994 == NFA_START_INVISIBLE_BEFORE_NEG
5995 || pim->state->c
5996 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005997 {
5998 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005999 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006000#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006001 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006002 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006003#endif
6004 }
6005 else
6006 /* look-behind match failed, don't add the state */
6007 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006008
6009 /* Postponed invisible match was handled, don't add it to
6010 * following states. */
6011 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006012 }
6013
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006014 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006015 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006016 else
6017 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006018 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006019 if (add_count > 0)
6020 nextlist->t[nextlist->n - 1].count = add_count;
6021 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006022 }
6023
6024 } /* for (thislist = thislist; thislist->state; thislist++) */
6025
Bram Moolenaare23febd2013-05-26 18:40:14 +02006026 /* Look for the start of a match in the current position by adding the
6027 * start state to the list of states.
6028 * The first found match is the leftmost one, thus the order of states
6029 * matters!
6030 * Do not add the start state in recursive calls of nfa_regmatch(),
6031 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006032 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006033 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006034 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006035 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006036 && reglnum == 0
6037 && clen != 0
6038 && (ireg_maxcol == 0
6039 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006040 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006041 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006042 ? (reglnum < nfa_endp->se_u.pos.lnum
6043 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006044 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006045 < nfa_endp->se_u.pos.col))
6046 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006047 {
6048#ifdef ENABLE_LOG
6049 fprintf(log_fd, "(---) STARTSTATE\n");
6050#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006051 /* Inline optimized code for addstate() if we know the state is
6052 * the first MOPEN. */
6053 if (toplevel)
6054 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006055 int add = TRUE;
6056 int c;
6057
6058 if (prog->regstart != NUL && clen != 0)
6059 {
6060 if (nextlist->n == 0)
6061 {
6062 colnr_T col = (colnr_T)(reginput - regline) + clen;
6063
6064 /* Nextlist is empty, we can skip ahead to the
6065 * character that must appear at the start. */
6066 if (skip_to_start(prog->regstart, &col) == FAIL)
6067 break;
6068#ifdef ENABLE_LOG
6069 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6070 col - ((colnr_T)(reginput - regline) + clen));
6071#endif
6072 reginput = regline + col - clen;
6073 }
6074 else
6075 {
6076 /* Checking if the required start character matches is
6077 * cheaper than adding a state that won't match. */
6078 c = PTR2CHAR(reginput + clen);
6079 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6080 != MB_TOLOWER(prog->regstart)))
6081 {
6082#ifdef ENABLE_LOG
6083 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6084#endif
6085 add = FALSE;
6086 }
6087 }
6088 }
6089
6090 if (add)
6091 {
6092 if (REG_MULTI)
6093 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006094 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006095 else
6096 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006097 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006098 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006099 }
6100 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006101 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 }
6103
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006104#ifdef ENABLE_LOG
6105 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006106 {
6107 int i;
6108
6109 for (i = 0; i < thislist->n; i++)
6110 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6111 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006112 fprintf(log_fd, "\n");
6113#endif
6114
6115nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006116 /* Advance to the next character, or advance to the next line, or
6117 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006118 if (clen != 0)
6119 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006120 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6121 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006122 reg_nextline();
6123 else
6124 break;
6125 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006126
6127#ifdef ENABLE_LOG
6128 if (log_fd != stderr)
6129 fclose(log_fd);
6130 log_fd = NULL;
6131#endif
6132
6133theend:
6134 /* Free memory */
6135 vim_free(list[0].t);
6136 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006137 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006138#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006139#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006140 fclose(debug);
6141#endif
6142
Bram Moolenaar963fee22013-05-26 21:47:28 +02006143 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006144}
6145
6146/*
6147 * Try match of "prog" with at regline["col"].
6148 * Returns 0 for failure, number of lines contained in the match otherwise.
6149 */
6150 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006151nfa_regtry(prog, col)
6152 nfa_regprog_T *prog;
6153 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006154{
6155 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006156 regsubs_T subs, m;
6157 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006158#ifdef ENABLE_LOG
6159 FILE *f;
6160#endif
6161
6162 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006163
6164#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006165 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006166 if (f != NULL)
6167 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006168 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006169#ifdef DEBUG
6170 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6171#endif
6172 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006173 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006174 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006175 fprintf(f, "\n\n");
6176 fclose(f);
6177 }
6178 else
6179 EMSG(_("Could not open temporary log file for writing "));
6180#endif
6181
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006182 clear_sub(&subs.norm);
6183 clear_sub(&m.norm);
6184#ifdef FEAT_SYN_HL
6185 clear_sub(&subs.synt);
6186 clear_sub(&m.synt);
6187#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006188
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006189 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006190 return 0;
6191
6192 cleanup_subexpr();
6193 if (REG_MULTI)
6194 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006195 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006196 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006197 reg_startpos[i] = subs.norm.list.multi[i].start;
6198 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006199 }
6200
6201 if (reg_startpos[0].lnum < 0)
6202 {
6203 reg_startpos[0].lnum = 0;
6204 reg_startpos[0].col = col;
6205 }
6206 if (reg_endpos[0].lnum < 0)
6207 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006208 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006209 reg_endpos[0].lnum = reglnum;
6210 reg_endpos[0].col = (int)(reginput - regline);
6211 }
6212 else
6213 /* Use line number of "\ze". */
6214 reglnum = reg_endpos[0].lnum;
6215 }
6216 else
6217 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006218 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006219 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006220 reg_startp[i] = subs.norm.list.line[i].start;
6221 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006222 }
6223
6224 if (reg_startp[0] == NULL)
6225 reg_startp[0] = regline + col;
6226 if (reg_endp[0] == NULL)
6227 reg_endp[0] = reginput;
6228 }
6229
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006230#ifdef FEAT_SYN_HL
6231 /* Package any found \z(...\) matches for export. Default is none. */
6232 unref_extmatch(re_extmatch_out);
6233 re_extmatch_out = NULL;
6234
6235 if (prog->reghasz == REX_SET)
6236 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006237 cleanup_zsubexpr();
6238 re_extmatch_out = make_extmatch();
6239 for (i = 0; i < subs.synt.in_use; i++)
6240 {
6241 if (REG_MULTI)
6242 {
6243 struct multipos *mpos = &subs.synt.list.multi[i];
6244
6245 /* Only accept single line matches. */
6246 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6247 re_extmatch_out->matches[i] =
6248 vim_strnsave(reg_getline(mpos->start.lnum)
6249 + mpos->start.col,
6250 mpos->end.col - mpos->start.col);
6251 }
6252 else
6253 {
6254 struct linepos *lpos = &subs.synt.list.line[i];
6255
6256 if (lpos->start != NULL && lpos->end != NULL)
6257 re_extmatch_out->matches[i] =
6258 vim_strnsave(lpos->start,
6259 (int)(lpos->end - lpos->start));
6260 }
6261 }
6262 }
6263#endif
6264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006265 return 1 + reglnum;
6266}
6267
6268/*
6269 * Match a regexp against a string ("line" points to the string) or multiple
6270 * lines ("line" is NULL, use reg_getline()).
6271 *
6272 * Returns 0 for failure, number of lines contained in the match otherwise.
6273 */
6274 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006275nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006276 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006277 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006278{
6279 nfa_regprog_T *prog;
6280 long retval = 0L;
6281 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006282 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006283
6284 if (REG_MULTI)
6285 {
6286 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6287 line = reg_getline((linenr_T)0); /* relative to the cursor */
6288 reg_startpos = reg_mmatch->startpos;
6289 reg_endpos = reg_mmatch->endpos;
6290 }
6291 else
6292 {
6293 prog = (nfa_regprog_T *)reg_match->regprog;
6294 reg_startp = reg_match->startp;
6295 reg_endp = reg_match->endp;
6296 }
6297
6298 /* Be paranoid... */
6299 if (prog == NULL || line == NULL)
6300 {
6301 EMSG(_(e_null));
6302 goto theend;
6303 }
6304
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006305 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6306 if (prog->regflags & RF_ICASE)
6307 ireg_ic = TRUE;
6308 else if (prog->regflags & RF_NOICASE)
6309 ireg_ic = FALSE;
6310
6311#ifdef FEAT_MBYTE
6312 /* If pattern contains "\Z" overrule value of ireg_icombine */
6313 if (prog->regflags & RF_ICOMBINE)
6314 ireg_icombine = TRUE;
6315#endif
6316
6317 regline = line;
6318 reglnum = 0; /* relative to line */
6319
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006320 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006321 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006322 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006323 nfa_listid = 1;
6324 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006325#ifdef DEBUG
6326 nfa_regengine.expr = prog->pattern;
6327#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006328
Bram Moolenaard89616e2013-06-06 18:46:06 +02006329 if (prog->reganch && col > 0)
6330 return 0L;
6331
Bram Moolenaar473de612013-06-08 18:19:48 +02006332 need_clear_subexpr = TRUE;
6333#ifdef FEAT_SYN_HL
6334 /* Clear the external match subpointers if necessary. */
6335 if (prog->reghasz == REX_SET)
6336 {
6337 nfa_has_zsubexpr = TRUE;
6338 need_clear_zsubexpr = TRUE;
6339 }
6340 else
6341 nfa_has_zsubexpr = FALSE;
6342#endif
6343
Bram Moolenaard89616e2013-06-06 18:46:06 +02006344 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006345 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006346 /* Skip ahead until a character we know the match must start with.
6347 * When there is none there is no match. */
6348 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006349 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006350
Bram Moolenaar473de612013-06-08 18:19:48 +02006351 /* If match_text is set it contains the full text that must match.
6352 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006353 if (prog->match_text != NULL
6354#ifdef FEAT_MBYTE
6355 && !ireg_icombine
6356#endif
6357 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006358 return find_match_text(col, prog->regstart, prog->match_text);
6359 }
6360
Bram Moolenaard89616e2013-06-06 18:46:06 +02006361 /* If the start column is past the maximum column: no need to try. */
6362 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6363 goto theend;
6364
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006365 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 for (i = 0; i < nstate; ++i)
6367 {
6368 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006369 prog->state[i].lastlist[0] = 0;
6370 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006371 }
6372
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006373 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006374
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006375#ifdef DEBUG
6376 nfa_regengine.expr = NULL;
6377#endif
6378
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379theend:
6380 return retval;
6381}
6382
6383/*
6384 * Compile a regular expression into internal code for the NFA matcher.
6385 * Returns the program in allocated space. Returns NULL for an error.
6386 */
6387 static regprog_T *
6388nfa_regcomp(expr, re_flags)
6389 char_u *expr;
6390 int re_flags;
6391{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006392 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006393 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006394 int *postfix;
6395
6396 if (expr == NULL)
6397 return NULL;
6398
6399#ifdef DEBUG
6400 nfa_regengine.expr = expr;
6401#endif
6402
6403 init_class_tab();
6404
6405 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6406 return NULL;
6407
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006408 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006409 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006410 postfix = re2post();
6411 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006412 {
6413 /* TODO: only give this error for debugging? */
6414 if (post_ptr >= post_end)
6415 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006416 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006417 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006418
6419 /*
6420 * In order to build the NFA, we parse the input regexp twice:
6421 * 1. first pass to count size (so we can allocate space)
6422 * 2. second to emit code
6423 */
6424#ifdef ENABLE_LOG
6425 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006426 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006427
6428 if (f != NULL)
6429 {
6430 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6431 fclose(f);
6432 }
6433 }
6434#endif
6435
6436 /*
6437 * PASS 1
6438 * Count number of NFA states in "nstate". Do not build the NFA.
6439 */
6440 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006441
Bram Moolenaar16619a22013-06-11 18:42:36 +02006442 /* allocate the regprog with space for the compiled regexp */
6443 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006444 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6445 if (prog == NULL)
6446 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006447 state_ptr = prog->state;
6448
6449 /*
6450 * PASS 2
6451 * Build the NFA
6452 */
6453 prog->start = post2nfa(postfix, post_ptr, FALSE);
6454 if (prog->start == NULL)
6455 goto fail;
6456
6457 prog->regflags = regflags;
6458 prog->engine = &nfa_regengine;
6459 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006460 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006461 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006462 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006463
Bram Moolenaara2947e22013-06-11 22:44:09 +02006464 nfa_postprocess(prog);
6465
Bram Moolenaard89616e2013-06-06 18:46:06 +02006466 prog->reganch = nfa_get_reganch(prog->start, 0);
6467 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006468 prog->match_text = nfa_get_match_text(prog->start);
6469
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006470#ifdef ENABLE_LOG
6471 nfa_postfix_dump(expr, OK);
6472 nfa_dump(prog);
6473#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006474#ifdef FEAT_SYN_HL
6475 /* Remember whether this pattern has any \z specials in it. */
6476 prog->reghasz = re_has_z;
6477#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006478#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006479 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006480 nfa_regengine.expr = NULL;
6481#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006482
6483out:
6484 vim_free(post_start);
6485 post_start = post_ptr = post_end = NULL;
6486 state_ptr = NULL;
6487 return (regprog_T *)prog;
6488
6489fail:
6490 vim_free(prog);
6491 prog = NULL;
6492#ifdef ENABLE_LOG
6493 nfa_postfix_dump(expr, FAIL);
6494#endif
6495#ifdef DEBUG
6496 nfa_regengine.expr = NULL;
6497#endif
6498 goto out;
6499}
6500
Bram Moolenaar473de612013-06-08 18:19:48 +02006501/*
6502 * Free a compiled regexp program, returned by nfa_regcomp().
6503 */
6504 static void
6505nfa_regfree(prog)
6506 regprog_T *prog;
6507{
6508 if (prog != NULL)
6509 {
6510 vim_free(((nfa_regprog_T *)prog)->match_text);
6511#ifdef DEBUG
6512 vim_free(((nfa_regprog_T *)prog)->pattern);
6513#endif
6514 vim_free(prog);
6515 }
6516}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006517
6518/*
6519 * Match a regexp against a string.
6520 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6521 * Uses curbuf for line count and 'iskeyword'.
6522 *
6523 * Return TRUE if there is a match, FALSE if not.
6524 */
6525 static int
6526nfa_regexec(rmp, line, col)
6527 regmatch_T *rmp;
6528 char_u *line; /* string to match against */
6529 colnr_T col; /* column to start looking for match */
6530{
6531 reg_match = rmp;
6532 reg_mmatch = NULL;
6533 reg_maxline = 0;
6534 reg_line_lbr = FALSE;
6535 reg_buf = curbuf;
6536 reg_win = NULL;
6537 ireg_ic = rmp->rm_ic;
6538#ifdef FEAT_MBYTE
6539 ireg_icombine = FALSE;
6540#endif
6541 ireg_maxcol = 0;
6542 return (nfa_regexec_both(line, col) != 0);
6543}
6544
6545#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6546 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6547
6548static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6549
6550/*
6551 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6552 */
6553 static int
6554nfa_regexec_nl(rmp, line, col)
6555 regmatch_T *rmp;
6556 char_u *line; /* string to match against */
6557 colnr_T col; /* column to start looking for match */
6558{
6559 reg_match = rmp;
6560 reg_mmatch = NULL;
6561 reg_maxline = 0;
6562 reg_line_lbr = TRUE;
6563 reg_buf = curbuf;
6564 reg_win = NULL;
6565 ireg_ic = rmp->rm_ic;
6566#ifdef FEAT_MBYTE
6567 ireg_icombine = FALSE;
6568#endif
6569 ireg_maxcol = 0;
6570 return (nfa_regexec_both(line, col) != 0);
6571}
6572#endif
6573
6574
6575/*
6576 * Match a regexp against multiple lines.
6577 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6578 * Uses curbuf for line count and 'iskeyword'.
6579 *
6580 * Return zero if there is no match. Return number of lines contained in the
6581 * match otherwise.
6582 *
6583 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6584 *
6585 * ! Also NOTE : match may actually be in another line. e.g.:
6586 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6587 *
6588 * +-------------------------+
6589 * |a |
6590 * |b |
6591 * |c |
6592 * | |
6593 * +-------------------------+
6594 *
6595 * then nfa_regexec_multi() returns 3. while the original
6596 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6597 *
6598 * FIXME if this behavior is not compatible.
6599 */
6600 static long
6601nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6602 regmmatch_T *rmp;
6603 win_T *win; /* window in which to search or NULL */
6604 buf_T *buf; /* buffer in which to search */
6605 linenr_T lnum; /* nr of line to start looking for match */
6606 colnr_T col; /* column to start looking for match */
6607 proftime_T *tm UNUSED; /* timeout limit or NULL */
6608{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006609 reg_match = NULL;
6610 reg_mmatch = rmp;
6611 reg_buf = buf;
6612 reg_win = win;
6613 reg_firstlnum = lnum;
6614 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6615 reg_line_lbr = FALSE;
6616 ireg_ic = rmp->rmm_ic;
6617#ifdef FEAT_MBYTE
6618 ireg_icombine = FALSE;
6619#endif
6620 ireg_maxcol = rmp->rmm_maxcol;
6621
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006622 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006623}
6624
6625#ifdef DEBUG
6626# undef ENABLE_LOG
6627#endif