blob: e49a243daf8cd2dde17e1fa29de656c9927b3f17 [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 */
41 NFA_END_NEG_COLL, /* [^abc] end (only used in postfix) */
42 NFA_RANGE, /* range of the two previous items (only
43 * used in postfix) */
44 NFA_RANGE_MIN, /* low end of a range */
45 NFA_RANGE_MAX, /* high end of a range */
46
47 NFA_CONCAT, /* concatenate two previous items (only
48 * used in postfix) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020049 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020050 NFA_STAR, /* greedy * */
51 NFA_STAR_NONGREEDY, /* non-greedy * */
52 NFA_QUEST, /* greedy \? */
53 NFA_QUEST_NONGREEDY, /* non-greedy \? */
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 Moolenaar61602c52013-06-01 19:54:43 +020067 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaar87953742013-06-05 18:52:40 +020068 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020069 NFA_END_INVISIBLE,
Bram Moolenaar87953742013-06-05 18:52:40 +020070 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020071 NFA_COMPOSING, /* Next nodes in NFA are part of the
72 composing multibyte char */
73 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020074 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020075
76 /* The following are used only in the postfix form, not in the NFA */
77 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
78 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
79 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
80 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
81 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
82
Bram Moolenaar5714b802013-05-28 22:03:20 +020083 NFA_BACKREF1, /* \1 */
84 NFA_BACKREF2, /* \2 */
85 NFA_BACKREF3, /* \3 */
86 NFA_BACKREF4, /* \4 */
87 NFA_BACKREF5, /* \5 */
88 NFA_BACKREF6, /* \6 */
89 NFA_BACKREF7, /* \7 */
90 NFA_BACKREF8, /* \8 */
91 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020092#ifdef FEAT_SYN_HL
93 NFA_ZREF1, /* \z1 */
94 NFA_ZREF2, /* \z2 */
95 NFA_ZREF3, /* \z3 */
96 NFA_ZREF4, /* \z4 */
97 NFA_ZREF5, /* \z5 */
98 NFA_ZREF6, /* \z6 */
99 NFA_ZREF7, /* \z7 */
100 NFA_ZREF8, /* \z8 */
101 NFA_ZREF9, /* \z9 */
102#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200103 NFA_SKIP, /* Skip characters */
104
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200105 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200106 NFA_MOPEN1,
107 NFA_MOPEN2,
108 NFA_MOPEN3,
109 NFA_MOPEN4,
110 NFA_MOPEN5,
111 NFA_MOPEN6,
112 NFA_MOPEN7,
113 NFA_MOPEN8,
114 NFA_MOPEN9,
115
116 NFA_MCLOSE,
117 NFA_MCLOSE1,
118 NFA_MCLOSE2,
119 NFA_MCLOSE3,
120 NFA_MCLOSE4,
121 NFA_MCLOSE5,
122 NFA_MCLOSE6,
123 NFA_MCLOSE7,
124 NFA_MCLOSE8,
125 NFA_MCLOSE9,
126
127#ifdef FEAT_SYN_HL
128 NFA_ZOPEN,
129 NFA_ZOPEN1,
130 NFA_ZOPEN2,
131 NFA_ZOPEN3,
132 NFA_ZOPEN4,
133 NFA_ZOPEN5,
134 NFA_ZOPEN6,
135 NFA_ZOPEN7,
136 NFA_ZOPEN8,
137 NFA_ZOPEN9,
138
139 NFA_ZCLOSE,
140 NFA_ZCLOSE1,
141 NFA_ZCLOSE2,
142 NFA_ZCLOSE3,
143 NFA_ZCLOSE4,
144 NFA_ZCLOSE5,
145 NFA_ZCLOSE6,
146 NFA_ZCLOSE7,
147 NFA_ZCLOSE8,
148 NFA_ZCLOSE9,
149#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200150
151 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200152 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200153 NFA_ANYOF, /* Match any character in this string. */
154 NFA_ANYBUT, /* Match any character not in this string. */
155 NFA_IDENT, /* Match identifier char */
156 NFA_SIDENT, /* Match identifier char but no digit */
157 NFA_KWORD, /* Match keyword char */
158 NFA_SKWORD, /* Match word char but no digit */
159 NFA_FNAME, /* Match file name char */
160 NFA_SFNAME, /* Match file name char but no digit */
161 NFA_PRINT, /* Match printable char */
162 NFA_SPRINT, /* Match printable char but no digit */
163 NFA_WHITE, /* Match whitespace char */
164 NFA_NWHITE, /* Match non-whitespace char */
165 NFA_DIGIT, /* Match digit char */
166 NFA_NDIGIT, /* Match non-digit char */
167 NFA_HEX, /* Match hex char */
168 NFA_NHEX, /* Match non-hex char */
169 NFA_OCTAL, /* Match octal char */
170 NFA_NOCTAL, /* Match non-octal char */
171 NFA_WORD, /* Match word char */
172 NFA_NWORD, /* Match non-word char */
173 NFA_HEAD, /* Match head char */
174 NFA_NHEAD, /* Match non-head char */
175 NFA_ALPHA, /* Match alpha char */
176 NFA_NALPHA, /* Match non-alpha char */
177 NFA_LOWER, /* Match lowercase char */
178 NFA_NLOWER, /* Match non-lowercase char */
179 NFA_UPPER, /* Match uppercase char */
180 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200181
182 NFA_CURSOR, /* Match cursor pos */
183 NFA_LNUM, /* Match line number */
184 NFA_LNUM_GT, /* Match > line number */
185 NFA_LNUM_LT, /* Match < line number */
186 NFA_COL, /* Match cursor column */
187 NFA_COL_GT, /* Match > cursor column */
188 NFA_COL_LT, /* Match < cursor column */
189 NFA_VCOL, /* Match cursor virtual column */
190 NFA_VCOL_GT, /* Match > cursor virtual column */
191 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200192 NFA_MARK, /* Match mark */
193 NFA_MARK_GT, /* Match > mark */
194 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200195 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200196
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200197 NFA_FIRST_NL = NFA_ANY + ADD_NL,
198 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
199
200 /* Character classes [:alnum:] etc */
201 NFA_CLASS_ALNUM,
202 NFA_CLASS_ALPHA,
203 NFA_CLASS_BLANK,
204 NFA_CLASS_CNTRL,
205 NFA_CLASS_DIGIT,
206 NFA_CLASS_GRAPH,
207 NFA_CLASS_LOWER,
208 NFA_CLASS_PRINT,
209 NFA_CLASS_PUNCT,
210 NFA_CLASS_SPACE,
211 NFA_CLASS_UPPER,
212 NFA_CLASS_XDIGIT,
213 NFA_CLASS_TAB,
214 NFA_CLASS_RETURN,
215 NFA_CLASS_BACKSPACE,
216 NFA_CLASS_ESCAPE
217};
218
219/* Keep in sync with classchars. */
220static int nfa_classcodes[] = {
221 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
222 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
223 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
224 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
225 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
226 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
227 NFA_UPPER, NFA_NUPPER
228};
229
230static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
231
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200232/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200233static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200234
Bram Moolenaar428e9872013-05-30 17:05:39 +0200235/* NFA regexp \1 .. \9 encountered. */
236static int nfa_has_backref;
237
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200238#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200239/* NFA regexp has \z( ), set zsubexpr. */
240static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200241#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200242
Bram Moolenaar963fee22013-05-26 21:47:28 +0200243/* Number of sub expressions actually being used during execution. 1 if only
244 * the whole match (subexpr 0) is used. */
245static int nfa_nsubexpr;
246
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static int *post_start; /* holds the postfix form of r.e. */
248static int *post_end;
249static int *post_ptr;
250
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200251static int nstate; /* Number of states in the NFA. Also used when
252 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200253static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200254
Bram Moolenaar307aa162013-06-02 16:34:21 +0200255/* If not NULL match must end at this position */
256static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200258/* listid is global, so that it increases on recursive calls to
259 * nfa_regmatch(), which means we don't have to clear the lastlist field of
260 * all the states. */
261static int nfa_listid;
262static int nfa_alt_listid;
263
264/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
265static int nfa_ll_index = 0;
266
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200267static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200268static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
269static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200270static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200271static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272static int nfa_regatom __ARGS((void));
273static int nfa_regpiece __ARGS((void));
274static int nfa_regconcat __ARGS((void));
275static int nfa_regbranch __ARGS((void));
276static int nfa_reg __ARGS((int paren));
277#ifdef DEBUG
278static void nfa_set_code __ARGS((int c));
279static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200280static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
281static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200282static void nfa_dump __ARGS((nfa_regprog_T *prog));
283#endif
284static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200285static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
287static int check_char_class __ARGS((int class, int c));
288static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200289static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
290static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200291static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200292static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200293static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
294static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
295static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
296static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
297
298/* helper functions used when doing re2post() ... regatom() parsing */
299#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200300 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200301 return FAIL; \
302 *post_ptr++ = c; \
303 } while (0)
304
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305/*
306 * Initialize internal variables before NFA compilation.
307 * Return OK on success, FAIL otherwise.
308 */
309 static int
310nfa_regcomp_start(expr, re_flags)
311 char_u *expr;
312 int re_flags; /* see vim_regcomp() */
313{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200314 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200315 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316
317 nstate = 0;
318 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200319 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200320 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200321
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200322 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200323 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200324 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200325
326 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200327 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200328
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200329 post_start = (int *)lalloc(postfix_size, TRUE);
330 if (post_start == NULL)
331 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200332 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200333 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200334 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200335 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200336
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200337 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338 regcomp_start(expr, re_flags);
339
340 return OK;
341}
342
343/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200344 * Figure out if the NFA state list starts with an anchor, must match at start
345 * of the line.
346 */
347 static int
348nfa_get_reganch(start, depth)
349 nfa_state_T *start;
350 int depth;
351{
352 nfa_state_T *p = start;
353
354 if (depth > 4)
355 return 0;
356
357 while (p != NULL)
358 {
359 switch (p->c)
360 {
361 case NFA_BOL:
362 case NFA_BOF:
363 return 1; /* yes! */
364
365 case NFA_ZSTART:
366 case NFA_ZEND:
367 case NFA_CURSOR:
368 case NFA_VISUAL:
369
370 case NFA_MOPEN:
371 case NFA_MOPEN1:
372 case NFA_MOPEN2:
373 case NFA_MOPEN3:
374 case NFA_MOPEN4:
375 case NFA_MOPEN5:
376 case NFA_MOPEN6:
377 case NFA_MOPEN7:
378 case NFA_MOPEN8:
379 case NFA_MOPEN9:
380 case NFA_NOPEN:
381#ifdef FEAT_SYN_HL
382 case NFA_ZOPEN:
383 case NFA_ZOPEN1:
384 case NFA_ZOPEN2:
385 case NFA_ZOPEN3:
386 case NFA_ZOPEN4:
387 case NFA_ZOPEN5:
388 case NFA_ZOPEN6:
389 case NFA_ZOPEN7:
390 case NFA_ZOPEN8:
391 case NFA_ZOPEN9:
392#endif
393 p = p->out;
394 break;
395
396 case NFA_SPLIT:
397 return nfa_get_reganch(p->out, depth + 1)
398 && nfa_get_reganch(p->out1, depth + 1);
399
400 default:
401 return 0; /* noooo */
402 }
403 }
404 return 0;
405}
406
407/*
408 * Figure out if the NFA state list starts with a character which must match
409 * at start of the match.
410 */
411 static int
412nfa_get_regstart(start, depth)
413 nfa_state_T *start;
414 int depth;
415{
416 nfa_state_T *p = start;
417
418 if (depth > 4)
419 return 0;
420
421 while (p != NULL)
422 {
423 switch (p->c)
424 {
425 /* all kinds of zero-width matches */
426 case NFA_BOL:
427 case NFA_BOF:
428 case NFA_BOW:
429 case NFA_EOW:
430 case NFA_ZSTART:
431 case NFA_ZEND:
432 case NFA_CURSOR:
433 case NFA_VISUAL:
434 case NFA_LNUM:
435 case NFA_LNUM_GT:
436 case NFA_LNUM_LT:
437 case NFA_COL:
438 case NFA_COL_GT:
439 case NFA_COL_LT:
440 case NFA_VCOL:
441 case NFA_VCOL_GT:
442 case NFA_VCOL_LT:
443 case NFA_MARK:
444 case NFA_MARK_GT:
445 case NFA_MARK_LT:
446
447 case NFA_MOPEN:
448 case NFA_MOPEN1:
449 case NFA_MOPEN2:
450 case NFA_MOPEN3:
451 case NFA_MOPEN4:
452 case NFA_MOPEN5:
453 case NFA_MOPEN6:
454 case NFA_MOPEN7:
455 case NFA_MOPEN8:
456 case NFA_MOPEN9:
457 case NFA_NOPEN:
458#ifdef FEAT_SYN_HL
459 case NFA_ZOPEN:
460 case NFA_ZOPEN1:
461 case NFA_ZOPEN2:
462 case NFA_ZOPEN3:
463 case NFA_ZOPEN4:
464 case NFA_ZOPEN5:
465 case NFA_ZOPEN6:
466 case NFA_ZOPEN7:
467 case NFA_ZOPEN8:
468 case NFA_ZOPEN9:
469#endif
470 p = p->out;
471 break;
472
473 case NFA_SPLIT:
474 {
475 int c1 = nfa_get_regstart(p->out, depth + 1);
476 int c2 = nfa_get_regstart(p->out1, depth + 1);
477
478 if (c1 == c2)
479 return c1; /* yes! */
480 return 0;
481 }
482
483 default:
484 if (p->c > 0 && !p->negated)
485 return p->c; /* yes! */
486 return 0;
487 }
488 }
489 return 0;
490}
491
492/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200493 * Allocate more space for post_start. Called when
494 * running above the estimated number of states.
495 */
496 static int
497realloc_post_list()
498{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200499 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200500 int new_max = nstate_max + 1000;
501 int *new_start;
502 int *old_start;
503
504 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
505 if (new_start == NULL)
506 return FAIL;
507 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200508 old_start = post_start;
509 post_start = new_start;
510 post_ptr = new_start + (post_ptr - old_start);
511 post_end = post_start + new_max;
512 vim_free(old_start);
513 return OK;
514}
515
516/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200517 * Search between "start" and "end" and try to recognize a
518 * character class in expanded form. For example [0-9].
519 * On success, return the id the character class to be emitted.
520 * On failure, return 0 (=FAIL)
521 * Start points to the first char of the range, while end should point
522 * to the closing brace.
523 */
524 static int
525nfa_recognize_char_class(start, end, extra_newl)
526 char_u *start;
527 char_u *end;
528 int extra_newl;
529{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200530# define CLASS_not 0x80
531# define CLASS_af 0x40
532# define CLASS_AF 0x20
533# define CLASS_az 0x10
534# define CLASS_AZ 0x08
535# define CLASS_o7 0x04
536# define CLASS_o9 0x02
537# define CLASS_underscore 0x01
538
539 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200541 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200542
543 if (extra_newl == TRUE)
544 newl = TRUE;
545
546 if (*end != ']')
547 return FAIL;
548 p = start;
549 if (*p == '^')
550 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200551 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200552 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200553 }
554
555 while (p < end)
556 {
557 if (p + 2 < end && *(p + 1) == '-')
558 {
559 switch (*p)
560 {
561 case '0':
562 if (*(p + 2) == '9')
563 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200564 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200565 break;
566 }
567 else
568 if (*(p + 2) == '7')
569 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200570 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200571 break;
572 }
573 case 'a':
574 if (*(p + 2) == 'z')
575 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200576 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200577 break;
578 }
579 else
580 if (*(p + 2) == 'f')
581 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200582 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200583 break;
584 }
585 case 'A':
586 if (*(p + 2) == 'Z')
587 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200588 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200589 break;
590 }
591 else
592 if (*(p + 2) == 'F')
593 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200594 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200595 break;
596 }
597 /* FALLTHROUGH */
598 default:
599 return FAIL;
600 }
601 p += 3;
602 }
603 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
604 {
605 newl = TRUE;
606 p += 2;
607 }
608 else if (*p == '_')
609 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200610 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611 p ++;
612 }
613 else if (*p == '\n')
614 {
615 newl = TRUE;
616 p ++;
617 }
618 else
619 return FAIL;
620 } /* while (p < end) */
621
622 if (p != end)
623 return FAIL;
624
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200625 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200626 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200627
628 switch (config)
629 {
630 case CLASS_o9:
631 return extra_newl + NFA_DIGIT;
632 case CLASS_not | CLASS_o9:
633 return extra_newl + NFA_NDIGIT;
634 case CLASS_af | CLASS_AF | CLASS_o9:
635 return extra_newl + NFA_HEX;
636 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
637 return extra_newl + NFA_NHEX;
638 case CLASS_o7:
639 return extra_newl + NFA_OCTAL;
640 case CLASS_not | CLASS_o7:
641 return extra_newl + NFA_NOCTAL;
642 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
643 return extra_newl + NFA_WORD;
644 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
645 return extra_newl + NFA_NWORD;
646 case CLASS_az | CLASS_AZ | CLASS_underscore:
647 return extra_newl + NFA_HEAD;
648 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
649 return extra_newl + NFA_NHEAD;
650 case CLASS_az | CLASS_AZ:
651 return extra_newl + NFA_ALPHA;
652 case CLASS_not | CLASS_az | CLASS_AZ:
653 return extra_newl + NFA_NALPHA;
654 case CLASS_az:
655 return extra_newl + NFA_LOWER;
656 case CLASS_not | CLASS_az:
657 return extra_newl + NFA_NLOWER;
658 case CLASS_AZ:
659 return extra_newl + NFA_UPPER;
660 case CLASS_not | CLASS_AZ:
661 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200662 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200663 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200664}
665
666/*
667 * Produce the bytes for equivalence class "c".
668 * Currently only handles latin1, latin9 and utf-8.
669 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
670 * equivalent to 'a OR b OR c'
671 *
672 * NOTE! When changing this function, also update reg_equi_class()
673 */
674 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200675nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200676 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200678#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679
680#ifdef FEAT_MBYTE
681 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
682 || STRCMP(p_enc, "iso-8859-15") == 0)
683#endif
684 {
685 switch (c)
686 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200687 case 'A': case 0300: case 0301: case 0302:
688 case 0303: case 0304: case 0305:
689 EMIT2('A'); EMIT2(0300); EMIT2(0301);
690 EMIT2(0302); EMIT2(0303); EMIT2(0304);
691 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692 return OK;
693
Bram Moolenaar417bad22013-06-07 14:08:30 +0200694 case 'C': case 0307:
695 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200696 return OK;
697
Bram Moolenaar417bad22013-06-07 14:08:30 +0200698 case 'E': case 0310: case 0311: case 0312: case 0313:
699 EMIT2('E'); EMIT2(0310); EMIT2(0311);
700 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200701 return OK;
702
Bram Moolenaar417bad22013-06-07 14:08:30 +0200703 case 'I': case 0314: case 0315: case 0316: case 0317:
704 EMIT2('I'); EMIT2(0314); EMIT2(0315);
705 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200706 return OK;
707
Bram Moolenaar417bad22013-06-07 14:08:30 +0200708 case 'N': case 0321:
709 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200710 return OK;
711
Bram Moolenaar417bad22013-06-07 14:08:30 +0200712 case 'O': case 0322: case 0323: case 0324: case 0325:
713 case 0326:
714 EMIT2('O'); EMIT2(0322); EMIT2(0323);
715 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200716 return OK;
717
Bram Moolenaar417bad22013-06-07 14:08:30 +0200718 case 'U': case 0331: case 0332: case 0333: case 0334:
719 EMIT2('U'); EMIT2(0331); EMIT2(0332);
720 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200721 return OK;
722
Bram Moolenaar417bad22013-06-07 14:08:30 +0200723 case 'Y': case 0335:
724 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200725 return OK;
726
Bram Moolenaar417bad22013-06-07 14:08:30 +0200727 case 'a': case 0340: case 0341: case 0342:
728 case 0343: case 0344: case 0345:
729 EMIT2('a'); EMIT2(0340); EMIT2(0341);
730 EMIT2(0342); EMIT2(0343); EMIT2(0344);
731 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732 return OK;
733
Bram Moolenaar417bad22013-06-07 14:08:30 +0200734 case 'c': case 0347:
735 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200736 return OK;
737
Bram Moolenaar417bad22013-06-07 14:08:30 +0200738 case 'e': case 0350: case 0351: case 0352: case 0353:
739 EMIT2('e'); EMIT2(0350); EMIT2(0351);
740 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200741 return OK;
742
Bram Moolenaar417bad22013-06-07 14:08:30 +0200743 case 'i': case 0354: case 0355: case 0356: case 0357:
744 EMIT2('i'); EMIT2(0354); EMIT2(0355);
745 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200746 return OK;
747
Bram Moolenaar417bad22013-06-07 14:08:30 +0200748 case 'n': case 0361:
749 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750 return OK;
751
Bram Moolenaar417bad22013-06-07 14:08:30 +0200752 case 'o': case 0362: case 0363: case 0364: case 0365:
753 case 0366:
754 EMIT2('o'); EMIT2(0362); EMIT2(0363);
755 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200756 return OK;
757
Bram Moolenaar417bad22013-06-07 14:08:30 +0200758 case 'u': case 0371: case 0372: case 0373: case 0374:
759 EMIT2('u'); EMIT2(0371); EMIT2(0372);
760 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200761 return OK;
762
Bram Moolenaar417bad22013-06-07 14:08:30 +0200763 case 'y': case 0375: case 0377:
764 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200765 return OK;
766
767 default:
768 return FAIL;
769 }
770 }
771
772 EMIT(c);
773 return OK;
774#undef EMIT2
775}
776
777/*
778 * Code to parse regular expression.
779 *
780 * We try to reuse parsing functions in regexp.c to
781 * minimize surprise and keep the syntax consistent.
782 */
783
784/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200785 * Parse the lowest level.
786 *
787 * An atom can be one of a long list of items. Many atoms match one character
788 * in the text. It is often an ordinary character or a character class.
789 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
790 * is only for syntax highlighting.
791 *
792 * atom ::= ordinary-atom
793 * or \( pattern \)
794 * or \%( pattern \)
795 * or \z( pattern \)
796 */
797 static int
798nfa_regatom()
799{
800 int c;
801 int charclass;
802 int equiclass;
803 int collclass;
804 int got_coll_char;
805 char_u *p;
806 char_u *endp;
807#ifdef FEAT_MBYTE
808 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200809#endif
810 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200811 int emit_range;
812 int negated;
813 int result;
814 int startc = -1;
815 int endc = -1;
816 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200818 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200819 switch (c)
820 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200821 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200822 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
823
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200824 case Magic('^'):
825 EMIT(NFA_BOL);
826 break;
827
828 case Magic('$'):
829 EMIT(NFA_EOL);
830#if defined(FEAT_SYN_HL) || defined(PROTO)
831 had_eol = TRUE;
832#endif
833 break;
834
835 case Magic('<'):
836 EMIT(NFA_BOW);
837 break;
838
839 case Magic('>'):
840 EMIT(NFA_EOW);
841 break;
842
843 case Magic('_'):
844 c = no_Magic(getchr());
845 if (c == '^') /* "\_^" is start-of-line */
846 {
847 EMIT(NFA_BOL);
848 break;
849 }
850 if (c == '$') /* "\_$" is end-of-line */
851 {
852 EMIT(NFA_EOL);
853#if defined(FEAT_SYN_HL) || defined(PROTO)
854 had_eol = TRUE;
855#endif
856 break;
857 }
858
859 extra = ADD_NL;
860
861 /* "\_[" is collection plus newline */
862 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200863 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200864
865 /* "\_x" is character class plus newline */
866 /*FALLTHROUGH*/
867
868 /*
869 * Character classes.
870 */
871 case Magic('.'):
872 case Magic('i'):
873 case Magic('I'):
874 case Magic('k'):
875 case Magic('K'):
876 case Magic('f'):
877 case Magic('F'):
878 case Magic('p'):
879 case Magic('P'):
880 case Magic('s'):
881 case Magic('S'):
882 case Magic('d'):
883 case Magic('D'):
884 case Magic('x'):
885 case Magic('X'):
886 case Magic('o'):
887 case Magic('O'):
888 case Magic('w'):
889 case Magic('W'):
890 case Magic('h'):
891 case Magic('H'):
892 case Magic('a'):
893 case Magic('A'):
894 case Magic('l'):
895 case Magic('L'):
896 case Magic('u'):
897 case Magic('U'):
898 p = vim_strchr(classchars, no_Magic(c));
899 if (p == NULL)
900 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200901 EMSGN("INTERNAL: Unknown character class char: %ld", c);
902 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200903 }
904#ifdef FEAT_MBYTE
905 /* When '.' is followed by a composing char ignore the dot, so that
906 * the composing char is matched here. */
907 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
908 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200909 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200910 c = getchr();
911 goto nfa_do_multibyte;
912 }
913#endif
914 EMIT(nfa_classcodes[p - classchars]);
915 if (extra == ADD_NL)
916 {
917 EMIT(NFA_NEWL);
918 EMIT(NFA_OR);
919 regflags |= RF_HASNL;
920 }
921 break;
922
923 case Magic('n'):
924 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200925 /* In a string "\n" matches a newline character. */
926 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200927 else
928 {
929 /* In buffer text "\n" matches the end of a line. */
930 EMIT(NFA_NEWL);
931 regflags |= RF_HASNL;
932 }
933 break;
934
935 case Magic('('):
936 if (nfa_reg(REG_PAREN) == FAIL)
937 return FAIL; /* cascaded error */
938 break;
939
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200940 case Magic('|'):
941 case Magic('&'):
942 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200943 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200944 return FAIL;
945
946 case Magic('='):
947 case Magic('?'):
948 case Magic('+'):
949 case Magic('@'):
950 case Magic('*'):
951 case Magic('{'):
952 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +0200953 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200954 return FAIL;
955
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200956 case Magic('~'):
957 {
958 char_u *lp;
959
960 /* Previous substitute pattern.
961 * Generated as "\%(pattern\)". */
962 if (reg_prev_sub == NULL)
963 {
964 EMSG(_(e_nopresub));
965 return FAIL;
966 }
967 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
968 {
969 EMIT(PTR2CHAR(lp));
970 if (lp != reg_prev_sub)
971 EMIT(NFA_CONCAT);
972 }
973 EMIT(NFA_NOPEN);
974 break;
975 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200976
Bram Moolenaar428e9872013-05-30 17:05:39 +0200977 case Magic('1'):
978 case Magic('2'):
979 case Magic('3'):
980 case Magic('4'):
981 case Magic('5'):
982 case Magic('6'):
983 case Magic('7'):
984 case Magic('8'):
985 case Magic('9'):
986 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
987 nfa_has_backref = TRUE;
988 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200989
990 case Magic('z'):
991 c = no_Magic(getchr());
992 switch (c)
993 {
994 case 's':
995 EMIT(NFA_ZSTART);
996 break;
997 case 'e':
998 EMIT(NFA_ZEND);
999 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001000 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001001#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001002 case '1':
1003 case '2':
1004 case '3':
1005 case '4':
1006 case '5':
1007 case '6':
1008 case '7':
1009 case '8':
1010 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001011 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001012 if (reg_do_extmatch != REX_USE)
1013 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001014 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1015 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001016 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001017 re_has_z = REX_USE;
1018 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001019 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001020 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001021 if (reg_do_extmatch != REX_SET)
1022 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001023 if (nfa_reg(REG_ZPAREN) == FAIL)
1024 return FAIL; /* cascaded error */
1025 re_has_z = REX_SET;
1026 break;
1027#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001028 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001029 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001030 no_Magic(c));
1031 return FAIL;
1032 }
1033 break;
1034
1035 case Magic('%'):
1036 c = no_Magic(getchr());
1037 switch (c)
1038 {
1039 /* () without a back reference */
1040 case '(':
1041 if (nfa_reg(REG_NPAREN) == FAIL)
1042 return FAIL;
1043 EMIT(NFA_NOPEN);
1044 break;
1045
1046 case 'd': /* %d123 decimal */
1047 case 'o': /* %o123 octal */
1048 case 'x': /* %xab hex 2 */
1049 case 'u': /* %uabcd hex 4 */
1050 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001051 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001052 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001053
Bram Moolenaar47196582013-05-25 22:04:23 +02001054 switch (c)
1055 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001056 case 'd': nr = getdecchrs(); break;
1057 case 'o': nr = getoctchrs(); break;
1058 case 'x': nr = gethexchrs(2); break;
1059 case 'u': nr = gethexchrs(4); break;
1060 case 'U': nr = gethexchrs(8); break;
1061 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001062 }
1063
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001064 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001065 EMSG2_RET_FAIL(
1066 _("E678: Invalid character after %s%%[dxouU]"),
1067 reg_magic == MAGIC_ALL);
1068 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001069 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001070 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001071 break;
1072
1073 /* Catch \%^ and \%$ regardless of where they appear in the
1074 * pattern -- regardless of whether or not it makes sense. */
1075 case '^':
1076 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001077 break;
1078
1079 case '$':
1080 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001081 break;
1082
1083 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001084 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 break;
1086
1087 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001088 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001089 break;
1090
1091 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001092 {
1093 int n;
1094
1095 /* \%[abc] */
1096 for (n = 0; (c = getchr()) != ']'; ++n)
1097 {
1098 if (c == NUL)
1099 EMSG2_RET_FAIL(_(e_missing_sb),
1100 reg_magic == MAGIC_ALL);
1101 EMIT(c);
1102 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001103 if (n == 0)
1104 EMSG2_RET_FAIL(_(e_empty_sb),
1105 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001106 EMIT(NFA_OPT_CHARS);
1107 EMIT(n);
1108 break;
1109 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001110
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001111 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001112 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001113 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001114 int cmp = c;
1115
1116 if (c == '<' || c == '>')
1117 c = getchr();
1118 while (VIM_ISDIGIT(c))
1119 {
1120 n = n * 10 + (c - '0');
1121 c = getchr();
1122 }
1123 if (c == 'l' || c == 'c' || c == 'v')
1124 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001125 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001126 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001127 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001128 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001129 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001130 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001131 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001132 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001133 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001134 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001135 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001136 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001137 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001138 break;
1139 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001140 else if (c == '\'' && n == 0)
1141 {
1142 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001143 EMIT(cmp == '<' ? NFA_MARK_LT :
1144 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001145 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001146 break;
1147 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001148 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001149 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1150 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001151 return FAIL;
1152 }
1153 break;
1154
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001155 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001156collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001157 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001158 * [abc] uses NFA_START_COLL - NFA_END_COLL
1159 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1160 * Each character is produced as a regular state, using
1161 * NFA_CONCAT to bind them together.
1162 * Besides normal characters there can be:
1163 * - character classes NFA_CLASS_*
1164 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001165 */
1166
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001167 p = regparse;
1168 endp = skip_anyof(p);
1169 if (*endp == ']')
1170 {
1171 /*
1172 * Try to reverse engineer character classes. For example,
1173 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1174 * and perform the necessary substitutions in the NFA.
1175 */
1176 result = nfa_recognize_char_class(regparse, endp,
1177 extra == ADD_NL);
1178 if (result != FAIL)
1179 {
1180 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1181 EMIT(result);
1182 else /* must be char class + newline */
1183 {
1184 EMIT(result - ADD_NL);
1185 EMIT(NFA_NEWL);
1186 EMIT(NFA_OR);
1187 }
1188 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001189 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190 return OK;
1191 }
1192 /*
1193 * Failed to recognize a character class. Use the simple
1194 * version that turns [abc] into 'a' OR 'b' OR 'c'
1195 */
1196 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001197 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001198 if (*regparse == '^') /* negated range */
1199 {
1200 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001201 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001202 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001204 else
1205 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 if (*regparse == '-')
1207 {
1208 startc = '-';
1209 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001210 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001211 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 }
1213 /* Emit the OR branches for each character in the [] */
1214 emit_range = FALSE;
1215 while (regparse < endp)
1216 {
1217 oldstartc = startc;
1218 startc = -1;
1219 got_coll_char = FALSE;
1220 if (*regparse == '[')
1221 {
1222 /* Check for [: :], [= =], [. .] */
1223 equiclass = collclass = 0;
1224 charclass = get_char_class(&regparse);
1225 if (charclass == CLASS_NONE)
1226 {
1227 equiclass = get_equi_class(&regparse);
1228 if (equiclass == 0)
1229 collclass = get_coll_element(&regparse);
1230 }
1231
1232 /* Character class like [:alpha:] */
1233 if (charclass != CLASS_NONE)
1234 {
1235 switch (charclass)
1236 {
1237 case CLASS_ALNUM:
1238 EMIT(NFA_CLASS_ALNUM);
1239 break;
1240 case CLASS_ALPHA:
1241 EMIT(NFA_CLASS_ALPHA);
1242 break;
1243 case CLASS_BLANK:
1244 EMIT(NFA_CLASS_BLANK);
1245 break;
1246 case CLASS_CNTRL:
1247 EMIT(NFA_CLASS_CNTRL);
1248 break;
1249 case CLASS_DIGIT:
1250 EMIT(NFA_CLASS_DIGIT);
1251 break;
1252 case CLASS_GRAPH:
1253 EMIT(NFA_CLASS_GRAPH);
1254 break;
1255 case CLASS_LOWER:
1256 EMIT(NFA_CLASS_LOWER);
1257 break;
1258 case CLASS_PRINT:
1259 EMIT(NFA_CLASS_PRINT);
1260 break;
1261 case CLASS_PUNCT:
1262 EMIT(NFA_CLASS_PUNCT);
1263 break;
1264 case CLASS_SPACE:
1265 EMIT(NFA_CLASS_SPACE);
1266 break;
1267 case CLASS_UPPER:
1268 EMIT(NFA_CLASS_UPPER);
1269 break;
1270 case CLASS_XDIGIT:
1271 EMIT(NFA_CLASS_XDIGIT);
1272 break;
1273 case CLASS_TAB:
1274 EMIT(NFA_CLASS_TAB);
1275 break;
1276 case CLASS_RETURN:
1277 EMIT(NFA_CLASS_RETURN);
1278 break;
1279 case CLASS_BACKSPACE:
1280 EMIT(NFA_CLASS_BACKSPACE);
1281 break;
1282 case CLASS_ESCAPE:
1283 EMIT(NFA_CLASS_ESCAPE);
1284 break;
1285 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001286 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001287 continue;
1288 }
1289 /* Try equivalence class [=a=] and the like */
1290 if (equiclass != 0)
1291 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001292 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293 if (result == FAIL)
1294 {
1295 /* should never happen */
1296 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1297 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001298 continue;
1299 }
1300 /* Try collating class like [. .] */
1301 if (collclass != 0)
1302 {
1303 startc = collclass; /* allow [.a.]-x as a range */
1304 /* Will emit the proper atom at the end of the
1305 * while loop. */
1306 }
1307 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001308 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1309 * start character. */
1310 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001311 {
1312 emit_range = TRUE;
1313 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001314 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 continue; /* reading the end of the range */
1316 }
1317
1318 /* Now handle simple and escaped characters.
1319 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1320 * accepts "\t", "\e", etc., but only when the 'l' flag in
1321 * 'cpoptions' is not included.
1322 * Posix doesn't recognize backslash at all.
1323 */
1324 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001325 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001326 && regparse + 1 <= endp
1327 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001328 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001329 && vim_strchr(REGEXP_ABBR, regparse[1])
1330 != NULL)
1331 )
1332 )
1333 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001334 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001335
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001336 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001337 startc = reg_string ? NL : NFA_NEWL;
1338 else
1339 if (*regparse == 'd'
1340 || *regparse == 'o'
1341 || *regparse == 'x'
1342 || *regparse == 'u'
1343 || *regparse == 'U'
1344 )
1345 {
1346 /* TODO(RE) This needs more testing */
1347 startc = coll_get_char();
1348 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001349 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001350 }
1351 else
1352 {
1353 /* \r,\t,\e,\b */
1354 startc = backslash_trans(*regparse);
1355 }
1356 }
1357
1358 /* Normal printable char */
1359 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001360 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001361
1362 /* Previous char was '-', so this char is end of range. */
1363 if (emit_range)
1364 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001365 endc = startc;
1366 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001367 if (startc > endc)
1368 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001369
1370 if (endc > startc + 2)
1371 {
1372 /* Emit a range instead of the sequence of
1373 * individual characters. */
1374 if (startc == 0)
1375 /* \x00 is translated to \x0a, start at \x01. */
1376 EMIT(1);
1377 else
1378 --post_ptr; /* remove NFA_CONCAT */
1379 EMIT(endc);
1380 EMIT(NFA_RANGE);
1381 EMIT(NFA_CONCAT);
1382 }
1383 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001384#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001385 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386 || (*mb_char2len)(endc) > 1))
1387 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001388 /* Emit the characters in the range.
1389 * "startc" was already emitted, so skip it.
1390 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001391 for (c = startc + 1; c <= endc; c++)
1392 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001393 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001394 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001395 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001396 }
1397 else
1398#endif
1399 {
1400#ifdef EBCDIC
1401 int alpha_only = FALSE;
1402
1403 /* for alphabetical range skip the gaps
1404 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1405 if (isalpha(startc) && isalpha(endc))
1406 alpha_only = TRUE;
1407#endif
1408 /* Emit the range. "startc" was already emitted, so
1409 * skip it. */
1410 for (c = startc + 1; c <= endc; c++)
1411#ifdef EBCDIC
1412 if (!alpha_only || isalpha(startc))
1413#endif
1414 {
1415 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001416 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001418 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001419 emit_range = FALSE;
1420 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001421 }
1422 else
1423 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001424 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001425 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001426 * Normally, simply emit startc. But if we get char
1427 * code=0 from a collating char, then replace it with
1428 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001429 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001430 * the backtracking engine. */
1431 if (startc == NFA_NEWL)
1432 {
1433 /* Line break can't be matched as part of the
1434 * collection, add an OR below. But not for negated
1435 * range. */
1436 if (!negated)
1437 extra = ADD_NL;
1438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001440 {
1441 if (got_coll_char == TRUE && startc == 0)
1442 EMIT(0x0a);
1443 else
1444 EMIT(startc);
1445 EMIT(NFA_CONCAT);
1446 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 }
1448
Bram Moolenaar51a29832013-05-28 22:30:35 +02001449 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 } /* while (p < endp) */
1451
Bram Moolenaar51a29832013-05-28 22:30:35 +02001452 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001453 if (*regparse == '-') /* if last, '-' is just a char */
1454 {
1455 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001456 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001457 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001458 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001459
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001460 /* skip the trailing ] */
1461 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001462 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001463
1464 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001466 EMIT(NFA_END_NEG_COLL);
1467 else
1468 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001469
1470 /* \_[] also matches \n but it's not negated */
1471 if (extra == ADD_NL)
1472 {
1473 EMIT(reg_string ? NL : NFA_NEWL);
1474 EMIT(NFA_OR);
1475 }
1476
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001478 } /* if exists closing ] */
1479
1480 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001481 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001482 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001484 default:
1485 {
1486#ifdef FEAT_MBYTE
1487 int plen;
1488
1489nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001490 /* plen is length of current char with composing chars */
1491 if (enc_utf8 && ((*mb_char2len)(c)
1492 != (plen = (*mb_ptr2len)(old_regparse))
1493 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001495 int i = 0;
1496
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001497 /* A base character plus composing characters, or just one
1498 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001499 * This requires creating a separate atom as if enclosing
1500 * the characters in (), where NFA_COMPOSING is the ( and
1501 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001502 * building the postfix form, not the NFA itself;
1503 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001504 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001505 for (;;)
1506 {
1507 EMIT(c);
1508 if (i > 0)
1509 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001510 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001511 break;
1512 c = utf_ptr2char(old_regparse + i);
1513 }
1514 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001515 regparse = old_regparse + plen;
1516 }
1517 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518#endif
1519 {
1520 c = no_Magic(c);
1521 EMIT(c);
1522 }
1523 return OK;
1524 }
1525 }
1526
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001527 return OK;
1528}
1529
1530/*
1531 * Parse something followed by possible [*+=].
1532 *
1533 * A piece is an atom, possibly followed by a multi, an indication of how many
1534 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1535 * characters: "", "a", "aa", etc.
1536 *
1537 * piece ::= atom
1538 * or atom multi
1539 */
1540 static int
1541nfa_regpiece()
1542{
1543 int i;
1544 int op;
1545 int ret;
1546 long minval, maxval;
1547 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001548 parse_state_T old_state;
1549 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001551 int old_post_pos;
1552 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 int quest;
1554
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001555 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1556 * next. */
1557 save_parse_state(&old_state);
1558
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001560 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561
1562 ret = nfa_regatom();
1563 if (ret == FAIL)
1564 return FAIL; /* cascaded error */
1565
1566 op = peekchr();
1567 if (re_multi_type(op) == NOT_MULTI)
1568 return OK;
1569
1570 skipchr();
1571 switch (op)
1572 {
1573 case Magic('*'):
1574 EMIT(NFA_STAR);
1575 break;
1576
1577 case Magic('+'):
1578 /*
1579 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1580 * first and only submatch would be "aaa". But the backtracking
1581 * engine interprets the plus as "try matching one more time", and
1582 * a* matches a second time at the end of the input, the empty
1583 * string.
1584 * The submatch will the empty string.
1585 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001586 * In order to be consistent with the old engine, we replace
1587 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001588 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001589 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001590 curchr = -1;
1591 if (nfa_regatom() == FAIL)
1592 return FAIL;
1593 EMIT(NFA_STAR);
1594 EMIT(NFA_CONCAT);
1595 skipchr(); /* skip the \+ */
1596 break;
1597
1598 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001599 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001600 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001601 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602 switch(op)
1603 {
1604 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001605 /* \@= */
1606 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001607 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001608 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001609 /* \@! */
1610 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001611 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001613 op = no_Magic(getchr());
1614 if (op == '=')
1615 /* \@<= */
1616 i = NFA_PREV_ATOM_JUST_BEFORE;
1617 else if (op == '!')
1618 /* \@<! */
1619 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1620 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001621 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001622 /* \@> */
1623 i = NFA_PREV_ATOM_LIKE_PATTERN;
1624 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001625 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001626 if (i == 0)
1627 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001628 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1629 return FAIL;
1630 }
1631 EMIT(i);
1632 if (i == NFA_PREV_ATOM_JUST_BEFORE
1633 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1634 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 break;
1636
1637 case Magic('?'):
1638 case Magic('='):
1639 EMIT(NFA_QUEST);
1640 break;
1641
1642 case Magic('{'):
1643 /* a{2,5} will expand to 'aaa?a?a?'
1644 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1645 * version of '?'
1646 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1647 * parenthesis have the same id
1648 */
1649
1650 greedy = TRUE;
1651 c2 = peekchr();
1652 if (c2 == '-' || c2 == Magic('-'))
1653 {
1654 skipchr();
1655 greedy = FALSE;
1656 }
1657 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001659
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001660 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1661 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001662 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001664 if (greedy)
1665 /* \{}, \{0,} */
1666 EMIT(NFA_STAR);
1667 else
1668 /* \{-}, \{-0,} */
1669 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001670 break;
1671 }
1672
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001673 /* Special case: x{0} or x{-0} */
1674 if (maxval == 0)
1675 {
1676 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001677 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1679 EMIT(NFA_SKIP_CHAR);
1680 return OK;
1681 }
1682
1683 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001684 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001685 /* Save parse state after the repeated atom and the \{} */
1686 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001687
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001688 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1689 for (i = 0; i < maxval; i++)
1690 {
1691 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001692 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001693 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694 if (nfa_regatom() == FAIL)
1695 return FAIL;
1696 /* after "minval" times, atoms are optional */
1697 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001698 {
1699 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001700 {
1701 if (greedy)
1702 EMIT(NFA_STAR);
1703 else
1704 EMIT(NFA_STAR_NONGREEDY);
1705 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001706 else
1707 EMIT(quest);
1708 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001709 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001710 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001711 if (i + 1 > minval && maxval == MAX_LIMIT)
1712 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713 }
1714
1715 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001716 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001717 curchr = -1;
1718
1719 break;
1720
1721
1722 default:
1723 break;
1724 } /* end switch */
1725
1726 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001727 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001728 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001729
1730 return OK;
1731}
1732
1733/*
1734 * Parse one or more pieces, concatenated. It matches a match for the
1735 * first piece, followed by a match for the second piece, etc. Example:
1736 * "f[0-9]b", first matches "f", then a digit and then "b".
1737 *
1738 * concat ::= piece
1739 * or piece piece
1740 * or piece piece piece
1741 * etc.
1742 */
1743 static int
1744nfa_regconcat()
1745{
1746 int cont = TRUE;
1747 int first = TRUE;
1748
1749 while (cont)
1750 {
1751 switch (peekchr())
1752 {
1753 case NUL:
1754 case Magic('|'):
1755 case Magic('&'):
1756 case Magic(')'):
1757 cont = FALSE;
1758 break;
1759
1760 case Magic('Z'):
1761#ifdef FEAT_MBYTE
1762 regflags |= RF_ICOMBINE;
1763#endif
1764 skipchr_keepstart();
1765 break;
1766 case Magic('c'):
1767 regflags |= RF_ICASE;
1768 skipchr_keepstart();
1769 break;
1770 case Magic('C'):
1771 regflags |= RF_NOICASE;
1772 skipchr_keepstart();
1773 break;
1774 case Magic('v'):
1775 reg_magic = MAGIC_ALL;
1776 skipchr_keepstart();
1777 curchr = -1;
1778 break;
1779 case Magic('m'):
1780 reg_magic = MAGIC_ON;
1781 skipchr_keepstart();
1782 curchr = -1;
1783 break;
1784 case Magic('M'):
1785 reg_magic = MAGIC_OFF;
1786 skipchr_keepstart();
1787 curchr = -1;
1788 break;
1789 case Magic('V'):
1790 reg_magic = MAGIC_NONE;
1791 skipchr_keepstart();
1792 curchr = -1;
1793 break;
1794
1795 default:
1796 if (nfa_regpiece() == FAIL)
1797 return FAIL;
1798 if (first == FALSE)
1799 EMIT(NFA_CONCAT);
1800 else
1801 first = FALSE;
1802 break;
1803 }
1804 }
1805
1806 return OK;
1807}
1808
1809/*
1810 * Parse a branch, one or more concats, separated by "\&". It matches the
1811 * last concat, but only if all the preceding concats also match at the same
1812 * position. Examples:
1813 * "foobeep\&..." matches "foo" in "foobeep".
1814 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1815 *
1816 * branch ::= concat
1817 * or concat \& concat
1818 * or concat \& concat \& concat
1819 * etc.
1820 */
1821 static int
1822nfa_regbranch()
1823{
1824 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001825 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001826
Bram Moolenaar16299b52013-05-30 18:45:23 +02001827 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001828
1829 /* First branch, possibly the only one */
1830 if (nfa_regconcat() == FAIL)
1831 return FAIL;
1832
1833 ch = peekchr();
1834 /* Try next concats */
1835 while (ch == Magic('&'))
1836 {
1837 skipchr();
1838 EMIT(NFA_NOPEN);
1839 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001840 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001841 if (nfa_regconcat() == FAIL)
1842 return FAIL;
1843 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001844 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001845 EMIT(NFA_SKIP_CHAR);
1846 EMIT(NFA_CONCAT);
1847 ch = peekchr();
1848 }
1849
1850 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001851 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001852 EMIT(NFA_SKIP_CHAR);
1853
1854 return OK;
1855}
1856
1857/*
1858 * Parse a pattern, one or more branches, separated by "\|". It matches
1859 * anything that matches one of the branches. Example: "foo\|beep" matches
1860 * "foo" and matches "beep". If more than one branch matches, the first one
1861 * is used.
1862 *
1863 * pattern ::= branch
1864 * or branch \| branch
1865 * or branch \| branch \| branch
1866 * etc.
1867 */
1868 static int
1869nfa_reg(paren)
1870 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1871{
1872 int parno = 0;
1873
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 if (paren == REG_PAREN)
1875 {
1876 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001878 parno = regnpar++;
1879 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001880#ifdef FEAT_SYN_HL
1881 else if (paren == REG_ZPAREN)
1882 {
1883 /* Make a ZOPEN node. */
1884 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001885 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001886 parno = regnzpar++;
1887 }
1888#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889
1890 if (nfa_regbranch() == FAIL)
1891 return FAIL; /* cascaded error */
1892
1893 while (peekchr() == Magic('|'))
1894 {
1895 skipchr();
1896 if (nfa_regbranch() == FAIL)
1897 return FAIL; /* cascaded error */
1898 EMIT(NFA_OR);
1899 }
1900
1901 /* Check for proper termination. */
1902 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1903 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 if (paren == REG_NPAREN)
1905 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1906 else
1907 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1908 }
1909 else if (paren == REG_NOPAREN && peekchr() != NUL)
1910 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001911 if (peekchr() == Magic(')'))
1912 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1913 else
1914 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1915 }
1916 /*
1917 * Here we set the flag allowing back references to this set of
1918 * parentheses.
1919 */
1920 if (paren == REG_PAREN)
1921 {
1922 had_endbrace[parno] = TRUE; /* have seen the close paren */
1923 EMIT(NFA_MOPEN + parno);
1924 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001925#ifdef FEAT_SYN_HL
1926 else if (paren == REG_ZPAREN)
1927 EMIT(NFA_ZOPEN + parno);
1928#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929
1930 return OK;
1931}
1932
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933#ifdef DEBUG
1934static char_u code[50];
1935
1936 static void
1937nfa_set_code(c)
1938 int c;
1939{
1940 int addnl = FALSE;
1941
1942 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1943 {
1944 addnl = TRUE;
1945 c -= ADD_NL;
1946 }
1947
1948 STRCPY(code, "");
1949 switch (c)
1950 {
1951 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1952 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1953 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1954 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1955 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1956 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1957
Bram Moolenaar5714b802013-05-28 22:03:20 +02001958 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1959 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1960 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1961 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1962 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1963 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1964 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1965 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1966 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001967#ifdef FEAT_SYN_HL
1968 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1969 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1970 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1971 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1972 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1973 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1974 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1975 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1976 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1977#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001978 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1979
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001980 case NFA_PREV_ATOM_NO_WIDTH:
1981 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001982 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1983 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001984 case NFA_PREV_ATOM_JUST_BEFORE:
1985 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1986 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1987 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001988 case NFA_PREV_ATOM_LIKE_PATTERN:
1989 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
1990
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001991 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1992 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001993 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001994 case NFA_START_INVISIBLE_BEFORE:
1995 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001996 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001997 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001998 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002000 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2001 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002002 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002004 case NFA_MOPEN:
2005 case NFA_MOPEN1:
2006 case NFA_MOPEN2:
2007 case NFA_MOPEN3:
2008 case NFA_MOPEN4:
2009 case NFA_MOPEN5:
2010 case NFA_MOPEN6:
2011 case NFA_MOPEN7:
2012 case NFA_MOPEN8:
2013 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014 STRCPY(code, "NFA_MOPEN(x)");
2015 code[10] = c - NFA_MOPEN + '0';
2016 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002017 case NFA_MCLOSE:
2018 case NFA_MCLOSE1:
2019 case NFA_MCLOSE2:
2020 case NFA_MCLOSE3:
2021 case NFA_MCLOSE4:
2022 case NFA_MCLOSE5:
2023 case NFA_MCLOSE6:
2024 case NFA_MCLOSE7:
2025 case NFA_MCLOSE8:
2026 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002027 STRCPY(code, "NFA_MCLOSE(x)");
2028 code[11] = c - NFA_MCLOSE + '0';
2029 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002030#ifdef FEAT_SYN_HL
2031 case NFA_ZOPEN:
2032 case NFA_ZOPEN1:
2033 case NFA_ZOPEN2:
2034 case NFA_ZOPEN3:
2035 case NFA_ZOPEN4:
2036 case NFA_ZOPEN5:
2037 case NFA_ZOPEN6:
2038 case NFA_ZOPEN7:
2039 case NFA_ZOPEN8:
2040 case NFA_ZOPEN9:
2041 STRCPY(code, "NFA_ZOPEN(x)");
2042 code[10] = c - NFA_ZOPEN + '0';
2043 break;
2044 case NFA_ZCLOSE:
2045 case NFA_ZCLOSE1:
2046 case NFA_ZCLOSE2:
2047 case NFA_ZCLOSE3:
2048 case NFA_ZCLOSE4:
2049 case NFA_ZCLOSE5:
2050 case NFA_ZCLOSE6:
2051 case NFA_ZCLOSE7:
2052 case NFA_ZCLOSE8:
2053 case NFA_ZCLOSE9:
2054 STRCPY(code, "NFA_ZCLOSE(x)");
2055 code[11] = c - NFA_ZCLOSE + '0';
2056 break;
2057#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2059 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2060 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2061 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002062 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2063 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002064 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2065 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2066 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2067 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2068 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2069 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2070 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2071 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2072 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2073 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2074 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2075 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2076 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2077 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2078
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002079 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002080 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2081 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2082 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2084 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002085
2086 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2087 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2088 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2089 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2090 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2091 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2092 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2093
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002094 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2095 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2096 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2097 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2098 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2099 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2100 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2101 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2102 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2103 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2104 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2105 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2106 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2107 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2108 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2109 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2110
2111 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2112 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2113 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2114 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2115 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2116 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2117 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2118 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2119 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2120 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2121 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2122 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2123 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2124 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2125 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2126 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2127 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2128 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2129 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2130 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2131 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2132 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2133 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2134 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2135 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2136 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2137 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2138
2139 default:
2140 STRCPY(code, "CHAR(x)");
2141 code[5] = c;
2142 }
2143
2144 if (addnl == TRUE)
2145 STRCAT(code, " + NEWLINE ");
2146
2147}
2148
2149#ifdef ENABLE_LOG
2150static FILE *log_fd;
2151
2152/*
2153 * Print the postfix notation of the current regexp.
2154 */
2155 static void
2156nfa_postfix_dump(expr, retval)
2157 char_u *expr;
2158 int retval;
2159{
2160 int *p;
2161 FILE *f;
2162
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002163 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164 if (f != NULL)
2165 {
2166 fprintf(f, "\n-------------------------\n");
2167 if (retval == FAIL)
2168 fprintf(f, ">>> NFA engine failed ... \n");
2169 else if (retval == OK)
2170 fprintf(f, ">>> NFA engine succeeded !\n");
2171 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002172 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173 {
2174 nfa_set_code(*p);
2175 fprintf(f, "%s, ", code);
2176 }
2177 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002178 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179 fprintf(f, "%d ", *p);
2180 fprintf(f, "\n\n");
2181 fclose(f);
2182 }
2183}
2184
2185/*
2186 * Print the NFA starting with a root node "state".
2187 */
2188 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002189nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002190 FILE *debugf;
2191 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002192{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002193 garray_T indent;
2194
2195 ga_init2(&indent, 1, 64);
2196 ga_append(&indent, '\0');
2197 nfa_print_state2(debugf, state, &indent);
2198 ga_clear(&indent);
2199}
2200
2201 static void
2202nfa_print_state2(debugf, state, indent)
2203 FILE *debugf;
2204 nfa_state_T *state;
2205 garray_T *indent;
2206{
2207 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002208
2209 if (state == NULL)
2210 return;
2211
2212 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002213
2214 /* Output indent */
2215 p = (char_u *)indent->ga_data;
2216 if (indent->ga_len >= 3)
2217 {
2218 int last = indent->ga_len - 3;
2219 char_u save[2];
2220
2221 STRNCPY(save, &p[last], 2);
2222 STRNCPY(&p[last], "+-", 2);
2223 fprintf(debugf, " %s", p);
2224 STRNCPY(&p[last], save, 2);
2225 }
2226 else
2227 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228
2229 nfa_set_code(state->c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02002230 fprintf(debugf, "%s%s (%d) (id=%d) val=%d\n",
2231 state->negated ? "NOT " : "",
2232 code,
2233 state->c,
2234 abs(state->id),
2235 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236 if (state->id < 0)
2237 return;
2238
2239 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002240
2241 /* grow indent for state->out */
2242 indent->ga_len -= 1;
2243 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002244 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002245 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002246 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002247 ga_append(indent, '\0');
2248
2249 nfa_print_state2(debugf, state->out, indent);
2250
2251 /* replace last part of indent for state->out1 */
2252 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002253 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002254 ga_append(indent, '\0');
2255
2256 nfa_print_state2(debugf, state->out1, indent);
2257
2258 /* shrink indent */
2259 indent->ga_len -= 3;
2260 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261}
2262
2263/*
2264 * Print the NFA state machine.
2265 */
2266 static void
2267nfa_dump(prog)
2268 nfa_regprog_T *prog;
2269{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002270 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002271
2272 if (debugf != NULL)
2273 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002274 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002275
2276 fprintf(debugf, "reganch: %d\n", prog->reganch);
2277 fprintf(debugf, "regstart: %d\n", prog->regstart);
2278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279 fclose(debugf);
2280 }
2281}
2282#endif /* ENABLE_LOG */
2283#endif /* DEBUG */
2284
2285/*
2286 * Parse r.e. @expr and convert it into postfix form.
2287 * Return the postfix string on success, NULL otherwise.
2288 */
2289 static int *
2290re2post()
2291{
2292 if (nfa_reg(REG_NOPAREN) == FAIL)
2293 return NULL;
2294 EMIT(NFA_MOPEN);
2295 return post_start;
2296}
2297
2298/* NB. Some of the code below is inspired by Russ's. */
2299
2300/*
2301 * Represents an NFA state plus zero or one or two arrows exiting.
2302 * if c == MATCH, no arrows out; matching state.
2303 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2304 * If c < 256, labeled arrow with character c to out.
2305 */
2306
2307static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2308
2309/*
2310 * Allocate and initialize nfa_state_T.
2311 */
2312 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002313alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314 int c;
2315 nfa_state_T *out;
2316 nfa_state_T *out1;
2317{
2318 nfa_state_T *s;
2319
2320 if (istate >= nstate)
2321 return NULL;
2322
2323 s = &state_ptr[istate++];
2324
2325 s->c = c;
2326 s->out = out;
2327 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002328 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329
2330 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002331 s->lastlist[0] = 0;
2332 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002333 s->negated = FALSE;
2334
2335 return s;
2336}
2337
2338/*
2339 * A partially built NFA without the matching state filled in.
2340 * Frag_T.start points at the start state.
2341 * Frag_T.out is a list of places that need to be set to the
2342 * next state for this fragment.
2343 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002344
2345/* Since the out pointers in the list are always
2346 * uninitialized, we use the pointers themselves
2347 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002348typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002349union Ptrlist
2350{
2351 Ptrlist *next;
2352 nfa_state_T *s;
2353};
2354
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355struct Frag
2356{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002357 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 Ptrlist *out;
2359};
2360typedef struct Frag Frag_T;
2361
2362static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2363static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2364static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2365static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2366static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2367static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2368
2369/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002370 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 */
2372 static Frag_T
2373frag(start, out)
2374 nfa_state_T *start;
2375 Ptrlist *out;
2376{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002377 Frag_T n;
2378
2379 n.start = start;
2380 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381 return n;
2382}
2383
2384/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002385 * Create singleton list containing just outp.
2386 */
2387 static Ptrlist *
2388list1(outp)
2389 nfa_state_T **outp;
2390{
2391 Ptrlist *l;
2392
2393 l = (Ptrlist *)outp;
2394 l->next = NULL;
2395 return l;
2396}
2397
2398/*
2399 * Patch the list of states at out to point to start.
2400 */
2401 static void
2402patch(l, s)
2403 Ptrlist *l;
2404 nfa_state_T *s;
2405{
2406 Ptrlist *next;
2407
2408 for (; l; l = next)
2409 {
2410 next = l->next;
2411 l->s = s;
2412 }
2413}
2414
2415
2416/*
2417 * Join the two lists l1 and l2, returning the combination.
2418 */
2419 static Ptrlist *
2420append(l1, l2)
2421 Ptrlist *l1;
2422 Ptrlist *l2;
2423{
2424 Ptrlist *oldl1;
2425
2426 oldl1 = l1;
2427 while (l1->next)
2428 l1 = l1->next;
2429 l1->next = l2;
2430 return oldl1;
2431}
2432
2433/*
2434 * Stack used for transforming postfix form into NFA.
2435 */
2436static Frag_T empty;
2437
2438 static void
2439st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002440 int *postfix UNUSED;
2441 int *end UNUSED;
2442 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002444#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 FILE *df;
2446 int *p2;
2447
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002448 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002449 if (df)
2450 {
2451 fprintf(df, "Error popping the stack!\n");
2452#ifdef DEBUG
2453 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2454#endif
2455 fprintf(df, "Postfix form is: ");
2456#ifdef DEBUG
2457 for (p2 = postfix; p2 < end; p2++)
2458 {
2459 nfa_set_code(*p2);
2460 fprintf(df, "%s, ", code);
2461 }
2462 nfa_set_code(*p);
2463 fprintf(df, "\nCurrent position is: ");
2464 for (p2 = postfix; p2 <= p; p2 ++)
2465 {
2466 nfa_set_code(*p2);
2467 fprintf(df, "%s, ", code);
2468 }
2469#else
2470 for (p2 = postfix; p2 < end; p2++)
2471 {
2472 fprintf(df, "%d, ", *p2);
2473 }
2474 fprintf(df, "\nCurrent position is: ");
2475 for (p2 = postfix; p2 <= p; p2 ++)
2476 {
2477 fprintf(df, "%d, ", *p2);
2478 }
2479#endif
2480 fprintf(df, "\n--------------------------\n");
2481 fclose(df);
2482 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002483#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002484 EMSG(_("E874: (NFA) Could not pop the stack !"));
2485}
2486
2487/*
2488 * Push an item onto the stack.
2489 */
2490 static void
2491st_push(s, p, stack_end)
2492 Frag_T s;
2493 Frag_T **p;
2494 Frag_T *stack_end;
2495{
2496 Frag_T *stackp = *p;
2497
2498 if (stackp >= stack_end)
2499 return;
2500 *stackp = s;
2501 *p = *p + 1;
2502}
2503
2504/*
2505 * Pop an item from the stack.
2506 */
2507 static Frag_T
2508st_pop(p, stack)
2509 Frag_T **p;
2510 Frag_T *stack;
2511{
2512 Frag_T *stackp;
2513
2514 *p = *p - 1;
2515 stackp = *p;
2516 if (stackp < stack)
2517 return empty;
2518 return **p;
2519}
2520
2521/*
2522 * Convert a postfix form into its equivalent NFA.
2523 * Return the NFA start state on success, NULL otherwise.
2524 */
2525 static nfa_state_T *
2526post2nfa(postfix, end, nfa_calc_size)
2527 int *postfix;
2528 int *end;
2529 int nfa_calc_size;
2530{
2531 int *p;
2532 int mopen;
2533 int mclose;
2534 Frag_T *stack = NULL;
2535 Frag_T *stackp = NULL;
2536 Frag_T *stack_end = NULL;
2537 Frag_T e1;
2538 Frag_T e2;
2539 Frag_T e;
2540 nfa_state_T *s;
2541 nfa_state_T *s1;
2542 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002543 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544
2545 if (postfix == NULL)
2546 return NULL;
2547
Bram Moolenaar053bb602013-05-20 13:55:21 +02002548#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002549#define POP() st_pop(&stackp, stack); \
2550 if (stackp < stack) \
2551 { \
2552 st_error(postfix, end, p); \
2553 return NULL; \
2554 }
2555
2556 if (nfa_calc_size == FALSE)
2557 {
2558 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002559 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002561 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562 }
2563
2564 for (p = postfix; p < end; ++p)
2565 {
2566 switch (*p)
2567 {
2568 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002569 /* Concatenation.
2570 * Pay attention: this operator does not exist in the r.e. itself
2571 * (it is implicit, really). It is added when r.e. is translated
2572 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573 if (nfa_calc_size == TRUE)
2574 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002575 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002576 break;
2577 }
2578 e2 = POP();
2579 e1 = POP();
2580 patch(e1.out, e2.start);
2581 PUSH(frag(e1.start, e2.out));
2582 break;
2583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002584 case NFA_OR:
2585 /* Alternation */
2586 if (nfa_calc_size == TRUE)
2587 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002588 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002589 break;
2590 }
2591 e2 = POP();
2592 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002593 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002594 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002595 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002596 PUSH(frag(s, append(e1.out, e2.out)));
2597 break;
2598
2599 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002600 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002601 if (nfa_calc_size == TRUE)
2602 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002603 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002604 break;
2605 }
2606 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002607 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002609 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002610 patch(e.out, s);
2611 PUSH(frag(s, list1(&s->out1)));
2612 break;
2613
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002614 case NFA_STAR_NONGREEDY:
2615 /* Zero or more, prefer zero */
2616 if (nfa_calc_size == TRUE)
2617 {
2618 nstate++;
2619 break;
2620 }
2621 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002622 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002623 if (s == NULL)
2624 goto theend;
2625 patch(e.out, s);
2626 PUSH(frag(s, list1(&s->out)));
2627 break;
2628
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002629 case NFA_QUEST:
2630 /* one or zero atoms=> greedy match */
2631 if (nfa_calc_size == TRUE)
2632 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002633 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002634 break;
2635 }
2636 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002637 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002639 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002640 PUSH(frag(s, append(e.out, list1(&s->out1))));
2641 break;
2642
2643 case NFA_QUEST_NONGREEDY:
2644 /* zero or one atoms => non-greedy match */
2645 if (nfa_calc_size == TRUE)
2646 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002647 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 break;
2649 }
2650 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002651 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002652 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002653 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654 PUSH(frag(s, append(e.out, list1(&s->out))));
2655 break;
2656
Bram Moolenaar417bad22013-06-07 14:08:30 +02002657 case NFA_END_COLL:
2658 case NFA_END_NEG_COLL:
2659 /* On the stack is the sequence starting with NFA_START_COLL or
2660 * NFA_START_NEG_COLL and all possible characters. Patch it to
2661 * add the output to the start. */
2662 if (nfa_calc_size == TRUE)
2663 {
2664 nstate++;
2665 break;
2666 }
2667 e = POP();
2668 s = alloc_state(NFA_END_COLL, NULL, NULL);
2669 if (s == NULL)
2670 goto theend;
2671 patch(e.out, s);
2672 e.start->out1 = s;
2673 PUSH(frag(e.start, list1(&s->out)));
2674 break;
2675
2676 case NFA_RANGE:
2677 /* Before this are two characters, the low and high end of a
2678 * range. Turn them into two states with MIN and MAX. */
2679 if (nfa_calc_size == TRUE)
2680 {
2681 /* nstate += 0; */
2682 break;
2683 }
2684 e2 = POP();
2685 e1 = POP();
2686 e2.start->val = e2.start->c;
2687 e2.start->c = NFA_RANGE_MAX;
2688 e1.start->val = e1.start->c;
2689 e1.start->c = NFA_RANGE_MIN;
2690 patch(e1.out, e2.start);
2691 PUSH(frag(e1.start, e2.out));
2692 break;
2693
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002694 case NFA_SKIP_CHAR:
2695 /* Symbol of 0-length, Used in a repetition
2696 * with max/min count of 0 */
2697 if (nfa_calc_size == TRUE)
2698 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002699 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002700 break;
2701 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002702 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002703 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002704 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002705 PUSH(frag(s, list1(&s->out)));
2706 break;
2707
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002708 case NFA_OPT_CHARS:
2709 {
2710 int n;
2711
2712 /* \%[abc] */
2713 n = *++p; /* get number of characters */
2714 if (nfa_calc_size == TRUE)
2715 {
2716 nstate += n;
2717 break;
2718 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002719 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002720 e1.out = NULL; /* stores list with out1's */
2721 s1 = NULL; /* previous NFA_SPLIT to connect to */
2722 while (n-- > 0)
2723 {
2724 e = POP(); /* get character */
2725 s = alloc_state(NFA_SPLIT, e.start, NULL);
2726 if (s == NULL)
2727 goto theend;
2728 if (e1.out == NULL)
2729 e1 = e;
2730 patch(e.out, s1);
2731 append(e1.out, list1(&s->out1));
2732 s1 = s;
2733 }
2734 PUSH(frag(s, e1.out));
2735 break;
2736 }
2737
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002739 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002740 case NFA_PREV_ATOM_JUST_BEFORE:
2741 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002742 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002743 {
2744 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2745 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2746 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2747 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002748 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
2749 int start_state = NFA_START_INVISIBLE;
2750 int end_state = NFA_END_INVISIBLE;
2751 int n = 0;
2752 nfa_state_T *zend;
2753 nfa_state_T *skip;
2754
2755 if (before)
2756 start_state = NFA_START_INVISIBLE_BEFORE;
2757 else if (pattern)
2758 {
2759 start_state = NFA_START_PATTERN;
2760 end_state = NFA_END_PATTERN;
2761 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002762
2763 if (before)
2764 n = *++p; /* get the count */
2765
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002766 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002767 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002768 * The \@<= operator: match for the preceding atom.
2769 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002770 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002771 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002772
2773 if (nfa_calc_size == TRUE)
2774 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002775 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776 break;
2777 }
2778 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002779 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002780 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002781 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002782
Bram Moolenaar87953742013-06-05 18:52:40 +02002783 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002784 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002785 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002786 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002787 {
2788 s->negated = TRUE;
2789 s1->negated = TRUE;
2790 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002791 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002792 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002793 if (pattern)
2794 {
2795 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2796 skip = alloc_state(NFA_SKIP, NULL, NULL);
2797 zend = alloc_state(NFA_ZEND, s1, NULL);
2798 s1->out= skip;
2799 patch(e.out, zend);
2800 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002801 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002802 else
2803 {
2804 patch(e.out, s1);
2805 PUSH(frag(s, list1(&s1->out)));
2806 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002807 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002808 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002809
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002810#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002811 case NFA_COMPOSING: /* char with composing char */
2812#if 0
2813 /* TODO */
2814 if (regflags & RF_ICOMBINE)
2815 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002816 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002817 }
2818#endif
2819 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002820#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002821
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002822 case NFA_MOPEN: /* \( \) Submatch */
2823 case NFA_MOPEN1:
2824 case NFA_MOPEN2:
2825 case NFA_MOPEN3:
2826 case NFA_MOPEN4:
2827 case NFA_MOPEN5:
2828 case NFA_MOPEN6:
2829 case NFA_MOPEN7:
2830 case NFA_MOPEN8:
2831 case NFA_MOPEN9:
2832#ifdef FEAT_SYN_HL
2833 case NFA_ZOPEN: /* \z( \) Submatch */
2834 case NFA_ZOPEN1:
2835 case NFA_ZOPEN2:
2836 case NFA_ZOPEN3:
2837 case NFA_ZOPEN4:
2838 case NFA_ZOPEN5:
2839 case NFA_ZOPEN6:
2840 case NFA_ZOPEN7:
2841 case NFA_ZOPEN8:
2842 case NFA_ZOPEN9:
2843#endif
2844 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845 if (nfa_calc_size == TRUE)
2846 {
2847 nstate += 2;
2848 break;
2849 }
2850
2851 mopen = *p;
2852 switch (*p)
2853 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002854 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2855#ifdef FEAT_SYN_HL
2856 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2857 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2858 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2859 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2860 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2861 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2862 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2863 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2864 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2865 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2866#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002867#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002868 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002869#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002870 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002871 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002872 mclose = *p + NSUBEXP;
2873 break;
2874 }
2875
2876 /* Allow "NFA_MOPEN" as a valid postfix representation for
2877 * the empty regexp "". In this case, the NFA will be
2878 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2879 * empty groups of parenthesis, and empty mbyte chars */
2880 if (stackp == stack)
2881 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002882 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002883 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002884 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002885 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002887 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888 patch(list1(&s->out), s1);
2889 PUSH(frag(s, list1(&s1->out)));
2890 break;
2891 }
2892
2893 /* At least one node was emitted before NFA_MOPEN, so
2894 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2895 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002896 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002898 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002899
Bram Moolenaar525666f2013-06-02 16:40:55 +02002900 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002902 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002903 patch(e.out, s1);
2904
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002905#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002906 if (mopen == NFA_COMPOSING)
2907 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002908 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002909#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002910
2911 PUSH(frag(s, list1(&s1->out)));
2912 break;
2913
Bram Moolenaar5714b802013-05-28 22:03:20 +02002914 case NFA_BACKREF1:
2915 case NFA_BACKREF2:
2916 case NFA_BACKREF3:
2917 case NFA_BACKREF4:
2918 case NFA_BACKREF5:
2919 case NFA_BACKREF6:
2920 case NFA_BACKREF7:
2921 case NFA_BACKREF8:
2922 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002923#ifdef FEAT_SYN_HL
2924 case NFA_ZREF1:
2925 case NFA_ZREF2:
2926 case NFA_ZREF3:
2927 case NFA_ZREF4:
2928 case NFA_ZREF5:
2929 case NFA_ZREF6:
2930 case NFA_ZREF7:
2931 case NFA_ZREF8:
2932 case NFA_ZREF9:
2933#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002934 if (nfa_calc_size == TRUE)
2935 {
2936 nstate += 2;
2937 break;
2938 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002939 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002940 if (s == NULL)
2941 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002942 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002943 if (s1 == NULL)
2944 goto theend;
2945 patch(list1(&s->out), s1);
2946 PUSH(frag(s, list1(&s1->out)));
2947 break;
2948
Bram Moolenaar423532e2013-05-29 21:14:42 +02002949 case NFA_LNUM:
2950 case NFA_LNUM_GT:
2951 case NFA_LNUM_LT:
2952 case NFA_VCOL:
2953 case NFA_VCOL_GT:
2954 case NFA_VCOL_LT:
2955 case NFA_COL:
2956 case NFA_COL_GT:
2957 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002958 case NFA_MARK:
2959 case NFA_MARK_GT:
2960 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002961 {
2962 int n = *++p; /* lnum, col or mark name */
2963
Bram Moolenaar423532e2013-05-29 21:14:42 +02002964 if (nfa_calc_size == TRUE)
2965 {
2966 nstate += 1;
2967 break;
2968 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002969 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002970 if (s == NULL)
2971 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002972 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002973 PUSH(frag(s, list1(&s->out)));
2974 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002975 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002976
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002977 case NFA_ZSTART:
2978 case NFA_ZEND:
2979 default:
2980 /* Operands */
2981 if (nfa_calc_size == TRUE)
2982 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002983 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002984 break;
2985 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002986 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002987 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002988 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002989 PUSH(frag(s, list1(&s->out)));
2990 break;
2991
2992 } /* switch(*p) */
2993
2994 } /* for(p = postfix; *p; ++p) */
2995
2996 if (nfa_calc_size == TRUE)
2997 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002998 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002999 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000 }
3001
3002 e = POP();
3003 if (stackp != stack)
3004 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3005
3006 if (istate >= nstate)
3007 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3008
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003009 matchstate = &state_ptr[istate++]; /* the match state */
3010 matchstate->c = NFA_MATCH;
3011 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003012 matchstate->negated = FALSE;
3013 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014
3015 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003016 ret = e.start;
3017
3018theend:
3019 vim_free(stack);
3020 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003021
3022#undef POP1
3023#undef PUSH1
3024#undef POP2
3025#undef PUSH2
3026#undef POP
3027#undef PUSH
3028}
3029
3030/****************************************************************
3031 * NFA execution code.
3032 ****************************************************************/
3033
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003034typedef struct
3035{
3036 int in_use; /* number of subexpr with useful info */
3037
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003038 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003039 union
3040 {
3041 struct multipos
3042 {
3043 lpos_T start;
3044 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003045 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003046 struct linepos
3047 {
3048 char_u *start;
3049 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003050 } line[NSUBEXP];
3051 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003052} regsub_T;
3053
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003054typedef struct
3055{
3056 regsub_T norm; /* \( .. \) matches */
3057#ifdef FEAT_SYN_HL
3058 regsub_T synt; /* \z( .. \) matches */
3059#endif
3060} regsubs_T;
3061
Bram Moolenaara2d95102013-06-04 14:23:05 +02003062/* nfa_pim_T stores a Postponed Invisible Match. */
3063typedef struct nfa_pim_S nfa_pim_T;
3064struct nfa_pim_S
3065{
3066 nfa_state_T *state;
3067 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
3068 nfa_pim_T *pim; /* another PIM at the same position */
3069 regsubs_T subs; /* submatch info, only party used */
3070};
3071
3072/* Values for done in nfa_pim_T. */
3073#define NFA_PIM_TODO 0
3074#define NFA_PIM_MATCH 1
3075#define NFA_PIM_NOMATCH -1
3076
3077
Bram Moolenaar963fee22013-05-26 21:47:28 +02003078/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003079typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003080{
3081 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003082 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003083 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003084 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003085} nfa_thread_T;
3086
Bram Moolenaar963fee22013-05-26 21:47:28 +02003087/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003088typedef struct
3089{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003090 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003091 int n; /* nr of states currently in "t" */
3092 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003093 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003094} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003095
Bram Moolenaar5714b802013-05-28 22:03:20 +02003096#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003097static void log_subsexpr __ARGS((regsubs_T *subs));
3098static void log_subexpr __ARGS((regsub_T *sub));
3099
3100 static void
3101log_subsexpr(subs)
3102 regsubs_T *subs;
3103{
3104 log_subexpr(&subs->norm);
3105# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003106 if (nfa_has_zsubexpr)
3107 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003108# endif
3109}
3110
Bram Moolenaar5714b802013-05-28 22:03:20 +02003111 static void
3112log_subexpr(sub)
3113 regsub_T *sub;
3114{
3115 int j;
3116
3117 for (j = 0; j < sub->in_use; j++)
3118 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003119 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003120 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003121 sub->list.multi[j].start.col,
3122 (int)sub->list.multi[j].start.lnum,
3123 sub->list.multi[j].end.col,
3124 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003125 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003126 {
3127 char *s = (char *)sub->list.line[j].start;
3128 char *e = (char *)sub->list.line[j].end;
3129
Bram Moolenaar87953742013-06-05 18:52:40 +02003130 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003131 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003132 s == NULL ? "NULL" : s,
3133 e == NULL ? "NULL" : e);
3134 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003135}
3136#endif
3137
Bram Moolenaar963fee22013-05-26 21:47:28 +02003138/* Used during execution: whether a match has been found. */
3139static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003140
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003141static void clear_sub __ARGS((regsub_T *sub));
3142static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3143static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003144static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003145static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003146static 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 +02003147
3148 static void
3149clear_sub(sub)
3150 regsub_T *sub;
3151{
3152 if (REG_MULTI)
3153 /* Use 0xff to set lnum to -1 */
3154 vim_memset(sub->list.multi, 0xff,
3155 sizeof(struct multipos) * nfa_nsubexpr);
3156 else
3157 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3158 sub->in_use = 0;
3159}
3160
3161/*
3162 * Copy the submatches from "from" to "to".
3163 */
3164 static void
3165copy_sub(to, from)
3166 regsub_T *to;
3167 regsub_T *from;
3168{
3169 to->in_use = from->in_use;
3170 if (from->in_use > 0)
3171 {
3172 /* Copy the match start and end positions. */
3173 if (REG_MULTI)
3174 mch_memmove(&to->list.multi[0],
3175 &from->list.multi[0],
3176 sizeof(struct multipos) * from->in_use);
3177 else
3178 mch_memmove(&to->list.line[0],
3179 &from->list.line[0],
3180 sizeof(struct linepos) * from->in_use);
3181 }
3182}
3183
3184/*
3185 * Like copy_sub() but exclude the main match.
3186 */
3187 static void
3188copy_sub_off(to, from)
3189 regsub_T *to;
3190 regsub_T *from;
3191{
3192 if (to->in_use < from->in_use)
3193 to->in_use = from->in_use;
3194 if (from->in_use > 1)
3195 {
3196 /* Copy the match start and end positions. */
3197 if (REG_MULTI)
3198 mch_memmove(&to->list.multi[1],
3199 &from->list.multi[1],
3200 sizeof(struct multipos) * (from->in_use - 1));
3201 else
3202 mch_memmove(&to->list.line[1],
3203 &from->list.line[1],
3204 sizeof(struct linepos) * (from->in_use - 1));
3205 }
3206}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207
Bram Moolenaar428e9872013-05-30 17:05:39 +02003208/*
3209 * Return TRUE if "sub1" and "sub2" have the same positions.
3210 */
3211 static int
3212sub_equal(sub1, sub2)
3213 regsub_T *sub1;
3214 regsub_T *sub2;
3215{
3216 int i;
3217 int todo;
3218 linenr_T s1, e1;
3219 linenr_T s2, e2;
3220 char_u *sp1, *ep1;
3221 char_u *sp2, *ep2;
3222
3223 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3224 if (REG_MULTI)
3225 {
3226 for (i = 0; i < todo; ++i)
3227 {
3228 if (i < sub1->in_use)
3229 {
3230 s1 = sub1->list.multi[i].start.lnum;
3231 e1 = sub1->list.multi[i].end.lnum;
3232 }
3233 else
3234 {
3235 s1 = 0;
3236 e1 = 0;
3237 }
3238 if (i < sub2->in_use)
3239 {
3240 s2 = sub2->list.multi[i].start.lnum;
3241 e2 = sub2->list.multi[i].end.lnum;
3242 }
3243 else
3244 {
3245 s2 = 0;
3246 e2 = 0;
3247 }
3248 if (s1 != s2 || e1 != e2)
3249 return FALSE;
3250 if (s1 != 0 && sub1->list.multi[i].start.col
3251 != sub2->list.multi[i].start.col)
3252 return FALSE;
3253 if (e1 != 0 && sub1->list.multi[i].end.col
3254 != sub2->list.multi[i].end.col)
3255 return FALSE;
3256 }
3257 }
3258 else
3259 {
3260 for (i = 0; i < todo; ++i)
3261 {
3262 if (i < sub1->in_use)
3263 {
3264 sp1 = sub1->list.line[i].start;
3265 ep1 = sub1->list.line[i].end;
3266 }
3267 else
3268 {
3269 sp1 = NULL;
3270 ep1 = NULL;
3271 }
3272 if (i < sub2->in_use)
3273 {
3274 sp2 = sub2->list.line[i].start;
3275 ep2 = sub2->list.line[i].end;
3276 }
3277 else
3278 {
3279 sp2 = NULL;
3280 ep2 = NULL;
3281 }
3282 if (sp1 != sp2 || ep1 != ep2)
3283 return FALSE;
3284 }
3285 }
3286
3287 return TRUE;
3288}
3289
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003290#ifdef ENABLE_LOG
3291 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003292report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003293{
3294 int col;
3295
3296 if (sub->in_use <= 0)
3297 col = -1;
3298 else if (REG_MULTI)
3299 col = sub->list.multi[0].start.col;
3300 else
3301 col = (int)(sub->list.line[0].start - regline);
3302 nfa_set_code(state->c);
3303 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3304 action, abs(state->id), lid, state->c, code, col);
3305}
3306#endif
3307
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003309addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003310 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003312 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003313 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003315 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003316 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003317 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003318 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003319 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003320 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003321 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003322#ifdef ENABLE_LOG
3323 int did_print = FALSE;
3324#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003325
3326 if (l == NULL || state == NULL)
3327 return;
3328
3329 switch (state->c)
3330 {
3331 case NFA_SPLIT:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003333 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 case NFA_NCLOSE:
3335 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003336 case NFA_MCLOSE1:
3337 case NFA_MCLOSE2:
3338 case NFA_MCLOSE3:
3339 case NFA_MCLOSE4:
3340 case NFA_MCLOSE5:
3341 case NFA_MCLOSE6:
3342 case NFA_MCLOSE7:
3343 case NFA_MCLOSE8:
3344 case NFA_MCLOSE9:
3345#ifdef FEAT_SYN_HL
3346 case NFA_ZCLOSE:
3347 case NFA_ZCLOSE1:
3348 case NFA_ZCLOSE2:
3349 case NFA_ZCLOSE3:
3350 case NFA_ZCLOSE4:
3351 case NFA_ZCLOSE5:
3352 case NFA_ZCLOSE6:
3353 case NFA_ZCLOSE7:
3354 case NFA_ZCLOSE8:
3355 case NFA_ZCLOSE9:
3356#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003357 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003358 /* These nodes are not added themselves but their "out" and/or
3359 * "out1" may be added below. */
3360 break;
3361
3362 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003363 case NFA_MOPEN1:
3364 case NFA_MOPEN2:
3365 case NFA_MOPEN3:
3366 case NFA_MOPEN4:
3367 case NFA_MOPEN5:
3368 case NFA_MOPEN6:
3369 case NFA_MOPEN7:
3370 case NFA_MOPEN8:
3371 case NFA_MOPEN9:
3372#ifdef FEAT_SYN_HL
3373 case NFA_ZOPEN:
3374 case NFA_ZOPEN1:
3375 case NFA_ZOPEN2:
3376 case NFA_ZOPEN3:
3377 case NFA_ZOPEN4:
3378 case NFA_ZOPEN5:
3379 case NFA_ZOPEN6:
3380 case NFA_ZOPEN7:
3381 case NFA_ZOPEN8:
3382 case NFA_ZOPEN9:
3383#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003384 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003385 /* These nodes do not need to be added, but we need to bail out
3386 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003387 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003388 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003389 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003390 break;
3391
Bram Moolenaar307aa162013-06-02 16:34:21 +02003392 case NFA_BOL:
3393 case NFA_BOF:
3394 /* "^" won't match past end-of-line, don't bother trying.
3395 * Except when we are going to the next line for a look-behind
3396 * match. */
3397 if (reginput > regline
3398 && (nfa_endp == NULL
3399 || !REG_MULTI
3400 || reglnum == nfa_endp->se_u.pos.lnum))
3401 goto skip_add;
3402 /* FALLTHROUGH */
3403
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003405 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003406 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003407 /* This state is already in the list, don't add it again,
3408 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003409 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003410 {
3411skip_add:
3412#ifdef ENABLE_LOG
3413 nfa_set_code(state->c);
3414 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3415 abs(state->id), l->id, state->c, code);
3416#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003417 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003418 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003419
3420 /* See if the same state is already in the list with the same
3421 * positions. */
3422 for (i = 0; i < l->n; ++i)
3423 {
3424 thread = &l->t[i];
3425 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003426 && sub_equal(&thread->subs.norm, &subs->norm)
3427#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003428 && (!nfa_has_zsubexpr ||
3429 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003430#endif
3431 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003432 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003433 }
3434 }
3435
Bram Moolenaara2d95102013-06-04 14:23:05 +02003436 /* when there are backreferences or look-behind matches the number
3437 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003438 if (nfa_has_backref && l->n == l->len)
3439 {
3440 int newlen = l->len * 3 / 2 + 50;
3441
3442 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3443 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003445
3446 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003447 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003448 thread = &l->t[l->n++];
3449 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003450 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003451 copy_sub(&thread->subs.norm, &subs->norm);
3452#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003453 if (nfa_has_zsubexpr)
3454 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003455#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003456#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003457 report_state("Adding", &thread->subs.norm, state, l->id);
3458 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003459#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460 }
3461
3462#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003463 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003464 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465#endif
3466 switch (state->c)
3467 {
3468 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003469 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003470 break;
3471
3472 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003473 /* order matters here */
3474 addstate(l, state->out, subs, off);
3475 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003476 break;
3477
Bram Moolenaar5714b802013-05-28 22:03:20 +02003478 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 case NFA_NOPEN:
3480 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003481 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 break;
3483
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003484 case NFA_MOPEN:
3485 case NFA_MOPEN1:
3486 case NFA_MOPEN2:
3487 case NFA_MOPEN3:
3488 case NFA_MOPEN4:
3489 case NFA_MOPEN5:
3490 case NFA_MOPEN6:
3491 case NFA_MOPEN7:
3492 case NFA_MOPEN8:
3493 case NFA_MOPEN9:
3494#ifdef FEAT_SYN_HL
3495 case NFA_ZOPEN:
3496 case NFA_ZOPEN1:
3497 case NFA_ZOPEN2:
3498 case NFA_ZOPEN3:
3499 case NFA_ZOPEN4:
3500 case NFA_ZOPEN5:
3501 case NFA_ZOPEN6:
3502 case NFA_ZOPEN7:
3503 case NFA_ZOPEN8:
3504 case NFA_ZOPEN9:
3505#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003506 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003507 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003508 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003510 sub = &subs->norm;
3511 }
3512#ifdef FEAT_SYN_HL
3513 else if (state->c >= NFA_ZOPEN)
3514 {
3515 subidx = state->c - NFA_ZOPEN;
3516 sub = &subs->synt;
3517 }
3518#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003519 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003520 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003521 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003522 sub = &subs->norm;
3523 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003525 /* Set the position (with "off") in the subexpression. Save and
3526 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003527 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003528 if (REG_MULTI)
3529 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003530 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003531 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003532 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003533 save_in_use = -1;
3534 }
3535 else
3536 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003537 save_in_use = sub->in_use;
3538 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003539 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003540 sub->list.multi[i].start.lnum = -1;
3541 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003542 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003543 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003544 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003545 if (off == -1)
3546 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003547 sub->list.multi[subidx].start.lnum = reglnum + 1;
3548 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003549 }
3550 else
3551 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003552 sub->list.multi[subidx].start.lnum = reglnum;
3553 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003554 (colnr_T)(reginput - regline + off);
3555 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003556 }
3557 else
3558 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003559 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003560 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003561 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003562 save_in_use = -1;
3563 }
3564 else
3565 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003566 save_in_use = sub->in_use;
3567 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003568 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003569 sub->list.line[i].start = NULL;
3570 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003571 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003572 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003573 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003574 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003575 }
3576
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003577 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003579 if (save_in_use == -1)
3580 {
3581 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003582 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003583 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003584 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003585 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003587 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 break;
3589
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003590 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003591 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003593 /* Do not overwrite the position set by \ze. If no \ze
3594 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003595 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596 break;
3597 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003598 case NFA_MCLOSE1:
3599 case NFA_MCLOSE2:
3600 case NFA_MCLOSE3:
3601 case NFA_MCLOSE4:
3602 case NFA_MCLOSE5:
3603 case NFA_MCLOSE6:
3604 case NFA_MCLOSE7:
3605 case NFA_MCLOSE8:
3606 case NFA_MCLOSE9:
3607#ifdef FEAT_SYN_HL
3608 case NFA_ZCLOSE:
3609 case NFA_ZCLOSE1:
3610 case NFA_ZCLOSE2:
3611 case NFA_ZCLOSE3:
3612 case NFA_ZCLOSE4:
3613 case NFA_ZCLOSE5:
3614 case NFA_ZCLOSE6:
3615 case NFA_ZCLOSE7:
3616 case NFA_ZCLOSE8:
3617 case NFA_ZCLOSE9:
3618#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003620 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003621 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003622 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003623 sub = &subs->norm;
3624 }
3625#ifdef FEAT_SYN_HL
3626 else if (state->c >= NFA_ZCLOSE)
3627 {
3628 subidx = state->c - NFA_ZCLOSE;
3629 sub = &subs->synt;
3630 }
3631#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003632 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003633 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003634 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003635 sub = &subs->norm;
3636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003638 /* We don't fill in gaps here, there must have been an MOPEN that
3639 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003640 save_in_use = sub->in_use;
3641 if (sub->in_use <= subidx)
3642 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643 if (REG_MULTI)
3644 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003645 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003646 if (off == -1)
3647 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003648 sub->list.multi[subidx].end.lnum = reglnum + 1;
3649 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003650 }
3651 else
3652 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003653 sub->list.multi[subidx].end.lnum = reglnum;
3654 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003655 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003656 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003657 }
3658 else
3659 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003660 save_ptr = sub->list.line[subidx].end;
3661 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003662 }
3663
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003664 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003665
3666 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003667 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003668 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003669 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003670 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 break;
3672 }
3673}
3674
3675/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003676 * Like addstate(), but the new state(s) are put at position "*ip".
3677 * Used for zero-width matches, next state to use is the added one.
3678 * This makes sure the order of states to be tried does not change, which
3679 * matters for alternatives.
3680 */
3681 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003682addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003683 nfa_list_T *l; /* runtime state list */
3684 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003685 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003686 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003687 int *ip;
3688{
3689 int tlen = l->n;
3690 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003691 int listidx = *ip;
3692 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003693
3694 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003695 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003696
Bram Moolenaara2d95102013-06-04 14:23:05 +02003697 /* fill in the "pim" field in the new states */
3698 if (pim != NULL)
3699 for (i = tlen; i < l->n; ++i)
3700 l->t[i].pim = pim;
3701
Bram Moolenaar4b417062013-05-25 20:19:50 +02003702 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003703 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003704 return;
3705
3706 /* re-order to put the new state at the current position */
3707 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003708 if (count == 1)
3709 {
3710 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003711 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003712 }
3713 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003714 {
3715 /* make space for new states, then move them from the
3716 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003717 mch_memmove(&(l->t[listidx + count]),
3718 &(l->t[listidx + 1]),
3719 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3720 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003721 &(l->t[l->n - 1]),
3722 sizeof(nfa_thread_T) * count);
3723 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003724 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003725 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003726}
3727
3728/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 * Check character class "class" against current character c.
3730 */
3731 static int
3732check_char_class(class, c)
3733 int class;
3734 int c;
3735{
3736 switch (class)
3737 {
3738 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003739 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003740 return OK;
3741 break;
3742 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003743 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003744 return OK;
3745 break;
3746 case NFA_CLASS_BLANK:
3747 if (c == ' ' || c == '\t')
3748 return OK;
3749 break;
3750 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003751 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752 return OK;
3753 break;
3754 case NFA_CLASS_DIGIT:
3755 if (VIM_ISDIGIT(c))
3756 return OK;
3757 break;
3758 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003759 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760 return OK;
3761 break;
3762 case NFA_CLASS_LOWER:
3763 if (MB_ISLOWER(c))
3764 return OK;
3765 break;
3766 case NFA_CLASS_PRINT:
3767 if (vim_isprintc(c))
3768 return OK;
3769 break;
3770 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003771 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003772 return OK;
3773 break;
3774 case NFA_CLASS_SPACE:
3775 if ((c >=9 && c <= 13) || (c == ' '))
3776 return OK;
3777 break;
3778 case NFA_CLASS_UPPER:
3779 if (MB_ISUPPER(c))
3780 return OK;
3781 break;
3782 case NFA_CLASS_XDIGIT:
3783 if (vim_isxdigit(c))
3784 return OK;
3785 break;
3786 case NFA_CLASS_TAB:
3787 if (c == '\t')
3788 return OK;
3789 break;
3790 case NFA_CLASS_RETURN:
3791 if (c == '\r')
3792 return OK;
3793 break;
3794 case NFA_CLASS_BACKSPACE:
3795 if (c == '\b')
3796 return OK;
3797 break;
3798 case NFA_CLASS_ESCAPE:
3799 if (c == '\033')
3800 return OK;
3801 break;
3802
3803 default:
3804 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02003805 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
3806 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807 }
3808 return FAIL;
3809}
3810
Bram Moolenaar5714b802013-05-28 22:03:20 +02003811static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3812
3813/*
3814 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003815 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003816 */
3817 static int
3818match_backref(sub, subidx, bytelen)
3819 regsub_T *sub; /* pointers to subexpressions */
3820 int subidx;
3821 int *bytelen; /* out: length of match in bytes */
3822{
3823 int len;
3824
3825 if (sub->in_use <= subidx)
3826 {
3827retempty:
3828 /* backref was not set, match an empty string */
3829 *bytelen = 0;
3830 return TRUE;
3831 }
3832
3833 if (REG_MULTI)
3834 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003835 if (sub->list.multi[subidx].start.lnum < 0
3836 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003837 goto retempty;
3838 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003839 len = sub->list.multi[subidx].end.col
3840 - sub->list.multi[subidx].start.col;
3841 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003842 reginput, &len) == 0)
3843 {
3844 *bytelen = len;
3845 return TRUE;
3846 }
3847 }
3848 else
3849 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003850 if (sub->list.line[subidx].start == NULL
3851 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003852 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003853 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3854 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003855 {
3856 *bytelen = len;
3857 return TRUE;
3858 }
3859 }
3860 return FALSE;
3861}
3862
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003863#ifdef FEAT_SYN_HL
3864
3865static int match_zref __ARGS((int subidx, int *bytelen));
3866
3867/*
3868 * Check for a match with \z subexpression "subidx".
3869 * Return TRUE if it matches.
3870 */
3871 static int
3872match_zref(subidx, bytelen)
3873 int subidx;
3874 int *bytelen; /* out: length of match in bytes */
3875{
3876 int len;
3877
3878 cleanup_zsubexpr();
3879 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3880 {
3881 /* backref was not set, match an empty string */
3882 *bytelen = 0;
3883 return TRUE;
3884 }
3885
3886 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3887 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3888 {
3889 *bytelen = len;
3890 return TRUE;
3891 }
3892 return FALSE;
3893}
3894#endif
3895
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003896/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003897 * Save list IDs for all NFA states of "prog" into "list".
3898 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003899 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900 */
3901 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003902nfa_save_listids(prog, list)
3903 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003904 int *list;
3905{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003906 int i;
3907 nfa_state_T *p;
3908
3909 /* Order in the list is reverse, it's a bit faster that way. */
3910 p = &prog->state[0];
3911 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003912 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003913 list[i] = p->lastlist[1];
3914 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003915 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003916 }
3917}
3918
3919/*
3920 * Restore list IDs from "list" to all NFA states.
3921 */
3922 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003923nfa_restore_listids(prog, list)
3924 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003925 int *list;
3926{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003927 int i;
3928 nfa_state_T *p;
3929
3930 p = &prog->state[0];
3931 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003932 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003933 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003934 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003935 }
3936}
3937
Bram Moolenaar423532e2013-05-29 21:14:42 +02003938 static int
3939nfa_re_num_cmp(val, op, pos)
3940 long_u val;
3941 int op;
3942 long_u pos;
3943{
3944 if (op == 1) return pos > val;
3945 if (op == 2) return pos < val;
3946 return val == pos;
3947}
3948
Bram Moolenaarf46da702013-06-02 22:37:42 +02003949static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003950static 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 +02003951
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003952/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003953 * Recursively call nfa_regmatch()
3954 */
3955 static int
3956recursive_regmatch(state, prog, submatch, m, listids)
3957 nfa_state_T *state;
3958 nfa_regprog_T *prog;
3959 regsubs_T *submatch;
3960 regsubs_T *m;
3961 int **listids;
3962{
3963 char_u *save_reginput = reginput;
3964 char_u *save_regline = regline;
3965 int save_reglnum = reglnum;
3966 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003967 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003968 save_se_T *save_nfa_endp = nfa_endp;
3969 save_se_T endpos;
3970 save_se_T *endposp = NULL;
3971 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003972 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003973
3974 if (state->c == NFA_START_INVISIBLE_BEFORE)
3975 {
3976 /* The recursive match must end at the current position. */
3977 endposp = &endpos;
3978 if (REG_MULTI)
3979 {
3980 endpos.se_u.pos.col = (int)(reginput - regline);
3981 endpos.se_u.pos.lnum = reglnum;
3982 }
3983 else
3984 endpos.se_u.ptr = reginput;
3985
3986 /* Go back the specified number of bytes, or as far as the
3987 * start of the previous line, to try matching "\@<=" or
3988 * not matching "\@<!".
3989 * TODO: This is very inefficient! Would be better to
3990 * first check for a match with what follows. */
3991 if (state->val <= 0)
3992 {
3993 if (REG_MULTI)
3994 {
3995 regline = reg_getline(--reglnum);
3996 if (regline == NULL)
3997 /* can't go before the first line */
3998 regline = reg_getline(++reglnum);
3999 }
4000 reginput = regline;
4001 }
4002 else
4003 {
4004 if (REG_MULTI && (int)(reginput - regline) < state->val)
4005 {
4006 /* Not enough bytes in this line, go to end of
4007 * previous line. */
4008 regline = reg_getline(--reglnum);
4009 if (regline == NULL)
4010 {
4011 /* can't go before the first line */
4012 regline = reg_getline(++reglnum);
4013 reginput = regline;
4014 }
4015 else
4016 reginput = regline + STRLEN(regline);
4017 }
4018 if ((int)(reginput - regline) >= state->val)
4019 {
4020 reginput -= state->val;
4021#ifdef FEAT_MBYTE
4022 if (has_mbyte)
4023 reginput -= mb_head_off(regline, reginput);
4024#endif
4025 }
4026 else
4027 reginput = regline;
4028 }
4029 }
4030
Bram Moolenaarf46da702013-06-02 22:37:42 +02004031#ifdef ENABLE_LOG
4032 if (log_fd != stderr)
4033 fclose(log_fd);
4034 log_fd = NULL;
4035#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004036 /* Have to clear the lastlist field of the NFA nodes, so that
4037 * nfa_regmatch() and addstate() can run properly after recursion. */
4038 if (nfa_ll_index == 1)
4039 {
4040 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4041 * values and clear them. */
4042 if (*listids == NULL)
4043 {
4044 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4045 if (*listids == NULL)
4046 {
4047 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4048 return 0;
4049 }
4050 }
4051 nfa_save_listids(prog, *listids);
4052 need_restore = TRUE;
4053 /* any value of nfa_listid will do */
4054 }
4055 else
4056 {
4057 /* First recursive nfa_regmatch() call, switch to the second lastlist
4058 * entry. Make sure nfa_listid is different from a previous recursive
4059 * call, because some states may still have this ID. */
4060 ++nfa_ll_index;
4061 if (nfa_listid <= nfa_alt_listid)
4062 nfa_listid = nfa_alt_listid;
4063 }
4064
4065 /* Call nfa_regmatch() to check if the current concat matches at this
4066 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004067 nfa_endp = endposp;
4068 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004069
4070 if (need_restore)
4071 nfa_restore_listids(prog, *listids);
4072 else
4073 {
4074 --nfa_ll_index;
4075 nfa_alt_listid = nfa_listid;
4076 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004077
4078 /* restore position in input text */
4079 reginput = save_reginput;
4080 regline = save_regline;
4081 reglnum = save_reglnum;
4082 nfa_match = save_nfa_match;
4083 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004084 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004085
4086#ifdef ENABLE_LOG
4087 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4088 if (log_fd != NULL)
4089 {
4090 fprintf(log_fd, "****************************\n");
4091 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4092 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4093 fprintf(log_fd, "****************************\n");
4094 }
4095 else
4096 {
4097 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4098 log_fd = stderr;
4099 }
4100#endif
4101
4102 return result;
4103}
4104
Bram Moolenaara2d95102013-06-04 14:23:05 +02004105static int failure_chance __ARGS((nfa_state_T *state, int depth));
4106
4107/*
4108 * Estimate the chance of a match with "state" failing.
4109 * NFA_ANY: 1
4110 * specific character: 99
4111 */
4112 static int
4113failure_chance(state, depth)
4114 nfa_state_T *state;
4115 int depth;
4116{
4117 int c = state->c;
4118 int l, r;
4119
4120 /* detect looping */
4121 if (depth > 4)
4122 return 1;
4123
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004124 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004125 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004126 case NFA_SPLIT:
4127 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4128 /* avoid recursive stuff */
4129 return 1;
4130 /* two alternatives, use the lowest failure chance */
4131 l = failure_chance(state->out, depth + 1);
4132 r = failure_chance(state->out1, depth + 1);
4133 return l < r ? l : r;
4134
4135 case NFA_ANY:
4136 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004137 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004138 case NFA_MATCH:
4139 /* empty match works always */
4140 return 0;
4141
4142 case NFA_BOL:
4143 case NFA_EOL:
4144 case NFA_BOF:
4145 case NFA_EOF:
4146 case NFA_NEWL:
4147 return 99;
4148
4149 case NFA_BOW:
4150 case NFA_EOW:
4151 return 90;
4152
4153 case NFA_MOPEN:
4154 case NFA_MOPEN1:
4155 case NFA_MOPEN2:
4156 case NFA_MOPEN3:
4157 case NFA_MOPEN4:
4158 case NFA_MOPEN5:
4159 case NFA_MOPEN6:
4160 case NFA_MOPEN7:
4161 case NFA_MOPEN8:
4162 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004163#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004164 case NFA_ZOPEN:
4165 case NFA_ZOPEN1:
4166 case NFA_ZOPEN2:
4167 case NFA_ZOPEN3:
4168 case NFA_ZOPEN4:
4169 case NFA_ZOPEN5:
4170 case NFA_ZOPEN6:
4171 case NFA_ZOPEN7:
4172 case NFA_ZOPEN8:
4173 case NFA_ZOPEN9:
4174 case NFA_ZCLOSE:
4175 case NFA_ZCLOSE1:
4176 case NFA_ZCLOSE2:
4177 case NFA_ZCLOSE3:
4178 case NFA_ZCLOSE4:
4179 case NFA_ZCLOSE5:
4180 case NFA_ZCLOSE6:
4181 case NFA_ZCLOSE7:
4182 case NFA_ZCLOSE8:
4183 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004184#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004185 case NFA_NOPEN:
4186 case NFA_MCLOSE:
4187 case NFA_MCLOSE1:
4188 case NFA_MCLOSE2:
4189 case NFA_MCLOSE3:
4190 case NFA_MCLOSE4:
4191 case NFA_MCLOSE5:
4192 case NFA_MCLOSE6:
4193 case NFA_MCLOSE7:
4194 case NFA_MCLOSE8:
4195 case NFA_MCLOSE9:
4196 case NFA_NCLOSE:
4197 return failure_chance(state->out, depth + 1);
4198
4199 case NFA_BACKREF1:
4200 case NFA_BACKREF2:
4201 case NFA_BACKREF3:
4202 case NFA_BACKREF4:
4203 case NFA_BACKREF5:
4204 case NFA_BACKREF6:
4205 case NFA_BACKREF7:
4206 case NFA_BACKREF8:
4207 case NFA_BACKREF9:
4208#ifdef FEAT_SYN_HL
4209 case NFA_ZREF1:
4210 case NFA_ZREF2:
4211 case NFA_ZREF3:
4212 case NFA_ZREF4:
4213 case NFA_ZREF5:
4214 case NFA_ZREF6:
4215 case NFA_ZREF7:
4216 case NFA_ZREF8:
4217 case NFA_ZREF9:
4218#endif
4219 /* backreferences don't match in many places */
4220 return 94;
4221
4222 case NFA_LNUM_GT:
4223 case NFA_LNUM_LT:
4224 case NFA_COL_GT:
4225 case NFA_COL_LT:
4226 case NFA_VCOL_GT:
4227 case NFA_VCOL_LT:
4228 case NFA_MARK_GT:
4229 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004230 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004231 /* before/after positions don't match very often */
4232 return 85;
4233
4234 case NFA_LNUM:
4235 return 90;
4236
4237 case NFA_CURSOR:
4238 case NFA_COL:
4239 case NFA_VCOL:
4240 case NFA_MARK:
4241 /* specific positions rarely match */
4242 return 98;
4243
4244 case NFA_COMPOSING:
4245 return 95;
4246
4247 default:
4248 if (c > 0)
4249 /* character match fails often */
4250 return 95;
4251 }
4252
4253 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004254 return 50;
4255}
4256
Bram Moolenaarf46da702013-06-02 22:37:42 +02004257/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004258 * Main matching routine.
4259 *
4260 * Run NFA to determine whether it matches reginput.
4261 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004262 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004263 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004264 * Return TRUE if there is a match, FALSE otherwise.
4265 * Note: Caller must ensure that: start != NULL.
4266 */
4267 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004268nfa_regmatch(prog, start, submatch, m)
4269 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004270 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004271 regsubs_T *submatch;
4272 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004273{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004274 int result;
4275 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004276 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004277 int go_to_nextline = FALSE;
4278 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004279 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004280 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004281 nfa_list_T *thislist;
4282 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004283 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004284 nfa_state_T *add_state;
4285 int add_count;
4286 int add_off;
4287 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004288#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004289 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004290
4291 if (debug == NULL)
4292 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004293 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294 return FALSE;
4295 }
4296#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004297 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004298 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004299
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004300 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004301 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004302 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004303 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004304 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004305 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004306 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004307 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308
4309#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004310 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004311 if (log_fd != NULL)
4312 {
4313 fprintf(log_fd, "**********************************\n");
4314 nfa_set_code(start->c);
4315 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4316 abs(start->id), code);
4317 fprintf(log_fd, "**********************************\n");
4318 }
4319 else
4320 {
4321 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4322 log_fd = stderr;
4323 }
4324#endif
4325
4326 thislist = &list[0];
4327 thislist->n = 0;
4328 nextlist = &list[1];
4329 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330#ifdef ENABLE_LOG
4331 fprintf(log_fd, "(---) STARTSTATE\n");
4332#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004333 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004334 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004335
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004336#define ADD_STATE_IF_MATCH(state) \
4337 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004338 add_state = state->out; \
4339 add_off = clen; \
4340 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004341
4342 /*
4343 * Run for each character.
4344 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004345 for (;;)
4346 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004347 int curc;
4348 int clen;
4349
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350#ifdef FEAT_MBYTE
4351 if (has_mbyte)
4352 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004353 curc = (*mb_ptr2char)(reginput);
4354 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004355 }
4356 else
4357#endif
4358 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004359 curc = *reginput;
4360 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004361 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004362 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004363 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004364 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004365 go_to_nextline = FALSE;
4366 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367
4368 /* swap lists */
4369 thislist = &list[flag];
4370 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004371 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004372 ++nfa_listid;
4373 thislist->id = nfa_listid;
4374 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004375
Bram Moolenaara2d95102013-06-04 14:23:05 +02004376 pimlist.ga_len = 0;
4377
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004378#ifdef ENABLE_LOG
4379 fprintf(log_fd, "------------------------------------------\n");
4380 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004381 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004382 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004383 {
4384 int i;
4385
4386 for (i = 0; i < thislist->n; i++)
4387 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4388 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004389 fprintf(log_fd, "\n");
4390#endif
4391
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004392#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 fprintf(debug, "\n-------------------\n");
4394#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004395 /*
4396 * If the state lists are empty we can stop.
4397 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004398 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004399 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004400
4401 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004402 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004403 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004404 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004405
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004406#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004407 nfa_set_code(t->state->c);
4408 fprintf(debug, "%s, ", code);
4409#endif
4410#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004411 {
4412 int col;
4413
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004414 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004415 col = -1;
4416 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004417 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004418 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004419 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004420 nfa_set_code(t->state->c);
4421 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4422 abs(t->state->id), (int)t->state->c, code, col);
4423 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004424#endif
4425
4426 /*
4427 * Handle the possible codes of the current state.
4428 * The most important is NFA_MATCH.
4429 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004430 add_state = NULL;
4431 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004432 switch (t->state->c)
4433 {
4434 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004435 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004436 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004437 copy_sub(&submatch->norm, &t->subs.norm);
4438#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004439 if (nfa_has_zsubexpr)
4440 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004441#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004442#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004443 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004444#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004445 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004446 * states at this position. When the list of states is going
4447 * to be empty quit without advancing, so that "reginput" is
4448 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004449 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004450 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004451 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004452 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004453
4454 case NFA_END_INVISIBLE:
Bram Moolenaar87953742013-06-05 18:52:40 +02004455 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004456 /*
4457 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004458 * NFA_START_INVISIBLE_BEFORE node.
4459 * They surround a zero-width group, used with "\@=", "\&",
4460 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004461 * If we got here, it means that the current "invisible" group
4462 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004463 * nfa_regmatch(). For a look-behind match only when it ends
4464 * in the position in "nfa_endp".
4465 * Submatches are stored in *m, and used in the parent call.
4466 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004467#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004468 if (nfa_endp != NULL)
4469 {
4470 if (REG_MULTI)
4471 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4472 (int)reglnum,
4473 (int)nfa_endp->se_u.pos.lnum,
4474 (int)(reginput - regline),
4475 nfa_endp->se_u.pos.col);
4476 else
4477 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4478 (int)(reginput - regline),
4479 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004480 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004481#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004482 /* If "nfa_endp" is set it's only a match if it ends at
4483 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004484 if (nfa_endp != NULL && (REG_MULTI
4485 ? (reglnum != nfa_endp->se_u.pos.lnum
4486 || (int)(reginput - regline)
4487 != nfa_endp->se_u.pos.col)
4488 : reginput != nfa_endp->se_u.ptr))
4489 break;
4490
4491 /* do not set submatches for \@! */
4492 if (!t->state->negated)
4493 {
4494 copy_sub(&m->norm, &t->subs.norm);
4495#ifdef FEAT_SYN_HL
4496 if (nfa_has_zsubexpr)
4497 copy_sub(&m->synt, &t->subs.synt);
4498#endif
4499 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004500#ifdef ENABLE_LOG
4501 fprintf(log_fd, "Match found:\n");
4502 log_subsexpr(m);
4503#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004504 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004505 break;
4506
4507 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004508 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004509 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004510 nfa_pim_T *pim;
4511 int cout = t->state->out1->out->c;
4512
4513 /* Do it directly when what follows is possibly end of
4514 * match (closing paren).
4515 * Postpone when it is \@<= or \@<!, these are expensive.
4516 * TODO: remove the check for t->pim and check multiple
4517 * where it's used?
4518 * Otherwise first do the one that has the highest chance
4519 * of failing. */
4520 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004521#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004522 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004523#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004524 || cout == NFA_NCLOSE
4525 || t->pim != NULL
4526 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4527 && failure_chance(t->state->out1->out, 0)
4528 < failure_chance(t->state->out, 0)))
4529 {
4530 /*
4531 * First try matching the invisible match, then what
4532 * follows.
4533 */
4534 result = recursive_regmatch(t->state, prog,
4535 submatch, m, &listids);
4536
4537 /* for \@! it is a match when result is FALSE */
4538 if (result != t->state->negated)
4539 {
4540 /* Copy submatch info from the recursive call */
4541 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004542#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004543 if (nfa_has_zsubexpr)
4544 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004545#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004546
Bram Moolenaara2d95102013-06-04 14:23:05 +02004547 /* t->state->out1 is the corresponding
4548 * END_INVISIBLE node; Add its out to the current
4549 * list (zero-width match). */
4550 addstate_here(thislist, t->state->out1->out,
4551 &t->subs, t->pim, &listidx);
4552 }
4553 }
4554 else
4555 {
4556 /*
4557 * First try matching what follows at the current
4558 * position. Only if a match is found, addstate() is
4559 * called, then verify the invisible match matches.
4560 * Add a nfa_pim_T to the following states, it
4561 * contains info about the invisible match.
4562 */
4563 if (ga_grow(&pimlist, 1) == FAIL)
4564 goto theend;
4565 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4566 ++pimlist.ga_len;
4567 pim->state = t->state;
4568 pim->pim = NULL;
4569 pim->result = NFA_PIM_TODO;
4570
4571 /* t->state->out1 is the corresponding END_INVISIBLE
4572 * node; Add its out to the current list (zero-width
4573 * match). */
4574 addstate_here(thislist, t->state->out1->out, &t->subs,
4575 pim, &listidx);
4576 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578 break;
4579
Bram Moolenaar87953742013-06-05 18:52:40 +02004580 case NFA_START_PATTERN:
4581 /* First try matching the pattern. */
4582 result = recursive_regmatch(t->state, prog,
4583 submatch, m, &listids);
4584 if (result)
4585 {
4586 int bytelen;
4587
4588#ifdef ENABLE_LOG
4589 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4590 log_subsexpr(m);
4591#endif
4592 /* Copy submatch info from the recursive call */
4593 copy_sub_off(&t->subs.norm, &m->norm);
4594#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004595 if (nfa_has_zsubexpr)
4596 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02004597#endif
4598 /* Now we need to skip over the matched text and then
4599 * continue with what follows. */
4600 if (REG_MULTI)
4601 /* TODO: multi-line match */
4602 bytelen = m->norm.list.multi[0].end.col
4603 - (int)(reginput - regline);
4604 else
4605 bytelen = (int)(m->norm.list.line[0].end - reginput);
4606
4607#ifdef ENABLE_LOG
4608 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4609#endif
4610 if (bytelen == 0)
4611 {
4612 /* empty match, output of corresponding
4613 * NFA_END_PATTERN/NFA_SKIP to be used at current
4614 * position */
4615 addstate_here(thislist, t->state->out1->out->out,
4616 &t->subs, t->pim, &listidx);
4617 }
4618 else if (bytelen <= clen)
4619 {
4620 /* match current character, output of corresponding
4621 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02004622 add_state = t->state->out1->out->out;
4623 add_off = clen;
4624 }
4625 else
4626 {
4627 /* skip over the matched characters, set character
4628 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02004629 add_state = t->state->out1->out;
4630 add_off = bytelen;
4631 add_count = bytelen - clen;
4632 }
4633 }
4634 break;
4635
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004636 case NFA_BOL:
4637 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004638 addstate_here(thislist, t->state->out, &t->subs,
4639 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004640 break;
4641
4642 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004643 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004644 addstate_here(thislist, t->state->out, &t->subs,
4645 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004646 break;
4647
4648 case NFA_BOW:
4649 {
4650 int bow = TRUE;
4651
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004652 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004653 bow = FALSE;
4654#ifdef FEAT_MBYTE
4655 else if (has_mbyte)
4656 {
4657 int this_class;
4658
4659 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004660 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004661 if (this_class <= 1)
4662 bow = FALSE;
4663 else if (reg_prev_class() == this_class)
4664 bow = FALSE;
4665 }
4666#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004667 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004668 || (reginput > regline
4669 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004670 bow = FALSE;
4671 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004672 addstate_here(thislist, t->state->out, &t->subs,
4673 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004674 break;
4675 }
4676
4677 case NFA_EOW:
4678 {
4679 int eow = TRUE;
4680
4681 if (reginput == regline)
4682 eow = FALSE;
4683#ifdef FEAT_MBYTE
4684 else if (has_mbyte)
4685 {
4686 int this_class, prev_class;
4687
4688 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004689 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004690 prev_class = reg_prev_class();
4691 if (this_class == prev_class
4692 || prev_class == 0 || prev_class == 1)
4693 eow = FALSE;
4694 }
4695#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004696 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004697 || (reginput[0] != NUL
4698 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699 eow = FALSE;
4700 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004701 addstate_here(thislist, t->state->out, &t->subs,
4702 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004703 break;
4704 }
4705
Bram Moolenaar4b780632013-05-31 22:14:52 +02004706 case NFA_BOF:
4707 if (reglnum == 0 && reginput == regline
4708 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004709 addstate_here(thislist, t->state->out, &t->subs,
4710 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004711 break;
4712
4713 case NFA_EOF:
4714 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004715 addstate_here(thislist, t->state->out, &t->subs,
4716 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004717 break;
4718
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004719#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004720 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004721 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004722 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004723 int len = 0;
4724 nfa_state_T *end;
4725 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004726 int cchars[MAX_MCO];
4727 int ccount = 0;
4728 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004729
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004731 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004732 if (utf_iscomposing(sta->c))
4733 {
4734 /* Only match composing character(s), ignore base
4735 * character. Used for ".{composing}" and "{composing}"
4736 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004737 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004738 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004739 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004740 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004741 /* If \Z was present, then ignore composing characters.
4742 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004743 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004744 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004745 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004746 else
4747 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004748 while (sta->c != NFA_END_COMPOSING)
4749 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004750 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004751
4752 /* Check base character matches first, unless ignored. */
4753 else if (len > 0 || mc == sta->c)
4754 {
4755 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004756 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004757 len += mb_char2len(mc);
4758 sta = sta->out;
4759 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004760
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004761 /* We don't care about the order of composing characters.
4762 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004763 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004764 {
4765 mc = mb_ptr2char(reginput + len);
4766 cchars[ccount++] = mc;
4767 len += mb_char2len(mc);
4768 if (ccount == MAX_MCO)
4769 break;
4770 }
4771
4772 /* Check that each composing char in the pattern matches a
4773 * composing char in the text. We do not check if all
4774 * composing chars are matched. */
4775 result = OK;
4776 while (sta->c != NFA_END_COMPOSING)
4777 {
4778 for (j = 0; j < ccount; ++j)
4779 if (cchars[j] == sta->c)
4780 break;
4781 if (j == ccount)
4782 {
4783 result = FAIL;
4784 break;
4785 }
4786 sta = sta->out;
4787 }
4788 }
4789 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004790 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004791
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004792 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004793 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004794 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004795 }
4796#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004797
4798 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004799 if (curc == NUL && !reg_line_lbr && REG_MULTI
4800 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004801 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004802 go_to_nextline = TRUE;
4803 /* Pass -1 for the offset, which means taking the position
4804 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004805 add_state = t->state->out;
4806 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004807 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004808 else if (curc == '\n' && reg_line_lbr)
4809 {
4810 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004811 add_state = t->state->out;
4812 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004813 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004814 break;
4815
4816 case NFA_CLASS_ALNUM:
4817 case NFA_CLASS_ALPHA:
4818 case NFA_CLASS_BLANK:
4819 case NFA_CLASS_CNTRL:
4820 case NFA_CLASS_DIGIT:
4821 case NFA_CLASS_GRAPH:
4822 case NFA_CLASS_LOWER:
4823 case NFA_CLASS_PRINT:
4824 case NFA_CLASS_PUNCT:
4825 case NFA_CLASS_SPACE:
4826 case NFA_CLASS_UPPER:
4827 case NFA_CLASS_XDIGIT:
4828 case NFA_CLASS_TAB:
4829 case NFA_CLASS_RETURN:
4830 case NFA_CLASS_BACKSPACE:
4831 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004832 result = check_char_class(t->state->c, curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004833 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004834 break;
4835
Bram Moolenaar417bad22013-06-07 14:08:30 +02004836 case NFA_START_COLL:
4837 case NFA_START_NEG_COLL:
4838 {
4839 /* What follows is a list of characters, until NFA_END_COLL.
4840 * One of them must match or none of them must match. */
4841 nfa_state_T *state;
4842 int result_if_matched;
4843 int c1, c2;
4844
4845 /* Never match EOL. If it's part of the collection it is added
4846 * as a separate state with an OR. */
4847 if (curc == NUL)
4848 break;
4849
4850 state = t->state->out;
4851 result_if_matched = (t->state->c == NFA_START_COLL);
4852 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004853 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02004854 if (state->c == NFA_END_COLL)
4855 {
4856 result = !result_if_matched;
4857 break;
4858 }
4859 if (state->c == NFA_RANGE_MIN)
4860 {
4861 c1 = state->val;
4862 state = state->out; /* advance to NFA_RANGE_MAX */
4863 c2 = state->val;
4864#ifdef ENABLE_LOG
4865 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
4866 curc, c1, c2);
4867#endif
4868 if (curc >= c1 && curc <= c2)
4869 {
4870 result = result_if_matched;
4871 break;
4872 }
4873 if (ireg_ic)
4874 {
4875 int curc_low = MB_TOLOWER(curc);
4876 int done = FALSE;
4877
4878 for ( ; c1 <= c2; ++c1)
4879 if (MB_TOLOWER(c1) == curc_low)
4880 {
4881 result = result_if_matched;
4882 done = TRUE;
4883 break;
4884 }
4885 if (done)
4886 break;
4887 }
4888 }
4889 else if (state->c < 0 ? check_char_class(state->c, curc)
4890 : (curc == state->c
4891 || (ireg_ic && MB_TOLOWER(curc)
4892 == MB_TOLOWER(state->c))))
4893 {
4894 result = result_if_matched;
4895 break;
4896 }
4897 state = state->out;
4898 }
4899 if (result)
4900 {
4901 /* next state is in out of the NFA_END_COLL, out1 of
4902 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004903 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004904 add_off = clen;
4905 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004906 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02004907 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004908
4909 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004910 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004911 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004912 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004913 add_state = t->state->out;
4914 add_off = clen;
4915 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004916 break;
4917
4918 /*
4919 * Character classes like \a for alpha, \d for digit etc.
4920 */
4921 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004922 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004923 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004924 break;
4925
4926 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004927 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004928 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004929 break;
4930
4931 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004932 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004933 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004934 break;
4935
4936 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004937 result = !VIM_ISDIGIT(curc)
4938 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004939 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940 break;
4941
4942 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004943 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004944 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004945 break;
4946
4947 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004948 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004949 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004950 break;
4951
4952 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004953 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004954 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004955 break;
4956
4957 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004958 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004959 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004960 break;
4961
4962 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004963 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004964 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004965 break;
4966
4967 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004968 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004969 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004970 break;
4971
4972 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004973 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004974 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004975 break;
4976
4977 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004978 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004979 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004980 break;
4981
4982 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004983 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004984 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004985 break;
4986
4987 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004988 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004989 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004990 break;
4991
4992 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004993 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004994 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004995 break;
4996
4997 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004998 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004999 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005000 break;
5001
5002 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005003 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005004 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005005 break;
5006
5007 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005008 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005009 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005010 break;
5011
5012 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005013 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005014 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005015 break;
5016
5017 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005018 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005019 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005020 break;
5021
5022 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005023 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005024 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005025 break;
5026
5027 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005028 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005029 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030 break;
5031
5032 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005033 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005034 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005035 break;
5036
5037 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005038 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005039 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005040 break;
5041
5042 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005043 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005044 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005045 break;
5046
5047 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005048 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005049 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005050 break;
5051
Bram Moolenaar5714b802013-05-28 22:03:20 +02005052 case NFA_BACKREF1:
5053 case NFA_BACKREF2:
5054 case NFA_BACKREF3:
5055 case NFA_BACKREF4:
5056 case NFA_BACKREF5:
5057 case NFA_BACKREF6:
5058 case NFA_BACKREF7:
5059 case NFA_BACKREF8:
5060 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005061#ifdef FEAT_SYN_HL
5062 case NFA_ZREF1:
5063 case NFA_ZREF2:
5064 case NFA_ZREF3:
5065 case NFA_ZREF4:
5066 case NFA_ZREF5:
5067 case NFA_ZREF6:
5068 case NFA_ZREF7:
5069 case NFA_ZREF8:
5070 case NFA_ZREF9:
5071#endif
5072 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005073 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005074 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005075 int bytelen;
5076
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005077 if (t->state->c <= NFA_BACKREF9)
5078 {
5079 subidx = t->state->c - NFA_BACKREF1 + 1;
5080 result = match_backref(&t->subs.norm, subidx, &bytelen);
5081 }
5082#ifdef FEAT_SYN_HL
5083 else
5084 {
5085 subidx = t->state->c - NFA_ZREF1 + 1;
5086 result = match_zref(subidx, &bytelen);
5087 }
5088#endif
5089
Bram Moolenaar5714b802013-05-28 22:03:20 +02005090 if (result)
5091 {
5092 if (bytelen == 0)
5093 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005094 /* empty match always works, output of NFA_SKIP to be
5095 * used next */
5096 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005097 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02005098 }
5099 else if (bytelen <= clen)
5100 {
5101 /* match current character, jump ahead to out of
5102 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005103 add_state = t->state->out->out;
5104 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005105 }
5106 else
5107 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005108 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005109 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005110 add_state = t->state->out;
5111 add_off = bytelen;
5112 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005113 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005114 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005115 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005116 }
5117 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005118 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005119 if (t->count - clen <= 0)
5120 {
5121 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005122 add_state = t->state->out;
5123 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005124 }
5125 else
5126 {
5127 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005128 add_state = t->state;
5129 add_off = 0;
5130 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005131 }
5132 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005133
Bram Moolenaar423532e2013-05-29 21:14:42 +02005134 case NFA_LNUM:
5135 case NFA_LNUM_GT:
5136 case NFA_LNUM_LT:
5137 result = (REG_MULTI &&
5138 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5139 (long_u)(reglnum + reg_firstlnum)));
5140 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005141 addstate_here(thislist, t->state->out, &t->subs,
5142 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005143 break;
5144
5145 case NFA_COL:
5146 case NFA_COL_GT:
5147 case NFA_COL_LT:
5148 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5149 (long_u)(reginput - regline) + 1);
5150 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005151 addstate_here(thislist, t->state->out, &t->subs,
5152 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005153 break;
5154
5155 case NFA_VCOL:
5156 case NFA_VCOL_GT:
5157 case NFA_VCOL_LT:
5158 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5159 (long_u)win_linetabsize(
5160 reg_win == NULL ? curwin : reg_win,
5161 regline, (colnr_T)(reginput - regline)) + 1);
5162 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005163 addstate_here(thislist, t->state->out, &t->subs,
5164 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005165 break;
5166
Bram Moolenaar044aa292013-06-04 21:27:38 +02005167 case NFA_MARK:
5168 case NFA_MARK_GT:
5169 case NFA_MARK_LT:
5170 {
5171 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5172
5173 /* Compare the mark position to the match position. */
5174 result = (pos != NULL /* mark doesn't exist */
5175 && pos->lnum > 0 /* mark isn't set in reg_buf */
5176 && (pos->lnum == reglnum + reg_firstlnum
5177 ? (pos->col == (colnr_T)(reginput - regline)
5178 ? t->state->c == NFA_MARK
5179 : (pos->col < (colnr_T)(reginput - regline)
5180 ? t->state->c == NFA_MARK_GT
5181 : t->state->c == NFA_MARK_LT))
5182 : (pos->lnum < reglnum + reg_firstlnum
5183 ? t->state->c == NFA_MARK_GT
5184 : t->state->c == NFA_MARK_LT)));
5185 if (result)
5186 addstate_here(thislist, t->state->out, &t->subs,
5187 t->pim, &listidx);
5188 break;
5189 }
5190
Bram Moolenaar423532e2013-05-29 21:14:42 +02005191 case NFA_CURSOR:
5192 result = (reg_win != NULL
5193 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5194 && ((colnr_T)(reginput - regline)
5195 == reg_win->w_cursor.col));
5196 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005197 addstate_here(thislist, t->state->out, &t->subs,
5198 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005199 break;
5200
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005201 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005202#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005203 result = reg_match_visual();
5204 if (result)
5205 addstate_here(thislist, t->state->out, &t->subs,
5206 t->pim, &listidx);
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005207#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005208 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005209
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005210 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005211 {
5212 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005213
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005214 /* TODO: put this in #ifdef later */
5215 if (c < -256)
5216 EMSGN("INTERNAL: Negative state char: %ld", c);
5217 if (is_Magic(c))
5218 c = un_Magic(c);
5219 result = (c == curc);
5220
5221 if (!result && ireg_ic)
5222 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005223#ifdef FEAT_MBYTE
5224 /* If there is a composing character which is not being
5225 * ignored there can be no match. Match with composing
5226 * character uses NFA_COMPOSING above. */
5227 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005228 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005229 result = FALSE;
5230#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005231 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005232 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005233 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005234
5235 } /* switch (t->state->c) */
5236
5237 if (add_state != NULL)
5238 {
5239 if (t->pim != NULL)
5240 {
5241 /* postponed invisible match */
5242 /* TODO: also do t->pim->pim recursively? */
5243 if (t->pim->result == NFA_PIM_TODO)
5244 {
5245#ifdef ENABLE_LOG
5246 fprintf(log_fd, "\n");
5247 fprintf(log_fd, "==================================\n");
5248 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5249 fprintf(log_fd, "\n");
5250#endif
5251 result = recursive_regmatch(t->pim->state,
5252 prog, submatch, m, &listids);
5253 t->pim->result = result ? NFA_PIM_MATCH
5254 : NFA_PIM_NOMATCH;
5255 /* for \@! it is a match when result is FALSE */
5256 if (result != t->pim->state->negated)
5257 {
5258 /* Copy submatch info from the recursive call */
5259 copy_sub_off(&t->pim->subs.norm, &m->norm);
5260#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005261 if (nfa_has_zsubexpr)
5262 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005263#endif
5264 }
5265 }
5266 else
5267 {
5268 result = (t->pim->result == NFA_PIM_MATCH);
5269#ifdef ENABLE_LOG
5270 fprintf(log_fd, "\n");
5271 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5272 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5273 fprintf(log_fd, "\n");
5274#endif
5275 }
5276
5277 /* for \@! it is a match when result is FALSE */
5278 if (result != t->pim->state->negated)
5279 {
5280 /* Copy submatch info from the recursive call */
5281 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5282#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005283 if (nfa_has_zsubexpr)
5284 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005285#endif
5286 }
5287 else
5288 /* look-behind match failed, don't add the state */
5289 continue;
5290 }
5291
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005292 addstate(nextlist, add_state, &t->subs, add_off);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005293 if (add_count > 0)
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005294 nextlist->t[nextlist->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005295 }
5296
5297 } /* for (thislist = thislist; thislist->state; thislist++) */
5298
Bram Moolenaare23febd2013-05-26 18:40:14 +02005299 /* Look for the start of a match in the current position by adding the
5300 * start state to the list of states.
5301 * The first found match is the leftmost one, thus the order of states
5302 * matters!
5303 * Do not add the start state in recursive calls of nfa_regmatch(),
5304 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005305 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005306 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005307 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005308 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005309 && reglnum == 0
5310 && clen != 0
5311 && (ireg_maxcol == 0
5312 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005313 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005314 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005315 ? (reglnum < nfa_endp->se_u.pos.lnum
5316 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005317 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005318 < nfa_endp->se_u.pos.col))
5319 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005320 {
5321#ifdef ENABLE_LOG
5322 fprintf(log_fd, "(---) STARTSTATE\n");
5323#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005324 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005325 }
5326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005327#ifdef ENABLE_LOG
5328 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005329 {
5330 int i;
5331
5332 for (i = 0; i < thislist->n; i++)
5333 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5334 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005335 fprintf(log_fd, "\n");
5336#endif
5337
5338nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005339 /* Advance to the next character, or advance to the next line, or
5340 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005341 if (clen != 0)
5342 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005343 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5344 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005345 reg_nextline();
5346 else
5347 break;
5348 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005349
5350#ifdef ENABLE_LOG
5351 if (log_fd != stderr)
5352 fclose(log_fd);
5353 log_fd = NULL;
5354#endif
5355
5356theend:
5357 /* Free memory */
5358 vim_free(list[0].t);
5359 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005360 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005361 ga_clear(&pimlist);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005362#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005363#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005364 fclose(debug);
5365#endif
5366
Bram Moolenaar963fee22013-05-26 21:47:28 +02005367 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005368}
5369
5370/*
5371 * Try match of "prog" with at regline["col"].
5372 * Returns 0 for failure, number of lines contained in the match otherwise.
5373 */
5374 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005375nfa_regtry(prog, col)
5376 nfa_regprog_T *prog;
5377 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005378{
5379 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005380 regsubs_T subs, m;
5381 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005382#ifdef ENABLE_LOG
5383 FILE *f;
5384#endif
5385
5386 reginput = regline + col;
5387 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005388#ifdef FEAT_SYN_HL
5389 /* Clear the external match subpointers if necessary. */
5390 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005391 {
5392 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005393 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005394 }
5395 else
5396 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005397#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005398
5399#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005400 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005401 if (f != NULL)
5402 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005403 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005404#ifdef DEBUG
5405 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5406#endif
5407 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005408 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005409 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 fprintf(f, "\n\n");
5411 fclose(f);
5412 }
5413 else
5414 EMSG(_("Could not open temporary log file for writing "));
5415#endif
5416
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005417 clear_sub(&subs.norm);
5418 clear_sub(&m.norm);
5419#ifdef FEAT_SYN_HL
5420 clear_sub(&subs.synt);
5421 clear_sub(&m.synt);
5422#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005423
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005424 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005425 return 0;
5426
5427 cleanup_subexpr();
5428 if (REG_MULTI)
5429 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005430 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005431 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005432 reg_startpos[i] = subs.norm.list.multi[i].start;
5433 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005434 }
5435
5436 if (reg_startpos[0].lnum < 0)
5437 {
5438 reg_startpos[0].lnum = 0;
5439 reg_startpos[0].col = col;
5440 }
5441 if (reg_endpos[0].lnum < 0)
5442 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005443 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444 reg_endpos[0].lnum = reglnum;
5445 reg_endpos[0].col = (int)(reginput - regline);
5446 }
5447 else
5448 /* Use line number of "\ze". */
5449 reglnum = reg_endpos[0].lnum;
5450 }
5451 else
5452 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005453 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005454 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005455 reg_startp[i] = subs.norm.list.line[i].start;
5456 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005457 }
5458
5459 if (reg_startp[0] == NULL)
5460 reg_startp[0] = regline + col;
5461 if (reg_endp[0] == NULL)
5462 reg_endp[0] = reginput;
5463 }
5464
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005465#ifdef FEAT_SYN_HL
5466 /* Package any found \z(...\) matches for export. Default is none. */
5467 unref_extmatch(re_extmatch_out);
5468 re_extmatch_out = NULL;
5469
5470 if (prog->reghasz == REX_SET)
5471 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005472 cleanup_zsubexpr();
5473 re_extmatch_out = make_extmatch();
5474 for (i = 0; i < subs.synt.in_use; i++)
5475 {
5476 if (REG_MULTI)
5477 {
5478 struct multipos *mpos = &subs.synt.list.multi[i];
5479
5480 /* Only accept single line matches. */
5481 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5482 re_extmatch_out->matches[i] =
5483 vim_strnsave(reg_getline(mpos->start.lnum)
5484 + mpos->start.col,
5485 mpos->end.col - mpos->start.col);
5486 }
5487 else
5488 {
5489 struct linepos *lpos = &subs.synt.list.line[i];
5490
5491 if (lpos->start != NULL && lpos->end != NULL)
5492 re_extmatch_out->matches[i] =
5493 vim_strnsave(lpos->start,
5494 (int)(lpos->end - lpos->start));
5495 }
5496 }
5497 }
5498#endif
5499
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005500 return 1 + reglnum;
5501}
5502
5503/*
5504 * Match a regexp against a string ("line" points to the string) or multiple
5505 * lines ("line" is NULL, use reg_getline()).
5506 *
5507 * Returns 0 for failure, number of lines contained in the match otherwise.
5508 */
5509 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02005510nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005511 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005512 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513{
5514 nfa_regprog_T *prog;
5515 long retval = 0L;
5516 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005517 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518
5519 if (REG_MULTI)
5520 {
5521 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5522 line = reg_getline((linenr_T)0); /* relative to the cursor */
5523 reg_startpos = reg_mmatch->startpos;
5524 reg_endpos = reg_mmatch->endpos;
5525 }
5526 else
5527 {
5528 prog = (nfa_regprog_T *)reg_match->regprog;
5529 reg_startp = reg_match->startp;
5530 reg_endp = reg_match->endp;
5531 }
5532
5533 /* Be paranoid... */
5534 if (prog == NULL || line == NULL)
5535 {
5536 EMSG(_(e_null));
5537 goto theend;
5538 }
5539
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005540 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5541 if (prog->regflags & RF_ICASE)
5542 ireg_ic = TRUE;
5543 else if (prog->regflags & RF_NOICASE)
5544 ireg_ic = FALSE;
5545
5546#ifdef FEAT_MBYTE
5547 /* If pattern contains "\Z" overrule value of ireg_icombine */
5548 if (prog->regflags & RF_ICOMBINE)
5549 ireg_icombine = TRUE;
5550#endif
5551
5552 regline = line;
5553 reglnum = 0; /* relative to line */
5554
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005555 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005556 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005557 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005558 nfa_listid = 1;
5559 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005560#ifdef DEBUG
5561 nfa_regengine.expr = prog->pattern;
5562#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563
Bram Moolenaard89616e2013-06-06 18:46:06 +02005564 if (prog->reganch && col > 0)
5565 return 0L;
5566
5567 if (prog->regstart != NUL)
5568 {
5569 char_u *s;
5570
5571 /* Skip until the char we know it must start with.
5572 * Used often, do some work to avoid call overhead. */
5573 if (!ireg_ic
5574#ifdef FEAT_MBYTE
5575 && !has_mbyte
5576#endif
5577 )
5578 s = vim_strbyte(regline + col, prog->regstart);
5579 else
5580 s = cstrchr(regline + col, prog->regstart);
5581 if (s == NULL)
5582 return 0L;
5583 col = (int)(s - regline);
5584 }
5585
5586 /* If the start column is past the maximum column: no need to try. */
5587 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5588 goto theend;
5589
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005590 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 for (i = 0; i < nstate; ++i)
5592 {
5593 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005594 prog->state[i].lastlist[0] = 0;
5595 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005596 }
5597
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005598 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005600#ifdef DEBUG
5601 nfa_regengine.expr = NULL;
5602#endif
5603
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005604theend:
5605 return retval;
5606}
5607
5608/*
5609 * Compile a regular expression into internal code for the NFA matcher.
5610 * Returns the program in allocated space. Returns NULL for an error.
5611 */
5612 static regprog_T *
5613nfa_regcomp(expr, re_flags)
5614 char_u *expr;
5615 int re_flags;
5616{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005617 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005618 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005619 int *postfix;
5620
5621 if (expr == NULL)
5622 return NULL;
5623
5624#ifdef DEBUG
5625 nfa_regengine.expr = expr;
5626#endif
5627
5628 init_class_tab();
5629
5630 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5631 return NULL;
5632
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005633 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005634 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005635 postfix = re2post();
5636 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005637 {
5638 /* TODO: only give this error for debugging? */
5639 if (post_ptr >= post_end)
5640 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005642 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005643
5644 /*
5645 * In order to build the NFA, we parse the input regexp twice:
5646 * 1. first pass to count size (so we can allocate space)
5647 * 2. second to emit code
5648 */
5649#ifdef ENABLE_LOG
5650 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005651 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005652
5653 if (f != NULL)
5654 {
5655 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5656 fclose(f);
5657 }
5658 }
5659#endif
5660
5661 /*
5662 * PASS 1
5663 * Count number of NFA states in "nstate". Do not build the NFA.
5664 */
5665 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005666
5667 /* Space for compiled regexp */
5668 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5669 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5670 if (prog == NULL)
5671 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005672 state_ptr = prog->state;
5673
5674 /*
5675 * PASS 2
5676 * Build the NFA
5677 */
5678 prog->start = post2nfa(postfix, post_ptr, FALSE);
5679 if (prog->start == NULL)
5680 goto fail;
5681
5682 prog->regflags = regflags;
5683 prog->engine = &nfa_regengine;
5684 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005685 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005686 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005687 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005688
5689 prog->reganch = nfa_get_reganch(prog->start, 0);
5690 prog->regstart = nfa_get_regstart(prog->start, 0);
5691
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005692#ifdef ENABLE_LOG
5693 nfa_postfix_dump(expr, OK);
5694 nfa_dump(prog);
5695#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005696#ifdef FEAT_SYN_HL
5697 /* Remember whether this pattern has any \z specials in it. */
5698 prog->reghasz = re_has_z;
5699#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005700#ifdef DEBUG
5701 prog->pattern = vim_strsave(expr); /* memory will leak */
5702 nfa_regengine.expr = NULL;
5703#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005704
5705out:
5706 vim_free(post_start);
5707 post_start = post_ptr = post_end = NULL;
5708 state_ptr = NULL;
5709 return (regprog_T *)prog;
5710
5711fail:
5712 vim_free(prog);
5713 prog = NULL;
5714#ifdef ENABLE_LOG
5715 nfa_postfix_dump(expr, FAIL);
5716#endif
5717#ifdef DEBUG
5718 nfa_regengine.expr = NULL;
5719#endif
5720 goto out;
5721}
5722
5723
5724/*
5725 * Match a regexp against a string.
5726 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5727 * Uses curbuf for line count and 'iskeyword'.
5728 *
5729 * Return TRUE if there is a match, FALSE if not.
5730 */
5731 static int
5732nfa_regexec(rmp, line, col)
5733 regmatch_T *rmp;
5734 char_u *line; /* string to match against */
5735 colnr_T col; /* column to start looking for match */
5736{
5737 reg_match = rmp;
5738 reg_mmatch = NULL;
5739 reg_maxline = 0;
5740 reg_line_lbr = FALSE;
5741 reg_buf = curbuf;
5742 reg_win = NULL;
5743 ireg_ic = rmp->rm_ic;
5744#ifdef FEAT_MBYTE
5745 ireg_icombine = FALSE;
5746#endif
5747 ireg_maxcol = 0;
5748 return (nfa_regexec_both(line, col) != 0);
5749}
5750
5751#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5752 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5753
5754static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5755
5756/*
5757 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5758 */
5759 static int
5760nfa_regexec_nl(rmp, line, col)
5761 regmatch_T *rmp;
5762 char_u *line; /* string to match against */
5763 colnr_T col; /* column to start looking for match */
5764{
5765 reg_match = rmp;
5766 reg_mmatch = NULL;
5767 reg_maxline = 0;
5768 reg_line_lbr = TRUE;
5769 reg_buf = curbuf;
5770 reg_win = NULL;
5771 ireg_ic = rmp->rm_ic;
5772#ifdef FEAT_MBYTE
5773 ireg_icombine = FALSE;
5774#endif
5775 ireg_maxcol = 0;
5776 return (nfa_regexec_both(line, col) != 0);
5777}
5778#endif
5779
5780
5781/*
5782 * Match a regexp against multiple lines.
5783 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5784 * Uses curbuf for line count and 'iskeyword'.
5785 *
5786 * Return zero if there is no match. Return number of lines contained in the
5787 * match otherwise.
5788 *
5789 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5790 *
5791 * ! Also NOTE : match may actually be in another line. e.g.:
5792 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5793 *
5794 * +-------------------------+
5795 * |a |
5796 * |b |
5797 * |c |
5798 * | |
5799 * +-------------------------+
5800 *
5801 * then nfa_regexec_multi() returns 3. while the original
5802 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5803 *
5804 * FIXME if this behavior is not compatible.
5805 */
5806 static long
5807nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5808 regmmatch_T *rmp;
5809 win_T *win; /* window in which to search or NULL */
5810 buf_T *buf; /* buffer in which to search */
5811 linenr_T lnum; /* nr of line to start looking for match */
5812 colnr_T col; /* column to start looking for match */
5813 proftime_T *tm UNUSED; /* timeout limit or NULL */
5814{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005815 reg_match = NULL;
5816 reg_mmatch = rmp;
5817 reg_buf = buf;
5818 reg_win = win;
5819 reg_firstlnum = lnum;
5820 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5821 reg_line_lbr = FALSE;
5822 ireg_ic = rmp->rmm_ic;
5823#ifdef FEAT_MBYTE
5824 ireg_icombine = FALSE;
5825#endif
5826 ireg_maxcol = rmp->rmm_maxcol;
5827
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005828 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005829}
5830
5831#ifdef DEBUG
5832# undef ENABLE_LOG
5833#endif