blob: 5873cfcc10282da2e17e41a0deb3a809f9e8933a [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;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689 int glue; /* ID that will "glue" nodes together */
690
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200691 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692 switch (c)
693 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200694 case NUL:
695 syntax_error = TRUE;
696 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
697
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 case Magic('^'):
699 EMIT(NFA_BOL);
700 break;
701
702 case Magic('$'):
703 EMIT(NFA_EOL);
704#if defined(FEAT_SYN_HL) || defined(PROTO)
705 had_eol = TRUE;
706#endif
707 break;
708
709 case Magic('<'):
710 EMIT(NFA_BOW);
711 break;
712
713 case Magic('>'):
714 EMIT(NFA_EOW);
715 break;
716
717 case Magic('_'):
718 c = no_Magic(getchr());
719 if (c == '^') /* "\_^" is start-of-line */
720 {
721 EMIT(NFA_BOL);
722 break;
723 }
724 if (c == '$') /* "\_$" is end-of-line */
725 {
726 EMIT(NFA_EOL);
727#if defined(FEAT_SYN_HL) || defined(PROTO)
728 had_eol = TRUE;
729#endif
730 break;
731 }
732
733 extra = ADD_NL;
734
735 /* "\_[" is collection plus newline */
736 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200737 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200738
739 /* "\_x" is character class plus newline */
740 /*FALLTHROUGH*/
741
742 /*
743 * Character classes.
744 */
745 case Magic('.'):
746 case Magic('i'):
747 case Magic('I'):
748 case Magic('k'):
749 case Magic('K'):
750 case Magic('f'):
751 case Magic('F'):
752 case Magic('p'):
753 case Magic('P'):
754 case Magic('s'):
755 case Magic('S'):
756 case Magic('d'):
757 case Magic('D'):
758 case Magic('x'):
759 case Magic('X'):
760 case Magic('o'):
761 case Magic('O'):
762 case Magic('w'):
763 case Magic('W'):
764 case Magic('h'):
765 case Magic('H'):
766 case Magic('a'):
767 case Magic('A'):
768 case Magic('l'):
769 case Magic('L'):
770 case Magic('u'):
771 case Magic('U'):
772 p = vim_strchr(classchars, no_Magic(c));
773 if (p == NULL)
774 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200775 EMSGN("INTERNAL: Unknown character class char: %ld", c);
776 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200777 }
778#ifdef FEAT_MBYTE
779 /* When '.' is followed by a composing char ignore the dot, so that
780 * the composing char is matched here. */
781 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
782 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200783 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200784 c = getchr();
785 goto nfa_do_multibyte;
786 }
787#endif
788 EMIT(nfa_classcodes[p - classchars]);
789 if (extra == ADD_NL)
790 {
791 EMIT(NFA_NEWL);
792 EMIT(NFA_OR);
793 regflags |= RF_HASNL;
794 }
795 break;
796
797 case Magic('n'):
798 if (reg_string)
799 /* In a string "\n" matches a newline character. */
800 EMIT(NL);
801 else
802 {
803 /* In buffer text "\n" matches the end of a line. */
804 EMIT(NFA_NEWL);
805 regflags |= RF_HASNL;
806 }
807 break;
808
809 case Magic('('):
810 if (nfa_reg(REG_PAREN) == FAIL)
811 return FAIL; /* cascaded error */
812 break;
813
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 case Magic('|'):
815 case Magic('&'):
816 case Magic(')'):
817 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200818 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200819 return FAIL;
820
821 case Magic('='):
822 case Magic('?'):
823 case Magic('+'):
824 case Magic('@'):
825 case Magic('*'):
826 case Magic('{'):
827 /* these should follow an atom, not form an atom */
828 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200829 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200830 return FAIL;
831
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200832 case Magic('~'):
833 {
834 char_u *lp;
835
836 /* Previous substitute pattern.
837 * Generated as "\%(pattern\)". */
838 if (reg_prev_sub == NULL)
839 {
840 EMSG(_(e_nopresub));
841 return FAIL;
842 }
843 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
844 {
845 EMIT(PTR2CHAR(lp));
846 if (lp != reg_prev_sub)
847 EMIT(NFA_CONCAT);
848 }
849 EMIT(NFA_NOPEN);
850 break;
851 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200852
Bram Moolenaar428e9872013-05-30 17:05:39 +0200853 case Magic('1'):
854 case Magic('2'):
855 case Magic('3'):
856 case Magic('4'):
857 case Magic('5'):
858 case Magic('6'):
859 case Magic('7'):
860 case Magic('8'):
861 case Magic('9'):
862 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
863 nfa_has_backref = TRUE;
864 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200865
866 case Magic('z'):
867 c = no_Magic(getchr());
868 switch (c)
869 {
870 case 's':
871 EMIT(NFA_ZSTART);
872 break;
873 case 'e':
874 EMIT(NFA_ZEND);
875 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200876 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200877#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200878 case '1':
879 case '2':
880 case '3':
881 case '4':
882 case '5':
883 case '6':
884 case '7':
885 case '8':
886 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200887 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200888 if (reg_do_extmatch != REX_USE)
889 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200890 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
891 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +0200892 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200893 re_has_z = REX_USE;
894 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200895 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200896 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200897 if (reg_do_extmatch != REX_SET)
898 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200899 if (nfa_reg(REG_ZPAREN) == FAIL)
900 return FAIL; /* cascaded error */
901 re_has_z = REX_SET;
902 break;
903#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200904 default:
905 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200906 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200907 no_Magic(c));
908 return FAIL;
909 }
910 break;
911
912 case Magic('%'):
913 c = no_Magic(getchr());
914 switch (c)
915 {
916 /* () without a back reference */
917 case '(':
918 if (nfa_reg(REG_NPAREN) == FAIL)
919 return FAIL;
920 EMIT(NFA_NOPEN);
921 break;
922
923 case 'd': /* %d123 decimal */
924 case 'o': /* %o123 octal */
925 case 'x': /* %xab hex 2 */
926 case 'u': /* %uabcd hex 4 */
927 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200928 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200929 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200930
Bram Moolenaar47196582013-05-25 22:04:23 +0200931 switch (c)
932 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200933 case 'd': nr = getdecchrs(); break;
934 case 'o': nr = getoctchrs(); break;
935 case 'x': nr = gethexchrs(2); break;
936 case 'u': nr = gethexchrs(4); break;
937 case 'U': nr = gethexchrs(8); break;
938 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200939 }
940
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200941 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200942 EMSG2_RET_FAIL(
943 _("E678: Invalid character after %s%%[dxouU]"),
944 reg_magic == MAGIC_ALL);
945 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200946 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200947 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200948 break;
949
950 /* Catch \%^ and \%$ regardless of where they appear in the
951 * pattern -- regardless of whether or not it makes sense. */
952 case '^':
953 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200954 break;
955
956 case '$':
957 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200958 break;
959
960 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200961 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200962 break;
963
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200964#ifdef FEAT_VISUAL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200966 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200967 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200968#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200969
970 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200971 {
972 int n;
973
974 /* \%[abc] */
975 for (n = 0; (c = getchr()) != ']'; ++n)
976 {
977 if (c == NUL)
978 EMSG2_RET_FAIL(_(e_missing_sb),
979 reg_magic == MAGIC_ALL);
980 EMIT(c);
981 }
982 EMIT(NFA_OPT_CHARS);
983 EMIT(n);
984 break;
985 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200986
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200987 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200988 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200989 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200990 int cmp = c;
991
992 if (c == '<' || c == '>')
993 c = getchr();
994 while (VIM_ISDIGIT(c))
995 {
996 n = n * 10 + (c - '0');
997 c = getchr();
998 }
999 if (c == 'l' || c == 'c' || c == 'v')
1000 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001001 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001002 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001003 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001004 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001005 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001006 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001007 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001008 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001009 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001010 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001011 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001012 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001013 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001014 break;
1015 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001016 else if (c == '\'' && n == 0)
1017 {
1018 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001019 EMIT(cmp == '<' ? NFA_MARK_LT :
1020 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001021 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001022 break;
1023 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001024 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001025 syntax_error = TRUE;
1026 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1027 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001028 return FAIL;
1029 }
1030 break;
1031
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001032 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001033collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001034 /*
1035 * Glue is emitted between several atoms from the [].
1036 * It is either NFA_OR, or NFA_CONCAT.
1037 *
1038 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1039 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1040 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1041 * notation)
1042 *
1043 */
1044
1045
1046/* Emit negation atoms, if needed.
1047 * The CONCAT below merges the NOT with the previous node. */
1048#define TRY_NEG() \
1049 if (negated == TRUE) \
1050 { \
1051 EMIT(NFA_NOT); \
1052 }
1053
1054/* Emit glue between important nodes : CONCAT or OR. */
1055#define EMIT_GLUE() \
1056 if (first == FALSE) \
1057 EMIT(glue); \
1058 else \
1059 first = FALSE;
1060
1061 p = regparse;
1062 endp = skip_anyof(p);
1063 if (*endp == ']')
1064 {
1065 /*
1066 * Try to reverse engineer character classes. For example,
1067 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1068 * and perform the necessary substitutions in the NFA.
1069 */
1070 result = nfa_recognize_char_class(regparse, endp,
1071 extra == ADD_NL);
1072 if (result != FAIL)
1073 {
1074 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1075 EMIT(result);
1076 else /* must be char class + newline */
1077 {
1078 EMIT(result - ADD_NL);
1079 EMIT(NFA_NEWL);
1080 EMIT(NFA_OR);
1081 }
1082 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001083 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001084 return OK;
1085 }
1086 /*
1087 * Failed to recognize a character class. Use the simple
1088 * version that turns [abc] into 'a' OR 'b' OR 'c'
1089 */
1090 startc = endc = oldstartc = -1;
1091 first = TRUE; /* Emitting first atom in this sequence? */
1092 negated = FALSE;
1093 glue = NFA_OR;
1094 if (*regparse == '^') /* negated range */
1095 {
1096 negated = TRUE;
1097 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001098 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001099 }
1100 if (*regparse == '-')
1101 {
1102 startc = '-';
1103 EMIT(startc);
1104 TRY_NEG();
1105 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001106 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001107 }
1108 /* Emit the OR branches for each character in the [] */
1109 emit_range = FALSE;
1110 while (regparse < endp)
1111 {
1112 oldstartc = startc;
1113 startc = -1;
1114 got_coll_char = FALSE;
1115 if (*regparse == '[')
1116 {
1117 /* Check for [: :], [= =], [. .] */
1118 equiclass = collclass = 0;
1119 charclass = get_char_class(&regparse);
1120 if (charclass == CLASS_NONE)
1121 {
1122 equiclass = get_equi_class(&regparse);
1123 if (equiclass == 0)
1124 collclass = get_coll_element(&regparse);
1125 }
1126
1127 /* Character class like [:alpha:] */
1128 if (charclass != CLASS_NONE)
1129 {
1130 switch (charclass)
1131 {
1132 case CLASS_ALNUM:
1133 EMIT(NFA_CLASS_ALNUM);
1134 break;
1135 case CLASS_ALPHA:
1136 EMIT(NFA_CLASS_ALPHA);
1137 break;
1138 case CLASS_BLANK:
1139 EMIT(NFA_CLASS_BLANK);
1140 break;
1141 case CLASS_CNTRL:
1142 EMIT(NFA_CLASS_CNTRL);
1143 break;
1144 case CLASS_DIGIT:
1145 EMIT(NFA_CLASS_DIGIT);
1146 break;
1147 case CLASS_GRAPH:
1148 EMIT(NFA_CLASS_GRAPH);
1149 break;
1150 case CLASS_LOWER:
1151 EMIT(NFA_CLASS_LOWER);
1152 break;
1153 case CLASS_PRINT:
1154 EMIT(NFA_CLASS_PRINT);
1155 break;
1156 case CLASS_PUNCT:
1157 EMIT(NFA_CLASS_PUNCT);
1158 break;
1159 case CLASS_SPACE:
1160 EMIT(NFA_CLASS_SPACE);
1161 break;
1162 case CLASS_UPPER:
1163 EMIT(NFA_CLASS_UPPER);
1164 break;
1165 case CLASS_XDIGIT:
1166 EMIT(NFA_CLASS_XDIGIT);
1167 break;
1168 case CLASS_TAB:
1169 EMIT(NFA_CLASS_TAB);
1170 break;
1171 case CLASS_RETURN:
1172 EMIT(NFA_CLASS_RETURN);
1173 break;
1174 case CLASS_BACKSPACE:
1175 EMIT(NFA_CLASS_BACKSPACE);
1176 break;
1177 case CLASS_ESCAPE:
1178 EMIT(NFA_CLASS_ESCAPE);
1179 break;
1180 }
1181 TRY_NEG();
1182 EMIT_GLUE();
1183 continue;
1184 }
1185 /* Try equivalence class [=a=] and the like */
1186 if (equiclass != 0)
1187 {
1188 result = nfa_emit_equi_class(equiclass, negated);
1189 if (result == FAIL)
1190 {
1191 /* should never happen */
1192 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1193 }
1194 EMIT_GLUE();
1195 continue;
1196 }
1197 /* Try collating class like [. .] */
1198 if (collclass != 0)
1199 {
1200 startc = collclass; /* allow [.a.]-x as a range */
1201 /* Will emit the proper atom at the end of the
1202 * while loop. */
1203 }
1204 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001205 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1206 * start character. */
1207 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 {
1209 emit_range = TRUE;
1210 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001211 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 continue; /* reading the end of the range */
1213 }
1214
1215 /* Now handle simple and escaped characters.
1216 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1217 * accepts "\t", "\e", etc., but only when the 'l' flag in
1218 * 'cpoptions' is not included.
1219 * Posix doesn't recognize backslash at all.
1220 */
1221 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001222 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 && regparse + 1 <= endp
1224 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001225 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226 && vim_strchr(REGEXP_ABBR, regparse[1])
1227 != NULL)
1228 )
1229 )
1230 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001231 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001232
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001233 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 startc = reg_string ? NL : NFA_NEWL;
1235 else
1236 if (*regparse == 'd'
1237 || *regparse == 'o'
1238 || *regparse == 'x'
1239 || *regparse == 'u'
1240 || *regparse == 'U'
1241 )
1242 {
1243 /* TODO(RE) This needs more testing */
1244 startc = coll_get_char();
1245 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001246 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001247 }
1248 else
1249 {
1250 /* \r,\t,\e,\b */
1251 startc = backslash_trans(*regparse);
1252 }
1253 }
1254
1255 /* Normal printable char */
1256 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001257 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001258
1259 /* Previous char was '-', so this char is end of range. */
1260 if (emit_range)
1261 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001262 endc = startc;
1263 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 if (startc > endc)
1265 EMSG_RET_FAIL(_(e_invrange));
1266#ifdef FEAT_MBYTE
1267 if (has_mbyte && ((*mb_char2len)(startc) > 1
1268 || (*mb_char2len)(endc) > 1))
1269 {
1270 if (endc > startc + 256)
1271 EMSG_RET_FAIL(_(e_invrange));
1272 /* Emit the range. "startc" was already emitted, so
1273 * skip it. */
1274 for (c = startc + 1; c <= endc; c++)
1275 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001276 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001277 TRY_NEG();
1278 EMIT_GLUE();
1279 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001280 }
1281 else
1282#endif
1283 {
1284#ifdef EBCDIC
1285 int alpha_only = FALSE;
1286
1287 /* for alphabetical range skip the gaps
1288 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1289 if (isalpha(startc) && isalpha(endc))
1290 alpha_only = TRUE;
1291#endif
1292 /* Emit the range. "startc" was already emitted, so
1293 * skip it. */
1294 for (c = startc + 1; c <= endc; c++)
1295#ifdef EBCDIC
1296 if (!alpha_only || isalpha(startc))
1297#endif
1298 {
1299 EMIT(c);
1300 TRY_NEG();
1301 EMIT_GLUE();
1302 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001303 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001304 emit_range = FALSE;
1305 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001306 }
1307 else
1308 {
1309 /*
1310 * This char (startc) is not part of a range. Just
1311 * emit it.
1312 *
1313 * Normally, simply emit startc. But if we get char
1314 * code=0 from a collating char, then replace it with
1315 * 0x0a.
1316 *
1317 * This is needed to completely mimic the behaviour of
1318 * the backtracking engine.
1319 */
1320 if (got_coll_char == TRUE && startc == 0)
1321 EMIT(0x0a);
1322 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001323 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001324 TRY_NEG();
1325 EMIT_GLUE();
1326 }
1327
Bram Moolenaar51a29832013-05-28 22:30:35 +02001328 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001329 } /* while (p < endp) */
1330
Bram Moolenaar51a29832013-05-28 22:30:35 +02001331 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001332 if (*regparse == '-') /* if last, '-' is just a char */
1333 {
1334 EMIT('-');
1335 TRY_NEG();
1336 EMIT_GLUE();
1337 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001338 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001339
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001340 /* skip the trailing ] */
1341 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001342 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 if (negated == TRUE)
1344 {
1345 /* Mark end of negated char range */
1346 EMIT(NFA_END_NEG_RANGE);
1347 EMIT(NFA_CONCAT);
1348 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001349
1350 /* \_[] also matches \n but it's not negated */
1351 if (extra == ADD_NL)
1352 {
1353 EMIT(reg_string ? NL : NFA_NEWL);
1354 EMIT(NFA_OR);
1355 }
1356
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001357 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001358 } /* if exists closing ] */
1359
1360 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001361 {
1362 syntax_error = TRUE;
1363 EMSG_RET_FAIL(_(e_missingbracket));
1364 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001365 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001367 default:
1368 {
1369#ifdef FEAT_MBYTE
1370 int plen;
1371
1372nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001373 /* plen is length of current char with composing chars */
1374 if (enc_utf8 && ((*mb_char2len)(c)
1375 != (plen = (*mb_ptr2len)(old_regparse))
1376 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001378 int i = 0;
1379
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001380 /* A base character plus composing characters, or just one
1381 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001382 * This requires creating a separate atom as if enclosing
1383 * the characters in (), where NFA_COMPOSING is the ( and
1384 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385 * building the postfix form, not the NFA itself;
1386 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001387 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001388 for (;;)
1389 {
1390 EMIT(c);
1391 if (i > 0)
1392 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001393 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001394 break;
1395 c = utf_ptr2char(old_regparse + i);
1396 }
1397 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001398 regparse = old_regparse + plen;
1399 }
1400 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001401#endif
1402 {
1403 c = no_Magic(c);
1404 EMIT(c);
1405 }
1406 return OK;
1407 }
1408 }
1409
1410#undef TRY_NEG
1411#undef EMIT_GLUE
1412
1413 return OK;
1414}
1415
1416/*
1417 * Parse something followed by possible [*+=].
1418 *
1419 * A piece is an atom, possibly followed by a multi, an indication of how many
1420 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1421 * characters: "", "a", "aa", etc.
1422 *
1423 * piece ::= atom
1424 * or atom multi
1425 */
1426 static int
1427nfa_regpiece()
1428{
1429 int i;
1430 int op;
1431 int ret;
1432 long minval, maxval;
1433 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001434 parse_state_T old_state;
1435 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001436 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001437 int old_post_pos;
1438 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439 int quest;
1440
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001441 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1442 * next. */
1443 save_parse_state(&old_state);
1444
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001446 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447
1448 ret = nfa_regatom();
1449 if (ret == FAIL)
1450 return FAIL; /* cascaded error */
1451
1452 op = peekchr();
1453 if (re_multi_type(op) == NOT_MULTI)
1454 return OK;
1455
1456 skipchr();
1457 switch (op)
1458 {
1459 case Magic('*'):
1460 EMIT(NFA_STAR);
1461 break;
1462
1463 case Magic('+'):
1464 /*
1465 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1466 * first and only submatch would be "aaa". But the backtracking
1467 * engine interprets the plus as "try matching one more time", and
1468 * a* matches a second time at the end of the input, the empty
1469 * string.
1470 * The submatch will the empty string.
1471 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001472 * In order to be consistent with the old engine, we replace
1473 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001475 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001476 curchr = -1;
1477 if (nfa_regatom() == FAIL)
1478 return FAIL;
1479 EMIT(NFA_STAR);
1480 EMIT(NFA_CONCAT);
1481 skipchr(); /* skip the \+ */
1482 break;
1483
1484 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001485 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001487 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001488 switch(op)
1489 {
1490 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001491 /* \@= */
1492 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001494 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001495 /* \@! */
1496 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001497 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001499 op = no_Magic(getchr());
1500 if (op == '=')
1501 /* \@<= */
1502 i = NFA_PREV_ATOM_JUST_BEFORE;
1503 else if (op == '!')
1504 /* \@<! */
1505 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1506 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001507 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001508 /* \@> Not supported yet */
1509 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510 return FAIL;
1511 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001512 if (i == 0)
1513 {
1514 syntax_error = TRUE;
1515 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1516 return FAIL;
1517 }
1518 EMIT(i);
1519 if (i == NFA_PREV_ATOM_JUST_BEFORE
1520 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1521 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001522 break;
1523
1524 case Magic('?'):
1525 case Magic('='):
1526 EMIT(NFA_QUEST);
1527 break;
1528
1529 case Magic('{'):
1530 /* a{2,5} will expand to 'aaa?a?a?'
1531 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1532 * version of '?'
1533 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1534 * parenthesis have the same id
1535 */
1536
1537 greedy = TRUE;
1538 c2 = peekchr();
1539 if (c2 == '-' || c2 == Magic('-'))
1540 {
1541 skipchr();
1542 greedy = FALSE;
1543 }
1544 if (!read_limits(&minval, &maxval))
1545 {
1546 syntax_error = TRUE;
1547 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1548 }
1549 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1550 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001551 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001552 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001553 if (greedy)
1554 /* \{}, \{0,} */
1555 EMIT(NFA_STAR);
1556 else
1557 /* \{-}, \{-0,} */
1558 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 break;
1560 }
1561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001562 /* Special case: x{0} or x{-0} */
1563 if (maxval == 0)
1564 {
1565 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001566 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001567 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1568 EMIT(NFA_SKIP_CHAR);
1569 return OK;
1570 }
1571
1572 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001573 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001574 /* Save parse state after the repeated atom and the \{} */
1575 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001576
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001577 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1578 for (i = 0; i < maxval; i++)
1579 {
1580 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001581 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001582 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001583 if (nfa_regatom() == FAIL)
1584 return FAIL;
1585 /* after "minval" times, atoms are optional */
1586 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001587 {
1588 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001589 {
1590 if (greedy)
1591 EMIT(NFA_STAR);
1592 else
1593 EMIT(NFA_STAR_NONGREEDY);
1594 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001595 else
1596 EMIT(quest);
1597 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001598 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001599 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001600 if (i + 1 > minval && maxval == MAX_LIMIT)
1601 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602 }
1603
1604 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001605 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001606 curchr = -1;
1607
1608 break;
1609
1610
1611 default:
1612 break;
1613 } /* end switch */
1614
1615 if (re_multi_type(peekchr()) != NOT_MULTI)
1616 {
1617 /* Can't have a multi follow a multi. */
1618 syntax_error = TRUE;
1619 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1620 }
1621
1622 return OK;
1623}
1624
1625/*
1626 * Parse one or more pieces, concatenated. It matches a match for the
1627 * first piece, followed by a match for the second piece, etc. Example:
1628 * "f[0-9]b", first matches "f", then a digit and then "b".
1629 *
1630 * concat ::= piece
1631 * or piece piece
1632 * or piece piece piece
1633 * etc.
1634 */
1635 static int
1636nfa_regconcat()
1637{
1638 int cont = TRUE;
1639 int first = TRUE;
1640
1641 while (cont)
1642 {
1643 switch (peekchr())
1644 {
1645 case NUL:
1646 case Magic('|'):
1647 case Magic('&'):
1648 case Magic(')'):
1649 cont = FALSE;
1650 break;
1651
1652 case Magic('Z'):
1653#ifdef FEAT_MBYTE
1654 regflags |= RF_ICOMBINE;
1655#endif
1656 skipchr_keepstart();
1657 break;
1658 case Magic('c'):
1659 regflags |= RF_ICASE;
1660 skipchr_keepstart();
1661 break;
1662 case Magic('C'):
1663 regflags |= RF_NOICASE;
1664 skipchr_keepstart();
1665 break;
1666 case Magic('v'):
1667 reg_magic = MAGIC_ALL;
1668 skipchr_keepstart();
1669 curchr = -1;
1670 break;
1671 case Magic('m'):
1672 reg_magic = MAGIC_ON;
1673 skipchr_keepstart();
1674 curchr = -1;
1675 break;
1676 case Magic('M'):
1677 reg_magic = MAGIC_OFF;
1678 skipchr_keepstart();
1679 curchr = -1;
1680 break;
1681 case Magic('V'):
1682 reg_magic = MAGIC_NONE;
1683 skipchr_keepstart();
1684 curchr = -1;
1685 break;
1686
1687 default:
1688 if (nfa_regpiece() == FAIL)
1689 return FAIL;
1690 if (first == FALSE)
1691 EMIT(NFA_CONCAT);
1692 else
1693 first = FALSE;
1694 break;
1695 }
1696 }
1697
1698 return OK;
1699}
1700
1701/*
1702 * Parse a branch, one or more concats, separated by "\&". It matches the
1703 * last concat, but only if all the preceding concats also match at the same
1704 * position. Examples:
1705 * "foobeep\&..." matches "foo" in "foobeep".
1706 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1707 *
1708 * branch ::= concat
1709 * or concat \& concat
1710 * or concat \& concat \& concat
1711 * etc.
1712 */
1713 static int
1714nfa_regbranch()
1715{
1716 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001717 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001718
Bram Moolenaar16299b52013-05-30 18:45:23 +02001719 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720
1721 /* First branch, possibly the only one */
1722 if (nfa_regconcat() == FAIL)
1723 return FAIL;
1724
1725 ch = peekchr();
1726 /* Try next concats */
1727 while (ch == Magic('&'))
1728 {
1729 skipchr();
1730 EMIT(NFA_NOPEN);
1731 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001732 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001733 if (nfa_regconcat() == FAIL)
1734 return FAIL;
1735 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001736 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001737 EMIT(NFA_SKIP_CHAR);
1738 EMIT(NFA_CONCAT);
1739 ch = peekchr();
1740 }
1741
1742 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001743 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744 EMIT(NFA_SKIP_CHAR);
1745
1746 return OK;
1747}
1748
1749/*
1750 * Parse a pattern, one or more branches, separated by "\|". It matches
1751 * anything that matches one of the branches. Example: "foo\|beep" matches
1752 * "foo" and matches "beep". If more than one branch matches, the first one
1753 * is used.
1754 *
1755 * pattern ::= branch
1756 * or branch \| branch
1757 * or branch \| branch \| branch
1758 * etc.
1759 */
1760 static int
1761nfa_reg(paren)
1762 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1763{
1764 int parno = 0;
1765
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 if (paren == REG_PAREN)
1767 {
1768 if (regnpar >= NSUBEXP) /* Too many `(' */
1769 {
1770 syntax_error = TRUE;
1771 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1772 }
1773 parno = regnpar++;
1774 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001775#ifdef FEAT_SYN_HL
1776 else if (paren == REG_ZPAREN)
1777 {
1778 /* Make a ZOPEN node. */
1779 if (regnzpar >= NSUBEXP)
1780 {
1781 syntax_error = TRUE;
1782 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1783 }
1784 parno = regnzpar++;
1785 }
1786#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001787
1788 if (nfa_regbranch() == FAIL)
1789 return FAIL; /* cascaded error */
1790
1791 while (peekchr() == Magic('|'))
1792 {
1793 skipchr();
1794 if (nfa_regbranch() == FAIL)
1795 return FAIL; /* cascaded error */
1796 EMIT(NFA_OR);
1797 }
1798
1799 /* Check for proper termination. */
1800 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1801 {
1802 syntax_error = TRUE;
1803 if (paren == REG_NPAREN)
1804 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1805 else
1806 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1807 }
1808 else if (paren == REG_NOPAREN && peekchr() != NUL)
1809 {
1810 syntax_error = TRUE;
1811 if (peekchr() == Magic(')'))
1812 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1813 else
1814 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1815 }
1816 /*
1817 * Here we set the flag allowing back references to this set of
1818 * parentheses.
1819 */
1820 if (paren == REG_PAREN)
1821 {
1822 had_endbrace[parno] = TRUE; /* have seen the close paren */
1823 EMIT(NFA_MOPEN + parno);
1824 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001825#ifdef FEAT_SYN_HL
1826 else if (paren == REG_ZPAREN)
1827 EMIT(NFA_ZOPEN + parno);
1828#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829
1830 return OK;
1831}
1832
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001833#ifdef DEBUG
1834static char_u code[50];
1835
1836 static void
1837nfa_set_code(c)
1838 int c;
1839{
1840 int addnl = FALSE;
1841
1842 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1843 {
1844 addnl = TRUE;
1845 c -= ADD_NL;
1846 }
1847
1848 STRCPY(code, "");
1849 switch (c)
1850 {
1851 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1852 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1853 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1854 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1855 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1856 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1857
Bram Moolenaar5714b802013-05-28 22:03:20 +02001858 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1859 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1860 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1861 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1862 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1863 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1864 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1865 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1866 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001867#ifdef FEAT_SYN_HL
1868 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1869 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1870 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1871 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1872 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1873 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1874 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1875 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1876 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1877#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001878 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1879
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 case NFA_PREV_ATOM_NO_WIDTH:
1881 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001882 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1883 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001884 case NFA_PREV_ATOM_JUST_BEFORE:
1885 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1886 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1887 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001888 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1889 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001891 case NFA_START_INVISIBLE_BEFORE:
1892 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001893 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1894
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1896 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001897 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001899 case NFA_MOPEN:
1900 case NFA_MOPEN1:
1901 case NFA_MOPEN2:
1902 case NFA_MOPEN3:
1903 case NFA_MOPEN4:
1904 case NFA_MOPEN5:
1905 case NFA_MOPEN6:
1906 case NFA_MOPEN7:
1907 case NFA_MOPEN8:
1908 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 STRCPY(code, "NFA_MOPEN(x)");
1910 code[10] = c - NFA_MOPEN + '0';
1911 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001912 case NFA_MCLOSE:
1913 case NFA_MCLOSE1:
1914 case NFA_MCLOSE2:
1915 case NFA_MCLOSE3:
1916 case NFA_MCLOSE4:
1917 case NFA_MCLOSE5:
1918 case NFA_MCLOSE6:
1919 case NFA_MCLOSE7:
1920 case NFA_MCLOSE8:
1921 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001922 STRCPY(code, "NFA_MCLOSE(x)");
1923 code[11] = c - NFA_MCLOSE + '0';
1924 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001925#ifdef FEAT_SYN_HL
1926 case NFA_ZOPEN:
1927 case NFA_ZOPEN1:
1928 case NFA_ZOPEN2:
1929 case NFA_ZOPEN3:
1930 case NFA_ZOPEN4:
1931 case NFA_ZOPEN5:
1932 case NFA_ZOPEN6:
1933 case NFA_ZOPEN7:
1934 case NFA_ZOPEN8:
1935 case NFA_ZOPEN9:
1936 STRCPY(code, "NFA_ZOPEN(x)");
1937 code[10] = c - NFA_ZOPEN + '0';
1938 break;
1939 case NFA_ZCLOSE:
1940 case NFA_ZCLOSE1:
1941 case NFA_ZCLOSE2:
1942 case NFA_ZCLOSE3:
1943 case NFA_ZCLOSE4:
1944 case NFA_ZCLOSE5:
1945 case NFA_ZCLOSE6:
1946 case NFA_ZCLOSE7:
1947 case NFA_ZCLOSE8:
1948 case NFA_ZCLOSE9:
1949 STRCPY(code, "NFA_ZCLOSE(x)");
1950 code[11] = c - NFA_ZCLOSE + '0';
1951 break;
1952#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1954 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1955 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1956 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001957 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1958 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001959 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1960 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1961 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1962 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1963 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1964 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1965 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1966 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1967 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1968 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1969 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1970 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1971 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001972#ifdef FEAT_VISUAL
Bram Moolenaar044aa292013-06-04 21:27:38 +02001973 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001974#endif
Bram Moolenaar044aa292013-06-04 21:27:38 +02001975
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001976 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001977 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1978 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1979 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001980 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1981 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1982 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1984 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1985 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1986 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1987 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1988 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1989 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1990 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1991 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1992 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1993 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1994 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1995 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1996 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1997 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1998 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1999 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2000
2001 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2002 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2003 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2004 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2005 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2006 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2007 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2008 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2009 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2010 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2011 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2012 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2013 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2014 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2015 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2016 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2017 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2018 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2019 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2020 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2021 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2022 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2023 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2024 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2025 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2026 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2027 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2028
2029 default:
2030 STRCPY(code, "CHAR(x)");
2031 code[5] = c;
2032 }
2033
2034 if (addnl == TRUE)
2035 STRCAT(code, " + NEWLINE ");
2036
2037}
2038
2039#ifdef ENABLE_LOG
2040static FILE *log_fd;
2041
2042/*
2043 * Print the postfix notation of the current regexp.
2044 */
2045 static void
2046nfa_postfix_dump(expr, retval)
2047 char_u *expr;
2048 int retval;
2049{
2050 int *p;
2051 FILE *f;
2052
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002053 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054 if (f != NULL)
2055 {
2056 fprintf(f, "\n-------------------------\n");
2057 if (retval == FAIL)
2058 fprintf(f, ">>> NFA engine failed ... \n");
2059 else if (retval == OK)
2060 fprintf(f, ">>> NFA engine succeeded !\n");
2061 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002062 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063 {
2064 nfa_set_code(*p);
2065 fprintf(f, "%s, ", code);
2066 }
2067 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002068 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 fprintf(f, "%d ", *p);
2070 fprintf(f, "\n\n");
2071 fclose(f);
2072 }
2073}
2074
2075/*
2076 * Print the NFA starting with a root node "state".
2077 */
2078 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002079nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002080 FILE *debugf;
2081 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002083 garray_T indent;
2084
2085 ga_init2(&indent, 1, 64);
2086 ga_append(&indent, '\0');
2087 nfa_print_state2(debugf, state, &indent);
2088 ga_clear(&indent);
2089}
2090
2091 static void
2092nfa_print_state2(debugf, state, indent)
2093 FILE *debugf;
2094 nfa_state_T *state;
2095 garray_T *indent;
2096{
2097 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002098
2099 if (state == NULL)
2100 return;
2101
2102 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002103
2104 /* Output indent */
2105 p = (char_u *)indent->ga_data;
2106 if (indent->ga_len >= 3)
2107 {
2108 int last = indent->ga_len - 3;
2109 char_u save[2];
2110
2111 STRNCPY(save, &p[last], 2);
2112 STRNCPY(&p[last], "+-", 2);
2113 fprintf(debugf, " %s", p);
2114 STRNCPY(&p[last], save, 2);
2115 }
2116 else
2117 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002118
2119 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002120 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2121 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122 if (state->id < 0)
2123 return;
2124
2125 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002126
2127 /* grow indent for state->out */
2128 indent->ga_len -= 1;
2129 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002130 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002131 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002132 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002133 ga_append(indent, '\0');
2134
2135 nfa_print_state2(debugf, state->out, indent);
2136
2137 /* replace last part of indent for state->out1 */
2138 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002139 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002140 ga_append(indent, '\0');
2141
2142 nfa_print_state2(debugf, state->out1, indent);
2143
2144 /* shrink indent */
2145 indent->ga_len -= 3;
2146 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147}
2148
2149/*
2150 * Print the NFA state machine.
2151 */
2152 static void
2153nfa_dump(prog)
2154 nfa_regprog_T *prog;
2155{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002156 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002157
2158 if (debugf != NULL)
2159 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002160 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002161 fclose(debugf);
2162 }
2163}
2164#endif /* ENABLE_LOG */
2165#endif /* DEBUG */
2166
2167/*
2168 * Parse r.e. @expr and convert it into postfix form.
2169 * Return the postfix string on success, NULL otherwise.
2170 */
2171 static int *
2172re2post()
2173{
2174 if (nfa_reg(REG_NOPAREN) == FAIL)
2175 return NULL;
2176 EMIT(NFA_MOPEN);
2177 return post_start;
2178}
2179
2180/* NB. Some of the code below is inspired by Russ's. */
2181
2182/*
2183 * Represents an NFA state plus zero or one or two arrows exiting.
2184 * if c == MATCH, no arrows out; matching state.
2185 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2186 * If c < 256, labeled arrow with character c to out.
2187 */
2188
2189static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2190
2191/*
2192 * Allocate and initialize nfa_state_T.
2193 */
2194 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002195alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002196 int c;
2197 nfa_state_T *out;
2198 nfa_state_T *out1;
2199{
2200 nfa_state_T *s;
2201
2202 if (istate >= nstate)
2203 return NULL;
2204
2205 s = &state_ptr[istate++];
2206
2207 s->c = c;
2208 s->out = out;
2209 s->out1 = out1;
2210
2211 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002212 s->lastlist[0] = 0;
2213 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002214 s->negated = FALSE;
2215
2216 return s;
2217}
2218
2219/*
2220 * A partially built NFA without the matching state filled in.
2221 * Frag_T.start points at the start state.
2222 * Frag_T.out is a list of places that need to be set to the
2223 * next state for this fragment.
2224 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002225
2226/* Since the out pointers in the list are always
2227 * uninitialized, we use the pointers themselves
2228 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002230union Ptrlist
2231{
2232 Ptrlist *next;
2233 nfa_state_T *s;
2234};
2235
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236struct Frag
2237{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002238 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002239 Ptrlist *out;
2240};
2241typedef struct Frag Frag_T;
2242
2243static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2244static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2245static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2246static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2247static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2248static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2249
2250/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002251 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002252 */
2253 static Frag_T
2254frag(start, out)
2255 nfa_state_T *start;
2256 Ptrlist *out;
2257{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002258 Frag_T n;
2259
2260 n.start = start;
2261 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002262 return n;
2263}
2264
2265/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 * Create singleton list containing just outp.
2267 */
2268 static Ptrlist *
2269list1(outp)
2270 nfa_state_T **outp;
2271{
2272 Ptrlist *l;
2273
2274 l = (Ptrlist *)outp;
2275 l->next = NULL;
2276 return l;
2277}
2278
2279/*
2280 * Patch the list of states at out to point to start.
2281 */
2282 static void
2283patch(l, s)
2284 Ptrlist *l;
2285 nfa_state_T *s;
2286{
2287 Ptrlist *next;
2288
2289 for (; l; l = next)
2290 {
2291 next = l->next;
2292 l->s = s;
2293 }
2294}
2295
2296
2297/*
2298 * Join the two lists l1 and l2, returning the combination.
2299 */
2300 static Ptrlist *
2301append(l1, l2)
2302 Ptrlist *l1;
2303 Ptrlist *l2;
2304{
2305 Ptrlist *oldl1;
2306
2307 oldl1 = l1;
2308 while (l1->next)
2309 l1 = l1->next;
2310 l1->next = l2;
2311 return oldl1;
2312}
2313
2314/*
2315 * Stack used for transforming postfix form into NFA.
2316 */
2317static Frag_T empty;
2318
2319 static void
2320st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002321 int *postfix UNUSED;
2322 int *end UNUSED;
2323 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002325#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002326 FILE *df;
2327 int *p2;
2328
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002329 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330 if (df)
2331 {
2332 fprintf(df, "Error popping the stack!\n");
2333#ifdef DEBUG
2334 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2335#endif
2336 fprintf(df, "Postfix form is: ");
2337#ifdef DEBUG
2338 for (p2 = postfix; p2 < end; p2++)
2339 {
2340 nfa_set_code(*p2);
2341 fprintf(df, "%s, ", code);
2342 }
2343 nfa_set_code(*p);
2344 fprintf(df, "\nCurrent position is: ");
2345 for (p2 = postfix; p2 <= p; p2 ++)
2346 {
2347 nfa_set_code(*p2);
2348 fprintf(df, "%s, ", code);
2349 }
2350#else
2351 for (p2 = postfix; p2 < end; p2++)
2352 {
2353 fprintf(df, "%d, ", *p2);
2354 }
2355 fprintf(df, "\nCurrent position is: ");
2356 for (p2 = postfix; p2 <= p; p2 ++)
2357 {
2358 fprintf(df, "%d, ", *p2);
2359 }
2360#endif
2361 fprintf(df, "\n--------------------------\n");
2362 fclose(df);
2363 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002364#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365 EMSG(_("E874: (NFA) Could not pop the stack !"));
2366}
2367
2368/*
2369 * Push an item onto the stack.
2370 */
2371 static void
2372st_push(s, p, stack_end)
2373 Frag_T s;
2374 Frag_T **p;
2375 Frag_T *stack_end;
2376{
2377 Frag_T *stackp = *p;
2378
2379 if (stackp >= stack_end)
2380 return;
2381 *stackp = s;
2382 *p = *p + 1;
2383}
2384
2385/*
2386 * Pop an item from the stack.
2387 */
2388 static Frag_T
2389st_pop(p, stack)
2390 Frag_T **p;
2391 Frag_T *stack;
2392{
2393 Frag_T *stackp;
2394
2395 *p = *p - 1;
2396 stackp = *p;
2397 if (stackp < stack)
2398 return empty;
2399 return **p;
2400}
2401
2402/*
2403 * Convert a postfix form into its equivalent NFA.
2404 * Return the NFA start state on success, NULL otherwise.
2405 */
2406 static nfa_state_T *
2407post2nfa(postfix, end, nfa_calc_size)
2408 int *postfix;
2409 int *end;
2410 int nfa_calc_size;
2411{
2412 int *p;
2413 int mopen;
2414 int mclose;
2415 Frag_T *stack = NULL;
2416 Frag_T *stackp = NULL;
2417 Frag_T *stack_end = NULL;
2418 Frag_T e1;
2419 Frag_T e2;
2420 Frag_T e;
2421 nfa_state_T *s;
2422 nfa_state_T *s1;
2423 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002424 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425
2426 if (postfix == NULL)
2427 return NULL;
2428
Bram Moolenaar053bb602013-05-20 13:55:21 +02002429#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430#define POP() st_pop(&stackp, stack); \
2431 if (stackp < stack) \
2432 { \
2433 st_error(postfix, end, p); \
2434 return NULL; \
2435 }
2436
2437 if (nfa_calc_size == FALSE)
2438 {
2439 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002440 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002441 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002442 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 }
2444
2445 for (p = postfix; p < end; ++p)
2446 {
2447 switch (*p)
2448 {
2449 case NFA_CONCAT:
2450 /* Catenation.
2451 * Pay attention: this operator does not exist
2452 * in the r.e. itself (it is implicit, really).
2453 * It is added when r.e. is translated to postfix
2454 * form in re2post().
2455 *
2456 * No new state added here. */
2457 if (nfa_calc_size == TRUE)
2458 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002459 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002460 break;
2461 }
2462 e2 = POP();
2463 e1 = POP();
2464 patch(e1.out, e2.start);
2465 PUSH(frag(e1.start, e2.out));
2466 break;
2467
2468 case NFA_NOT:
2469 /* Negation of a character */
2470 if (nfa_calc_size == TRUE)
2471 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002472 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 break;
2474 }
2475 e1 = POP();
2476 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002477#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002478 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002480#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002481 PUSH(e1);
2482 break;
2483
2484 case NFA_OR:
2485 /* Alternation */
2486 if (nfa_calc_size == TRUE)
2487 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002488 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002489 break;
2490 }
2491 e2 = POP();
2492 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002493 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002495 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002496 PUSH(frag(s, append(e1.out, e2.out)));
2497 break;
2498
2499 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002500 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002501 if (nfa_calc_size == TRUE)
2502 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002503 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504 break;
2505 }
2506 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002507 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002508 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002509 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002510 patch(e.out, s);
2511 PUSH(frag(s, list1(&s->out1)));
2512 break;
2513
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002514 case NFA_STAR_NONGREEDY:
2515 /* Zero or more, prefer zero */
2516 if (nfa_calc_size == TRUE)
2517 {
2518 nstate++;
2519 break;
2520 }
2521 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002522 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002523 if (s == NULL)
2524 goto theend;
2525 patch(e.out, s);
2526 PUSH(frag(s, list1(&s->out)));
2527 break;
2528
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002529 case NFA_QUEST:
2530 /* one or zero atoms=> greedy match */
2531 if (nfa_calc_size == TRUE)
2532 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002533 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002534 break;
2535 }
2536 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002537 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002539 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002540 PUSH(frag(s, append(e.out, list1(&s->out1))));
2541 break;
2542
2543 case NFA_QUEST_NONGREEDY:
2544 /* zero or one atoms => non-greedy match */
2545 if (nfa_calc_size == TRUE)
2546 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002547 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 break;
2549 }
2550 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002551 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002552 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002553 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002554 PUSH(frag(s, append(e.out, list1(&s->out))));
2555 break;
2556
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002557 case NFA_SKIP_CHAR:
2558 /* Symbol of 0-length, Used in a repetition
2559 * with max/min count of 0 */
2560 if (nfa_calc_size == TRUE)
2561 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002562 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002563 break;
2564 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002565 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002566 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002567 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002568 PUSH(frag(s, list1(&s->out)));
2569 break;
2570
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002571 case NFA_OPT_CHARS:
2572 {
2573 int n;
2574
2575 /* \%[abc] */
2576 n = *++p; /* get number of characters */
2577 if (nfa_calc_size == TRUE)
2578 {
2579 nstate += n;
2580 break;
2581 }
2582 e1.out = NULL; /* stores list with out1's */
2583 s1 = NULL; /* previous NFA_SPLIT to connect to */
2584 while (n-- > 0)
2585 {
2586 e = POP(); /* get character */
2587 s = alloc_state(NFA_SPLIT, e.start, NULL);
2588 if (s == NULL)
2589 goto theend;
2590 if (e1.out == NULL)
2591 e1 = e;
2592 patch(e.out, s1);
2593 append(e1.out, list1(&s->out1));
2594 s1 = s;
2595 }
2596 PUSH(frag(s, e1.out));
2597 break;
2598 }
2599
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002600 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002601 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002602 case NFA_PREV_ATOM_JUST_BEFORE:
2603 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002604 {
2605 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2606 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2607 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2608 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2609 int n;
2610
2611 if (before)
2612 n = *++p; /* get the count */
2613
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002614 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002615 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002616 * The \@<= operator: match for the preceding atom.
2617 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002619 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002620
2621 if (nfa_calc_size == TRUE)
2622 {
2623 nstate += 2;
2624 break;
2625 }
2626 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002627 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002628 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002629 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002630 patch(e.out, s1);
2631
Bram Moolenaar525666f2013-06-02 16:40:55 +02002632 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002633 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002634 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002635 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002636 {
2637 s->negated = TRUE;
2638 s1->negated = TRUE;
2639 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002640 if (before)
Bram Moolenaar61602c52013-06-01 19:54:43 +02002641 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002642 s->val = n; /* store the count */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002643 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2644 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002645
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002646 PUSH(frag(s, list1(&s1->out)));
2647 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002648 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002650#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002651 case NFA_COMPOSING: /* char with composing char */
2652#if 0
2653 /* TODO */
2654 if (regflags & RF_ICOMBINE)
2655 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002656 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002657 }
2658#endif
2659 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002660#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002661
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002662 case NFA_MOPEN: /* \( \) Submatch */
2663 case NFA_MOPEN1:
2664 case NFA_MOPEN2:
2665 case NFA_MOPEN3:
2666 case NFA_MOPEN4:
2667 case NFA_MOPEN5:
2668 case NFA_MOPEN6:
2669 case NFA_MOPEN7:
2670 case NFA_MOPEN8:
2671 case NFA_MOPEN9:
2672#ifdef FEAT_SYN_HL
2673 case NFA_ZOPEN: /* \z( \) Submatch */
2674 case NFA_ZOPEN1:
2675 case NFA_ZOPEN2:
2676 case NFA_ZOPEN3:
2677 case NFA_ZOPEN4:
2678 case NFA_ZOPEN5:
2679 case NFA_ZOPEN6:
2680 case NFA_ZOPEN7:
2681 case NFA_ZOPEN8:
2682 case NFA_ZOPEN9:
2683#endif
2684 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002685 if (nfa_calc_size == TRUE)
2686 {
2687 nstate += 2;
2688 break;
2689 }
2690
2691 mopen = *p;
2692 switch (*p)
2693 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002694 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2695#ifdef FEAT_SYN_HL
2696 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2697 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2698 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2699 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2700 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2701 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2702 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2703 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2704 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2705 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2706#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002707#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002708 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002709#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002710 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002711 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712 mclose = *p + NSUBEXP;
2713 break;
2714 }
2715
2716 /* Allow "NFA_MOPEN" as a valid postfix representation for
2717 * the empty regexp "". In this case, the NFA will be
2718 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2719 * empty groups of parenthesis, and empty mbyte chars */
2720 if (stackp == stack)
2721 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002722 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002723 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002724 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002725 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002726 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002727 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002728 patch(list1(&s->out), s1);
2729 PUSH(frag(s, list1(&s1->out)));
2730 break;
2731 }
2732
2733 /* At least one node was emitted before NFA_MOPEN, so
2734 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2735 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002736 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002737 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002738 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739
Bram Moolenaar525666f2013-06-02 16:40:55 +02002740 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002741 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002742 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002743 patch(e.out, s1);
2744
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002745#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002746 if (mopen == NFA_COMPOSING)
2747 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002749#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002750
2751 PUSH(frag(s, list1(&s1->out)));
2752 break;
2753
Bram Moolenaar5714b802013-05-28 22:03:20 +02002754 case NFA_BACKREF1:
2755 case NFA_BACKREF2:
2756 case NFA_BACKREF3:
2757 case NFA_BACKREF4:
2758 case NFA_BACKREF5:
2759 case NFA_BACKREF6:
2760 case NFA_BACKREF7:
2761 case NFA_BACKREF8:
2762 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002763#ifdef FEAT_SYN_HL
2764 case NFA_ZREF1:
2765 case NFA_ZREF2:
2766 case NFA_ZREF3:
2767 case NFA_ZREF4:
2768 case NFA_ZREF5:
2769 case NFA_ZREF6:
2770 case NFA_ZREF7:
2771 case NFA_ZREF8:
2772 case NFA_ZREF9:
2773#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002774 if (nfa_calc_size == TRUE)
2775 {
2776 nstate += 2;
2777 break;
2778 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002779 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002780 if (s == NULL)
2781 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002782 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002783 if (s1 == NULL)
2784 goto theend;
2785 patch(list1(&s->out), s1);
2786 PUSH(frag(s, list1(&s1->out)));
2787 break;
2788
Bram Moolenaar423532e2013-05-29 21:14:42 +02002789 case NFA_LNUM:
2790 case NFA_LNUM_GT:
2791 case NFA_LNUM_LT:
2792 case NFA_VCOL:
2793 case NFA_VCOL_GT:
2794 case NFA_VCOL_LT:
2795 case NFA_COL:
2796 case NFA_COL_GT:
2797 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002798 case NFA_MARK:
2799 case NFA_MARK_GT:
2800 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002801 {
2802 int n = *++p; /* lnum, col or mark name */
2803
Bram Moolenaar423532e2013-05-29 21:14:42 +02002804 if (nfa_calc_size == TRUE)
2805 {
2806 nstate += 1;
2807 break;
2808 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002809 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002810 if (s == NULL)
2811 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002812 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002813 PUSH(frag(s, list1(&s->out)));
2814 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002815 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002816
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002817 case NFA_ZSTART:
2818 case NFA_ZEND:
2819 default:
2820 /* Operands */
2821 if (nfa_calc_size == TRUE)
2822 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002823 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824 break;
2825 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002826 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002828 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002829 PUSH(frag(s, list1(&s->out)));
2830 break;
2831
2832 } /* switch(*p) */
2833
2834 } /* for(p = postfix; *p; ++p) */
2835
2836 if (nfa_calc_size == TRUE)
2837 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002838 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002839 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840 }
2841
2842 e = POP();
2843 if (stackp != stack)
2844 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2845
2846 if (istate >= nstate)
2847 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2848
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849 matchstate = &state_ptr[istate++]; /* the match state */
2850 matchstate->c = NFA_MATCH;
2851 matchstate->out = matchstate->out1 = NULL;
2852
2853 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002854 ret = e.start;
2855
2856theend:
2857 vim_free(stack);
2858 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002859
2860#undef POP1
2861#undef PUSH1
2862#undef POP2
2863#undef PUSH2
2864#undef POP
2865#undef PUSH
2866}
2867
2868/****************************************************************
2869 * NFA execution code.
2870 ****************************************************************/
2871
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002872typedef struct
2873{
2874 int in_use; /* number of subexpr with useful info */
2875
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002876 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002877 union
2878 {
2879 struct multipos
2880 {
2881 lpos_T start;
2882 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002883 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002884 struct linepos
2885 {
2886 char_u *start;
2887 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002888 } line[NSUBEXP];
2889 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002890} regsub_T;
2891
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002892typedef struct
2893{
2894 regsub_T norm; /* \( .. \) matches */
2895#ifdef FEAT_SYN_HL
2896 regsub_T synt; /* \z( .. \) matches */
2897#endif
2898} regsubs_T;
2899
Bram Moolenaara2d95102013-06-04 14:23:05 +02002900/* nfa_pim_T stores a Postponed Invisible Match. */
2901typedef struct nfa_pim_S nfa_pim_T;
2902struct nfa_pim_S
2903{
2904 nfa_state_T *state;
2905 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2906 nfa_pim_T *pim; /* another PIM at the same position */
2907 regsubs_T subs; /* submatch info, only party used */
2908};
2909
2910/* Values for done in nfa_pim_T. */
2911#define NFA_PIM_TODO 0
2912#define NFA_PIM_MATCH 1
2913#define NFA_PIM_NOMATCH -1
2914
2915
Bram Moolenaar963fee22013-05-26 21:47:28 +02002916/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002917typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918{
2919 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002920 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002921 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002922 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002923} nfa_thread_T;
2924
Bram Moolenaar963fee22013-05-26 21:47:28 +02002925/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926typedef struct
2927{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002928 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002929 int n; /* nr of states currently in "t" */
2930 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002931 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002932} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002933
Bram Moolenaar5714b802013-05-28 22:03:20 +02002934#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002935static void log_subsexpr __ARGS((regsubs_T *subs));
2936static void log_subexpr __ARGS((regsub_T *sub));
2937
2938 static void
2939log_subsexpr(subs)
2940 regsubs_T *subs;
2941{
2942 log_subexpr(&subs->norm);
2943# ifdef FEAT_SYN_HL
2944 log_subexpr(&subs->synt);
2945# endif
2946}
2947
Bram Moolenaar5714b802013-05-28 22:03:20 +02002948 static void
2949log_subexpr(sub)
2950 regsub_T *sub;
2951{
2952 int j;
2953
2954 for (j = 0; j < sub->in_use; j++)
2955 if (REG_MULTI)
2956 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2957 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002958 sub->list.multi[j].start.col,
2959 (int)sub->list.multi[j].start.lnum,
2960 sub->list.multi[j].end.col,
2961 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002962 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002963 {
2964 char *s = (char *)sub->list.line[j].start;
2965 char *e = (char *)sub->list.line[j].end;
2966
Bram Moolenaar5714b802013-05-28 22:03:20 +02002967 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2968 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002969 s == NULL ? "NULL" : s,
2970 e == NULL ? "NULL" : e);
2971 }
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
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003959 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02003960 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003961 case NFA_SPLIT:
3962 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3963 /* avoid recursive stuff */
3964 return 1;
3965 /* two alternatives, use the lowest failure chance */
3966 l = failure_chance(state->out, depth + 1);
3967 r = failure_chance(state->out1, depth + 1);
3968 return l < r ? l : r;
3969
3970 case NFA_ANY:
3971 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003972 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003973 case NFA_MATCH:
3974 /* empty match works always */
3975 return 0;
3976
3977 case NFA_BOL:
3978 case NFA_EOL:
3979 case NFA_BOF:
3980 case NFA_EOF:
3981 case NFA_NEWL:
3982 return 99;
3983
3984 case NFA_BOW:
3985 case NFA_EOW:
3986 return 90;
3987
3988 case NFA_MOPEN:
3989 case NFA_MOPEN1:
3990 case NFA_MOPEN2:
3991 case NFA_MOPEN3:
3992 case NFA_MOPEN4:
3993 case NFA_MOPEN5:
3994 case NFA_MOPEN6:
3995 case NFA_MOPEN7:
3996 case NFA_MOPEN8:
3997 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02003998#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003999 case NFA_ZOPEN:
4000 case NFA_ZOPEN1:
4001 case NFA_ZOPEN2:
4002 case NFA_ZOPEN3:
4003 case NFA_ZOPEN4:
4004 case NFA_ZOPEN5:
4005 case NFA_ZOPEN6:
4006 case NFA_ZOPEN7:
4007 case NFA_ZOPEN8:
4008 case NFA_ZOPEN9:
4009 case NFA_ZCLOSE:
4010 case NFA_ZCLOSE1:
4011 case NFA_ZCLOSE2:
4012 case NFA_ZCLOSE3:
4013 case NFA_ZCLOSE4:
4014 case NFA_ZCLOSE5:
4015 case NFA_ZCLOSE6:
4016 case NFA_ZCLOSE7:
4017 case NFA_ZCLOSE8:
4018 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004019#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004020 case NFA_NOPEN:
4021 case NFA_MCLOSE:
4022 case NFA_MCLOSE1:
4023 case NFA_MCLOSE2:
4024 case NFA_MCLOSE3:
4025 case NFA_MCLOSE4:
4026 case NFA_MCLOSE5:
4027 case NFA_MCLOSE6:
4028 case NFA_MCLOSE7:
4029 case NFA_MCLOSE8:
4030 case NFA_MCLOSE9:
4031 case NFA_NCLOSE:
4032 return failure_chance(state->out, depth + 1);
4033
4034 case NFA_BACKREF1:
4035 case NFA_BACKREF2:
4036 case NFA_BACKREF3:
4037 case NFA_BACKREF4:
4038 case NFA_BACKREF5:
4039 case NFA_BACKREF6:
4040 case NFA_BACKREF7:
4041 case NFA_BACKREF8:
4042 case NFA_BACKREF9:
4043#ifdef FEAT_SYN_HL
4044 case NFA_ZREF1:
4045 case NFA_ZREF2:
4046 case NFA_ZREF3:
4047 case NFA_ZREF4:
4048 case NFA_ZREF5:
4049 case NFA_ZREF6:
4050 case NFA_ZREF7:
4051 case NFA_ZREF8:
4052 case NFA_ZREF9:
4053#endif
4054 /* backreferences don't match in many places */
4055 return 94;
4056
4057 case NFA_LNUM_GT:
4058 case NFA_LNUM_LT:
4059 case NFA_COL_GT:
4060 case NFA_COL_LT:
4061 case NFA_VCOL_GT:
4062 case NFA_VCOL_LT:
4063 case NFA_MARK_GT:
4064 case NFA_MARK_LT:
4065#ifdef FEAT_VISUAL
4066 case NFA_VISUAL:
4067#endif
4068 /* before/after positions don't match very often */
4069 return 85;
4070
4071 case NFA_LNUM:
4072 return 90;
4073
4074 case NFA_CURSOR:
4075 case NFA_COL:
4076 case NFA_VCOL:
4077 case NFA_MARK:
4078 /* specific positions rarely match */
4079 return 98;
4080
4081 case NFA_COMPOSING:
4082 return 95;
4083
4084 default:
4085 if (c > 0)
4086 /* character match fails often */
4087 return 95;
4088 }
4089
4090 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004091 return 50;
4092}
4093
Bram Moolenaarf46da702013-06-02 22:37:42 +02004094/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 * Main matching routine.
4096 *
4097 * Run NFA to determine whether it matches reginput.
4098 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004099 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004100 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004101 * Return TRUE if there is a match, FALSE otherwise.
4102 * Note: Caller must ensure that: start != NULL.
4103 */
4104 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004105nfa_regmatch(prog, start, submatch, m)
4106 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004107 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004108 regsubs_T *submatch;
4109 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004110{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004111 int result;
4112 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004114 int go_to_nextline = FALSE;
4115 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004116 nfa_list_T list[3];
4117 nfa_list_T *listtbl[2][2];
4118 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004119 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004120 nfa_list_T *thislist;
4121 nfa_list_T *nextlist;
4122 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004123 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004124 nfa_state_T *add_state;
4125 int add_count;
4126 int add_off;
4127 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004128#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004129 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004130
4131 if (debug == NULL)
4132 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004133 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004134 return FALSE;
4135 }
4136#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004137 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004138 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004139
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004140 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004141 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004142 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4143 list[0].len = nstate + 1;
4144 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4145 list[1].len = nstate + 1;
4146 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4147 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004148 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
4149 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004150
4151#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004152 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004153 if (log_fd != NULL)
4154 {
4155 fprintf(log_fd, "**********************************\n");
4156 nfa_set_code(start->c);
4157 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4158 abs(start->id), code);
4159 fprintf(log_fd, "**********************************\n");
4160 }
4161 else
4162 {
4163 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4164 log_fd = stderr;
4165 }
4166#endif
4167
4168 thislist = &list[0];
4169 thislist->n = 0;
4170 nextlist = &list[1];
4171 nextlist->n = 0;
4172 neglist = &list[2];
4173 neglist->n = 0;
4174#ifdef ENABLE_LOG
4175 fprintf(log_fd, "(---) STARTSTATE\n");
4176#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004177 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004178 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004179
4180 /* There are two cases when the NFA advances: 1. input char matches the
4181 * NFA node and 2. input char does not match the NFA node, but the next
4182 * node is NFA_NOT. The following macro calls addstate() according to
4183 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4184 listtbl[0][0] = NULL;
4185 listtbl[0][1] = neglist;
4186 listtbl[1][0] = nextlist;
4187 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004188#define ADD_POS_NEG_STATE(state) \
4189 ll = listtbl[result ? 1 : 0][state->negated]; \
4190 if (ll != NULL) { \
4191 add_state = state->out; \
4192 add_off = clen; \
4193 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004194
4195 /*
4196 * Run for each character.
4197 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004198 for (;;)
4199 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004200 int curc;
4201 int clen;
4202
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004203#ifdef FEAT_MBYTE
4204 if (has_mbyte)
4205 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004206 curc = (*mb_ptr2char)(reginput);
4207 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208 }
4209 else
4210#endif
4211 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004212 curc = *reginput;
4213 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004214 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004215 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004216 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004217 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004218 go_to_nextline = FALSE;
4219 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004220
4221 /* swap lists */
4222 thislist = &list[flag];
4223 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004224 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004226 ++nfa_listid;
4227 thislist->id = nfa_listid;
4228 nextlist->id = nfa_listid + 1;
4229 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004230
Bram Moolenaara2d95102013-06-04 14:23:05 +02004231 pimlist.ga_len = 0;
4232
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004233#ifdef ENABLE_LOG
4234 fprintf(log_fd, "------------------------------------------\n");
4235 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004236 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004237 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004238 {
4239 int i;
4240
4241 for (i = 0; i < thislist->n; i++)
4242 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4243 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004244 fprintf(log_fd, "\n");
4245#endif
4246
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004247#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248 fprintf(debug, "\n-------------------\n");
4249#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004250 /*
4251 * If the state lists are empty we can stop.
4252 */
4253 if (thislist->n == 0 && neglist->n == 0)
4254 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004255
4256 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004257 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004258 {
4259 if (neglist->n > 0)
4260 {
4261 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004262 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004263 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004264 }
4265 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004266 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004267
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004268#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269 nfa_set_code(t->state->c);
4270 fprintf(debug, "%s, ", code);
4271#endif
4272#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004273 {
4274 int col;
4275
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004276 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004277 col = -1;
4278 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004279 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004280 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004281 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004282 nfa_set_code(t->state->c);
4283 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4284 abs(t->state->id), (int)t->state->c, code, col);
4285 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004286#endif
4287
4288 /*
4289 * Handle the possible codes of the current state.
4290 * The most important is NFA_MATCH.
4291 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004292 add_state = NULL;
4293 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294 switch (t->state->c)
4295 {
4296 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004297 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004298 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004299 copy_sub(&submatch->norm, &t->subs.norm);
4300#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004301 if (nfa_has_zsubexpr)
4302 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004303#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004305 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004306#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004307 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004308 * states at this position. When the list of states is going
4309 * to be empty quit without advancing, so that "reginput" is
4310 * correct. */
4311 if (nextlist->n == 0 && neglist->n == 0)
4312 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004313 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004314 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004315
4316 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004317 /*
4318 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004319 * NFA_START_INVISIBLE_BEFORE node.
4320 * They surround a zero-width group, used with "\@=", "\&",
4321 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004322 * If we got here, it means that the current "invisible" group
4323 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004324 * nfa_regmatch(). For a look-behind match only when it ends
4325 * in the position in "nfa_endp".
4326 * Submatches are stored in *m, and used in the parent call.
4327 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004328#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004329 if (nfa_endp != NULL)
4330 {
4331 if (REG_MULTI)
4332 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4333 (int)reglnum,
4334 (int)nfa_endp->se_u.pos.lnum,
4335 (int)(reginput - regline),
4336 nfa_endp->se_u.pos.col);
4337 else
4338 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4339 (int)(reginput - regline),
4340 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004341 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004342#endif
4343 /* It's only a match if it ends at "nfa_endp" */
4344 if (nfa_endp != NULL && (REG_MULTI
4345 ? (reglnum != nfa_endp->se_u.pos.lnum
4346 || (int)(reginput - regline)
4347 != nfa_endp->se_u.pos.col)
4348 : reginput != nfa_endp->se_u.ptr))
4349 break;
4350
4351 /* do not set submatches for \@! */
4352 if (!t->state->negated)
4353 {
4354 copy_sub(&m->norm, &t->subs.norm);
4355#ifdef FEAT_SYN_HL
4356 if (nfa_has_zsubexpr)
4357 copy_sub(&m->synt, &t->subs.synt);
4358#endif
4359 }
4360 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004361 break;
4362
4363 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004364 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2d95102013-06-04 14:23:05 +02004365 /* If invisible match has a higher chance to fail, do it
4366 * right away. Otherwise postpone it until what follows is
4367 * matching and causes addstate(nextlist, ..) to be called.
4368 * This is indicated by the "pim" field. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004369 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004370 nfa_pim_T *pim;
4371 int cout = t->state->out1->out->c;
4372
4373 /* Do it directly when what follows is possibly end of
4374 * match (closing paren).
4375 * Postpone when it is \@<= or \@<!, these are expensive.
4376 * TODO: remove the check for t->pim and check multiple
4377 * where it's used?
4378 * Otherwise first do the one that has the highest chance
4379 * of failing. */
4380 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004381#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004382 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004383#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004384 || cout == NFA_NCLOSE
4385 || t->pim != NULL
4386 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4387 && failure_chance(t->state->out1->out, 0)
4388 < failure_chance(t->state->out, 0)))
4389 {
4390 /*
4391 * First try matching the invisible match, then what
4392 * follows.
4393 */
4394 result = recursive_regmatch(t->state, prog,
4395 submatch, m, &listids);
4396
4397 /* for \@! it is a match when result is FALSE */
4398 if (result != t->state->negated)
4399 {
4400 /* Copy submatch info from the recursive call */
4401 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004402#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004403 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004404#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004405
Bram Moolenaara2d95102013-06-04 14:23:05 +02004406 /* t->state->out1 is the corresponding
4407 * END_INVISIBLE node; Add its out to the current
4408 * list (zero-width match). */
4409 addstate_here(thislist, t->state->out1->out,
4410 &t->subs, t->pim, &listidx);
4411 }
4412 }
4413 else
4414 {
4415 /*
4416 * First try matching what follows at the current
4417 * position. Only if a match is found, addstate() is
4418 * called, then verify the invisible match matches.
4419 * Add a nfa_pim_T to the following states, it
4420 * contains info about the invisible match.
4421 */
4422 if (ga_grow(&pimlist, 1) == FAIL)
4423 goto theend;
4424 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4425 ++pimlist.ga_len;
4426 pim->state = t->state;
4427 pim->pim = NULL;
4428 pim->result = NFA_PIM_TODO;
4429
4430 /* t->state->out1 is the corresponding END_INVISIBLE
4431 * node; Add its out to the current list (zero-width
4432 * match). */
4433 addstate_here(thislist, t->state->out1->out, &t->subs,
4434 pim, &listidx);
4435 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004436 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004437 break;
4438
4439 case NFA_BOL:
4440 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004441 addstate_here(thislist, t->state->out, &t->subs,
4442 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 break;
4444
4445 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004446 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004447 addstate_here(thislist, t->state->out, &t->subs,
4448 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004449 break;
4450
4451 case NFA_BOW:
4452 {
4453 int bow = TRUE;
4454
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004455 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004456 bow = FALSE;
4457#ifdef FEAT_MBYTE
4458 else if (has_mbyte)
4459 {
4460 int this_class;
4461
4462 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004463 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004464 if (this_class <= 1)
4465 bow = FALSE;
4466 else if (reg_prev_class() == this_class)
4467 bow = FALSE;
4468 }
4469#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004470 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004471 || (reginput > regline
4472 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004473 bow = FALSE;
4474 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004475 addstate_here(thislist, t->state->out, &t->subs,
4476 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004477 break;
4478 }
4479
4480 case NFA_EOW:
4481 {
4482 int eow = TRUE;
4483
4484 if (reginput == regline)
4485 eow = FALSE;
4486#ifdef FEAT_MBYTE
4487 else if (has_mbyte)
4488 {
4489 int this_class, prev_class;
4490
4491 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004492 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004493 prev_class = reg_prev_class();
4494 if (this_class == prev_class
4495 || prev_class == 0 || prev_class == 1)
4496 eow = FALSE;
4497 }
4498#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004499 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004500 || (reginput[0] != NUL
4501 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004502 eow = FALSE;
4503 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004504 addstate_here(thislist, t->state->out, &t->subs,
4505 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004506 break;
4507 }
4508
Bram Moolenaar4b780632013-05-31 22:14:52 +02004509 case NFA_BOF:
4510 if (reglnum == 0 && reginput == regline
4511 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004512 addstate_here(thislist, t->state->out, &t->subs,
4513 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004514 break;
4515
4516 case NFA_EOF:
4517 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004518 addstate_here(thislist, t->state->out, &t->subs,
4519 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004520 break;
4521
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004522#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004523 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004524 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004525 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004526 int len = 0;
4527 nfa_state_T *end;
4528 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004529 int cchars[MAX_MCO];
4530 int ccount = 0;
4531 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004532
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004533 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004534 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004535 if (utf_iscomposing(sta->c))
4536 {
4537 /* Only match composing character(s), ignore base
4538 * character. Used for ".{composing}" and "{composing}"
4539 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004540 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004541 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004542 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004543 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004544 /* If \Z was present, then ignore composing characters.
4545 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004546 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004547 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004548 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004549 else
4550 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004551 while (sta->c != NFA_END_COMPOSING)
4552 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004554
4555 /* Check base character matches first, unless ignored. */
4556 else if (len > 0 || mc == sta->c)
4557 {
4558 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004559 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004560 len += mb_char2len(mc);
4561 sta = sta->out;
4562 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004564 /* We don't care about the order of composing characters.
4565 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004566 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004567 {
4568 mc = mb_ptr2char(reginput + len);
4569 cchars[ccount++] = mc;
4570 len += mb_char2len(mc);
4571 if (ccount == MAX_MCO)
4572 break;
4573 }
4574
4575 /* Check that each composing char in the pattern matches a
4576 * composing char in the text. We do not check if all
4577 * composing chars are matched. */
4578 result = OK;
4579 while (sta->c != NFA_END_COMPOSING)
4580 {
4581 for (j = 0; j < ccount; ++j)
4582 if (cchars[j] == sta->c)
4583 break;
4584 if (j == ccount)
4585 {
4586 result = FAIL;
4587 break;
4588 }
4589 sta = sta->out;
4590 }
4591 }
4592 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004593 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004594
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004595 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004596 ADD_POS_NEG_STATE(end);
4597 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004598 }
4599#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600
4601 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004602 if (curc == NUL && !reg_line_lbr && REG_MULTI
4603 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004604 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004605 go_to_nextline = TRUE;
4606 /* Pass -1 for the offset, which means taking the position
4607 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004608 ll = nextlist;
4609 add_state = t->state->out;
4610 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004612 else if (curc == '\n' && reg_line_lbr)
4613 {
4614 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004615 ll = nextlist;
4616 add_state = t->state->out;
4617 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004618 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004619 break;
4620
4621 case NFA_CLASS_ALNUM:
4622 case NFA_CLASS_ALPHA:
4623 case NFA_CLASS_BLANK:
4624 case NFA_CLASS_CNTRL:
4625 case NFA_CLASS_DIGIT:
4626 case NFA_CLASS_GRAPH:
4627 case NFA_CLASS_LOWER:
4628 case NFA_CLASS_PRINT:
4629 case NFA_CLASS_PUNCT:
4630 case NFA_CLASS_SPACE:
4631 case NFA_CLASS_UPPER:
4632 case NFA_CLASS_XDIGIT:
4633 case NFA_CLASS_TAB:
4634 case NFA_CLASS_RETURN:
4635 case NFA_CLASS_BACKSPACE:
4636 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004637 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638 ADD_POS_NEG_STATE(t->state);
4639 break;
4640
4641 case NFA_END_NEG_RANGE:
4642 /* This follows a series of negated nodes, like:
4643 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004644 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004645 {
4646 ll = nextlist;
4647 add_state = t->state->out;
4648 add_off = clen;
4649 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004650 break;
4651
4652 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004653 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004654 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004655 {
4656 ll = nextlist;
4657 add_state = t->state->out;
4658 add_off = clen;
4659 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004660 break;
4661
4662 /*
4663 * Character classes like \a for alpha, \d for digit etc.
4664 */
4665 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004666 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004667 ADD_POS_NEG_STATE(t->state);
4668 break;
4669
4670 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004671 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004672 ADD_POS_NEG_STATE(t->state);
4673 break;
4674
4675 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004676 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677 ADD_POS_NEG_STATE(t->state);
4678 break;
4679
4680 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004681 result = !VIM_ISDIGIT(curc)
4682 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683 ADD_POS_NEG_STATE(t->state);
4684 break;
4685
4686 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004687 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 ADD_POS_NEG_STATE(t->state);
4689 break;
4690
4691 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004692 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004693 ADD_POS_NEG_STATE(t->state);
4694 break;
4695
4696 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004697 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 ADD_POS_NEG_STATE(t->state);
4699 break;
4700
4701 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004702 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004703 ADD_POS_NEG_STATE(t->state);
4704 break;
4705
4706 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004707 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 ADD_POS_NEG_STATE(t->state);
4709 break;
4710
4711 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004712 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004713 ADD_POS_NEG_STATE(t->state);
4714 break;
4715
4716 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004717 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004718 ADD_POS_NEG_STATE(t->state);
4719 break;
4720
4721 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004722 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004723 ADD_POS_NEG_STATE(t->state);
4724 break;
4725
4726 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004727 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004728 ADD_POS_NEG_STATE(t->state);
4729 break;
4730
4731 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004732 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733 ADD_POS_NEG_STATE(t->state);
4734 break;
4735
4736 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004737 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004738 ADD_POS_NEG_STATE(t->state);
4739 break;
4740
4741 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004742 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004743 ADD_POS_NEG_STATE(t->state);
4744 break;
4745
4746 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004747 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004748 ADD_POS_NEG_STATE(t->state);
4749 break;
4750
4751 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004752 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004753 ADD_POS_NEG_STATE(t->state);
4754 break;
4755
4756 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004757 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004758 ADD_POS_NEG_STATE(t->state);
4759 break;
4760
4761 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004762 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004763 ADD_POS_NEG_STATE(t->state);
4764 break;
4765
4766 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004767 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004768 ADD_POS_NEG_STATE(t->state);
4769 break;
4770
4771 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004772 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004773 ADD_POS_NEG_STATE(t->state);
4774 break;
4775
4776 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004777 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 ADD_POS_NEG_STATE(t->state);
4779 break;
4780
4781 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004782 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004783 ADD_POS_NEG_STATE(t->state);
4784 break;
4785
4786 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004787 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004788 ADD_POS_NEG_STATE(t->state);
4789 break;
4790
4791 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004792 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004793 ADD_POS_NEG_STATE(t->state);
4794 break;
4795
Bram Moolenaar5714b802013-05-28 22:03:20 +02004796 case NFA_BACKREF1:
4797 case NFA_BACKREF2:
4798 case NFA_BACKREF3:
4799 case NFA_BACKREF4:
4800 case NFA_BACKREF5:
4801 case NFA_BACKREF6:
4802 case NFA_BACKREF7:
4803 case NFA_BACKREF8:
4804 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004805#ifdef FEAT_SYN_HL
4806 case NFA_ZREF1:
4807 case NFA_ZREF2:
4808 case NFA_ZREF3:
4809 case NFA_ZREF4:
4810 case NFA_ZREF5:
4811 case NFA_ZREF6:
4812 case NFA_ZREF7:
4813 case NFA_ZREF8:
4814 case NFA_ZREF9:
4815#endif
4816 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004817 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004818 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004819 int bytelen;
4820
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004821 if (t->state->c <= NFA_BACKREF9)
4822 {
4823 subidx = t->state->c - NFA_BACKREF1 + 1;
4824 result = match_backref(&t->subs.norm, subidx, &bytelen);
4825 }
4826#ifdef FEAT_SYN_HL
4827 else
4828 {
4829 subidx = t->state->c - NFA_ZREF1 + 1;
4830 result = match_zref(subidx, &bytelen);
4831 }
4832#endif
4833
Bram Moolenaar5714b802013-05-28 22:03:20 +02004834 if (result)
4835 {
4836 if (bytelen == 0)
4837 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004838 /* empty match always works, output of NFA_SKIP to be
4839 * used next */
4840 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004841 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004842 }
4843 else if (bytelen <= clen)
4844 {
4845 /* match current character, jump ahead to out of
4846 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004847 ll = nextlist;
4848 add_state = t->state->out->out;
4849 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004850#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004851 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004852#endif
4853 }
4854 else
4855 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004856 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004857 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004858 ll = nextlist;
4859 add_state = t->state->out;
4860 add_off = bytelen;
4861 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004862#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004863 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004864#endif
4865 }
4866
4867 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004868 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004869 }
4870 case NFA_SKIP:
4871 /* charater of previous matching \1 .. \9 */
4872 if (t->count - clen <= 0)
4873 {
4874 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004875 ll = nextlist;
4876 add_state = t->state->out;
4877 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004878#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004879 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004880#endif
4881 }
4882 else
4883 {
4884 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004885 ll = nextlist;
4886 add_state = t->state;
4887 add_off = 0;
4888 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004889#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004890 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004891#endif
4892 }
4893 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004894
4895 case NFA_SKIP_CHAR:
4896 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004897 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004898 /* TODO: should not happen? */
4899 break;
4900
Bram Moolenaar423532e2013-05-29 21:14:42 +02004901 case NFA_LNUM:
4902 case NFA_LNUM_GT:
4903 case NFA_LNUM_LT:
4904 result = (REG_MULTI &&
4905 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4906 (long_u)(reglnum + reg_firstlnum)));
4907 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004908 addstate_here(thislist, t->state->out, &t->subs,
4909 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004910 break;
4911
4912 case NFA_COL:
4913 case NFA_COL_GT:
4914 case NFA_COL_LT:
4915 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4916 (long_u)(reginput - regline) + 1);
4917 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004918 addstate_here(thislist, t->state->out, &t->subs,
4919 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004920 break;
4921
4922 case NFA_VCOL:
4923 case NFA_VCOL_GT:
4924 case NFA_VCOL_LT:
4925 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4926 (long_u)win_linetabsize(
4927 reg_win == NULL ? curwin : reg_win,
4928 regline, (colnr_T)(reginput - regline)) + 1);
4929 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004930 addstate_here(thislist, t->state->out, &t->subs,
4931 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004932 break;
4933
Bram Moolenaar044aa292013-06-04 21:27:38 +02004934 case NFA_MARK:
4935 case NFA_MARK_GT:
4936 case NFA_MARK_LT:
4937 {
4938 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
4939
4940 /* Compare the mark position to the match position. */
4941 result = (pos != NULL /* mark doesn't exist */
4942 && pos->lnum > 0 /* mark isn't set in reg_buf */
4943 && (pos->lnum == reglnum + reg_firstlnum
4944 ? (pos->col == (colnr_T)(reginput - regline)
4945 ? t->state->c == NFA_MARK
4946 : (pos->col < (colnr_T)(reginput - regline)
4947 ? t->state->c == NFA_MARK_GT
4948 : t->state->c == NFA_MARK_LT))
4949 : (pos->lnum < reglnum + reg_firstlnum
4950 ? t->state->c == NFA_MARK_GT
4951 : t->state->c == NFA_MARK_LT)));
4952 if (result)
4953 addstate_here(thislist, t->state->out, &t->subs,
4954 t->pim, &listidx);
4955 break;
4956 }
4957
Bram Moolenaar423532e2013-05-29 21:14:42 +02004958 case NFA_CURSOR:
4959 result = (reg_win != NULL
4960 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4961 && ((colnr_T)(reginput - regline)
4962 == reg_win->w_cursor.col));
4963 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004964 addstate_here(thislist, t->state->out, &t->subs,
4965 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004966 break;
4967
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02004968#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004969 case NFA_VISUAL:
4970 result = reg_match_visual();
4971 if (result)
4972 addstate_here(thislist, t->state->out, &t->subs,
4973 t->pim, &listidx);
4974 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02004975#endif
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004976
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004977 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004978 {
4979 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004980
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004981 /* TODO: put this in #ifdef later */
4982 if (c < -256)
4983 EMSGN("INTERNAL: Negative state char: %ld", c);
4984 if (is_Magic(c))
4985 c = un_Magic(c);
4986 result = (c == curc);
4987
4988 if (!result && ireg_ic)
4989 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004990#ifdef FEAT_MBYTE
4991 /* If there is a composing character which is not being
4992 * ignored there can be no match. Match with composing
4993 * character uses NFA_COMPOSING above. */
4994 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004995 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004996 result = FALSE;
4997#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004998 ADD_POS_NEG_STATE(t->state);
4999 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005000 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005001
5002 } /* switch (t->state->c) */
5003
5004 if (add_state != NULL)
5005 {
5006 if (t->pim != NULL)
5007 {
5008 /* postponed invisible match */
5009 /* TODO: also do t->pim->pim recursively? */
5010 if (t->pim->result == NFA_PIM_TODO)
5011 {
5012#ifdef ENABLE_LOG
5013 fprintf(log_fd, "\n");
5014 fprintf(log_fd, "==================================\n");
5015 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5016 fprintf(log_fd, "\n");
5017#endif
5018 result = recursive_regmatch(t->pim->state,
5019 prog, submatch, m, &listids);
5020 t->pim->result = result ? NFA_PIM_MATCH
5021 : NFA_PIM_NOMATCH;
5022 /* for \@! it is a match when result is FALSE */
5023 if (result != t->pim->state->negated)
5024 {
5025 /* Copy submatch info from the recursive call */
5026 copy_sub_off(&t->pim->subs.norm, &m->norm);
5027#ifdef FEAT_SYN_HL
5028 copy_sub_off(&t->pim->subs.synt, &m->synt);
5029#endif
5030 }
5031 }
5032 else
5033 {
5034 result = (t->pim->result == NFA_PIM_MATCH);
5035#ifdef ENABLE_LOG
5036 fprintf(log_fd, "\n");
5037 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5038 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5039 fprintf(log_fd, "\n");
5040#endif
5041 }
5042
5043 /* for \@! it is a match when result is FALSE */
5044 if (result != t->pim->state->negated)
5045 {
5046 /* Copy submatch info from the recursive call */
5047 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5048#ifdef FEAT_SYN_HL
5049 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
5050#endif
5051 }
5052 else
5053 /* look-behind match failed, don't add the state */
5054 continue;
5055 }
5056
5057 addstate(ll, add_state, &t->subs, add_off);
5058 if (add_count > 0)
5059 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005060 }
5061
5062 } /* for (thislist = thislist; thislist->state; thislist++) */
5063
Bram Moolenaare23febd2013-05-26 18:40:14 +02005064 /* Look for the start of a match in the current position by adding the
5065 * start state to the list of states.
5066 * The first found match is the leftmost one, thus the order of states
5067 * matters!
5068 * Do not add the start state in recursive calls of nfa_regmatch(),
5069 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005070 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005071 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005072 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005073 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005074 && reglnum == 0
5075 && clen != 0
5076 && (ireg_maxcol == 0
5077 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005078 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005079 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005080 ? (reglnum < nfa_endp->se_u.pos.lnum
5081 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005082 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005083 < nfa_endp->se_u.pos.col))
5084 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005085 {
5086#ifdef ENABLE_LOG
5087 fprintf(log_fd, "(---) STARTSTATE\n");
5088#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005089 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005090 }
5091
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005092#ifdef ENABLE_LOG
5093 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005094 {
5095 int i;
5096
5097 for (i = 0; i < thislist->n; i++)
5098 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5099 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005100 fprintf(log_fd, "\n");
5101#endif
5102
5103nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005104 /* Advance to the next character, or advance to the next line, or
5105 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005106 if (clen != 0)
5107 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005108 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5109 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005110 reg_nextline();
5111 else
5112 break;
5113 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005114
5115#ifdef ENABLE_LOG
5116 if (log_fd != stderr)
5117 fclose(log_fd);
5118 log_fd = NULL;
5119#endif
5120
5121theend:
5122 /* Free memory */
5123 vim_free(list[0].t);
5124 vim_free(list[1].t);
5125 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005126 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005127 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005128#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005129#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005130 fclose(debug);
5131#endif
5132
Bram Moolenaar963fee22013-05-26 21:47:28 +02005133 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005134}
5135
5136/*
5137 * Try match of "prog" with at regline["col"].
5138 * Returns 0 for failure, number of lines contained in the match otherwise.
5139 */
5140 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005141nfa_regtry(prog, col)
5142 nfa_regprog_T *prog;
5143 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005144{
5145 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005146 regsubs_T subs, m;
5147 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005148#ifdef ENABLE_LOG
5149 FILE *f;
5150#endif
5151
5152 reginput = regline + col;
5153 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005154#ifdef FEAT_SYN_HL
5155 /* Clear the external match subpointers if necessary. */
5156 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005157 {
5158 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005159 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005160 }
5161 else
5162 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005163#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005164
5165#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005166 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005167 if (f != NULL)
5168 {
5169 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
5170 fprintf(f, " =======================================================\n");
5171#ifdef DEBUG
5172 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5173#endif
5174 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005175 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005176 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005177 fprintf(f, "\n\n");
5178 fclose(f);
5179 }
5180 else
5181 EMSG(_("Could not open temporary log file for writing "));
5182#endif
5183
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005184 clear_sub(&subs.norm);
5185 clear_sub(&m.norm);
5186#ifdef FEAT_SYN_HL
5187 clear_sub(&subs.synt);
5188 clear_sub(&m.synt);
5189#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005190
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005191 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005192 return 0;
5193
5194 cleanup_subexpr();
5195 if (REG_MULTI)
5196 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005197 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005198 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005199 reg_startpos[i] = subs.norm.list.multi[i].start;
5200 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005201 }
5202
5203 if (reg_startpos[0].lnum < 0)
5204 {
5205 reg_startpos[0].lnum = 0;
5206 reg_startpos[0].col = col;
5207 }
5208 if (reg_endpos[0].lnum < 0)
5209 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005210 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005211 reg_endpos[0].lnum = reglnum;
5212 reg_endpos[0].col = (int)(reginput - regline);
5213 }
5214 else
5215 /* Use line number of "\ze". */
5216 reglnum = reg_endpos[0].lnum;
5217 }
5218 else
5219 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005220 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005221 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005222 reg_startp[i] = subs.norm.list.line[i].start;
5223 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005224 }
5225
5226 if (reg_startp[0] == NULL)
5227 reg_startp[0] = regline + col;
5228 if (reg_endp[0] == NULL)
5229 reg_endp[0] = reginput;
5230 }
5231
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005232#ifdef FEAT_SYN_HL
5233 /* Package any found \z(...\) matches for export. Default is none. */
5234 unref_extmatch(re_extmatch_out);
5235 re_extmatch_out = NULL;
5236
5237 if (prog->reghasz == REX_SET)
5238 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005239 cleanup_zsubexpr();
5240 re_extmatch_out = make_extmatch();
5241 for (i = 0; i < subs.synt.in_use; i++)
5242 {
5243 if (REG_MULTI)
5244 {
5245 struct multipos *mpos = &subs.synt.list.multi[i];
5246
5247 /* Only accept single line matches. */
5248 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5249 re_extmatch_out->matches[i] =
5250 vim_strnsave(reg_getline(mpos->start.lnum)
5251 + mpos->start.col,
5252 mpos->end.col - mpos->start.col);
5253 }
5254 else
5255 {
5256 struct linepos *lpos = &subs.synt.list.line[i];
5257
5258 if (lpos->start != NULL && lpos->end != NULL)
5259 re_extmatch_out->matches[i] =
5260 vim_strnsave(lpos->start,
5261 (int)(lpos->end - lpos->start));
5262 }
5263 }
5264 }
5265#endif
5266
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005267 return 1 + reglnum;
5268}
5269
5270/*
5271 * Match a regexp against a string ("line" points to the string) or multiple
5272 * lines ("line" is NULL, use reg_getline()).
5273 *
5274 * Returns 0 for failure, number of lines contained in the match otherwise.
5275 */
5276 static long
5277nfa_regexec_both(line, col)
5278 char_u *line;
5279 colnr_T col; /* column to start looking for match */
5280{
5281 nfa_regprog_T *prog;
5282 long retval = 0L;
5283 int i;
5284
5285 if (REG_MULTI)
5286 {
5287 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5288 line = reg_getline((linenr_T)0); /* relative to the cursor */
5289 reg_startpos = reg_mmatch->startpos;
5290 reg_endpos = reg_mmatch->endpos;
5291 }
5292 else
5293 {
5294 prog = (nfa_regprog_T *)reg_match->regprog;
5295 reg_startp = reg_match->startp;
5296 reg_endp = reg_match->endp;
5297 }
5298
5299 /* Be paranoid... */
5300 if (prog == NULL || line == NULL)
5301 {
5302 EMSG(_(e_null));
5303 goto theend;
5304 }
5305
5306 /* If the start column is past the maximum column: no need to try. */
5307 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5308 goto theend;
5309
5310 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5311 if (prog->regflags & RF_ICASE)
5312 ireg_ic = TRUE;
5313 else if (prog->regflags & RF_NOICASE)
5314 ireg_ic = FALSE;
5315
5316#ifdef FEAT_MBYTE
5317 /* If pattern contains "\Z" overrule value of ireg_icombine */
5318 if (prog->regflags & RF_ICOMBINE)
5319 ireg_icombine = TRUE;
5320#endif
5321
5322 regline = line;
5323 reglnum = 0; /* relative to line */
5324
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005325 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005326 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005327 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005328 nfa_listid = 1;
5329 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005330#ifdef DEBUG
5331 nfa_regengine.expr = prog->pattern;
5332#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005333
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005334 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005335 for (i = 0; i < nstate; ++i)
5336 {
5337 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005338 prog->state[i].lastlist[0] = 0;
5339 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005340 }
5341
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005342 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005343
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005344#ifdef DEBUG
5345 nfa_regengine.expr = NULL;
5346#endif
5347
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005348theend:
5349 return retval;
5350}
5351
5352/*
5353 * Compile a regular expression into internal code for the NFA matcher.
5354 * Returns the program in allocated space. Returns NULL for an error.
5355 */
5356 static regprog_T *
5357nfa_regcomp(expr, re_flags)
5358 char_u *expr;
5359 int re_flags;
5360{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005361 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005362 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005363 int *postfix;
5364
5365 if (expr == NULL)
5366 return NULL;
5367
5368#ifdef DEBUG
5369 nfa_regengine.expr = expr;
5370#endif
5371
5372 init_class_tab();
5373
5374 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5375 return NULL;
5376
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005377 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005378 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005379 postfix = re2post();
5380 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005381 {
5382 /* TODO: only give this error for debugging? */
5383 if (post_ptr >= post_end)
5384 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005385 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005386 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005387
5388 /*
5389 * In order to build the NFA, we parse the input regexp twice:
5390 * 1. first pass to count size (so we can allocate space)
5391 * 2. second to emit code
5392 */
5393#ifdef ENABLE_LOG
5394 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005395 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005396
5397 if (f != NULL)
5398 {
5399 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5400 fclose(f);
5401 }
5402 }
5403#endif
5404
5405 /*
5406 * PASS 1
5407 * Count number of NFA states in "nstate". Do not build the NFA.
5408 */
5409 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005410
5411 /* Space for compiled regexp */
5412 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5413 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5414 if (prog == NULL)
5415 goto fail;
5416 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005417 state_ptr = prog->state;
5418
5419 /*
5420 * PASS 2
5421 * Build the NFA
5422 */
5423 prog->start = post2nfa(postfix, post_ptr, FALSE);
5424 if (prog->start == NULL)
5425 goto fail;
5426
5427 prog->regflags = regflags;
5428 prog->engine = &nfa_regengine;
5429 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005430 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005431 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005432 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005433#ifdef ENABLE_LOG
5434 nfa_postfix_dump(expr, OK);
5435 nfa_dump(prog);
5436#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005437#ifdef FEAT_SYN_HL
5438 /* Remember whether this pattern has any \z specials in it. */
5439 prog->reghasz = re_has_z;
5440#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005441#ifdef DEBUG
5442 prog->pattern = vim_strsave(expr); /* memory will leak */
5443 nfa_regengine.expr = NULL;
5444#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005445
5446out:
5447 vim_free(post_start);
5448 post_start = post_ptr = post_end = NULL;
5449 state_ptr = NULL;
5450 return (regprog_T *)prog;
5451
5452fail:
5453 vim_free(prog);
5454 prog = NULL;
5455#ifdef ENABLE_LOG
5456 nfa_postfix_dump(expr, FAIL);
5457#endif
5458#ifdef DEBUG
5459 nfa_regengine.expr = NULL;
5460#endif
5461 goto out;
5462}
5463
5464
5465/*
5466 * Match a regexp against a string.
5467 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5468 * Uses curbuf for line count and 'iskeyword'.
5469 *
5470 * Return TRUE if there is a match, FALSE if not.
5471 */
5472 static int
5473nfa_regexec(rmp, line, col)
5474 regmatch_T *rmp;
5475 char_u *line; /* string to match against */
5476 colnr_T col; /* column to start looking for match */
5477{
5478 reg_match = rmp;
5479 reg_mmatch = NULL;
5480 reg_maxline = 0;
5481 reg_line_lbr = FALSE;
5482 reg_buf = curbuf;
5483 reg_win = NULL;
5484 ireg_ic = rmp->rm_ic;
5485#ifdef FEAT_MBYTE
5486 ireg_icombine = FALSE;
5487#endif
5488 ireg_maxcol = 0;
5489 return (nfa_regexec_both(line, col) != 0);
5490}
5491
5492#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5493 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5494
5495static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5496
5497/*
5498 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5499 */
5500 static int
5501nfa_regexec_nl(rmp, line, col)
5502 regmatch_T *rmp;
5503 char_u *line; /* string to match against */
5504 colnr_T col; /* column to start looking for match */
5505{
5506 reg_match = rmp;
5507 reg_mmatch = NULL;
5508 reg_maxline = 0;
5509 reg_line_lbr = TRUE;
5510 reg_buf = curbuf;
5511 reg_win = NULL;
5512 ireg_ic = rmp->rm_ic;
5513#ifdef FEAT_MBYTE
5514 ireg_icombine = FALSE;
5515#endif
5516 ireg_maxcol = 0;
5517 return (nfa_regexec_both(line, col) != 0);
5518}
5519#endif
5520
5521
5522/*
5523 * Match a regexp against multiple lines.
5524 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5525 * Uses curbuf for line count and 'iskeyword'.
5526 *
5527 * Return zero if there is no match. Return number of lines contained in the
5528 * match otherwise.
5529 *
5530 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5531 *
5532 * ! Also NOTE : match may actually be in another line. e.g.:
5533 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5534 *
5535 * +-------------------------+
5536 * |a |
5537 * |b |
5538 * |c |
5539 * | |
5540 * +-------------------------+
5541 *
5542 * then nfa_regexec_multi() returns 3. while the original
5543 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5544 *
5545 * FIXME if this behavior is not compatible.
5546 */
5547 static long
5548nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5549 regmmatch_T *rmp;
5550 win_T *win; /* window in which to search or NULL */
5551 buf_T *buf; /* buffer in which to search */
5552 linenr_T lnum; /* nr of line to start looking for match */
5553 colnr_T col; /* column to start looking for match */
5554 proftime_T *tm UNUSED; /* timeout limit or NULL */
5555{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005556 reg_match = NULL;
5557 reg_mmatch = rmp;
5558 reg_buf = buf;
5559 reg_win = win;
5560 reg_firstlnum = lnum;
5561 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5562 reg_line_lbr = FALSE;
5563 ireg_ic = rmp->rmm_ic;
5564#ifdef FEAT_MBYTE
5565 ireg_icombine = FALSE;
5566#endif
5567 ireg_maxcol = rmp->rmm_maxcol;
5568
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005569 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005570}
5571
5572#ifdef DEBUG
5573# undef ENABLE_LOG
5574#endif