blob: 8791cb53e1186ff8f0b802991f7bf88cb20be1e9 [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 Moolenaarefb23f22013-06-01 23:02:54 +02003159static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003160static 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 +02003161
3162 static void
3163clear_sub(sub)
3164 regsub_T *sub;
3165{
3166 if (REG_MULTI)
3167 /* Use 0xff to set lnum to -1 */
3168 vim_memset(sub->list.multi, 0xff,
3169 sizeof(struct multipos) * nfa_nsubexpr);
3170 else
3171 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3172 sub->in_use = 0;
3173}
3174
3175/*
3176 * Copy the submatches from "from" to "to".
3177 */
3178 static void
3179copy_sub(to, from)
3180 regsub_T *to;
3181 regsub_T *from;
3182{
3183 to->in_use = from->in_use;
3184 if (from->in_use > 0)
3185 {
3186 /* Copy the match start and end positions. */
3187 if (REG_MULTI)
3188 mch_memmove(&to->list.multi[0],
3189 &from->list.multi[0],
3190 sizeof(struct multipos) * from->in_use);
3191 else
3192 mch_memmove(&to->list.line[0],
3193 &from->list.line[0],
3194 sizeof(struct linepos) * from->in_use);
3195 }
3196}
3197
3198/*
3199 * Like copy_sub() but exclude the main match.
3200 */
3201 static void
3202copy_sub_off(to, from)
3203 regsub_T *to;
3204 regsub_T *from;
3205{
3206 if (to->in_use < from->in_use)
3207 to->in_use = from->in_use;
3208 if (from->in_use > 1)
3209 {
3210 /* Copy the match start and end positions. */
3211 if (REG_MULTI)
3212 mch_memmove(&to->list.multi[1],
3213 &from->list.multi[1],
3214 sizeof(struct multipos) * (from->in_use - 1));
3215 else
3216 mch_memmove(&to->list.line[1],
3217 &from->list.line[1],
3218 sizeof(struct linepos) * (from->in_use - 1));
3219 }
3220}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003221
Bram Moolenaar428e9872013-05-30 17:05:39 +02003222/*
3223 * Return TRUE if "sub1" and "sub2" have the same positions.
3224 */
3225 static int
3226sub_equal(sub1, sub2)
3227 regsub_T *sub1;
3228 regsub_T *sub2;
3229{
3230 int i;
3231 int todo;
3232 linenr_T s1, e1;
3233 linenr_T s2, e2;
3234 char_u *sp1, *ep1;
3235 char_u *sp2, *ep2;
3236
3237 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3238 if (REG_MULTI)
3239 {
3240 for (i = 0; i < todo; ++i)
3241 {
3242 if (i < sub1->in_use)
3243 {
3244 s1 = sub1->list.multi[i].start.lnum;
3245 e1 = sub1->list.multi[i].end.lnum;
3246 }
3247 else
3248 {
3249 s1 = 0;
3250 e1 = 0;
3251 }
3252 if (i < sub2->in_use)
3253 {
3254 s2 = sub2->list.multi[i].start.lnum;
3255 e2 = sub2->list.multi[i].end.lnum;
3256 }
3257 else
3258 {
3259 s2 = 0;
3260 e2 = 0;
3261 }
3262 if (s1 != s2 || e1 != e2)
3263 return FALSE;
3264 if (s1 != 0 && sub1->list.multi[i].start.col
3265 != sub2->list.multi[i].start.col)
3266 return FALSE;
3267 if (e1 != 0 && sub1->list.multi[i].end.col
3268 != sub2->list.multi[i].end.col)
3269 return FALSE;
3270 }
3271 }
3272 else
3273 {
3274 for (i = 0; i < todo; ++i)
3275 {
3276 if (i < sub1->in_use)
3277 {
3278 sp1 = sub1->list.line[i].start;
3279 ep1 = sub1->list.line[i].end;
3280 }
3281 else
3282 {
3283 sp1 = NULL;
3284 ep1 = NULL;
3285 }
3286 if (i < sub2->in_use)
3287 {
3288 sp2 = sub2->list.line[i].start;
3289 ep2 = sub2->list.line[i].end;
3290 }
3291 else
3292 {
3293 sp2 = NULL;
3294 ep2 = NULL;
3295 }
3296 if (sp1 != sp2 || ep1 != ep2)
3297 return FALSE;
3298 }
3299 }
3300
3301 return TRUE;
3302}
3303
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003304#ifdef ENABLE_LOG
3305 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003306report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003307{
3308 int col;
3309
3310 if (sub->in_use <= 0)
3311 col = -1;
3312 else if (REG_MULTI)
3313 col = sub->list.multi[0].start.col;
3314 else
3315 col = (int)(sub->list.line[0].start - regline);
3316 nfa_set_code(state->c);
3317 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3318 action, abs(state->id), lid, state->c, code, col);
3319}
3320#endif
3321
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003323addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003324 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003325 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003326 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003327 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003329 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003330 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003331 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003332 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003333 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003334 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003335 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003336#ifdef ENABLE_LOG
3337 int did_print = FALSE;
3338#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339
3340 if (l == NULL || state == NULL)
3341 return;
3342
3343 switch (state->c)
3344 {
3345 case NFA_SPLIT:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003347 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 case NFA_NCLOSE:
3349 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003350 case NFA_MCLOSE1:
3351 case NFA_MCLOSE2:
3352 case NFA_MCLOSE3:
3353 case NFA_MCLOSE4:
3354 case NFA_MCLOSE5:
3355 case NFA_MCLOSE6:
3356 case NFA_MCLOSE7:
3357 case NFA_MCLOSE8:
3358 case NFA_MCLOSE9:
3359#ifdef FEAT_SYN_HL
3360 case NFA_ZCLOSE:
3361 case NFA_ZCLOSE1:
3362 case NFA_ZCLOSE2:
3363 case NFA_ZCLOSE3:
3364 case NFA_ZCLOSE4:
3365 case NFA_ZCLOSE5:
3366 case NFA_ZCLOSE6:
3367 case NFA_ZCLOSE7:
3368 case NFA_ZCLOSE8:
3369 case NFA_ZCLOSE9:
3370#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003371 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003372 /* These nodes are not added themselves but their "out" and/or
3373 * "out1" may be added below. */
3374 break;
3375
3376 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003377 case NFA_MOPEN1:
3378 case NFA_MOPEN2:
3379 case NFA_MOPEN3:
3380 case NFA_MOPEN4:
3381 case NFA_MOPEN5:
3382 case NFA_MOPEN6:
3383 case NFA_MOPEN7:
3384 case NFA_MOPEN8:
3385 case NFA_MOPEN9:
3386#ifdef FEAT_SYN_HL
3387 case NFA_ZOPEN:
3388 case NFA_ZOPEN1:
3389 case NFA_ZOPEN2:
3390 case NFA_ZOPEN3:
3391 case NFA_ZOPEN4:
3392 case NFA_ZOPEN5:
3393 case NFA_ZOPEN6:
3394 case NFA_ZOPEN7:
3395 case NFA_ZOPEN8:
3396 case NFA_ZOPEN9:
3397#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003398 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003399 /* These nodes do not need to be added, but we need to bail out
3400 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003401 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003402 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003403 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404 break;
3405
Bram Moolenaar307aa162013-06-02 16:34:21 +02003406 case NFA_BOL:
3407 case NFA_BOF:
3408 /* "^" won't match past end-of-line, don't bother trying.
3409 * Except when we are going to the next line for a look-behind
3410 * match. */
3411 if (reginput > regline
3412 && (nfa_endp == NULL
3413 || !REG_MULTI
3414 || reglnum == nfa_endp->se_u.pos.lnum))
3415 goto skip_add;
3416 /* FALLTHROUGH */
3417
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003419 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003420 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003421 /* This state is already in the list, don't add it again,
3422 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003423 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003424 {
3425skip_add:
3426#ifdef ENABLE_LOG
3427 nfa_set_code(state->c);
3428 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3429 abs(state->id), l->id, state->c, code);
3430#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003431 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003432 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003433
3434 /* See if the same state is already in the list with the same
3435 * positions. */
3436 for (i = 0; i < l->n; ++i)
3437 {
3438 thread = &l->t[i];
3439 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003440 && sub_equal(&thread->subs.norm, &subs->norm)
3441#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003442 && (!nfa_has_zsubexpr ||
3443 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003444#endif
3445 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003446 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003447 }
3448 }
3449
Bram Moolenaara2d95102013-06-04 14:23:05 +02003450 /* when there are backreferences or look-behind matches the number
3451 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003452 if (nfa_has_backref && l->n == l->len)
3453 {
3454 int newlen = l->len * 3 / 2 + 50;
3455
3456 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3457 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003459
3460 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003461 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003462 thread = &l->t[l->n++];
3463 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003464 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003465 copy_sub(&thread->subs.norm, &subs->norm);
3466#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003467 if (nfa_has_zsubexpr)
3468 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003469#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003470#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003471 report_state("Adding", &thread->subs.norm, state, l->id);
3472 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003473#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 }
3475
3476#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003477 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003478 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479#endif
3480 switch (state->c)
3481 {
3482 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003483 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484 break;
3485
3486 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003487 /* order matters here */
3488 addstate(l, state->out, subs, off);
3489 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003490 break;
3491
Bram Moolenaar5714b802013-05-28 22:03:20 +02003492 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 case NFA_NOPEN:
3494 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003495 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003496 break;
3497
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003498 case NFA_MOPEN:
3499 case NFA_MOPEN1:
3500 case NFA_MOPEN2:
3501 case NFA_MOPEN3:
3502 case NFA_MOPEN4:
3503 case NFA_MOPEN5:
3504 case NFA_MOPEN6:
3505 case NFA_MOPEN7:
3506 case NFA_MOPEN8:
3507 case NFA_MOPEN9:
3508#ifdef FEAT_SYN_HL
3509 case NFA_ZOPEN:
3510 case NFA_ZOPEN1:
3511 case NFA_ZOPEN2:
3512 case NFA_ZOPEN3:
3513 case NFA_ZOPEN4:
3514 case NFA_ZOPEN5:
3515 case NFA_ZOPEN6:
3516 case NFA_ZOPEN7:
3517 case NFA_ZOPEN8:
3518 case NFA_ZOPEN9:
3519#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003521 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003522 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003523 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003524 sub = &subs->norm;
3525 }
3526#ifdef FEAT_SYN_HL
3527 else if (state->c >= NFA_ZOPEN)
3528 {
3529 subidx = state->c - NFA_ZOPEN;
3530 sub = &subs->synt;
3531 }
3532#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003533 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003534 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003535 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003536 sub = &subs->norm;
3537 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003538
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003539 /* Set the position (with "off") in the subexpression. Save and
3540 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003541 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542 if (REG_MULTI)
3543 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003544 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003545 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003546 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003547 save_in_use = -1;
3548 }
3549 else
3550 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003551 save_in_use = sub->in_use;
3552 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003553 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003554 sub->list.multi[i].start.lnum = -1;
3555 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003556 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003557 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003558 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003559 if (off == -1)
3560 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003561 sub->list.multi[subidx].start.lnum = reglnum + 1;
3562 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003563 }
3564 else
3565 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003566 sub->list.multi[subidx].start.lnum = reglnum;
3567 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003568 (colnr_T)(reginput - regline + off);
3569 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003570 }
3571 else
3572 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003573 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003574 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003575 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003576 save_in_use = -1;
3577 }
3578 else
3579 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003580 save_in_use = sub->in_use;
3581 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003582 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003583 sub->list.line[i].start = NULL;
3584 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003585 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003586 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003587 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003588 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003589 }
3590
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003591 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003593 if (save_in_use == -1)
3594 {
3595 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003596 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003597 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003598 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003599 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003601 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 break;
3603
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003604 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003605 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003606 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003607 /* Do not overwrite the position set by \ze. If no \ze
3608 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003609 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 break;
3611 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003612 case NFA_MCLOSE1:
3613 case NFA_MCLOSE2:
3614 case NFA_MCLOSE3:
3615 case NFA_MCLOSE4:
3616 case NFA_MCLOSE5:
3617 case NFA_MCLOSE6:
3618 case NFA_MCLOSE7:
3619 case NFA_MCLOSE8:
3620 case NFA_MCLOSE9:
3621#ifdef FEAT_SYN_HL
3622 case NFA_ZCLOSE:
3623 case NFA_ZCLOSE1:
3624 case NFA_ZCLOSE2:
3625 case NFA_ZCLOSE3:
3626 case NFA_ZCLOSE4:
3627 case NFA_ZCLOSE5:
3628 case NFA_ZCLOSE6:
3629 case NFA_ZCLOSE7:
3630 case NFA_ZCLOSE8:
3631 case NFA_ZCLOSE9:
3632#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003633 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003635 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003637 sub = &subs->norm;
3638 }
3639#ifdef FEAT_SYN_HL
3640 else if (state->c >= NFA_ZCLOSE)
3641 {
3642 subidx = state->c - NFA_ZCLOSE;
3643 sub = &subs->synt;
3644 }
3645#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003646 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003647 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003648 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003649 sub = &subs->norm;
3650 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003652 /* We don't fill in gaps here, there must have been an MOPEN that
3653 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003654 save_in_use = sub->in_use;
3655 if (sub->in_use <= subidx)
3656 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003657 if (REG_MULTI)
3658 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003659 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003660 if (off == -1)
3661 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003662 sub->list.multi[subidx].end.lnum = reglnum + 1;
3663 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003664 }
3665 else
3666 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003667 sub->list.multi[subidx].end.lnum = reglnum;
3668 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003669 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003670 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 }
3672 else
3673 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003674 save_ptr = sub->list.line[subidx].end;
3675 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003676 }
3677
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003678 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679
3680 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003681 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003682 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003683 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003684 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685 break;
3686 }
3687}
3688
3689/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003690 * Like addstate(), but the new state(s) are put at position "*ip".
3691 * Used for zero-width matches, next state to use is the added one.
3692 * This makes sure the order of states to be tried does not change, which
3693 * matters for alternatives.
3694 */
3695 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003696addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003697 nfa_list_T *l; /* runtime state list */
3698 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003699 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003700 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003701 int *ip;
3702{
3703 int tlen = l->n;
3704 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003705 int listidx = *ip;
3706 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003707
3708 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003709 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003710
Bram Moolenaara2d95102013-06-04 14:23:05 +02003711 /* fill in the "pim" field in the new states */
3712 if (pim != NULL)
3713 for (i = tlen; i < l->n; ++i)
3714 l->t[i].pim = pim;
3715
Bram Moolenaar4b417062013-05-25 20:19:50 +02003716 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003717 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003718 return;
3719
3720 /* re-order to put the new state at the current position */
3721 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003722 if (count == 1)
3723 {
3724 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003725 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003726 }
3727 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003728 {
3729 /* make space for new states, then move them from the
3730 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003731 mch_memmove(&(l->t[listidx + count]),
3732 &(l->t[listidx + 1]),
3733 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3734 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003735 &(l->t[l->n - 1]),
3736 sizeof(nfa_thread_T) * count);
3737 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003738 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003739 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003740}
3741
3742/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003743 * Check character class "class" against current character c.
3744 */
3745 static int
3746check_char_class(class, c)
3747 int class;
3748 int c;
3749{
3750 switch (class)
3751 {
3752 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003753 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754 return OK;
3755 break;
3756 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003757 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003758 return OK;
3759 break;
3760 case NFA_CLASS_BLANK:
3761 if (c == ' ' || c == '\t')
3762 return OK;
3763 break;
3764 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003765 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003766 return OK;
3767 break;
3768 case NFA_CLASS_DIGIT:
3769 if (VIM_ISDIGIT(c))
3770 return OK;
3771 break;
3772 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003773 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774 return OK;
3775 break;
3776 case NFA_CLASS_LOWER:
3777 if (MB_ISLOWER(c))
3778 return OK;
3779 break;
3780 case NFA_CLASS_PRINT:
3781 if (vim_isprintc(c))
3782 return OK;
3783 break;
3784 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003785 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003786 return OK;
3787 break;
3788 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02003789 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003790 return OK;
3791 break;
3792 case NFA_CLASS_UPPER:
3793 if (MB_ISUPPER(c))
3794 return OK;
3795 break;
3796 case NFA_CLASS_XDIGIT:
3797 if (vim_isxdigit(c))
3798 return OK;
3799 break;
3800 case NFA_CLASS_TAB:
3801 if (c == '\t')
3802 return OK;
3803 break;
3804 case NFA_CLASS_RETURN:
3805 if (c == '\r')
3806 return OK;
3807 break;
3808 case NFA_CLASS_BACKSPACE:
3809 if (c == '\b')
3810 return OK;
3811 break;
3812 case NFA_CLASS_ESCAPE:
3813 if (c == '\033')
3814 return OK;
3815 break;
3816
3817 default:
3818 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02003819 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
3820 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003821 }
3822 return FAIL;
3823}
3824
Bram Moolenaar5714b802013-05-28 22:03:20 +02003825static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3826
3827/*
3828 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003829 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003830 */
3831 static int
3832match_backref(sub, subidx, bytelen)
3833 regsub_T *sub; /* pointers to subexpressions */
3834 int subidx;
3835 int *bytelen; /* out: length of match in bytes */
3836{
3837 int len;
3838
3839 if (sub->in_use <= subidx)
3840 {
3841retempty:
3842 /* backref was not set, match an empty string */
3843 *bytelen = 0;
3844 return TRUE;
3845 }
3846
3847 if (REG_MULTI)
3848 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003849 if (sub->list.multi[subidx].start.lnum < 0
3850 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003851 goto retempty;
3852 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003853 len = sub->list.multi[subidx].end.col
3854 - sub->list.multi[subidx].start.col;
3855 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003856 reginput, &len) == 0)
3857 {
3858 *bytelen = len;
3859 return TRUE;
3860 }
3861 }
3862 else
3863 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003864 if (sub->list.line[subidx].start == NULL
3865 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003866 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003867 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3868 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003869 {
3870 *bytelen = len;
3871 return TRUE;
3872 }
3873 }
3874 return FALSE;
3875}
3876
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003877#ifdef FEAT_SYN_HL
3878
3879static int match_zref __ARGS((int subidx, int *bytelen));
3880
3881/*
3882 * Check for a match with \z subexpression "subidx".
3883 * Return TRUE if it matches.
3884 */
3885 static int
3886match_zref(subidx, bytelen)
3887 int subidx;
3888 int *bytelen; /* out: length of match in bytes */
3889{
3890 int len;
3891
3892 cleanup_zsubexpr();
3893 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3894 {
3895 /* backref was not set, match an empty string */
3896 *bytelen = 0;
3897 return TRUE;
3898 }
3899
3900 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3901 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3902 {
3903 *bytelen = len;
3904 return TRUE;
3905 }
3906 return FALSE;
3907}
3908#endif
3909
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003911 * Save list IDs for all NFA states of "prog" into "list".
3912 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003913 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003914 */
3915 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003916nfa_save_listids(prog, list)
3917 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918 int *list;
3919{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003920 int i;
3921 nfa_state_T *p;
3922
3923 /* Order in the list is reverse, it's a bit faster that way. */
3924 p = &prog->state[0];
3925 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003926 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003927 list[i] = p->lastlist[1];
3928 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003929 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003930 }
3931}
3932
3933/*
3934 * Restore list IDs from "list" to all NFA states.
3935 */
3936 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003937nfa_restore_listids(prog, list)
3938 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003939 int *list;
3940{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003941 int i;
3942 nfa_state_T *p;
3943
3944 p = &prog->state[0];
3945 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003946 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003947 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003948 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003949 }
3950}
3951
Bram Moolenaar423532e2013-05-29 21:14:42 +02003952 static int
3953nfa_re_num_cmp(val, op, pos)
3954 long_u val;
3955 int op;
3956 long_u pos;
3957{
3958 if (op == 1) return pos > val;
3959 if (op == 2) return pos < val;
3960 return val == pos;
3961}
3962
Bram Moolenaarf46da702013-06-02 22:37:42 +02003963static 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 +02003964static 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 +02003965
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003966/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003967 * Recursively call nfa_regmatch()
3968 */
3969 static int
3970recursive_regmatch(state, prog, submatch, m, listids)
3971 nfa_state_T *state;
3972 nfa_regprog_T *prog;
3973 regsubs_T *submatch;
3974 regsubs_T *m;
3975 int **listids;
3976{
3977 char_u *save_reginput = reginput;
3978 char_u *save_regline = regline;
3979 int save_reglnum = reglnum;
3980 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003981 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003982 save_se_T *save_nfa_endp = nfa_endp;
3983 save_se_T endpos;
3984 save_se_T *endposp = NULL;
3985 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003986 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003987
Bram Moolenaardecd9542013-06-07 16:31:50 +02003988 if (state->c == NFA_START_INVISIBLE_BEFORE
3989 || state->c == NFA_START_INVISIBLE_BEFORE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02003990 {
3991 /* The recursive match must end at the current position. */
3992 endposp = &endpos;
3993 if (REG_MULTI)
3994 {
3995 endpos.se_u.pos.col = (int)(reginput - regline);
3996 endpos.se_u.pos.lnum = reglnum;
3997 }
3998 else
3999 endpos.se_u.ptr = reginput;
4000
4001 /* Go back the specified number of bytes, or as far as the
4002 * start of the previous line, to try matching "\@<=" or
4003 * not matching "\@<!".
4004 * TODO: This is very inefficient! Would be better to
4005 * first check for a match with what follows. */
4006 if (state->val <= 0)
4007 {
4008 if (REG_MULTI)
4009 {
4010 regline = reg_getline(--reglnum);
4011 if (regline == NULL)
4012 /* can't go before the first line */
4013 regline = reg_getline(++reglnum);
4014 }
4015 reginput = regline;
4016 }
4017 else
4018 {
4019 if (REG_MULTI && (int)(reginput - regline) < state->val)
4020 {
4021 /* Not enough bytes in this line, go to end of
4022 * previous line. */
4023 regline = reg_getline(--reglnum);
4024 if (regline == NULL)
4025 {
4026 /* can't go before the first line */
4027 regline = reg_getline(++reglnum);
4028 reginput = regline;
4029 }
4030 else
4031 reginput = regline + STRLEN(regline);
4032 }
4033 if ((int)(reginput - regline) >= state->val)
4034 {
4035 reginput -= state->val;
4036#ifdef FEAT_MBYTE
4037 if (has_mbyte)
4038 reginput -= mb_head_off(regline, reginput);
4039#endif
4040 }
4041 else
4042 reginput = regline;
4043 }
4044 }
4045
Bram Moolenaarf46da702013-06-02 22:37:42 +02004046#ifdef ENABLE_LOG
4047 if (log_fd != stderr)
4048 fclose(log_fd);
4049 log_fd = NULL;
4050#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004051 /* Have to clear the lastlist field of the NFA nodes, so that
4052 * nfa_regmatch() and addstate() can run properly after recursion. */
4053 if (nfa_ll_index == 1)
4054 {
4055 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4056 * values and clear them. */
4057 if (*listids == NULL)
4058 {
4059 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4060 if (*listids == NULL)
4061 {
4062 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4063 return 0;
4064 }
4065 }
4066 nfa_save_listids(prog, *listids);
4067 need_restore = TRUE;
4068 /* any value of nfa_listid will do */
4069 }
4070 else
4071 {
4072 /* First recursive nfa_regmatch() call, switch to the second lastlist
4073 * entry. Make sure nfa_listid is different from a previous recursive
4074 * call, because some states may still have this ID. */
4075 ++nfa_ll_index;
4076 if (nfa_listid <= nfa_alt_listid)
4077 nfa_listid = nfa_alt_listid;
4078 }
4079
4080 /* Call nfa_regmatch() to check if the current concat matches at this
4081 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004082 nfa_endp = endposp;
4083 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004084
4085 if (need_restore)
4086 nfa_restore_listids(prog, *listids);
4087 else
4088 {
4089 --nfa_ll_index;
4090 nfa_alt_listid = nfa_listid;
4091 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004092
4093 /* restore position in input text */
4094 reginput = save_reginput;
4095 regline = save_regline;
4096 reglnum = save_reglnum;
4097 nfa_match = save_nfa_match;
4098 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004099 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004100
4101#ifdef ENABLE_LOG
4102 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4103 if (log_fd != NULL)
4104 {
4105 fprintf(log_fd, "****************************\n");
4106 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4107 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4108 fprintf(log_fd, "****************************\n");
4109 }
4110 else
4111 {
4112 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4113 log_fd = stderr;
4114 }
4115#endif
4116
4117 return result;
4118}
4119
Bram Moolenaara2d95102013-06-04 14:23:05 +02004120static int failure_chance __ARGS((nfa_state_T *state, int depth));
4121
4122/*
4123 * Estimate the chance of a match with "state" failing.
4124 * NFA_ANY: 1
4125 * specific character: 99
4126 */
4127 static int
4128failure_chance(state, depth)
4129 nfa_state_T *state;
4130 int depth;
4131{
4132 int c = state->c;
4133 int l, r;
4134
4135 /* detect looping */
4136 if (depth > 4)
4137 return 1;
4138
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004139 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004140 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004141 case NFA_SPLIT:
4142 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4143 /* avoid recursive stuff */
4144 return 1;
4145 /* two alternatives, use the lowest failure chance */
4146 l = failure_chance(state->out, depth + 1);
4147 r = failure_chance(state->out1, depth + 1);
4148 return l < r ? l : r;
4149
4150 case NFA_ANY:
4151 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004152 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004153 case NFA_MATCH:
4154 /* empty match works always */
4155 return 0;
4156
4157 case NFA_BOL:
4158 case NFA_EOL:
4159 case NFA_BOF:
4160 case NFA_EOF:
4161 case NFA_NEWL:
4162 return 99;
4163
4164 case NFA_BOW:
4165 case NFA_EOW:
4166 return 90;
4167
4168 case NFA_MOPEN:
4169 case NFA_MOPEN1:
4170 case NFA_MOPEN2:
4171 case NFA_MOPEN3:
4172 case NFA_MOPEN4:
4173 case NFA_MOPEN5:
4174 case NFA_MOPEN6:
4175 case NFA_MOPEN7:
4176 case NFA_MOPEN8:
4177 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004178#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004179 case NFA_ZOPEN:
4180 case NFA_ZOPEN1:
4181 case NFA_ZOPEN2:
4182 case NFA_ZOPEN3:
4183 case NFA_ZOPEN4:
4184 case NFA_ZOPEN5:
4185 case NFA_ZOPEN6:
4186 case NFA_ZOPEN7:
4187 case NFA_ZOPEN8:
4188 case NFA_ZOPEN9:
4189 case NFA_ZCLOSE:
4190 case NFA_ZCLOSE1:
4191 case NFA_ZCLOSE2:
4192 case NFA_ZCLOSE3:
4193 case NFA_ZCLOSE4:
4194 case NFA_ZCLOSE5:
4195 case NFA_ZCLOSE6:
4196 case NFA_ZCLOSE7:
4197 case NFA_ZCLOSE8:
4198 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004199#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004200 case NFA_NOPEN:
4201 case NFA_MCLOSE:
4202 case NFA_MCLOSE1:
4203 case NFA_MCLOSE2:
4204 case NFA_MCLOSE3:
4205 case NFA_MCLOSE4:
4206 case NFA_MCLOSE5:
4207 case NFA_MCLOSE6:
4208 case NFA_MCLOSE7:
4209 case NFA_MCLOSE8:
4210 case NFA_MCLOSE9:
4211 case NFA_NCLOSE:
4212 return failure_chance(state->out, depth + 1);
4213
4214 case NFA_BACKREF1:
4215 case NFA_BACKREF2:
4216 case NFA_BACKREF3:
4217 case NFA_BACKREF4:
4218 case NFA_BACKREF5:
4219 case NFA_BACKREF6:
4220 case NFA_BACKREF7:
4221 case NFA_BACKREF8:
4222 case NFA_BACKREF9:
4223#ifdef FEAT_SYN_HL
4224 case NFA_ZREF1:
4225 case NFA_ZREF2:
4226 case NFA_ZREF3:
4227 case NFA_ZREF4:
4228 case NFA_ZREF5:
4229 case NFA_ZREF6:
4230 case NFA_ZREF7:
4231 case NFA_ZREF8:
4232 case NFA_ZREF9:
4233#endif
4234 /* backreferences don't match in many places */
4235 return 94;
4236
4237 case NFA_LNUM_GT:
4238 case NFA_LNUM_LT:
4239 case NFA_COL_GT:
4240 case NFA_COL_LT:
4241 case NFA_VCOL_GT:
4242 case NFA_VCOL_LT:
4243 case NFA_MARK_GT:
4244 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004245 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004246 /* before/after positions don't match very often */
4247 return 85;
4248
4249 case NFA_LNUM:
4250 return 90;
4251
4252 case NFA_CURSOR:
4253 case NFA_COL:
4254 case NFA_VCOL:
4255 case NFA_MARK:
4256 /* specific positions rarely match */
4257 return 98;
4258
4259 case NFA_COMPOSING:
4260 return 95;
4261
4262 default:
4263 if (c > 0)
4264 /* character match fails often */
4265 return 95;
4266 }
4267
4268 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004269 return 50;
4270}
4271
Bram Moolenaarf46da702013-06-02 22:37:42 +02004272/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004273 * Main matching routine.
4274 *
4275 * Run NFA to determine whether it matches reginput.
4276 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004277 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004278 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004279 * Return TRUE if there is a match, FALSE otherwise.
4280 * Note: Caller must ensure that: start != NULL.
4281 */
4282 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004283nfa_regmatch(prog, start, submatch, m)
4284 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004285 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004286 regsubs_T *submatch;
4287 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004288{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004289 int result;
4290 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004291 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004292 int go_to_nextline = FALSE;
4293 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004294 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004295 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004296 nfa_list_T *thislist;
4297 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004299 nfa_state_T *add_state;
4300 int add_count;
4301 int add_off;
4302 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004303#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004304 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004305
4306 if (debug == NULL)
4307 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004308 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004309 return FALSE;
4310 }
4311#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004312 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004313 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004314
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004315 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004316 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004317 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004318 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004319 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004320 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004321 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004322 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323
4324#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004325 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326 if (log_fd != NULL)
4327 {
4328 fprintf(log_fd, "**********************************\n");
4329 nfa_set_code(start->c);
4330 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4331 abs(start->id), code);
4332 fprintf(log_fd, "**********************************\n");
4333 }
4334 else
4335 {
4336 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4337 log_fd = stderr;
4338 }
4339#endif
4340
4341 thislist = &list[0];
4342 thislist->n = 0;
4343 nextlist = &list[1];
4344 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004345#ifdef ENABLE_LOG
4346 fprintf(log_fd, "(---) STARTSTATE\n");
4347#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004348 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004349 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004351#define ADD_STATE_IF_MATCH(state) \
4352 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004353 add_state = state->out; \
4354 add_off = clen; \
4355 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004356
4357 /*
4358 * Run for each character.
4359 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004360 for (;;)
4361 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004362 int curc;
4363 int clen;
4364
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004365#ifdef FEAT_MBYTE
4366 if (has_mbyte)
4367 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004368 curc = (*mb_ptr2char)(reginput);
4369 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004370 }
4371 else
4372#endif
4373 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004374 curc = *reginput;
4375 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004376 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004377 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004378 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004379 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004380 go_to_nextline = FALSE;
4381 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004382
4383 /* swap lists */
4384 thislist = &list[flag];
4385 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004386 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004387 ++nfa_listid;
4388 thislist->id = nfa_listid;
4389 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004390
Bram Moolenaara2d95102013-06-04 14:23:05 +02004391 pimlist.ga_len = 0;
4392
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393#ifdef ENABLE_LOG
4394 fprintf(log_fd, "------------------------------------------\n");
4395 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004396 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004397 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004398 {
4399 int i;
4400
4401 for (i = 0; i < thislist->n; i++)
4402 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4403 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004404 fprintf(log_fd, "\n");
4405#endif
4406
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004407#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408 fprintf(debug, "\n-------------------\n");
4409#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004410 /*
4411 * If the state lists are empty we can stop.
4412 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004413 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004414 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004415
4416 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004417 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004418 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004419 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004420
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004421#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004422 nfa_set_code(t->state->c);
4423 fprintf(debug, "%s, ", code);
4424#endif
4425#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004426 {
4427 int col;
4428
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004429 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004430 col = -1;
4431 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004432 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004433 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004434 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004435 nfa_set_code(t->state->c);
4436 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4437 abs(t->state->id), (int)t->state->c, code, col);
4438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004439#endif
4440
4441 /*
4442 * Handle the possible codes of the current state.
4443 * The most important is NFA_MATCH.
4444 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004445 add_state = NULL;
4446 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004447 switch (t->state->c)
4448 {
4449 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004450 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004451 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004452 copy_sub(&submatch->norm, &t->subs.norm);
4453#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004454 if (nfa_has_zsubexpr)
4455 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004456#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004457#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004458 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004459#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004460 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004461 * states at this position. When the list of states is going
4462 * to be empty quit without advancing, so that "reginput" is
4463 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004464 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004465 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004466 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004467 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004468
4469 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004470 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02004471 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004472 /*
4473 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004474 * NFA_START_INVISIBLE_BEFORE node.
4475 * They surround a zero-width group, used with "\@=", "\&",
4476 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004477 * If we got here, it means that the current "invisible" group
4478 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004479 * nfa_regmatch(). For a look-behind match only when it ends
4480 * in the position in "nfa_endp".
4481 * Submatches are stored in *m, and used in the parent call.
4482 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004483#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004484 if (nfa_endp != NULL)
4485 {
4486 if (REG_MULTI)
4487 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4488 (int)reglnum,
4489 (int)nfa_endp->se_u.pos.lnum,
4490 (int)(reginput - regline),
4491 nfa_endp->se_u.pos.col);
4492 else
4493 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4494 (int)(reginput - regline),
4495 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004496 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004497#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004498 /* If "nfa_endp" is set it's only a match if it ends at
4499 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004500 if (nfa_endp != NULL && (REG_MULTI
4501 ? (reglnum != nfa_endp->se_u.pos.lnum
4502 || (int)(reginput - regline)
4503 != nfa_endp->se_u.pos.col)
4504 : reginput != nfa_endp->se_u.ptr))
4505 break;
4506
4507 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02004508 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004509 {
4510 copy_sub(&m->norm, &t->subs.norm);
4511#ifdef FEAT_SYN_HL
4512 if (nfa_has_zsubexpr)
4513 copy_sub(&m->synt, &t->subs.synt);
4514#endif
4515 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004516#ifdef ENABLE_LOG
4517 fprintf(log_fd, "Match found:\n");
4518 log_subsexpr(m);
4519#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004520 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004521 break;
4522
4523 case NFA_START_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004524 case NFA_START_INVISIBLE_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004525 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004526 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004527 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004528 nfa_pim_T *pim;
4529 int cout = t->state->out1->out->c;
4530
4531 /* Do it directly when what follows is possibly end of
4532 * match (closing paren).
4533 * Postpone when it is \@<= or \@<!, these are expensive.
4534 * TODO: remove the check for t->pim and check multiple
4535 * where it's used?
4536 * Otherwise first do the one that has the highest chance
4537 * of failing. */
4538 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004539#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004540 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004541#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004542 || cout == NFA_NCLOSE
4543 || t->pim != NULL
4544 || (t->state->c != NFA_START_INVISIBLE_BEFORE
Bram Moolenaardecd9542013-06-07 16:31:50 +02004545 && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG
Bram Moolenaara2d95102013-06-04 14:23:05 +02004546 && failure_chance(t->state->out1->out, 0)
4547 < failure_chance(t->state->out, 0)))
4548 {
4549 /*
4550 * First try matching the invisible match, then what
4551 * follows.
4552 */
4553 result = recursive_regmatch(t->state, prog,
4554 submatch, m, &listids);
4555
Bram Moolenaardecd9542013-06-07 16:31:50 +02004556 /* for \@! and \@<! it is a match when the result is
4557 * FALSE */
4558 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
4559 || t->state->c
4560 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004561 {
4562 /* Copy submatch info from the recursive call */
4563 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004564#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004565 if (nfa_has_zsubexpr)
4566 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004567#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004568
Bram Moolenaara2d95102013-06-04 14:23:05 +02004569 /* t->state->out1 is the corresponding
4570 * END_INVISIBLE node; Add its out to the current
4571 * list (zero-width match). */
4572 addstate_here(thislist, t->state->out1->out,
4573 &t->subs, t->pim, &listidx);
4574 }
4575 }
4576 else
4577 {
4578 /*
4579 * First try matching what follows at the current
4580 * position. Only if a match is found, addstate() is
4581 * called, then verify the invisible match matches.
4582 * Add a nfa_pim_T to the following states, it
4583 * contains info about the invisible match.
4584 */
4585 if (ga_grow(&pimlist, 1) == FAIL)
4586 goto theend;
4587 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4588 ++pimlist.ga_len;
4589 pim->state = t->state;
4590 pim->pim = NULL;
4591 pim->result = NFA_PIM_TODO;
4592
4593 /* t->state->out1 is the corresponding END_INVISIBLE
4594 * node; Add its out to the current list (zero-width
4595 * match). */
4596 addstate_here(thislist, t->state->out1->out, &t->subs,
4597 pim, &listidx);
4598 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004599 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 break;
4601
Bram Moolenaar87953742013-06-05 18:52:40 +02004602 case NFA_START_PATTERN:
4603 /* First try matching the pattern. */
4604 result = recursive_regmatch(t->state, prog,
4605 submatch, m, &listids);
4606 if (result)
4607 {
4608 int bytelen;
4609
4610#ifdef ENABLE_LOG
4611 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4612 log_subsexpr(m);
4613#endif
4614 /* Copy submatch info from the recursive call */
4615 copy_sub_off(&t->subs.norm, &m->norm);
4616#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 Moolenaar87953742013-06-05 18:52:40 +02004619#endif
4620 /* Now we need to skip over the matched text and then
4621 * continue with what follows. */
4622 if (REG_MULTI)
4623 /* TODO: multi-line match */
4624 bytelen = m->norm.list.multi[0].end.col
4625 - (int)(reginput - regline);
4626 else
4627 bytelen = (int)(m->norm.list.line[0].end - reginput);
4628
4629#ifdef ENABLE_LOG
4630 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4631#endif
4632 if (bytelen == 0)
4633 {
4634 /* empty match, output of corresponding
4635 * NFA_END_PATTERN/NFA_SKIP to be used at current
4636 * position */
4637 addstate_here(thislist, t->state->out1->out->out,
4638 &t->subs, t->pim, &listidx);
4639 }
4640 else if (bytelen <= clen)
4641 {
4642 /* match current character, output of corresponding
4643 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02004644 add_state = t->state->out1->out->out;
4645 add_off = clen;
4646 }
4647 else
4648 {
4649 /* skip over the matched characters, set character
4650 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02004651 add_state = t->state->out1->out;
4652 add_off = bytelen;
4653 add_count = bytelen - clen;
4654 }
4655 }
4656 break;
4657
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004658 case NFA_BOL:
4659 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004660 addstate_here(thislist, t->state->out, &t->subs,
4661 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004662 break;
4663
4664 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004665 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004666 addstate_here(thislist, t->state->out, &t->subs,
4667 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004668 break;
4669
4670 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004671 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004672
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004673 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004674 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004675#ifdef FEAT_MBYTE
4676 else if (has_mbyte)
4677 {
4678 int this_class;
4679
4680 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004681 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004683 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004684 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004685 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686 }
4687#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004688 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004689 || (reginput > regline
4690 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02004691 result = FALSE;
4692 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004693 addstate_here(thislist, t->state->out, &t->subs,
4694 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004696
4697 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004698 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004700 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701#ifdef FEAT_MBYTE
4702 else if (has_mbyte)
4703 {
4704 int this_class, prev_class;
4705
4706 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004707 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 prev_class = reg_prev_class();
4709 if (this_class == prev_class
4710 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004711 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004712 }
4713#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004714 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004715 || (reginput[0] != NUL
4716 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02004717 result = FALSE;
4718 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004719 addstate_here(thislist, t->state->out, &t->subs,
4720 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004721 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004722
Bram Moolenaar4b780632013-05-31 22:14:52 +02004723 case NFA_BOF:
4724 if (reglnum == 0 && reginput == regline
4725 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004726 addstate_here(thislist, t->state->out, &t->subs,
4727 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004728 break;
4729
4730 case NFA_EOF:
4731 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004732 addstate_here(thislist, t->state->out, &t->subs,
4733 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004734 break;
4735
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004736#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004737 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004738 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004739 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004740 int len = 0;
4741 nfa_state_T *end;
4742 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004743 int cchars[MAX_MCO];
4744 int ccount = 0;
4745 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004746
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004747 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004748 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004749 if (utf_iscomposing(sta->c))
4750 {
4751 /* Only match composing character(s), ignore base
4752 * character. Used for ".{composing}" and "{composing}"
4753 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004754 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004755 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004756 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004757 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004758 /* If \Z was present, then ignore composing characters.
4759 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004760 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004761 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004762 else
4763 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004764 while (sta->c != NFA_END_COMPOSING)
4765 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004766 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004767
4768 /* Check base character matches first, unless ignored. */
4769 else if (len > 0 || mc == sta->c)
4770 {
4771 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004772 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004773 len += mb_char2len(mc);
4774 sta = sta->out;
4775 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004776
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004777 /* We don't care about the order of composing characters.
4778 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004779 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004780 {
4781 mc = mb_ptr2char(reginput + len);
4782 cchars[ccount++] = mc;
4783 len += mb_char2len(mc);
4784 if (ccount == MAX_MCO)
4785 break;
4786 }
4787
4788 /* Check that each composing char in the pattern matches a
4789 * composing char in the text. We do not check if all
4790 * composing chars are matched. */
4791 result = OK;
4792 while (sta->c != NFA_END_COMPOSING)
4793 {
4794 for (j = 0; j < ccount; ++j)
4795 if (cchars[j] == sta->c)
4796 break;
4797 if (j == ccount)
4798 {
4799 result = FAIL;
4800 break;
4801 }
4802 sta = sta->out;
4803 }
4804 }
4805 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004806 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004807
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004808 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004809 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004810 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004811 }
4812#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004813
4814 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004815 if (curc == NUL && !reg_line_lbr && REG_MULTI
4816 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004817 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004818 go_to_nextline = TRUE;
4819 /* Pass -1 for the offset, which means taking the position
4820 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004821 add_state = t->state->out;
4822 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004823 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004824 else if (curc == '\n' && reg_line_lbr)
4825 {
4826 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004827 add_state = t->state->out;
4828 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004829 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004830 break;
4831
Bram Moolenaar417bad22013-06-07 14:08:30 +02004832 case NFA_START_COLL:
4833 case NFA_START_NEG_COLL:
4834 {
4835 /* What follows is a list of characters, until NFA_END_COLL.
4836 * One of them must match or none of them must match. */
4837 nfa_state_T *state;
4838 int result_if_matched;
4839 int c1, c2;
4840
4841 /* Never match EOL. If it's part of the collection it is added
4842 * as a separate state with an OR. */
4843 if (curc == NUL)
4844 break;
4845
4846 state = t->state->out;
4847 result_if_matched = (t->state->c == NFA_START_COLL);
4848 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004849 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02004850 if (state->c == NFA_END_COLL)
4851 {
4852 result = !result_if_matched;
4853 break;
4854 }
4855 if (state->c == NFA_RANGE_MIN)
4856 {
4857 c1 = state->val;
4858 state = state->out; /* advance to NFA_RANGE_MAX */
4859 c2 = state->val;
4860#ifdef ENABLE_LOG
4861 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
4862 curc, c1, c2);
4863#endif
4864 if (curc >= c1 && curc <= c2)
4865 {
4866 result = result_if_matched;
4867 break;
4868 }
4869 if (ireg_ic)
4870 {
4871 int curc_low = MB_TOLOWER(curc);
4872 int done = FALSE;
4873
4874 for ( ; c1 <= c2; ++c1)
4875 if (MB_TOLOWER(c1) == curc_low)
4876 {
4877 result = result_if_matched;
4878 done = TRUE;
4879 break;
4880 }
4881 if (done)
4882 break;
4883 }
4884 }
4885 else if (state->c < 0 ? check_char_class(state->c, curc)
4886 : (curc == state->c
4887 || (ireg_ic && MB_TOLOWER(curc)
4888 == MB_TOLOWER(state->c))))
4889 {
4890 result = result_if_matched;
4891 break;
4892 }
4893 state = state->out;
4894 }
4895 if (result)
4896 {
4897 /* next state is in out of the NFA_END_COLL, out1 of
4898 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004899 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004900 add_off = clen;
4901 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004902 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02004903 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004904
4905 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004906 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004907 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004908 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004909 add_state = t->state->out;
4910 add_off = clen;
4911 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004912 break;
4913
4914 /*
4915 * Character classes like \a for alpha, \d for digit etc.
4916 */
4917 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004918 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004919 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920 break;
4921
4922 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004923 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004924 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925 break;
4926
4927 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004928 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004929 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004930 break;
4931
4932 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004933 result = !VIM_ISDIGIT(curc)
4934 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004935 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004936 break;
4937
4938 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004939 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004940 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004941 break;
4942
4943 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004944 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004945 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946 break;
4947
4948 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004949 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004950 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004951 break;
4952
4953 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004954 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004955 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004956 break;
4957
4958 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004959 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004960 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004961 break;
4962
4963 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004964 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004965 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004966 break;
4967
4968 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004969 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004970 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004971 break;
4972
4973 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004974 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004975 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004976 break;
4977
4978 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004979 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004980 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004981 break;
4982
4983 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004984 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004985 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004986 break;
4987
4988 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004989 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004990 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004991 break;
4992
4993 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004994 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004995 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004996 break;
4997
4998 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004999 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005000 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005001 break;
5002
5003 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005004 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005005 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005006 break;
5007
5008 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005009 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005010 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005011 break;
5012
5013 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005014 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005015 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005016 break;
5017
5018 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005019 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005020 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005021 break;
5022
5023 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005024 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005025 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005026 break;
5027
5028 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005029 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005030 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005031 break;
5032
5033 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005034 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005035 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005036 break;
5037
5038 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005039 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005040 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005041 break;
5042
5043 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005044 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005045 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005046 break;
5047
Bram Moolenaar5714b802013-05-28 22:03:20 +02005048 case NFA_BACKREF1:
5049 case NFA_BACKREF2:
5050 case NFA_BACKREF3:
5051 case NFA_BACKREF4:
5052 case NFA_BACKREF5:
5053 case NFA_BACKREF6:
5054 case NFA_BACKREF7:
5055 case NFA_BACKREF8:
5056 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005057#ifdef FEAT_SYN_HL
5058 case NFA_ZREF1:
5059 case NFA_ZREF2:
5060 case NFA_ZREF3:
5061 case NFA_ZREF4:
5062 case NFA_ZREF5:
5063 case NFA_ZREF6:
5064 case NFA_ZREF7:
5065 case NFA_ZREF8:
5066 case NFA_ZREF9:
5067#endif
5068 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005069 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005070 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005071 int bytelen;
5072
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005073 if (t->state->c <= NFA_BACKREF9)
5074 {
5075 subidx = t->state->c - NFA_BACKREF1 + 1;
5076 result = match_backref(&t->subs.norm, subidx, &bytelen);
5077 }
5078#ifdef FEAT_SYN_HL
5079 else
5080 {
5081 subidx = t->state->c - NFA_ZREF1 + 1;
5082 result = match_zref(subidx, &bytelen);
5083 }
5084#endif
5085
Bram Moolenaar5714b802013-05-28 22:03:20 +02005086 if (result)
5087 {
5088 if (bytelen == 0)
5089 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005090 /* empty match always works, output of NFA_SKIP to be
5091 * used next */
5092 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005093 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02005094 }
5095 else if (bytelen <= clen)
5096 {
5097 /* match current character, jump ahead to out of
5098 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005099 add_state = t->state->out->out;
5100 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005101 }
5102 else
5103 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005104 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005105 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005106 add_state = t->state->out;
5107 add_off = bytelen;
5108 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005109 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005110 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005111 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005112 }
5113 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005114 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005115 if (t->count - clen <= 0)
5116 {
5117 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005118 add_state = t->state->out;
5119 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005120 }
5121 else
5122 {
5123 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005124 add_state = t->state;
5125 add_off = 0;
5126 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005127 }
5128 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005129
Bram Moolenaar423532e2013-05-29 21:14:42 +02005130 case NFA_LNUM:
5131 case NFA_LNUM_GT:
5132 case NFA_LNUM_LT:
5133 result = (REG_MULTI &&
5134 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5135 (long_u)(reglnum + reg_firstlnum)));
5136 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005137 addstate_here(thislist, t->state->out, &t->subs,
5138 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005139 break;
5140
5141 case NFA_COL:
5142 case NFA_COL_GT:
5143 case NFA_COL_LT:
5144 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5145 (long_u)(reginput - regline) + 1);
5146 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005147 addstate_here(thislist, t->state->out, &t->subs,
5148 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005149 break;
5150
5151 case NFA_VCOL:
5152 case NFA_VCOL_GT:
5153 case NFA_VCOL_LT:
5154 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5155 (long_u)win_linetabsize(
5156 reg_win == NULL ? curwin : reg_win,
5157 regline, (colnr_T)(reginput - regline)) + 1);
5158 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005159 addstate_here(thislist, t->state->out, &t->subs,
5160 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005161 break;
5162
Bram Moolenaar044aa292013-06-04 21:27:38 +02005163 case NFA_MARK:
5164 case NFA_MARK_GT:
5165 case NFA_MARK_LT:
5166 {
5167 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5168
5169 /* Compare the mark position to the match position. */
5170 result = (pos != NULL /* mark doesn't exist */
5171 && pos->lnum > 0 /* mark isn't set in reg_buf */
5172 && (pos->lnum == reglnum + reg_firstlnum
5173 ? (pos->col == (colnr_T)(reginput - regline)
5174 ? t->state->c == NFA_MARK
5175 : (pos->col < (colnr_T)(reginput - regline)
5176 ? t->state->c == NFA_MARK_GT
5177 : t->state->c == NFA_MARK_LT))
5178 : (pos->lnum < reglnum + reg_firstlnum
5179 ? t->state->c == NFA_MARK_GT
5180 : t->state->c == NFA_MARK_LT)));
5181 if (result)
5182 addstate_here(thislist, t->state->out, &t->subs,
5183 t->pim, &listidx);
5184 break;
5185 }
5186
Bram Moolenaar423532e2013-05-29 21:14:42 +02005187 case NFA_CURSOR:
5188 result = (reg_win != NULL
5189 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5190 && ((colnr_T)(reginput - regline)
5191 == reg_win->w_cursor.col));
5192 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005193 addstate_here(thislist, t->state->out, &t->subs,
5194 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005195 break;
5196
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005197 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005198#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005199 result = reg_match_visual();
5200 if (result)
5201 addstate_here(thislist, t->state->out, &t->subs,
5202 t->pim, &listidx);
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005203#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005204 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005205
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005206 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005207 {
5208 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005209
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005210 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005211 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005212 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005213 result = (c == curc);
5214
5215 if (!result && ireg_ic)
5216 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005217#ifdef FEAT_MBYTE
5218 /* If there is a composing character which is not being
5219 * ignored there can be no match. Match with composing
5220 * character uses NFA_COMPOSING above. */
5221 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005222 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005223 result = FALSE;
5224#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005225 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005226 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005227 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005228
5229 } /* switch (t->state->c) */
5230
5231 if (add_state != NULL)
5232 {
5233 if (t->pim != NULL)
5234 {
5235 /* postponed invisible match */
5236 /* TODO: also do t->pim->pim recursively? */
5237 if (t->pim->result == NFA_PIM_TODO)
5238 {
5239#ifdef ENABLE_LOG
5240 fprintf(log_fd, "\n");
5241 fprintf(log_fd, "==================================\n");
5242 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5243 fprintf(log_fd, "\n");
5244#endif
5245 result = recursive_regmatch(t->pim->state,
5246 prog, submatch, m, &listids);
5247 t->pim->result = result ? NFA_PIM_MATCH
5248 : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005249 /* for \@! and \@<! it is a match when the result is
5250 * FALSE */
5251 if (result != (t->pim->state->c
5252 == NFA_START_INVISIBLE_NEG
5253 || t->pim->state->c
5254 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005255 {
5256 /* Copy submatch info from the recursive call */
5257 copy_sub_off(&t->pim->subs.norm, &m->norm);
5258#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005259 if (nfa_has_zsubexpr)
5260 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005261#endif
5262 }
5263 }
5264 else
5265 {
5266 result = (t->pim->result == NFA_PIM_MATCH);
5267#ifdef ENABLE_LOG
5268 fprintf(log_fd, "\n");
5269 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5270 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5271 fprintf(log_fd, "\n");
5272#endif
5273 }
5274
Bram Moolenaardecd9542013-06-07 16:31:50 +02005275 /* for \@! and \@<! it is a match when result is FALSE */
5276 if (result != (t->pim->state->c == NFA_START_INVISIBLE_NEG
5277 || t->pim->state->c
5278 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005279 {
5280 /* Copy submatch info from the recursive call */
5281 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5282#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005283 if (nfa_has_zsubexpr)
5284 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005285#endif
5286 }
5287 else
5288 /* look-behind match failed, don't add the state */
5289 continue;
5290 }
5291
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005292 addstate(nextlist, add_state, &t->subs, add_off);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005293 if (add_count > 0)
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005294 nextlist->t[nextlist->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005295 }
5296
5297 } /* for (thislist = thislist; thislist->state; thislist++) */
5298
Bram Moolenaare23febd2013-05-26 18:40:14 +02005299 /* Look for the start of a match in the current position by adding the
5300 * start state to the list of states.
5301 * The first found match is the leftmost one, thus the order of states
5302 * matters!
5303 * Do not add the start state in recursive calls of nfa_regmatch(),
5304 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005305 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005306 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005307 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005308 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005309 && reglnum == 0
5310 && clen != 0
5311 && (ireg_maxcol == 0
5312 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005313 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005314 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005315 ? (reglnum < nfa_endp->se_u.pos.lnum
5316 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005317 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005318 < nfa_endp->se_u.pos.col))
5319 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005320 {
5321#ifdef ENABLE_LOG
5322 fprintf(log_fd, "(---) STARTSTATE\n");
5323#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005324 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005325 }
5326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005327#ifdef ENABLE_LOG
5328 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005329 {
5330 int i;
5331
5332 for (i = 0; i < thislist->n; i++)
5333 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5334 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005335 fprintf(log_fd, "\n");
5336#endif
5337
5338nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005339 /* Advance to the next character, or advance to the next line, or
5340 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005341 if (clen != 0)
5342 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005343 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5344 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005345 reg_nextline();
5346 else
5347 break;
5348 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005349
5350#ifdef ENABLE_LOG
5351 if (log_fd != stderr)
5352 fclose(log_fd);
5353 log_fd = NULL;
5354#endif
5355
5356theend:
5357 /* Free memory */
5358 vim_free(list[0].t);
5359 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005360 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005361 ga_clear(&pimlist);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005362#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005363#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005364 fclose(debug);
5365#endif
5366
Bram Moolenaar963fee22013-05-26 21:47:28 +02005367 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005368}
5369
5370/*
5371 * Try match of "prog" with at regline["col"].
5372 * Returns 0 for failure, number of lines contained in the match otherwise.
5373 */
5374 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005375nfa_regtry(prog, col)
5376 nfa_regprog_T *prog;
5377 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005378{
5379 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005380 regsubs_T subs, m;
5381 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005382#ifdef ENABLE_LOG
5383 FILE *f;
5384#endif
5385
5386 reginput = regline + col;
5387 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005388#ifdef FEAT_SYN_HL
5389 /* Clear the external match subpointers if necessary. */
5390 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005391 {
5392 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005393 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005394 }
5395 else
5396 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005397#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005398
5399#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005400 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005401 if (f != NULL)
5402 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005403 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005404#ifdef DEBUG
5405 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5406#endif
5407 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005408 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005409 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 fprintf(f, "\n\n");
5411 fclose(f);
5412 }
5413 else
5414 EMSG(_("Could not open temporary log file for writing "));
5415#endif
5416
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005417 clear_sub(&subs.norm);
5418 clear_sub(&m.norm);
5419#ifdef FEAT_SYN_HL
5420 clear_sub(&subs.synt);
5421 clear_sub(&m.synt);
5422#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005423
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005424 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005425 return 0;
5426
5427 cleanup_subexpr();
5428 if (REG_MULTI)
5429 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005430 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005431 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005432 reg_startpos[i] = subs.norm.list.multi[i].start;
5433 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005434 }
5435
5436 if (reg_startpos[0].lnum < 0)
5437 {
5438 reg_startpos[0].lnum = 0;
5439 reg_startpos[0].col = col;
5440 }
5441 if (reg_endpos[0].lnum < 0)
5442 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005443 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444 reg_endpos[0].lnum = reglnum;
5445 reg_endpos[0].col = (int)(reginput - regline);
5446 }
5447 else
5448 /* Use line number of "\ze". */
5449 reglnum = reg_endpos[0].lnum;
5450 }
5451 else
5452 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005453 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005454 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005455 reg_startp[i] = subs.norm.list.line[i].start;
5456 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005457 }
5458
5459 if (reg_startp[0] == NULL)
5460 reg_startp[0] = regline + col;
5461 if (reg_endp[0] == NULL)
5462 reg_endp[0] = reginput;
5463 }
5464
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005465#ifdef FEAT_SYN_HL
5466 /* Package any found \z(...\) matches for export. Default is none. */
5467 unref_extmatch(re_extmatch_out);
5468 re_extmatch_out = NULL;
5469
5470 if (prog->reghasz == REX_SET)
5471 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005472 cleanup_zsubexpr();
5473 re_extmatch_out = make_extmatch();
5474 for (i = 0; i < subs.synt.in_use; i++)
5475 {
5476 if (REG_MULTI)
5477 {
5478 struct multipos *mpos = &subs.synt.list.multi[i];
5479
5480 /* Only accept single line matches. */
5481 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5482 re_extmatch_out->matches[i] =
5483 vim_strnsave(reg_getline(mpos->start.lnum)
5484 + mpos->start.col,
5485 mpos->end.col - mpos->start.col);
5486 }
5487 else
5488 {
5489 struct linepos *lpos = &subs.synt.list.line[i];
5490
5491 if (lpos->start != NULL && lpos->end != NULL)
5492 re_extmatch_out->matches[i] =
5493 vim_strnsave(lpos->start,
5494 (int)(lpos->end - lpos->start));
5495 }
5496 }
5497 }
5498#endif
5499
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005500 return 1 + reglnum;
5501}
5502
5503/*
5504 * Match a regexp against a string ("line" points to the string) or multiple
5505 * lines ("line" is NULL, use reg_getline()).
5506 *
5507 * Returns 0 for failure, number of lines contained in the match otherwise.
5508 */
5509 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02005510nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005511 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005512 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513{
5514 nfa_regprog_T *prog;
5515 long retval = 0L;
5516 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005517 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518
5519 if (REG_MULTI)
5520 {
5521 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5522 line = reg_getline((linenr_T)0); /* relative to the cursor */
5523 reg_startpos = reg_mmatch->startpos;
5524 reg_endpos = reg_mmatch->endpos;
5525 }
5526 else
5527 {
5528 prog = (nfa_regprog_T *)reg_match->regprog;
5529 reg_startp = reg_match->startp;
5530 reg_endp = reg_match->endp;
5531 }
5532
5533 /* Be paranoid... */
5534 if (prog == NULL || line == NULL)
5535 {
5536 EMSG(_(e_null));
5537 goto theend;
5538 }
5539
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005540 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5541 if (prog->regflags & RF_ICASE)
5542 ireg_ic = TRUE;
5543 else if (prog->regflags & RF_NOICASE)
5544 ireg_ic = FALSE;
5545
5546#ifdef FEAT_MBYTE
5547 /* If pattern contains "\Z" overrule value of ireg_icombine */
5548 if (prog->regflags & RF_ICOMBINE)
5549 ireg_icombine = TRUE;
5550#endif
5551
5552 regline = line;
5553 reglnum = 0; /* relative to line */
5554
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005555 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005556 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005557 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005558 nfa_listid = 1;
5559 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005560#ifdef DEBUG
5561 nfa_regengine.expr = prog->pattern;
5562#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563
Bram Moolenaard89616e2013-06-06 18:46:06 +02005564 if (prog->reganch && col > 0)
5565 return 0L;
5566
5567 if (prog->regstart != NUL)
5568 {
5569 char_u *s;
5570
5571 /* Skip until the char we know it must start with.
5572 * Used often, do some work to avoid call overhead. */
5573 if (!ireg_ic
5574#ifdef FEAT_MBYTE
5575 && !has_mbyte
5576#endif
5577 )
5578 s = vim_strbyte(regline + col, prog->regstart);
5579 else
5580 s = cstrchr(regline + col, prog->regstart);
5581 if (s == NULL)
5582 return 0L;
5583 col = (int)(s - regline);
5584 }
5585
5586 /* If the start column is past the maximum column: no need to try. */
5587 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5588 goto theend;
5589
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005590 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 for (i = 0; i < nstate; ++i)
5592 {
5593 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005594 prog->state[i].lastlist[0] = 0;
5595 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005596 }
5597
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005598 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005600#ifdef DEBUG
5601 nfa_regengine.expr = NULL;
5602#endif
5603
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005604theend:
5605 return retval;
5606}
5607
5608/*
5609 * Compile a regular expression into internal code for the NFA matcher.
5610 * Returns the program in allocated space. Returns NULL for an error.
5611 */
5612 static regprog_T *
5613nfa_regcomp(expr, re_flags)
5614 char_u *expr;
5615 int re_flags;
5616{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005617 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005618 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005619 int *postfix;
5620
5621 if (expr == NULL)
5622 return NULL;
5623
5624#ifdef DEBUG
5625 nfa_regengine.expr = expr;
5626#endif
5627
5628 init_class_tab();
5629
5630 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5631 return NULL;
5632
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005633 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005634 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005635 postfix = re2post();
5636 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005637 {
5638 /* TODO: only give this error for debugging? */
5639 if (post_ptr >= post_end)
5640 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005642 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005643
5644 /*
5645 * In order to build the NFA, we parse the input regexp twice:
5646 * 1. first pass to count size (so we can allocate space)
5647 * 2. second to emit code
5648 */
5649#ifdef ENABLE_LOG
5650 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005651 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005652
5653 if (f != NULL)
5654 {
5655 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5656 fclose(f);
5657 }
5658 }
5659#endif
5660
5661 /*
5662 * PASS 1
5663 * Count number of NFA states in "nstate". Do not build the NFA.
5664 */
5665 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005666
5667 /* Space for compiled regexp */
5668 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5669 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5670 if (prog == NULL)
5671 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005672 state_ptr = prog->state;
5673
5674 /*
5675 * PASS 2
5676 * Build the NFA
5677 */
5678 prog->start = post2nfa(postfix, post_ptr, FALSE);
5679 if (prog->start == NULL)
5680 goto fail;
5681
5682 prog->regflags = regflags;
5683 prog->engine = &nfa_regengine;
5684 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005685 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005686 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005687 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005688
5689 prog->reganch = nfa_get_reganch(prog->start, 0);
5690 prog->regstart = nfa_get_regstart(prog->start, 0);
5691
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005692#ifdef ENABLE_LOG
5693 nfa_postfix_dump(expr, OK);
5694 nfa_dump(prog);
5695#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005696#ifdef FEAT_SYN_HL
5697 /* Remember whether this pattern has any \z specials in it. */
5698 prog->reghasz = re_has_z;
5699#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005700#ifdef DEBUG
5701 prog->pattern = vim_strsave(expr); /* memory will leak */
5702 nfa_regengine.expr = NULL;
5703#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005704
5705out:
5706 vim_free(post_start);
5707 post_start = post_ptr = post_end = NULL;
5708 state_ptr = NULL;
5709 return (regprog_T *)prog;
5710
5711fail:
5712 vim_free(prog);
5713 prog = NULL;
5714#ifdef ENABLE_LOG
5715 nfa_postfix_dump(expr, FAIL);
5716#endif
5717#ifdef DEBUG
5718 nfa_regengine.expr = NULL;
5719#endif
5720 goto out;
5721}
5722
5723
5724/*
5725 * Match a regexp against a string.
5726 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5727 * Uses curbuf for line count and 'iskeyword'.
5728 *
5729 * Return TRUE if there is a match, FALSE if not.
5730 */
5731 static int
5732nfa_regexec(rmp, line, col)
5733 regmatch_T *rmp;
5734 char_u *line; /* string to match against */
5735 colnr_T col; /* column to start looking for match */
5736{
5737 reg_match = rmp;
5738 reg_mmatch = NULL;
5739 reg_maxline = 0;
5740 reg_line_lbr = FALSE;
5741 reg_buf = curbuf;
5742 reg_win = NULL;
5743 ireg_ic = rmp->rm_ic;
5744#ifdef FEAT_MBYTE
5745 ireg_icombine = FALSE;
5746#endif
5747 ireg_maxcol = 0;
5748 return (nfa_regexec_both(line, col) != 0);
5749}
5750
5751#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5752 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5753
5754static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5755
5756/*
5757 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5758 */
5759 static int
5760nfa_regexec_nl(rmp, line, col)
5761 regmatch_T *rmp;
5762 char_u *line; /* string to match against */
5763 colnr_T col; /* column to start looking for match */
5764{
5765 reg_match = rmp;
5766 reg_mmatch = NULL;
5767 reg_maxline = 0;
5768 reg_line_lbr = TRUE;
5769 reg_buf = curbuf;
5770 reg_win = NULL;
5771 ireg_ic = rmp->rm_ic;
5772#ifdef FEAT_MBYTE
5773 ireg_icombine = FALSE;
5774#endif
5775 ireg_maxcol = 0;
5776 return (nfa_regexec_both(line, col) != 0);
5777}
5778#endif
5779
5780
5781/*
5782 * Match a regexp against multiple lines.
5783 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5784 * Uses curbuf for line count and 'iskeyword'.
5785 *
5786 * Return zero if there is no match. Return number of lines contained in the
5787 * match otherwise.
5788 *
5789 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5790 *
5791 * ! Also NOTE : match may actually be in another line. e.g.:
5792 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5793 *
5794 * +-------------------------+
5795 * |a |
5796 * |b |
5797 * |c |
5798 * | |
5799 * +-------------------------+
5800 *
5801 * then nfa_regexec_multi() returns 3. while the original
5802 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5803 *
5804 * FIXME if this behavior is not compatible.
5805 */
5806 static long
5807nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5808 regmmatch_T *rmp;
5809 win_T *win; /* window in which to search or NULL */
5810 buf_T *buf; /* buffer in which to search */
5811 linenr_T lnum; /* nr of line to start looking for match */
5812 colnr_T col; /* column to start looking for match */
5813 proftime_T *tm UNUSED; /* timeout limit or NULL */
5814{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005815 reg_match = NULL;
5816 reg_mmatch = rmp;
5817 reg_buf = buf;
5818 reg_win = win;
5819 reg_firstlnum = lnum;
5820 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5821 reg_line_lbr = FALSE;
5822 ireg_ic = rmp->rmm_ic;
5823#ifdef FEAT_MBYTE
5824 ireg_icombine = FALSE;
5825#endif
5826 ireg_maxcol = rmp->rmm_maxcol;
5827
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005828 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005829}
5830
5831#ifdef DEBUG
5832# undef ENABLE_LOG
5833#endif