blob: 8fdbb3e5ced7d0f441f191a9fe0b1f4ce3ff7d11 [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 Moolenaardecd9542013-06-07 16:31:50 +020067 NFA_START_INVISIBLE_NEG,
Bram Moolenaar61602c52013-06-01 19:54:43 +020068 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020069 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020070 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020071 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020072 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020073 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020074 NFA_COMPOSING, /* Next nodes in NFA are part of the
75 composing multibyte char */
76 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020077 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078
79 /* The following are used only in the postfix form, not in the NFA */
80 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
81 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
82 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
83 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
84 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
85
Bram Moolenaar5714b802013-05-28 22:03:20 +020086 NFA_BACKREF1, /* \1 */
87 NFA_BACKREF2, /* \2 */
88 NFA_BACKREF3, /* \3 */
89 NFA_BACKREF4, /* \4 */
90 NFA_BACKREF5, /* \5 */
91 NFA_BACKREF6, /* \6 */
92 NFA_BACKREF7, /* \7 */
93 NFA_BACKREF8, /* \8 */
94 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020095#ifdef FEAT_SYN_HL
96 NFA_ZREF1, /* \z1 */
97 NFA_ZREF2, /* \z2 */
98 NFA_ZREF3, /* \z3 */
99 NFA_ZREF4, /* \z4 */
100 NFA_ZREF5, /* \z5 */
101 NFA_ZREF6, /* \z6 */
102 NFA_ZREF7, /* \z7 */
103 NFA_ZREF8, /* \z8 */
104 NFA_ZREF9, /* \z9 */
105#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200106 NFA_SKIP, /* Skip characters */
107
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200108 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200109 NFA_MOPEN1,
110 NFA_MOPEN2,
111 NFA_MOPEN3,
112 NFA_MOPEN4,
113 NFA_MOPEN5,
114 NFA_MOPEN6,
115 NFA_MOPEN7,
116 NFA_MOPEN8,
117 NFA_MOPEN9,
118
119 NFA_MCLOSE,
120 NFA_MCLOSE1,
121 NFA_MCLOSE2,
122 NFA_MCLOSE3,
123 NFA_MCLOSE4,
124 NFA_MCLOSE5,
125 NFA_MCLOSE6,
126 NFA_MCLOSE7,
127 NFA_MCLOSE8,
128 NFA_MCLOSE9,
129
130#ifdef FEAT_SYN_HL
131 NFA_ZOPEN,
132 NFA_ZOPEN1,
133 NFA_ZOPEN2,
134 NFA_ZOPEN3,
135 NFA_ZOPEN4,
136 NFA_ZOPEN5,
137 NFA_ZOPEN6,
138 NFA_ZOPEN7,
139 NFA_ZOPEN8,
140 NFA_ZOPEN9,
141
142 NFA_ZCLOSE,
143 NFA_ZCLOSE1,
144 NFA_ZCLOSE2,
145 NFA_ZCLOSE3,
146 NFA_ZCLOSE4,
147 NFA_ZCLOSE5,
148 NFA_ZCLOSE6,
149 NFA_ZCLOSE7,
150 NFA_ZCLOSE8,
151 NFA_ZCLOSE9,
152#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200153
154 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200155 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200156 NFA_ANYOF, /* Match any character in this string. */
157 NFA_ANYBUT, /* Match any character not in this string. */
158 NFA_IDENT, /* Match identifier char */
159 NFA_SIDENT, /* Match identifier char but no digit */
160 NFA_KWORD, /* Match keyword char */
161 NFA_SKWORD, /* Match word char but no digit */
162 NFA_FNAME, /* Match file name char */
163 NFA_SFNAME, /* Match file name char but no digit */
164 NFA_PRINT, /* Match printable char */
165 NFA_SPRINT, /* Match printable char but no digit */
166 NFA_WHITE, /* Match whitespace char */
167 NFA_NWHITE, /* Match non-whitespace char */
168 NFA_DIGIT, /* Match digit char */
169 NFA_NDIGIT, /* Match non-digit char */
170 NFA_HEX, /* Match hex char */
171 NFA_NHEX, /* Match non-hex char */
172 NFA_OCTAL, /* Match octal char */
173 NFA_NOCTAL, /* Match non-octal char */
174 NFA_WORD, /* Match word char */
175 NFA_NWORD, /* Match non-word char */
176 NFA_HEAD, /* Match head char */
177 NFA_NHEAD, /* Match non-head char */
178 NFA_ALPHA, /* Match alpha char */
179 NFA_NALPHA, /* Match non-alpha char */
180 NFA_LOWER, /* Match lowercase char */
181 NFA_NLOWER, /* Match non-lowercase char */
182 NFA_UPPER, /* Match uppercase char */
183 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200184
185 NFA_CURSOR, /* Match cursor pos */
186 NFA_LNUM, /* Match line number */
187 NFA_LNUM_GT, /* Match > line number */
188 NFA_LNUM_LT, /* Match < line number */
189 NFA_COL, /* Match cursor column */
190 NFA_COL_GT, /* Match > cursor column */
191 NFA_COL_LT, /* Match < cursor column */
192 NFA_VCOL, /* Match cursor virtual column */
193 NFA_VCOL_GT, /* Match > cursor virtual column */
194 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200195 NFA_MARK, /* Match mark */
196 NFA_MARK_GT, /* Match > mark */
197 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200198 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200199
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200200 NFA_FIRST_NL = NFA_ANY + ADD_NL,
201 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
202
203 /* Character classes [:alnum:] etc */
204 NFA_CLASS_ALNUM,
205 NFA_CLASS_ALPHA,
206 NFA_CLASS_BLANK,
207 NFA_CLASS_CNTRL,
208 NFA_CLASS_DIGIT,
209 NFA_CLASS_GRAPH,
210 NFA_CLASS_LOWER,
211 NFA_CLASS_PRINT,
212 NFA_CLASS_PUNCT,
213 NFA_CLASS_SPACE,
214 NFA_CLASS_UPPER,
215 NFA_CLASS_XDIGIT,
216 NFA_CLASS_TAB,
217 NFA_CLASS_RETURN,
218 NFA_CLASS_BACKSPACE,
219 NFA_CLASS_ESCAPE
220};
221
222/* Keep in sync with classchars. */
223static int nfa_classcodes[] = {
224 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
225 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
226 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
227 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
228 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
229 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
230 NFA_UPPER, NFA_NUPPER
231};
232
233static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
234
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200235/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200236static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237
Bram Moolenaar428e9872013-05-30 17:05:39 +0200238/* NFA regexp \1 .. \9 encountered. */
239static int nfa_has_backref;
240
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200241#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200242/* NFA regexp has \z( ), set zsubexpr. */
243static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200244#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200245
Bram Moolenaar963fee22013-05-26 21:47:28 +0200246/* Number of sub expressions actually being used during execution. 1 if only
247 * the whole match (subexpr 0) is used. */
248static int nfa_nsubexpr;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250static int *post_start; /* holds the postfix form of r.e. */
251static int *post_end;
252static int *post_ptr;
253
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200254static int nstate; /* Number of states in the NFA. Also used when
255 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200256static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar307aa162013-06-02 16:34:21 +0200258/* If not NULL match must end at this position */
259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200261/* listid is global, so that it increases on recursive calls to
262 * nfa_regmatch(), which means we don't have to clear the lastlist field of
263 * all the states. */
264static int nfa_listid;
265static int nfa_alt_listid;
266
267/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
268static int nfa_ll_index = 0;
269
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200270static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200271static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
272static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200274static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275static int nfa_regatom __ARGS((void));
276static int nfa_regpiece __ARGS((void));
277static int nfa_regconcat __ARGS((void));
278static int nfa_regbranch __ARGS((void));
279static int nfa_reg __ARGS((int paren));
280#ifdef DEBUG
281static void nfa_set_code __ARGS((int c));
282static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200283static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
284static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200285static void nfa_dump __ARGS((nfa_regprog_T *prog));
286#endif
287static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200288static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
290static int check_char_class __ARGS((int class, int c));
291static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200292static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
293static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200294static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200295static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200296static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
297static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
298static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
299static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
300
301/* helper functions used when doing re2post() ... regatom() parsing */
302#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200303 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200304 return FAIL; \
305 *post_ptr++ = c; \
306 } while (0)
307
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308/*
309 * Initialize internal variables before NFA compilation.
310 * Return OK on success, FAIL otherwise.
311 */
312 static int
313nfa_regcomp_start(expr, re_flags)
314 char_u *expr;
315 int re_flags; /* see vim_regcomp() */
316{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200317 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200318 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200319
320 nstate = 0;
321 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200322 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200323 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200325 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200326 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200327 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200328
329 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200330 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200331
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200332 post_start = (int *)lalloc(postfix_size, TRUE);
333 if (post_start == NULL)
334 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200335 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200336 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200337 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200338 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200340 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341 regcomp_start(expr, re_flags);
342
343 return OK;
344}
345
346/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200347 * Figure out if the NFA state list starts with an anchor, must match at start
348 * of the line.
349 */
350 static int
351nfa_get_reganch(start, depth)
352 nfa_state_T *start;
353 int depth;
354{
355 nfa_state_T *p = start;
356
357 if (depth > 4)
358 return 0;
359
360 while (p != NULL)
361 {
362 switch (p->c)
363 {
364 case NFA_BOL:
365 case NFA_BOF:
366 return 1; /* yes! */
367
368 case NFA_ZSTART:
369 case NFA_ZEND:
370 case NFA_CURSOR:
371 case NFA_VISUAL:
372
373 case NFA_MOPEN:
374 case NFA_MOPEN1:
375 case NFA_MOPEN2:
376 case NFA_MOPEN3:
377 case NFA_MOPEN4:
378 case NFA_MOPEN5:
379 case NFA_MOPEN6:
380 case NFA_MOPEN7:
381 case NFA_MOPEN8:
382 case NFA_MOPEN9:
383 case NFA_NOPEN:
384#ifdef FEAT_SYN_HL
385 case NFA_ZOPEN:
386 case NFA_ZOPEN1:
387 case NFA_ZOPEN2:
388 case NFA_ZOPEN3:
389 case NFA_ZOPEN4:
390 case NFA_ZOPEN5:
391 case NFA_ZOPEN6:
392 case NFA_ZOPEN7:
393 case NFA_ZOPEN8:
394 case NFA_ZOPEN9:
395#endif
396 p = p->out;
397 break;
398
399 case NFA_SPLIT:
400 return nfa_get_reganch(p->out, depth + 1)
401 && nfa_get_reganch(p->out1, depth + 1);
402
403 default:
404 return 0; /* noooo */
405 }
406 }
407 return 0;
408}
409
410/*
411 * Figure out if the NFA state list starts with a character which must match
412 * at start of the match.
413 */
414 static int
415nfa_get_regstart(start, depth)
416 nfa_state_T *start;
417 int depth;
418{
419 nfa_state_T *p = start;
420
421 if (depth > 4)
422 return 0;
423
424 while (p != NULL)
425 {
426 switch (p->c)
427 {
428 /* all kinds of zero-width matches */
429 case NFA_BOL:
430 case NFA_BOF:
431 case NFA_BOW:
432 case NFA_EOW:
433 case NFA_ZSTART:
434 case NFA_ZEND:
435 case NFA_CURSOR:
436 case NFA_VISUAL:
437 case NFA_LNUM:
438 case NFA_LNUM_GT:
439 case NFA_LNUM_LT:
440 case NFA_COL:
441 case NFA_COL_GT:
442 case NFA_COL_LT:
443 case NFA_VCOL:
444 case NFA_VCOL_GT:
445 case NFA_VCOL_LT:
446 case NFA_MARK:
447 case NFA_MARK_GT:
448 case NFA_MARK_LT:
449
450 case NFA_MOPEN:
451 case NFA_MOPEN1:
452 case NFA_MOPEN2:
453 case NFA_MOPEN3:
454 case NFA_MOPEN4:
455 case NFA_MOPEN5:
456 case NFA_MOPEN6:
457 case NFA_MOPEN7:
458 case NFA_MOPEN8:
459 case NFA_MOPEN9:
460 case NFA_NOPEN:
461#ifdef FEAT_SYN_HL
462 case NFA_ZOPEN:
463 case NFA_ZOPEN1:
464 case NFA_ZOPEN2:
465 case NFA_ZOPEN3:
466 case NFA_ZOPEN4:
467 case NFA_ZOPEN5:
468 case NFA_ZOPEN6:
469 case NFA_ZOPEN7:
470 case NFA_ZOPEN8:
471 case NFA_ZOPEN9:
472#endif
473 p = p->out;
474 break;
475
476 case NFA_SPLIT:
477 {
478 int c1 = nfa_get_regstart(p->out, depth + 1);
479 int c2 = nfa_get_regstart(p->out1, depth + 1);
480
481 if (c1 == c2)
482 return c1; /* yes! */
483 return 0;
484 }
485
486 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200487 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200488 return p->c; /* yes! */
489 return 0;
490 }
491 }
492 return 0;
493}
494
495/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200496 * Allocate more space for post_start. Called when
497 * running above the estimated number of states.
498 */
499 static int
500realloc_post_list()
501{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200502 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200503 int new_max = nstate_max + 1000;
504 int *new_start;
505 int *old_start;
506
507 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
508 if (new_start == NULL)
509 return FAIL;
510 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200511 old_start = post_start;
512 post_start = new_start;
513 post_ptr = new_start + (post_ptr - old_start);
514 post_end = post_start + new_max;
515 vim_free(old_start);
516 return OK;
517}
518
519/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200520 * Search between "start" and "end" and try to recognize a
521 * character class in expanded form. For example [0-9].
522 * On success, return the id the character class to be emitted.
523 * On failure, return 0 (=FAIL)
524 * Start points to the first char of the range, while end should point
525 * to the closing brace.
526 */
527 static int
528nfa_recognize_char_class(start, end, extra_newl)
529 char_u *start;
530 char_u *end;
531 int extra_newl;
532{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200533# define CLASS_not 0x80
534# define CLASS_af 0x40
535# define CLASS_AF 0x20
536# define CLASS_az 0x10
537# define CLASS_AZ 0x08
538# define CLASS_o7 0x04
539# define CLASS_o9 0x02
540# define CLASS_underscore 0x01
541
542 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200543 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200544 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200545
546 if (extra_newl == TRUE)
547 newl = TRUE;
548
549 if (*end != ']')
550 return FAIL;
551 p = start;
552 if (*p == '^')
553 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200554 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200555 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200556 }
557
558 while (p < end)
559 {
560 if (p + 2 < end && *(p + 1) == '-')
561 {
562 switch (*p)
563 {
564 case '0':
565 if (*(p + 2) == '9')
566 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200567 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200568 break;
569 }
570 else
571 if (*(p + 2) == '7')
572 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200573 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200574 break;
575 }
576 case 'a':
577 if (*(p + 2) == 'z')
578 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200579 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200580 break;
581 }
582 else
583 if (*(p + 2) == 'f')
584 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200585 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200586 break;
587 }
588 case 'A':
589 if (*(p + 2) == 'Z')
590 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200591 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592 break;
593 }
594 else
595 if (*(p + 2) == 'F')
596 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200597 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598 break;
599 }
600 /* FALLTHROUGH */
601 default:
602 return FAIL;
603 }
604 p += 3;
605 }
606 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
607 {
608 newl = TRUE;
609 p += 2;
610 }
611 else if (*p == '_')
612 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200613 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200614 p ++;
615 }
616 else if (*p == '\n')
617 {
618 newl = TRUE;
619 p ++;
620 }
621 else
622 return FAIL;
623 } /* while (p < end) */
624
625 if (p != end)
626 return FAIL;
627
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200628 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200630
631 switch (config)
632 {
633 case CLASS_o9:
634 return extra_newl + NFA_DIGIT;
635 case CLASS_not | CLASS_o9:
636 return extra_newl + NFA_NDIGIT;
637 case CLASS_af | CLASS_AF | CLASS_o9:
638 return extra_newl + NFA_HEX;
639 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
640 return extra_newl + NFA_NHEX;
641 case CLASS_o7:
642 return extra_newl + NFA_OCTAL;
643 case CLASS_not | CLASS_o7:
644 return extra_newl + NFA_NOCTAL;
645 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
646 return extra_newl + NFA_WORD;
647 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
648 return extra_newl + NFA_NWORD;
649 case CLASS_az | CLASS_AZ | CLASS_underscore:
650 return extra_newl + NFA_HEAD;
651 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
652 return extra_newl + NFA_NHEAD;
653 case CLASS_az | CLASS_AZ:
654 return extra_newl + NFA_ALPHA;
655 case CLASS_not | CLASS_az | CLASS_AZ:
656 return extra_newl + NFA_NALPHA;
657 case CLASS_az:
658 return extra_newl + NFA_LOWER;
659 case CLASS_not | CLASS_az:
660 return extra_newl + NFA_NLOWER;
661 case CLASS_AZ:
662 return extra_newl + NFA_UPPER;
663 case CLASS_not | CLASS_AZ:
664 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200665 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200666 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200667}
668
669/*
670 * Produce the bytes for equivalence class "c".
671 * Currently only handles latin1, latin9 and utf-8.
672 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
673 * equivalent to 'a OR b OR c'
674 *
675 * NOTE! When changing this function, also update reg_equi_class()
676 */
677 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200678nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200681#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200682
683#ifdef FEAT_MBYTE
684 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
685 || STRCMP(p_enc, "iso-8859-15") == 0)
686#endif
687 {
688 switch (c)
689 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200690 case 'A': case 0300: case 0301: case 0302:
691 case 0303: case 0304: case 0305:
692 EMIT2('A'); EMIT2(0300); EMIT2(0301);
693 EMIT2(0302); EMIT2(0303); EMIT2(0304);
694 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695 return OK;
696
Bram Moolenaar417bad22013-06-07 14:08:30 +0200697 case 'C': case 0307:
698 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200699 return OK;
700
Bram Moolenaar417bad22013-06-07 14:08:30 +0200701 case 'E': case 0310: case 0311: case 0312: case 0313:
702 EMIT2('E'); EMIT2(0310); EMIT2(0311);
703 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200704 return OK;
705
Bram Moolenaar417bad22013-06-07 14:08:30 +0200706 case 'I': case 0314: case 0315: case 0316: case 0317:
707 EMIT2('I'); EMIT2(0314); EMIT2(0315);
708 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200709 return OK;
710
Bram Moolenaar417bad22013-06-07 14:08:30 +0200711 case 'N': case 0321:
712 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200713 return OK;
714
Bram Moolenaar417bad22013-06-07 14:08:30 +0200715 case 'O': case 0322: case 0323: case 0324: case 0325:
716 case 0326:
717 EMIT2('O'); EMIT2(0322); EMIT2(0323);
718 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200719 return OK;
720
Bram Moolenaar417bad22013-06-07 14:08:30 +0200721 case 'U': case 0331: case 0332: case 0333: case 0334:
722 EMIT2('U'); EMIT2(0331); EMIT2(0332);
723 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200724 return OK;
725
Bram Moolenaar417bad22013-06-07 14:08:30 +0200726 case 'Y': case 0335:
727 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728 return OK;
729
Bram Moolenaar417bad22013-06-07 14:08:30 +0200730 case 'a': case 0340: case 0341: case 0342:
731 case 0343: case 0344: case 0345:
732 EMIT2('a'); EMIT2(0340); EMIT2(0341);
733 EMIT2(0342); EMIT2(0343); EMIT2(0344);
734 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735 return OK;
736
Bram Moolenaar417bad22013-06-07 14:08:30 +0200737 case 'c': case 0347:
738 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200739 return OK;
740
Bram Moolenaar417bad22013-06-07 14:08:30 +0200741 case 'e': case 0350: case 0351: case 0352: case 0353:
742 EMIT2('e'); EMIT2(0350); EMIT2(0351);
743 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200744 return OK;
745
Bram Moolenaar417bad22013-06-07 14:08:30 +0200746 case 'i': case 0354: case 0355: case 0356: case 0357:
747 EMIT2('i'); EMIT2(0354); EMIT2(0355);
748 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749 return OK;
750
Bram Moolenaar417bad22013-06-07 14:08:30 +0200751 case 'n': case 0361:
752 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200753 return OK;
754
Bram Moolenaar417bad22013-06-07 14:08:30 +0200755 case 'o': case 0362: case 0363: case 0364: case 0365:
756 case 0366:
757 EMIT2('o'); EMIT2(0362); EMIT2(0363);
758 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200759 return OK;
760
Bram Moolenaar417bad22013-06-07 14:08:30 +0200761 case 'u': case 0371: case 0372: case 0373: case 0374:
762 EMIT2('u'); EMIT2(0371); EMIT2(0372);
763 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200764 return OK;
765
Bram Moolenaar417bad22013-06-07 14:08:30 +0200766 case 'y': case 0375: case 0377:
767 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200768 return OK;
769
770 default:
771 return FAIL;
772 }
773 }
774
775 EMIT(c);
776 return OK;
777#undef EMIT2
778}
779
780/*
781 * Code to parse regular expression.
782 *
783 * We try to reuse parsing functions in regexp.c to
784 * minimize surprise and keep the syntax consistent.
785 */
786
787/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 * Parse the lowest level.
789 *
790 * An atom can be one of a long list of items. Many atoms match one character
791 * in the text. It is often an ordinary character or a character class.
792 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
793 * is only for syntax highlighting.
794 *
795 * atom ::= ordinary-atom
796 * or \( pattern \)
797 * or \%( pattern \)
798 * or \z( pattern \)
799 */
800 static int
801nfa_regatom()
802{
803 int c;
804 int charclass;
805 int equiclass;
806 int collclass;
807 int got_coll_char;
808 char_u *p;
809 char_u *endp;
810#ifdef FEAT_MBYTE
811 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812#endif
813 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 int emit_range;
815 int negated;
816 int result;
817 int startc = -1;
818 int endc = -1;
819 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200820
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200822 switch (c)
823 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200824 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200825 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
826
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200827 case Magic('^'):
828 EMIT(NFA_BOL);
829 break;
830
831 case Magic('$'):
832 EMIT(NFA_EOL);
833#if defined(FEAT_SYN_HL) || defined(PROTO)
834 had_eol = TRUE;
835#endif
836 break;
837
838 case Magic('<'):
839 EMIT(NFA_BOW);
840 break;
841
842 case Magic('>'):
843 EMIT(NFA_EOW);
844 break;
845
846 case Magic('_'):
847 c = no_Magic(getchr());
848 if (c == '^') /* "\_^" is start-of-line */
849 {
850 EMIT(NFA_BOL);
851 break;
852 }
853 if (c == '$') /* "\_$" is end-of-line */
854 {
855 EMIT(NFA_EOL);
856#if defined(FEAT_SYN_HL) || defined(PROTO)
857 had_eol = TRUE;
858#endif
859 break;
860 }
861
862 extra = ADD_NL;
863
864 /* "\_[" is collection plus newline */
865 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200866 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867
868 /* "\_x" is character class plus newline */
869 /*FALLTHROUGH*/
870
871 /*
872 * Character classes.
873 */
874 case Magic('.'):
875 case Magic('i'):
876 case Magic('I'):
877 case Magic('k'):
878 case Magic('K'):
879 case Magic('f'):
880 case Magic('F'):
881 case Magic('p'):
882 case Magic('P'):
883 case Magic('s'):
884 case Magic('S'):
885 case Magic('d'):
886 case Magic('D'):
887 case Magic('x'):
888 case Magic('X'):
889 case Magic('o'):
890 case Magic('O'):
891 case Magic('w'):
892 case Magic('W'):
893 case Magic('h'):
894 case Magic('H'):
895 case Magic('a'):
896 case Magic('A'):
897 case Magic('l'):
898 case Magic('L'):
899 case Magic('u'):
900 case Magic('U'):
901 p = vim_strchr(classchars, no_Magic(c));
902 if (p == NULL)
903 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200904 EMSGN("INTERNAL: Unknown character class char: %ld", c);
905 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906 }
907#ifdef FEAT_MBYTE
908 /* When '.' is followed by a composing char ignore the dot, so that
909 * the composing char is matched here. */
910 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
911 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200912 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200913 c = getchr();
914 goto nfa_do_multibyte;
915 }
916#endif
917 EMIT(nfa_classcodes[p - classchars]);
918 if (extra == ADD_NL)
919 {
920 EMIT(NFA_NEWL);
921 EMIT(NFA_OR);
922 regflags |= RF_HASNL;
923 }
924 break;
925
926 case Magic('n'):
927 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200928 /* In a string "\n" matches a newline character. */
929 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200930 else
931 {
932 /* In buffer text "\n" matches the end of a line. */
933 EMIT(NFA_NEWL);
934 regflags |= RF_HASNL;
935 }
936 break;
937
938 case Magic('('):
939 if (nfa_reg(REG_PAREN) == FAIL)
940 return FAIL; /* cascaded error */
941 break;
942
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200943 case Magic('|'):
944 case Magic('&'):
945 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200946 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200947 return FAIL;
948
949 case Magic('='):
950 case Magic('?'):
951 case Magic('+'):
952 case Magic('@'):
953 case Magic('*'):
954 case Magic('{'):
955 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +0200956 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200957 return FAIL;
958
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200959 case Magic('~'):
960 {
961 char_u *lp;
962
963 /* Previous substitute pattern.
964 * Generated as "\%(pattern\)". */
965 if (reg_prev_sub == NULL)
966 {
967 EMSG(_(e_nopresub));
968 return FAIL;
969 }
970 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
971 {
972 EMIT(PTR2CHAR(lp));
973 if (lp != reg_prev_sub)
974 EMIT(NFA_CONCAT);
975 }
976 EMIT(NFA_NOPEN);
977 break;
978 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200979
Bram Moolenaar428e9872013-05-30 17:05:39 +0200980 case Magic('1'):
981 case Magic('2'):
982 case Magic('3'):
983 case Magic('4'):
984 case Magic('5'):
985 case Magic('6'):
986 case Magic('7'):
987 case Magic('8'):
988 case Magic('9'):
989 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
990 nfa_has_backref = TRUE;
991 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200992
993 case Magic('z'):
994 c = no_Magic(getchr());
995 switch (c)
996 {
997 case 's':
998 EMIT(NFA_ZSTART);
999 break;
1000 case 'e':
1001 EMIT(NFA_ZEND);
1002 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001003 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001004#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001005 case '1':
1006 case '2':
1007 case '3':
1008 case '4':
1009 case '5':
1010 case '6':
1011 case '7':
1012 case '8':
1013 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001014 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001015 if (reg_do_extmatch != REX_USE)
1016 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001017 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1018 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001019 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001020 re_has_z = REX_USE;
1021 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001022 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001023 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001024 if (reg_do_extmatch != REX_SET)
1025 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001026 if (nfa_reg(REG_ZPAREN) == FAIL)
1027 return FAIL; /* cascaded error */
1028 re_has_z = REX_SET;
1029 break;
1030#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001031 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001032 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001033 no_Magic(c));
1034 return FAIL;
1035 }
1036 break;
1037
1038 case Magic('%'):
1039 c = no_Magic(getchr());
1040 switch (c)
1041 {
1042 /* () without a back reference */
1043 case '(':
1044 if (nfa_reg(REG_NPAREN) == FAIL)
1045 return FAIL;
1046 EMIT(NFA_NOPEN);
1047 break;
1048
1049 case 'd': /* %d123 decimal */
1050 case 'o': /* %o123 octal */
1051 case 'x': /* %xab hex 2 */
1052 case 'u': /* %uabcd hex 4 */
1053 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001054 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001055 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001056
Bram Moolenaar47196582013-05-25 22:04:23 +02001057 switch (c)
1058 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001059 case 'd': nr = getdecchrs(); break;
1060 case 'o': nr = getoctchrs(); break;
1061 case 'x': nr = gethexchrs(2); break;
1062 case 'u': nr = gethexchrs(4); break;
1063 case 'U': nr = gethexchrs(8); break;
1064 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001065 }
1066
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001067 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001068 EMSG2_RET_FAIL(
1069 _("E678: Invalid character after %s%%[dxouU]"),
1070 reg_magic == MAGIC_ALL);
1071 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001072 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001073 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001074 break;
1075
1076 /* Catch \%^ and \%$ regardless of where they appear in the
1077 * pattern -- regardless of whether or not it makes sense. */
1078 case '^':
1079 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001080 break;
1081
1082 case '$':
1083 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001084 break;
1085
1086 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001087 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001088 break;
1089
1090 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001091 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001092 break;
1093
1094 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001095 {
1096 int n;
1097
1098 /* \%[abc] */
1099 for (n = 0; (c = getchr()) != ']'; ++n)
1100 {
1101 if (c == NUL)
1102 EMSG2_RET_FAIL(_(e_missing_sb),
1103 reg_magic == MAGIC_ALL);
1104 EMIT(c);
1105 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001106 if (n == 0)
1107 EMSG2_RET_FAIL(_(e_empty_sb),
1108 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001109 EMIT(NFA_OPT_CHARS);
1110 EMIT(n);
1111 break;
1112 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001113
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001114 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001115 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001116 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001117 int cmp = c;
1118
1119 if (c == '<' || c == '>')
1120 c = getchr();
1121 while (VIM_ISDIGIT(c))
1122 {
1123 n = n * 10 + (c - '0');
1124 c = getchr();
1125 }
1126 if (c == 'l' || c == 'c' || c == 'v')
1127 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001128 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001129 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001130 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001131 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001132 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001133 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001134 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001135 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001136 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001137 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001138 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001139 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001140 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001141 break;
1142 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001143 else if (c == '\'' && n == 0)
1144 {
1145 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001146 EMIT(cmp == '<' ? NFA_MARK_LT :
1147 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001148 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001149 break;
1150 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001151 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001152 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1153 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001154 return FAIL;
1155 }
1156 break;
1157
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001158 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001159collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001160 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001161 * [abc] uses NFA_START_COLL - NFA_END_COLL
1162 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1163 * Each character is produced as a regular state, using
1164 * NFA_CONCAT to bind them together.
1165 * Besides normal characters there can be:
1166 * - character classes NFA_CLASS_*
1167 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001168 */
1169
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001170 p = regparse;
1171 endp = skip_anyof(p);
1172 if (*endp == ']')
1173 {
1174 /*
1175 * Try to reverse engineer character classes. For example,
1176 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1177 * and perform the necessary substitutions in the NFA.
1178 */
1179 result = nfa_recognize_char_class(regparse, endp,
1180 extra == ADD_NL);
1181 if (result != FAIL)
1182 {
1183 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1184 EMIT(result);
1185 else /* must be char class + newline */
1186 {
1187 EMIT(result - ADD_NL);
1188 EMIT(NFA_NEWL);
1189 EMIT(NFA_OR);
1190 }
1191 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001192 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001193 return OK;
1194 }
1195 /*
1196 * Failed to recognize a character class. Use the simple
1197 * version that turns [abc] into 'a' OR 'b' OR 'c'
1198 */
1199 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001200 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 if (*regparse == '^') /* negated range */
1202 {
1203 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001204 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001205 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001207 else
1208 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001209 if (*regparse == '-')
1210 {
1211 startc = '-';
1212 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001213 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001214 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001215 }
1216 /* Emit the OR branches for each character in the [] */
1217 emit_range = FALSE;
1218 while (regparse < endp)
1219 {
1220 oldstartc = startc;
1221 startc = -1;
1222 got_coll_char = FALSE;
1223 if (*regparse == '[')
1224 {
1225 /* Check for [: :], [= =], [. .] */
1226 equiclass = collclass = 0;
1227 charclass = get_char_class(&regparse);
1228 if (charclass == CLASS_NONE)
1229 {
1230 equiclass = get_equi_class(&regparse);
1231 if (equiclass == 0)
1232 collclass = get_coll_element(&regparse);
1233 }
1234
1235 /* Character class like [:alpha:] */
1236 if (charclass != CLASS_NONE)
1237 {
1238 switch (charclass)
1239 {
1240 case CLASS_ALNUM:
1241 EMIT(NFA_CLASS_ALNUM);
1242 break;
1243 case CLASS_ALPHA:
1244 EMIT(NFA_CLASS_ALPHA);
1245 break;
1246 case CLASS_BLANK:
1247 EMIT(NFA_CLASS_BLANK);
1248 break;
1249 case CLASS_CNTRL:
1250 EMIT(NFA_CLASS_CNTRL);
1251 break;
1252 case CLASS_DIGIT:
1253 EMIT(NFA_CLASS_DIGIT);
1254 break;
1255 case CLASS_GRAPH:
1256 EMIT(NFA_CLASS_GRAPH);
1257 break;
1258 case CLASS_LOWER:
1259 EMIT(NFA_CLASS_LOWER);
1260 break;
1261 case CLASS_PRINT:
1262 EMIT(NFA_CLASS_PRINT);
1263 break;
1264 case CLASS_PUNCT:
1265 EMIT(NFA_CLASS_PUNCT);
1266 break;
1267 case CLASS_SPACE:
1268 EMIT(NFA_CLASS_SPACE);
1269 break;
1270 case CLASS_UPPER:
1271 EMIT(NFA_CLASS_UPPER);
1272 break;
1273 case CLASS_XDIGIT:
1274 EMIT(NFA_CLASS_XDIGIT);
1275 break;
1276 case CLASS_TAB:
1277 EMIT(NFA_CLASS_TAB);
1278 break;
1279 case CLASS_RETURN:
1280 EMIT(NFA_CLASS_RETURN);
1281 break;
1282 case CLASS_BACKSPACE:
1283 EMIT(NFA_CLASS_BACKSPACE);
1284 break;
1285 case CLASS_ESCAPE:
1286 EMIT(NFA_CLASS_ESCAPE);
1287 break;
1288 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001289 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001290 continue;
1291 }
1292 /* Try equivalence class [=a=] and the like */
1293 if (equiclass != 0)
1294 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001295 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001296 if (result == FAIL)
1297 {
1298 /* should never happen */
1299 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1300 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001301 continue;
1302 }
1303 /* Try collating class like [. .] */
1304 if (collclass != 0)
1305 {
1306 startc = collclass; /* allow [.a.]-x as a range */
1307 /* Will emit the proper atom at the end of the
1308 * while loop. */
1309 }
1310 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001311 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1312 * start character. */
1313 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001314 {
1315 emit_range = TRUE;
1316 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001317 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 continue; /* reading the end of the range */
1319 }
1320
1321 /* Now handle simple and escaped characters.
1322 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1323 * accepts "\t", "\e", etc., but only when the 'l' flag in
1324 * 'cpoptions' is not included.
1325 * Posix doesn't recognize backslash at all.
1326 */
1327 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001328 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001329 && regparse + 1 <= endp
1330 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001331 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001332 && vim_strchr(REGEXP_ABBR, regparse[1])
1333 != NULL)
1334 )
1335 )
1336 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001337 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001338
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001339 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001340 startc = reg_string ? NL : NFA_NEWL;
1341 else
1342 if (*regparse == 'd'
1343 || *regparse == 'o'
1344 || *regparse == 'x'
1345 || *regparse == 'u'
1346 || *regparse == 'U'
1347 )
1348 {
1349 /* TODO(RE) This needs more testing */
1350 startc = coll_get_char();
1351 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001352 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001353 }
1354 else
1355 {
1356 /* \r,\t,\e,\b */
1357 startc = backslash_trans(*regparse);
1358 }
1359 }
1360
1361 /* Normal printable char */
1362 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001363 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364
1365 /* Previous char was '-', so this char is end of range. */
1366 if (emit_range)
1367 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001368 endc = startc;
1369 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001370 if (startc > endc)
1371 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001372
1373 if (endc > startc + 2)
1374 {
1375 /* Emit a range instead of the sequence of
1376 * individual characters. */
1377 if (startc == 0)
1378 /* \x00 is translated to \x0a, start at \x01. */
1379 EMIT(1);
1380 else
1381 --post_ptr; /* remove NFA_CONCAT */
1382 EMIT(endc);
1383 EMIT(NFA_RANGE);
1384 EMIT(NFA_CONCAT);
1385 }
1386 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001387#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001388 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001389 || (*mb_char2len)(endc) > 1))
1390 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001391 /* Emit the characters in the range.
1392 * "startc" was already emitted, so skip it.
1393 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001394 for (c = startc + 1; c <= endc; c++)
1395 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001396 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001397 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001398 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399 }
1400 else
1401#endif
1402 {
1403#ifdef EBCDIC
1404 int alpha_only = FALSE;
1405
1406 /* for alphabetical range skip the gaps
1407 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1408 if (isalpha(startc) && isalpha(endc))
1409 alpha_only = TRUE;
1410#endif
1411 /* Emit the range. "startc" was already emitted, so
1412 * skip it. */
1413 for (c = startc + 1; c <= endc; c++)
1414#ifdef EBCDIC
1415 if (!alpha_only || isalpha(startc))
1416#endif
1417 {
1418 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001419 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001420 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001421 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001422 emit_range = FALSE;
1423 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001424 }
1425 else
1426 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001427 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001428 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001429 * Normally, simply emit startc. But if we get char
1430 * code=0 from a collating char, then replace it with
1431 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001432 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001433 * the backtracking engine. */
1434 if (startc == NFA_NEWL)
1435 {
1436 /* Line break can't be matched as part of the
1437 * collection, add an OR below. But not for negated
1438 * range. */
1439 if (!negated)
1440 extra = ADD_NL;
1441 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001443 {
1444 if (got_coll_char == TRUE && startc == 0)
1445 EMIT(0x0a);
1446 else
1447 EMIT(startc);
1448 EMIT(NFA_CONCAT);
1449 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 }
1451
Bram Moolenaar51a29832013-05-28 22:30:35 +02001452 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001453 } /* while (p < endp) */
1454
Bram Moolenaar51a29832013-05-28 22:30:35 +02001455 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001456 if (*regparse == '-') /* if last, '-' is just a char */
1457 {
1458 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001459 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001460 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001461 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001463 /* skip the trailing ] */
1464 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001465 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001466
1467 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001468 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001469 EMIT(NFA_END_NEG_COLL);
1470 else
1471 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001472
1473 /* \_[] also matches \n but it's not negated */
1474 if (extra == ADD_NL)
1475 {
1476 EMIT(reg_string ? NL : NFA_NEWL);
1477 EMIT(NFA_OR);
1478 }
1479
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001480 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001481 } /* if exists closing ] */
1482
1483 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001484 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001485 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 default:
1488 {
1489#ifdef FEAT_MBYTE
1490 int plen;
1491
1492nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001493 /* plen is length of current char with composing chars */
1494 if (enc_utf8 && ((*mb_char2len)(c)
1495 != (plen = (*mb_ptr2len)(old_regparse))
1496 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001498 int i = 0;
1499
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001500 /* A base character plus composing characters, or just one
1501 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001502 * This requires creating a separate atom as if enclosing
1503 * the characters in (), where NFA_COMPOSING is the ( and
1504 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001505 * building the postfix form, not the NFA itself;
1506 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001507 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001508 for (;;)
1509 {
1510 EMIT(c);
1511 if (i > 0)
1512 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001513 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001514 break;
1515 c = utf_ptr2char(old_regparse + i);
1516 }
1517 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 regparse = old_regparse + plen;
1519 }
1520 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521#endif
1522 {
1523 c = no_Magic(c);
1524 EMIT(c);
1525 }
1526 return OK;
1527 }
1528 }
1529
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 return OK;
1531}
1532
1533/*
1534 * Parse something followed by possible [*+=].
1535 *
1536 * A piece is an atom, possibly followed by a multi, an indication of how many
1537 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1538 * characters: "", "a", "aa", etc.
1539 *
1540 * piece ::= atom
1541 * or atom multi
1542 */
1543 static int
1544nfa_regpiece()
1545{
1546 int i;
1547 int op;
1548 int ret;
1549 long minval, maxval;
1550 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001551 parse_state_T old_state;
1552 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001554 int old_post_pos;
1555 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001556 int quest;
1557
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001558 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1559 * next. */
1560 save_parse_state(&old_state);
1561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001562 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001563 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564
1565 ret = nfa_regatom();
1566 if (ret == FAIL)
1567 return FAIL; /* cascaded error */
1568
1569 op = peekchr();
1570 if (re_multi_type(op) == NOT_MULTI)
1571 return OK;
1572
1573 skipchr();
1574 switch (op)
1575 {
1576 case Magic('*'):
1577 EMIT(NFA_STAR);
1578 break;
1579
1580 case Magic('+'):
1581 /*
1582 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1583 * first and only submatch would be "aaa". But the backtracking
1584 * engine interprets the plus as "try matching one more time", and
1585 * a* matches a second time at the end of the input, the empty
1586 * string.
1587 * The submatch will the empty string.
1588 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001589 * In order to be consistent with the old engine, we replace
1590 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001591 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001592 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 curchr = -1;
1594 if (nfa_regatom() == FAIL)
1595 return FAIL;
1596 EMIT(NFA_STAR);
1597 EMIT(NFA_CONCAT);
1598 skipchr(); /* skip the \+ */
1599 break;
1600
1601 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001602 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001603 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001604 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001605 switch(op)
1606 {
1607 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001608 /* \@= */
1609 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001611 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001612 /* \@! */
1613 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001614 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001615 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001616 op = no_Magic(getchr());
1617 if (op == '=')
1618 /* \@<= */
1619 i = NFA_PREV_ATOM_JUST_BEFORE;
1620 else if (op == '!')
1621 /* \@<! */
1622 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1623 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001624 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001625 /* \@> */
1626 i = NFA_PREV_ATOM_LIKE_PATTERN;
1627 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001629 if (i == 0)
1630 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001631 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1632 return FAIL;
1633 }
1634 EMIT(i);
1635 if (i == NFA_PREV_ATOM_JUST_BEFORE
1636 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1637 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001638 break;
1639
1640 case Magic('?'):
1641 case Magic('='):
1642 EMIT(NFA_QUEST);
1643 break;
1644
1645 case Magic('{'):
1646 /* a{2,5} will expand to 'aaa?a?a?'
1647 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1648 * version of '?'
1649 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1650 * parenthesis have the same id
1651 */
1652
1653 greedy = TRUE;
1654 c2 = peekchr();
1655 if (c2 == '-' || c2 == Magic('-'))
1656 {
1657 skipchr();
1658 greedy = FALSE;
1659 }
1660 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001661 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001662
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1664 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001665 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001667 if (greedy)
1668 /* \{}, \{0,} */
1669 EMIT(NFA_STAR);
1670 else
1671 /* \{-}, \{-0,} */
1672 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001673 break;
1674 }
1675
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 /* Special case: x{0} or x{-0} */
1677 if (maxval == 0)
1678 {
1679 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001680 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1682 EMIT(NFA_SKIP_CHAR);
1683 return OK;
1684 }
1685
1686 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001687 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001688 /* Save parse state after the repeated atom and the \{} */
1689 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001690
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1692 for (i = 0; i < maxval; i++)
1693 {
1694 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001695 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001696 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001697 if (nfa_regatom() == FAIL)
1698 return FAIL;
1699 /* after "minval" times, atoms are optional */
1700 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001701 {
1702 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001703 {
1704 if (greedy)
1705 EMIT(NFA_STAR);
1706 else
1707 EMIT(NFA_STAR_NONGREEDY);
1708 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001709 else
1710 EMIT(quest);
1711 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001712 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001714 if (i + 1 > minval && maxval == MAX_LIMIT)
1715 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001716 }
1717
1718 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001719 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720 curchr = -1;
1721
1722 break;
1723
1724
1725 default:
1726 break;
1727 } /* end switch */
1728
1729 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001730 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001731 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001732
1733 return OK;
1734}
1735
1736/*
1737 * Parse one or more pieces, concatenated. It matches a match for the
1738 * first piece, followed by a match for the second piece, etc. Example:
1739 * "f[0-9]b", first matches "f", then a digit and then "b".
1740 *
1741 * concat ::= piece
1742 * or piece piece
1743 * or piece piece piece
1744 * etc.
1745 */
1746 static int
1747nfa_regconcat()
1748{
1749 int cont = TRUE;
1750 int first = TRUE;
1751
1752 while (cont)
1753 {
1754 switch (peekchr())
1755 {
1756 case NUL:
1757 case Magic('|'):
1758 case Magic('&'):
1759 case Magic(')'):
1760 cont = FALSE;
1761 break;
1762
1763 case Magic('Z'):
1764#ifdef FEAT_MBYTE
1765 regflags |= RF_ICOMBINE;
1766#endif
1767 skipchr_keepstart();
1768 break;
1769 case Magic('c'):
1770 regflags |= RF_ICASE;
1771 skipchr_keepstart();
1772 break;
1773 case Magic('C'):
1774 regflags |= RF_NOICASE;
1775 skipchr_keepstart();
1776 break;
1777 case Magic('v'):
1778 reg_magic = MAGIC_ALL;
1779 skipchr_keepstart();
1780 curchr = -1;
1781 break;
1782 case Magic('m'):
1783 reg_magic = MAGIC_ON;
1784 skipchr_keepstart();
1785 curchr = -1;
1786 break;
1787 case Magic('M'):
1788 reg_magic = MAGIC_OFF;
1789 skipchr_keepstart();
1790 curchr = -1;
1791 break;
1792 case Magic('V'):
1793 reg_magic = MAGIC_NONE;
1794 skipchr_keepstart();
1795 curchr = -1;
1796 break;
1797
1798 default:
1799 if (nfa_regpiece() == FAIL)
1800 return FAIL;
1801 if (first == FALSE)
1802 EMIT(NFA_CONCAT);
1803 else
1804 first = FALSE;
1805 break;
1806 }
1807 }
1808
1809 return OK;
1810}
1811
1812/*
1813 * Parse a branch, one or more concats, separated by "\&". It matches the
1814 * last concat, but only if all the preceding concats also match at the same
1815 * position. Examples:
1816 * "foobeep\&..." matches "foo" in "foobeep".
1817 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1818 *
1819 * branch ::= concat
1820 * or concat \& concat
1821 * or concat \& concat \& concat
1822 * etc.
1823 */
1824 static int
1825nfa_regbranch()
1826{
1827 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001828 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829
Bram Moolenaar16299b52013-05-30 18:45:23 +02001830 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831
1832 /* First branch, possibly the only one */
1833 if (nfa_regconcat() == FAIL)
1834 return FAIL;
1835
1836 ch = peekchr();
1837 /* Try next concats */
1838 while (ch == Magic('&'))
1839 {
1840 skipchr();
1841 EMIT(NFA_NOPEN);
1842 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001843 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 if (nfa_regconcat() == FAIL)
1845 return FAIL;
1846 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001847 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001848 EMIT(NFA_SKIP_CHAR);
1849 EMIT(NFA_CONCAT);
1850 ch = peekchr();
1851 }
1852
1853 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001854 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 EMIT(NFA_SKIP_CHAR);
1856
1857 return OK;
1858}
1859
1860/*
1861 * Parse a pattern, one or more branches, separated by "\|". It matches
1862 * anything that matches one of the branches. Example: "foo\|beep" matches
1863 * "foo" and matches "beep". If more than one branch matches, the first one
1864 * is used.
1865 *
1866 * pattern ::= branch
1867 * or branch \| branch
1868 * or branch \| branch \| branch
1869 * etc.
1870 */
1871 static int
1872nfa_reg(paren)
1873 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1874{
1875 int parno = 0;
1876
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 if (paren == REG_PAREN)
1878 {
1879 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881 parno = regnpar++;
1882 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001883#ifdef FEAT_SYN_HL
1884 else if (paren == REG_ZPAREN)
1885 {
1886 /* Make a ZOPEN node. */
1887 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001888 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001889 parno = regnzpar++;
1890 }
1891#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001892
1893 if (nfa_regbranch() == FAIL)
1894 return FAIL; /* cascaded error */
1895
1896 while (peekchr() == Magic('|'))
1897 {
1898 skipchr();
1899 if (nfa_regbranch() == FAIL)
1900 return FAIL; /* cascaded error */
1901 EMIT(NFA_OR);
1902 }
1903
1904 /* Check for proper termination. */
1905 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1906 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 if (paren == REG_NPAREN)
1908 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1909 else
1910 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1911 }
1912 else if (paren == REG_NOPAREN && peekchr() != NUL)
1913 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914 if (peekchr() == Magic(')'))
1915 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1916 else
1917 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1918 }
1919 /*
1920 * Here we set the flag allowing back references to this set of
1921 * parentheses.
1922 */
1923 if (paren == REG_PAREN)
1924 {
1925 had_endbrace[parno] = TRUE; /* have seen the close paren */
1926 EMIT(NFA_MOPEN + parno);
1927 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001928#ifdef FEAT_SYN_HL
1929 else if (paren == REG_ZPAREN)
1930 EMIT(NFA_ZOPEN + parno);
1931#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001932
1933 return OK;
1934}
1935
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936#ifdef DEBUG
1937static char_u code[50];
1938
1939 static void
1940nfa_set_code(c)
1941 int c;
1942{
1943 int addnl = FALSE;
1944
1945 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1946 {
1947 addnl = TRUE;
1948 c -= ADD_NL;
1949 }
1950
1951 STRCPY(code, "");
1952 switch (c)
1953 {
1954 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1955 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1956 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1957 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1958 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1959 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1960
Bram Moolenaar5714b802013-05-28 22:03:20 +02001961 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1962 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1963 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1964 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1965 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1966 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1967 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1968 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1969 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001970#ifdef FEAT_SYN_HL
1971 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1972 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1973 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1974 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1975 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1976 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1977 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1978 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1979 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1980#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001981 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1982
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983 case NFA_PREV_ATOM_NO_WIDTH:
1984 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001985 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1986 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001987 case NFA_PREV_ATOM_JUST_BEFORE:
1988 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1989 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1990 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001991 case NFA_PREV_ATOM_LIKE_PATTERN:
1992 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
1993
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001994 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1995 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001996 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02001997 case NFA_START_INVISIBLE_NEG:
1998 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001999 case NFA_START_INVISIBLE_BEFORE:
2000 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002001 case NFA_START_INVISIBLE_BEFORE_NEG:
2002 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002003 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002004 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002005 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002006 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2009 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002010 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002012 case NFA_MOPEN:
2013 case NFA_MOPEN1:
2014 case NFA_MOPEN2:
2015 case NFA_MOPEN3:
2016 case NFA_MOPEN4:
2017 case NFA_MOPEN5:
2018 case NFA_MOPEN6:
2019 case NFA_MOPEN7:
2020 case NFA_MOPEN8:
2021 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002022 STRCPY(code, "NFA_MOPEN(x)");
2023 code[10] = c - NFA_MOPEN + '0';
2024 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002025 case NFA_MCLOSE:
2026 case NFA_MCLOSE1:
2027 case NFA_MCLOSE2:
2028 case NFA_MCLOSE3:
2029 case NFA_MCLOSE4:
2030 case NFA_MCLOSE5:
2031 case NFA_MCLOSE6:
2032 case NFA_MCLOSE7:
2033 case NFA_MCLOSE8:
2034 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035 STRCPY(code, "NFA_MCLOSE(x)");
2036 code[11] = c - NFA_MCLOSE + '0';
2037 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002038#ifdef FEAT_SYN_HL
2039 case NFA_ZOPEN:
2040 case NFA_ZOPEN1:
2041 case NFA_ZOPEN2:
2042 case NFA_ZOPEN3:
2043 case NFA_ZOPEN4:
2044 case NFA_ZOPEN5:
2045 case NFA_ZOPEN6:
2046 case NFA_ZOPEN7:
2047 case NFA_ZOPEN8:
2048 case NFA_ZOPEN9:
2049 STRCPY(code, "NFA_ZOPEN(x)");
2050 code[10] = c - NFA_ZOPEN + '0';
2051 break;
2052 case NFA_ZCLOSE:
2053 case NFA_ZCLOSE1:
2054 case NFA_ZCLOSE2:
2055 case NFA_ZCLOSE3:
2056 case NFA_ZCLOSE4:
2057 case NFA_ZCLOSE5:
2058 case NFA_ZCLOSE6:
2059 case NFA_ZCLOSE7:
2060 case NFA_ZCLOSE8:
2061 case NFA_ZCLOSE9:
2062 STRCPY(code, "NFA_ZCLOSE(x)");
2063 code[11] = c - NFA_ZCLOSE + '0';
2064 break;
2065#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002066 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2067 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2068 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2069 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002070 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2071 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002072 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2073 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2074 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2075 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2076 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2077 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2078 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2079 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2080 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2081 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2082 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2083 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2084 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2085 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2086
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002088 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2089 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2090 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002091 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2092 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002093
2094 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2095 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2096 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2097 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2098 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2099 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2100 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2101
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002102 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2103 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2104 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2105 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2106 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2107 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2108 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2109 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2110 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2111 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2112 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2113 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2114 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2115 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2116 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2117 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2118
2119 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2120 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2121 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2122 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2123 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2124 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2125 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2126 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2127 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2128 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2129 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2130 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2131 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2132 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2133 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2134 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2135 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2136 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2137 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2138 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2139 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2140 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2141 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2142 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2143 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2144 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2145 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2146
2147 default:
2148 STRCPY(code, "CHAR(x)");
2149 code[5] = c;
2150 }
2151
2152 if (addnl == TRUE)
2153 STRCAT(code, " + NEWLINE ");
2154
2155}
2156
2157#ifdef ENABLE_LOG
2158static FILE *log_fd;
2159
2160/*
2161 * Print the postfix notation of the current regexp.
2162 */
2163 static void
2164nfa_postfix_dump(expr, retval)
2165 char_u *expr;
2166 int retval;
2167{
2168 int *p;
2169 FILE *f;
2170
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002171 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002172 if (f != NULL)
2173 {
2174 fprintf(f, "\n-------------------------\n");
2175 if (retval == FAIL)
2176 fprintf(f, ">>> NFA engine failed ... \n");
2177 else if (retval == OK)
2178 fprintf(f, ">>> NFA engine succeeded !\n");
2179 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002180 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181 {
2182 nfa_set_code(*p);
2183 fprintf(f, "%s, ", code);
2184 }
2185 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002186 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187 fprintf(f, "%d ", *p);
2188 fprintf(f, "\n\n");
2189 fclose(f);
2190 }
2191}
2192
2193/*
2194 * Print the NFA starting with a root node "state".
2195 */
2196 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002197nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198 FILE *debugf;
2199 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002200{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002201 garray_T indent;
2202
2203 ga_init2(&indent, 1, 64);
2204 ga_append(&indent, '\0');
2205 nfa_print_state2(debugf, state, &indent);
2206 ga_clear(&indent);
2207}
2208
2209 static void
2210nfa_print_state2(debugf, state, indent)
2211 FILE *debugf;
2212 nfa_state_T *state;
2213 garray_T *indent;
2214{
2215 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002216
2217 if (state == NULL)
2218 return;
2219
2220 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002221
2222 /* Output indent */
2223 p = (char_u *)indent->ga_data;
2224 if (indent->ga_len >= 3)
2225 {
2226 int last = indent->ga_len - 3;
2227 char_u save[2];
2228
2229 STRNCPY(save, &p[last], 2);
2230 STRNCPY(&p[last], "+-", 2);
2231 fprintf(debugf, " %s", p);
2232 STRNCPY(&p[last], save, 2);
2233 }
2234 else
2235 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236
2237 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002238 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002239 code,
2240 state->c,
2241 abs(state->id),
2242 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 if (state->id < 0)
2244 return;
2245
2246 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002247
2248 /* grow indent for state->out */
2249 indent->ga_len -= 1;
2250 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002251 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002252 else
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->out, indent);
2257
2258 /* replace last part of indent for state->out1 */
2259 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002260 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002261 ga_append(indent, '\0');
2262
2263 nfa_print_state2(debugf, state->out1, indent);
2264
2265 /* shrink indent */
2266 indent->ga_len -= 3;
2267 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002268}
2269
2270/*
2271 * Print the NFA state machine.
2272 */
2273 static void
2274nfa_dump(prog)
2275 nfa_regprog_T *prog;
2276{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002277 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002278
2279 if (debugf != NULL)
2280 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002281 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002282
2283 fprintf(debugf, "reganch: %d\n", prog->reganch);
2284 fprintf(debugf, "regstart: %d\n", prog->regstart);
2285
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002286 fclose(debugf);
2287 }
2288}
2289#endif /* ENABLE_LOG */
2290#endif /* DEBUG */
2291
2292/*
2293 * Parse r.e. @expr and convert it into postfix form.
2294 * Return the postfix string on success, NULL otherwise.
2295 */
2296 static int *
2297re2post()
2298{
2299 if (nfa_reg(REG_NOPAREN) == FAIL)
2300 return NULL;
2301 EMIT(NFA_MOPEN);
2302 return post_start;
2303}
2304
2305/* NB. Some of the code below is inspired by Russ's. */
2306
2307/*
2308 * Represents an NFA state plus zero or one or two arrows exiting.
2309 * if c == MATCH, no arrows out; matching state.
2310 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2311 * If c < 256, labeled arrow with character c to out.
2312 */
2313
2314static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2315
2316/*
2317 * Allocate and initialize nfa_state_T.
2318 */
2319 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002320alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002321 int c;
2322 nfa_state_T *out;
2323 nfa_state_T *out1;
2324{
2325 nfa_state_T *s;
2326
2327 if (istate >= nstate)
2328 return NULL;
2329
2330 s = &state_ptr[istate++];
2331
2332 s->c = c;
2333 s->out = out;
2334 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002335 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002336
2337 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002338 s->lastlist[0] = 0;
2339 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340
2341 return s;
2342}
2343
2344/*
2345 * A partially built NFA without the matching state filled in.
2346 * Frag_T.start points at the start state.
2347 * Frag_T.out is a list of places that need to be set to the
2348 * next state for this fragment.
2349 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002350
2351/* Since the out pointers in the list are always
2352 * uninitialized, we use the pointers themselves
2353 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002355union Ptrlist
2356{
2357 Ptrlist *next;
2358 nfa_state_T *s;
2359};
2360
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002361struct Frag
2362{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002363 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364 Ptrlist *out;
2365};
2366typedef struct Frag Frag_T;
2367
2368static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2369static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2370static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2371static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2372static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2373static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2374
2375/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002376 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002377 */
2378 static Frag_T
2379frag(start, out)
2380 nfa_state_T *start;
2381 Ptrlist *out;
2382{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002383 Frag_T n;
2384
2385 n.start = start;
2386 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387 return n;
2388}
2389
2390/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002391 * Create singleton list containing just outp.
2392 */
2393 static Ptrlist *
2394list1(outp)
2395 nfa_state_T **outp;
2396{
2397 Ptrlist *l;
2398
2399 l = (Ptrlist *)outp;
2400 l->next = NULL;
2401 return l;
2402}
2403
2404/*
2405 * Patch the list of states at out to point to start.
2406 */
2407 static void
2408patch(l, s)
2409 Ptrlist *l;
2410 nfa_state_T *s;
2411{
2412 Ptrlist *next;
2413
2414 for (; l; l = next)
2415 {
2416 next = l->next;
2417 l->s = s;
2418 }
2419}
2420
2421
2422/*
2423 * Join the two lists l1 and l2, returning the combination.
2424 */
2425 static Ptrlist *
2426append(l1, l2)
2427 Ptrlist *l1;
2428 Ptrlist *l2;
2429{
2430 Ptrlist *oldl1;
2431
2432 oldl1 = l1;
2433 while (l1->next)
2434 l1 = l1->next;
2435 l1->next = l2;
2436 return oldl1;
2437}
2438
2439/*
2440 * Stack used for transforming postfix form into NFA.
2441 */
2442static Frag_T empty;
2443
2444 static void
2445st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002446 int *postfix UNUSED;
2447 int *end UNUSED;
2448 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002449{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002450#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002451 FILE *df;
2452 int *p2;
2453
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002454 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002455 if (df)
2456 {
2457 fprintf(df, "Error popping the stack!\n");
2458#ifdef DEBUG
2459 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2460#endif
2461 fprintf(df, "Postfix form is: ");
2462#ifdef DEBUG
2463 for (p2 = postfix; p2 < end; p2++)
2464 {
2465 nfa_set_code(*p2);
2466 fprintf(df, "%s, ", code);
2467 }
2468 nfa_set_code(*p);
2469 fprintf(df, "\nCurrent position is: ");
2470 for (p2 = postfix; p2 <= p; p2 ++)
2471 {
2472 nfa_set_code(*p2);
2473 fprintf(df, "%s, ", code);
2474 }
2475#else
2476 for (p2 = postfix; p2 < end; p2++)
2477 {
2478 fprintf(df, "%d, ", *p2);
2479 }
2480 fprintf(df, "\nCurrent position is: ");
2481 for (p2 = postfix; p2 <= p; p2 ++)
2482 {
2483 fprintf(df, "%d, ", *p2);
2484 }
2485#endif
2486 fprintf(df, "\n--------------------------\n");
2487 fclose(df);
2488 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002489#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002490 EMSG(_("E874: (NFA) Could not pop the stack !"));
2491}
2492
2493/*
2494 * Push an item onto the stack.
2495 */
2496 static void
2497st_push(s, p, stack_end)
2498 Frag_T s;
2499 Frag_T **p;
2500 Frag_T *stack_end;
2501{
2502 Frag_T *stackp = *p;
2503
2504 if (stackp >= stack_end)
2505 return;
2506 *stackp = s;
2507 *p = *p + 1;
2508}
2509
2510/*
2511 * Pop an item from the stack.
2512 */
2513 static Frag_T
2514st_pop(p, stack)
2515 Frag_T **p;
2516 Frag_T *stack;
2517{
2518 Frag_T *stackp;
2519
2520 *p = *p - 1;
2521 stackp = *p;
2522 if (stackp < stack)
2523 return empty;
2524 return **p;
2525}
2526
2527/*
2528 * Convert a postfix form into its equivalent NFA.
2529 * Return the NFA start state on success, NULL otherwise.
2530 */
2531 static nfa_state_T *
2532post2nfa(postfix, end, nfa_calc_size)
2533 int *postfix;
2534 int *end;
2535 int nfa_calc_size;
2536{
2537 int *p;
2538 int mopen;
2539 int mclose;
2540 Frag_T *stack = NULL;
2541 Frag_T *stackp = NULL;
2542 Frag_T *stack_end = NULL;
2543 Frag_T e1;
2544 Frag_T e2;
2545 Frag_T e;
2546 nfa_state_T *s;
2547 nfa_state_T *s1;
2548 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002549 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550
2551 if (postfix == NULL)
2552 return NULL;
2553
Bram Moolenaar053bb602013-05-20 13:55:21 +02002554#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002555#define POP() st_pop(&stackp, stack); \
2556 if (stackp < stack) \
2557 { \
2558 st_error(postfix, end, p); \
2559 return NULL; \
2560 }
2561
2562 if (nfa_calc_size == FALSE)
2563 {
2564 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002565 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002566 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002567 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002568 }
2569
2570 for (p = postfix; p < end; ++p)
2571 {
2572 switch (*p)
2573 {
2574 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002575 /* Concatenation.
2576 * Pay attention: this operator does not exist in the r.e. itself
2577 * (it is implicit, really). It is added when r.e. is translated
2578 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002579 if (nfa_calc_size == TRUE)
2580 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002581 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002582 break;
2583 }
2584 e2 = POP();
2585 e1 = POP();
2586 patch(e1.out, e2.start);
2587 PUSH(frag(e1.start, e2.out));
2588 break;
2589
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002590 case NFA_OR:
2591 /* Alternation */
2592 if (nfa_calc_size == TRUE)
2593 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002594 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002595 break;
2596 }
2597 e2 = POP();
2598 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002599 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002600 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002601 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002602 PUSH(frag(s, append(e1.out, e2.out)));
2603 break;
2604
2605 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002606 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002607 if (nfa_calc_size == TRUE)
2608 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002609 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002610 break;
2611 }
2612 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002613 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002615 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616 patch(e.out, s);
2617 PUSH(frag(s, list1(&s->out1)));
2618 break;
2619
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002620 case NFA_STAR_NONGREEDY:
2621 /* Zero or more, prefer zero */
2622 if (nfa_calc_size == TRUE)
2623 {
2624 nstate++;
2625 break;
2626 }
2627 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002628 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002629 if (s == NULL)
2630 goto theend;
2631 patch(e.out, s);
2632 PUSH(frag(s, list1(&s->out)));
2633 break;
2634
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635 case NFA_QUEST:
2636 /* one or zero atoms=> greedy match */
2637 if (nfa_calc_size == TRUE)
2638 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002639 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002640 break;
2641 }
2642 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002643 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002644 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002645 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002646 PUSH(frag(s, append(e.out, list1(&s->out1))));
2647 break;
2648
2649 case NFA_QUEST_NONGREEDY:
2650 /* zero or one atoms => non-greedy match */
2651 if (nfa_calc_size == TRUE)
2652 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002653 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654 break;
2655 }
2656 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002657 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002659 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002660 PUSH(frag(s, append(e.out, list1(&s->out))));
2661 break;
2662
Bram Moolenaar417bad22013-06-07 14:08:30 +02002663 case NFA_END_COLL:
2664 case NFA_END_NEG_COLL:
2665 /* On the stack is the sequence starting with NFA_START_COLL or
2666 * NFA_START_NEG_COLL and all possible characters. Patch it to
2667 * add the output to the start. */
2668 if (nfa_calc_size == TRUE)
2669 {
2670 nstate++;
2671 break;
2672 }
2673 e = POP();
2674 s = alloc_state(NFA_END_COLL, NULL, NULL);
2675 if (s == NULL)
2676 goto theend;
2677 patch(e.out, s);
2678 e.start->out1 = s;
2679 PUSH(frag(e.start, list1(&s->out)));
2680 break;
2681
2682 case NFA_RANGE:
2683 /* Before this are two characters, the low and high end of a
2684 * range. Turn them into two states with MIN and MAX. */
2685 if (nfa_calc_size == TRUE)
2686 {
2687 /* nstate += 0; */
2688 break;
2689 }
2690 e2 = POP();
2691 e1 = POP();
2692 e2.start->val = e2.start->c;
2693 e2.start->c = NFA_RANGE_MAX;
2694 e1.start->val = e1.start->c;
2695 e1.start->c = NFA_RANGE_MIN;
2696 patch(e1.out, e2.start);
2697 PUSH(frag(e1.start, e2.out));
2698 break;
2699
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002700 case NFA_SKIP_CHAR:
2701 /* Symbol of 0-length, Used in a repetition
2702 * with max/min count of 0 */
2703 if (nfa_calc_size == TRUE)
2704 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002705 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002706 break;
2707 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002708 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002709 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002710 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002711 PUSH(frag(s, list1(&s->out)));
2712 break;
2713
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002714 case NFA_OPT_CHARS:
2715 {
2716 int n;
2717
2718 /* \%[abc] */
2719 n = *++p; /* get number of characters */
2720 if (nfa_calc_size == TRUE)
2721 {
2722 nstate += n;
2723 break;
2724 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002725 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002726 e1.out = NULL; /* stores list with out1's */
2727 s1 = NULL; /* previous NFA_SPLIT to connect to */
2728 while (n-- > 0)
2729 {
2730 e = POP(); /* get character */
2731 s = alloc_state(NFA_SPLIT, e.start, NULL);
2732 if (s == NULL)
2733 goto theend;
2734 if (e1.out == NULL)
2735 e1 = e;
2736 patch(e.out, s1);
2737 append(e1.out, list1(&s->out1));
2738 s1 = s;
2739 }
2740 PUSH(frag(s, e1.out));
2741 break;
2742 }
2743
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002745 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002746 case NFA_PREV_ATOM_JUST_BEFORE:
2747 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002748 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002749 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002750 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2751 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002752 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002753 int start_state;
2754 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02002755 int n = 0;
2756 nfa_state_T *zend;
2757 nfa_state_T *skip;
2758
Bram Moolenaardecd9542013-06-07 16:31:50 +02002759 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02002760 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02002761 case NFA_PREV_ATOM_NO_WIDTH:
2762 start_state = NFA_START_INVISIBLE;
2763 end_state = NFA_END_INVISIBLE;
2764 break;
2765 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2766 start_state = NFA_START_INVISIBLE_NEG;
2767 end_state = NFA_END_INVISIBLE_NEG;
2768 break;
2769 case NFA_PREV_ATOM_JUST_BEFORE:
2770 start_state = NFA_START_INVISIBLE_BEFORE;
2771 end_state = NFA_END_INVISIBLE;
2772 break;
2773 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2774 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
2775 end_state = NFA_END_INVISIBLE_NEG;
2776 break;
2777 case NFA_PREV_ATOM_LIKE_PATTERN:
2778 start_state = NFA_START_PATTERN;
2779 end_state = NFA_END_PATTERN;
2780 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002781 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002782
2783 if (before)
2784 n = *++p; /* get the count */
2785
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002786 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002787 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002788 * The \@<= operator: match for the preceding atom.
2789 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002790 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002791 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002792
2793 if (nfa_calc_size == TRUE)
2794 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002795 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002796 break;
2797 }
2798 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002799 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002801 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002802
Bram Moolenaar87953742013-06-05 18:52:40 +02002803 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002804 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002805 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002806 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002807 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002808 if (pattern)
2809 {
2810 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2811 skip = alloc_state(NFA_SKIP, NULL, NULL);
2812 zend = alloc_state(NFA_ZEND, s1, NULL);
2813 s1->out= skip;
2814 patch(e.out, zend);
2815 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002816 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002817 else
2818 {
2819 patch(e.out, s1);
2820 PUSH(frag(s, list1(&s1->out)));
2821 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002823 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002825#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002826 case NFA_COMPOSING: /* char with composing char */
2827#if 0
2828 /* TODO */
2829 if (regflags & RF_ICOMBINE)
2830 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002831 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002832 }
2833#endif
2834 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002835#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002836
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002837 case NFA_MOPEN: /* \( \) Submatch */
2838 case NFA_MOPEN1:
2839 case NFA_MOPEN2:
2840 case NFA_MOPEN3:
2841 case NFA_MOPEN4:
2842 case NFA_MOPEN5:
2843 case NFA_MOPEN6:
2844 case NFA_MOPEN7:
2845 case NFA_MOPEN8:
2846 case NFA_MOPEN9:
2847#ifdef FEAT_SYN_HL
2848 case NFA_ZOPEN: /* \z( \) Submatch */
2849 case NFA_ZOPEN1:
2850 case NFA_ZOPEN2:
2851 case NFA_ZOPEN3:
2852 case NFA_ZOPEN4:
2853 case NFA_ZOPEN5:
2854 case NFA_ZOPEN6:
2855 case NFA_ZOPEN7:
2856 case NFA_ZOPEN8:
2857 case NFA_ZOPEN9:
2858#endif
2859 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860 if (nfa_calc_size == TRUE)
2861 {
2862 nstate += 2;
2863 break;
2864 }
2865
2866 mopen = *p;
2867 switch (*p)
2868 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002869 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2870#ifdef FEAT_SYN_HL
2871 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2872 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2873 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2874 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2875 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2876 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2877 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2878 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2879 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2880 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2881#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002882#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002883 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002884#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002885 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002886 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002887 mclose = *p + NSUBEXP;
2888 break;
2889 }
2890
2891 /* Allow "NFA_MOPEN" as a valid postfix representation for
2892 * the empty regexp "". In this case, the NFA will be
2893 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2894 * empty groups of parenthesis, and empty mbyte chars */
2895 if (stackp == stack)
2896 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002897 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002898 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002899 goto theend;
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(list1(&s->out), s1);
2904 PUSH(frag(s, list1(&s1->out)));
2905 break;
2906 }
2907
2908 /* At least one node was emitted before NFA_MOPEN, so
2909 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2910 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002911 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002912 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002913 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914
Bram Moolenaar525666f2013-06-02 16:40:55 +02002915 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002916 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002917 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918 patch(e.out, s1);
2919
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002920#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002921 if (mopen == NFA_COMPOSING)
2922 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002923 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002924#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002925
2926 PUSH(frag(s, list1(&s1->out)));
2927 break;
2928
Bram Moolenaar5714b802013-05-28 22:03:20 +02002929 case NFA_BACKREF1:
2930 case NFA_BACKREF2:
2931 case NFA_BACKREF3:
2932 case NFA_BACKREF4:
2933 case NFA_BACKREF5:
2934 case NFA_BACKREF6:
2935 case NFA_BACKREF7:
2936 case NFA_BACKREF8:
2937 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002938#ifdef FEAT_SYN_HL
2939 case NFA_ZREF1:
2940 case NFA_ZREF2:
2941 case NFA_ZREF3:
2942 case NFA_ZREF4:
2943 case NFA_ZREF5:
2944 case NFA_ZREF6:
2945 case NFA_ZREF7:
2946 case NFA_ZREF8:
2947 case NFA_ZREF9:
2948#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002949 if (nfa_calc_size == TRUE)
2950 {
2951 nstate += 2;
2952 break;
2953 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002954 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002955 if (s == NULL)
2956 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002957 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002958 if (s1 == NULL)
2959 goto theend;
2960 patch(list1(&s->out), s1);
2961 PUSH(frag(s, list1(&s1->out)));
2962 break;
2963
Bram Moolenaar423532e2013-05-29 21:14:42 +02002964 case NFA_LNUM:
2965 case NFA_LNUM_GT:
2966 case NFA_LNUM_LT:
2967 case NFA_VCOL:
2968 case NFA_VCOL_GT:
2969 case NFA_VCOL_LT:
2970 case NFA_COL:
2971 case NFA_COL_GT:
2972 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002973 case NFA_MARK:
2974 case NFA_MARK_GT:
2975 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002976 {
2977 int n = *++p; /* lnum, col or mark name */
2978
Bram Moolenaar423532e2013-05-29 21:14:42 +02002979 if (nfa_calc_size == TRUE)
2980 {
2981 nstate += 1;
2982 break;
2983 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002984 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002985 if (s == NULL)
2986 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002987 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002988 PUSH(frag(s, list1(&s->out)));
2989 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002990 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002991
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992 case NFA_ZSTART:
2993 case NFA_ZEND:
2994 default:
2995 /* Operands */
2996 if (nfa_calc_size == TRUE)
2997 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002998 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002999 break;
3000 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003001 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003002 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003003 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003004 PUSH(frag(s, list1(&s->out)));
3005 break;
3006
3007 } /* switch(*p) */
3008
3009 } /* for(p = postfix; *p; ++p) */
3010
3011 if (nfa_calc_size == TRUE)
3012 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003013 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003014 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003015 }
3016
3017 e = POP();
3018 if (stackp != stack)
3019 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3020
3021 if (istate >= nstate)
3022 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3023
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003024 matchstate = &state_ptr[istate++]; /* the match state */
3025 matchstate->c = NFA_MATCH;
3026 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003027 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003028
3029 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003030 ret = e.start;
3031
3032theend:
3033 vim_free(stack);
3034 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003035
3036#undef POP1
3037#undef PUSH1
3038#undef POP2
3039#undef PUSH2
3040#undef POP
3041#undef PUSH
3042}
3043
3044/****************************************************************
3045 * NFA execution code.
3046 ****************************************************************/
3047
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003048typedef struct
3049{
3050 int in_use; /* number of subexpr with useful info */
3051
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003052 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003053 union
3054 {
3055 struct multipos
3056 {
3057 lpos_T start;
3058 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003059 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003060 struct linepos
3061 {
3062 char_u *start;
3063 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003064 } line[NSUBEXP];
3065 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003066} regsub_T;
3067
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003068typedef struct
3069{
3070 regsub_T norm; /* \( .. \) matches */
3071#ifdef FEAT_SYN_HL
3072 regsub_T synt; /* \z( .. \) matches */
3073#endif
3074} regsubs_T;
3075
Bram Moolenaara2d95102013-06-04 14:23:05 +02003076/* nfa_pim_T stores a Postponed Invisible Match. */
3077typedef struct nfa_pim_S nfa_pim_T;
3078struct nfa_pim_S
3079{
3080 nfa_state_T *state;
3081 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
3082 nfa_pim_T *pim; /* another PIM at the same position */
3083 regsubs_T subs; /* submatch info, only party used */
3084};
3085
3086/* Values for done in nfa_pim_T. */
3087#define NFA_PIM_TODO 0
3088#define NFA_PIM_MATCH 1
3089#define NFA_PIM_NOMATCH -1
3090
3091
Bram Moolenaar963fee22013-05-26 21:47:28 +02003092/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003093typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003094{
3095 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003096 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003097 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003098 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003099} nfa_thread_T;
3100
Bram Moolenaar963fee22013-05-26 21:47:28 +02003101/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003102typedef struct
3103{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003104 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003105 int n; /* nr of states currently in "t" */
3106 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003107 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003108} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003109
Bram Moolenaar5714b802013-05-28 22:03:20 +02003110#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003111static void log_subsexpr __ARGS((regsubs_T *subs));
3112static void log_subexpr __ARGS((regsub_T *sub));
3113
3114 static void
3115log_subsexpr(subs)
3116 regsubs_T *subs;
3117{
3118 log_subexpr(&subs->norm);
3119# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003120 if (nfa_has_zsubexpr)
3121 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003122# endif
3123}
3124
Bram Moolenaar5714b802013-05-28 22:03:20 +02003125 static void
3126log_subexpr(sub)
3127 regsub_T *sub;
3128{
3129 int j;
3130
3131 for (j = 0; j < sub->in_use; j++)
3132 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003133 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003134 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003135 sub->list.multi[j].start.col,
3136 (int)sub->list.multi[j].start.lnum,
3137 sub->list.multi[j].end.col,
3138 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003139 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003140 {
3141 char *s = (char *)sub->list.line[j].start;
3142 char *e = (char *)sub->list.line[j].end;
3143
Bram Moolenaar87953742013-06-05 18:52:40 +02003144 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003145 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003146 s == NULL ? "NULL" : s,
3147 e == NULL ? "NULL" : e);
3148 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003149}
3150#endif
3151
Bram Moolenaar963fee22013-05-26 21:47:28 +02003152/* Used during execution: whether a match has been found. */
3153static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003154
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003155static void clear_sub __ARGS((regsub_T *sub));
3156static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3157static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003158static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003159static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
3160static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003161static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003162static 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 +02003163
3164 static void
3165clear_sub(sub)
3166 regsub_T *sub;
3167{
3168 if (REG_MULTI)
3169 /* Use 0xff to set lnum to -1 */
3170 vim_memset(sub->list.multi, 0xff,
3171 sizeof(struct multipos) * nfa_nsubexpr);
3172 else
3173 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3174 sub->in_use = 0;
3175}
3176
3177/*
3178 * Copy the submatches from "from" to "to".
3179 */
3180 static void
3181copy_sub(to, from)
3182 regsub_T *to;
3183 regsub_T *from;
3184{
3185 to->in_use = from->in_use;
3186 if (from->in_use > 0)
3187 {
3188 /* Copy the match start and end positions. */
3189 if (REG_MULTI)
3190 mch_memmove(&to->list.multi[0],
3191 &from->list.multi[0],
3192 sizeof(struct multipos) * from->in_use);
3193 else
3194 mch_memmove(&to->list.line[0],
3195 &from->list.line[0],
3196 sizeof(struct linepos) * from->in_use);
3197 }
3198}
3199
3200/*
3201 * Like copy_sub() but exclude the main match.
3202 */
3203 static void
3204copy_sub_off(to, from)
3205 regsub_T *to;
3206 regsub_T *from;
3207{
3208 if (to->in_use < from->in_use)
3209 to->in_use = from->in_use;
3210 if (from->in_use > 1)
3211 {
3212 /* Copy the match start and end positions. */
3213 if (REG_MULTI)
3214 mch_memmove(&to->list.multi[1],
3215 &from->list.multi[1],
3216 sizeof(struct multipos) * (from->in_use - 1));
3217 else
3218 mch_memmove(&to->list.line[1],
3219 &from->list.line[1],
3220 sizeof(struct linepos) * (from->in_use - 1));
3221 }
3222}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223
Bram Moolenaar428e9872013-05-30 17:05:39 +02003224/*
3225 * Return TRUE if "sub1" and "sub2" have the same positions.
3226 */
3227 static int
3228sub_equal(sub1, sub2)
3229 regsub_T *sub1;
3230 regsub_T *sub2;
3231{
3232 int i;
3233 int todo;
3234 linenr_T s1, e1;
3235 linenr_T s2, e2;
3236 char_u *sp1, *ep1;
3237 char_u *sp2, *ep2;
3238
3239 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3240 if (REG_MULTI)
3241 {
3242 for (i = 0; i < todo; ++i)
3243 {
3244 if (i < sub1->in_use)
3245 {
3246 s1 = sub1->list.multi[i].start.lnum;
3247 e1 = sub1->list.multi[i].end.lnum;
3248 }
3249 else
3250 {
3251 s1 = 0;
3252 e1 = 0;
3253 }
3254 if (i < sub2->in_use)
3255 {
3256 s2 = sub2->list.multi[i].start.lnum;
3257 e2 = sub2->list.multi[i].end.lnum;
3258 }
3259 else
3260 {
3261 s2 = 0;
3262 e2 = 0;
3263 }
3264 if (s1 != s2 || e1 != e2)
3265 return FALSE;
3266 if (s1 != 0 && sub1->list.multi[i].start.col
3267 != sub2->list.multi[i].start.col)
3268 return FALSE;
3269 if (e1 != 0 && sub1->list.multi[i].end.col
3270 != sub2->list.multi[i].end.col)
3271 return FALSE;
3272 }
3273 }
3274 else
3275 {
3276 for (i = 0; i < todo; ++i)
3277 {
3278 if (i < sub1->in_use)
3279 {
3280 sp1 = sub1->list.line[i].start;
3281 ep1 = sub1->list.line[i].end;
3282 }
3283 else
3284 {
3285 sp1 = NULL;
3286 ep1 = NULL;
3287 }
3288 if (i < sub2->in_use)
3289 {
3290 sp2 = sub2->list.line[i].start;
3291 ep2 = sub2->list.line[i].end;
3292 }
3293 else
3294 {
3295 sp2 = NULL;
3296 ep2 = NULL;
3297 }
3298 if (sp1 != sp2 || ep1 != ep2)
3299 return FALSE;
3300 }
3301 }
3302
3303 return TRUE;
3304}
3305
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003306#ifdef ENABLE_LOG
3307 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003308report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003309{
3310 int col;
3311
3312 if (sub->in_use <= 0)
3313 col = -1;
3314 else if (REG_MULTI)
3315 col = sub->list.multi[0].start.col;
3316 else
3317 col = (int)(sub->list.line[0].start - regline);
3318 nfa_set_code(state->c);
3319 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3320 action, abs(state->id), lid, state->c, code, col);
3321}
3322#endif
3323
Bram Moolenaar43e02982013-06-07 17:31:29 +02003324/*
3325 * Return TRUE if the same state is already in list "l" with the same
3326 * positions as "subs".
3327 */
3328 static int
3329has_state_with_pos(l, state, subs)
3330 nfa_list_T *l; /* runtime state list */
3331 nfa_state_T *state; /* state to update */
3332 regsubs_T *subs; /* pointers to subexpressions */
3333{
3334 nfa_thread_T *thread;
3335 int i;
3336
3337 for (i = 0; i < l->n; ++i)
3338 {
3339 thread = &l->t[i];
3340 if (thread->state->id == state->id
3341 && sub_equal(&thread->subs.norm, &subs->norm)
3342#ifdef FEAT_SYN_HL
3343 && (!nfa_has_zsubexpr ||
3344 sub_equal(&thread->subs.synt, &subs->synt))
3345#endif
3346 )
3347 return TRUE;
3348 }
3349 return FALSE;
3350}
3351
3352/*
3353 * Return TRUE if "state" is already in list "l".
3354 */
3355 static int
3356state_in_list(l, state, subs)
3357 nfa_list_T *l; /* runtime state list */
3358 nfa_state_T *state; /* state to update */
3359 regsubs_T *subs; /* pointers to subexpressions */
3360{
3361 if (state->lastlist[nfa_ll_index] == l->id)
3362 {
3363 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3364 return TRUE;
3365 }
3366 return FALSE;
3367}
3368
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003370addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003371 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003373 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003374 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003375{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003376 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003377 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003378 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003379 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003380 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003381 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003382 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003383#ifdef ENABLE_LOG
3384 int did_print = FALSE;
3385#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386
3387 if (l == NULL || state == NULL)
3388 return;
3389
3390 switch (state->c)
3391 {
3392 case NFA_SPLIT:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003393 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003394 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003395 case NFA_NCLOSE:
3396 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003397 case NFA_MCLOSE1:
3398 case NFA_MCLOSE2:
3399 case NFA_MCLOSE3:
3400 case NFA_MCLOSE4:
3401 case NFA_MCLOSE5:
3402 case NFA_MCLOSE6:
3403 case NFA_MCLOSE7:
3404 case NFA_MCLOSE8:
3405 case NFA_MCLOSE9:
3406#ifdef FEAT_SYN_HL
3407 case NFA_ZCLOSE:
3408 case NFA_ZCLOSE1:
3409 case NFA_ZCLOSE2:
3410 case NFA_ZCLOSE3:
3411 case NFA_ZCLOSE4:
3412 case NFA_ZCLOSE5:
3413 case NFA_ZCLOSE6:
3414 case NFA_ZCLOSE7:
3415 case NFA_ZCLOSE8:
3416 case NFA_ZCLOSE9:
3417#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003418 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003419 /* These nodes are not added themselves but their "out" and/or
3420 * "out1" may be added below. */
3421 break;
3422
3423 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003424 case NFA_MOPEN1:
3425 case NFA_MOPEN2:
3426 case NFA_MOPEN3:
3427 case NFA_MOPEN4:
3428 case NFA_MOPEN5:
3429 case NFA_MOPEN6:
3430 case NFA_MOPEN7:
3431 case NFA_MOPEN8:
3432 case NFA_MOPEN9:
3433#ifdef FEAT_SYN_HL
3434 case NFA_ZOPEN:
3435 case NFA_ZOPEN1:
3436 case NFA_ZOPEN2:
3437 case NFA_ZOPEN3:
3438 case NFA_ZOPEN4:
3439 case NFA_ZOPEN5:
3440 case NFA_ZOPEN6:
3441 case NFA_ZOPEN7:
3442 case NFA_ZOPEN8:
3443 case NFA_ZOPEN9:
3444#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003445 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003446 /* These nodes do not need to be added, but we need to bail out
3447 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003448 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003449 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003450 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003451 break;
3452
Bram Moolenaar307aa162013-06-02 16:34:21 +02003453 case NFA_BOL:
3454 case NFA_BOF:
3455 /* "^" won't match past end-of-line, don't bother trying.
3456 * Except when we are going to the next line for a look-behind
3457 * match. */
3458 if (reginput > regline
3459 && (nfa_endp == NULL
3460 || !REG_MULTI
3461 || reglnum == nfa_endp->se_u.pos.lnum))
3462 goto skip_add;
3463 /* FALLTHROUGH */
3464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003466 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003467 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003468 /* This state is already in the list, don't add it again,
3469 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003470 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003471 {
3472skip_add:
3473#ifdef ENABLE_LOG
3474 nfa_set_code(state->c);
3475 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3476 abs(state->id), l->id, state->c, code);
3477#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003478 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003479 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003480
Bram Moolenaar43e02982013-06-07 17:31:29 +02003481 if (has_state_with_pos(l, state, subs))
3482 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003483 }
3484
Bram Moolenaara2d95102013-06-04 14:23:05 +02003485 /* when there are backreferences or look-behind matches the number
3486 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003487 if (nfa_has_backref && l->n == l->len)
3488 {
3489 int newlen = l->len * 3 / 2 + 50;
3490
3491 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3492 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003494
3495 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003496 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003497 thread = &l->t[l->n++];
3498 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003499 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003500 copy_sub(&thread->subs.norm, &subs->norm);
3501#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003502 if (nfa_has_zsubexpr)
3503 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003504#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003505#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003506 report_state("Adding", &thread->subs.norm, state, l->id);
3507 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003508#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509 }
3510
3511#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003512 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003513 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514#endif
3515 switch (state->c)
3516 {
3517 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003518 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519 break;
3520
3521 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003522 /* order matters here */
3523 addstate(l, state->out, subs, off);
3524 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003525 break;
3526
Bram Moolenaar5714b802013-05-28 22:03:20 +02003527 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003528 case NFA_NOPEN:
3529 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003530 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003531 break;
3532
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003533 case NFA_MOPEN:
3534 case NFA_MOPEN1:
3535 case NFA_MOPEN2:
3536 case NFA_MOPEN3:
3537 case NFA_MOPEN4:
3538 case NFA_MOPEN5:
3539 case NFA_MOPEN6:
3540 case NFA_MOPEN7:
3541 case NFA_MOPEN8:
3542 case NFA_MOPEN9:
3543#ifdef FEAT_SYN_HL
3544 case NFA_ZOPEN:
3545 case NFA_ZOPEN1:
3546 case NFA_ZOPEN2:
3547 case NFA_ZOPEN3:
3548 case NFA_ZOPEN4:
3549 case NFA_ZOPEN5:
3550 case NFA_ZOPEN6:
3551 case NFA_ZOPEN7:
3552 case NFA_ZOPEN8:
3553 case NFA_ZOPEN9:
3554#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003556 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003557 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003558 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003559 sub = &subs->norm;
3560 }
3561#ifdef FEAT_SYN_HL
3562 else if (state->c >= NFA_ZOPEN)
3563 {
3564 subidx = state->c - NFA_ZOPEN;
3565 sub = &subs->synt;
3566 }
3567#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003568 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003569 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003570 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003571 sub = &subs->norm;
3572 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003573
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003574 /* Set the position (with "off") in the subexpression. Save and
3575 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003576 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003577 if (REG_MULTI)
3578 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003579 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003580 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003581 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003582 save_in_use = -1;
3583 }
3584 else
3585 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003586 save_in_use = sub->in_use;
3587 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003588 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003589 sub->list.multi[i].start.lnum = -1;
3590 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003591 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003592 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003593 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003594 if (off == -1)
3595 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003596 sub->list.multi[subidx].start.lnum = reglnum + 1;
3597 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003598 }
3599 else
3600 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003601 sub->list.multi[subidx].start.lnum = reglnum;
3602 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003603 (colnr_T)(reginput - regline + off);
3604 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 }
3606 else
3607 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003608 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003609 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003610 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003611 save_in_use = -1;
3612 }
3613 else
3614 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003615 save_in_use = sub->in_use;
3616 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003617 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003618 sub->list.line[i].start = NULL;
3619 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003620 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003621 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003622 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003623 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003624 }
3625
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003626 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003627
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003628 if (save_in_use == -1)
3629 {
3630 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003631 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003632 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003633 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003634 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003635 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003636 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637 break;
3638
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003639 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003640 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003641 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003642 /* Do not overwrite the position set by \ze. If no \ze
3643 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003644 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003645 break;
3646 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003647 case NFA_MCLOSE1:
3648 case NFA_MCLOSE2:
3649 case NFA_MCLOSE3:
3650 case NFA_MCLOSE4:
3651 case NFA_MCLOSE5:
3652 case NFA_MCLOSE6:
3653 case NFA_MCLOSE7:
3654 case NFA_MCLOSE8:
3655 case NFA_MCLOSE9:
3656#ifdef FEAT_SYN_HL
3657 case NFA_ZCLOSE:
3658 case NFA_ZCLOSE1:
3659 case NFA_ZCLOSE2:
3660 case NFA_ZCLOSE3:
3661 case NFA_ZCLOSE4:
3662 case NFA_ZCLOSE5:
3663 case NFA_ZCLOSE6:
3664 case NFA_ZCLOSE7:
3665 case NFA_ZCLOSE8:
3666 case NFA_ZCLOSE9:
3667#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003668 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003670 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003672 sub = &subs->norm;
3673 }
3674#ifdef FEAT_SYN_HL
3675 else if (state->c >= NFA_ZCLOSE)
3676 {
3677 subidx = state->c - NFA_ZCLOSE;
3678 sub = &subs->synt;
3679 }
3680#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003681 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003682 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003683 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003684 sub = &subs->norm;
3685 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003686
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003687 /* We don't fill in gaps here, there must have been an MOPEN that
3688 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003689 save_in_use = sub->in_use;
3690 if (sub->in_use <= subidx)
3691 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003692 if (REG_MULTI)
3693 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003694 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003695 if (off == -1)
3696 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003697 sub->list.multi[subidx].end.lnum = reglnum + 1;
3698 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003699 }
3700 else
3701 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003702 sub->list.multi[subidx].end.lnum = reglnum;
3703 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003704 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003705 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003706 }
3707 else
3708 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003709 save_ptr = sub->list.line[subidx].end;
3710 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003711 }
3712
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003713 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003714
3715 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003716 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003718 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003719 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720 break;
3721 }
3722}
3723
3724/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003725 * Like addstate(), but the new state(s) are put at position "*ip".
3726 * Used for zero-width matches, next state to use is the added one.
3727 * This makes sure the order of states to be tried does not change, which
3728 * matters for alternatives.
3729 */
3730 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003731addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003732 nfa_list_T *l; /* runtime state list */
3733 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003734 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003735 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003736 int *ip;
3737{
3738 int tlen = l->n;
3739 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003740 int listidx = *ip;
3741 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003742
3743 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003744 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003745
Bram Moolenaara2d95102013-06-04 14:23:05 +02003746 /* fill in the "pim" field in the new states */
3747 if (pim != NULL)
3748 for (i = tlen; i < l->n; ++i)
3749 l->t[i].pim = pim;
3750
Bram Moolenaar4b417062013-05-25 20:19:50 +02003751 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003752 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003753 return;
3754
3755 /* re-order to put the new state at the current position */
3756 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003757 if (count == 1)
3758 {
3759 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003760 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003761 }
3762 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003763 {
3764 /* make space for new states, then move them from the
3765 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003766 mch_memmove(&(l->t[listidx + count]),
3767 &(l->t[listidx + 1]),
3768 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3769 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003770 &(l->t[l->n - 1]),
3771 sizeof(nfa_thread_T) * count);
3772 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003773 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003774 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003775}
3776
3777/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003778 * Check character class "class" against current character c.
3779 */
3780 static int
3781check_char_class(class, c)
3782 int class;
3783 int c;
3784{
3785 switch (class)
3786 {
3787 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003788 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003789 return OK;
3790 break;
3791 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003792 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003793 return OK;
3794 break;
3795 case NFA_CLASS_BLANK:
3796 if (c == ' ' || c == '\t')
3797 return OK;
3798 break;
3799 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003800 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003801 return OK;
3802 break;
3803 case NFA_CLASS_DIGIT:
3804 if (VIM_ISDIGIT(c))
3805 return OK;
3806 break;
3807 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003808 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003809 return OK;
3810 break;
3811 case NFA_CLASS_LOWER:
3812 if (MB_ISLOWER(c))
3813 return OK;
3814 break;
3815 case NFA_CLASS_PRINT:
3816 if (vim_isprintc(c))
3817 return OK;
3818 break;
3819 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003820 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003821 return OK;
3822 break;
3823 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02003824 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003825 return OK;
3826 break;
3827 case NFA_CLASS_UPPER:
3828 if (MB_ISUPPER(c))
3829 return OK;
3830 break;
3831 case NFA_CLASS_XDIGIT:
3832 if (vim_isxdigit(c))
3833 return OK;
3834 break;
3835 case NFA_CLASS_TAB:
3836 if (c == '\t')
3837 return OK;
3838 break;
3839 case NFA_CLASS_RETURN:
3840 if (c == '\r')
3841 return OK;
3842 break;
3843 case NFA_CLASS_BACKSPACE:
3844 if (c == '\b')
3845 return OK;
3846 break;
3847 case NFA_CLASS_ESCAPE:
3848 if (c == '\033')
3849 return OK;
3850 break;
3851
3852 default:
3853 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02003854 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
3855 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003856 }
3857 return FAIL;
3858}
3859
Bram Moolenaar5714b802013-05-28 22:03:20 +02003860static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3861
3862/*
3863 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003864 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003865 */
3866 static int
3867match_backref(sub, subidx, bytelen)
3868 regsub_T *sub; /* pointers to subexpressions */
3869 int subidx;
3870 int *bytelen; /* out: length of match in bytes */
3871{
3872 int len;
3873
3874 if (sub->in_use <= subidx)
3875 {
3876retempty:
3877 /* backref was not set, match an empty string */
3878 *bytelen = 0;
3879 return TRUE;
3880 }
3881
3882 if (REG_MULTI)
3883 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003884 if (sub->list.multi[subidx].start.lnum < 0
3885 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003886 goto retempty;
3887 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003888 len = sub->list.multi[subidx].end.col
3889 - sub->list.multi[subidx].start.col;
3890 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003891 reginput, &len) == 0)
3892 {
3893 *bytelen = len;
3894 return TRUE;
3895 }
3896 }
3897 else
3898 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003899 if (sub->list.line[subidx].start == NULL
3900 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003901 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003902 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3903 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003904 {
3905 *bytelen = len;
3906 return TRUE;
3907 }
3908 }
3909 return FALSE;
3910}
3911
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003912#ifdef FEAT_SYN_HL
3913
3914static int match_zref __ARGS((int subidx, int *bytelen));
3915
3916/*
3917 * Check for a match with \z subexpression "subidx".
3918 * Return TRUE if it matches.
3919 */
3920 static int
3921match_zref(subidx, bytelen)
3922 int subidx;
3923 int *bytelen; /* out: length of match in bytes */
3924{
3925 int len;
3926
3927 cleanup_zsubexpr();
3928 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3929 {
3930 /* backref was not set, match an empty string */
3931 *bytelen = 0;
3932 return TRUE;
3933 }
3934
3935 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3936 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3937 {
3938 *bytelen = len;
3939 return TRUE;
3940 }
3941 return FALSE;
3942}
3943#endif
3944
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003945/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003946 * Save list IDs for all NFA states of "prog" into "list".
3947 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003948 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003949 */
3950 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003951nfa_save_listids(prog, list)
3952 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003953 int *list;
3954{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003955 int i;
3956 nfa_state_T *p;
3957
3958 /* Order in the list is reverse, it's a bit faster that way. */
3959 p = &prog->state[0];
3960 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003961 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003962 list[i] = p->lastlist[1];
3963 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003964 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965 }
3966}
3967
3968/*
3969 * Restore list IDs from "list" to all NFA states.
3970 */
3971 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003972nfa_restore_listids(prog, list)
3973 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003974 int *list;
3975{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003976 int i;
3977 nfa_state_T *p;
3978
3979 p = &prog->state[0];
3980 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003981 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003982 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003983 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003984 }
3985}
3986
Bram Moolenaar423532e2013-05-29 21:14:42 +02003987 static int
3988nfa_re_num_cmp(val, op, pos)
3989 long_u val;
3990 int op;
3991 long_u pos;
3992{
3993 if (op == 1) return pos > val;
3994 if (op == 2) return pos < val;
3995 return val == pos;
3996}
3997
Bram Moolenaarf46da702013-06-02 22:37:42 +02003998static 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 +02003999static 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 +02004000
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004001/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004002 * Recursively call nfa_regmatch()
4003 */
4004 static int
4005recursive_regmatch(state, prog, submatch, m, listids)
4006 nfa_state_T *state;
4007 nfa_regprog_T *prog;
4008 regsubs_T *submatch;
4009 regsubs_T *m;
4010 int **listids;
4011{
4012 char_u *save_reginput = reginput;
4013 char_u *save_regline = regline;
4014 int save_reglnum = reglnum;
4015 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004016 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004017 save_se_T *save_nfa_endp = nfa_endp;
4018 save_se_T endpos;
4019 save_se_T *endposp = NULL;
4020 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004021 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004022
Bram Moolenaardecd9542013-06-07 16:31:50 +02004023 if (state->c == NFA_START_INVISIBLE_BEFORE
4024 || state->c == NFA_START_INVISIBLE_BEFORE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004025 {
4026 /* The recursive match must end at the current position. */
4027 endposp = &endpos;
4028 if (REG_MULTI)
4029 {
4030 endpos.se_u.pos.col = (int)(reginput - regline);
4031 endpos.se_u.pos.lnum = reglnum;
4032 }
4033 else
4034 endpos.se_u.ptr = reginput;
4035
4036 /* Go back the specified number of bytes, or as far as the
4037 * start of the previous line, to try matching "\@<=" or
4038 * not matching "\@<!".
4039 * TODO: This is very inefficient! Would be better to
4040 * first check for a match with what follows. */
4041 if (state->val <= 0)
4042 {
4043 if (REG_MULTI)
4044 {
4045 regline = reg_getline(--reglnum);
4046 if (regline == NULL)
4047 /* can't go before the first line */
4048 regline = reg_getline(++reglnum);
4049 }
4050 reginput = regline;
4051 }
4052 else
4053 {
4054 if (REG_MULTI && (int)(reginput - regline) < state->val)
4055 {
4056 /* Not enough bytes in this line, go to end of
4057 * previous line. */
4058 regline = reg_getline(--reglnum);
4059 if (regline == NULL)
4060 {
4061 /* can't go before the first line */
4062 regline = reg_getline(++reglnum);
4063 reginput = regline;
4064 }
4065 else
4066 reginput = regline + STRLEN(regline);
4067 }
4068 if ((int)(reginput - regline) >= state->val)
4069 {
4070 reginput -= state->val;
4071#ifdef FEAT_MBYTE
4072 if (has_mbyte)
4073 reginput -= mb_head_off(regline, reginput);
4074#endif
4075 }
4076 else
4077 reginput = regline;
4078 }
4079 }
4080
Bram Moolenaarf46da702013-06-02 22:37:42 +02004081#ifdef ENABLE_LOG
4082 if (log_fd != stderr)
4083 fclose(log_fd);
4084 log_fd = NULL;
4085#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004086 /* Have to clear the lastlist field of the NFA nodes, so that
4087 * nfa_regmatch() and addstate() can run properly after recursion. */
4088 if (nfa_ll_index == 1)
4089 {
4090 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4091 * values and clear them. */
4092 if (*listids == NULL)
4093 {
4094 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4095 if (*listids == NULL)
4096 {
4097 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4098 return 0;
4099 }
4100 }
4101 nfa_save_listids(prog, *listids);
4102 need_restore = TRUE;
4103 /* any value of nfa_listid will do */
4104 }
4105 else
4106 {
4107 /* First recursive nfa_regmatch() call, switch to the second lastlist
4108 * entry. Make sure nfa_listid is different from a previous recursive
4109 * call, because some states may still have this ID. */
4110 ++nfa_ll_index;
4111 if (nfa_listid <= nfa_alt_listid)
4112 nfa_listid = nfa_alt_listid;
4113 }
4114
4115 /* Call nfa_regmatch() to check if the current concat matches at this
4116 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004117 nfa_endp = endposp;
4118 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004119
4120 if (need_restore)
4121 nfa_restore_listids(prog, *listids);
4122 else
4123 {
4124 --nfa_ll_index;
4125 nfa_alt_listid = nfa_listid;
4126 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004127
4128 /* restore position in input text */
4129 reginput = save_reginput;
4130 regline = save_regline;
4131 reglnum = save_reglnum;
4132 nfa_match = save_nfa_match;
4133 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004134 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004135
4136#ifdef ENABLE_LOG
4137 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4138 if (log_fd != NULL)
4139 {
4140 fprintf(log_fd, "****************************\n");
4141 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4142 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4143 fprintf(log_fd, "****************************\n");
4144 }
4145 else
4146 {
4147 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4148 log_fd = stderr;
4149 }
4150#endif
4151
4152 return result;
4153}
4154
Bram Moolenaara2d95102013-06-04 14:23:05 +02004155static int failure_chance __ARGS((nfa_state_T *state, int depth));
4156
4157/*
4158 * Estimate the chance of a match with "state" failing.
4159 * NFA_ANY: 1
4160 * specific character: 99
4161 */
4162 static int
4163failure_chance(state, depth)
4164 nfa_state_T *state;
4165 int depth;
4166{
4167 int c = state->c;
4168 int l, r;
4169
4170 /* detect looping */
4171 if (depth > 4)
4172 return 1;
4173
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004174 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004175 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004176 case NFA_SPLIT:
4177 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4178 /* avoid recursive stuff */
4179 return 1;
4180 /* two alternatives, use the lowest failure chance */
4181 l = failure_chance(state->out, depth + 1);
4182 r = failure_chance(state->out1, depth + 1);
4183 return l < r ? l : r;
4184
4185 case NFA_ANY:
4186 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004187 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004188 case NFA_MATCH:
4189 /* empty match works always */
4190 return 0;
4191
4192 case NFA_BOL:
4193 case NFA_EOL:
4194 case NFA_BOF:
4195 case NFA_EOF:
4196 case NFA_NEWL:
4197 return 99;
4198
4199 case NFA_BOW:
4200 case NFA_EOW:
4201 return 90;
4202
4203 case NFA_MOPEN:
4204 case NFA_MOPEN1:
4205 case NFA_MOPEN2:
4206 case NFA_MOPEN3:
4207 case NFA_MOPEN4:
4208 case NFA_MOPEN5:
4209 case NFA_MOPEN6:
4210 case NFA_MOPEN7:
4211 case NFA_MOPEN8:
4212 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004213#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004214 case NFA_ZOPEN:
4215 case NFA_ZOPEN1:
4216 case NFA_ZOPEN2:
4217 case NFA_ZOPEN3:
4218 case NFA_ZOPEN4:
4219 case NFA_ZOPEN5:
4220 case NFA_ZOPEN6:
4221 case NFA_ZOPEN7:
4222 case NFA_ZOPEN8:
4223 case NFA_ZOPEN9:
4224 case NFA_ZCLOSE:
4225 case NFA_ZCLOSE1:
4226 case NFA_ZCLOSE2:
4227 case NFA_ZCLOSE3:
4228 case NFA_ZCLOSE4:
4229 case NFA_ZCLOSE5:
4230 case NFA_ZCLOSE6:
4231 case NFA_ZCLOSE7:
4232 case NFA_ZCLOSE8:
4233 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004234#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004235 case NFA_NOPEN:
4236 case NFA_MCLOSE:
4237 case NFA_MCLOSE1:
4238 case NFA_MCLOSE2:
4239 case NFA_MCLOSE3:
4240 case NFA_MCLOSE4:
4241 case NFA_MCLOSE5:
4242 case NFA_MCLOSE6:
4243 case NFA_MCLOSE7:
4244 case NFA_MCLOSE8:
4245 case NFA_MCLOSE9:
4246 case NFA_NCLOSE:
4247 return failure_chance(state->out, depth + 1);
4248
4249 case NFA_BACKREF1:
4250 case NFA_BACKREF2:
4251 case NFA_BACKREF3:
4252 case NFA_BACKREF4:
4253 case NFA_BACKREF5:
4254 case NFA_BACKREF6:
4255 case NFA_BACKREF7:
4256 case NFA_BACKREF8:
4257 case NFA_BACKREF9:
4258#ifdef FEAT_SYN_HL
4259 case NFA_ZREF1:
4260 case NFA_ZREF2:
4261 case NFA_ZREF3:
4262 case NFA_ZREF4:
4263 case NFA_ZREF5:
4264 case NFA_ZREF6:
4265 case NFA_ZREF7:
4266 case NFA_ZREF8:
4267 case NFA_ZREF9:
4268#endif
4269 /* backreferences don't match in many places */
4270 return 94;
4271
4272 case NFA_LNUM_GT:
4273 case NFA_LNUM_LT:
4274 case NFA_COL_GT:
4275 case NFA_COL_LT:
4276 case NFA_VCOL_GT:
4277 case NFA_VCOL_LT:
4278 case NFA_MARK_GT:
4279 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004280 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004281 /* before/after positions don't match very often */
4282 return 85;
4283
4284 case NFA_LNUM:
4285 return 90;
4286
4287 case NFA_CURSOR:
4288 case NFA_COL:
4289 case NFA_VCOL:
4290 case NFA_MARK:
4291 /* specific positions rarely match */
4292 return 98;
4293
4294 case NFA_COMPOSING:
4295 return 95;
4296
4297 default:
4298 if (c > 0)
4299 /* character match fails often */
4300 return 95;
4301 }
4302
4303 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004304 return 50;
4305}
4306
Bram Moolenaarf46da702013-06-02 22:37:42 +02004307/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308 * Main matching routine.
4309 *
4310 * Run NFA to determine whether it matches reginput.
4311 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004312 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004313 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004314 * Return TRUE if there is a match, FALSE otherwise.
4315 * Note: Caller must ensure that: start != NULL.
4316 */
4317 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004318nfa_regmatch(prog, start, submatch, m)
4319 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004320 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004321 regsubs_T *submatch;
4322 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004324 int result;
4325 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004327 int go_to_nextline = FALSE;
4328 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004329 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004330 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004331 nfa_list_T *thislist;
4332 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004333 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004334 nfa_state_T *add_state;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004335 int add_count;
4336 int add_off;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004337 garray_T pimlist;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004338 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004339#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004340 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004341
4342 if (debug == NULL)
4343 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004344 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004345 return FALSE;
4346 }
4347#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004348 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004349 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004351 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004352 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004353 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004354 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004355 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004356 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004357 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004358 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004359
4360#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004361 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004362 if (log_fd != NULL)
4363 {
4364 fprintf(log_fd, "**********************************\n");
4365 nfa_set_code(start->c);
4366 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4367 abs(start->id), code);
4368 fprintf(log_fd, "**********************************\n");
4369 }
4370 else
4371 {
4372 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4373 log_fd = stderr;
4374 }
4375#endif
4376
4377 thislist = &list[0];
4378 thislist->n = 0;
4379 nextlist = &list[1];
4380 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004381#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004382 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004383#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004384 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004385
4386 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4387 * it's the first MOPEN. */
4388 if (toplevel)
4389 {
4390 if (REG_MULTI)
4391 {
4392 m->norm.list.multi[0].start.lnum = reglnum;
4393 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4394 }
4395 else
4396 m->norm.list.line[0].start = reginput;
4397 m->norm.in_use = 1;
4398 addstate(thislist, start->out, m, 0);
4399 }
4400 else
4401 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004402
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004403#define ADD_STATE_IF_MATCH(state) \
4404 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004405 add_state = state->out; \
4406 add_off = clen; \
4407 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408
4409 /*
4410 * Run for each character.
4411 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004412 for (;;)
4413 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004414 int curc;
4415 int clen;
4416
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004417#ifdef FEAT_MBYTE
4418 if (has_mbyte)
4419 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004420 curc = (*mb_ptr2char)(reginput);
4421 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004422 }
4423 else
4424#endif
4425 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004426 curc = *reginput;
4427 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004428 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004429 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004430 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004431 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004432 go_to_nextline = FALSE;
4433 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004434
4435 /* swap lists */
4436 thislist = &list[flag];
4437 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004438 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004439 ++nfa_listid;
4440 thislist->id = nfa_listid;
4441 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004442
Bram Moolenaara2d95102013-06-04 14:23:05 +02004443 pimlist.ga_len = 0;
4444
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004445#ifdef ENABLE_LOG
4446 fprintf(log_fd, "------------------------------------------\n");
4447 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004448 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004449 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004450 {
4451 int i;
4452
4453 for (i = 0; i < thislist->n; i++)
4454 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4455 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004456 fprintf(log_fd, "\n");
4457#endif
4458
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004459#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004460 fprintf(debug, "\n-------------------\n");
4461#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004462 /*
4463 * If the state lists are empty we can stop.
4464 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004465 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004466 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004467
4468 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004469 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004470 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004471 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004472
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004473#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004474 nfa_set_code(t->state->c);
4475 fprintf(debug, "%s, ", code);
4476#endif
4477#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004478 {
4479 int col;
4480
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004481 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004482 col = -1;
4483 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004484 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004485 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004486 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004487 nfa_set_code(t->state->c);
4488 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4489 abs(t->state->id), (int)t->state->c, code, col);
4490 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004491#endif
4492
4493 /*
4494 * Handle the possible codes of the current state.
4495 * The most important is NFA_MATCH.
4496 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004497 add_state = NULL;
4498 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004499 switch (t->state->c)
4500 {
4501 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004502 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004503 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004504 copy_sub(&submatch->norm, &t->subs.norm);
4505#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004506 if (nfa_has_zsubexpr)
4507 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004508#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004509#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004510 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004511#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004512 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004513 * states at this position. When the list of states is going
4514 * to be empty quit without advancing, so that "reginput" is
4515 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004516 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004517 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004518 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004519 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004520
4521 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004522 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02004523 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004524 /*
4525 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004526 * NFA_START_INVISIBLE_BEFORE node.
4527 * They surround a zero-width group, used with "\@=", "\&",
4528 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529 * If we got here, it means that the current "invisible" group
4530 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004531 * nfa_regmatch(). For a look-behind match only when it ends
4532 * in the position in "nfa_endp".
4533 * Submatches are stored in *m, and used in the parent call.
4534 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004535#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004536 if (nfa_endp != NULL)
4537 {
4538 if (REG_MULTI)
4539 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4540 (int)reglnum,
4541 (int)nfa_endp->se_u.pos.lnum,
4542 (int)(reginput - regline),
4543 nfa_endp->se_u.pos.col);
4544 else
4545 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4546 (int)(reginput - regline),
4547 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004549#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004550 /* If "nfa_endp" is set it's only a match if it ends at
4551 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004552 if (nfa_endp != NULL && (REG_MULTI
4553 ? (reglnum != nfa_endp->se_u.pos.lnum
4554 || (int)(reginput - regline)
4555 != nfa_endp->se_u.pos.col)
4556 : reginput != nfa_endp->se_u.ptr))
4557 break;
4558
4559 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02004560 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004561 {
4562 copy_sub(&m->norm, &t->subs.norm);
4563#ifdef FEAT_SYN_HL
4564 if (nfa_has_zsubexpr)
4565 copy_sub(&m->synt, &t->subs.synt);
4566#endif
4567 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004568#ifdef ENABLE_LOG
4569 fprintf(log_fd, "Match found:\n");
4570 log_subsexpr(m);
4571#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004572 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004573 break;
4574
4575 case NFA_START_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004576 case NFA_START_INVISIBLE_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004577 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004578 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004579 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004580 nfa_pim_T *pim;
4581 int cout = t->state->out1->out->c;
4582
4583 /* Do it directly when what follows is possibly end of
4584 * match (closing paren).
4585 * Postpone when it is \@<= or \@<!, these are expensive.
4586 * TODO: remove the check for t->pim and check multiple
4587 * where it's used?
4588 * Otherwise first do the one that has the highest chance
4589 * of failing. */
4590 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004591#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004592 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004593#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004594 || cout == NFA_NCLOSE
4595 || t->pim != NULL
4596 || (t->state->c != NFA_START_INVISIBLE_BEFORE
Bram Moolenaardecd9542013-06-07 16:31:50 +02004597 && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG
Bram Moolenaara2d95102013-06-04 14:23:05 +02004598 && failure_chance(t->state->out1->out, 0)
4599 < failure_chance(t->state->out, 0)))
4600 {
4601 /*
4602 * First try matching the invisible match, then what
4603 * follows.
4604 */
4605 result = recursive_regmatch(t->state, prog,
4606 submatch, m, &listids);
4607
Bram Moolenaardecd9542013-06-07 16:31:50 +02004608 /* for \@! and \@<! it is a match when the result is
4609 * FALSE */
4610 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
4611 || t->state->c
4612 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004613 {
4614 /* Copy submatch info from the recursive call */
4615 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004616#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004617 if (nfa_has_zsubexpr)
4618 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004619#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004620
Bram Moolenaara2d95102013-06-04 14:23:05 +02004621 /* t->state->out1 is the corresponding
4622 * END_INVISIBLE node; Add its out to the current
4623 * list (zero-width match). */
4624 addstate_here(thislist, t->state->out1->out,
4625 &t->subs, t->pim, &listidx);
4626 }
4627 }
4628 else
4629 {
4630 /*
4631 * First try matching what follows at the current
4632 * position. Only if a match is found, addstate() is
4633 * called, then verify the invisible match matches.
4634 * Add a nfa_pim_T to the following states, it
4635 * contains info about the invisible match.
4636 */
4637 if (ga_grow(&pimlist, 1) == FAIL)
4638 goto theend;
4639 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4640 ++pimlist.ga_len;
4641 pim->state = t->state;
4642 pim->pim = NULL;
4643 pim->result = NFA_PIM_TODO;
4644
4645 /* t->state->out1 is the corresponding END_INVISIBLE
4646 * node; Add its out to the current list (zero-width
4647 * match). */
4648 addstate_here(thislist, t->state->out1->out, &t->subs,
4649 pim, &listidx);
4650 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004652 break;
4653
Bram Moolenaar87953742013-06-05 18:52:40 +02004654 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02004655 {
4656 nfa_state_T *skip = NULL;
4657#ifdef ENABLE_LOG
4658 int skip_lid = 0;
4659#endif
4660
4661 /* There is no point in trying to match the pattern if the
4662 * output state is not going to be added to the list. */
4663 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
4664 {
4665 skip = t->state->out1->out;
4666#ifdef ENABLE_LOG
4667 skip_lid = nextlist->id;
4668#endif
4669 }
4670 else if (state_in_list(nextlist,
4671 t->state->out1->out->out, &t->subs))
4672 {
4673 skip = t->state->out1->out->out;
4674#ifdef ENABLE_LOG
4675 skip_lid = nextlist->id;
4676#endif
4677 }
4678 else if(state_in_list(thislist,
4679 t->state->out1->out->out, &t->subs))
4680 {
4681 skip = t->state->out1->out->out;
4682#ifdef ENABLE_LOG
4683 skip_lid = thislist->id;
4684#endif
4685 }
4686 if (skip != NULL)
4687 {
4688#ifdef ENABLE_LOG
4689 nfa_set_code(skip->c);
4690 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
4691 abs(skip->id), skip_lid, skip->c, code);
4692#endif
4693 break;
4694 }
4695
Bram Moolenaar87953742013-06-05 18:52:40 +02004696 /* First try matching the pattern. */
4697 result = recursive_regmatch(t->state, prog,
4698 submatch, m, &listids);
4699 if (result)
4700 {
4701 int bytelen;
4702
4703#ifdef ENABLE_LOG
4704 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4705 log_subsexpr(m);
4706#endif
4707 /* Copy submatch info from the recursive call */
4708 copy_sub_off(&t->subs.norm, &m->norm);
4709#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004710 if (nfa_has_zsubexpr)
4711 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02004712#endif
4713 /* Now we need to skip over the matched text and then
4714 * continue with what follows. */
4715 if (REG_MULTI)
4716 /* TODO: multi-line match */
4717 bytelen = m->norm.list.multi[0].end.col
4718 - (int)(reginput - regline);
4719 else
4720 bytelen = (int)(m->norm.list.line[0].end - reginput);
4721
4722#ifdef ENABLE_LOG
4723 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4724#endif
4725 if (bytelen == 0)
4726 {
4727 /* empty match, output of corresponding
4728 * NFA_END_PATTERN/NFA_SKIP to be used at current
4729 * position */
4730 addstate_here(thislist, t->state->out1->out->out,
4731 &t->subs, t->pim, &listidx);
4732 }
4733 else if (bytelen <= clen)
4734 {
4735 /* match current character, output of corresponding
4736 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02004737 add_state = t->state->out1->out->out;
4738 add_off = clen;
4739 }
4740 else
4741 {
4742 /* skip over the matched characters, set character
4743 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02004744 add_state = t->state->out1->out;
4745 add_off = bytelen;
4746 add_count = bytelen - clen;
4747 }
4748 }
4749 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02004750 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004751
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752 case NFA_BOL:
4753 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004754 addstate_here(thislist, t->state->out, &t->subs,
4755 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004756 break;
4757
4758 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004759 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004760 addstate_here(thislist, t->state->out, &t->subs,
4761 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004762 break;
4763
4764 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004765 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004766
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004767 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004768 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004769#ifdef FEAT_MBYTE
4770 else if (has_mbyte)
4771 {
4772 int this_class;
4773
4774 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004775 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004776 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004777 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004779 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004780 }
4781#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004782 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004783 || (reginput > regline
4784 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02004785 result = FALSE;
4786 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004787 addstate_here(thislist, t->state->out, &t->subs,
4788 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004789 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004790
4791 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004792 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004793 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004794 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004795#ifdef FEAT_MBYTE
4796 else if (has_mbyte)
4797 {
4798 int this_class, prev_class;
4799
4800 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004801 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004802 prev_class = reg_prev_class();
4803 if (this_class == prev_class
4804 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004805 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004806 }
4807#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004808 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004809 || (reginput[0] != NUL
4810 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02004811 result = FALSE;
4812 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004813 addstate_here(thislist, t->state->out, &t->subs,
4814 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004815 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004816
Bram Moolenaar4b780632013-05-31 22:14:52 +02004817 case NFA_BOF:
4818 if (reglnum == 0 && reginput == regline
4819 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004820 addstate_here(thislist, t->state->out, &t->subs,
4821 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004822 break;
4823
4824 case NFA_EOF:
4825 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004826 addstate_here(thislist, t->state->out, &t->subs,
4827 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004828 break;
4829
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004830#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004831 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004832 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004833 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004834 int len = 0;
4835 nfa_state_T *end;
4836 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004837 int cchars[MAX_MCO];
4838 int ccount = 0;
4839 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004840
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004841 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004842 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004843 if (utf_iscomposing(sta->c))
4844 {
4845 /* Only match composing character(s), ignore base
4846 * character. Used for ".{composing}" and "{composing}"
4847 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004848 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004849 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004850 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004851 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004852 /* If \Z was present, then ignore composing characters.
4853 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004854 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004855 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004856 else
4857 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004858 while (sta->c != NFA_END_COMPOSING)
4859 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004860 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004861
4862 /* Check base character matches first, unless ignored. */
4863 else if (len > 0 || mc == sta->c)
4864 {
4865 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004866 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004867 len += mb_char2len(mc);
4868 sta = sta->out;
4869 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004870
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004871 /* We don't care about the order of composing characters.
4872 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004873 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004874 {
4875 mc = mb_ptr2char(reginput + len);
4876 cchars[ccount++] = mc;
4877 len += mb_char2len(mc);
4878 if (ccount == MAX_MCO)
4879 break;
4880 }
4881
4882 /* Check that each composing char in the pattern matches a
4883 * composing char in the text. We do not check if all
4884 * composing chars are matched. */
4885 result = OK;
4886 while (sta->c != NFA_END_COMPOSING)
4887 {
4888 for (j = 0; j < ccount; ++j)
4889 if (cchars[j] == sta->c)
4890 break;
4891 if (j == ccount)
4892 {
4893 result = FAIL;
4894 break;
4895 }
4896 sta = sta->out;
4897 }
4898 }
4899 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004900 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004901
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004902 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004903 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004904 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004905 }
4906#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004907
4908 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004909 if (curc == NUL && !reg_line_lbr && REG_MULTI
4910 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004911 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004912 go_to_nextline = TRUE;
4913 /* Pass -1 for the offset, which means taking the position
4914 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004915 add_state = t->state->out;
4916 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004917 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004918 else if (curc == '\n' && reg_line_lbr)
4919 {
4920 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004921 add_state = t->state->out;
4922 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004923 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004924 break;
4925
Bram Moolenaar417bad22013-06-07 14:08:30 +02004926 case NFA_START_COLL:
4927 case NFA_START_NEG_COLL:
4928 {
4929 /* What follows is a list of characters, until NFA_END_COLL.
4930 * One of them must match or none of them must match. */
4931 nfa_state_T *state;
4932 int result_if_matched;
4933 int c1, c2;
4934
4935 /* Never match EOL. If it's part of the collection it is added
4936 * as a separate state with an OR. */
4937 if (curc == NUL)
4938 break;
4939
4940 state = t->state->out;
4941 result_if_matched = (t->state->c == NFA_START_COLL);
4942 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004943 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02004944 if (state->c == NFA_END_COLL)
4945 {
4946 result = !result_if_matched;
4947 break;
4948 }
4949 if (state->c == NFA_RANGE_MIN)
4950 {
4951 c1 = state->val;
4952 state = state->out; /* advance to NFA_RANGE_MAX */
4953 c2 = state->val;
4954#ifdef ENABLE_LOG
4955 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
4956 curc, c1, c2);
4957#endif
4958 if (curc >= c1 && curc <= c2)
4959 {
4960 result = result_if_matched;
4961 break;
4962 }
4963 if (ireg_ic)
4964 {
4965 int curc_low = MB_TOLOWER(curc);
4966 int done = FALSE;
4967
4968 for ( ; c1 <= c2; ++c1)
4969 if (MB_TOLOWER(c1) == curc_low)
4970 {
4971 result = result_if_matched;
4972 done = TRUE;
4973 break;
4974 }
4975 if (done)
4976 break;
4977 }
4978 }
4979 else if (state->c < 0 ? check_char_class(state->c, curc)
4980 : (curc == state->c
4981 || (ireg_ic && MB_TOLOWER(curc)
4982 == MB_TOLOWER(state->c))))
4983 {
4984 result = result_if_matched;
4985 break;
4986 }
4987 state = state->out;
4988 }
4989 if (result)
4990 {
4991 /* next state is in out of the NFA_END_COLL, out1 of
4992 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004993 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004994 add_off = clen;
4995 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004996 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02004997 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004998
4999 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005000 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005001 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005002 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005003 add_state = t->state->out;
5004 add_off = clen;
5005 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005006 break;
5007
5008 /*
5009 * Character classes like \a for alpha, \d for digit etc.
5010 */
5011 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005012 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005013 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005014 break;
5015
5016 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005017 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005018 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005019 break;
5020
5021 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005022 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005023 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005024 break;
5025
5026 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005027 result = !VIM_ISDIGIT(curc)
5028 && vim_iswordp_buf(reginput, reg_buf);
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_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005033 result = vim_isfilec(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_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005038 result = !VIM_ISDIGIT(curc) && vim_isfilec(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_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005043 result = ptr2cells(reginput) == 1;
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_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005048 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005049 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005050 break;
5051
5052 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005053 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005054 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055 break;
5056
5057 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005058 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005059 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005060 break;
5061
5062 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005063 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005064 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005065 break;
5066
5067 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005068 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005069 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005070 break;
5071
5072 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005073 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005074 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005075 break;
5076
5077 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005078 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005079 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005080 break;
5081
5082 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005083 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005084 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005085 break;
5086
5087 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005088 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005089 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005090 break;
5091
5092 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005093 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005094 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005095 break;
5096
5097 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005098 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005099 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005100 break;
5101
5102 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005103 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005104 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005105 break;
5106
5107 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005108 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005109 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005110 break;
5111
5112 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005113 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005114 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005115 break;
5116
5117 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005118 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005119 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005120 break;
5121
5122 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005123 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005124 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005125 break;
5126
5127 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005128 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005129 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005130 break;
5131
5132 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005133 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005134 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005135 break;
5136
5137 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005138 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005139 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005140 break;
5141
Bram Moolenaar5714b802013-05-28 22:03:20 +02005142 case NFA_BACKREF1:
5143 case NFA_BACKREF2:
5144 case NFA_BACKREF3:
5145 case NFA_BACKREF4:
5146 case NFA_BACKREF5:
5147 case NFA_BACKREF6:
5148 case NFA_BACKREF7:
5149 case NFA_BACKREF8:
5150 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005151#ifdef FEAT_SYN_HL
5152 case NFA_ZREF1:
5153 case NFA_ZREF2:
5154 case NFA_ZREF3:
5155 case NFA_ZREF4:
5156 case NFA_ZREF5:
5157 case NFA_ZREF6:
5158 case NFA_ZREF7:
5159 case NFA_ZREF8:
5160 case NFA_ZREF9:
5161#endif
5162 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005163 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005164 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005165 int bytelen;
5166
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005167 if (t->state->c <= NFA_BACKREF9)
5168 {
5169 subidx = t->state->c - NFA_BACKREF1 + 1;
5170 result = match_backref(&t->subs.norm, subidx, &bytelen);
5171 }
5172#ifdef FEAT_SYN_HL
5173 else
5174 {
5175 subidx = t->state->c - NFA_ZREF1 + 1;
5176 result = match_zref(subidx, &bytelen);
5177 }
5178#endif
5179
Bram Moolenaar5714b802013-05-28 22:03:20 +02005180 if (result)
5181 {
5182 if (bytelen == 0)
5183 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005184 /* empty match always works, output of NFA_SKIP to be
5185 * used next */
5186 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005187 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02005188 }
5189 else if (bytelen <= clen)
5190 {
5191 /* match current character, jump ahead to out of
5192 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005193 add_state = t->state->out->out;
5194 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005195 }
5196 else
5197 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005198 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005199 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005200 add_state = t->state->out;
5201 add_off = bytelen;
5202 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005203 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005204 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005205 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005206 }
5207 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005208 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005209 if (t->count - clen <= 0)
5210 {
5211 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005212 add_state = t->state->out;
5213 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005214 }
5215 else
5216 {
5217 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005218 add_state = t->state;
5219 add_off = 0;
5220 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005221 }
5222 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005223
Bram Moolenaar423532e2013-05-29 21:14:42 +02005224 case NFA_LNUM:
5225 case NFA_LNUM_GT:
5226 case NFA_LNUM_LT:
5227 result = (REG_MULTI &&
5228 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5229 (long_u)(reglnum + reg_firstlnum)));
5230 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005231 addstate_here(thislist, t->state->out, &t->subs,
5232 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005233 break;
5234
5235 case NFA_COL:
5236 case NFA_COL_GT:
5237 case NFA_COL_LT:
5238 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5239 (long_u)(reginput - regline) + 1);
5240 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005241 addstate_here(thislist, t->state->out, &t->subs,
5242 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005243 break;
5244
5245 case NFA_VCOL:
5246 case NFA_VCOL_GT:
5247 case NFA_VCOL_LT:
5248 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5249 (long_u)win_linetabsize(
5250 reg_win == NULL ? curwin : reg_win,
5251 regline, (colnr_T)(reginput - regline)) + 1);
5252 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005253 addstate_here(thislist, t->state->out, &t->subs,
5254 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005255 break;
5256
Bram Moolenaar044aa292013-06-04 21:27:38 +02005257 case NFA_MARK:
5258 case NFA_MARK_GT:
5259 case NFA_MARK_LT:
5260 {
5261 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5262
5263 /* Compare the mark position to the match position. */
5264 result = (pos != NULL /* mark doesn't exist */
5265 && pos->lnum > 0 /* mark isn't set in reg_buf */
5266 && (pos->lnum == reglnum + reg_firstlnum
5267 ? (pos->col == (colnr_T)(reginput - regline)
5268 ? t->state->c == NFA_MARK
5269 : (pos->col < (colnr_T)(reginput - regline)
5270 ? t->state->c == NFA_MARK_GT
5271 : t->state->c == NFA_MARK_LT))
5272 : (pos->lnum < reglnum + reg_firstlnum
5273 ? t->state->c == NFA_MARK_GT
5274 : t->state->c == NFA_MARK_LT)));
5275 if (result)
5276 addstate_here(thislist, t->state->out, &t->subs,
5277 t->pim, &listidx);
5278 break;
5279 }
5280
Bram Moolenaar423532e2013-05-29 21:14:42 +02005281 case NFA_CURSOR:
5282 result = (reg_win != NULL
5283 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5284 && ((colnr_T)(reginput - regline)
5285 == reg_win->w_cursor.col));
5286 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005287 addstate_here(thislist, t->state->out, &t->subs,
5288 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005289 break;
5290
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005291 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005292#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005293 result = reg_match_visual();
5294 if (result)
5295 addstate_here(thislist, t->state->out, &t->subs,
5296 t->pim, &listidx);
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005297#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005298 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005300 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005301 {
5302 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005303
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005304 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005305 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005306 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005307 result = (c == curc);
5308
5309 if (!result && ireg_ic)
5310 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005311#ifdef FEAT_MBYTE
5312 /* If there is a composing character which is not being
5313 * ignored there can be no match. Match with composing
5314 * character uses NFA_COMPOSING above. */
5315 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005316 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005317 result = FALSE;
5318#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005319 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005320 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005321 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005322
5323 } /* switch (t->state->c) */
5324
5325 if (add_state != NULL)
5326 {
5327 if (t->pim != NULL)
5328 {
5329 /* postponed invisible match */
5330 /* TODO: also do t->pim->pim recursively? */
5331 if (t->pim->result == NFA_PIM_TODO)
5332 {
5333#ifdef ENABLE_LOG
5334 fprintf(log_fd, "\n");
5335 fprintf(log_fd, "==================================\n");
5336 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5337 fprintf(log_fd, "\n");
5338#endif
5339 result = recursive_regmatch(t->pim->state,
5340 prog, submatch, m, &listids);
5341 t->pim->result = result ? NFA_PIM_MATCH
5342 : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005343 /* for \@! and \@<! it is a match when the result is
5344 * FALSE */
5345 if (result != (t->pim->state->c
5346 == NFA_START_INVISIBLE_NEG
5347 || t->pim->state->c
5348 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005349 {
5350 /* Copy submatch info from the recursive call */
5351 copy_sub_off(&t->pim->subs.norm, &m->norm);
5352#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005353 if (nfa_has_zsubexpr)
5354 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005355#endif
5356 }
5357 }
5358 else
5359 {
5360 result = (t->pim->result == NFA_PIM_MATCH);
5361#ifdef ENABLE_LOG
5362 fprintf(log_fd, "\n");
5363 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5364 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5365 fprintf(log_fd, "\n");
5366#endif
5367 }
5368
Bram Moolenaardecd9542013-06-07 16:31:50 +02005369 /* for \@! and \@<! it is a match when result is FALSE */
5370 if (result != (t->pim->state->c == NFA_START_INVISIBLE_NEG
5371 || t->pim->state->c
5372 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005373 {
5374 /* Copy submatch info from the recursive call */
5375 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5376#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005377 if (nfa_has_zsubexpr)
5378 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005379#endif
5380 }
5381 else
5382 /* look-behind match failed, don't add the state */
5383 continue;
5384 }
5385
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005386 addstate(nextlist, add_state, &t->subs, add_off);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005387 if (add_count > 0)
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005388 nextlist->t[nextlist->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005389 }
5390
5391 } /* for (thislist = thislist; thislist->state; thislist++) */
5392
Bram Moolenaare23febd2013-05-26 18:40:14 +02005393 /* Look for the start of a match in the current position by adding the
5394 * start state to the list of states.
5395 * The first found match is the leftmost one, thus the order of states
5396 * matters!
5397 * Do not add the start state in recursive calls of nfa_regmatch(),
5398 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005399 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005400 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005401 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005402 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02005403 && reglnum == 0
5404 && clen != 0
5405 && (ireg_maxcol == 0
5406 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005407 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005408 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005409 ? (reglnum < nfa_endp->se_u.pos.lnum
5410 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005411 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005412 < nfa_endp->se_u.pos.col))
5413 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005414 {
5415#ifdef ENABLE_LOG
5416 fprintf(log_fd, "(---) STARTSTATE\n");
5417#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005418 /* Inline optimized code for addstate() if we know the state is
5419 * the first MOPEN. */
5420 if (toplevel)
5421 {
5422 if (REG_MULTI)
5423 m->norm.list.multi[0].start.col =
5424 (colnr_T)(reginput - regline) + clen;
5425 else
5426 m->norm.list.line[0].start = reginput + clen;
5427 addstate(nextlist, start->out, m, clen);
5428 }
5429 else
5430 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005431 }
5432
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005433#ifdef ENABLE_LOG
5434 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005435 {
5436 int i;
5437
5438 for (i = 0; i < thislist->n; i++)
5439 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5440 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005441 fprintf(log_fd, "\n");
5442#endif
5443
5444nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005445 /* Advance to the next character, or advance to the next line, or
5446 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005447 if (clen != 0)
5448 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005449 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5450 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005451 reg_nextline();
5452 else
5453 break;
5454 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005455
5456#ifdef ENABLE_LOG
5457 if (log_fd != stderr)
5458 fclose(log_fd);
5459 log_fd = NULL;
5460#endif
5461
5462theend:
5463 /* Free memory */
5464 vim_free(list[0].t);
5465 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005466 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005467 ga_clear(&pimlist);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005468#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005469#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005470 fclose(debug);
5471#endif
5472
Bram Moolenaar963fee22013-05-26 21:47:28 +02005473 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005474}
5475
5476/*
5477 * Try match of "prog" with at regline["col"].
5478 * Returns 0 for failure, number of lines contained in the match otherwise.
5479 */
5480 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005481nfa_regtry(prog, col)
5482 nfa_regprog_T *prog;
5483 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005484{
5485 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005486 regsubs_T subs, m;
5487 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005488#ifdef ENABLE_LOG
5489 FILE *f;
5490#endif
5491
5492 reginput = regline + col;
5493 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005494#ifdef FEAT_SYN_HL
5495 /* Clear the external match subpointers if necessary. */
5496 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005497 {
5498 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005499 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005500 }
5501 else
5502 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005503#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005504
5505#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005506 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005507 if (f != NULL)
5508 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005509 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005510#ifdef DEBUG
5511 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5512#endif
5513 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005514 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005515 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005516 fprintf(f, "\n\n");
5517 fclose(f);
5518 }
5519 else
5520 EMSG(_("Could not open temporary log file for writing "));
5521#endif
5522
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005523 clear_sub(&subs.norm);
5524 clear_sub(&m.norm);
5525#ifdef FEAT_SYN_HL
5526 clear_sub(&subs.synt);
5527 clear_sub(&m.synt);
5528#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005530 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531 return 0;
5532
5533 cleanup_subexpr();
5534 if (REG_MULTI)
5535 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005536 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005537 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005538 reg_startpos[i] = subs.norm.list.multi[i].start;
5539 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005540 }
5541
5542 if (reg_startpos[0].lnum < 0)
5543 {
5544 reg_startpos[0].lnum = 0;
5545 reg_startpos[0].col = col;
5546 }
5547 if (reg_endpos[0].lnum < 0)
5548 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005549 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550 reg_endpos[0].lnum = reglnum;
5551 reg_endpos[0].col = (int)(reginput - regline);
5552 }
5553 else
5554 /* Use line number of "\ze". */
5555 reglnum = reg_endpos[0].lnum;
5556 }
5557 else
5558 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005559 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005560 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005561 reg_startp[i] = subs.norm.list.line[i].start;
5562 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563 }
5564
5565 if (reg_startp[0] == NULL)
5566 reg_startp[0] = regline + col;
5567 if (reg_endp[0] == NULL)
5568 reg_endp[0] = reginput;
5569 }
5570
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005571#ifdef FEAT_SYN_HL
5572 /* Package any found \z(...\) matches for export. Default is none. */
5573 unref_extmatch(re_extmatch_out);
5574 re_extmatch_out = NULL;
5575
5576 if (prog->reghasz == REX_SET)
5577 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005578 cleanup_zsubexpr();
5579 re_extmatch_out = make_extmatch();
5580 for (i = 0; i < subs.synt.in_use; i++)
5581 {
5582 if (REG_MULTI)
5583 {
5584 struct multipos *mpos = &subs.synt.list.multi[i];
5585
5586 /* Only accept single line matches. */
5587 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5588 re_extmatch_out->matches[i] =
5589 vim_strnsave(reg_getline(mpos->start.lnum)
5590 + mpos->start.col,
5591 mpos->end.col - mpos->start.col);
5592 }
5593 else
5594 {
5595 struct linepos *lpos = &subs.synt.list.line[i];
5596
5597 if (lpos->start != NULL && lpos->end != NULL)
5598 re_extmatch_out->matches[i] =
5599 vim_strnsave(lpos->start,
5600 (int)(lpos->end - lpos->start));
5601 }
5602 }
5603 }
5604#endif
5605
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005606 return 1 + reglnum;
5607}
5608
5609/*
5610 * Match a regexp against a string ("line" points to the string) or multiple
5611 * lines ("line" is NULL, use reg_getline()).
5612 *
5613 * Returns 0 for failure, number of lines contained in the match otherwise.
5614 */
5615 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02005616nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005617 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005618 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005619{
5620 nfa_regprog_T *prog;
5621 long retval = 0L;
5622 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005623 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624
5625 if (REG_MULTI)
5626 {
5627 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5628 line = reg_getline((linenr_T)0); /* relative to the cursor */
5629 reg_startpos = reg_mmatch->startpos;
5630 reg_endpos = reg_mmatch->endpos;
5631 }
5632 else
5633 {
5634 prog = (nfa_regprog_T *)reg_match->regprog;
5635 reg_startp = reg_match->startp;
5636 reg_endp = reg_match->endp;
5637 }
5638
5639 /* Be paranoid... */
5640 if (prog == NULL || line == NULL)
5641 {
5642 EMSG(_(e_null));
5643 goto theend;
5644 }
5645
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005646 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5647 if (prog->regflags & RF_ICASE)
5648 ireg_ic = TRUE;
5649 else if (prog->regflags & RF_NOICASE)
5650 ireg_ic = FALSE;
5651
5652#ifdef FEAT_MBYTE
5653 /* If pattern contains "\Z" overrule value of ireg_icombine */
5654 if (prog->regflags & RF_ICOMBINE)
5655 ireg_icombine = TRUE;
5656#endif
5657
5658 regline = line;
5659 reglnum = 0; /* relative to line */
5660
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005661 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005662 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005663 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005664 nfa_listid = 1;
5665 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005666#ifdef DEBUG
5667 nfa_regengine.expr = prog->pattern;
5668#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005669
Bram Moolenaard89616e2013-06-06 18:46:06 +02005670 if (prog->reganch && col > 0)
5671 return 0L;
5672
5673 if (prog->regstart != NUL)
5674 {
5675 char_u *s;
5676
5677 /* Skip until the char we know it must start with.
5678 * Used often, do some work to avoid call overhead. */
5679 if (!ireg_ic
5680#ifdef FEAT_MBYTE
5681 && !has_mbyte
5682#endif
5683 )
5684 s = vim_strbyte(regline + col, prog->regstart);
5685 else
5686 s = cstrchr(regline + col, prog->regstart);
5687 if (s == NULL)
5688 return 0L;
5689 col = (int)(s - regline);
5690 }
5691
5692 /* If the start column is past the maximum column: no need to try. */
5693 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5694 goto theend;
5695
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005696 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005697 for (i = 0; i < nstate; ++i)
5698 {
5699 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005700 prog->state[i].lastlist[0] = 0;
5701 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005702 }
5703
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005704 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005705
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005706#ifdef DEBUG
5707 nfa_regengine.expr = NULL;
5708#endif
5709
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005710theend:
5711 return retval;
5712}
5713
5714/*
5715 * Compile a regular expression into internal code for the NFA matcher.
5716 * Returns the program in allocated space. Returns NULL for an error.
5717 */
5718 static regprog_T *
5719nfa_regcomp(expr, re_flags)
5720 char_u *expr;
5721 int re_flags;
5722{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005723 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005724 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005725 int *postfix;
5726
5727 if (expr == NULL)
5728 return NULL;
5729
5730#ifdef DEBUG
5731 nfa_regengine.expr = expr;
5732#endif
5733
5734 init_class_tab();
5735
5736 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5737 return NULL;
5738
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005739 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005740 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005741 postfix = re2post();
5742 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005743 {
5744 /* TODO: only give this error for debugging? */
5745 if (post_ptr >= post_end)
5746 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005747 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005748 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005749
5750 /*
5751 * In order to build the NFA, we parse the input regexp twice:
5752 * 1. first pass to count size (so we can allocate space)
5753 * 2. second to emit code
5754 */
5755#ifdef ENABLE_LOG
5756 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005757 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005758
5759 if (f != NULL)
5760 {
5761 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5762 fclose(f);
5763 }
5764 }
5765#endif
5766
5767 /*
5768 * PASS 1
5769 * Count number of NFA states in "nstate". Do not build the NFA.
5770 */
5771 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005772
5773 /* Space for compiled regexp */
5774 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5775 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5776 if (prog == NULL)
5777 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005778 state_ptr = prog->state;
5779
5780 /*
5781 * PASS 2
5782 * Build the NFA
5783 */
5784 prog->start = post2nfa(postfix, post_ptr, FALSE);
5785 if (prog->start == NULL)
5786 goto fail;
5787
5788 prog->regflags = regflags;
5789 prog->engine = &nfa_regengine;
5790 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005791 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005792 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005793 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005794
5795 prog->reganch = nfa_get_reganch(prog->start, 0);
5796 prog->regstart = nfa_get_regstart(prog->start, 0);
5797
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005798#ifdef ENABLE_LOG
5799 nfa_postfix_dump(expr, OK);
5800 nfa_dump(prog);
5801#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005802#ifdef FEAT_SYN_HL
5803 /* Remember whether this pattern has any \z specials in it. */
5804 prog->reghasz = re_has_z;
5805#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005806#ifdef DEBUG
5807 prog->pattern = vim_strsave(expr); /* memory will leak */
5808 nfa_regengine.expr = NULL;
5809#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005810
5811out:
5812 vim_free(post_start);
5813 post_start = post_ptr = post_end = NULL;
5814 state_ptr = NULL;
5815 return (regprog_T *)prog;
5816
5817fail:
5818 vim_free(prog);
5819 prog = NULL;
5820#ifdef ENABLE_LOG
5821 nfa_postfix_dump(expr, FAIL);
5822#endif
5823#ifdef DEBUG
5824 nfa_regengine.expr = NULL;
5825#endif
5826 goto out;
5827}
5828
5829
5830/*
5831 * Match a regexp against a string.
5832 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5833 * Uses curbuf for line count and 'iskeyword'.
5834 *
5835 * Return TRUE if there is a match, FALSE if not.
5836 */
5837 static int
5838nfa_regexec(rmp, line, col)
5839 regmatch_T *rmp;
5840 char_u *line; /* string to match against */
5841 colnr_T col; /* column to start looking for match */
5842{
5843 reg_match = rmp;
5844 reg_mmatch = NULL;
5845 reg_maxline = 0;
5846 reg_line_lbr = FALSE;
5847 reg_buf = curbuf;
5848 reg_win = NULL;
5849 ireg_ic = rmp->rm_ic;
5850#ifdef FEAT_MBYTE
5851 ireg_icombine = FALSE;
5852#endif
5853 ireg_maxcol = 0;
5854 return (nfa_regexec_both(line, col) != 0);
5855}
5856
5857#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5858 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5859
5860static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5861
5862/*
5863 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5864 */
5865 static int
5866nfa_regexec_nl(rmp, line, col)
5867 regmatch_T *rmp;
5868 char_u *line; /* string to match against */
5869 colnr_T col; /* column to start looking for match */
5870{
5871 reg_match = rmp;
5872 reg_mmatch = NULL;
5873 reg_maxline = 0;
5874 reg_line_lbr = TRUE;
5875 reg_buf = curbuf;
5876 reg_win = NULL;
5877 ireg_ic = rmp->rm_ic;
5878#ifdef FEAT_MBYTE
5879 ireg_icombine = FALSE;
5880#endif
5881 ireg_maxcol = 0;
5882 return (nfa_regexec_both(line, col) != 0);
5883}
5884#endif
5885
5886
5887/*
5888 * Match a regexp against multiple lines.
5889 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5890 * Uses curbuf for line count and 'iskeyword'.
5891 *
5892 * Return zero if there is no match. Return number of lines contained in the
5893 * match otherwise.
5894 *
5895 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5896 *
5897 * ! Also NOTE : match may actually be in another line. e.g.:
5898 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5899 *
5900 * +-------------------------+
5901 * |a |
5902 * |b |
5903 * |c |
5904 * | |
5905 * +-------------------------+
5906 *
5907 * then nfa_regexec_multi() returns 3. while the original
5908 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5909 *
5910 * FIXME if this behavior is not compatible.
5911 */
5912 static long
5913nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5914 regmmatch_T *rmp;
5915 win_T *win; /* window in which to search or NULL */
5916 buf_T *buf; /* buffer in which to search */
5917 linenr_T lnum; /* nr of line to start looking for match */
5918 colnr_T col; /* column to start looking for match */
5919 proftime_T *tm UNUSED; /* timeout limit or NULL */
5920{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005921 reg_match = NULL;
5922 reg_mmatch = rmp;
5923 reg_buf = buf;
5924 reg_win = win;
5925 reg_firstlnum = lnum;
5926 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5927 reg_line_lbr = FALSE;
5928 ireg_ic = rmp->rmm_ic;
5929#ifdef FEAT_MBYTE
5930 ireg_icombine = FALSE;
5931#endif
5932 ireg_maxcol = rmp->rmm_maxcol;
5933
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005934 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005935}
5936
5937#ifdef DEBUG
5938# undef ENABLE_LOG
5939#endif