blob: ec5543c466d3f5e4348d30094e5760e4f5fd25e5 [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 Moolenaar87953742013-06-05 18:52:40 +020060 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020061 NFA_END_INVISIBLE,
Bram Moolenaar87953742013-06-05 18:52:40 +020062 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020063 NFA_COMPOSING, /* Next nodes in NFA are part of the
64 composing multibyte char */
65 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020066 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020067
68 /* The following are used only in the postfix form, not in the NFA */
69 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
70 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
71 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
72 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
73 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
74
Bram Moolenaar5714b802013-05-28 22:03:20 +020075 NFA_BACKREF1, /* \1 */
76 NFA_BACKREF2, /* \2 */
77 NFA_BACKREF3, /* \3 */
78 NFA_BACKREF4, /* \4 */
79 NFA_BACKREF5, /* \5 */
80 NFA_BACKREF6, /* \6 */
81 NFA_BACKREF7, /* \7 */
82 NFA_BACKREF8, /* \8 */
83 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020084#ifdef FEAT_SYN_HL
85 NFA_ZREF1, /* \z1 */
86 NFA_ZREF2, /* \z2 */
87 NFA_ZREF3, /* \z3 */
88 NFA_ZREF4, /* \z4 */
89 NFA_ZREF5, /* \z5 */
90 NFA_ZREF6, /* \z6 */
91 NFA_ZREF7, /* \z7 */
92 NFA_ZREF8, /* \z8 */
93 NFA_ZREF9, /* \z9 */
94#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +020095 NFA_SKIP, /* Skip characters */
96
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020097 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +020098 NFA_MOPEN1,
99 NFA_MOPEN2,
100 NFA_MOPEN3,
101 NFA_MOPEN4,
102 NFA_MOPEN5,
103 NFA_MOPEN6,
104 NFA_MOPEN7,
105 NFA_MOPEN8,
106 NFA_MOPEN9,
107
108 NFA_MCLOSE,
109 NFA_MCLOSE1,
110 NFA_MCLOSE2,
111 NFA_MCLOSE3,
112 NFA_MCLOSE4,
113 NFA_MCLOSE5,
114 NFA_MCLOSE6,
115 NFA_MCLOSE7,
116 NFA_MCLOSE8,
117 NFA_MCLOSE9,
118
119#ifdef FEAT_SYN_HL
120 NFA_ZOPEN,
121 NFA_ZOPEN1,
122 NFA_ZOPEN2,
123 NFA_ZOPEN3,
124 NFA_ZOPEN4,
125 NFA_ZOPEN5,
126 NFA_ZOPEN6,
127 NFA_ZOPEN7,
128 NFA_ZOPEN8,
129 NFA_ZOPEN9,
130
131 NFA_ZCLOSE,
132 NFA_ZCLOSE1,
133 NFA_ZCLOSE2,
134 NFA_ZCLOSE3,
135 NFA_ZCLOSE4,
136 NFA_ZCLOSE5,
137 NFA_ZCLOSE6,
138 NFA_ZCLOSE7,
139 NFA_ZCLOSE8,
140 NFA_ZCLOSE9,
141#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200142
143 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200144 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200145 NFA_ANYOF, /* Match any character in this string. */
146 NFA_ANYBUT, /* Match any character not in this string. */
147 NFA_IDENT, /* Match identifier char */
148 NFA_SIDENT, /* Match identifier char but no digit */
149 NFA_KWORD, /* Match keyword char */
150 NFA_SKWORD, /* Match word char but no digit */
151 NFA_FNAME, /* Match file name char */
152 NFA_SFNAME, /* Match file name char but no digit */
153 NFA_PRINT, /* Match printable char */
154 NFA_SPRINT, /* Match printable char but no digit */
155 NFA_WHITE, /* Match whitespace char */
156 NFA_NWHITE, /* Match non-whitespace char */
157 NFA_DIGIT, /* Match digit char */
158 NFA_NDIGIT, /* Match non-digit char */
159 NFA_HEX, /* Match hex char */
160 NFA_NHEX, /* Match non-hex char */
161 NFA_OCTAL, /* Match octal char */
162 NFA_NOCTAL, /* Match non-octal char */
163 NFA_WORD, /* Match word char */
164 NFA_NWORD, /* Match non-word char */
165 NFA_HEAD, /* Match head char */
166 NFA_NHEAD, /* Match non-head char */
167 NFA_ALPHA, /* Match alpha char */
168 NFA_NALPHA, /* Match non-alpha char */
169 NFA_LOWER, /* Match lowercase char */
170 NFA_NLOWER, /* Match non-lowercase char */
171 NFA_UPPER, /* Match uppercase char */
172 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200173
174 NFA_CURSOR, /* Match cursor pos */
175 NFA_LNUM, /* Match line number */
176 NFA_LNUM_GT, /* Match > line number */
177 NFA_LNUM_LT, /* Match < line number */
178 NFA_COL, /* Match cursor column */
179 NFA_COL_GT, /* Match > cursor column */
180 NFA_COL_LT, /* Match < cursor column */
181 NFA_VCOL, /* Match cursor virtual column */
182 NFA_VCOL_GT, /* Match > cursor virtual column */
183 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200184 NFA_MARK, /* Match mark */
185 NFA_MARK_GT, /* Match > mark */
186 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200187 NFA_VISUAL, /* Match Visual area */
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
964 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200965 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200966 break;
967
968 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200969 {
970 int n;
971
972 /* \%[abc] */
973 for (n = 0; (c = getchr()) != ']'; ++n)
974 {
975 if (c == NUL)
976 EMSG2_RET_FAIL(_(e_missing_sb),
977 reg_magic == MAGIC_ALL);
978 EMIT(c);
979 }
Bram Moolenaar2976c022013-06-05 21:30:37 +0200980 if (n == 0)
981 EMSG2_RET_FAIL(_(e_empty_sb),
982 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200983 EMIT(NFA_OPT_CHARS);
984 EMIT(n);
985 break;
986 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200987
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200988 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200989 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200990 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200991 int cmp = c;
992
993 if (c == '<' || c == '>')
994 c = getchr();
995 while (VIM_ISDIGIT(c))
996 {
997 n = n * 10 + (c - '0');
998 c = getchr();
999 }
1000 if (c == 'l' || c == 'c' || c == 'v')
1001 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001002 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001003 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001004 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001005 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001006 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001007 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001008 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001009 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001010 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001011 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001012 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001013 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001014 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001015 break;
1016 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001017 else if (c == '\'' && n == 0)
1018 {
1019 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001020 EMIT(cmp == '<' ? NFA_MARK_LT :
1021 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001022 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001023 break;
1024 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001025 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001026 syntax_error = TRUE;
1027 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1028 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001029 return FAIL;
1030 }
1031 break;
1032
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001033 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001034collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001035 /*
1036 * Glue is emitted between several atoms from the [].
1037 * It is either NFA_OR, or NFA_CONCAT.
1038 *
1039 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1040 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1041 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1042 * notation)
1043 *
1044 */
1045
1046
1047/* Emit negation atoms, if needed.
1048 * The CONCAT below merges the NOT with the previous node. */
1049#define TRY_NEG() \
1050 if (negated == TRUE) \
1051 { \
1052 EMIT(NFA_NOT); \
1053 }
1054
1055/* Emit glue between important nodes : CONCAT or OR. */
1056#define EMIT_GLUE() \
1057 if (first == FALSE) \
1058 EMIT(glue); \
1059 else \
1060 first = FALSE;
1061
1062 p = regparse;
1063 endp = skip_anyof(p);
1064 if (*endp == ']')
1065 {
1066 /*
1067 * Try to reverse engineer character classes. For example,
1068 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1069 * and perform the necessary substitutions in the NFA.
1070 */
1071 result = nfa_recognize_char_class(regparse, endp,
1072 extra == ADD_NL);
1073 if (result != FAIL)
1074 {
1075 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1076 EMIT(result);
1077 else /* must be char class + newline */
1078 {
1079 EMIT(result - ADD_NL);
1080 EMIT(NFA_NEWL);
1081 EMIT(NFA_OR);
1082 }
1083 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001084 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 return OK;
1086 }
1087 /*
1088 * Failed to recognize a character class. Use the simple
1089 * version that turns [abc] into 'a' OR 'b' OR 'c'
1090 */
1091 startc = endc = oldstartc = -1;
1092 first = TRUE; /* Emitting first atom in this sequence? */
1093 negated = FALSE;
1094 glue = NFA_OR;
1095 if (*regparse == '^') /* negated range */
1096 {
1097 negated = TRUE;
1098 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001099 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001100 }
1101 if (*regparse == '-')
1102 {
1103 startc = '-';
1104 EMIT(startc);
1105 TRY_NEG();
1106 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001107 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001108 }
1109 /* Emit the OR branches for each character in the [] */
1110 emit_range = FALSE;
1111 while (regparse < endp)
1112 {
1113 oldstartc = startc;
1114 startc = -1;
1115 got_coll_char = FALSE;
1116 if (*regparse == '[')
1117 {
1118 /* Check for [: :], [= =], [. .] */
1119 equiclass = collclass = 0;
1120 charclass = get_char_class(&regparse);
1121 if (charclass == CLASS_NONE)
1122 {
1123 equiclass = get_equi_class(&regparse);
1124 if (equiclass == 0)
1125 collclass = get_coll_element(&regparse);
1126 }
1127
1128 /* Character class like [:alpha:] */
1129 if (charclass != CLASS_NONE)
1130 {
1131 switch (charclass)
1132 {
1133 case CLASS_ALNUM:
1134 EMIT(NFA_CLASS_ALNUM);
1135 break;
1136 case CLASS_ALPHA:
1137 EMIT(NFA_CLASS_ALPHA);
1138 break;
1139 case CLASS_BLANK:
1140 EMIT(NFA_CLASS_BLANK);
1141 break;
1142 case CLASS_CNTRL:
1143 EMIT(NFA_CLASS_CNTRL);
1144 break;
1145 case CLASS_DIGIT:
1146 EMIT(NFA_CLASS_DIGIT);
1147 break;
1148 case CLASS_GRAPH:
1149 EMIT(NFA_CLASS_GRAPH);
1150 break;
1151 case CLASS_LOWER:
1152 EMIT(NFA_CLASS_LOWER);
1153 break;
1154 case CLASS_PRINT:
1155 EMIT(NFA_CLASS_PRINT);
1156 break;
1157 case CLASS_PUNCT:
1158 EMIT(NFA_CLASS_PUNCT);
1159 break;
1160 case CLASS_SPACE:
1161 EMIT(NFA_CLASS_SPACE);
1162 break;
1163 case CLASS_UPPER:
1164 EMIT(NFA_CLASS_UPPER);
1165 break;
1166 case CLASS_XDIGIT:
1167 EMIT(NFA_CLASS_XDIGIT);
1168 break;
1169 case CLASS_TAB:
1170 EMIT(NFA_CLASS_TAB);
1171 break;
1172 case CLASS_RETURN:
1173 EMIT(NFA_CLASS_RETURN);
1174 break;
1175 case CLASS_BACKSPACE:
1176 EMIT(NFA_CLASS_BACKSPACE);
1177 break;
1178 case CLASS_ESCAPE:
1179 EMIT(NFA_CLASS_ESCAPE);
1180 break;
1181 }
1182 TRY_NEG();
1183 EMIT_GLUE();
1184 continue;
1185 }
1186 /* Try equivalence class [=a=] and the like */
1187 if (equiclass != 0)
1188 {
1189 result = nfa_emit_equi_class(equiclass, negated);
1190 if (result == FAIL)
1191 {
1192 /* should never happen */
1193 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1194 }
1195 EMIT_GLUE();
1196 continue;
1197 }
1198 /* Try collating class like [. .] */
1199 if (collclass != 0)
1200 {
1201 startc = collclass; /* allow [.a.]-x as a range */
1202 /* Will emit the proper atom at the end of the
1203 * while loop. */
1204 }
1205 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001206 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1207 * start character. */
1208 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001209 {
1210 emit_range = TRUE;
1211 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001212 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 continue; /* reading the end of the range */
1214 }
1215
1216 /* Now handle simple and escaped characters.
1217 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1218 * accepts "\t", "\e", etc., but only when the 'l' flag in
1219 * 'cpoptions' is not included.
1220 * Posix doesn't recognize backslash at all.
1221 */
1222 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001223 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001224 && regparse + 1 <= endp
1225 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001226 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 && vim_strchr(REGEXP_ABBR, regparse[1])
1228 != NULL)
1229 )
1230 )
1231 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001232 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001233
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001234 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001235 startc = reg_string ? NL : NFA_NEWL;
1236 else
1237 if (*regparse == 'd'
1238 || *regparse == 'o'
1239 || *regparse == 'x'
1240 || *regparse == 'u'
1241 || *regparse == 'U'
1242 )
1243 {
1244 /* TODO(RE) This needs more testing */
1245 startc = coll_get_char();
1246 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001247 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001248 }
1249 else
1250 {
1251 /* \r,\t,\e,\b */
1252 startc = backslash_trans(*regparse);
1253 }
1254 }
1255
1256 /* Normal printable char */
1257 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001258 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259
1260 /* Previous char was '-', so this char is end of range. */
1261 if (emit_range)
1262 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001263 endc = startc;
1264 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001265 if (startc > endc)
1266 EMSG_RET_FAIL(_(e_invrange));
1267#ifdef FEAT_MBYTE
1268 if (has_mbyte && ((*mb_char2len)(startc) > 1
1269 || (*mb_char2len)(endc) > 1))
1270 {
1271 if (endc > startc + 256)
1272 EMSG_RET_FAIL(_(e_invrange));
1273 /* Emit the range. "startc" was already emitted, so
1274 * skip it. */
1275 for (c = startc + 1; c <= endc; c++)
1276 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001277 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001278 TRY_NEG();
1279 EMIT_GLUE();
1280 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001281 }
1282 else
1283#endif
1284 {
1285#ifdef EBCDIC
1286 int alpha_only = FALSE;
1287
1288 /* for alphabetical range skip the gaps
1289 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1290 if (isalpha(startc) && isalpha(endc))
1291 alpha_only = TRUE;
1292#endif
1293 /* Emit the range. "startc" was already emitted, so
1294 * skip it. */
1295 for (c = startc + 1; c <= endc; c++)
1296#ifdef EBCDIC
1297 if (!alpha_only || isalpha(startc))
1298#endif
1299 {
1300 EMIT(c);
1301 TRY_NEG();
1302 EMIT_GLUE();
1303 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001304 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001305 emit_range = FALSE;
1306 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001307 }
1308 else
1309 {
1310 /*
1311 * This char (startc) is not part of a range. Just
1312 * emit it.
1313 *
1314 * Normally, simply emit startc. But if we get char
1315 * code=0 from a collating char, then replace it with
1316 * 0x0a.
1317 *
1318 * This is needed to completely mimic the behaviour of
1319 * the backtracking engine.
1320 */
1321 if (got_coll_char == TRUE && startc == 0)
1322 EMIT(0x0a);
1323 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001324 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001325 TRY_NEG();
1326 EMIT_GLUE();
1327 }
1328
Bram Moolenaar51a29832013-05-28 22:30:35 +02001329 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001330 } /* while (p < endp) */
1331
Bram Moolenaar51a29832013-05-28 22:30:35 +02001332 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001333 if (*regparse == '-') /* if last, '-' is just a char */
1334 {
1335 EMIT('-');
1336 TRY_NEG();
1337 EMIT_GLUE();
1338 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001339 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001341 /* skip the trailing ] */
1342 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001343 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 if (negated == TRUE)
1345 {
1346 /* Mark end of negated char range */
1347 EMIT(NFA_END_NEG_RANGE);
1348 EMIT(NFA_CONCAT);
1349 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001350
1351 /* \_[] also matches \n but it's not negated */
1352 if (extra == ADD_NL)
1353 {
1354 EMIT(reg_string ? NL : NFA_NEWL);
1355 EMIT(NFA_OR);
1356 }
1357
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001359 } /* if exists closing ] */
1360
1361 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001362 {
1363 syntax_error = TRUE;
1364 EMSG_RET_FAIL(_(e_missingbracket));
1365 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001366 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001367
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001368 default:
1369 {
1370#ifdef FEAT_MBYTE
1371 int plen;
1372
1373nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001374 /* plen is length of current char with composing chars */
1375 if (enc_utf8 && ((*mb_char2len)(c)
1376 != (plen = (*mb_ptr2len)(old_regparse))
1377 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001378 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001379 int i = 0;
1380
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001381 /* A base character plus composing characters, or just one
1382 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001383 * This requires creating a separate atom as if enclosing
1384 * the characters in (), where NFA_COMPOSING is the ( and
1385 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386 * building the postfix form, not the NFA itself;
1387 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001388 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001389 for (;;)
1390 {
1391 EMIT(c);
1392 if (i > 0)
1393 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001394 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001395 break;
1396 c = utf_ptr2char(old_regparse + i);
1397 }
1398 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399 regparse = old_regparse + plen;
1400 }
1401 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001402#endif
1403 {
1404 c = no_Magic(c);
1405 EMIT(c);
1406 }
1407 return OK;
1408 }
1409 }
1410
1411#undef TRY_NEG
1412#undef EMIT_GLUE
1413
1414 return OK;
1415}
1416
1417/*
1418 * Parse something followed by possible [*+=].
1419 *
1420 * A piece is an atom, possibly followed by a multi, an indication of how many
1421 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1422 * characters: "", "a", "aa", etc.
1423 *
1424 * piece ::= atom
1425 * or atom multi
1426 */
1427 static int
1428nfa_regpiece()
1429{
1430 int i;
1431 int op;
1432 int ret;
1433 long minval, maxval;
1434 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001435 parse_state_T old_state;
1436 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001438 int old_post_pos;
1439 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440 int quest;
1441
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001442 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1443 * next. */
1444 save_parse_state(&old_state);
1445
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001446 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001447 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001448
1449 ret = nfa_regatom();
1450 if (ret == FAIL)
1451 return FAIL; /* cascaded error */
1452
1453 op = peekchr();
1454 if (re_multi_type(op) == NOT_MULTI)
1455 return OK;
1456
1457 skipchr();
1458 switch (op)
1459 {
1460 case Magic('*'):
1461 EMIT(NFA_STAR);
1462 break;
1463
1464 case Magic('+'):
1465 /*
1466 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1467 * first and only submatch would be "aaa". But the backtracking
1468 * engine interprets the plus as "try matching one more time", and
1469 * a* matches a second time at the end of the input, the empty
1470 * string.
1471 * The submatch will the empty string.
1472 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001473 * In order to be consistent with the old engine, we replace
1474 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001476 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 curchr = -1;
1478 if (nfa_regatom() == FAIL)
1479 return FAIL;
1480 EMIT(NFA_STAR);
1481 EMIT(NFA_CONCAT);
1482 skipchr(); /* skip the \+ */
1483 break;
1484
1485 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001486 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001488 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001489 switch(op)
1490 {
1491 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001492 /* \@= */
1493 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001495 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001496 /* \@! */
1497 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001498 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001500 op = no_Magic(getchr());
1501 if (op == '=')
1502 /* \@<= */
1503 i = NFA_PREV_ATOM_JUST_BEFORE;
1504 else if (op == '!')
1505 /* \@<! */
1506 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1507 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001509 /* \@> */
1510 i = NFA_PREV_ATOM_LIKE_PATTERN;
1511 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001512 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001513 if (i == 0)
1514 {
1515 syntax_error = TRUE;
1516 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1517 return FAIL;
1518 }
1519 EMIT(i);
1520 if (i == NFA_PREV_ATOM_JUST_BEFORE
1521 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1522 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001523 break;
1524
1525 case Magic('?'):
1526 case Magic('='):
1527 EMIT(NFA_QUEST);
1528 break;
1529
1530 case Magic('{'):
1531 /* a{2,5} will expand to 'aaa?a?a?'
1532 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1533 * version of '?'
1534 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1535 * parenthesis have the same id
1536 */
1537
1538 greedy = TRUE;
1539 c2 = peekchr();
1540 if (c2 == '-' || c2 == Magic('-'))
1541 {
1542 skipchr();
1543 greedy = FALSE;
1544 }
1545 if (!read_limits(&minval, &maxval))
1546 {
1547 syntax_error = TRUE;
1548 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1549 }
1550 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1551 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001552 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001554 if (greedy)
1555 /* \{}, \{0,} */
1556 EMIT(NFA_STAR);
1557 else
1558 /* \{-}, \{-0,} */
1559 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001560 break;
1561 }
1562
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 /* Special case: x{0} or x{-0} */
1564 if (maxval == 0)
1565 {
1566 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001567 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001568 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1569 EMIT(NFA_SKIP_CHAR);
1570 return OK;
1571 }
1572
1573 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001574 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001575 /* Save parse state after the repeated atom and the \{} */
1576 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001577
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001578 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1579 for (i = 0; i < maxval; i++)
1580 {
1581 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001582 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001583 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001584 if (nfa_regatom() == FAIL)
1585 return FAIL;
1586 /* after "minval" times, atoms are optional */
1587 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001588 {
1589 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001590 {
1591 if (greedy)
1592 EMIT(NFA_STAR);
1593 else
1594 EMIT(NFA_STAR_NONGREEDY);
1595 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001596 else
1597 EMIT(quest);
1598 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001599 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001600 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001601 if (i + 1 > minval && maxval == MAX_LIMIT)
1602 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001603 }
1604
1605 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001606 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001607 curchr = -1;
1608
1609 break;
1610
1611
1612 default:
1613 break;
1614 } /* end switch */
1615
1616 if (re_multi_type(peekchr()) != NOT_MULTI)
1617 {
1618 /* Can't have a multi follow a multi. */
1619 syntax_error = TRUE;
1620 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1621 }
1622
1623 return OK;
1624}
1625
1626/*
1627 * Parse one or more pieces, concatenated. It matches a match for the
1628 * first piece, followed by a match for the second piece, etc. Example:
1629 * "f[0-9]b", first matches "f", then a digit and then "b".
1630 *
1631 * concat ::= piece
1632 * or piece piece
1633 * or piece piece piece
1634 * etc.
1635 */
1636 static int
1637nfa_regconcat()
1638{
1639 int cont = TRUE;
1640 int first = TRUE;
1641
1642 while (cont)
1643 {
1644 switch (peekchr())
1645 {
1646 case NUL:
1647 case Magic('|'):
1648 case Magic('&'):
1649 case Magic(')'):
1650 cont = FALSE;
1651 break;
1652
1653 case Magic('Z'):
1654#ifdef FEAT_MBYTE
1655 regflags |= RF_ICOMBINE;
1656#endif
1657 skipchr_keepstart();
1658 break;
1659 case Magic('c'):
1660 regflags |= RF_ICASE;
1661 skipchr_keepstart();
1662 break;
1663 case Magic('C'):
1664 regflags |= RF_NOICASE;
1665 skipchr_keepstart();
1666 break;
1667 case Magic('v'):
1668 reg_magic = MAGIC_ALL;
1669 skipchr_keepstart();
1670 curchr = -1;
1671 break;
1672 case Magic('m'):
1673 reg_magic = MAGIC_ON;
1674 skipchr_keepstart();
1675 curchr = -1;
1676 break;
1677 case Magic('M'):
1678 reg_magic = MAGIC_OFF;
1679 skipchr_keepstart();
1680 curchr = -1;
1681 break;
1682 case Magic('V'):
1683 reg_magic = MAGIC_NONE;
1684 skipchr_keepstart();
1685 curchr = -1;
1686 break;
1687
1688 default:
1689 if (nfa_regpiece() == FAIL)
1690 return FAIL;
1691 if (first == FALSE)
1692 EMIT(NFA_CONCAT);
1693 else
1694 first = FALSE;
1695 break;
1696 }
1697 }
1698
1699 return OK;
1700}
1701
1702/*
1703 * Parse a branch, one or more concats, separated by "\&". It matches the
1704 * last concat, but only if all the preceding concats also match at the same
1705 * position. Examples:
1706 * "foobeep\&..." matches "foo" in "foobeep".
1707 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1708 *
1709 * branch ::= concat
1710 * or concat \& concat
1711 * or concat \& concat \& concat
1712 * etc.
1713 */
1714 static int
1715nfa_regbranch()
1716{
1717 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001718 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001719
Bram Moolenaar16299b52013-05-30 18:45:23 +02001720 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001721
1722 /* First branch, possibly the only one */
1723 if (nfa_regconcat() == FAIL)
1724 return FAIL;
1725
1726 ch = peekchr();
1727 /* Try next concats */
1728 while (ch == Magic('&'))
1729 {
1730 skipchr();
1731 EMIT(NFA_NOPEN);
1732 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001733 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001734 if (nfa_regconcat() == FAIL)
1735 return FAIL;
1736 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001737 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 EMIT(NFA_SKIP_CHAR);
1739 EMIT(NFA_CONCAT);
1740 ch = peekchr();
1741 }
1742
1743 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001744 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745 EMIT(NFA_SKIP_CHAR);
1746
1747 return OK;
1748}
1749
1750/*
1751 * Parse a pattern, one or more branches, separated by "\|". It matches
1752 * anything that matches one of the branches. Example: "foo\|beep" matches
1753 * "foo" and matches "beep". If more than one branch matches, the first one
1754 * is used.
1755 *
1756 * pattern ::= branch
1757 * or branch \| branch
1758 * or branch \| branch \| branch
1759 * etc.
1760 */
1761 static int
1762nfa_reg(paren)
1763 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1764{
1765 int parno = 0;
1766
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001767 if (paren == REG_PAREN)
1768 {
1769 if (regnpar >= NSUBEXP) /* Too many `(' */
1770 {
1771 syntax_error = TRUE;
1772 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1773 }
1774 parno = regnpar++;
1775 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001776#ifdef FEAT_SYN_HL
1777 else if (paren == REG_ZPAREN)
1778 {
1779 /* Make a ZOPEN node. */
1780 if (regnzpar >= NSUBEXP)
1781 {
1782 syntax_error = TRUE;
1783 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1784 }
1785 parno = regnzpar++;
1786 }
1787#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788
1789 if (nfa_regbranch() == FAIL)
1790 return FAIL; /* cascaded error */
1791
1792 while (peekchr() == Magic('|'))
1793 {
1794 skipchr();
1795 if (nfa_regbranch() == FAIL)
1796 return FAIL; /* cascaded error */
1797 EMIT(NFA_OR);
1798 }
1799
1800 /* Check for proper termination. */
1801 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1802 {
1803 syntax_error = TRUE;
1804 if (paren == REG_NPAREN)
1805 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1806 else
1807 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1808 }
1809 else if (paren == REG_NOPAREN && peekchr() != NUL)
1810 {
1811 syntax_error = TRUE;
1812 if (peekchr() == Magic(')'))
1813 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1814 else
1815 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1816 }
1817 /*
1818 * Here we set the flag allowing back references to this set of
1819 * parentheses.
1820 */
1821 if (paren == REG_PAREN)
1822 {
1823 had_endbrace[parno] = TRUE; /* have seen the close paren */
1824 EMIT(NFA_MOPEN + parno);
1825 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001826#ifdef FEAT_SYN_HL
1827 else if (paren == REG_ZPAREN)
1828 EMIT(NFA_ZOPEN + parno);
1829#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001830
1831 return OK;
1832}
1833
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834#ifdef DEBUG
1835static char_u code[50];
1836
1837 static void
1838nfa_set_code(c)
1839 int c;
1840{
1841 int addnl = FALSE;
1842
1843 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1844 {
1845 addnl = TRUE;
1846 c -= ADD_NL;
1847 }
1848
1849 STRCPY(code, "");
1850 switch (c)
1851 {
1852 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1853 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1854 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1855 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1856 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1857 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1858
Bram Moolenaar5714b802013-05-28 22:03:20 +02001859 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1860 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1861 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1862 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1863 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1864 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1865 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1866 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1867 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001868#ifdef FEAT_SYN_HL
1869 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1870 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1871 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1872 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1873 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1874 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1875 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1876 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1877 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1878#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001879 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1880
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881 case NFA_PREV_ATOM_NO_WIDTH:
1882 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001883 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1884 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001885 case NFA_PREV_ATOM_JUST_BEFORE:
1886 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1887 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1888 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001889 case NFA_PREV_ATOM_LIKE_PATTERN:
1890 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
1891
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001892 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1893 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001894 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001895 case NFA_START_INVISIBLE_BEFORE:
1896 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001897 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001899 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1902 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001903 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001905 case NFA_MOPEN:
1906 case NFA_MOPEN1:
1907 case NFA_MOPEN2:
1908 case NFA_MOPEN3:
1909 case NFA_MOPEN4:
1910 case NFA_MOPEN5:
1911 case NFA_MOPEN6:
1912 case NFA_MOPEN7:
1913 case NFA_MOPEN8:
1914 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 STRCPY(code, "NFA_MOPEN(x)");
1916 code[10] = c - NFA_MOPEN + '0';
1917 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001918 case NFA_MCLOSE:
1919 case NFA_MCLOSE1:
1920 case NFA_MCLOSE2:
1921 case NFA_MCLOSE3:
1922 case NFA_MCLOSE4:
1923 case NFA_MCLOSE5:
1924 case NFA_MCLOSE6:
1925 case NFA_MCLOSE7:
1926 case NFA_MCLOSE8:
1927 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 STRCPY(code, "NFA_MCLOSE(x)");
1929 code[11] = c - NFA_MCLOSE + '0';
1930 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001931#ifdef FEAT_SYN_HL
1932 case NFA_ZOPEN:
1933 case NFA_ZOPEN1:
1934 case NFA_ZOPEN2:
1935 case NFA_ZOPEN3:
1936 case NFA_ZOPEN4:
1937 case NFA_ZOPEN5:
1938 case NFA_ZOPEN6:
1939 case NFA_ZOPEN7:
1940 case NFA_ZOPEN8:
1941 case NFA_ZOPEN9:
1942 STRCPY(code, "NFA_ZOPEN(x)");
1943 code[10] = c - NFA_ZOPEN + '0';
1944 break;
1945 case NFA_ZCLOSE:
1946 case NFA_ZCLOSE1:
1947 case NFA_ZCLOSE2:
1948 case NFA_ZCLOSE3:
1949 case NFA_ZCLOSE4:
1950 case NFA_ZCLOSE5:
1951 case NFA_ZCLOSE6:
1952 case NFA_ZCLOSE7:
1953 case NFA_ZCLOSE8:
1954 case NFA_ZCLOSE9:
1955 STRCPY(code, "NFA_ZCLOSE(x)");
1956 code[11] = c - NFA_ZCLOSE + '0';
1957 break;
1958#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001959 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1960 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1961 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1962 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001963 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1964 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001965 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1966 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1967 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1968 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1969 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1970 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1971 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1972 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1973 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1974 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1975 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1976 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1977 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
1978 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
1979
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001980 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001981 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1982 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1983 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001984 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1985 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1986 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001987 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1988 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1989 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1990 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1991 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1992 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1993 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1994 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1995 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1996 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1997 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1998 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1999 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2000 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2001 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2002 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2003 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2004
2005 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2006 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2007 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2008 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2009 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2010 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2011 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2012 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2013 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2014 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2015 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2016 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2017 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2018 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2019 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2020 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2021 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2022 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2023 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2024 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2025 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2026 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2027 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2028 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2029 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2030 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2031 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2032
2033 default:
2034 STRCPY(code, "CHAR(x)");
2035 code[5] = c;
2036 }
2037
2038 if (addnl == TRUE)
2039 STRCAT(code, " + NEWLINE ");
2040
2041}
2042
2043#ifdef ENABLE_LOG
2044static FILE *log_fd;
2045
2046/*
2047 * Print the postfix notation of the current regexp.
2048 */
2049 static void
2050nfa_postfix_dump(expr, retval)
2051 char_u *expr;
2052 int retval;
2053{
2054 int *p;
2055 FILE *f;
2056
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002057 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 if (f != NULL)
2059 {
2060 fprintf(f, "\n-------------------------\n");
2061 if (retval == FAIL)
2062 fprintf(f, ">>> NFA engine failed ... \n");
2063 else if (retval == OK)
2064 fprintf(f, ">>> NFA engine succeeded !\n");
2065 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002066 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 {
2068 nfa_set_code(*p);
2069 fprintf(f, "%s, ", code);
2070 }
2071 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002072 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073 fprintf(f, "%d ", *p);
2074 fprintf(f, "\n\n");
2075 fclose(f);
2076 }
2077}
2078
2079/*
2080 * Print the NFA starting with a root node "state".
2081 */
2082 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002083nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002084 FILE *debugf;
2085 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002086{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002087 garray_T indent;
2088
2089 ga_init2(&indent, 1, 64);
2090 ga_append(&indent, '\0');
2091 nfa_print_state2(debugf, state, &indent);
2092 ga_clear(&indent);
2093}
2094
2095 static void
2096nfa_print_state2(debugf, state, indent)
2097 FILE *debugf;
2098 nfa_state_T *state;
2099 garray_T *indent;
2100{
2101 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002102
2103 if (state == NULL)
2104 return;
2105
2106 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002107
2108 /* Output indent */
2109 p = (char_u *)indent->ga_data;
2110 if (indent->ga_len >= 3)
2111 {
2112 int last = indent->ga_len - 3;
2113 char_u save[2];
2114
2115 STRNCPY(save, &p[last], 2);
2116 STRNCPY(&p[last], "+-", 2);
2117 fprintf(debugf, " %s", p);
2118 STRNCPY(&p[last], save, 2);
2119 }
2120 else
2121 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122
2123 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002124 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2125 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002126 if (state->id < 0)
2127 return;
2128
2129 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002130
2131 /* grow indent for state->out */
2132 indent->ga_len -= 1;
2133 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002134 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002135 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002136 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002137 ga_append(indent, '\0');
2138
2139 nfa_print_state2(debugf, state->out, indent);
2140
2141 /* replace last part of indent for state->out1 */
2142 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002143 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002144 ga_append(indent, '\0');
2145
2146 nfa_print_state2(debugf, state->out1, indent);
2147
2148 /* shrink indent */
2149 indent->ga_len -= 3;
2150 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002151}
2152
2153/*
2154 * Print the NFA state machine.
2155 */
2156 static void
2157nfa_dump(prog)
2158 nfa_regprog_T *prog;
2159{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002160 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002161
2162 if (debugf != NULL)
2163 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002164 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 fclose(debugf);
2166 }
2167}
2168#endif /* ENABLE_LOG */
2169#endif /* DEBUG */
2170
2171/*
2172 * Parse r.e. @expr and convert it into postfix form.
2173 * Return the postfix string on success, NULL otherwise.
2174 */
2175 static int *
2176re2post()
2177{
2178 if (nfa_reg(REG_NOPAREN) == FAIL)
2179 return NULL;
2180 EMIT(NFA_MOPEN);
2181 return post_start;
2182}
2183
2184/* NB. Some of the code below is inspired by Russ's. */
2185
2186/*
2187 * Represents an NFA state plus zero or one or two arrows exiting.
2188 * if c == MATCH, no arrows out; matching state.
2189 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2190 * If c < 256, labeled arrow with character c to out.
2191 */
2192
2193static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2194
2195/*
2196 * Allocate and initialize nfa_state_T.
2197 */
2198 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002199alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002200 int c;
2201 nfa_state_T *out;
2202 nfa_state_T *out1;
2203{
2204 nfa_state_T *s;
2205
2206 if (istate >= nstate)
2207 return NULL;
2208
2209 s = &state_ptr[istate++];
2210
2211 s->c = c;
2212 s->out = out;
2213 s->out1 = out1;
2214
2215 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002216 s->lastlist[0] = 0;
2217 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 s->negated = FALSE;
2219
2220 return s;
2221}
2222
2223/*
2224 * A partially built NFA without the matching state filled in.
2225 * Frag_T.start points at the start state.
2226 * Frag_T.out is a list of places that need to be set to the
2227 * next state for this fragment.
2228 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002229
2230/* Since the out pointers in the list are always
2231 * uninitialized, we use the pointers themselves
2232 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002234union Ptrlist
2235{
2236 Ptrlist *next;
2237 nfa_state_T *s;
2238};
2239
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002240struct Frag
2241{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002242 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 Ptrlist *out;
2244};
2245typedef struct Frag Frag_T;
2246
2247static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2248static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2249static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2250static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2251static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2252static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2253
2254/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002255 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002256 */
2257 static Frag_T
2258frag(start, out)
2259 nfa_state_T *start;
2260 Ptrlist *out;
2261{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002262 Frag_T n;
2263
2264 n.start = start;
2265 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 return n;
2267}
2268
2269/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002270 * Create singleton list containing just outp.
2271 */
2272 static Ptrlist *
2273list1(outp)
2274 nfa_state_T **outp;
2275{
2276 Ptrlist *l;
2277
2278 l = (Ptrlist *)outp;
2279 l->next = NULL;
2280 return l;
2281}
2282
2283/*
2284 * Patch the list of states at out to point to start.
2285 */
2286 static void
2287patch(l, s)
2288 Ptrlist *l;
2289 nfa_state_T *s;
2290{
2291 Ptrlist *next;
2292
2293 for (; l; l = next)
2294 {
2295 next = l->next;
2296 l->s = s;
2297 }
2298}
2299
2300
2301/*
2302 * Join the two lists l1 and l2, returning the combination.
2303 */
2304 static Ptrlist *
2305append(l1, l2)
2306 Ptrlist *l1;
2307 Ptrlist *l2;
2308{
2309 Ptrlist *oldl1;
2310
2311 oldl1 = l1;
2312 while (l1->next)
2313 l1 = l1->next;
2314 l1->next = l2;
2315 return oldl1;
2316}
2317
2318/*
2319 * Stack used for transforming postfix form into NFA.
2320 */
2321static Frag_T empty;
2322
2323 static void
2324st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002325 int *postfix UNUSED;
2326 int *end UNUSED;
2327 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002329#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330 FILE *df;
2331 int *p2;
2332
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002333 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 if (df)
2335 {
2336 fprintf(df, "Error popping the stack!\n");
2337#ifdef DEBUG
2338 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2339#endif
2340 fprintf(df, "Postfix form is: ");
2341#ifdef DEBUG
2342 for (p2 = postfix; p2 < end; p2++)
2343 {
2344 nfa_set_code(*p2);
2345 fprintf(df, "%s, ", code);
2346 }
2347 nfa_set_code(*p);
2348 fprintf(df, "\nCurrent position is: ");
2349 for (p2 = postfix; p2 <= p; p2 ++)
2350 {
2351 nfa_set_code(*p2);
2352 fprintf(df, "%s, ", code);
2353 }
2354#else
2355 for (p2 = postfix; p2 < end; p2++)
2356 {
2357 fprintf(df, "%d, ", *p2);
2358 }
2359 fprintf(df, "\nCurrent position is: ");
2360 for (p2 = postfix; p2 <= p; p2 ++)
2361 {
2362 fprintf(df, "%d, ", *p2);
2363 }
2364#endif
2365 fprintf(df, "\n--------------------------\n");
2366 fclose(df);
2367 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002368#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369 EMSG(_("E874: (NFA) Could not pop the stack !"));
2370}
2371
2372/*
2373 * Push an item onto the stack.
2374 */
2375 static void
2376st_push(s, p, stack_end)
2377 Frag_T s;
2378 Frag_T **p;
2379 Frag_T *stack_end;
2380{
2381 Frag_T *stackp = *p;
2382
2383 if (stackp >= stack_end)
2384 return;
2385 *stackp = s;
2386 *p = *p + 1;
2387}
2388
2389/*
2390 * Pop an item from the stack.
2391 */
2392 static Frag_T
2393st_pop(p, stack)
2394 Frag_T **p;
2395 Frag_T *stack;
2396{
2397 Frag_T *stackp;
2398
2399 *p = *p - 1;
2400 stackp = *p;
2401 if (stackp < stack)
2402 return empty;
2403 return **p;
2404}
2405
2406/*
2407 * Convert a postfix form into its equivalent NFA.
2408 * Return the NFA start state on success, NULL otherwise.
2409 */
2410 static nfa_state_T *
2411post2nfa(postfix, end, nfa_calc_size)
2412 int *postfix;
2413 int *end;
2414 int nfa_calc_size;
2415{
2416 int *p;
2417 int mopen;
2418 int mclose;
2419 Frag_T *stack = NULL;
2420 Frag_T *stackp = NULL;
2421 Frag_T *stack_end = NULL;
2422 Frag_T e1;
2423 Frag_T e2;
2424 Frag_T e;
2425 nfa_state_T *s;
2426 nfa_state_T *s1;
2427 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002428 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002429
2430 if (postfix == NULL)
2431 return NULL;
2432
Bram Moolenaar053bb602013-05-20 13:55:21 +02002433#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002434#define POP() st_pop(&stackp, stack); \
2435 if (stackp < stack) \
2436 { \
2437 st_error(postfix, end, p); \
2438 return NULL; \
2439 }
2440
2441 if (nfa_calc_size == FALSE)
2442 {
2443 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002444 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002446 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447 }
2448
2449 for (p = postfix; p < end; ++p)
2450 {
2451 switch (*p)
2452 {
2453 case NFA_CONCAT:
2454 /* Catenation.
2455 * Pay attention: this operator does not exist
2456 * in the r.e. itself (it is implicit, really).
2457 * It is added when r.e. is translated to postfix
2458 * form in re2post().
2459 *
2460 * No new state added here. */
2461 if (nfa_calc_size == TRUE)
2462 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002463 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002464 break;
2465 }
2466 e2 = POP();
2467 e1 = POP();
2468 patch(e1.out, e2.start);
2469 PUSH(frag(e1.start, e2.out));
2470 break;
2471
2472 case NFA_NOT:
2473 /* Negation of a character */
2474 if (nfa_calc_size == TRUE)
2475 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002476 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002477 break;
2478 }
2479 e1 = POP();
2480 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002481#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002482 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002483 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002484#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002485 PUSH(e1);
2486 break;
2487
2488 case NFA_OR:
2489 /* Alternation */
2490 if (nfa_calc_size == TRUE)
2491 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002492 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002493 break;
2494 }
2495 e2 = POP();
2496 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002497 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002498 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002499 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002500 PUSH(frag(s, append(e1.out, e2.out)));
2501 break;
2502
2503 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002504 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505 if (nfa_calc_size == TRUE)
2506 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002507 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002508 break;
2509 }
2510 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002511 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002512 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002513 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002514 patch(e.out, s);
2515 PUSH(frag(s, list1(&s->out1)));
2516 break;
2517
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002518 case NFA_STAR_NONGREEDY:
2519 /* Zero or more, prefer zero */
2520 if (nfa_calc_size == TRUE)
2521 {
2522 nstate++;
2523 break;
2524 }
2525 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002526 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002527 if (s == NULL)
2528 goto theend;
2529 patch(e.out, s);
2530 PUSH(frag(s, list1(&s->out)));
2531 break;
2532
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002533 case NFA_QUEST:
2534 /* one or zero atoms=> greedy match */
2535 if (nfa_calc_size == TRUE)
2536 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002537 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538 break;
2539 }
2540 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002541 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002542 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002543 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544 PUSH(frag(s, append(e.out, list1(&s->out1))));
2545 break;
2546
2547 case NFA_QUEST_NONGREEDY:
2548 /* zero or one atoms => non-greedy match */
2549 if (nfa_calc_size == TRUE)
2550 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002551 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002552 break;
2553 }
2554 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002555 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002556 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002557 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 PUSH(frag(s, append(e.out, list1(&s->out))));
2559 break;
2560
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002561 case NFA_SKIP_CHAR:
2562 /* Symbol of 0-length, Used in a repetition
2563 * with max/min count of 0 */
2564 if (nfa_calc_size == TRUE)
2565 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002566 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002567 break;
2568 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002569 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002570 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002571 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002572 PUSH(frag(s, list1(&s->out)));
2573 break;
2574
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002575 case NFA_OPT_CHARS:
2576 {
2577 int n;
2578
2579 /* \%[abc] */
2580 n = *++p; /* get number of characters */
2581 if (nfa_calc_size == TRUE)
2582 {
2583 nstate += n;
2584 break;
2585 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002586 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002587 e1.out = NULL; /* stores list with out1's */
2588 s1 = NULL; /* previous NFA_SPLIT to connect to */
2589 while (n-- > 0)
2590 {
2591 e = POP(); /* get character */
2592 s = alloc_state(NFA_SPLIT, e.start, NULL);
2593 if (s == NULL)
2594 goto theend;
2595 if (e1.out == NULL)
2596 e1 = e;
2597 patch(e.out, s1);
2598 append(e1.out, list1(&s->out1));
2599 s1 = s;
2600 }
2601 PUSH(frag(s, e1.out));
2602 break;
2603 }
2604
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002605 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002606 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002607 case NFA_PREV_ATOM_JUST_BEFORE:
2608 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002609 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002610 {
2611 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2612 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2613 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2614 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002615 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
2616 int start_state = NFA_START_INVISIBLE;
2617 int end_state = NFA_END_INVISIBLE;
2618 int n = 0;
2619 nfa_state_T *zend;
2620 nfa_state_T *skip;
2621
2622 if (before)
2623 start_state = NFA_START_INVISIBLE_BEFORE;
2624 else if (pattern)
2625 {
2626 start_state = NFA_START_PATTERN;
2627 end_state = NFA_END_PATTERN;
2628 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002629
2630 if (before)
2631 n = *++p; /* get the count */
2632
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002633 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002634 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002635 * The \@<= operator: match for the preceding atom.
2636 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002637 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002638 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002639
2640 if (nfa_calc_size == TRUE)
2641 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002642 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 break;
2644 }
2645 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002646 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002648 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649
Bram Moolenaar87953742013-06-05 18:52:40 +02002650 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002651 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002652 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002653 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002654 {
2655 s->negated = TRUE;
2656 s1->negated = TRUE;
2657 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002658 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002659 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002660 if (pattern)
2661 {
2662 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2663 skip = alloc_state(NFA_SKIP, NULL, NULL);
2664 zend = alloc_state(NFA_ZEND, s1, NULL);
2665 s1->out= skip;
2666 patch(e.out, zend);
2667 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002668 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002669 else
2670 {
2671 patch(e.out, s1);
2672 PUSH(frag(s, list1(&s1->out)));
2673 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002675 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002676
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002677#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002678 case NFA_COMPOSING: /* char with composing char */
2679#if 0
2680 /* TODO */
2681 if (regflags & RF_ICOMBINE)
2682 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002683 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002684 }
2685#endif
2686 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002687#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002688
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002689 case NFA_MOPEN: /* \( \) Submatch */
2690 case NFA_MOPEN1:
2691 case NFA_MOPEN2:
2692 case NFA_MOPEN3:
2693 case NFA_MOPEN4:
2694 case NFA_MOPEN5:
2695 case NFA_MOPEN6:
2696 case NFA_MOPEN7:
2697 case NFA_MOPEN8:
2698 case NFA_MOPEN9:
2699#ifdef FEAT_SYN_HL
2700 case NFA_ZOPEN: /* \z( \) Submatch */
2701 case NFA_ZOPEN1:
2702 case NFA_ZOPEN2:
2703 case NFA_ZOPEN3:
2704 case NFA_ZOPEN4:
2705 case NFA_ZOPEN5:
2706 case NFA_ZOPEN6:
2707 case NFA_ZOPEN7:
2708 case NFA_ZOPEN8:
2709 case NFA_ZOPEN9:
2710#endif
2711 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712 if (nfa_calc_size == TRUE)
2713 {
2714 nstate += 2;
2715 break;
2716 }
2717
2718 mopen = *p;
2719 switch (*p)
2720 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002721 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2722#ifdef FEAT_SYN_HL
2723 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2724 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2725 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2726 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2727 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2728 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2729 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2730 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2731 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2732 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2733#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002734#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002735 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002736#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002737 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002738 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739 mclose = *p + NSUBEXP;
2740 break;
2741 }
2742
2743 /* Allow "NFA_MOPEN" as a valid postfix representation for
2744 * the empty regexp "". In this case, the NFA will be
2745 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2746 * empty groups of parenthesis, and empty mbyte chars */
2747 if (stackp == stack)
2748 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002749 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002750 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002751 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002752 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002753 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002754 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002755 patch(list1(&s->out), s1);
2756 PUSH(frag(s, list1(&s1->out)));
2757 break;
2758 }
2759
2760 /* At least one node was emitted before NFA_MOPEN, so
2761 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2762 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002763 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002764 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002765 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002766
Bram Moolenaar525666f2013-06-02 16:40:55 +02002767 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002768 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002769 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002770 patch(e.out, s1);
2771
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002772#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002773 if (mopen == NFA_COMPOSING)
2774 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002775 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002776#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002777
2778 PUSH(frag(s, list1(&s1->out)));
2779 break;
2780
Bram Moolenaar5714b802013-05-28 22:03:20 +02002781 case NFA_BACKREF1:
2782 case NFA_BACKREF2:
2783 case NFA_BACKREF3:
2784 case NFA_BACKREF4:
2785 case NFA_BACKREF5:
2786 case NFA_BACKREF6:
2787 case NFA_BACKREF7:
2788 case NFA_BACKREF8:
2789 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002790#ifdef FEAT_SYN_HL
2791 case NFA_ZREF1:
2792 case NFA_ZREF2:
2793 case NFA_ZREF3:
2794 case NFA_ZREF4:
2795 case NFA_ZREF5:
2796 case NFA_ZREF6:
2797 case NFA_ZREF7:
2798 case NFA_ZREF8:
2799 case NFA_ZREF9:
2800#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002801 if (nfa_calc_size == TRUE)
2802 {
2803 nstate += 2;
2804 break;
2805 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002806 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002807 if (s == NULL)
2808 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002809 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002810 if (s1 == NULL)
2811 goto theend;
2812 patch(list1(&s->out), s1);
2813 PUSH(frag(s, list1(&s1->out)));
2814 break;
2815
Bram Moolenaar423532e2013-05-29 21:14:42 +02002816 case NFA_LNUM:
2817 case NFA_LNUM_GT:
2818 case NFA_LNUM_LT:
2819 case NFA_VCOL:
2820 case NFA_VCOL_GT:
2821 case NFA_VCOL_LT:
2822 case NFA_COL:
2823 case NFA_COL_GT:
2824 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002825 case NFA_MARK:
2826 case NFA_MARK_GT:
2827 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002828 {
2829 int n = *++p; /* lnum, col or mark name */
2830
Bram Moolenaar423532e2013-05-29 21:14:42 +02002831 if (nfa_calc_size == TRUE)
2832 {
2833 nstate += 1;
2834 break;
2835 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002836 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002837 if (s == NULL)
2838 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002839 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002840 PUSH(frag(s, list1(&s->out)));
2841 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002842 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002843
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002844 case NFA_ZSTART:
2845 case NFA_ZEND:
2846 default:
2847 /* Operands */
2848 if (nfa_calc_size == TRUE)
2849 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002850 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851 break;
2852 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002853 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002855 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002856 PUSH(frag(s, list1(&s->out)));
2857 break;
2858
2859 } /* switch(*p) */
2860
2861 } /* for(p = postfix; *p; ++p) */
2862
2863 if (nfa_calc_size == TRUE)
2864 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002865 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002866 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002867 }
2868
2869 e = POP();
2870 if (stackp != stack)
2871 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2872
2873 if (istate >= nstate)
2874 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2875
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876 matchstate = &state_ptr[istate++]; /* the match state */
2877 matchstate->c = NFA_MATCH;
2878 matchstate->out = matchstate->out1 = NULL;
2879
2880 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002881 ret = e.start;
2882
2883theend:
2884 vim_free(stack);
2885 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886
2887#undef POP1
2888#undef PUSH1
2889#undef POP2
2890#undef PUSH2
2891#undef POP
2892#undef PUSH
2893}
2894
2895/****************************************************************
2896 * NFA execution code.
2897 ****************************************************************/
2898
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002899typedef struct
2900{
2901 int in_use; /* number of subexpr with useful info */
2902
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002903 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002904 union
2905 {
2906 struct multipos
2907 {
2908 lpos_T start;
2909 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002910 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002911 struct linepos
2912 {
2913 char_u *start;
2914 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002915 } line[NSUBEXP];
2916 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002917} regsub_T;
2918
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002919typedef struct
2920{
2921 regsub_T norm; /* \( .. \) matches */
2922#ifdef FEAT_SYN_HL
2923 regsub_T synt; /* \z( .. \) matches */
2924#endif
2925} regsubs_T;
2926
Bram Moolenaara2d95102013-06-04 14:23:05 +02002927/* nfa_pim_T stores a Postponed Invisible Match. */
2928typedef struct nfa_pim_S nfa_pim_T;
2929struct nfa_pim_S
2930{
2931 nfa_state_T *state;
2932 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2933 nfa_pim_T *pim; /* another PIM at the same position */
2934 regsubs_T subs; /* submatch info, only party used */
2935};
2936
2937/* Values for done in nfa_pim_T. */
2938#define NFA_PIM_TODO 0
2939#define NFA_PIM_MATCH 1
2940#define NFA_PIM_NOMATCH -1
2941
2942
Bram Moolenaar963fee22013-05-26 21:47:28 +02002943/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002944typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945{
2946 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002947 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002948 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002949 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002950} nfa_thread_T;
2951
Bram Moolenaar963fee22013-05-26 21:47:28 +02002952/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953typedef struct
2954{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002955 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002956 int n; /* nr of states currently in "t" */
2957 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002958 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002959} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002960
Bram Moolenaar5714b802013-05-28 22:03:20 +02002961#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002962static void log_subsexpr __ARGS((regsubs_T *subs));
2963static void log_subexpr __ARGS((regsub_T *sub));
2964
2965 static void
2966log_subsexpr(subs)
2967 regsubs_T *subs;
2968{
2969 log_subexpr(&subs->norm);
2970# ifdef FEAT_SYN_HL
2971 log_subexpr(&subs->synt);
2972# endif
2973}
2974
Bram Moolenaar5714b802013-05-28 22:03:20 +02002975 static void
2976log_subexpr(sub)
2977 regsub_T *sub;
2978{
2979 int j;
2980
2981 for (j = 0; j < sub->in_use; j++)
2982 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02002983 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002984 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002985 sub->list.multi[j].start.col,
2986 (int)sub->list.multi[j].start.lnum,
2987 sub->list.multi[j].end.col,
2988 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002989 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002990 {
2991 char *s = (char *)sub->list.line[j].start;
2992 char *e = (char *)sub->list.line[j].end;
2993
Bram Moolenaar87953742013-06-05 18:52:40 +02002994 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002995 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002996 s == NULL ? "NULL" : s,
2997 e == NULL ? "NULL" : e);
2998 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002999}
3000#endif
3001
Bram Moolenaar963fee22013-05-26 21:47:28 +02003002/* Used during execution: whether a match has been found. */
3003static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003004
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003005static void clear_sub __ARGS((regsub_T *sub));
3006static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3007static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003008static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003009static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003010static 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 +02003011
3012 static void
3013clear_sub(sub)
3014 regsub_T *sub;
3015{
3016 if (REG_MULTI)
3017 /* Use 0xff to set lnum to -1 */
3018 vim_memset(sub->list.multi, 0xff,
3019 sizeof(struct multipos) * nfa_nsubexpr);
3020 else
3021 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3022 sub->in_use = 0;
3023}
3024
3025/*
3026 * Copy the submatches from "from" to "to".
3027 */
3028 static void
3029copy_sub(to, from)
3030 regsub_T *to;
3031 regsub_T *from;
3032{
3033 to->in_use = from->in_use;
3034 if (from->in_use > 0)
3035 {
3036 /* Copy the match start and end positions. */
3037 if (REG_MULTI)
3038 mch_memmove(&to->list.multi[0],
3039 &from->list.multi[0],
3040 sizeof(struct multipos) * from->in_use);
3041 else
3042 mch_memmove(&to->list.line[0],
3043 &from->list.line[0],
3044 sizeof(struct linepos) * from->in_use);
3045 }
3046}
3047
3048/*
3049 * Like copy_sub() but exclude the main match.
3050 */
3051 static void
3052copy_sub_off(to, from)
3053 regsub_T *to;
3054 regsub_T *from;
3055{
3056 if (to->in_use < from->in_use)
3057 to->in_use = from->in_use;
3058 if (from->in_use > 1)
3059 {
3060 /* Copy the match start and end positions. */
3061 if (REG_MULTI)
3062 mch_memmove(&to->list.multi[1],
3063 &from->list.multi[1],
3064 sizeof(struct multipos) * (from->in_use - 1));
3065 else
3066 mch_memmove(&to->list.line[1],
3067 &from->list.line[1],
3068 sizeof(struct linepos) * (from->in_use - 1));
3069 }
3070}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003071
Bram Moolenaar428e9872013-05-30 17:05:39 +02003072/*
3073 * Return TRUE if "sub1" and "sub2" have the same positions.
3074 */
3075 static int
3076sub_equal(sub1, sub2)
3077 regsub_T *sub1;
3078 regsub_T *sub2;
3079{
3080 int i;
3081 int todo;
3082 linenr_T s1, e1;
3083 linenr_T s2, e2;
3084 char_u *sp1, *ep1;
3085 char_u *sp2, *ep2;
3086
3087 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3088 if (REG_MULTI)
3089 {
3090 for (i = 0; i < todo; ++i)
3091 {
3092 if (i < sub1->in_use)
3093 {
3094 s1 = sub1->list.multi[i].start.lnum;
3095 e1 = sub1->list.multi[i].end.lnum;
3096 }
3097 else
3098 {
3099 s1 = 0;
3100 e1 = 0;
3101 }
3102 if (i < sub2->in_use)
3103 {
3104 s2 = sub2->list.multi[i].start.lnum;
3105 e2 = sub2->list.multi[i].end.lnum;
3106 }
3107 else
3108 {
3109 s2 = 0;
3110 e2 = 0;
3111 }
3112 if (s1 != s2 || e1 != e2)
3113 return FALSE;
3114 if (s1 != 0 && sub1->list.multi[i].start.col
3115 != sub2->list.multi[i].start.col)
3116 return FALSE;
3117 if (e1 != 0 && sub1->list.multi[i].end.col
3118 != sub2->list.multi[i].end.col)
3119 return FALSE;
3120 }
3121 }
3122 else
3123 {
3124 for (i = 0; i < todo; ++i)
3125 {
3126 if (i < sub1->in_use)
3127 {
3128 sp1 = sub1->list.line[i].start;
3129 ep1 = sub1->list.line[i].end;
3130 }
3131 else
3132 {
3133 sp1 = NULL;
3134 ep1 = NULL;
3135 }
3136 if (i < sub2->in_use)
3137 {
3138 sp2 = sub2->list.line[i].start;
3139 ep2 = sub2->list.line[i].end;
3140 }
3141 else
3142 {
3143 sp2 = NULL;
3144 ep2 = NULL;
3145 }
3146 if (sp1 != sp2 || ep1 != ep2)
3147 return FALSE;
3148 }
3149 }
3150
3151 return TRUE;
3152}
3153
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003154#ifdef ENABLE_LOG
3155 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003156report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003157{
3158 int col;
3159
3160 if (sub->in_use <= 0)
3161 col = -1;
3162 else if (REG_MULTI)
3163 col = sub->list.multi[0].start.col;
3164 else
3165 col = (int)(sub->list.line[0].start - regline);
3166 nfa_set_code(state->c);
3167 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3168 action, abs(state->id), lid, state->c, code, col);
3169}
3170#endif
3171
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003172 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003173addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003174 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003175 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003176 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003177 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003178{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003179 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003180 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003181 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003182 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003183 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003184 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003185 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003186#ifdef ENABLE_LOG
3187 int did_print = FALSE;
3188#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189
3190 if (l == NULL || state == NULL)
3191 return;
3192
3193 switch (state->c)
3194 {
3195 case NFA_SPLIT:
3196 case NFA_NOT:
3197 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003198 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003199 case NFA_NCLOSE:
3200 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003201 case NFA_MCLOSE1:
3202 case NFA_MCLOSE2:
3203 case NFA_MCLOSE3:
3204 case NFA_MCLOSE4:
3205 case NFA_MCLOSE5:
3206 case NFA_MCLOSE6:
3207 case NFA_MCLOSE7:
3208 case NFA_MCLOSE8:
3209 case NFA_MCLOSE9:
3210#ifdef FEAT_SYN_HL
3211 case NFA_ZCLOSE:
3212 case NFA_ZCLOSE1:
3213 case NFA_ZCLOSE2:
3214 case NFA_ZCLOSE3:
3215 case NFA_ZCLOSE4:
3216 case NFA_ZCLOSE5:
3217 case NFA_ZCLOSE6:
3218 case NFA_ZCLOSE7:
3219 case NFA_ZCLOSE8:
3220 case NFA_ZCLOSE9:
3221#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003222 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003223 /* These nodes are not added themselves but their "out" and/or
3224 * "out1" may be added below. */
3225 break;
3226
3227 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003228 case NFA_MOPEN1:
3229 case NFA_MOPEN2:
3230 case NFA_MOPEN3:
3231 case NFA_MOPEN4:
3232 case NFA_MOPEN5:
3233 case NFA_MOPEN6:
3234 case NFA_MOPEN7:
3235 case NFA_MOPEN8:
3236 case NFA_MOPEN9:
3237#ifdef FEAT_SYN_HL
3238 case NFA_ZOPEN:
3239 case NFA_ZOPEN1:
3240 case NFA_ZOPEN2:
3241 case NFA_ZOPEN3:
3242 case NFA_ZOPEN4:
3243 case NFA_ZOPEN5:
3244 case NFA_ZOPEN6:
3245 case NFA_ZOPEN7:
3246 case NFA_ZOPEN8:
3247 case NFA_ZOPEN9:
3248#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003249 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003250 /* These nodes do not need to be added, but we need to bail out
3251 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003252 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003253 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003254 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003255 break;
3256
Bram Moolenaar307aa162013-06-02 16:34:21 +02003257 case NFA_BOL:
3258 case NFA_BOF:
3259 /* "^" won't match past end-of-line, don't bother trying.
3260 * Except when we are going to the next line for a look-behind
3261 * match. */
3262 if (reginput > regline
3263 && (nfa_endp == NULL
3264 || !REG_MULTI
3265 || reglnum == nfa_endp->se_u.pos.lnum))
3266 goto skip_add;
3267 /* FALLTHROUGH */
3268
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003270 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003272 /* This state is already in the list, don't add it again,
3273 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003274 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003275 {
3276skip_add:
3277#ifdef ENABLE_LOG
3278 nfa_set_code(state->c);
3279 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3280 abs(state->id), l->id, state->c, code);
3281#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003282 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003283 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003284
3285 /* See if the same state is already in the list with the same
3286 * positions. */
3287 for (i = 0; i < l->n; ++i)
3288 {
3289 thread = &l->t[i];
3290 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003291 && sub_equal(&thread->subs.norm, &subs->norm)
3292#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003293 && (!nfa_has_zsubexpr ||
3294 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003295#endif
3296 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003297 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003298 }
3299 }
3300
Bram Moolenaara2d95102013-06-04 14:23:05 +02003301 /* when there are backreferences or look-behind matches the number
3302 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003303 if (nfa_has_backref && l->n == l->len)
3304 {
3305 int newlen = l->len * 3 / 2 + 50;
3306
3307 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3308 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003309 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003310
3311 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003312 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003313 thread = &l->t[l->n++];
3314 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003315 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003316 copy_sub(&thread->subs.norm, &subs->norm);
3317#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003318 if (nfa_has_zsubexpr)
3319 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003320#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003321#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003322 report_state("Adding", &thread->subs.norm, state, l->id);
3323 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003324#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003325 }
3326
3327#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003328 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003329 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003330#endif
3331 switch (state->c)
3332 {
3333 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003334 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003335 break;
3336
3337 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003338 /* order matters here */
3339 addstate(l, state->out, subs, off);
3340 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003341 break;
3342
Bram Moolenaar5714b802013-05-28 22:03:20 +02003343 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003344 case NFA_NOPEN:
3345 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003346 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003347 break;
3348
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003349 case NFA_MOPEN:
3350 case NFA_MOPEN1:
3351 case NFA_MOPEN2:
3352 case NFA_MOPEN3:
3353 case NFA_MOPEN4:
3354 case NFA_MOPEN5:
3355 case NFA_MOPEN6:
3356 case NFA_MOPEN7:
3357 case NFA_MOPEN8:
3358 case NFA_MOPEN9:
3359#ifdef FEAT_SYN_HL
3360 case NFA_ZOPEN:
3361 case NFA_ZOPEN1:
3362 case NFA_ZOPEN2:
3363 case NFA_ZOPEN3:
3364 case NFA_ZOPEN4:
3365 case NFA_ZOPEN5:
3366 case NFA_ZOPEN6:
3367 case NFA_ZOPEN7:
3368 case NFA_ZOPEN8:
3369 case NFA_ZOPEN9:
3370#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003373 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003374 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003375 sub = &subs->norm;
3376 }
3377#ifdef FEAT_SYN_HL
3378 else if (state->c >= NFA_ZOPEN)
3379 {
3380 subidx = state->c - NFA_ZOPEN;
3381 sub = &subs->synt;
3382 }
3383#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003384 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003385 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003386 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003387 sub = &subs->norm;
3388 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003389
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003390 /* Set the position (with "off") in the subexpression. Save and
3391 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003392 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003393 if (REG_MULTI)
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_lpos = sub->list.multi[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.multi[i].start.lnum = -1;
3406 sub->list.multi[i].end.lnum = -1;
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 Moolenaar35b23862013-05-22 23:00:40 +02003410 if (off == -1)
3411 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003412 sub->list.multi[subidx].start.lnum = reglnum + 1;
3413 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003414 }
3415 else
3416 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003417 sub->list.multi[subidx].start.lnum = reglnum;
3418 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003419 (colnr_T)(reginput - regline + off);
3420 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003421 }
3422 else
3423 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003424 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003425 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003426 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003427 save_in_use = -1;
3428 }
3429 else
3430 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003431 save_in_use = sub->in_use;
3432 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003433 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003434 sub->list.line[i].start = NULL;
3435 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003436 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003437 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003438 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003439 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003440 }
3441
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003442 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003443
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003444 if (save_in_use == -1)
3445 {
3446 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003447 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003448 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003449 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003450 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003451 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003452 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003453 break;
3454
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003455 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003456 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003457 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003458 /* Do not overwrite the position set by \ze. If no \ze
3459 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003460 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461 break;
3462 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003463 case NFA_MCLOSE1:
3464 case NFA_MCLOSE2:
3465 case NFA_MCLOSE3:
3466 case NFA_MCLOSE4:
3467 case NFA_MCLOSE5:
3468 case NFA_MCLOSE6:
3469 case NFA_MCLOSE7:
3470 case NFA_MCLOSE8:
3471 case NFA_MCLOSE9:
3472#ifdef FEAT_SYN_HL
3473 case NFA_ZCLOSE:
3474 case NFA_ZCLOSE1:
3475 case NFA_ZCLOSE2:
3476 case NFA_ZCLOSE3:
3477 case NFA_ZCLOSE4:
3478 case NFA_ZCLOSE5:
3479 case NFA_ZCLOSE6:
3480 case NFA_ZCLOSE7:
3481 case NFA_ZCLOSE8:
3482 case NFA_ZCLOSE9:
3483#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003486 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003488 sub = &subs->norm;
3489 }
3490#ifdef FEAT_SYN_HL
3491 else if (state->c >= NFA_ZCLOSE)
3492 {
3493 subidx = state->c - NFA_ZCLOSE;
3494 sub = &subs->synt;
3495 }
3496#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003497 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003498 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003499 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003500 sub = &subs->norm;
3501 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003503 /* We don't fill in gaps here, there must have been an MOPEN that
3504 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003505 save_in_use = sub->in_use;
3506 if (sub->in_use <= subidx)
3507 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003508 if (REG_MULTI)
3509 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003510 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003511 if (off == -1)
3512 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003513 sub->list.multi[subidx].end.lnum = reglnum + 1;
3514 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003515 }
3516 else
3517 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003518 sub->list.multi[subidx].end.lnum = reglnum;
3519 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003520 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003521 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003522 }
3523 else
3524 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003525 save_ptr = sub->list.line[subidx].end;
3526 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003527 }
3528
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003529 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530
3531 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003532 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003533 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003534 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003535 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003536 break;
3537 }
3538}
3539
3540/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003541 * Like addstate(), but the new state(s) are put at position "*ip".
3542 * Used for zero-width matches, next state to use is the added one.
3543 * This makes sure the order of states to be tried does not change, which
3544 * matters for alternatives.
3545 */
3546 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003547addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003548 nfa_list_T *l; /* runtime state list */
3549 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003550 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003551 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003552 int *ip;
3553{
3554 int tlen = l->n;
3555 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003556 int listidx = *ip;
3557 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003558
3559 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003560 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003561
Bram Moolenaara2d95102013-06-04 14:23:05 +02003562 /* fill in the "pim" field in the new states */
3563 if (pim != NULL)
3564 for (i = tlen; i < l->n; ++i)
3565 l->t[i].pim = pim;
3566
Bram Moolenaar4b417062013-05-25 20:19:50 +02003567 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003568 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003569 return;
3570
3571 /* re-order to put the new state at the current position */
3572 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003573 if (count == 1)
3574 {
3575 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003576 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003577 }
3578 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003579 {
3580 /* make space for new states, then move them from the
3581 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003582 mch_memmove(&(l->t[listidx + count]),
3583 &(l->t[listidx + 1]),
3584 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3585 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003586 &(l->t[l->n - 1]),
3587 sizeof(nfa_thread_T) * count);
3588 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003589 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003590 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003591}
3592
3593/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 * Check character class "class" against current character c.
3595 */
3596 static int
3597check_char_class(class, c)
3598 int class;
3599 int c;
3600{
3601 switch (class)
3602 {
3603 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003604 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 return OK;
3606 break;
3607 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003608 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003609 return OK;
3610 break;
3611 case NFA_CLASS_BLANK:
3612 if (c == ' ' || c == '\t')
3613 return OK;
3614 break;
3615 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003616 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617 return OK;
3618 break;
3619 case NFA_CLASS_DIGIT:
3620 if (VIM_ISDIGIT(c))
3621 return OK;
3622 break;
3623 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003624 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003625 return OK;
3626 break;
3627 case NFA_CLASS_LOWER:
3628 if (MB_ISLOWER(c))
3629 return OK;
3630 break;
3631 case NFA_CLASS_PRINT:
3632 if (vim_isprintc(c))
3633 return OK;
3634 break;
3635 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003636 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637 return OK;
3638 break;
3639 case NFA_CLASS_SPACE:
3640 if ((c >=9 && c <= 13) || (c == ' '))
3641 return OK;
3642 break;
3643 case NFA_CLASS_UPPER:
3644 if (MB_ISUPPER(c))
3645 return OK;
3646 break;
3647 case NFA_CLASS_XDIGIT:
3648 if (vim_isxdigit(c))
3649 return OK;
3650 break;
3651 case NFA_CLASS_TAB:
3652 if (c == '\t')
3653 return OK;
3654 break;
3655 case NFA_CLASS_RETURN:
3656 if (c == '\r')
3657 return OK;
3658 break;
3659 case NFA_CLASS_BACKSPACE:
3660 if (c == '\b')
3661 return OK;
3662 break;
3663 case NFA_CLASS_ESCAPE:
3664 if (c == '\033')
3665 return OK;
3666 break;
3667
3668 default:
3669 /* should not be here :P */
3670 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3671 }
3672 return FAIL;
3673}
3674
Bram Moolenaar5714b802013-05-28 22:03:20 +02003675static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3676
3677/*
3678 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003679 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003680 */
3681 static int
3682match_backref(sub, subidx, bytelen)
3683 regsub_T *sub; /* pointers to subexpressions */
3684 int subidx;
3685 int *bytelen; /* out: length of match in bytes */
3686{
3687 int len;
3688
3689 if (sub->in_use <= subidx)
3690 {
3691retempty:
3692 /* backref was not set, match an empty string */
3693 *bytelen = 0;
3694 return TRUE;
3695 }
3696
3697 if (REG_MULTI)
3698 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003699 if (sub->list.multi[subidx].start.lnum < 0
3700 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003701 goto retempty;
3702 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003703 len = sub->list.multi[subidx].end.col
3704 - sub->list.multi[subidx].start.col;
3705 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003706 reginput, &len) == 0)
3707 {
3708 *bytelen = len;
3709 return TRUE;
3710 }
3711 }
3712 else
3713 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003714 if (sub->list.line[subidx].start == NULL
3715 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003716 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003717 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3718 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003719 {
3720 *bytelen = len;
3721 return TRUE;
3722 }
3723 }
3724 return FALSE;
3725}
3726
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003727#ifdef FEAT_SYN_HL
3728
3729static int match_zref __ARGS((int subidx, int *bytelen));
3730
3731/*
3732 * Check for a match with \z subexpression "subidx".
3733 * Return TRUE if it matches.
3734 */
3735 static int
3736match_zref(subidx, bytelen)
3737 int subidx;
3738 int *bytelen; /* out: length of match in bytes */
3739{
3740 int len;
3741
3742 cleanup_zsubexpr();
3743 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3744 {
3745 /* backref was not set, match an empty string */
3746 *bytelen = 0;
3747 return TRUE;
3748 }
3749
3750 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3751 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3752 {
3753 *bytelen = len;
3754 return TRUE;
3755 }
3756 return FALSE;
3757}
3758#endif
3759
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003761 * Save list IDs for all NFA states of "prog" into "list".
3762 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003763 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764 */
3765 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003766nfa_save_listids(prog, list)
3767 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003768 int *list;
3769{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003770 int i;
3771 nfa_state_T *p;
3772
3773 /* Order in the list is reverse, it's a bit faster that way. */
3774 p = &prog->state[0];
3775 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003776 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003777 list[i] = p->lastlist[1];
3778 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003779 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003780 }
3781}
3782
3783/*
3784 * Restore list IDs from "list" to all NFA states.
3785 */
3786 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003787nfa_restore_listids(prog, list)
3788 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003789 int *list;
3790{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003791 int i;
3792 nfa_state_T *p;
3793
3794 p = &prog->state[0];
3795 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003796 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003797 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003798 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003799 }
3800}
3801
Bram Moolenaar423532e2013-05-29 21:14:42 +02003802 static int
3803nfa_re_num_cmp(val, op, pos)
3804 long_u val;
3805 int op;
3806 long_u pos;
3807{
3808 if (op == 1) return pos > val;
3809 if (op == 2) return pos < val;
3810 return val == pos;
3811}
3812
Bram Moolenaarf46da702013-06-02 22:37:42 +02003813static 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 +02003814static 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 +02003815
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003816/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003817 * Recursively call nfa_regmatch()
3818 */
3819 static int
3820recursive_regmatch(state, prog, submatch, m, listids)
3821 nfa_state_T *state;
3822 nfa_regprog_T *prog;
3823 regsubs_T *submatch;
3824 regsubs_T *m;
3825 int **listids;
3826{
3827 char_u *save_reginput = reginput;
3828 char_u *save_regline = regline;
3829 int save_reglnum = reglnum;
3830 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003831 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003832 save_se_T *save_nfa_endp = nfa_endp;
3833 save_se_T endpos;
3834 save_se_T *endposp = NULL;
3835 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003836 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003837
3838 if (state->c == NFA_START_INVISIBLE_BEFORE)
3839 {
3840 /* The recursive match must end at the current position. */
3841 endposp = &endpos;
3842 if (REG_MULTI)
3843 {
3844 endpos.se_u.pos.col = (int)(reginput - regline);
3845 endpos.se_u.pos.lnum = reglnum;
3846 }
3847 else
3848 endpos.se_u.ptr = reginput;
3849
3850 /* Go back the specified number of bytes, or as far as the
3851 * start of the previous line, to try matching "\@<=" or
3852 * not matching "\@<!".
3853 * TODO: This is very inefficient! Would be better to
3854 * first check for a match with what follows. */
3855 if (state->val <= 0)
3856 {
3857 if (REG_MULTI)
3858 {
3859 regline = reg_getline(--reglnum);
3860 if (regline == NULL)
3861 /* can't go before the first line */
3862 regline = reg_getline(++reglnum);
3863 }
3864 reginput = regline;
3865 }
3866 else
3867 {
3868 if (REG_MULTI && (int)(reginput - regline) < state->val)
3869 {
3870 /* Not enough bytes in this line, go to end of
3871 * previous line. */
3872 regline = reg_getline(--reglnum);
3873 if (regline == NULL)
3874 {
3875 /* can't go before the first line */
3876 regline = reg_getline(++reglnum);
3877 reginput = regline;
3878 }
3879 else
3880 reginput = regline + STRLEN(regline);
3881 }
3882 if ((int)(reginput - regline) >= state->val)
3883 {
3884 reginput -= state->val;
3885#ifdef FEAT_MBYTE
3886 if (has_mbyte)
3887 reginput -= mb_head_off(regline, reginput);
3888#endif
3889 }
3890 else
3891 reginput = regline;
3892 }
3893 }
3894
Bram Moolenaarf46da702013-06-02 22:37:42 +02003895#ifdef ENABLE_LOG
3896 if (log_fd != stderr)
3897 fclose(log_fd);
3898 log_fd = NULL;
3899#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003900 /* Have to clear the lastlist field of the NFA nodes, so that
3901 * nfa_regmatch() and addstate() can run properly after recursion. */
3902 if (nfa_ll_index == 1)
3903 {
3904 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3905 * values and clear them. */
3906 if (*listids == NULL)
3907 {
3908 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3909 if (*listids == NULL)
3910 {
3911 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3912 return 0;
3913 }
3914 }
3915 nfa_save_listids(prog, *listids);
3916 need_restore = TRUE;
3917 /* any value of nfa_listid will do */
3918 }
3919 else
3920 {
3921 /* First recursive nfa_regmatch() call, switch to the second lastlist
3922 * entry. Make sure nfa_listid is different from a previous recursive
3923 * call, because some states may still have this ID. */
3924 ++nfa_ll_index;
3925 if (nfa_listid <= nfa_alt_listid)
3926 nfa_listid = nfa_alt_listid;
3927 }
3928
3929 /* Call nfa_regmatch() to check if the current concat matches at this
3930 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003931 nfa_endp = endposp;
3932 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003933
3934 if (need_restore)
3935 nfa_restore_listids(prog, *listids);
3936 else
3937 {
3938 --nfa_ll_index;
3939 nfa_alt_listid = nfa_listid;
3940 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003941
3942 /* restore position in input text */
3943 reginput = save_reginput;
3944 regline = save_regline;
3945 reglnum = save_reglnum;
3946 nfa_match = save_nfa_match;
3947 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003948 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003949
3950#ifdef ENABLE_LOG
3951 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3952 if (log_fd != NULL)
3953 {
3954 fprintf(log_fd, "****************************\n");
3955 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3956 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3957 fprintf(log_fd, "****************************\n");
3958 }
3959 else
3960 {
3961 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3962 log_fd = stderr;
3963 }
3964#endif
3965
3966 return result;
3967}
3968
Bram Moolenaara2d95102013-06-04 14:23:05 +02003969static int failure_chance __ARGS((nfa_state_T *state, int depth));
3970
3971/*
3972 * Estimate the chance of a match with "state" failing.
3973 * NFA_ANY: 1
3974 * specific character: 99
3975 */
3976 static int
3977failure_chance(state, depth)
3978 nfa_state_T *state;
3979 int depth;
3980{
3981 int c = state->c;
3982 int l, r;
3983
3984 /* detect looping */
3985 if (depth > 4)
3986 return 1;
3987
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003988 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02003989 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003990 case NFA_SPLIT:
3991 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3992 /* avoid recursive stuff */
3993 return 1;
3994 /* two alternatives, use the lowest failure chance */
3995 l = failure_chance(state->out, depth + 1);
3996 r = failure_chance(state->out1, depth + 1);
3997 return l < r ? l : r;
3998
3999 case NFA_ANY:
4000 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004001 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004002 case NFA_MATCH:
4003 /* empty match works always */
4004 return 0;
4005
4006 case NFA_BOL:
4007 case NFA_EOL:
4008 case NFA_BOF:
4009 case NFA_EOF:
4010 case NFA_NEWL:
4011 return 99;
4012
4013 case NFA_BOW:
4014 case NFA_EOW:
4015 return 90;
4016
4017 case NFA_MOPEN:
4018 case NFA_MOPEN1:
4019 case NFA_MOPEN2:
4020 case NFA_MOPEN3:
4021 case NFA_MOPEN4:
4022 case NFA_MOPEN5:
4023 case NFA_MOPEN6:
4024 case NFA_MOPEN7:
4025 case NFA_MOPEN8:
4026 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004027#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004028 case NFA_ZOPEN:
4029 case NFA_ZOPEN1:
4030 case NFA_ZOPEN2:
4031 case NFA_ZOPEN3:
4032 case NFA_ZOPEN4:
4033 case NFA_ZOPEN5:
4034 case NFA_ZOPEN6:
4035 case NFA_ZOPEN7:
4036 case NFA_ZOPEN8:
4037 case NFA_ZOPEN9:
4038 case NFA_ZCLOSE:
4039 case NFA_ZCLOSE1:
4040 case NFA_ZCLOSE2:
4041 case NFA_ZCLOSE3:
4042 case NFA_ZCLOSE4:
4043 case NFA_ZCLOSE5:
4044 case NFA_ZCLOSE6:
4045 case NFA_ZCLOSE7:
4046 case NFA_ZCLOSE8:
4047 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004048#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004049 case NFA_NOPEN:
4050 case NFA_MCLOSE:
4051 case NFA_MCLOSE1:
4052 case NFA_MCLOSE2:
4053 case NFA_MCLOSE3:
4054 case NFA_MCLOSE4:
4055 case NFA_MCLOSE5:
4056 case NFA_MCLOSE6:
4057 case NFA_MCLOSE7:
4058 case NFA_MCLOSE8:
4059 case NFA_MCLOSE9:
4060 case NFA_NCLOSE:
4061 return failure_chance(state->out, depth + 1);
4062
4063 case NFA_BACKREF1:
4064 case NFA_BACKREF2:
4065 case NFA_BACKREF3:
4066 case NFA_BACKREF4:
4067 case NFA_BACKREF5:
4068 case NFA_BACKREF6:
4069 case NFA_BACKREF7:
4070 case NFA_BACKREF8:
4071 case NFA_BACKREF9:
4072#ifdef FEAT_SYN_HL
4073 case NFA_ZREF1:
4074 case NFA_ZREF2:
4075 case NFA_ZREF3:
4076 case NFA_ZREF4:
4077 case NFA_ZREF5:
4078 case NFA_ZREF6:
4079 case NFA_ZREF7:
4080 case NFA_ZREF8:
4081 case NFA_ZREF9:
4082#endif
4083 /* backreferences don't match in many places */
4084 return 94;
4085
4086 case NFA_LNUM_GT:
4087 case NFA_LNUM_LT:
4088 case NFA_COL_GT:
4089 case NFA_COL_LT:
4090 case NFA_VCOL_GT:
4091 case NFA_VCOL_LT:
4092 case NFA_MARK_GT:
4093 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004094 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004095 /* before/after positions don't match very often */
4096 return 85;
4097
4098 case NFA_LNUM:
4099 return 90;
4100
4101 case NFA_CURSOR:
4102 case NFA_COL:
4103 case NFA_VCOL:
4104 case NFA_MARK:
4105 /* specific positions rarely match */
4106 return 98;
4107
4108 case NFA_COMPOSING:
4109 return 95;
4110
4111 default:
4112 if (c > 0)
4113 /* character match fails often */
4114 return 95;
4115 }
4116
4117 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004118 return 50;
4119}
4120
Bram Moolenaarf46da702013-06-02 22:37:42 +02004121/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004122 * Main matching routine.
4123 *
4124 * Run NFA to determine whether it matches reginput.
4125 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004126 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004127 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004128 * Return TRUE if there is a match, FALSE otherwise.
4129 * Note: Caller must ensure that: start != NULL.
4130 */
4131 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004132nfa_regmatch(prog, start, submatch, m)
4133 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004134 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004135 regsubs_T *submatch;
4136 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004137{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004138 int result;
4139 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004140 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004141 int go_to_nextline = FALSE;
4142 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004143 nfa_list_T list[3];
4144 nfa_list_T *listtbl[2][2];
4145 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004146 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004147 nfa_list_T *thislist;
4148 nfa_list_T *nextlist;
4149 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004150 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004151 nfa_state_T *add_state;
4152 int add_count;
4153 int add_off;
4154 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004155#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004156 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004157
4158 if (debug == NULL)
4159 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004160 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004161 return FALSE;
4162 }
4163#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004164 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004165 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004166
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004167 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004168 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004169 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4170 list[0].len = nstate + 1;
4171 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4172 list[1].len = nstate + 1;
4173 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4174 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004175 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
4176 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004177
4178#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004179 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004180 if (log_fd != NULL)
4181 {
4182 fprintf(log_fd, "**********************************\n");
4183 nfa_set_code(start->c);
4184 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4185 abs(start->id), code);
4186 fprintf(log_fd, "**********************************\n");
4187 }
4188 else
4189 {
4190 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4191 log_fd = stderr;
4192 }
4193#endif
4194
4195 thislist = &list[0];
4196 thislist->n = 0;
4197 nextlist = &list[1];
4198 nextlist->n = 0;
4199 neglist = &list[2];
4200 neglist->n = 0;
4201#ifdef ENABLE_LOG
4202 fprintf(log_fd, "(---) STARTSTATE\n");
4203#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004204 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004205 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004206
4207 /* There are two cases when the NFA advances: 1. input char matches the
4208 * NFA node and 2. input char does not match the NFA node, but the next
4209 * node is NFA_NOT. The following macro calls addstate() according to
4210 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4211 listtbl[0][0] = NULL;
4212 listtbl[0][1] = neglist;
4213 listtbl[1][0] = nextlist;
4214 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004215#define ADD_POS_NEG_STATE(state) \
4216 ll = listtbl[result ? 1 : 0][state->negated]; \
4217 if (ll != NULL) { \
4218 add_state = state->out; \
4219 add_off = clen; \
4220 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004221
4222 /*
4223 * Run for each character.
4224 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004225 for (;;)
4226 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004227 int curc;
4228 int clen;
4229
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004230#ifdef FEAT_MBYTE
4231 if (has_mbyte)
4232 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004233 curc = (*mb_ptr2char)(reginput);
4234 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235 }
4236 else
4237#endif
4238 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004239 curc = *reginput;
4240 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004241 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004242 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004243 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004244 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004245 go_to_nextline = FALSE;
4246 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004247
4248 /* swap lists */
4249 thislist = &list[flag];
4250 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004251 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004252 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004253 ++nfa_listid;
4254 thislist->id = nfa_listid;
4255 nextlist->id = nfa_listid + 1;
4256 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257
Bram Moolenaara2d95102013-06-04 14:23:05 +02004258 pimlist.ga_len = 0;
4259
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260#ifdef ENABLE_LOG
4261 fprintf(log_fd, "------------------------------------------\n");
4262 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004263 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004264 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004265 {
4266 int i;
4267
4268 for (i = 0; i < thislist->n; i++)
4269 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4270 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004271 fprintf(log_fd, "\n");
4272#endif
4273
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004274#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004275 fprintf(debug, "\n-------------------\n");
4276#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004277 /*
4278 * If the state lists are empty we can stop.
4279 */
4280 if (thislist->n == 0 && neglist->n == 0)
4281 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004282
4283 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004284 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004285 {
4286 if (neglist->n > 0)
4287 {
4288 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004289 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004290 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004291 }
4292 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004293 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004295#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004296 nfa_set_code(t->state->c);
4297 fprintf(debug, "%s, ", code);
4298#endif
4299#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004300 {
4301 int col;
4302
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004303 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004304 col = -1;
4305 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004306 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004307 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004308 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004309 nfa_set_code(t->state->c);
4310 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4311 abs(t->state->id), (int)t->state->c, code, col);
4312 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004313#endif
4314
4315 /*
4316 * Handle the possible codes of the current state.
4317 * The most important is NFA_MATCH.
4318 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004319 add_state = NULL;
4320 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 switch (t->state->c)
4322 {
4323 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004324 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004325 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004326 copy_sub(&submatch->norm, &t->subs.norm);
4327#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004328 if (nfa_has_zsubexpr)
4329 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004330#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004331#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004332 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004333#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004334 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004335 * states at this position. When the list of states is going
4336 * to be empty quit without advancing, so that "reginput" is
4337 * correct. */
4338 if (nextlist->n == 0 && neglist->n == 0)
4339 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004340 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004341 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004342
4343 case NFA_END_INVISIBLE:
Bram Moolenaar87953742013-06-05 18:52:40 +02004344 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004345 /*
4346 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004347 * NFA_START_INVISIBLE_BEFORE node.
4348 * They surround a zero-width group, used with "\@=", "\&",
4349 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350 * If we got here, it means that the current "invisible" group
4351 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004352 * nfa_regmatch(). For a look-behind match only when it ends
4353 * in the position in "nfa_endp".
4354 * Submatches are stored in *m, and used in the parent call.
4355 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004356#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004357 if (nfa_endp != NULL)
4358 {
4359 if (REG_MULTI)
4360 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4361 (int)reglnum,
4362 (int)nfa_endp->se_u.pos.lnum,
4363 (int)(reginput - regline),
4364 nfa_endp->se_u.pos.col);
4365 else
4366 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4367 (int)(reginput - regline),
4368 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004369 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004370#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004371 /* If "nfa_endp" is set it's only a match if it ends at
4372 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004373 if (nfa_endp != NULL && (REG_MULTI
4374 ? (reglnum != nfa_endp->se_u.pos.lnum
4375 || (int)(reginput - regline)
4376 != nfa_endp->se_u.pos.col)
4377 : reginput != nfa_endp->se_u.ptr))
4378 break;
4379
4380 /* do not set submatches for \@! */
4381 if (!t->state->negated)
4382 {
4383 copy_sub(&m->norm, &t->subs.norm);
4384#ifdef FEAT_SYN_HL
4385 if (nfa_has_zsubexpr)
4386 copy_sub(&m->synt, &t->subs.synt);
4387#endif
4388 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004389#ifdef ENABLE_LOG
4390 fprintf(log_fd, "Match found:\n");
4391 log_subsexpr(m);
4392#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004393 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004394 break;
4395
4396 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004397 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004398 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004399 nfa_pim_T *pim;
4400 int cout = t->state->out1->out->c;
4401
4402 /* Do it directly when what follows is possibly end of
4403 * match (closing paren).
4404 * Postpone when it is \@<= or \@<!, these are expensive.
4405 * TODO: remove the check for t->pim and check multiple
4406 * where it's used?
4407 * Otherwise first do the one that has the highest chance
4408 * of failing. */
4409 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004410#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004411 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004412#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004413 || cout == NFA_NCLOSE
4414 || t->pim != NULL
4415 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4416 && failure_chance(t->state->out1->out, 0)
4417 < failure_chance(t->state->out, 0)))
4418 {
4419 /*
4420 * First try matching the invisible match, then what
4421 * follows.
4422 */
4423 result = recursive_regmatch(t->state, prog,
4424 submatch, m, &listids);
4425
4426 /* for \@! it is a match when result is FALSE */
4427 if (result != t->state->negated)
4428 {
4429 /* Copy submatch info from the recursive call */
4430 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004431#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004432 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004433#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004434
Bram Moolenaara2d95102013-06-04 14:23:05 +02004435 /* t->state->out1 is the corresponding
4436 * END_INVISIBLE node; Add its out to the current
4437 * list (zero-width match). */
4438 addstate_here(thislist, t->state->out1->out,
4439 &t->subs, t->pim, &listidx);
4440 }
4441 }
4442 else
4443 {
4444 /*
4445 * First try matching what follows at the current
4446 * position. Only if a match is found, addstate() is
4447 * called, then verify the invisible match matches.
4448 * Add a nfa_pim_T to the following states, it
4449 * contains info about the invisible match.
4450 */
4451 if (ga_grow(&pimlist, 1) == FAIL)
4452 goto theend;
4453 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4454 ++pimlist.ga_len;
4455 pim->state = t->state;
4456 pim->pim = NULL;
4457 pim->result = NFA_PIM_TODO;
4458
4459 /* t->state->out1 is the corresponding END_INVISIBLE
4460 * node; Add its out to the current list (zero-width
4461 * match). */
4462 addstate_here(thislist, t->state->out1->out, &t->subs,
4463 pim, &listidx);
4464 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004465 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004466 break;
4467
Bram Moolenaar87953742013-06-05 18:52:40 +02004468 case NFA_START_PATTERN:
4469 /* First try matching the pattern. */
4470 result = recursive_regmatch(t->state, prog,
4471 submatch, m, &listids);
4472 if (result)
4473 {
4474 int bytelen;
4475
4476#ifdef ENABLE_LOG
4477 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4478 log_subsexpr(m);
4479#endif
4480 /* Copy submatch info from the recursive call */
4481 copy_sub_off(&t->subs.norm, &m->norm);
4482#ifdef FEAT_SYN_HL
4483 copy_sub_off(&t->subs.synt, &m->synt);
4484#endif
4485 /* Now we need to skip over the matched text and then
4486 * continue with what follows. */
4487 if (REG_MULTI)
4488 /* TODO: multi-line match */
4489 bytelen = m->norm.list.multi[0].end.col
4490 - (int)(reginput - regline);
4491 else
4492 bytelen = (int)(m->norm.list.line[0].end - reginput);
4493
4494#ifdef ENABLE_LOG
4495 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4496#endif
4497 if (bytelen == 0)
4498 {
4499 /* empty match, output of corresponding
4500 * NFA_END_PATTERN/NFA_SKIP to be used at current
4501 * position */
4502 addstate_here(thislist, t->state->out1->out->out,
4503 &t->subs, t->pim, &listidx);
4504 }
4505 else if (bytelen <= clen)
4506 {
4507 /* match current character, output of corresponding
4508 * NFA_END_PATTERN to be used at next position. */
4509 ll = nextlist;
4510 add_state = t->state->out1->out->out;
4511 add_off = clen;
4512 }
4513 else
4514 {
4515 /* skip over the matched characters, set character
4516 * count in NFA_SKIP */
4517 ll = nextlist;
4518 add_state = t->state->out1->out;
4519 add_off = bytelen;
4520 add_count = bytelen - clen;
4521 }
4522 }
4523 break;
4524
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 case NFA_BOL:
4526 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004527 addstate_here(thislist, t->state->out, &t->subs,
4528 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529 break;
4530
4531 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004532 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004533 addstate_here(thislist, t->state->out, &t->subs,
4534 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004535 break;
4536
4537 case NFA_BOW:
4538 {
4539 int bow = TRUE;
4540
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004541 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004542 bow = FALSE;
4543#ifdef FEAT_MBYTE
4544 else if (has_mbyte)
4545 {
4546 int this_class;
4547
4548 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004549 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550 if (this_class <= 1)
4551 bow = FALSE;
4552 else if (reg_prev_class() == this_class)
4553 bow = FALSE;
4554 }
4555#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004556 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004557 || (reginput > regline
4558 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 bow = FALSE;
4560 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004561 addstate_here(thislist, t->state->out, &t->subs,
4562 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 break;
4564 }
4565
4566 case NFA_EOW:
4567 {
4568 int eow = TRUE;
4569
4570 if (reginput == regline)
4571 eow = FALSE;
4572#ifdef FEAT_MBYTE
4573 else if (has_mbyte)
4574 {
4575 int this_class, prev_class;
4576
4577 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004578 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004579 prev_class = reg_prev_class();
4580 if (this_class == prev_class
4581 || prev_class == 0 || prev_class == 1)
4582 eow = FALSE;
4583 }
4584#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004585 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004586 || (reginput[0] != NUL
4587 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588 eow = FALSE;
4589 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004590 addstate_here(thislist, t->state->out, &t->subs,
4591 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004592 break;
4593 }
4594
Bram Moolenaar4b780632013-05-31 22:14:52 +02004595 case NFA_BOF:
4596 if (reglnum == 0 && reginput == regline
4597 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004598 addstate_here(thislist, t->state->out, &t->subs,
4599 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004600 break;
4601
4602 case NFA_EOF:
4603 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004604 addstate_here(thislist, t->state->out, &t->subs,
4605 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004606 break;
4607
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004608#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004610 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004611 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004612 int len = 0;
4613 nfa_state_T *end;
4614 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004615 int cchars[MAX_MCO];
4616 int ccount = 0;
4617 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004618
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004619 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004620 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004621 if (utf_iscomposing(sta->c))
4622 {
4623 /* Only match composing character(s), ignore base
4624 * character. Used for ".{composing}" and "{composing}"
4625 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004626 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004627 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004628 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004630 /* If \Z was present, then ignore composing characters.
4631 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004632 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004633 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004634 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004635 else
4636 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004637 while (sta->c != NFA_END_COMPOSING)
4638 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004639 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004640
4641 /* Check base character matches first, unless ignored. */
4642 else if (len > 0 || mc == sta->c)
4643 {
4644 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004645 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004646 len += mb_char2len(mc);
4647 sta = sta->out;
4648 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004649
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004650 /* We don't care about the order of composing characters.
4651 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004652 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004653 {
4654 mc = mb_ptr2char(reginput + len);
4655 cchars[ccount++] = mc;
4656 len += mb_char2len(mc);
4657 if (ccount == MAX_MCO)
4658 break;
4659 }
4660
4661 /* Check that each composing char in the pattern matches a
4662 * composing char in the text. We do not check if all
4663 * composing chars are matched. */
4664 result = OK;
4665 while (sta->c != NFA_END_COMPOSING)
4666 {
4667 for (j = 0; j < ccount; ++j)
4668 if (cchars[j] == sta->c)
4669 break;
4670 if (j == ccount)
4671 {
4672 result = FAIL;
4673 break;
4674 }
4675 sta = sta->out;
4676 }
4677 }
4678 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004679 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004680
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004681 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682 ADD_POS_NEG_STATE(end);
4683 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004684 }
4685#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686
4687 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004688 if (curc == NUL && !reg_line_lbr && REG_MULTI
4689 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004690 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004691 go_to_nextline = TRUE;
4692 /* Pass -1 for the offset, which means taking the position
4693 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004694 ll = nextlist;
4695 add_state = t->state->out;
4696 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004697 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004698 else if (curc == '\n' && reg_line_lbr)
4699 {
4700 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004701 ll = nextlist;
4702 add_state = t->state->out;
4703 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004704 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004705 break;
4706
4707 case NFA_CLASS_ALNUM:
4708 case NFA_CLASS_ALPHA:
4709 case NFA_CLASS_BLANK:
4710 case NFA_CLASS_CNTRL:
4711 case NFA_CLASS_DIGIT:
4712 case NFA_CLASS_GRAPH:
4713 case NFA_CLASS_LOWER:
4714 case NFA_CLASS_PRINT:
4715 case NFA_CLASS_PUNCT:
4716 case NFA_CLASS_SPACE:
4717 case NFA_CLASS_UPPER:
4718 case NFA_CLASS_XDIGIT:
4719 case NFA_CLASS_TAB:
4720 case NFA_CLASS_RETURN:
4721 case NFA_CLASS_BACKSPACE:
4722 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004723 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 ADD_POS_NEG_STATE(t->state);
4725 break;
4726
4727 case NFA_END_NEG_RANGE:
4728 /* This follows a series of negated nodes, like:
4729 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004730 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004731 {
4732 ll = nextlist;
4733 add_state = t->state->out;
4734 add_off = clen;
4735 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736 break;
4737
4738 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004739 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004740 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004741 {
4742 ll = nextlist;
4743 add_state = t->state->out;
4744 add_off = clen;
4745 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004746 break;
4747
4748 /*
4749 * Character classes like \a for alpha, \d for digit etc.
4750 */
4751 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004752 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004753 ADD_POS_NEG_STATE(t->state);
4754 break;
4755
4756 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004757 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004758 ADD_POS_NEG_STATE(t->state);
4759 break;
4760
4761 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004762 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004763 ADD_POS_NEG_STATE(t->state);
4764 break;
4765
4766 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004767 result = !VIM_ISDIGIT(curc)
4768 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004769 ADD_POS_NEG_STATE(t->state);
4770 break;
4771
4772 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004773 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004774 ADD_POS_NEG_STATE(t->state);
4775 break;
4776
4777 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004778 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004779 ADD_POS_NEG_STATE(t->state);
4780 break;
4781
4782 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004783 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004784 ADD_POS_NEG_STATE(t->state);
4785 break;
4786
4787 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004788 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004789 ADD_POS_NEG_STATE(t->state);
4790 break;
4791
4792 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004793 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004794 ADD_POS_NEG_STATE(t->state);
4795 break;
4796
4797 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004798 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004799 ADD_POS_NEG_STATE(t->state);
4800 break;
4801
4802 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004803 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004804 ADD_POS_NEG_STATE(t->state);
4805 break;
4806
4807 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004808 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004809 ADD_POS_NEG_STATE(t->state);
4810 break;
4811
4812 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004813 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004814 ADD_POS_NEG_STATE(t->state);
4815 break;
4816
4817 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004818 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004819 ADD_POS_NEG_STATE(t->state);
4820 break;
4821
4822 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004823 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004824 ADD_POS_NEG_STATE(t->state);
4825 break;
4826
4827 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004828 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004829 ADD_POS_NEG_STATE(t->state);
4830 break;
4831
4832 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004833 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004834 ADD_POS_NEG_STATE(t->state);
4835 break;
4836
4837 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004838 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004839 ADD_POS_NEG_STATE(t->state);
4840 break;
4841
4842 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004843 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004844 ADD_POS_NEG_STATE(t->state);
4845 break;
4846
4847 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004848 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004849 ADD_POS_NEG_STATE(t->state);
4850 break;
4851
4852 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004853 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004854 ADD_POS_NEG_STATE(t->state);
4855 break;
4856
4857 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004858 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004859 ADD_POS_NEG_STATE(t->state);
4860 break;
4861
4862 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004863 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004864 ADD_POS_NEG_STATE(t->state);
4865 break;
4866
4867 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004868 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004869 ADD_POS_NEG_STATE(t->state);
4870 break;
4871
4872 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004873 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004874 ADD_POS_NEG_STATE(t->state);
4875 break;
4876
4877 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004878 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004879 ADD_POS_NEG_STATE(t->state);
4880 break;
4881
Bram Moolenaar5714b802013-05-28 22:03:20 +02004882 case NFA_BACKREF1:
4883 case NFA_BACKREF2:
4884 case NFA_BACKREF3:
4885 case NFA_BACKREF4:
4886 case NFA_BACKREF5:
4887 case NFA_BACKREF6:
4888 case NFA_BACKREF7:
4889 case NFA_BACKREF8:
4890 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004891#ifdef FEAT_SYN_HL
4892 case NFA_ZREF1:
4893 case NFA_ZREF2:
4894 case NFA_ZREF3:
4895 case NFA_ZREF4:
4896 case NFA_ZREF5:
4897 case NFA_ZREF6:
4898 case NFA_ZREF7:
4899 case NFA_ZREF8:
4900 case NFA_ZREF9:
4901#endif
4902 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004903 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004904 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004905 int bytelen;
4906
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004907 if (t->state->c <= NFA_BACKREF9)
4908 {
4909 subidx = t->state->c - NFA_BACKREF1 + 1;
4910 result = match_backref(&t->subs.norm, subidx, &bytelen);
4911 }
4912#ifdef FEAT_SYN_HL
4913 else
4914 {
4915 subidx = t->state->c - NFA_ZREF1 + 1;
4916 result = match_zref(subidx, &bytelen);
4917 }
4918#endif
4919
Bram Moolenaar5714b802013-05-28 22:03:20 +02004920 if (result)
4921 {
4922 if (bytelen == 0)
4923 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004924 /* empty match always works, output of NFA_SKIP to be
4925 * used next */
4926 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004927 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004928 }
4929 else if (bytelen <= clen)
4930 {
4931 /* match current character, jump ahead to out of
4932 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004933 ll = nextlist;
4934 add_state = t->state->out->out;
4935 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004936 }
4937 else
4938 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004939 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004940 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004941 ll = nextlist;
4942 add_state = t->state->out;
4943 add_off = bytelen;
4944 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004945 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004946 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004947 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004948 }
4949 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004950 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004951 if (t->count - clen <= 0)
4952 {
4953 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004954 ll = nextlist;
4955 add_state = t->state->out;
4956 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004957 }
4958 else
4959 {
4960 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004961 ll = nextlist;
4962 add_state = t->state;
4963 add_off = 0;
4964 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004965 }
4966 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004967
Bram Moolenaar423532e2013-05-29 21:14:42 +02004968 case NFA_LNUM:
4969 case NFA_LNUM_GT:
4970 case NFA_LNUM_LT:
4971 result = (REG_MULTI &&
4972 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4973 (long_u)(reglnum + reg_firstlnum)));
4974 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004975 addstate_here(thislist, t->state->out, &t->subs,
4976 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004977 break;
4978
4979 case NFA_COL:
4980 case NFA_COL_GT:
4981 case NFA_COL_LT:
4982 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4983 (long_u)(reginput - regline) + 1);
4984 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004985 addstate_here(thislist, t->state->out, &t->subs,
4986 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004987 break;
4988
4989 case NFA_VCOL:
4990 case NFA_VCOL_GT:
4991 case NFA_VCOL_LT:
4992 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4993 (long_u)win_linetabsize(
4994 reg_win == NULL ? curwin : reg_win,
4995 regline, (colnr_T)(reginput - regline)) + 1);
4996 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004997 addstate_here(thislist, t->state->out, &t->subs,
4998 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004999 break;
5000
Bram Moolenaar044aa292013-06-04 21:27:38 +02005001 case NFA_MARK:
5002 case NFA_MARK_GT:
5003 case NFA_MARK_LT:
5004 {
5005 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5006
5007 /* Compare the mark position to the match position. */
5008 result = (pos != NULL /* mark doesn't exist */
5009 && pos->lnum > 0 /* mark isn't set in reg_buf */
5010 && (pos->lnum == reglnum + reg_firstlnum
5011 ? (pos->col == (colnr_T)(reginput - regline)
5012 ? t->state->c == NFA_MARK
5013 : (pos->col < (colnr_T)(reginput - regline)
5014 ? t->state->c == NFA_MARK_GT
5015 : t->state->c == NFA_MARK_LT))
5016 : (pos->lnum < reglnum + reg_firstlnum
5017 ? t->state->c == NFA_MARK_GT
5018 : t->state->c == NFA_MARK_LT)));
5019 if (result)
5020 addstate_here(thislist, t->state->out, &t->subs,
5021 t->pim, &listidx);
5022 break;
5023 }
5024
Bram Moolenaar423532e2013-05-29 21:14:42 +02005025 case NFA_CURSOR:
5026 result = (reg_win != NULL
5027 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5028 && ((colnr_T)(reginput - regline)
5029 == reg_win->w_cursor.col));
5030 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005031 addstate_here(thislist, t->state->out, &t->subs,
5032 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005033 break;
5034
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005035 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005036#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005037 result = reg_match_visual();
5038 if (result)
5039 addstate_here(thislist, t->state->out, &t->subs,
5040 t->pim, &listidx);
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005041#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005042 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005043
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005044 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005045 {
5046 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005047
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005048 /* TODO: put this in #ifdef later */
5049 if (c < -256)
5050 EMSGN("INTERNAL: Negative state char: %ld", c);
5051 if (is_Magic(c))
5052 c = un_Magic(c);
5053 result = (c == curc);
5054
5055 if (!result && ireg_ic)
5056 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005057#ifdef FEAT_MBYTE
5058 /* If there is a composing character which is not being
5059 * ignored there can be no match. Match with composing
5060 * character uses NFA_COMPOSING above. */
5061 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005062 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005063 result = FALSE;
5064#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005065 ADD_POS_NEG_STATE(t->state);
5066 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005067 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005068
5069 } /* switch (t->state->c) */
5070
5071 if (add_state != NULL)
5072 {
5073 if (t->pim != NULL)
5074 {
5075 /* postponed invisible match */
5076 /* TODO: also do t->pim->pim recursively? */
5077 if (t->pim->result == NFA_PIM_TODO)
5078 {
5079#ifdef ENABLE_LOG
5080 fprintf(log_fd, "\n");
5081 fprintf(log_fd, "==================================\n");
5082 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5083 fprintf(log_fd, "\n");
5084#endif
5085 result = recursive_regmatch(t->pim->state,
5086 prog, submatch, m, &listids);
5087 t->pim->result = result ? NFA_PIM_MATCH
5088 : NFA_PIM_NOMATCH;
5089 /* for \@! it is a match when result is FALSE */
5090 if (result != t->pim->state->negated)
5091 {
5092 /* Copy submatch info from the recursive call */
5093 copy_sub_off(&t->pim->subs.norm, &m->norm);
5094#ifdef FEAT_SYN_HL
5095 copy_sub_off(&t->pim->subs.synt, &m->synt);
5096#endif
5097 }
5098 }
5099 else
5100 {
5101 result = (t->pim->result == NFA_PIM_MATCH);
5102#ifdef ENABLE_LOG
5103 fprintf(log_fd, "\n");
5104 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5105 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5106 fprintf(log_fd, "\n");
5107#endif
5108 }
5109
5110 /* for \@! it is a match when result is FALSE */
5111 if (result != t->pim->state->negated)
5112 {
5113 /* Copy submatch info from the recursive call */
5114 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5115#ifdef FEAT_SYN_HL
5116 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
5117#endif
5118 }
5119 else
5120 /* look-behind match failed, don't add the state */
5121 continue;
5122 }
5123
5124 addstate(ll, add_state, &t->subs, add_off);
5125 if (add_count > 0)
5126 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005127 }
5128
5129 } /* for (thislist = thislist; thislist->state; thislist++) */
5130
Bram Moolenaare23febd2013-05-26 18:40:14 +02005131 /* Look for the start of a match in the current position by adding the
5132 * start state to the list of states.
5133 * The first found match is the leftmost one, thus the order of states
5134 * matters!
5135 * Do not add the start state in recursive calls of nfa_regmatch(),
5136 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005137 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005138 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005139 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005140 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005141 && reglnum == 0
5142 && clen != 0
5143 && (ireg_maxcol == 0
5144 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005145 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005146 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005147 ? (reglnum < nfa_endp->se_u.pos.lnum
5148 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005149 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005150 < nfa_endp->se_u.pos.col))
5151 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005152 {
5153#ifdef ENABLE_LOG
5154 fprintf(log_fd, "(---) STARTSTATE\n");
5155#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005156 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005157 }
5158
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005159#ifdef ENABLE_LOG
5160 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005161 {
5162 int i;
5163
5164 for (i = 0; i < thislist->n; i++)
5165 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5166 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005167 fprintf(log_fd, "\n");
5168#endif
5169
5170nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005171 /* Advance to the next character, or advance to the next line, or
5172 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005173 if (clen != 0)
5174 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005175 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5176 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005177 reg_nextline();
5178 else
5179 break;
5180 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005181
5182#ifdef ENABLE_LOG
5183 if (log_fd != stderr)
5184 fclose(log_fd);
5185 log_fd = NULL;
5186#endif
5187
5188theend:
5189 /* Free memory */
5190 vim_free(list[0].t);
5191 vim_free(list[1].t);
5192 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005193 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005194 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005195#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005196#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005197 fclose(debug);
5198#endif
5199
Bram Moolenaar963fee22013-05-26 21:47:28 +02005200 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005201}
5202
5203/*
5204 * Try match of "prog" with at regline["col"].
5205 * Returns 0 for failure, number of lines contained in the match otherwise.
5206 */
5207 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005208nfa_regtry(prog, col)
5209 nfa_regprog_T *prog;
5210 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005211{
5212 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005213 regsubs_T subs, m;
5214 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005215#ifdef ENABLE_LOG
5216 FILE *f;
5217#endif
5218
5219 reginput = regline + col;
5220 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005221#ifdef FEAT_SYN_HL
5222 /* Clear the external match subpointers if necessary. */
5223 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005224 {
5225 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005226 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005227 }
5228 else
5229 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005230#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005231
5232#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005233 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005234 if (f != NULL)
5235 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005236 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005237#ifdef DEBUG
5238 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5239#endif
5240 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005241 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005242 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005243 fprintf(f, "\n\n");
5244 fclose(f);
5245 }
5246 else
5247 EMSG(_("Could not open temporary log file for writing "));
5248#endif
5249
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005250 clear_sub(&subs.norm);
5251 clear_sub(&m.norm);
5252#ifdef FEAT_SYN_HL
5253 clear_sub(&subs.synt);
5254 clear_sub(&m.synt);
5255#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005256
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005257 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005258 return 0;
5259
5260 cleanup_subexpr();
5261 if (REG_MULTI)
5262 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005263 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005264 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005265 reg_startpos[i] = subs.norm.list.multi[i].start;
5266 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005267 }
5268
5269 if (reg_startpos[0].lnum < 0)
5270 {
5271 reg_startpos[0].lnum = 0;
5272 reg_startpos[0].col = col;
5273 }
5274 if (reg_endpos[0].lnum < 0)
5275 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005276 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005277 reg_endpos[0].lnum = reglnum;
5278 reg_endpos[0].col = (int)(reginput - regline);
5279 }
5280 else
5281 /* Use line number of "\ze". */
5282 reglnum = reg_endpos[0].lnum;
5283 }
5284 else
5285 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005286 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005287 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005288 reg_startp[i] = subs.norm.list.line[i].start;
5289 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005290 }
5291
5292 if (reg_startp[0] == NULL)
5293 reg_startp[0] = regline + col;
5294 if (reg_endp[0] == NULL)
5295 reg_endp[0] = reginput;
5296 }
5297
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005298#ifdef FEAT_SYN_HL
5299 /* Package any found \z(...\) matches for export. Default is none. */
5300 unref_extmatch(re_extmatch_out);
5301 re_extmatch_out = NULL;
5302
5303 if (prog->reghasz == REX_SET)
5304 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005305 cleanup_zsubexpr();
5306 re_extmatch_out = make_extmatch();
5307 for (i = 0; i < subs.synt.in_use; i++)
5308 {
5309 if (REG_MULTI)
5310 {
5311 struct multipos *mpos = &subs.synt.list.multi[i];
5312
5313 /* Only accept single line matches. */
5314 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5315 re_extmatch_out->matches[i] =
5316 vim_strnsave(reg_getline(mpos->start.lnum)
5317 + mpos->start.col,
5318 mpos->end.col - mpos->start.col);
5319 }
5320 else
5321 {
5322 struct linepos *lpos = &subs.synt.list.line[i];
5323
5324 if (lpos->start != NULL && lpos->end != NULL)
5325 re_extmatch_out->matches[i] =
5326 vim_strnsave(lpos->start,
5327 (int)(lpos->end - lpos->start));
5328 }
5329 }
5330 }
5331#endif
5332
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005333 return 1 + reglnum;
5334}
5335
5336/*
5337 * Match a regexp against a string ("line" points to the string) or multiple
5338 * lines ("line" is NULL, use reg_getline()).
5339 *
5340 * Returns 0 for failure, number of lines contained in the match otherwise.
5341 */
5342 static long
5343nfa_regexec_both(line, col)
5344 char_u *line;
5345 colnr_T col; /* column to start looking for match */
5346{
5347 nfa_regprog_T *prog;
5348 long retval = 0L;
5349 int i;
5350
5351 if (REG_MULTI)
5352 {
5353 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5354 line = reg_getline((linenr_T)0); /* relative to the cursor */
5355 reg_startpos = reg_mmatch->startpos;
5356 reg_endpos = reg_mmatch->endpos;
5357 }
5358 else
5359 {
5360 prog = (nfa_regprog_T *)reg_match->regprog;
5361 reg_startp = reg_match->startp;
5362 reg_endp = reg_match->endp;
5363 }
5364
5365 /* Be paranoid... */
5366 if (prog == NULL || line == NULL)
5367 {
5368 EMSG(_(e_null));
5369 goto theend;
5370 }
5371
5372 /* If the start column is past the maximum column: no need to try. */
5373 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5374 goto theend;
5375
5376 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5377 if (prog->regflags & RF_ICASE)
5378 ireg_ic = TRUE;
5379 else if (prog->regflags & RF_NOICASE)
5380 ireg_ic = FALSE;
5381
5382#ifdef FEAT_MBYTE
5383 /* If pattern contains "\Z" overrule value of ireg_icombine */
5384 if (prog->regflags & RF_ICOMBINE)
5385 ireg_icombine = TRUE;
5386#endif
5387
5388 regline = line;
5389 reglnum = 0; /* relative to line */
5390
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005391 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005392 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005393 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005394 nfa_listid = 1;
5395 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005396#ifdef DEBUG
5397 nfa_regengine.expr = prog->pattern;
5398#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005399
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005400 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005401 for (i = 0; i < nstate; ++i)
5402 {
5403 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005404 prog->state[i].lastlist[0] = 0;
5405 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005406 }
5407
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005408 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005409
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005410#ifdef DEBUG
5411 nfa_regengine.expr = NULL;
5412#endif
5413
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005414theend:
5415 return retval;
5416}
5417
5418/*
5419 * Compile a regular expression into internal code for the NFA matcher.
5420 * Returns the program in allocated space. Returns NULL for an error.
5421 */
5422 static regprog_T *
5423nfa_regcomp(expr, re_flags)
5424 char_u *expr;
5425 int re_flags;
5426{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005427 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005428 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005429 int *postfix;
5430
5431 if (expr == NULL)
5432 return NULL;
5433
5434#ifdef DEBUG
5435 nfa_regengine.expr = expr;
5436#endif
5437
5438 init_class_tab();
5439
5440 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5441 return NULL;
5442
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005443 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005444 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005445 postfix = re2post();
5446 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005447 {
5448 /* TODO: only give this error for debugging? */
5449 if (post_ptr >= post_end)
5450 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005451 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005452 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005453
5454 /*
5455 * In order to build the NFA, we parse the input regexp twice:
5456 * 1. first pass to count size (so we can allocate space)
5457 * 2. second to emit code
5458 */
5459#ifdef ENABLE_LOG
5460 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005461 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005462
5463 if (f != NULL)
5464 {
5465 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5466 fclose(f);
5467 }
5468 }
5469#endif
5470
5471 /*
5472 * PASS 1
5473 * Count number of NFA states in "nstate". Do not build the NFA.
5474 */
5475 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005476
5477 /* Space for compiled regexp */
5478 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5479 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5480 if (prog == NULL)
5481 goto fail;
5482 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005483 state_ptr = prog->state;
5484
5485 /*
5486 * PASS 2
5487 * Build the NFA
5488 */
5489 prog->start = post2nfa(postfix, post_ptr, FALSE);
5490 if (prog->start == NULL)
5491 goto fail;
5492
5493 prog->regflags = regflags;
5494 prog->engine = &nfa_regengine;
5495 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005496 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005497 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005498 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005499#ifdef ENABLE_LOG
5500 nfa_postfix_dump(expr, OK);
5501 nfa_dump(prog);
5502#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005503#ifdef FEAT_SYN_HL
5504 /* Remember whether this pattern has any \z specials in it. */
5505 prog->reghasz = re_has_z;
5506#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005507#ifdef DEBUG
5508 prog->pattern = vim_strsave(expr); /* memory will leak */
5509 nfa_regengine.expr = NULL;
5510#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005511
5512out:
5513 vim_free(post_start);
5514 post_start = post_ptr = post_end = NULL;
5515 state_ptr = NULL;
5516 return (regprog_T *)prog;
5517
5518fail:
5519 vim_free(prog);
5520 prog = NULL;
5521#ifdef ENABLE_LOG
5522 nfa_postfix_dump(expr, FAIL);
5523#endif
5524#ifdef DEBUG
5525 nfa_regengine.expr = NULL;
5526#endif
5527 goto out;
5528}
5529
5530
5531/*
5532 * Match a regexp against a string.
5533 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5534 * Uses curbuf for line count and 'iskeyword'.
5535 *
5536 * Return TRUE if there is a match, FALSE if not.
5537 */
5538 static int
5539nfa_regexec(rmp, line, col)
5540 regmatch_T *rmp;
5541 char_u *line; /* string to match against */
5542 colnr_T col; /* column to start looking for match */
5543{
5544 reg_match = rmp;
5545 reg_mmatch = NULL;
5546 reg_maxline = 0;
5547 reg_line_lbr = FALSE;
5548 reg_buf = curbuf;
5549 reg_win = NULL;
5550 ireg_ic = rmp->rm_ic;
5551#ifdef FEAT_MBYTE
5552 ireg_icombine = FALSE;
5553#endif
5554 ireg_maxcol = 0;
5555 return (nfa_regexec_both(line, col) != 0);
5556}
5557
5558#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5559 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5560
5561static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5562
5563/*
5564 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5565 */
5566 static int
5567nfa_regexec_nl(rmp, line, col)
5568 regmatch_T *rmp;
5569 char_u *line; /* string to match against */
5570 colnr_T col; /* column to start looking for match */
5571{
5572 reg_match = rmp;
5573 reg_mmatch = NULL;
5574 reg_maxline = 0;
5575 reg_line_lbr = TRUE;
5576 reg_buf = curbuf;
5577 reg_win = NULL;
5578 ireg_ic = rmp->rm_ic;
5579#ifdef FEAT_MBYTE
5580 ireg_icombine = FALSE;
5581#endif
5582 ireg_maxcol = 0;
5583 return (nfa_regexec_both(line, col) != 0);
5584}
5585#endif
5586
5587
5588/*
5589 * Match a regexp against multiple lines.
5590 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5591 * Uses curbuf for line count and 'iskeyword'.
5592 *
5593 * Return zero if there is no match. Return number of lines contained in the
5594 * match otherwise.
5595 *
5596 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5597 *
5598 * ! Also NOTE : match may actually be in another line. e.g.:
5599 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5600 *
5601 * +-------------------------+
5602 * |a |
5603 * |b |
5604 * |c |
5605 * | |
5606 * +-------------------------+
5607 *
5608 * then nfa_regexec_multi() returns 3. while the original
5609 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5610 *
5611 * FIXME if this behavior is not compatible.
5612 */
5613 static long
5614nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5615 regmmatch_T *rmp;
5616 win_T *win; /* window in which to search or NULL */
5617 buf_T *buf; /* buffer in which to search */
5618 linenr_T lnum; /* nr of line to start looking for match */
5619 colnr_T col; /* column to start looking for match */
5620 proftime_T *tm UNUSED; /* timeout limit or NULL */
5621{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005622 reg_match = NULL;
5623 reg_mmatch = rmp;
5624 reg_buf = buf;
5625 reg_win = win;
5626 reg_firstlnum = lnum;
5627 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5628 reg_line_lbr = FALSE;
5629 ireg_ic = rmp->rmm_ic;
5630#ifdef FEAT_MBYTE
5631 ireg_icombine = FALSE;
5632#endif
5633 ireg_maxcol = rmp->rmm_maxcol;
5634
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005635 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005636}
5637
5638#ifdef DEBUG
5639# undef ENABLE_LOG
5640#endif