blob: 4fbafcd9814e13322ee058363878e0b3a42701a6 [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 */
37 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
38
39 NFA_CONCAT,
40 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020041 NFA_STAR, /* greedy * */
42 NFA_STAR_NONGREEDY, /* non-greedy * */
43 NFA_QUEST, /* greedy \? */
44 NFA_QUEST_NONGREEDY, /* non-greedy \? */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020045 NFA_NOT, /* used for [^ab] negated char ranges */
46
47 NFA_BOL, /* ^ Begin line */
48 NFA_EOL, /* $ End line */
49 NFA_BOW, /* \< Begin word */
50 NFA_EOW, /* \> End word */
51 NFA_BOF, /* \%^ Begin file */
52 NFA_EOF, /* \%$ End file */
53 NFA_NEWL,
54 NFA_ZSTART, /* Used for \zs */
55 NFA_ZEND, /* Used for \ze */
56 NFA_NOPEN, /* Start of subexpression marked with \%( */
57 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
58 NFA_START_INVISIBLE,
Bram Moolenaar61602c52013-06-01 19:54:43 +020059 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020060 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020061 NFA_COMPOSING, /* Next nodes in NFA are part of the
62 composing multibyte char */
63 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020064 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020065
66 /* The following are used only in the postfix form, not in the NFA */
67 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
68 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
69 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
70 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
71 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
72
Bram Moolenaar5714b802013-05-28 22:03:20 +020073 NFA_BACKREF1, /* \1 */
74 NFA_BACKREF2, /* \2 */
75 NFA_BACKREF3, /* \3 */
76 NFA_BACKREF4, /* \4 */
77 NFA_BACKREF5, /* \5 */
78 NFA_BACKREF6, /* \6 */
79 NFA_BACKREF7, /* \7 */
80 NFA_BACKREF8, /* \8 */
81 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020082#ifdef FEAT_SYN_HL
83 NFA_ZREF1, /* \z1 */
84 NFA_ZREF2, /* \z2 */
85 NFA_ZREF3, /* \z3 */
86 NFA_ZREF4, /* \z4 */
87 NFA_ZREF5, /* \z5 */
88 NFA_ZREF6, /* \z6 */
89 NFA_ZREF7, /* \z7 */
90 NFA_ZREF8, /* \z8 */
91 NFA_ZREF9, /* \z9 */
92#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +020093 NFA_SKIP, /* Skip characters */
94
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020095 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +020096 NFA_MOPEN1,
97 NFA_MOPEN2,
98 NFA_MOPEN3,
99 NFA_MOPEN4,
100 NFA_MOPEN5,
101 NFA_MOPEN6,
102 NFA_MOPEN7,
103 NFA_MOPEN8,
104 NFA_MOPEN9,
105
106 NFA_MCLOSE,
107 NFA_MCLOSE1,
108 NFA_MCLOSE2,
109 NFA_MCLOSE3,
110 NFA_MCLOSE4,
111 NFA_MCLOSE5,
112 NFA_MCLOSE6,
113 NFA_MCLOSE7,
114 NFA_MCLOSE8,
115 NFA_MCLOSE9,
116
117#ifdef FEAT_SYN_HL
118 NFA_ZOPEN,
119 NFA_ZOPEN1,
120 NFA_ZOPEN2,
121 NFA_ZOPEN3,
122 NFA_ZOPEN4,
123 NFA_ZOPEN5,
124 NFA_ZOPEN6,
125 NFA_ZOPEN7,
126 NFA_ZOPEN8,
127 NFA_ZOPEN9,
128
129 NFA_ZCLOSE,
130 NFA_ZCLOSE1,
131 NFA_ZCLOSE2,
132 NFA_ZCLOSE3,
133 NFA_ZCLOSE4,
134 NFA_ZCLOSE5,
135 NFA_ZCLOSE6,
136 NFA_ZCLOSE7,
137 NFA_ZCLOSE8,
138 NFA_ZCLOSE9,
139#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200140
141 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200142 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200143 NFA_ANYOF, /* Match any character in this string. */
144 NFA_ANYBUT, /* Match any character not in this string. */
145 NFA_IDENT, /* Match identifier char */
146 NFA_SIDENT, /* Match identifier char but no digit */
147 NFA_KWORD, /* Match keyword char */
148 NFA_SKWORD, /* Match word char but no digit */
149 NFA_FNAME, /* Match file name char */
150 NFA_SFNAME, /* Match file name char but no digit */
151 NFA_PRINT, /* Match printable char */
152 NFA_SPRINT, /* Match printable char but no digit */
153 NFA_WHITE, /* Match whitespace char */
154 NFA_NWHITE, /* Match non-whitespace char */
155 NFA_DIGIT, /* Match digit char */
156 NFA_NDIGIT, /* Match non-digit char */
157 NFA_HEX, /* Match hex char */
158 NFA_NHEX, /* Match non-hex char */
159 NFA_OCTAL, /* Match octal char */
160 NFA_NOCTAL, /* Match non-octal char */
161 NFA_WORD, /* Match word char */
162 NFA_NWORD, /* Match non-word char */
163 NFA_HEAD, /* Match head char */
164 NFA_NHEAD, /* Match non-head char */
165 NFA_ALPHA, /* Match alpha char */
166 NFA_NALPHA, /* Match non-alpha char */
167 NFA_LOWER, /* Match lowercase char */
168 NFA_NLOWER, /* Match non-lowercase char */
169 NFA_UPPER, /* Match uppercase char */
170 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200171
172 NFA_CURSOR, /* Match cursor pos */
173 NFA_LNUM, /* Match line number */
174 NFA_LNUM_GT, /* Match > line number */
175 NFA_LNUM_LT, /* Match < line number */
176 NFA_COL, /* Match cursor column */
177 NFA_COL_GT, /* Match > cursor column */
178 NFA_COL_LT, /* Match < cursor column */
179 NFA_VCOL, /* Match cursor virtual column */
180 NFA_VCOL_GT, /* Match > cursor virtual column */
181 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200182 NFA_MARK, /* Match mark */
183 NFA_MARK_GT, /* Match > mark */
184 NFA_MARK_LT, /* Match < mark */
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200185#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200186 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200187#endif
Bram Moolenaar423532e2013-05-29 21:14:42 +0200188
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200189 NFA_FIRST_NL = NFA_ANY + ADD_NL,
190 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
191
192 /* Character classes [:alnum:] etc */
193 NFA_CLASS_ALNUM,
194 NFA_CLASS_ALPHA,
195 NFA_CLASS_BLANK,
196 NFA_CLASS_CNTRL,
197 NFA_CLASS_DIGIT,
198 NFA_CLASS_GRAPH,
199 NFA_CLASS_LOWER,
200 NFA_CLASS_PRINT,
201 NFA_CLASS_PUNCT,
202 NFA_CLASS_SPACE,
203 NFA_CLASS_UPPER,
204 NFA_CLASS_XDIGIT,
205 NFA_CLASS_TAB,
206 NFA_CLASS_RETURN,
207 NFA_CLASS_BACKSPACE,
208 NFA_CLASS_ESCAPE
209};
210
211/* Keep in sync with classchars. */
212static int nfa_classcodes[] = {
213 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
214 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
215 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
216 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
217 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
218 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
219 NFA_UPPER, NFA_NUPPER
220};
221
222static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
223
224/*
225 * NFA errors can be of 3 types:
226 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
227 * silently and revert the to backtracking engine.
228 * syntax_error = FALSE;
229 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
230 * The NFA engine displays an error message, and nothing else happens.
231 * syntax_error = TRUE
232 * *** Unsupported features, when the input regexp uses an operator that is not
233 * implemented in the NFA. The NFA engine fails silently, and reverts to the
234 * old backtracking engine.
235 * syntax_error = FALSE
236 * "The NFA fails" means that "compiling the regexp with the NFA fails":
237 * nfa_regcomp() returns FAIL.
238 */
239static int syntax_error = FALSE;
240
241/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200242static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200243
Bram Moolenaar428e9872013-05-30 17:05:39 +0200244/* NFA regexp \1 .. \9 encountered. */
245static int nfa_has_backref;
246
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200247#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200248/* NFA regexp has \z( ), set zsubexpr. */
249static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200250#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200251
Bram Moolenaar963fee22013-05-26 21:47:28 +0200252/* Number of sub expressions actually being used during execution. 1 if only
253 * the whole match (subexpr 0) is used. */
254static int nfa_nsubexpr;
255
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200256static int *post_start; /* holds the postfix form of r.e. */
257static int *post_end;
258static int *post_ptr;
259
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200260static int nstate; /* Number of states in the NFA. Also used when
261 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200262static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200263
Bram Moolenaar307aa162013-06-02 16:34:21 +0200264/* If not NULL match must end at this position */
265static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200267/* listid is global, so that it increases on recursive calls to
268 * nfa_regmatch(), which means we don't have to clear the lastlist field of
269 * all the states. */
270static int nfa_listid;
271static int nfa_alt_listid;
272
273/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
274static int nfa_ll_index = 0;
275
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
277static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
278static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279static int nfa_regatom __ARGS((void));
280static int nfa_regpiece __ARGS((void));
281static int nfa_regconcat __ARGS((void));
282static int nfa_regbranch __ARGS((void));
283static int nfa_reg __ARGS((int paren));
284#ifdef DEBUG
285static void nfa_set_code __ARGS((int c));
286static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200287static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
288static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289static void nfa_dump __ARGS((nfa_regprog_T *prog));
290#endif
291static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200292static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200293static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
294static int check_char_class __ARGS((int class, int c));
295static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200296static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
297static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200298static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200299static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
301static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
302static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
303static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
304
305/* helper functions used when doing re2post() ... regatom() parsing */
306#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200307 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308 return FAIL; \
309 *post_ptr++ = c; \
310 } while (0)
311
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312/*
313 * Initialize internal variables before NFA compilation.
314 * Return OK on success, FAIL otherwise.
315 */
316 static int
317nfa_regcomp_start(expr, re_flags)
318 char_u *expr;
319 int re_flags; /* see vim_regcomp() */
320{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200321 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200322 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200323
324 nstate = 0;
325 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200326 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200327 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200329 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200330 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200331 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200332
333 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200334 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200335
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200336 post_start = (int *)lalloc(postfix_size, TRUE);
337 if (post_start == NULL)
338 return FAIL;
339 vim_memset(post_start, 0, postfix_size);
340 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200341 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200342 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200343 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200345 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346 regcomp_start(expr, re_flags);
347
348 return OK;
349}
350
351/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200352 * Allocate more space for post_start. Called when
353 * running above the estimated number of states.
354 */
355 static int
356realloc_post_list()
357{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200358 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200359 int new_max = nstate_max + 1000;
360 int *new_start;
361 int *old_start;
362
363 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
364 if (new_start == NULL)
365 return FAIL;
366 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
367 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
368 old_start = post_start;
369 post_start = new_start;
370 post_ptr = new_start + (post_ptr - old_start);
371 post_end = post_start + new_max;
372 vim_free(old_start);
373 return OK;
374}
375
376/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200377 * Search between "start" and "end" and try to recognize a
378 * character class in expanded form. For example [0-9].
379 * On success, return the id the character class to be emitted.
380 * On failure, return 0 (=FAIL)
381 * Start points to the first char of the range, while end should point
382 * to the closing brace.
383 */
384 static int
385nfa_recognize_char_class(start, end, extra_newl)
386 char_u *start;
387 char_u *end;
388 int extra_newl;
389{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200390# define CLASS_not 0x80
391# define CLASS_af 0x40
392# define CLASS_AF 0x20
393# define CLASS_az 0x10
394# define CLASS_AZ 0x08
395# define CLASS_o7 0x04
396# define CLASS_o9 0x02
397# define CLASS_underscore 0x01
398
399 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200400 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200401 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200402
403 if (extra_newl == TRUE)
404 newl = TRUE;
405
406 if (*end != ']')
407 return FAIL;
408 p = start;
409 if (*p == '^')
410 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200411 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200412 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200413 }
414
415 while (p < end)
416 {
417 if (p + 2 < end && *(p + 1) == '-')
418 {
419 switch (*p)
420 {
421 case '0':
422 if (*(p + 2) == '9')
423 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200424 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200425 break;
426 }
427 else
428 if (*(p + 2) == '7')
429 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200430 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200431 break;
432 }
433 case 'a':
434 if (*(p + 2) == 'z')
435 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200436 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200437 break;
438 }
439 else
440 if (*(p + 2) == 'f')
441 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200442 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200443 break;
444 }
445 case 'A':
446 if (*(p + 2) == 'Z')
447 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200448 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200449 break;
450 }
451 else
452 if (*(p + 2) == 'F')
453 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200454 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200455 break;
456 }
457 /* FALLTHROUGH */
458 default:
459 return FAIL;
460 }
461 p += 3;
462 }
463 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
464 {
465 newl = TRUE;
466 p += 2;
467 }
468 else if (*p == '_')
469 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200470 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200471 p ++;
472 }
473 else if (*p == '\n')
474 {
475 newl = TRUE;
476 p ++;
477 }
478 else
479 return FAIL;
480 } /* while (p < end) */
481
482 if (p != end)
483 return FAIL;
484
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200485 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200486 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200487
488 switch (config)
489 {
490 case CLASS_o9:
491 return extra_newl + NFA_DIGIT;
492 case CLASS_not | CLASS_o9:
493 return extra_newl + NFA_NDIGIT;
494 case CLASS_af | CLASS_AF | CLASS_o9:
495 return extra_newl + NFA_HEX;
496 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
497 return extra_newl + NFA_NHEX;
498 case CLASS_o7:
499 return extra_newl + NFA_OCTAL;
500 case CLASS_not | CLASS_o7:
501 return extra_newl + NFA_NOCTAL;
502 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
503 return extra_newl + NFA_WORD;
504 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
505 return extra_newl + NFA_NWORD;
506 case CLASS_az | CLASS_AZ | CLASS_underscore:
507 return extra_newl + NFA_HEAD;
508 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
509 return extra_newl + NFA_NHEAD;
510 case CLASS_az | CLASS_AZ:
511 return extra_newl + NFA_ALPHA;
512 case CLASS_not | CLASS_az | CLASS_AZ:
513 return extra_newl + NFA_NALPHA;
514 case CLASS_az:
515 return extra_newl + NFA_LOWER;
516 case CLASS_not | CLASS_az:
517 return extra_newl + NFA_NLOWER;
518 case CLASS_AZ:
519 return extra_newl + NFA_UPPER;
520 case CLASS_not | CLASS_AZ:
521 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200522 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200523 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200524}
525
526/*
527 * Produce the bytes for equivalence class "c".
528 * Currently only handles latin1, latin9 and utf-8.
529 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
530 * equivalent to 'a OR b OR c'
531 *
532 * NOTE! When changing this function, also update reg_equi_class()
533 */
534 static int
535nfa_emit_equi_class(c, neg)
536 int c;
537 int neg;
538{
539 int first = TRUE;
540 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
541#define EMIT2(c) \
542 EMIT(c); \
543 if (neg == TRUE) { \
544 EMIT(NFA_NOT); \
545 } \
546 if (first == FALSE) \
547 EMIT(glue); \
548 else \
549 first = FALSE; \
550
551#ifdef FEAT_MBYTE
552 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
553 || STRCMP(p_enc, "iso-8859-15") == 0)
554#endif
555 {
556 switch (c)
557 {
558 case 'A': case '\300': case '\301': case '\302':
559 case '\303': case '\304': case '\305':
560 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
561 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
562 EMIT2('\305');
563 return OK;
564
565 case 'C': case '\307':
566 EMIT2('C'); EMIT2('\307');
567 return OK;
568
569 case 'E': case '\310': case '\311': case '\312': case '\313':
570 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
571 EMIT2('\312'); EMIT2('\313');
572 return OK;
573
574 case 'I': case '\314': case '\315': case '\316': case '\317':
575 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
576 EMIT2('\316'); EMIT2('\317');
577 return OK;
578
579 case 'N': case '\321':
580 EMIT2('N'); EMIT2('\321');
581 return OK;
582
583 case 'O': case '\322': case '\323': case '\324': case '\325':
584 case '\326':
585 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
586 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
587 return OK;
588
589 case 'U': case '\331': case '\332': case '\333': case '\334':
590 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
591 EMIT2('\333'); EMIT2('\334');
592 return OK;
593
594 case 'Y': case '\335':
595 EMIT2('Y'); EMIT2('\335');
596 return OK;
597
598 case 'a': case '\340': case '\341': case '\342':
599 case '\343': case '\344': case '\345':
600 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
601 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
602 EMIT2('\345');
603 return OK;
604
605 case 'c': case '\347':
606 EMIT2('c'); EMIT2('\347');
607 return OK;
608
609 case 'e': case '\350': case '\351': case '\352': case '\353':
610 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
611 EMIT2('\352'); EMIT2('\353');
612 return OK;
613
614 case 'i': case '\354': case '\355': case '\356': case '\357':
615 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
616 EMIT2('\356'); EMIT2('\357');
617 return OK;
618
619 case 'n': case '\361':
620 EMIT2('n'); EMIT2('\361');
621 return OK;
622
623 case 'o': case '\362': case '\363': case '\364': case '\365':
624 case '\366':
625 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
626 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
627 return OK;
628
629 case 'u': case '\371': case '\372': case '\373': case '\374':
630 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
631 EMIT2('\373'); EMIT2('\374');
632 return OK;
633
634 case 'y': case '\375': case '\377':
635 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
636 return OK;
637
638 default:
639 return FAIL;
640 }
641 }
642
643 EMIT(c);
644 return OK;
645#undef EMIT2
646}
647
648/*
649 * Code to parse regular expression.
650 *
651 * We try to reuse parsing functions in regexp.c to
652 * minimize surprise and keep the syntax consistent.
653 */
654
655/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200656 * Parse the lowest level.
657 *
658 * An atom can be one of a long list of items. Many atoms match one character
659 * in the text. It is often an ordinary character or a character class.
660 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
661 * is only for syntax highlighting.
662 *
663 * atom ::= ordinary-atom
664 * or \( pattern \)
665 * or \%( pattern \)
666 * or \z( pattern \)
667 */
668 static int
669nfa_regatom()
670{
671 int c;
672 int charclass;
673 int equiclass;
674 int collclass;
675 int got_coll_char;
676 char_u *p;
677 char_u *endp;
678#ifdef FEAT_MBYTE
679 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680#endif
681 int extra = 0;
682 int first;
683 int emit_range;
684 int negated;
685 int result;
686 int startc = -1;
687 int endc = -1;
688 int oldstartc = -1;
689 int cpo_lit; /* 'cpoptions' contains 'l' flag */
690 int cpo_bsl; /* 'cpoptions' contains '\' flag */
691 int glue; /* ID that will "glue" nodes together */
692
693 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
694 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
695
696 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200697 switch (c)
698 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200699 case NUL:
700 syntax_error = TRUE;
701 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
702
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200703 case Magic('^'):
704 EMIT(NFA_BOL);
705 break;
706
707 case Magic('$'):
708 EMIT(NFA_EOL);
709#if defined(FEAT_SYN_HL) || defined(PROTO)
710 had_eol = TRUE;
711#endif
712 break;
713
714 case Magic('<'):
715 EMIT(NFA_BOW);
716 break;
717
718 case Magic('>'):
719 EMIT(NFA_EOW);
720 break;
721
722 case Magic('_'):
723 c = no_Magic(getchr());
724 if (c == '^') /* "\_^" is start-of-line */
725 {
726 EMIT(NFA_BOL);
727 break;
728 }
729 if (c == '$') /* "\_$" is end-of-line */
730 {
731 EMIT(NFA_EOL);
732#if defined(FEAT_SYN_HL) || defined(PROTO)
733 had_eol = TRUE;
734#endif
735 break;
736 }
737
738 extra = ADD_NL;
739
740 /* "\_[" is collection plus newline */
741 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200742 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200743
744 /* "\_x" is character class plus newline */
745 /*FALLTHROUGH*/
746
747 /*
748 * Character classes.
749 */
750 case Magic('.'):
751 case Magic('i'):
752 case Magic('I'):
753 case Magic('k'):
754 case Magic('K'):
755 case Magic('f'):
756 case Magic('F'):
757 case Magic('p'):
758 case Magic('P'):
759 case Magic('s'):
760 case Magic('S'):
761 case Magic('d'):
762 case Magic('D'):
763 case Magic('x'):
764 case Magic('X'):
765 case Magic('o'):
766 case Magic('O'):
767 case Magic('w'):
768 case Magic('W'):
769 case Magic('h'):
770 case Magic('H'):
771 case Magic('a'):
772 case Magic('A'):
773 case Magic('l'):
774 case Magic('L'):
775 case Magic('u'):
776 case Magic('U'):
777 p = vim_strchr(classchars, no_Magic(c));
778 if (p == NULL)
779 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200780 EMSGN("INTERNAL: Unknown character class char: %ld", c);
781 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200782 }
783#ifdef FEAT_MBYTE
784 /* When '.' is followed by a composing char ignore the dot, so that
785 * the composing char is matched here. */
786 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
787 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200788 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200789 c = getchr();
790 goto nfa_do_multibyte;
791 }
792#endif
793 EMIT(nfa_classcodes[p - classchars]);
794 if (extra == ADD_NL)
795 {
796 EMIT(NFA_NEWL);
797 EMIT(NFA_OR);
798 regflags |= RF_HASNL;
799 }
800 break;
801
802 case Magic('n'):
803 if (reg_string)
804 /* In a string "\n" matches a newline character. */
805 EMIT(NL);
806 else
807 {
808 /* In buffer text "\n" matches the end of a line. */
809 EMIT(NFA_NEWL);
810 regflags |= RF_HASNL;
811 }
812 break;
813
814 case Magic('('):
815 if (nfa_reg(REG_PAREN) == FAIL)
816 return FAIL; /* cascaded error */
817 break;
818
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200819 case Magic('|'):
820 case Magic('&'):
821 case Magic(')'):
822 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200823 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200824 return FAIL;
825
826 case Magic('='):
827 case Magic('?'):
828 case Magic('+'):
829 case Magic('@'):
830 case Magic('*'):
831 case Magic('{'):
832 /* these should follow an atom, not form an atom */
833 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200834 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200835 return FAIL;
836
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200837 case Magic('~'):
838 {
839 char_u *lp;
840
841 /* Previous substitute pattern.
842 * Generated as "\%(pattern\)". */
843 if (reg_prev_sub == NULL)
844 {
845 EMSG(_(e_nopresub));
846 return FAIL;
847 }
848 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
849 {
850 EMIT(PTR2CHAR(lp));
851 if (lp != reg_prev_sub)
852 EMIT(NFA_CONCAT);
853 }
854 EMIT(NFA_NOPEN);
855 break;
856 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200857
Bram Moolenaar428e9872013-05-30 17:05:39 +0200858 case Magic('1'):
859 case Magic('2'):
860 case Magic('3'):
861 case Magic('4'):
862 case Magic('5'):
863 case Magic('6'):
864 case Magic('7'):
865 case Magic('8'):
866 case Magic('9'):
867 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
868 nfa_has_backref = TRUE;
869 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200870
871 case Magic('z'):
872 c = no_Magic(getchr());
873 switch (c)
874 {
875 case 's':
876 EMIT(NFA_ZSTART);
877 break;
878 case 'e':
879 EMIT(NFA_ZEND);
880 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200881 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200882#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200883 case '1':
884 case '2':
885 case '3':
886 case '4':
887 case '5':
888 case '6':
889 case '7':
890 case '8':
891 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200892 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200893 if (reg_do_extmatch != REX_USE)
894 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200895 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
896 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +0200897 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200898 re_has_z = REX_USE;
899 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200900 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200901 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200902 if (reg_do_extmatch != REX_SET)
903 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200904 if (nfa_reg(REG_ZPAREN) == FAIL)
905 return FAIL; /* cascaded error */
906 re_has_z = REX_SET;
907 break;
908#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200909 default:
910 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200911 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200912 no_Magic(c));
913 return FAIL;
914 }
915 break;
916
917 case Magic('%'):
918 c = no_Magic(getchr());
919 switch (c)
920 {
921 /* () without a back reference */
922 case '(':
923 if (nfa_reg(REG_NPAREN) == FAIL)
924 return FAIL;
925 EMIT(NFA_NOPEN);
926 break;
927
928 case 'd': /* %d123 decimal */
929 case 'o': /* %o123 octal */
930 case 'x': /* %xab hex 2 */
931 case 'u': /* %uabcd hex 4 */
932 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200933 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200934 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200935
Bram Moolenaar47196582013-05-25 22:04:23 +0200936 switch (c)
937 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200938 case 'd': nr = getdecchrs(); break;
939 case 'o': nr = getoctchrs(); break;
940 case 'x': nr = gethexchrs(2); break;
941 case 'u': nr = gethexchrs(4); break;
942 case 'U': nr = gethexchrs(8); break;
943 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200944 }
945
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200946 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200947 EMSG2_RET_FAIL(
948 _("E678: Invalid character after %s%%[dxouU]"),
949 reg_magic == MAGIC_ALL);
950 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200951 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200952 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200953 break;
954
955 /* Catch \%^ and \%$ regardless of where they appear in the
956 * pattern -- regardless of whether or not it makes sense. */
957 case '^':
958 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200959 break;
960
961 case '$':
962 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200963 break;
964
965 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200966 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200967 break;
968
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200969#ifdef FEAT_VISUAL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200970 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200971 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200972 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200973#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200974
975 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200976 {
977 int n;
978
979 /* \%[abc] */
980 for (n = 0; (c = getchr()) != ']'; ++n)
981 {
982 if (c == NUL)
983 EMSG2_RET_FAIL(_(e_missing_sb),
984 reg_magic == MAGIC_ALL);
985 EMIT(c);
986 }
987 EMIT(NFA_OPT_CHARS);
988 EMIT(n);
989 break;
990 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200991
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200992 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200993 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200994 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200995 int cmp = c;
996
997 if (c == '<' || c == '>')
998 c = getchr();
999 while (VIM_ISDIGIT(c))
1000 {
1001 n = n * 10 + (c - '0');
1002 c = getchr();
1003 }
1004 if (c == 'l' || c == 'c' || c == 'v')
1005 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001006 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001007 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001008 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001009 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001010 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001011 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001012 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001013 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001014 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001015 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001016 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001017 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001018 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001019 break;
1020 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001021 else if (c == '\'' && n == 0)
1022 {
1023 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001024 EMIT(cmp == '<' ? NFA_MARK_LT :
1025 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001026 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001027 break;
1028 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001029 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001030 syntax_error = TRUE;
1031 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1032 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001033 return FAIL;
1034 }
1035 break;
1036
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001037 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001038collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001039 /*
1040 * Glue is emitted between several atoms from the [].
1041 * It is either NFA_OR, or NFA_CONCAT.
1042 *
1043 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1044 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1045 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1046 * notation)
1047 *
1048 */
1049
1050
1051/* Emit negation atoms, if needed.
1052 * The CONCAT below merges the NOT with the previous node. */
1053#define TRY_NEG() \
1054 if (negated == TRUE) \
1055 { \
1056 EMIT(NFA_NOT); \
1057 }
1058
1059/* Emit glue between important nodes : CONCAT or OR. */
1060#define EMIT_GLUE() \
1061 if (first == FALSE) \
1062 EMIT(glue); \
1063 else \
1064 first = FALSE;
1065
1066 p = regparse;
1067 endp = skip_anyof(p);
1068 if (*endp == ']')
1069 {
1070 /*
1071 * Try to reverse engineer character classes. For example,
1072 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1073 * and perform the necessary substitutions in the NFA.
1074 */
1075 result = nfa_recognize_char_class(regparse, endp,
1076 extra == ADD_NL);
1077 if (result != FAIL)
1078 {
1079 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1080 EMIT(result);
1081 else /* must be char class + newline */
1082 {
1083 EMIT(result - ADD_NL);
1084 EMIT(NFA_NEWL);
1085 EMIT(NFA_OR);
1086 }
1087 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001088 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001089 return OK;
1090 }
1091 /*
1092 * Failed to recognize a character class. Use the simple
1093 * version that turns [abc] into 'a' OR 'b' OR 'c'
1094 */
1095 startc = endc = oldstartc = -1;
1096 first = TRUE; /* Emitting first atom in this sequence? */
1097 negated = FALSE;
1098 glue = NFA_OR;
1099 if (*regparse == '^') /* negated range */
1100 {
1101 negated = TRUE;
1102 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001103 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001104 }
1105 if (*regparse == '-')
1106 {
1107 startc = '-';
1108 EMIT(startc);
1109 TRY_NEG();
1110 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001111 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001112 }
1113 /* Emit the OR branches for each character in the [] */
1114 emit_range = FALSE;
1115 while (regparse < endp)
1116 {
1117 oldstartc = startc;
1118 startc = -1;
1119 got_coll_char = FALSE;
1120 if (*regparse == '[')
1121 {
1122 /* Check for [: :], [= =], [. .] */
1123 equiclass = collclass = 0;
1124 charclass = get_char_class(&regparse);
1125 if (charclass == CLASS_NONE)
1126 {
1127 equiclass = get_equi_class(&regparse);
1128 if (equiclass == 0)
1129 collclass = get_coll_element(&regparse);
1130 }
1131
1132 /* Character class like [:alpha:] */
1133 if (charclass != CLASS_NONE)
1134 {
1135 switch (charclass)
1136 {
1137 case CLASS_ALNUM:
1138 EMIT(NFA_CLASS_ALNUM);
1139 break;
1140 case CLASS_ALPHA:
1141 EMIT(NFA_CLASS_ALPHA);
1142 break;
1143 case CLASS_BLANK:
1144 EMIT(NFA_CLASS_BLANK);
1145 break;
1146 case CLASS_CNTRL:
1147 EMIT(NFA_CLASS_CNTRL);
1148 break;
1149 case CLASS_DIGIT:
1150 EMIT(NFA_CLASS_DIGIT);
1151 break;
1152 case CLASS_GRAPH:
1153 EMIT(NFA_CLASS_GRAPH);
1154 break;
1155 case CLASS_LOWER:
1156 EMIT(NFA_CLASS_LOWER);
1157 break;
1158 case CLASS_PRINT:
1159 EMIT(NFA_CLASS_PRINT);
1160 break;
1161 case CLASS_PUNCT:
1162 EMIT(NFA_CLASS_PUNCT);
1163 break;
1164 case CLASS_SPACE:
1165 EMIT(NFA_CLASS_SPACE);
1166 break;
1167 case CLASS_UPPER:
1168 EMIT(NFA_CLASS_UPPER);
1169 break;
1170 case CLASS_XDIGIT:
1171 EMIT(NFA_CLASS_XDIGIT);
1172 break;
1173 case CLASS_TAB:
1174 EMIT(NFA_CLASS_TAB);
1175 break;
1176 case CLASS_RETURN:
1177 EMIT(NFA_CLASS_RETURN);
1178 break;
1179 case CLASS_BACKSPACE:
1180 EMIT(NFA_CLASS_BACKSPACE);
1181 break;
1182 case CLASS_ESCAPE:
1183 EMIT(NFA_CLASS_ESCAPE);
1184 break;
1185 }
1186 TRY_NEG();
1187 EMIT_GLUE();
1188 continue;
1189 }
1190 /* Try equivalence class [=a=] and the like */
1191 if (equiclass != 0)
1192 {
1193 result = nfa_emit_equi_class(equiclass, negated);
1194 if (result == FAIL)
1195 {
1196 /* should never happen */
1197 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1198 }
1199 EMIT_GLUE();
1200 continue;
1201 }
1202 /* Try collating class like [. .] */
1203 if (collclass != 0)
1204 {
1205 startc = collclass; /* allow [.a.]-x as a range */
1206 /* Will emit the proper atom at the end of the
1207 * while loop. */
1208 }
1209 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001210 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1211 * start character. */
1212 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 {
1214 emit_range = TRUE;
1215 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001216 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001217 continue; /* reading the end of the range */
1218 }
1219
1220 /* Now handle simple and escaped characters.
1221 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1222 * accepts "\t", "\e", etc., but only when the 'l' flag in
1223 * 'cpoptions' is not included.
1224 * Posix doesn't recognize backslash at all.
1225 */
1226 if (*regparse == '\\'
1227 && !cpo_bsl
1228 && regparse + 1 <= endp
1229 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1230 || (!cpo_lit
1231 && vim_strchr(REGEXP_ABBR, regparse[1])
1232 != NULL)
1233 )
1234 )
1235 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001236 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001237
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001238 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001239 startc = reg_string ? NL : NFA_NEWL;
1240 else
1241 if (*regparse == 'd'
1242 || *regparse == 'o'
1243 || *regparse == 'x'
1244 || *regparse == 'u'
1245 || *regparse == 'U'
1246 )
1247 {
1248 /* TODO(RE) This needs more testing */
1249 startc = coll_get_char();
1250 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001251 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001252 }
1253 else
1254 {
1255 /* \r,\t,\e,\b */
1256 startc = backslash_trans(*regparse);
1257 }
1258 }
1259
1260 /* Normal printable char */
1261 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001262 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263
1264 /* Previous char was '-', so this char is end of range. */
1265 if (emit_range)
1266 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001267 endc = startc;
1268 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269 if (startc > endc)
1270 EMSG_RET_FAIL(_(e_invrange));
1271#ifdef FEAT_MBYTE
1272 if (has_mbyte && ((*mb_char2len)(startc) > 1
1273 || (*mb_char2len)(endc) > 1))
1274 {
1275 if (endc > startc + 256)
1276 EMSG_RET_FAIL(_(e_invrange));
1277 /* Emit the range. "startc" was already emitted, so
1278 * skip it. */
1279 for (c = startc + 1; c <= endc; c++)
1280 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001281 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 TRY_NEG();
1283 EMIT_GLUE();
1284 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001285 }
1286 else
1287#endif
1288 {
1289#ifdef EBCDIC
1290 int alpha_only = FALSE;
1291
1292 /* for alphabetical range skip the gaps
1293 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1294 if (isalpha(startc) && isalpha(endc))
1295 alpha_only = TRUE;
1296#endif
1297 /* Emit the range. "startc" was already emitted, so
1298 * skip it. */
1299 for (c = startc + 1; c <= endc; c++)
1300#ifdef EBCDIC
1301 if (!alpha_only || isalpha(startc))
1302#endif
1303 {
1304 EMIT(c);
1305 TRY_NEG();
1306 EMIT_GLUE();
1307 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001308 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001309 emit_range = FALSE;
1310 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001311 }
1312 else
1313 {
1314 /*
1315 * This char (startc) is not part of a range. Just
1316 * emit it.
1317 *
1318 * Normally, simply emit startc. But if we get char
1319 * code=0 from a collating char, then replace it with
1320 * 0x0a.
1321 *
1322 * This is needed to completely mimic the behaviour of
1323 * the backtracking engine.
1324 */
1325 if (got_coll_char == TRUE && startc == 0)
1326 EMIT(0x0a);
1327 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001328 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001329 TRY_NEG();
1330 EMIT_GLUE();
1331 }
1332
Bram Moolenaar51a29832013-05-28 22:30:35 +02001333 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001334 } /* while (p < endp) */
1335
Bram Moolenaar51a29832013-05-28 22:30:35 +02001336 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001337 if (*regparse == '-') /* if last, '-' is just a char */
1338 {
1339 EMIT('-');
1340 TRY_NEG();
1341 EMIT_GLUE();
1342 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001343 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001345 /* skip the trailing ] */
1346 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001347 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 if (negated == TRUE)
1349 {
1350 /* Mark end of negated char range */
1351 EMIT(NFA_END_NEG_RANGE);
1352 EMIT(NFA_CONCAT);
1353 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001354
1355 /* \_[] also matches \n but it's not negated */
1356 if (extra == ADD_NL)
1357 {
1358 EMIT(reg_string ? NL : NFA_NEWL);
1359 EMIT(NFA_OR);
1360 }
1361
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001362 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001363 } /* if exists closing ] */
1364
1365 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 {
1367 syntax_error = TRUE;
1368 EMSG_RET_FAIL(_(e_missingbracket));
1369 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001370 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001371
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001372 default:
1373 {
1374#ifdef FEAT_MBYTE
1375 int plen;
1376
1377nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001378 /* plen is length of current char with composing chars */
1379 if (enc_utf8 && ((*mb_char2len)(c)
1380 != (plen = (*mb_ptr2len)(old_regparse))
1381 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001382 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001383 int i = 0;
1384
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001385 /* A base character plus composing characters, or just one
1386 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001387 * This requires creating a separate atom as if enclosing
1388 * the characters in (), where NFA_COMPOSING is the ( and
1389 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 * building the postfix form, not the NFA itself;
1391 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001392 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001393 for (;;)
1394 {
1395 EMIT(c);
1396 if (i > 0)
1397 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001398 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001399 break;
1400 c = utf_ptr2char(old_regparse + i);
1401 }
1402 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 regparse = old_regparse + plen;
1404 }
1405 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406#endif
1407 {
1408 c = no_Magic(c);
1409 EMIT(c);
1410 }
1411 return OK;
1412 }
1413 }
1414
1415#undef TRY_NEG
1416#undef EMIT_GLUE
1417
1418 return OK;
1419}
1420
1421/*
1422 * Parse something followed by possible [*+=].
1423 *
1424 * A piece is an atom, possibly followed by a multi, an indication of how many
1425 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1426 * characters: "", "a", "aa", etc.
1427 *
1428 * piece ::= atom
1429 * or atom multi
1430 */
1431 static int
1432nfa_regpiece()
1433{
1434 int i;
1435 int op;
1436 int ret;
1437 long minval, maxval;
1438 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001439 parse_state_T old_state;
1440 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001442 int old_post_pos;
1443 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 int quest;
1445
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001446 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1447 * next. */
1448 save_parse_state(&old_state);
1449
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001451 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452
1453 ret = nfa_regatom();
1454 if (ret == FAIL)
1455 return FAIL; /* cascaded error */
1456
1457 op = peekchr();
1458 if (re_multi_type(op) == NOT_MULTI)
1459 return OK;
1460
1461 skipchr();
1462 switch (op)
1463 {
1464 case Magic('*'):
1465 EMIT(NFA_STAR);
1466 break;
1467
1468 case Magic('+'):
1469 /*
1470 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1471 * first and only submatch would be "aaa". But the backtracking
1472 * engine interprets the plus as "try matching one more time", and
1473 * a* matches a second time at the end of the input, the empty
1474 * string.
1475 * The submatch will the empty string.
1476 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001477 * In order to be consistent with the old engine, we replace
1478 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001479 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001480 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001481 curchr = -1;
1482 if (nfa_regatom() == FAIL)
1483 return FAIL;
1484 EMIT(NFA_STAR);
1485 EMIT(NFA_CONCAT);
1486 skipchr(); /* skip the \+ */
1487 break;
1488
1489 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001490 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001491 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001492 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 switch(op)
1494 {
1495 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001496 /* \@= */
1497 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001499 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001500 /* \@! */
1501 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001502 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001503 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001504 op = no_Magic(getchr());
1505 if (op == '=')
1506 /* \@<= */
1507 i = NFA_PREV_ATOM_JUST_BEFORE;
1508 else if (op == '!')
1509 /* \@<! */
1510 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1511 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001512 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001513 /* \@> Not supported yet */
1514 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001515 return FAIL;
1516 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001517 if (i == 0)
1518 {
1519 syntax_error = TRUE;
1520 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1521 return FAIL;
1522 }
1523 EMIT(i);
1524 if (i == NFA_PREV_ATOM_JUST_BEFORE
1525 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1526 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001527 break;
1528
1529 case Magic('?'):
1530 case Magic('='):
1531 EMIT(NFA_QUEST);
1532 break;
1533
1534 case Magic('{'):
1535 /* a{2,5} will expand to 'aaa?a?a?'
1536 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1537 * version of '?'
1538 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1539 * parenthesis have the same id
1540 */
1541
1542 greedy = TRUE;
1543 c2 = peekchr();
1544 if (c2 == '-' || c2 == Magic('-'))
1545 {
1546 skipchr();
1547 greedy = FALSE;
1548 }
1549 if (!read_limits(&minval, &maxval))
1550 {
1551 syntax_error = TRUE;
1552 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1553 }
1554 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1555 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001556 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001557 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001558 if (greedy)
1559 /* \{}, \{0,} */
1560 EMIT(NFA_STAR);
1561 else
1562 /* \{-}, \{-0,} */
1563 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 break;
1565 }
1566
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001567 /* Special case: x{0} or x{-0} */
1568 if (maxval == 0)
1569 {
1570 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001571 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001572 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1573 EMIT(NFA_SKIP_CHAR);
1574 return OK;
1575 }
1576
1577 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001578 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001579 /* Save parse state after the repeated atom and the \{} */
1580 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001581
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001582 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1583 for (i = 0; i < maxval; i++)
1584 {
1585 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001586 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001587 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001588 if (nfa_regatom() == FAIL)
1589 return FAIL;
1590 /* after "minval" times, atoms are optional */
1591 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001592 {
1593 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001594 {
1595 if (greedy)
1596 EMIT(NFA_STAR);
1597 else
1598 EMIT(NFA_STAR_NONGREEDY);
1599 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001600 else
1601 EMIT(quest);
1602 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001603 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001605 if (i + 1 > minval && maxval == MAX_LIMIT)
1606 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001607 }
1608
1609 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001610 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001611 curchr = -1;
1612
1613 break;
1614
1615
1616 default:
1617 break;
1618 } /* end switch */
1619
1620 if (re_multi_type(peekchr()) != NOT_MULTI)
1621 {
1622 /* Can't have a multi follow a multi. */
1623 syntax_error = TRUE;
1624 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1625 }
1626
1627 return OK;
1628}
1629
1630/*
1631 * Parse one or more pieces, concatenated. It matches a match for the
1632 * first piece, followed by a match for the second piece, etc. Example:
1633 * "f[0-9]b", first matches "f", then a digit and then "b".
1634 *
1635 * concat ::= piece
1636 * or piece piece
1637 * or piece piece piece
1638 * etc.
1639 */
1640 static int
1641nfa_regconcat()
1642{
1643 int cont = TRUE;
1644 int first = TRUE;
1645
1646 while (cont)
1647 {
1648 switch (peekchr())
1649 {
1650 case NUL:
1651 case Magic('|'):
1652 case Magic('&'):
1653 case Magic(')'):
1654 cont = FALSE;
1655 break;
1656
1657 case Magic('Z'):
1658#ifdef FEAT_MBYTE
1659 regflags |= RF_ICOMBINE;
1660#endif
1661 skipchr_keepstart();
1662 break;
1663 case Magic('c'):
1664 regflags |= RF_ICASE;
1665 skipchr_keepstart();
1666 break;
1667 case Magic('C'):
1668 regflags |= RF_NOICASE;
1669 skipchr_keepstart();
1670 break;
1671 case Magic('v'):
1672 reg_magic = MAGIC_ALL;
1673 skipchr_keepstart();
1674 curchr = -1;
1675 break;
1676 case Magic('m'):
1677 reg_magic = MAGIC_ON;
1678 skipchr_keepstart();
1679 curchr = -1;
1680 break;
1681 case Magic('M'):
1682 reg_magic = MAGIC_OFF;
1683 skipchr_keepstart();
1684 curchr = -1;
1685 break;
1686 case Magic('V'):
1687 reg_magic = MAGIC_NONE;
1688 skipchr_keepstart();
1689 curchr = -1;
1690 break;
1691
1692 default:
1693 if (nfa_regpiece() == FAIL)
1694 return FAIL;
1695 if (first == FALSE)
1696 EMIT(NFA_CONCAT);
1697 else
1698 first = FALSE;
1699 break;
1700 }
1701 }
1702
1703 return OK;
1704}
1705
1706/*
1707 * Parse a branch, one or more concats, separated by "\&". It matches the
1708 * last concat, but only if all the preceding concats also match at the same
1709 * position. Examples:
1710 * "foobeep\&..." matches "foo" in "foobeep".
1711 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1712 *
1713 * branch ::= concat
1714 * or concat \& concat
1715 * or concat \& concat \& concat
1716 * etc.
1717 */
1718 static int
1719nfa_regbranch()
1720{
1721 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001722 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001723
Bram Moolenaar16299b52013-05-30 18:45:23 +02001724 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001725
1726 /* First branch, possibly the only one */
1727 if (nfa_regconcat() == FAIL)
1728 return FAIL;
1729
1730 ch = peekchr();
1731 /* Try next concats */
1732 while (ch == Magic('&'))
1733 {
1734 skipchr();
1735 EMIT(NFA_NOPEN);
1736 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001737 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 if (nfa_regconcat() == FAIL)
1739 return FAIL;
1740 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001741 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001742 EMIT(NFA_SKIP_CHAR);
1743 EMIT(NFA_CONCAT);
1744 ch = peekchr();
1745 }
1746
1747 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001748 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001749 EMIT(NFA_SKIP_CHAR);
1750
1751 return OK;
1752}
1753
1754/*
1755 * Parse a pattern, one or more branches, separated by "\|". It matches
1756 * anything that matches one of the branches. Example: "foo\|beep" matches
1757 * "foo" and matches "beep". If more than one branch matches, the first one
1758 * is used.
1759 *
1760 * pattern ::= branch
1761 * or branch \| branch
1762 * or branch \| branch \| branch
1763 * etc.
1764 */
1765 static int
1766nfa_reg(paren)
1767 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1768{
1769 int parno = 0;
1770
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001771 if (paren == REG_PAREN)
1772 {
1773 if (regnpar >= NSUBEXP) /* Too many `(' */
1774 {
1775 syntax_error = TRUE;
1776 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1777 }
1778 parno = regnpar++;
1779 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001780#ifdef FEAT_SYN_HL
1781 else if (paren == REG_ZPAREN)
1782 {
1783 /* Make a ZOPEN node. */
1784 if (regnzpar >= NSUBEXP)
1785 {
1786 syntax_error = TRUE;
1787 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1788 }
1789 parno = regnzpar++;
1790 }
1791#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001792
1793 if (nfa_regbranch() == FAIL)
1794 return FAIL; /* cascaded error */
1795
1796 while (peekchr() == Magic('|'))
1797 {
1798 skipchr();
1799 if (nfa_regbranch() == FAIL)
1800 return FAIL; /* cascaded error */
1801 EMIT(NFA_OR);
1802 }
1803
1804 /* Check for proper termination. */
1805 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1806 {
1807 syntax_error = TRUE;
1808 if (paren == REG_NPAREN)
1809 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1810 else
1811 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1812 }
1813 else if (paren == REG_NOPAREN && peekchr() != NUL)
1814 {
1815 syntax_error = TRUE;
1816 if (peekchr() == Magic(')'))
1817 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1818 else
1819 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1820 }
1821 /*
1822 * Here we set the flag allowing back references to this set of
1823 * parentheses.
1824 */
1825 if (paren == REG_PAREN)
1826 {
1827 had_endbrace[parno] = TRUE; /* have seen the close paren */
1828 EMIT(NFA_MOPEN + parno);
1829 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001830#ifdef FEAT_SYN_HL
1831 else if (paren == REG_ZPAREN)
1832 EMIT(NFA_ZOPEN + parno);
1833#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834
1835 return OK;
1836}
1837
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838#ifdef DEBUG
1839static char_u code[50];
1840
1841 static void
1842nfa_set_code(c)
1843 int c;
1844{
1845 int addnl = FALSE;
1846
1847 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1848 {
1849 addnl = TRUE;
1850 c -= ADD_NL;
1851 }
1852
1853 STRCPY(code, "");
1854 switch (c)
1855 {
1856 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1857 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1858 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1859 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1860 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1861 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1862
Bram Moolenaar5714b802013-05-28 22:03:20 +02001863 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1864 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1865 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1866 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1867 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1868 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1869 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1870 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1871 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001872#ifdef FEAT_SYN_HL
1873 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1874 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1875 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1876 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1877 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1878 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1879 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1880 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1881 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1882#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001883 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1884
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885 case NFA_PREV_ATOM_NO_WIDTH:
1886 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001887 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1888 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001889 case NFA_PREV_ATOM_JUST_BEFORE:
1890 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1891 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1892 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001893 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1894 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001896 case NFA_START_INVISIBLE_BEFORE:
1897 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1899
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1901 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001902 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001904 case NFA_MOPEN:
1905 case NFA_MOPEN1:
1906 case NFA_MOPEN2:
1907 case NFA_MOPEN3:
1908 case NFA_MOPEN4:
1909 case NFA_MOPEN5:
1910 case NFA_MOPEN6:
1911 case NFA_MOPEN7:
1912 case NFA_MOPEN8:
1913 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914 STRCPY(code, "NFA_MOPEN(x)");
1915 code[10] = c - NFA_MOPEN + '0';
1916 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001917 case NFA_MCLOSE:
1918 case NFA_MCLOSE1:
1919 case NFA_MCLOSE2:
1920 case NFA_MCLOSE3:
1921 case NFA_MCLOSE4:
1922 case NFA_MCLOSE5:
1923 case NFA_MCLOSE6:
1924 case NFA_MCLOSE7:
1925 case NFA_MCLOSE8:
1926 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 STRCPY(code, "NFA_MCLOSE(x)");
1928 code[11] = c - NFA_MCLOSE + '0';
1929 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001930#ifdef FEAT_SYN_HL
1931 case NFA_ZOPEN:
1932 case NFA_ZOPEN1:
1933 case NFA_ZOPEN2:
1934 case NFA_ZOPEN3:
1935 case NFA_ZOPEN4:
1936 case NFA_ZOPEN5:
1937 case NFA_ZOPEN6:
1938 case NFA_ZOPEN7:
1939 case NFA_ZOPEN8:
1940 case NFA_ZOPEN9:
1941 STRCPY(code, "NFA_ZOPEN(x)");
1942 code[10] = c - NFA_ZOPEN + '0';
1943 break;
1944 case NFA_ZCLOSE:
1945 case NFA_ZCLOSE1:
1946 case NFA_ZCLOSE2:
1947 case NFA_ZCLOSE3:
1948 case NFA_ZCLOSE4:
1949 case NFA_ZCLOSE5:
1950 case NFA_ZCLOSE6:
1951 case NFA_ZCLOSE7:
1952 case NFA_ZCLOSE8:
1953 case NFA_ZCLOSE9:
1954 STRCPY(code, "NFA_ZCLOSE(x)");
1955 code[11] = c - NFA_ZCLOSE + '0';
1956 break;
1957#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1959 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1960 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1961 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001962 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1963 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001964 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1965 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1966 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1967 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1968 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1969 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1970 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1971 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1972 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1973 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1974 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1975 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1976 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001977#ifdef FEAT_VISUAL
Bram Moolenaar044aa292013-06-04 21:27:38 +02001978 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001979#endif
Bram Moolenaar044aa292013-06-04 21:27:38 +02001980
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001981 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001982 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1983 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1984 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001985 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1986 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1987 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001988 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1989 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1990 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1991 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1992 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1993 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1994 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1995 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1996 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1997 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1998 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1999 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2000 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2001 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2002 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2003 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2004 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2005
2006 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2007 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2008 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2009 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2010 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2011 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2012 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2013 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2014 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2015 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2016 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2017 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2018 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2019 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2020 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2021 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2022 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2023 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2024 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2025 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2026 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2027 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2028 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2029 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2030 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2031 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2032 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2033
2034 default:
2035 STRCPY(code, "CHAR(x)");
2036 code[5] = c;
2037 }
2038
2039 if (addnl == TRUE)
2040 STRCAT(code, " + NEWLINE ");
2041
2042}
2043
2044#ifdef ENABLE_LOG
2045static FILE *log_fd;
2046
2047/*
2048 * Print the postfix notation of the current regexp.
2049 */
2050 static void
2051nfa_postfix_dump(expr, retval)
2052 char_u *expr;
2053 int retval;
2054{
2055 int *p;
2056 FILE *f;
2057
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002058 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059 if (f != NULL)
2060 {
2061 fprintf(f, "\n-------------------------\n");
2062 if (retval == FAIL)
2063 fprintf(f, ">>> NFA engine failed ... \n");
2064 else if (retval == OK)
2065 fprintf(f, ">>> NFA engine succeeded !\n");
2066 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002067 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 {
2069 nfa_set_code(*p);
2070 fprintf(f, "%s, ", code);
2071 }
2072 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002073 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002074 fprintf(f, "%d ", *p);
2075 fprintf(f, "\n\n");
2076 fclose(f);
2077 }
2078}
2079
2080/*
2081 * Print the NFA starting with a root node "state".
2082 */
2083 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002084nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085 FILE *debugf;
2086 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002088 garray_T indent;
2089
2090 ga_init2(&indent, 1, 64);
2091 ga_append(&indent, '\0');
2092 nfa_print_state2(debugf, state, &indent);
2093 ga_clear(&indent);
2094}
2095
2096 static void
2097nfa_print_state2(debugf, state, indent)
2098 FILE *debugf;
2099 nfa_state_T *state;
2100 garray_T *indent;
2101{
2102 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002103
2104 if (state == NULL)
2105 return;
2106
2107 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002108
2109 /* Output indent */
2110 p = (char_u *)indent->ga_data;
2111 if (indent->ga_len >= 3)
2112 {
2113 int last = indent->ga_len - 3;
2114 char_u save[2];
2115
2116 STRNCPY(save, &p[last], 2);
2117 STRNCPY(&p[last], "+-", 2);
2118 fprintf(debugf, " %s", p);
2119 STRNCPY(&p[last], save, 2);
2120 }
2121 else
2122 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002123
2124 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002125 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2126 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002127 if (state->id < 0)
2128 return;
2129
2130 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002131
2132 /* grow indent for state->out */
2133 indent->ga_len -= 1;
2134 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002135 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002136 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002137 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002138 ga_append(indent, '\0');
2139
2140 nfa_print_state2(debugf, state->out, indent);
2141
2142 /* replace last part of indent for state->out1 */
2143 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002144 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002145 ga_append(indent, '\0');
2146
2147 nfa_print_state2(debugf, state->out1, indent);
2148
2149 /* shrink indent */
2150 indent->ga_len -= 3;
2151 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002152}
2153
2154/*
2155 * Print the NFA state machine.
2156 */
2157 static void
2158nfa_dump(prog)
2159 nfa_regprog_T *prog;
2160{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002161 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002162
2163 if (debugf != NULL)
2164 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002165 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166 fclose(debugf);
2167 }
2168}
2169#endif /* ENABLE_LOG */
2170#endif /* DEBUG */
2171
2172/*
2173 * Parse r.e. @expr and convert it into postfix form.
2174 * Return the postfix string on success, NULL otherwise.
2175 */
2176 static int *
2177re2post()
2178{
2179 if (nfa_reg(REG_NOPAREN) == FAIL)
2180 return NULL;
2181 EMIT(NFA_MOPEN);
2182 return post_start;
2183}
2184
2185/* NB. Some of the code below is inspired by Russ's. */
2186
2187/*
2188 * Represents an NFA state plus zero or one or two arrows exiting.
2189 * if c == MATCH, no arrows out; matching state.
2190 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2191 * If c < 256, labeled arrow with character c to out.
2192 */
2193
2194static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2195
2196/*
2197 * Allocate and initialize nfa_state_T.
2198 */
2199 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002200alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 int c;
2202 nfa_state_T *out;
2203 nfa_state_T *out1;
2204{
2205 nfa_state_T *s;
2206
2207 if (istate >= nstate)
2208 return NULL;
2209
2210 s = &state_ptr[istate++];
2211
2212 s->c = c;
2213 s->out = out;
2214 s->out1 = out1;
2215
2216 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002217 s->lastlist[0] = 0;
2218 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002219 s->negated = FALSE;
2220
2221 return s;
2222}
2223
2224/*
2225 * A partially built NFA without the matching state filled in.
2226 * Frag_T.start points at the start state.
2227 * Frag_T.out is a list of places that need to be set to the
2228 * next state for this fragment.
2229 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002230
2231/* Since the out pointers in the list are always
2232 * uninitialized, we use the pointers themselves
2233 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002235union Ptrlist
2236{
2237 Ptrlist *next;
2238 nfa_state_T *s;
2239};
2240
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002241struct Frag
2242{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002243 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002244 Ptrlist *out;
2245};
2246typedef struct Frag Frag_T;
2247
2248static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2249static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2250static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2251static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2252static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2253static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2254
2255/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002256 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002257 */
2258 static Frag_T
2259frag(start, out)
2260 nfa_state_T *start;
2261 Ptrlist *out;
2262{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002263 Frag_T n;
2264
2265 n.start = start;
2266 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002267 return n;
2268}
2269
2270/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002271 * Create singleton list containing just outp.
2272 */
2273 static Ptrlist *
2274list1(outp)
2275 nfa_state_T **outp;
2276{
2277 Ptrlist *l;
2278
2279 l = (Ptrlist *)outp;
2280 l->next = NULL;
2281 return l;
2282}
2283
2284/*
2285 * Patch the list of states at out to point to start.
2286 */
2287 static void
2288patch(l, s)
2289 Ptrlist *l;
2290 nfa_state_T *s;
2291{
2292 Ptrlist *next;
2293
2294 for (; l; l = next)
2295 {
2296 next = l->next;
2297 l->s = s;
2298 }
2299}
2300
2301
2302/*
2303 * Join the two lists l1 and l2, returning the combination.
2304 */
2305 static Ptrlist *
2306append(l1, l2)
2307 Ptrlist *l1;
2308 Ptrlist *l2;
2309{
2310 Ptrlist *oldl1;
2311
2312 oldl1 = l1;
2313 while (l1->next)
2314 l1 = l1->next;
2315 l1->next = l2;
2316 return oldl1;
2317}
2318
2319/*
2320 * Stack used for transforming postfix form into NFA.
2321 */
2322static Frag_T empty;
2323
2324 static void
2325st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002326 int *postfix UNUSED;
2327 int *end UNUSED;
2328 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002330#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331 FILE *df;
2332 int *p2;
2333
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002334 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002335 if (df)
2336 {
2337 fprintf(df, "Error popping the stack!\n");
2338#ifdef DEBUG
2339 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2340#endif
2341 fprintf(df, "Postfix form is: ");
2342#ifdef DEBUG
2343 for (p2 = postfix; p2 < end; p2++)
2344 {
2345 nfa_set_code(*p2);
2346 fprintf(df, "%s, ", code);
2347 }
2348 nfa_set_code(*p);
2349 fprintf(df, "\nCurrent position is: ");
2350 for (p2 = postfix; p2 <= p; p2 ++)
2351 {
2352 nfa_set_code(*p2);
2353 fprintf(df, "%s, ", code);
2354 }
2355#else
2356 for (p2 = postfix; p2 < end; p2++)
2357 {
2358 fprintf(df, "%d, ", *p2);
2359 }
2360 fprintf(df, "\nCurrent position is: ");
2361 for (p2 = postfix; p2 <= p; p2 ++)
2362 {
2363 fprintf(df, "%d, ", *p2);
2364 }
2365#endif
2366 fprintf(df, "\n--------------------------\n");
2367 fclose(df);
2368 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002369#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370 EMSG(_("E874: (NFA) Could not pop the stack !"));
2371}
2372
2373/*
2374 * Push an item onto the stack.
2375 */
2376 static void
2377st_push(s, p, stack_end)
2378 Frag_T s;
2379 Frag_T **p;
2380 Frag_T *stack_end;
2381{
2382 Frag_T *stackp = *p;
2383
2384 if (stackp >= stack_end)
2385 return;
2386 *stackp = s;
2387 *p = *p + 1;
2388}
2389
2390/*
2391 * Pop an item from the stack.
2392 */
2393 static Frag_T
2394st_pop(p, stack)
2395 Frag_T **p;
2396 Frag_T *stack;
2397{
2398 Frag_T *stackp;
2399
2400 *p = *p - 1;
2401 stackp = *p;
2402 if (stackp < stack)
2403 return empty;
2404 return **p;
2405}
2406
2407/*
2408 * Convert a postfix form into its equivalent NFA.
2409 * Return the NFA start state on success, NULL otherwise.
2410 */
2411 static nfa_state_T *
2412post2nfa(postfix, end, nfa_calc_size)
2413 int *postfix;
2414 int *end;
2415 int nfa_calc_size;
2416{
2417 int *p;
2418 int mopen;
2419 int mclose;
2420 Frag_T *stack = NULL;
2421 Frag_T *stackp = NULL;
2422 Frag_T *stack_end = NULL;
2423 Frag_T e1;
2424 Frag_T e2;
2425 Frag_T e;
2426 nfa_state_T *s;
2427 nfa_state_T *s1;
2428 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002429 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430
2431 if (postfix == NULL)
2432 return NULL;
2433
Bram Moolenaar053bb602013-05-20 13:55:21 +02002434#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002435#define POP() st_pop(&stackp, stack); \
2436 if (stackp < stack) \
2437 { \
2438 st_error(postfix, end, p); \
2439 return NULL; \
2440 }
2441
2442 if (nfa_calc_size == FALSE)
2443 {
2444 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002445 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002446 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002447 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 }
2449
2450 for (p = postfix; p < end; ++p)
2451 {
2452 switch (*p)
2453 {
2454 case NFA_CONCAT:
2455 /* Catenation.
2456 * Pay attention: this operator does not exist
2457 * in the r.e. itself (it is implicit, really).
2458 * It is added when r.e. is translated to postfix
2459 * form in re2post().
2460 *
2461 * No new state added here. */
2462 if (nfa_calc_size == TRUE)
2463 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002464 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465 break;
2466 }
2467 e2 = POP();
2468 e1 = POP();
2469 patch(e1.out, e2.start);
2470 PUSH(frag(e1.start, e2.out));
2471 break;
2472
2473 case NFA_NOT:
2474 /* Negation of a character */
2475 if (nfa_calc_size == TRUE)
2476 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002477 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002478 break;
2479 }
2480 e1 = POP();
2481 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002482#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002483 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002484 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002485#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 PUSH(e1);
2487 break;
2488
2489 case NFA_OR:
2490 /* Alternation */
2491 if (nfa_calc_size == TRUE)
2492 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002493 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494 break;
2495 }
2496 e2 = POP();
2497 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002498 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002500 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002501 PUSH(frag(s, append(e1.out, e2.out)));
2502 break;
2503
2504 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002505 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 if (nfa_calc_size == TRUE)
2507 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002508 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002509 break;
2510 }
2511 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002512 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002513 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002514 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515 patch(e.out, s);
2516 PUSH(frag(s, list1(&s->out1)));
2517 break;
2518
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002519 case NFA_STAR_NONGREEDY:
2520 /* Zero or more, prefer zero */
2521 if (nfa_calc_size == TRUE)
2522 {
2523 nstate++;
2524 break;
2525 }
2526 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002527 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002528 if (s == NULL)
2529 goto theend;
2530 patch(e.out, s);
2531 PUSH(frag(s, list1(&s->out)));
2532 break;
2533
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002534 case NFA_QUEST:
2535 /* one or zero atoms=> greedy match */
2536 if (nfa_calc_size == TRUE)
2537 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002538 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539 break;
2540 }
2541 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002542 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002544 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002545 PUSH(frag(s, append(e.out, list1(&s->out1))));
2546 break;
2547
2548 case NFA_QUEST_NONGREEDY:
2549 /* zero or one atoms => non-greedy match */
2550 if (nfa_calc_size == TRUE)
2551 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002552 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002553 break;
2554 }
2555 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002556 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002557 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002558 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002559 PUSH(frag(s, append(e.out, list1(&s->out))));
2560 break;
2561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562 case NFA_SKIP_CHAR:
2563 /* Symbol of 0-length, Used in a repetition
2564 * with max/min count of 0 */
2565 if (nfa_calc_size == TRUE)
2566 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002567 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002568 break;
2569 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002570 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002571 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002572 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573 PUSH(frag(s, list1(&s->out)));
2574 break;
2575
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002576 case NFA_OPT_CHARS:
2577 {
2578 int n;
2579
2580 /* \%[abc] */
2581 n = *++p; /* get number of characters */
2582 if (nfa_calc_size == TRUE)
2583 {
2584 nstate += n;
2585 break;
2586 }
2587 e1.out = NULL; /* stores list with out1's */
2588 s1 = NULL; /* previous NFA_SPLIT to connect to */
2589 while (n-- > 0)
2590 {
2591 e = POP(); /* get character */
2592 s = alloc_state(NFA_SPLIT, e.start, NULL);
2593 if (s == NULL)
2594 goto theend;
2595 if (e1.out == NULL)
2596 e1 = e;
2597 patch(e.out, s1);
2598 append(e1.out, list1(&s->out1));
2599 s1 = s;
2600 }
2601 PUSH(frag(s, e1.out));
2602 break;
2603 }
2604
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002605 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002606 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002607 case NFA_PREV_ATOM_JUST_BEFORE:
2608 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002609 {
2610 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2611 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2612 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2613 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2614 int n;
2615
2616 if (before)
2617 n = *++p; /* get the count */
2618
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002619 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002620 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002621 * The \@<= operator: match for the preceding atom.
2622 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002623 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002624 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002625
2626 if (nfa_calc_size == TRUE)
2627 {
2628 nstate += 2;
2629 break;
2630 }
2631 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002632 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002633 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002634 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635 patch(e.out, s1);
2636
Bram Moolenaar525666f2013-06-02 16:40:55 +02002637 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002639 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002640 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002641 {
2642 s->negated = TRUE;
2643 s1->negated = TRUE;
2644 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002645 if (before)
Bram Moolenaar61602c52013-06-01 19:54:43 +02002646 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002647 s->val = n; /* store the count */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002648 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2649 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002650
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002651 PUSH(frag(s, list1(&s1->out)));
2652 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002653 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002655#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002656 case NFA_COMPOSING: /* char with composing char */
2657#if 0
2658 /* TODO */
2659 if (regflags & RF_ICOMBINE)
2660 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002661 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002662 }
2663#endif
2664 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002665#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002666
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002667 case NFA_MOPEN: /* \( \) Submatch */
2668 case NFA_MOPEN1:
2669 case NFA_MOPEN2:
2670 case NFA_MOPEN3:
2671 case NFA_MOPEN4:
2672 case NFA_MOPEN5:
2673 case NFA_MOPEN6:
2674 case NFA_MOPEN7:
2675 case NFA_MOPEN8:
2676 case NFA_MOPEN9:
2677#ifdef FEAT_SYN_HL
2678 case NFA_ZOPEN: /* \z( \) Submatch */
2679 case NFA_ZOPEN1:
2680 case NFA_ZOPEN2:
2681 case NFA_ZOPEN3:
2682 case NFA_ZOPEN4:
2683 case NFA_ZOPEN5:
2684 case NFA_ZOPEN6:
2685 case NFA_ZOPEN7:
2686 case NFA_ZOPEN8:
2687 case NFA_ZOPEN9:
2688#endif
2689 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002690 if (nfa_calc_size == TRUE)
2691 {
2692 nstate += 2;
2693 break;
2694 }
2695
2696 mopen = *p;
2697 switch (*p)
2698 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002699 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2700#ifdef FEAT_SYN_HL
2701 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2702 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2703 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2704 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2705 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2706 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2707 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2708 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2709 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2710 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2711#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002712#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002713 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002714#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002716 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002717 mclose = *p + NSUBEXP;
2718 break;
2719 }
2720
2721 /* Allow "NFA_MOPEN" as a valid postfix representation for
2722 * the empty regexp "". In this case, the NFA will be
2723 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2724 * empty groups of parenthesis, and empty mbyte chars */
2725 if (stackp == stack)
2726 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002727 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002728 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002729 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002730 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002732 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002733 patch(list1(&s->out), s1);
2734 PUSH(frag(s, list1(&s1->out)));
2735 break;
2736 }
2737
2738 /* At least one node was emitted before NFA_MOPEN, so
2739 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2740 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002741 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002743 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744
Bram Moolenaar525666f2013-06-02 16:40:55 +02002745 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002746 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002747 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748 patch(e.out, s1);
2749
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002750#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002751 if (mopen == NFA_COMPOSING)
2752 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002753 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002754#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002755
2756 PUSH(frag(s, list1(&s1->out)));
2757 break;
2758
Bram Moolenaar5714b802013-05-28 22:03:20 +02002759 case NFA_BACKREF1:
2760 case NFA_BACKREF2:
2761 case NFA_BACKREF3:
2762 case NFA_BACKREF4:
2763 case NFA_BACKREF5:
2764 case NFA_BACKREF6:
2765 case NFA_BACKREF7:
2766 case NFA_BACKREF8:
2767 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002768#ifdef FEAT_SYN_HL
2769 case NFA_ZREF1:
2770 case NFA_ZREF2:
2771 case NFA_ZREF3:
2772 case NFA_ZREF4:
2773 case NFA_ZREF5:
2774 case NFA_ZREF6:
2775 case NFA_ZREF7:
2776 case NFA_ZREF8:
2777 case NFA_ZREF9:
2778#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002779 if (nfa_calc_size == TRUE)
2780 {
2781 nstate += 2;
2782 break;
2783 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002784 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002785 if (s == NULL)
2786 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002787 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002788 if (s1 == NULL)
2789 goto theend;
2790 patch(list1(&s->out), s1);
2791 PUSH(frag(s, list1(&s1->out)));
2792 break;
2793
Bram Moolenaar423532e2013-05-29 21:14:42 +02002794 case NFA_LNUM:
2795 case NFA_LNUM_GT:
2796 case NFA_LNUM_LT:
2797 case NFA_VCOL:
2798 case NFA_VCOL_GT:
2799 case NFA_VCOL_LT:
2800 case NFA_COL:
2801 case NFA_COL_GT:
2802 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002803 case NFA_MARK:
2804 case NFA_MARK_GT:
2805 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002806 {
2807 int n = *++p; /* lnum, col or mark name */
2808
Bram Moolenaar423532e2013-05-29 21:14:42 +02002809 if (nfa_calc_size == TRUE)
2810 {
2811 nstate += 1;
2812 break;
2813 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002814 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002815 if (s == NULL)
2816 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002817 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002818 PUSH(frag(s, list1(&s->out)));
2819 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002820 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002821
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 case NFA_ZSTART:
2823 case NFA_ZEND:
2824 default:
2825 /* Operands */
2826 if (nfa_calc_size == TRUE)
2827 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002828 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002829 break;
2830 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002831 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002833 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834 PUSH(frag(s, list1(&s->out)));
2835 break;
2836
2837 } /* switch(*p) */
2838
2839 } /* for(p = postfix; *p; ++p) */
2840
2841 if (nfa_calc_size == TRUE)
2842 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002843 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002844 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845 }
2846
2847 e = POP();
2848 if (stackp != stack)
2849 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2850
2851 if (istate >= nstate)
2852 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2853
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854 matchstate = &state_ptr[istate++]; /* the match state */
2855 matchstate->c = NFA_MATCH;
2856 matchstate->out = matchstate->out1 = NULL;
2857
2858 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002859 ret = e.start;
2860
2861theend:
2862 vim_free(stack);
2863 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002864
2865#undef POP1
2866#undef PUSH1
2867#undef POP2
2868#undef PUSH2
2869#undef POP
2870#undef PUSH
2871}
2872
2873/****************************************************************
2874 * NFA execution code.
2875 ****************************************************************/
2876
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002877typedef struct
2878{
2879 int in_use; /* number of subexpr with useful info */
2880
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002881 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002882 union
2883 {
2884 struct multipos
2885 {
2886 lpos_T start;
2887 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002888 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002889 struct linepos
2890 {
2891 char_u *start;
2892 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002893 } line[NSUBEXP];
2894 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002895} regsub_T;
2896
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002897typedef struct
2898{
2899 regsub_T norm; /* \( .. \) matches */
2900#ifdef FEAT_SYN_HL
2901 regsub_T synt; /* \z( .. \) matches */
2902#endif
2903} regsubs_T;
2904
Bram Moolenaara2d95102013-06-04 14:23:05 +02002905/* nfa_pim_T stores a Postponed Invisible Match. */
2906typedef struct nfa_pim_S nfa_pim_T;
2907struct nfa_pim_S
2908{
2909 nfa_state_T *state;
2910 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2911 nfa_pim_T *pim; /* another PIM at the same position */
2912 regsubs_T subs; /* submatch info, only party used */
2913};
2914
2915/* Values for done in nfa_pim_T. */
2916#define NFA_PIM_TODO 0
2917#define NFA_PIM_MATCH 1
2918#define NFA_PIM_NOMATCH -1
2919
2920
Bram Moolenaar963fee22013-05-26 21:47:28 +02002921/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002922typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002923{
2924 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002925 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002926 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002927 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002928} nfa_thread_T;
2929
Bram Moolenaar963fee22013-05-26 21:47:28 +02002930/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931typedef struct
2932{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002933 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002934 int n; /* nr of states currently in "t" */
2935 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002936 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002937} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938
Bram Moolenaar5714b802013-05-28 22:03:20 +02002939#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002940static void log_subsexpr __ARGS((regsubs_T *subs));
2941static void log_subexpr __ARGS((regsub_T *sub));
2942
2943 static void
2944log_subsexpr(subs)
2945 regsubs_T *subs;
2946{
2947 log_subexpr(&subs->norm);
2948# ifdef FEAT_SYN_HL
2949 log_subexpr(&subs->synt);
2950# endif
2951}
2952
Bram Moolenaar5714b802013-05-28 22:03:20 +02002953 static void
2954log_subexpr(sub)
2955 regsub_T *sub;
2956{
2957 int j;
2958
2959 for (j = 0; j < sub->in_use; j++)
2960 if (REG_MULTI)
2961 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2962 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002963 sub->list.multi[j].start.col,
2964 (int)sub->list.multi[j].start.lnum,
2965 sub->list.multi[j].end.col,
2966 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002967 else
2968 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2969 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002970 (char *)sub->list.line[j].start,
2971 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002972 fprintf(log_fd, "\n");
2973}
2974#endif
2975
Bram Moolenaar963fee22013-05-26 21:47:28 +02002976/* Used during execution: whether a match has been found. */
2977static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002978
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002979static void clear_sub __ARGS((regsub_T *sub));
2980static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2981static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002982static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002983static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02002984static 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 +02002985
2986 static void
2987clear_sub(sub)
2988 regsub_T *sub;
2989{
2990 if (REG_MULTI)
2991 /* Use 0xff to set lnum to -1 */
2992 vim_memset(sub->list.multi, 0xff,
2993 sizeof(struct multipos) * nfa_nsubexpr);
2994 else
2995 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2996 sub->in_use = 0;
2997}
2998
2999/*
3000 * Copy the submatches from "from" to "to".
3001 */
3002 static void
3003copy_sub(to, from)
3004 regsub_T *to;
3005 regsub_T *from;
3006{
3007 to->in_use = from->in_use;
3008 if (from->in_use > 0)
3009 {
3010 /* Copy the match start and end positions. */
3011 if (REG_MULTI)
3012 mch_memmove(&to->list.multi[0],
3013 &from->list.multi[0],
3014 sizeof(struct multipos) * from->in_use);
3015 else
3016 mch_memmove(&to->list.line[0],
3017 &from->list.line[0],
3018 sizeof(struct linepos) * from->in_use);
3019 }
3020}
3021
3022/*
3023 * Like copy_sub() but exclude the main match.
3024 */
3025 static void
3026copy_sub_off(to, from)
3027 regsub_T *to;
3028 regsub_T *from;
3029{
3030 if (to->in_use < from->in_use)
3031 to->in_use = from->in_use;
3032 if (from->in_use > 1)
3033 {
3034 /* Copy the match start and end positions. */
3035 if (REG_MULTI)
3036 mch_memmove(&to->list.multi[1],
3037 &from->list.multi[1],
3038 sizeof(struct multipos) * (from->in_use - 1));
3039 else
3040 mch_memmove(&to->list.line[1],
3041 &from->list.line[1],
3042 sizeof(struct linepos) * (from->in_use - 1));
3043 }
3044}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045
Bram Moolenaar428e9872013-05-30 17:05:39 +02003046/*
3047 * Return TRUE if "sub1" and "sub2" have the same positions.
3048 */
3049 static int
3050sub_equal(sub1, sub2)
3051 regsub_T *sub1;
3052 regsub_T *sub2;
3053{
3054 int i;
3055 int todo;
3056 linenr_T s1, e1;
3057 linenr_T s2, e2;
3058 char_u *sp1, *ep1;
3059 char_u *sp2, *ep2;
3060
3061 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3062 if (REG_MULTI)
3063 {
3064 for (i = 0; i < todo; ++i)
3065 {
3066 if (i < sub1->in_use)
3067 {
3068 s1 = sub1->list.multi[i].start.lnum;
3069 e1 = sub1->list.multi[i].end.lnum;
3070 }
3071 else
3072 {
3073 s1 = 0;
3074 e1 = 0;
3075 }
3076 if (i < sub2->in_use)
3077 {
3078 s2 = sub2->list.multi[i].start.lnum;
3079 e2 = sub2->list.multi[i].end.lnum;
3080 }
3081 else
3082 {
3083 s2 = 0;
3084 e2 = 0;
3085 }
3086 if (s1 != s2 || e1 != e2)
3087 return FALSE;
3088 if (s1 != 0 && sub1->list.multi[i].start.col
3089 != sub2->list.multi[i].start.col)
3090 return FALSE;
3091 if (e1 != 0 && sub1->list.multi[i].end.col
3092 != sub2->list.multi[i].end.col)
3093 return FALSE;
3094 }
3095 }
3096 else
3097 {
3098 for (i = 0; i < todo; ++i)
3099 {
3100 if (i < sub1->in_use)
3101 {
3102 sp1 = sub1->list.line[i].start;
3103 ep1 = sub1->list.line[i].end;
3104 }
3105 else
3106 {
3107 sp1 = NULL;
3108 ep1 = NULL;
3109 }
3110 if (i < sub2->in_use)
3111 {
3112 sp2 = sub2->list.line[i].start;
3113 ep2 = sub2->list.line[i].end;
3114 }
3115 else
3116 {
3117 sp2 = NULL;
3118 ep2 = NULL;
3119 }
3120 if (sp1 != sp2 || ep1 != ep2)
3121 return FALSE;
3122 }
3123 }
3124
3125 return TRUE;
3126}
3127
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003128#ifdef ENABLE_LOG
3129 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003130report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003131{
3132 int col;
3133
3134 if (sub->in_use <= 0)
3135 col = -1;
3136 else if (REG_MULTI)
3137 col = sub->list.multi[0].start.col;
3138 else
3139 col = (int)(sub->list.line[0].start - regline);
3140 nfa_set_code(state->c);
3141 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3142 action, abs(state->id), lid, state->c, code, col);
3143}
3144#endif
3145
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003146 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003147addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003148 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003149 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003150 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003151 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003152{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003153 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003154 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003155 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003156 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003157 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003158 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003159 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003160#ifdef ENABLE_LOG
3161 int did_print = FALSE;
3162#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003163
3164 if (l == NULL || state == NULL)
3165 return;
3166
3167 switch (state->c)
3168 {
3169 case NFA_SPLIT:
3170 case NFA_NOT:
3171 case NFA_NOPEN:
3172 case NFA_NCLOSE:
3173 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003174 case NFA_MCLOSE1:
3175 case NFA_MCLOSE2:
3176 case NFA_MCLOSE3:
3177 case NFA_MCLOSE4:
3178 case NFA_MCLOSE5:
3179 case NFA_MCLOSE6:
3180 case NFA_MCLOSE7:
3181 case NFA_MCLOSE8:
3182 case NFA_MCLOSE9:
3183#ifdef FEAT_SYN_HL
3184 case NFA_ZCLOSE:
3185 case NFA_ZCLOSE1:
3186 case NFA_ZCLOSE2:
3187 case NFA_ZCLOSE3:
3188 case NFA_ZCLOSE4:
3189 case NFA_ZCLOSE5:
3190 case NFA_ZCLOSE6:
3191 case NFA_ZCLOSE7:
3192 case NFA_ZCLOSE8:
3193 case NFA_ZCLOSE9:
3194#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003195 /* These nodes are not added themselves but their "out" and/or
3196 * "out1" may be added below. */
3197 break;
3198
3199 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003200 case NFA_MOPEN1:
3201 case NFA_MOPEN2:
3202 case NFA_MOPEN3:
3203 case NFA_MOPEN4:
3204 case NFA_MOPEN5:
3205 case NFA_MOPEN6:
3206 case NFA_MOPEN7:
3207 case NFA_MOPEN8:
3208 case NFA_MOPEN9:
3209#ifdef FEAT_SYN_HL
3210 case NFA_ZOPEN:
3211 case NFA_ZOPEN1:
3212 case NFA_ZOPEN2:
3213 case NFA_ZOPEN3:
3214 case NFA_ZOPEN4:
3215 case NFA_ZOPEN5:
3216 case NFA_ZOPEN6:
3217 case NFA_ZOPEN7:
3218 case NFA_ZOPEN8:
3219 case NFA_ZOPEN9:
3220#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003221 /* These nodes do not need to be added, but we need to bail out
3222 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003223 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003224 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003225 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003226 break;
3227
Bram Moolenaar307aa162013-06-02 16:34:21 +02003228 case NFA_BOL:
3229 case NFA_BOF:
3230 /* "^" won't match past end-of-line, don't bother trying.
3231 * Except when we are going to the next line for a look-behind
3232 * match. */
3233 if (reginput > regline
3234 && (nfa_endp == NULL
3235 || !REG_MULTI
3236 || reglnum == nfa_endp->se_u.pos.lnum))
3237 goto skip_add;
3238 /* FALLTHROUGH */
3239
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003241 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003242 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003243 /* This state is already in the list, don't add it again,
3244 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003245 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003246 {
3247skip_add:
3248#ifdef ENABLE_LOG
3249 nfa_set_code(state->c);
3250 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3251 abs(state->id), l->id, state->c, code);
3252#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003253 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003254 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003255
3256 /* See if the same state is already in the list with the same
3257 * positions. */
3258 for (i = 0; i < l->n; ++i)
3259 {
3260 thread = &l->t[i];
3261 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003262 && sub_equal(&thread->subs.norm, &subs->norm)
3263#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003264 && (!nfa_has_zsubexpr ||
3265 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003266#endif
3267 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003268 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003269 }
3270 }
3271
Bram Moolenaara2d95102013-06-04 14:23:05 +02003272 /* when there are backreferences or look-behind matches the number
3273 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003274 if (nfa_has_backref && l->n == l->len)
3275 {
3276 int newlen = l->len * 3 / 2 + 50;
3277
3278 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3279 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003280 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003281
3282 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003283 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003284 thread = &l->t[l->n++];
3285 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003286 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003287 copy_sub(&thread->subs.norm, &subs->norm);
3288#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003289 if (nfa_has_zsubexpr)
3290 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003291#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003292#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003293 report_state("Adding", &thread->subs.norm, state, l->id);
3294 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003295#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 }
3297
3298#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003299 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003300 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301#endif
3302 switch (state->c)
3303 {
3304 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003305 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306 break;
3307
3308 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003309 /* order matters here */
3310 addstate(l, state->out, subs, off);
3311 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 break;
3313
Bram Moolenaar5714b802013-05-28 22:03:20 +02003314 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003315 case NFA_NOPEN:
3316 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003317 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 break;
3319
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003320 case NFA_MOPEN:
3321 case NFA_MOPEN1:
3322 case NFA_MOPEN2:
3323 case NFA_MOPEN3:
3324 case NFA_MOPEN4:
3325 case NFA_MOPEN5:
3326 case NFA_MOPEN6:
3327 case NFA_MOPEN7:
3328 case NFA_MOPEN8:
3329 case NFA_MOPEN9:
3330#ifdef FEAT_SYN_HL
3331 case NFA_ZOPEN:
3332 case NFA_ZOPEN1:
3333 case NFA_ZOPEN2:
3334 case NFA_ZOPEN3:
3335 case NFA_ZOPEN4:
3336 case NFA_ZOPEN5:
3337 case NFA_ZOPEN6:
3338 case NFA_ZOPEN7:
3339 case NFA_ZOPEN8:
3340 case NFA_ZOPEN9:
3341#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003342 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003343 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003344 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003345 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003346 sub = &subs->norm;
3347 }
3348#ifdef FEAT_SYN_HL
3349 else if (state->c >= NFA_ZOPEN)
3350 {
3351 subidx = state->c - NFA_ZOPEN;
3352 sub = &subs->synt;
3353 }
3354#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003355 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003356 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003357 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003358 sub = &subs->norm;
3359 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003361 /* Set the position (with "off") in the subexpression. Save and
3362 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003363 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 if (REG_MULTI)
3365 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003366 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003367 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003368 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003369 save_in_use = -1;
3370 }
3371 else
3372 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003373 save_in_use = sub->in_use;
3374 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003375 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003376 sub->list.multi[i].start.lnum = -1;
3377 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003378 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003379 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003380 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003381 if (off == -1)
3382 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003383 sub->list.multi[subidx].start.lnum = reglnum + 1;
3384 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003385 }
3386 else
3387 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003388 sub->list.multi[subidx].start.lnum = reglnum;
3389 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003390 (colnr_T)(reginput - regline + off);
3391 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003392 }
3393 else
3394 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003395 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003396 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003397 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003398 save_in_use = -1;
3399 }
3400 else
3401 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003402 save_in_use = sub->in_use;
3403 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003404 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003405 sub->list.line[i].start = NULL;
3406 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003407 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003408 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003409 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003410 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411 }
3412
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003413 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003414
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003415 if (save_in_use == -1)
3416 {
3417 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003418 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003419 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003420 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003421 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003423 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 break;
3425
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003426 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003427 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003429 /* Do not overwrite the position set by \ze. If no \ze
3430 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003431 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003432 break;
3433 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003434 case NFA_MCLOSE1:
3435 case NFA_MCLOSE2:
3436 case NFA_MCLOSE3:
3437 case NFA_MCLOSE4:
3438 case NFA_MCLOSE5:
3439 case NFA_MCLOSE6:
3440 case NFA_MCLOSE7:
3441 case NFA_MCLOSE8:
3442 case NFA_MCLOSE9:
3443#ifdef FEAT_SYN_HL
3444 case NFA_ZCLOSE:
3445 case NFA_ZCLOSE1:
3446 case NFA_ZCLOSE2:
3447 case NFA_ZCLOSE3:
3448 case NFA_ZCLOSE4:
3449 case NFA_ZCLOSE5:
3450 case NFA_ZCLOSE6:
3451 case NFA_ZCLOSE7:
3452 case NFA_ZCLOSE8:
3453 case NFA_ZCLOSE9:
3454#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003457 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003459 sub = &subs->norm;
3460 }
3461#ifdef FEAT_SYN_HL
3462 else if (state->c >= NFA_ZCLOSE)
3463 {
3464 subidx = state->c - NFA_ZCLOSE;
3465 sub = &subs->synt;
3466 }
3467#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003468 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003469 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003470 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003471 sub = &subs->norm;
3472 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003473
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003474 /* We don't fill in gaps here, there must have been an MOPEN that
3475 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003476 save_in_use = sub->in_use;
3477 if (sub->in_use <= subidx)
3478 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 if (REG_MULTI)
3480 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003481 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003482 if (off == -1)
3483 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003484 sub->list.multi[subidx].end.lnum = reglnum + 1;
3485 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003486 }
3487 else
3488 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003489 sub->list.multi[subidx].end.lnum = reglnum;
3490 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003491 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003492 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 }
3494 else
3495 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003496 save_ptr = sub->list.line[subidx].end;
3497 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498 }
3499
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003500 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003501
3502 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003503 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003505 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003506 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003507 break;
3508 }
3509}
3510
3511/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003512 * Like addstate(), but the new state(s) are put at position "*ip".
3513 * Used for zero-width matches, next state to use is the added one.
3514 * This makes sure the order of states to be tried does not change, which
3515 * matters for alternatives.
3516 */
3517 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003518addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003519 nfa_list_T *l; /* runtime state list */
3520 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003521 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003522 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003523 int *ip;
3524{
3525 int tlen = l->n;
3526 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003527 int listidx = *ip;
3528 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003529
3530 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003531 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003532
Bram Moolenaara2d95102013-06-04 14:23:05 +02003533 /* fill in the "pim" field in the new states */
3534 if (pim != NULL)
3535 for (i = tlen; i < l->n; ++i)
3536 l->t[i].pim = pim;
3537
Bram Moolenaar4b417062013-05-25 20:19:50 +02003538 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003539 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003540 return;
3541
3542 /* re-order to put the new state at the current position */
3543 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003544 if (count == 1)
3545 {
3546 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003547 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003548 }
3549 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003550 {
3551 /* make space for new states, then move them from the
3552 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003553 mch_memmove(&(l->t[listidx + count]),
3554 &(l->t[listidx + 1]),
3555 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3556 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003557 &(l->t[l->n - 1]),
3558 sizeof(nfa_thread_T) * count);
3559 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003560 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003561 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003562}
3563
3564/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 * Check character class "class" against current character c.
3566 */
3567 static int
3568check_char_class(class, c)
3569 int class;
3570 int c;
3571{
3572 switch (class)
3573 {
3574 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003575 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003576 return OK;
3577 break;
3578 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003579 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 return OK;
3581 break;
3582 case NFA_CLASS_BLANK:
3583 if (c == ' ' || c == '\t')
3584 return OK;
3585 break;
3586 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003587 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 return OK;
3589 break;
3590 case NFA_CLASS_DIGIT:
3591 if (VIM_ISDIGIT(c))
3592 return OK;
3593 break;
3594 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003595 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596 return OK;
3597 break;
3598 case NFA_CLASS_LOWER:
3599 if (MB_ISLOWER(c))
3600 return OK;
3601 break;
3602 case NFA_CLASS_PRINT:
3603 if (vim_isprintc(c))
3604 return OK;
3605 break;
3606 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003607 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003608 return OK;
3609 break;
3610 case NFA_CLASS_SPACE:
3611 if ((c >=9 && c <= 13) || (c == ' '))
3612 return OK;
3613 break;
3614 case NFA_CLASS_UPPER:
3615 if (MB_ISUPPER(c))
3616 return OK;
3617 break;
3618 case NFA_CLASS_XDIGIT:
3619 if (vim_isxdigit(c))
3620 return OK;
3621 break;
3622 case NFA_CLASS_TAB:
3623 if (c == '\t')
3624 return OK;
3625 break;
3626 case NFA_CLASS_RETURN:
3627 if (c == '\r')
3628 return OK;
3629 break;
3630 case NFA_CLASS_BACKSPACE:
3631 if (c == '\b')
3632 return OK;
3633 break;
3634 case NFA_CLASS_ESCAPE:
3635 if (c == '\033')
3636 return OK;
3637 break;
3638
3639 default:
3640 /* should not be here :P */
3641 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3642 }
3643 return FAIL;
3644}
3645
Bram Moolenaar5714b802013-05-28 22:03:20 +02003646static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3647
3648/*
3649 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003650 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003651 */
3652 static int
3653match_backref(sub, subidx, bytelen)
3654 regsub_T *sub; /* pointers to subexpressions */
3655 int subidx;
3656 int *bytelen; /* out: length of match in bytes */
3657{
3658 int len;
3659
3660 if (sub->in_use <= subidx)
3661 {
3662retempty:
3663 /* backref was not set, match an empty string */
3664 *bytelen = 0;
3665 return TRUE;
3666 }
3667
3668 if (REG_MULTI)
3669 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003670 if (sub->list.multi[subidx].start.lnum < 0
3671 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003672 goto retempty;
3673 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003674 len = sub->list.multi[subidx].end.col
3675 - sub->list.multi[subidx].start.col;
3676 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003677 reginput, &len) == 0)
3678 {
3679 *bytelen = len;
3680 return TRUE;
3681 }
3682 }
3683 else
3684 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003685 if (sub->list.line[subidx].start == NULL
3686 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003687 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003688 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3689 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003690 {
3691 *bytelen = len;
3692 return TRUE;
3693 }
3694 }
3695 return FALSE;
3696}
3697
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003698#ifdef FEAT_SYN_HL
3699
3700static int match_zref __ARGS((int subidx, int *bytelen));
3701
3702/*
3703 * Check for a match with \z subexpression "subidx".
3704 * Return TRUE if it matches.
3705 */
3706 static int
3707match_zref(subidx, bytelen)
3708 int subidx;
3709 int *bytelen; /* out: length of match in bytes */
3710{
3711 int len;
3712
3713 cleanup_zsubexpr();
3714 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3715 {
3716 /* backref was not set, match an empty string */
3717 *bytelen = 0;
3718 return TRUE;
3719 }
3720
3721 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3722 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3723 {
3724 *bytelen = len;
3725 return TRUE;
3726 }
3727 return FALSE;
3728}
3729#endif
3730
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003731/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003732 * Save list IDs for all NFA states of "prog" into "list".
3733 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003734 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003735 */
3736 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003737nfa_save_listids(prog, list)
3738 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 int *list;
3740{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003741 int i;
3742 nfa_state_T *p;
3743
3744 /* Order in the list is reverse, it's a bit faster that way. */
3745 p = &prog->state[0];
3746 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003748 list[i] = p->lastlist[1];
3749 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003750 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003751 }
3752}
3753
3754/*
3755 * Restore list IDs from "list" to all NFA states.
3756 */
3757 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003758nfa_restore_listids(prog, list)
3759 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760 int *list;
3761{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003762 int i;
3763 nfa_state_T *p;
3764
3765 p = &prog->state[0];
3766 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003768 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003769 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003770 }
3771}
3772
Bram Moolenaar423532e2013-05-29 21:14:42 +02003773 static int
3774nfa_re_num_cmp(val, op, pos)
3775 long_u val;
3776 int op;
3777 long_u pos;
3778{
3779 if (op == 1) return pos > val;
3780 if (op == 2) return pos < val;
3781 return val == pos;
3782}
3783
Bram Moolenaarf46da702013-06-02 22:37:42 +02003784static 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 +02003785static 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 +02003786
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003787/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003788 * Recursively call nfa_regmatch()
3789 */
3790 static int
3791recursive_regmatch(state, prog, submatch, m, listids)
3792 nfa_state_T *state;
3793 nfa_regprog_T *prog;
3794 regsubs_T *submatch;
3795 regsubs_T *m;
3796 int **listids;
3797{
3798 char_u *save_reginput = reginput;
3799 char_u *save_regline = regline;
3800 int save_reglnum = reglnum;
3801 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003802 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003803 save_se_T *save_nfa_endp = nfa_endp;
3804 save_se_T endpos;
3805 save_se_T *endposp = NULL;
3806 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003807 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003808
3809 if (state->c == NFA_START_INVISIBLE_BEFORE)
3810 {
3811 /* The recursive match must end at the current position. */
3812 endposp = &endpos;
3813 if (REG_MULTI)
3814 {
3815 endpos.se_u.pos.col = (int)(reginput - regline);
3816 endpos.se_u.pos.lnum = reglnum;
3817 }
3818 else
3819 endpos.se_u.ptr = reginput;
3820
3821 /* Go back the specified number of bytes, or as far as the
3822 * start of the previous line, to try matching "\@<=" or
3823 * not matching "\@<!".
3824 * TODO: This is very inefficient! Would be better to
3825 * first check for a match with what follows. */
3826 if (state->val <= 0)
3827 {
3828 if (REG_MULTI)
3829 {
3830 regline = reg_getline(--reglnum);
3831 if (regline == NULL)
3832 /* can't go before the first line */
3833 regline = reg_getline(++reglnum);
3834 }
3835 reginput = regline;
3836 }
3837 else
3838 {
3839 if (REG_MULTI && (int)(reginput - regline) < state->val)
3840 {
3841 /* Not enough bytes in this line, go to end of
3842 * previous line. */
3843 regline = reg_getline(--reglnum);
3844 if (regline == NULL)
3845 {
3846 /* can't go before the first line */
3847 regline = reg_getline(++reglnum);
3848 reginput = regline;
3849 }
3850 else
3851 reginput = regline + STRLEN(regline);
3852 }
3853 if ((int)(reginput - regline) >= state->val)
3854 {
3855 reginput -= state->val;
3856#ifdef FEAT_MBYTE
3857 if (has_mbyte)
3858 reginput -= mb_head_off(regline, reginput);
3859#endif
3860 }
3861 else
3862 reginput = regline;
3863 }
3864 }
3865
Bram Moolenaarf46da702013-06-02 22:37:42 +02003866#ifdef ENABLE_LOG
3867 if (log_fd != stderr)
3868 fclose(log_fd);
3869 log_fd = NULL;
3870#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003871 /* Have to clear the lastlist field of the NFA nodes, so that
3872 * nfa_regmatch() and addstate() can run properly after recursion. */
3873 if (nfa_ll_index == 1)
3874 {
3875 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3876 * values and clear them. */
3877 if (*listids == NULL)
3878 {
3879 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3880 if (*listids == NULL)
3881 {
3882 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3883 return 0;
3884 }
3885 }
3886 nfa_save_listids(prog, *listids);
3887 need_restore = TRUE;
3888 /* any value of nfa_listid will do */
3889 }
3890 else
3891 {
3892 /* First recursive nfa_regmatch() call, switch to the second lastlist
3893 * entry. Make sure nfa_listid is different from a previous recursive
3894 * call, because some states may still have this ID. */
3895 ++nfa_ll_index;
3896 if (nfa_listid <= nfa_alt_listid)
3897 nfa_listid = nfa_alt_listid;
3898 }
3899
3900 /* Call nfa_regmatch() to check if the current concat matches at this
3901 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003902 nfa_endp = endposp;
3903 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003904
3905 if (need_restore)
3906 nfa_restore_listids(prog, *listids);
3907 else
3908 {
3909 --nfa_ll_index;
3910 nfa_alt_listid = nfa_listid;
3911 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003912
3913 /* restore position in input text */
3914 reginput = save_reginput;
3915 regline = save_regline;
3916 reglnum = save_reglnum;
3917 nfa_match = save_nfa_match;
3918 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003919 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003920
3921#ifdef ENABLE_LOG
3922 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3923 if (log_fd != NULL)
3924 {
3925 fprintf(log_fd, "****************************\n");
3926 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3927 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3928 fprintf(log_fd, "****************************\n");
3929 }
3930 else
3931 {
3932 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3933 log_fd = stderr;
3934 }
3935#endif
3936
3937 return result;
3938}
3939
Bram Moolenaara2d95102013-06-04 14:23:05 +02003940static int failure_chance __ARGS((nfa_state_T *state, int depth));
3941
3942/*
3943 * Estimate the chance of a match with "state" failing.
3944 * NFA_ANY: 1
3945 * specific character: 99
3946 */
3947 static int
3948failure_chance(state, depth)
3949 nfa_state_T *state;
3950 int depth;
3951{
3952 int c = state->c;
3953 int l, r;
3954
3955 /* detect looping */
3956 if (depth > 4)
3957 return 1;
3958
3959 if (c == NFA_SPLIT)
3960 {
3961 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3962 return 1;
3963 l = failure_chance(state->out, depth + 1);
3964 r = failure_chance(state->out1, depth + 1);
3965 return l < r ? l : r;
3966 }
3967 if (c == NFA_ANY)
3968 return 1;
3969 if (c > 0)
3970 return 99;
3971 if ((c >= NFA_MOPEN && c <= NFA_MOPEN9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02003972#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02003973 || (c >= NFA_ZOPEN && c <= NFA_ZOPEN9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02003974#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02003975 || c == NFA_NOPEN)
3976 return failure_chance(state->out, depth + 1);
3977 /* something else */
3978 return 50;
3979}
3980
Bram Moolenaarf46da702013-06-02 22:37:42 +02003981/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003982 * Main matching routine.
3983 *
3984 * Run NFA to determine whether it matches reginput.
3985 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003986 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003987 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003988 * Return TRUE if there is a match, FALSE otherwise.
3989 * Note: Caller must ensure that: start != NULL.
3990 */
3991 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003992nfa_regmatch(prog, start, submatch, m)
3993 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003994 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003995 regsubs_T *submatch;
3996 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003997{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003998 int result;
3999 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004000 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004001 int go_to_nextline = FALSE;
4002 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004003 nfa_list_T list[3];
4004 nfa_list_T *listtbl[2][2];
4005 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004006 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004007 nfa_list_T *thislist;
4008 nfa_list_T *nextlist;
4009 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004010 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004011 nfa_state_T *add_state;
4012 int add_count;
4013 int add_off;
4014 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004015#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004016 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004017
4018 if (debug == NULL)
4019 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004020 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004021 return FALSE;
4022 }
4023#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004024 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004025 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004026
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004027 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004028 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004029 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4030 list[0].len = nstate + 1;
4031 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4032 list[1].len = nstate + 1;
4033 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4034 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004035 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
4036 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037
4038#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004039 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004040 if (log_fd != NULL)
4041 {
4042 fprintf(log_fd, "**********************************\n");
4043 nfa_set_code(start->c);
4044 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4045 abs(start->id), code);
4046 fprintf(log_fd, "**********************************\n");
4047 }
4048 else
4049 {
4050 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4051 log_fd = stderr;
4052 }
4053#endif
4054
4055 thislist = &list[0];
4056 thislist->n = 0;
4057 nextlist = &list[1];
4058 nextlist->n = 0;
4059 neglist = &list[2];
4060 neglist->n = 0;
4061#ifdef ENABLE_LOG
4062 fprintf(log_fd, "(---) STARTSTATE\n");
4063#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004064 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004065 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004066
4067 /* There are two cases when the NFA advances: 1. input char matches the
4068 * NFA node and 2. input char does not match the NFA node, but the next
4069 * node is NFA_NOT. The following macro calls addstate() according to
4070 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4071 listtbl[0][0] = NULL;
4072 listtbl[0][1] = neglist;
4073 listtbl[1][0] = nextlist;
4074 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004075#define ADD_POS_NEG_STATE(state) \
4076 ll = listtbl[result ? 1 : 0][state->negated]; \
4077 if (ll != NULL) { \
4078 add_state = state->out; \
4079 add_off = clen; \
4080 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004081
4082 /*
4083 * Run for each character.
4084 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004085 for (;;)
4086 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004087 int curc;
4088 int clen;
4089
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004090#ifdef FEAT_MBYTE
4091 if (has_mbyte)
4092 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004093 curc = (*mb_ptr2char)(reginput);
4094 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 }
4096 else
4097#endif
4098 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004099 curc = *reginput;
4100 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004101 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004102 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004103 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004104 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004105 go_to_nextline = FALSE;
4106 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004107
4108 /* swap lists */
4109 thislist = &list[flag];
4110 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004111 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004112 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004113 ++nfa_listid;
4114 thislist->id = nfa_listid;
4115 nextlist->id = nfa_listid + 1;
4116 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117
Bram Moolenaara2d95102013-06-04 14:23:05 +02004118 pimlist.ga_len = 0;
4119
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004120#ifdef ENABLE_LOG
4121 fprintf(log_fd, "------------------------------------------\n");
4122 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004123 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004124 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004125 {
4126 int i;
4127
4128 for (i = 0; i < thislist->n; i++)
4129 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4130 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131 fprintf(log_fd, "\n");
4132#endif
4133
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004134#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135 fprintf(debug, "\n-------------------\n");
4136#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004137 /*
4138 * If the state lists are empty we can stop.
4139 */
4140 if (thislist->n == 0 && neglist->n == 0)
4141 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004142
4143 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004144 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004145 {
4146 if (neglist->n > 0)
4147 {
4148 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004149 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004150 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004151 }
4152 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004153 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004154
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004155#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004156 nfa_set_code(t->state->c);
4157 fprintf(debug, "%s, ", code);
4158#endif
4159#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004160 {
4161 int col;
4162
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004163 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004164 col = -1;
4165 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004166 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004167 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004168 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004169 nfa_set_code(t->state->c);
4170 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4171 abs(t->state->id), (int)t->state->c, code, col);
4172 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004173#endif
4174
4175 /*
4176 * Handle the possible codes of the current state.
4177 * The most important is NFA_MATCH.
4178 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004179 add_state = NULL;
4180 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004181 switch (t->state->c)
4182 {
4183 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004184 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004185 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004186 copy_sub(&submatch->norm, &t->subs.norm);
4187#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004188 if (nfa_has_zsubexpr)
4189 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004190#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004191#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004192 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004193#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004194 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004195 * states at this position. When the list of states is going
4196 * to be empty quit without advancing, so that "reginput" is
4197 * correct. */
4198 if (nextlist->n == 0 && neglist->n == 0)
4199 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004200 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004201 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004202
4203 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004204 /*
4205 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004206 * NFA_START_INVISIBLE_BEFORE node.
4207 * They surround a zero-width group, used with "\@=", "\&",
4208 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004209 * If we got here, it means that the current "invisible" group
4210 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004211 * nfa_regmatch(). For a look-behind match only when it ends
4212 * in the position in "nfa_endp".
4213 * Submatches are stored in *m, and used in the parent call.
4214 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004215#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004216 if (nfa_endp != NULL)
4217 {
4218 if (REG_MULTI)
4219 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4220 (int)reglnum,
4221 (int)nfa_endp->se_u.pos.lnum,
4222 (int)(reginput - regline),
4223 nfa_endp->se_u.pos.col);
4224 else
4225 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4226 (int)(reginput - regline),
4227 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004228 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004229#endif
4230 /* It's only a match if it ends at "nfa_endp" */
4231 if (nfa_endp != NULL && (REG_MULTI
4232 ? (reglnum != nfa_endp->se_u.pos.lnum
4233 || (int)(reginput - regline)
4234 != nfa_endp->se_u.pos.col)
4235 : reginput != nfa_endp->se_u.ptr))
4236 break;
4237
4238 /* do not set submatches for \@! */
4239 if (!t->state->negated)
4240 {
4241 copy_sub(&m->norm, &t->subs.norm);
4242#ifdef FEAT_SYN_HL
4243 if (nfa_has_zsubexpr)
4244 copy_sub(&m->synt, &t->subs.synt);
4245#endif
4246 }
4247 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248 break;
4249
4250 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004251 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2d95102013-06-04 14:23:05 +02004252 /* If invisible match has a higher chance to fail, do it
4253 * right away. Otherwise postpone it until what follows is
4254 * matching and causes addstate(nextlist, ..) to be called.
4255 * This is indicated by the "pim" field. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004257 nfa_pim_T *pim;
4258 int cout = t->state->out1->out->c;
4259
4260 /* Do it directly when what follows is possibly end of
4261 * match (closing paren).
4262 * Postpone when it is \@<= or \@<!, these are expensive.
4263 * TODO: remove the check for t->pim and check multiple
4264 * where it's used?
4265 * Otherwise first do the one that has the highest chance
4266 * of failing. */
4267 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004268#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004269 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004270#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004271 || cout == NFA_NCLOSE
4272 || t->pim != NULL
4273 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4274 && failure_chance(t->state->out1->out, 0)
4275 < failure_chance(t->state->out, 0)))
4276 {
4277 /*
4278 * First try matching the invisible match, then what
4279 * follows.
4280 */
4281 result = recursive_regmatch(t->state, prog,
4282 submatch, m, &listids);
4283
4284 /* for \@! it is a match when result is FALSE */
4285 if (result != t->state->negated)
4286 {
4287 /* Copy submatch info from the recursive call */
4288 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004289#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004290 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004291#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004292
Bram Moolenaara2d95102013-06-04 14:23:05 +02004293 /* t->state->out1 is the corresponding
4294 * END_INVISIBLE node; Add its out to the current
4295 * list (zero-width match). */
4296 addstate_here(thislist, t->state->out1->out,
4297 &t->subs, t->pim, &listidx);
4298 }
4299 }
4300 else
4301 {
4302 /*
4303 * First try matching what follows at the current
4304 * position. Only if a match is found, addstate() is
4305 * called, then verify the invisible match matches.
4306 * Add a nfa_pim_T to the following states, it
4307 * contains info about the invisible match.
4308 */
4309 if (ga_grow(&pimlist, 1) == FAIL)
4310 goto theend;
4311 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4312 ++pimlist.ga_len;
4313 pim->state = t->state;
4314 pim->pim = NULL;
4315 pim->result = NFA_PIM_TODO;
4316
4317 /* t->state->out1 is the corresponding END_INVISIBLE
4318 * node; Add its out to the current list (zero-width
4319 * match). */
4320 addstate_here(thislist, t->state->out1->out, &t->subs,
4321 pim, &listidx);
4322 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004324 break;
4325
4326 case NFA_BOL:
4327 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004328 addstate_here(thislist, t->state->out, &t->subs,
4329 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330 break;
4331
4332 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004333 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004334 addstate_here(thislist, t->state->out, &t->subs,
4335 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336 break;
4337
4338 case NFA_BOW:
4339 {
4340 int bow = TRUE;
4341
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004342 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004343 bow = FALSE;
4344#ifdef FEAT_MBYTE
4345 else if (has_mbyte)
4346 {
4347 int this_class;
4348
4349 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004350 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004351 if (this_class <= 1)
4352 bow = FALSE;
4353 else if (reg_prev_class() == this_class)
4354 bow = FALSE;
4355 }
4356#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004357 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004358 || (reginput > regline
4359 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004360 bow = FALSE;
4361 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004362 addstate_here(thislist, t->state->out, &t->subs,
4363 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004364 break;
4365 }
4366
4367 case NFA_EOW:
4368 {
4369 int eow = TRUE;
4370
4371 if (reginput == regline)
4372 eow = FALSE;
4373#ifdef FEAT_MBYTE
4374 else if (has_mbyte)
4375 {
4376 int this_class, prev_class;
4377
4378 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004379 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004380 prev_class = reg_prev_class();
4381 if (this_class == prev_class
4382 || prev_class == 0 || prev_class == 1)
4383 eow = FALSE;
4384 }
4385#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004386 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004387 || (reginput[0] != NUL
4388 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004389 eow = FALSE;
4390 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004391 addstate_here(thislist, t->state->out, &t->subs,
4392 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 break;
4394 }
4395
Bram Moolenaar4b780632013-05-31 22:14:52 +02004396 case NFA_BOF:
4397 if (reglnum == 0 && reginput == regline
4398 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004399 addstate_here(thislist, t->state->out, &t->subs,
4400 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004401 break;
4402
4403 case NFA_EOF:
4404 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004405 addstate_here(thislist, t->state->out, &t->subs,
4406 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004407 break;
4408
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004409#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004410 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004411 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004412 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004413 int len = 0;
4414 nfa_state_T *end;
4415 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004416 int cchars[MAX_MCO];
4417 int ccount = 0;
4418 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004419
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004420 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004421 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004422 if (utf_iscomposing(sta->c))
4423 {
4424 /* Only match composing character(s), ignore base
4425 * character. Used for ".{composing}" and "{composing}"
4426 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004427 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004428 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004429 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004430 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004431 /* If \Z was present, then ignore composing characters.
4432 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004433 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004434 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004435 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004436 else
4437 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004438 while (sta->c != NFA_END_COMPOSING)
4439 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004440 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004441
4442 /* Check base character matches first, unless ignored. */
4443 else if (len > 0 || mc == sta->c)
4444 {
4445 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004446 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004447 len += mb_char2len(mc);
4448 sta = sta->out;
4449 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004450
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004451 /* We don't care about the order of composing characters.
4452 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004453 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004454 {
4455 mc = mb_ptr2char(reginput + len);
4456 cchars[ccount++] = mc;
4457 len += mb_char2len(mc);
4458 if (ccount == MAX_MCO)
4459 break;
4460 }
4461
4462 /* Check that each composing char in the pattern matches a
4463 * composing char in the text. We do not check if all
4464 * composing chars are matched. */
4465 result = OK;
4466 while (sta->c != NFA_END_COMPOSING)
4467 {
4468 for (j = 0; j < ccount; ++j)
4469 if (cchars[j] == sta->c)
4470 break;
4471 if (j == ccount)
4472 {
4473 result = FAIL;
4474 break;
4475 }
4476 sta = sta->out;
4477 }
4478 }
4479 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004480 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004481
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004482 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004483 ADD_POS_NEG_STATE(end);
4484 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004485 }
4486#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004487
4488 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004489 if (curc == NUL && !reg_line_lbr && REG_MULTI
4490 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004491 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004492 go_to_nextline = TRUE;
4493 /* Pass -1 for the offset, which means taking the position
4494 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004495 ll = nextlist;
4496 add_state = t->state->out;
4497 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004498 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004499 else if (curc == '\n' && reg_line_lbr)
4500 {
4501 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004502 ll = nextlist;
4503 add_state = t->state->out;
4504 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004505 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004506 break;
4507
4508 case NFA_CLASS_ALNUM:
4509 case NFA_CLASS_ALPHA:
4510 case NFA_CLASS_BLANK:
4511 case NFA_CLASS_CNTRL:
4512 case NFA_CLASS_DIGIT:
4513 case NFA_CLASS_GRAPH:
4514 case NFA_CLASS_LOWER:
4515 case NFA_CLASS_PRINT:
4516 case NFA_CLASS_PUNCT:
4517 case NFA_CLASS_SPACE:
4518 case NFA_CLASS_UPPER:
4519 case NFA_CLASS_XDIGIT:
4520 case NFA_CLASS_TAB:
4521 case NFA_CLASS_RETURN:
4522 case NFA_CLASS_BACKSPACE:
4523 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004524 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 ADD_POS_NEG_STATE(t->state);
4526 break;
4527
4528 case NFA_END_NEG_RANGE:
4529 /* This follows a series of negated nodes, like:
4530 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004531 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004532 {
4533 ll = nextlist;
4534 add_state = t->state->out;
4535 add_off = clen;
4536 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004537 break;
4538
4539 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004540 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004541 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004542 {
4543 ll = nextlist;
4544 add_state = t->state->out;
4545 add_off = clen;
4546 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004547 break;
4548
4549 /*
4550 * Character classes like \a for alpha, \d for digit etc.
4551 */
4552 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004553 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 ADD_POS_NEG_STATE(t->state);
4555 break;
4556
4557 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004558 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 ADD_POS_NEG_STATE(t->state);
4560 break;
4561
4562 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004563 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004564 ADD_POS_NEG_STATE(t->state);
4565 break;
4566
4567 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004568 result = !VIM_ISDIGIT(curc)
4569 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004570 ADD_POS_NEG_STATE(t->state);
4571 break;
4572
4573 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004574 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004575 ADD_POS_NEG_STATE(t->state);
4576 break;
4577
4578 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004579 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580 ADD_POS_NEG_STATE(t->state);
4581 break;
4582
4583 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004584 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004585 ADD_POS_NEG_STATE(t->state);
4586 break;
4587
4588 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004589 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004590 ADD_POS_NEG_STATE(t->state);
4591 break;
4592
4593 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004594 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004595 ADD_POS_NEG_STATE(t->state);
4596 break;
4597
4598 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004599 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 ADD_POS_NEG_STATE(t->state);
4601 break;
4602
4603 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004604 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004605 ADD_POS_NEG_STATE(t->state);
4606 break;
4607
4608 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004609 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004610 ADD_POS_NEG_STATE(t->state);
4611 break;
4612
4613 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004614 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004615 ADD_POS_NEG_STATE(t->state);
4616 break;
4617
4618 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004619 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004620 ADD_POS_NEG_STATE(t->state);
4621 break;
4622
4623 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004624 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004625 ADD_POS_NEG_STATE(t->state);
4626 break;
4627
4628 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004629 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004630 ADD_POS_NEG_STATE(t->state);
4631 break;
4632
4633 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004634 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004635 ADD_POS_NEG_STATE(t->state);
4636 break;
4637
4638 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004639 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004640 ADD_POS_NEG_STATE(t->state);
4641 break;
4642
4643 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004644 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004645 ADD_POS_NEG_STATE(t->state);
4646 break;
4647
4648 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004649 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004650 ADD_POS_NEG_STATE(t->state);
4651 break;
4652
4653 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004654 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004655 ADD_POS_NEG_STATE(t->state);
4656 break;
4657
4658 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004659 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004660 ADD_POS_NEG_STATE(t->state);
4661 break;
4662
4663 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004664 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004665 ADD_POS_NEG_STATE(t->state);
4666 break;
4667
4668 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004669 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004670 ADD_POS_NEG_STATE(t->state);
4671 break;
4672
4673 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004674 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004675 ADD_POS_NEG_STATE(t->state);
4676 break;
4677
4678 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004679 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680 ADD_POS_NEG_STATE(t->state);
4681 break;
4682
Bram Moolenaar5714b802013-05-28 22:03:20 +02004683 case NFA_BACKREF1:
4684 case NFA_BACKREF2:
4685 case NFA_BACKREF3:
4686 case NFA_BACKREF4:
4687 case NFA_BACKREF5:
4688 case NFA_BACKREF6:
4689 case NFA_BACKREF7:
4690 case NFA_BACKREF8:
4691 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004692#ifdef FEAT_SYN_HL
4693 case NFA_ZREF1:
4694 case NFA_ZREF2:
4695 case NFA_ZREF3:
4696 case NFA_ZREF4:
4697 case NFA_ZREF5:
4698 case NFA_ZREF6:
4699 case NFA_ZREF7:
4700 case NFA_ZREF8:
4701 case NFA_ZREF9:
4702#endif
4703 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004704 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004705 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004706 int bytelen;
4707
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004708 if (t->state->c <= NFA_BACKREF9)
4709 {
4710 subidx = t->state->c - NFA_BACKREF1 + 1;
4711 result = match_backref(&t->subs.norm, subidx, &bytelen);
4712 }
4713#ifdef FEAT_SYN_HL
4714 else
4715 {
4716 subidx = t->state->c - NFA_ZREF1 + 1;
4717 result = match_zref(subidx, &bytelen);
4718 }
4719#endif
4720
Bram Moolenaar5714b802013-05-28 22:03:20 +02004721 if (result)
4722 {
4723 if (bytelen == 0)
4724 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004725 /* empty match always works, output of NFA_SKIP to be
4726 * used next */
4727 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004728 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004729 }
4730 else if (bytelen <= clen)
4731 {
4732 /* match current character, jump ahead to out of
4733 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004734 ll = nextlist;
4735 add_state = t->state->out->out;
4736 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004737#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004738 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004739#endif
4740 }
4741 else
4742 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004743 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004744 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004745 ll = nextlist;
4746 add_state = t->state->out;
4747 add_off = bytelen;
4748 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004749#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004750 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004751#endif
4752 }
4753
4754 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004755 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004756 }
4757 case NFA_SKIP:
4758 /* charater of previous matching \1 .. \9 */
4759 if (t->count - clen <= 0)
4760 {
4761 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004762 ll = nextlist;
4763 add_state = t->state->out;
4764 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004765#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004766 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004767#endif
4768 }
4769 else
4770 {
4771 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004772 ll = nextlist;
4773 add_state = t->state;
4774 add_off = 0;
4775 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004776#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004777 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004778#endif
4779 }
4780 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004781
4782 case NFA_SKIP_CHAR:
4783 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004784 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004785 /* TODO: should not happen? */
4786 break;
4787
Bram Moolenaar423532e2013-05-29 21:14:42 +02004788 case NFA_LNUM:
4789 case NFA_LNUM_GT:
4790 case NFA_LNUM_LT:
4791 result = (REG_MULTI &&
4792 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4793 (long_u)(reglnum + reg_firstlnum)));
4794 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004795 addstate_here(thislist, t->state->out, &t->subs,
4796 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004797 break;
4798
4799 case NFA_COL:
4800 case NFA_COL_GT:
4801 case NFA_COL_LT:
4802 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4803 (long_u)(reginput - regline) + 1);
4804 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004805 addstate_here(thislist, t->state->out, &t->subs,
4806 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004807 break;
4808
4809 case NFA_VCOL:
4810 case NFA_VCOL_GT:
4811 case NFA_VCOL_LT:
4812 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4813 (long_u)win_linetabsize(
4814 reg_win == NULL ? curwin : reg_win,
4815 regline, (colnr_T)(reginput - regline)) + 1);
4816 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004817 addstate_here(thislist, t->state->out, &t->subs,
4818 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004819 break;
4820
Bram Moolenaar044aa292013-06-04 21:27:38 +02004821 case NFA_MARK:
4822 case NFA_MARK_GT:
4823 case NFA_MARK_LT:
4824 {
4825 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
4826
4827 /* Compare the mark position to the match position. */
4828 result = (pos != NULL /* mark doesn't exist */
4829 && pos->lnum > 0 /* mark isn't set in reg_buf */
4830 && (pos->lnum == reglnum + reg_firstlnum
4831 ? (pos->col == (colnr_T)(reginput - regline)
4832 ? t->state->c == NFA_MARK
4833 : (pos->col < (colnr_T)(reginput - regline)
4834 ? t->state->c == NFA_MARK_GT
4835 : t->state->c == NFA_MARK_LT))
4836 : (pos->lnum < reglnum + reg_firstlnum
4837 ? t->state->c == NFA_MARK_GT
4838 : t->state->c == NFA_MARK_LT)));
4839 if (result)
4840 addstate_here(thislist, t->state->out, &t->subs,
4841 t->pim, &listidx);
4842 break;
4843 }
4844
Bram Moolenaar423532e2013-05-29 21:14:42 +02004845 case NFA_CURSOR:
4846 result = (reg_win != NULL
4847 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4848 && ((colnr_T)(reginput - regline)
4849 == reg_win->w_cursor.col));
4850 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004851 addstate_here(thislist, t->state->out, &t->subs,
4852 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004853 break;
4854
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02004855#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004856 case NFA_VISUAL:
4857 result = reg_match_visual();
4858 if (result)
4859 addstate_here(thislist, t->state->out, &t->subs,
4860 t->pim, &listidx);
4861 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02004862#endif
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004863
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004864 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004865 {
4866 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004867
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004868 /* TODO: put this in #ifdef later */
4869 if (c < -256)
4870 EMSGN("INTERNAL: Negative state char: %ld", c);
4871 if (is_Magic(c))
4872 c = un_Magic(c);
4873 result = (c == curc);
4874
4875 if (!result && ireg_ic)
4876 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004877#ifdef FEAT_MBYTE
4878 /* If there is a composing character which is not being
4879 * ignored there can be no match. Match with composing
4880 * character uses NFA_COMPOSING above. */
4881 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004882 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004883 result = FALSE;
4884#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004885 ADD_POS_NEG_STATE(t->state);
4886 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004887 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02004888
4889 } /* switch (t->state->c) */
4890
4891 if (add_state != NULL)
4892 {
4893 if (t->pim != NULL)
4894 {
4895 /* postponed invisible match */
4896 /* TODO: also do t->pim->pim recursively? */
4897 if (t->pim->result == NFA_PIM_TODO)
4898 {
4899#ifdef ENABLE_LOG
4900 fprintf(log_fd, "\n");
4901 fprintf(log_fd, "==================================\n");
4902 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
4903 fprintf(log_fd, "\n");
4904#endif
4905 result = recursive_regmatch(t->pim->state,
4906 prog, submatch, m, &listids);
4907 t->pim->result = result ? NFA_PIM_MATCH
4908 : NFA_PIM_NOMATCH;
4909 /* for \@! it is a match when result is FALSE */
4910 if (result != t->pim->state->negated)
4911 {
4912 /* Copy submatch info from the recursive call */
4913 copy_sub_off(&t->pim->subs.norm, &m->norm);
4914#ifdef FEAT_SYN_HL
4915 copy_sub_off(&t->pim->subs.synt, &m->synt);
4916#endif
4917 }
4918 }
4919 else
4920 {
4921 result = (t->pim->result == NFA_PIM_MATCH);
4922#ifdef ENABLE_LOG
4923 fprintf(log_fd, "\n");
4924 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
4925 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4926 fprintf(log_fd, "\n");
4927#endif
4928 }
4929
4930 /* for \@! it is a match when result is FALSE */
4931 if (result != t->pim->state->negated)
4932 {
4933 /* Copy submatch info from the recursive call */
4934 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
4935#ifdef FEAT_SYN_HL
4936 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
4937#endif
4938 }
4939 else
4940 /* look-behind match failed, don't add the state */
4941 continue;
4942 }
4943
4944 addstate(ll, add_state, &t->subs, add_off);
4945 if (add_count > 0)
4946 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004947 }
4948
4949 } /* for (thislist = thislist; thislist->state; thislist++) */
4950
Bram Moolenaare23febd2013-05-26 18:40:14 +02004951 /* Look for the start of a match in the current position by adding the
4952 * start state to the list of states.
4953 * The first found match is the leftmost one, thus the order of states
4954 * matters!
4955 * Do not add the start state in recursive calls of nfa_regmatch(),
4956 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004957 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004958 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004959 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004960 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004961 && reglnum == 0
4962 && clen != 0
4963 && (ireg_maxcol == 0
4964 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004965 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004966 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004967 ? (reglnum < nfa_endp->se_u.pos.lnum
4968 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004969 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004970 < nfa_endp->se_u.pos.col))
4971 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004972 {
4973#ifdef ENABLE_LOG
4974 fprintf(log_fd, "(---) STARTSTATE\n");
4975#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004976 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004977 }
4978
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004979#ifdef ENABLE_LOG
4980 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004981 {
4982 int i;
4983
4984 for (i = 0; i < thislist->n; i++)
4985 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4986 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004987 fprintf(log_fd, "\n");
4988#endif
4989
4990nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004991 /* Advance to the next character, or advance to the next line, or
4992 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004993 if (clen != 0)
4994 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004995 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4996 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004997 reg_nextline();
4998 else
4999 break;
5000 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005001
5002#ifdef ENABLE_LOG
5003 if (log_fd != stderr)
5004 fclose(log_fd);
5005 log_fd = NULL;
5006#endif
5007
5008theend:
5009 /* Free memory */
5010 vim_free(list[0].t);
5011 vim_free(list[1].t);
5012 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005013 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005014 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005015#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005016#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005017 fclose(debug);
5018#endif
5019
Bram Moolenaar963fee22013-05-26 21:47:28 +02005020 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005021}
5022
5023/*
5024 * Try match of "prog" with at regline["col"].
5025 * Returns 0 for failure, number of lines contained in the match otherwise.
5026 */
5027 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005028nfa_regtry(prog, col)
5029 nfa_regprog_T *prog;
5030 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005031{
5032 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005033 regsubs_T subs, m;
5034 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005035#ifdef ENABLE_LOG
5036 FILE *f;
5037#endif
5038
5039 reginput = regline + col;
5040 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005041#ifdef FEAT_SYN_HL
5042 /* Clear the external match subpointers if necessary. */
5043 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005044 {
5045 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005046 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005047 }
5048 else
5049 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005050#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051
5052#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005053 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005054 if (f != NULL)
5055 {
5056 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
5057 fprintf(f, " =======================================================\n");
5058#ifdef DEBUG
5059 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5060#endif
5061 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005062 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005063 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005064 fprintf(f, "\n\n");
5065 fclose(f);
5066 }
5067 else
5068 EMSG(_("Could not open temporary log file for writing "));
5069#endif
5070
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005071 clear_sub(&subs.norm);
5072 clear_sub(&m.norm);
5073#ifdef FEAT_SYN_HL
5074 clear_sub(&subs.synt);
5075 clear_sub(&m.synt);
5076#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005077
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005078 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005079 return 0;
5080
5081 cleanup_subexpr();
5082 if (REG_MULTI)
5083 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005084 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005085 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005086 reg_startpos[i] = subs.norm.list.multi[i].start;
5087 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005088 }
5089
5090 if (reg_startpos[0].lnum < 0)
5091 {
5092 reg_startpos[0].lnum = 0;
5093 reg_startpos[0].col = col;
5094 }
5095 if (reg_endpos[0].lnum < 0)
5096 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005097 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005098 reg_endpos[0].lnum = reglnum;
5099 reg_endpos[0].col = (int)(reginput - regline);
5100 }
5101 else
5102 /* Use line number of "\ze". */
5103 reglnum = reg_endpos[0].lnum;
5104 }
5105 else
5106 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005107 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005108 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005109 reg_startp[i] = subs.norm.list.line[i].start;
5110 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005111 }
5112
5113 if (reg_startp[0] == NULL)
5114 reg_startp[0] = regline + col;
5115 if (reg_endp[0] == NULL)
5116 reg_endp[0] = reginput;
5117 }
5118
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005119#ifdef FEAT_SYN_HL
5120 /* Package any found \z(...\) matches for export. Default is none. */
5121 unref_extmatch(re_extmatch_out);
5122 re_extmatch_out = NULL;
5123
5124 if (prog->reghasz == REX_SET)
5125 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005126 cleanup_zsubexpr();
5127 re_extmatch_out = make_extmatch();
5128 for (i = 0; i < subs.synt.in_use; i++)
5129 {
5130 if (REG_MULTI)
5131 {
5132 struct multipos *mpos = &subs.synt.list.multi[i];
5133
5134 /* Only accept single line matches. */
5135 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5136 re_extmatch_out->matches[i] =
5137 vim_strnsave(reg_getline(mpos->start.lnum)
5138 + mpos->start.col,
5139 mpos->end.col - mpos->start.col);
5140 }
5141 else
5142 {
5143 struct linepos *lpos = &subs.synt.list.line[i];
5144
5145 if (lpos->start != NULL && lpos->end != NULL)
5146 re_extmatch_out->matches[i] =
5147 vim_strnsave(lpos->start,
5148 (int)(lpos->end - lpos->start));
5149 }
5150 }
5151 }
5152#endif
5153
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005154 return 1 + reglnum;
5155}
5156
5157/*
5158 * Match a regexp against a string ("line" points to the string) or multiple
5159 * lines ("line" is NULL, use reg_getline()).
5160 *
5161 * Returns 0 for failure, number of lines contained in the match otherwise.
5162 */
5163 static long
5164nfa_regexec_both(line, col)
5165 char_u *line;
5166 colnr_T col; /* column to start looking for match */
5167{
5168 nfa_regprog_T *prog;
5169 long retval = 0L;
5170 int i;
5171
5172 if (REG_MULTI)
5173 {
5174 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5175 line = reg_getline((linenr_T)0); /* relative to the cursor */
5176 reg_startpos = reg_mmatch->startpos;
5177 reg_endpos = reg_mmatch->endpos;
5178 }
5179 else
5180 {
5181 prog = (nfa_regprog_T *)reg_match->regprog;
5182 reg_startp = reg_match->startp;
5183 reg_endp = reg_match->endp;
5184 }
5185
5186 /* Be paranoid... */
5187 if (prog == NULL || line == NULL)
5188 {
5189 EMSG(_(e_null));
5190 goto theend;
5191 }
5192
5193 /* If the start column is past the maximum column: no need to try. */
5194 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5195 goto theend;
5196
5197 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5198 if (prog->regflags & RF_ICASE)
5199 ireg_ic = TRUE;
5200 else if (prog->regflags & RF_NOICASE)
5201 ireg_ic = FALSE;
5202
5203#ifdef FEAT_MBYTE
5204 /* If pattern contains "\Z" overrule value of ireg_icombine */
5205 if (prog->regflags & RF_ICOMBINE)
5206 ireg_icombine = TRUE;
5207#endif
5208
5209 regline = line;
5210 reglnum = 0; /* relative to line */
5211
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005212 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005213 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005214 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005215 nfa_listid = 1;
5216 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005217#ifdef DEBUG
5218 nfa_regengine.expr = prog->pattern;
5219#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005220
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005221 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005222 for (i = 0; i < nstate; ++i)
5223 {
5224 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005225 prog->state[i].lastlist[0] = 0;
5226 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005227 }
5228
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005229 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005230
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005231#ifdef DEBUG
5232 nfa_regengine.expr = NULL;
5233#endif
5234
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005235theend:
5236 return retval;
5237}
5238
5239/*
5240 * Compile a regular expression into internal code for the NFA matcher.
5241 * Returns the program in allocated space. Returns NULL for an error.
5242 */
5243 static regprog_T *
5244nfa_regcomp(expr, re_flags)
5245 char_u *expr;
5246 int re_flags;
5247{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005248 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005249 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005250 int *postfix;
5251
5252 if (expr == NULL)
5253 return NULL;
5254
5255#ifdef DEBUG
5256 nfa_regengine.expr = expr;
5257#endif
5258
5259 init_class_tab();
5260
5261 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5262 return NULL;
5263
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005264 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005265 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005266 postfix = re2post();
5267 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005268 {
5269 /* TODO: only give this error for debugging? */
5270 if (post_ptr >= post_end)
5271 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005272 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005273 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005274
5275 /*
5276 * In order to build the NFA, we parse the input regexp twice:
5277 * 1. first pass to count size (so we can allocate space)
5278 * 2. second to emit code
5279 */
5280#ifdef ENABLE_LOG
5281 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005282 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005283
5284 if (f != NULL)
5285 {
5286 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5287 fclose(f);
5288 }
5289 }
5290#endif
5291
5292 /*
5293 * PASS 1
5294 * Count number of NFA states in "nstate". Do not build the NFA.
5295 */
5296 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005297
5298 /* Space for compiled regexp */
5299 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5300 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5301 if (prog == NULL)
5302 goto fail;
5303 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005304 state_ptr = prog->state;
5305
5306 /*
5307 * PASS 2
5308 * Build the NFA
5309 */
5310 prog->start = post2nfa(postfix, post_ptr, FALSE);
5311 if (prog->start == NULL)
5312 goto fail;
5313
5314 prog->regflags = regflags;
5315 prog->engine = &nfa_regengine;
5316 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005317 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005318 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005319 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005320#ifdef ENABLE_LOG
5321 nfa_postfix_dump(expr, OK);
5322 nfa_dump(prog);
5323#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005324#ifdef FEAT_SYN_HL
5325 /* Remember whether this pattern has any \z specials in it. */
5326 prog->reghasz = re_has_z;
5327#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005328#ifdef DEBUG
5329 prog->pattern = vim_strsave(expr); /* memory will leak */
5330 nfa_regengine.expr = NULL;
5331#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005332
5333out:
5334 vim_free(post_start);
5335 post_start = post_ptr = post_end = NULL;
5336 state_ptr = NULL;
5337 return (regprog_T *)prog;
5338
5339fail:
5340 vim_free(prog);
5341 prog = NULL;
5342#ifdef ENABLE_LOG
5343 nfa_postfix_dump(expr, FAIL);
5344#endif
5345#ifdef DEBUG
5346 nfa_regengine.expr = NULL;
5347#endif
5348 goto out;
5349}
5350
5351
5352/*
5353 * Match a regexp against a string.
5354 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5355 * Uses curbuf for line count and 'iskeyword'.
5356 *
5357 * Return TRUE if there is a match, FALSE if not.
5358 */
5359 static int
5360nfa_regexec(rmp, line, col)
5361 regmatch_T *rmp;
5362 char_u *line; /* string to match against */
5363 colnr_T col; /* column to start looking for match */
5364{
5365 reg_match = rmp;
5366 reg_mmatch = NULL;
5367 reg_maxline = 0;
5368 reg_line_lbr = FALSE;
5369 reg_buf = curbuf;
5370 reg_win = NULL;
5371 ireg_ic = rmp->rm_ic;
5372#ifdef FEAT_MBYTE
5373 ireg_icombine = FALSE;
5374#endif
5375 ireg_maxcol = 0;
5376 return (nfa_regexec_both(line, col) != 0);
5377}
5378
5379#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5380 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5381
5382static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5383
5384/*
5385 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5386 */
5387 static int
5388nfa_regexec_nl(rmp, line, col)
5389 regmatch_T *rmp;
5390 char_u *line; /* string to match against */
5391 colnr_T col; /* column to start looking for match */
5392{
5393 reg_match = rmp;
5394 reg_mmatch = NULL;
5395 reg_maxline = 0;
5396 reg_line_lbr = TRUE;
5397 reg_buf = curbuf;
5398 reg_win = NULL;
5399 ireg_ic = rmp->rm_ic;
5400#ifdef FEAT_MBYTE
5401 ireg_icombine = FALSE;
5402#endif
5403 ireg_maxcol = 0;
5404 return (nfa_regexec_both(line, col) != 0);
5405}
5406#endif
5407
5408
5409/*
5410 * Match a regexp against multiple lines.
5411 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5412 * Uses curbuf for line count and 'iskeyword'.
5413 *
5414 * Return zero if there is no match. Return number of lines contained in the
5415 * match otherwise.
5416 *
5417 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5418 *
5419 * ! Also NOTE : match may actually be in another line. e.g.:
5420 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5421 *
5422 * +-------------------------+
5423 * |a |
5424 * |b |
5425 * |c |
5426 * | |
5427 * +-------------------------+
5428 *
5429 * then nfa_regexec_multi() returns 3. while the original
5430 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5431 *
5432 * FIXME if this behavior is not compatible.
5433 */
5434 static long
5435nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5436 regmmatch_T *rmp;
5437 win_T *win; /* window in which to search or NULL */
5438 buf_T *buf; /* buffer in which to search */
5439 linenr_T lnum; /* nr of line to start looking for match */
5440 colnr_T col; /* column to start looking for match */
5441 proftime_T *tm UNUSED; /* timeout limit or NULL */
5442{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005443 reg_match = NULL;
5444 reg_mmatch = rmp;
5445 reg_buf = buf;
5446 reg_win = win;
5447 reg_firstlnum = lnum;
5448 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5449 reg_line_lbr = FALSE;
5450 ireg_ic = rmp->rmm_ic;
5451#ifdef FEAT_MBYTE
5452 ireg_icombine = FALSE;
5453#endif
5454 ireg_maxcol = rmp->rmm_maxcol;
5455
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005456 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005457}
5458
5459#ifdef DEBUG
5460# undef ENABLE_LOG
5461#endif