blob: f795d3744fbfd77dfa34411e2877c560e24aff26 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020032enum
33{
34 NFA_SPLIT = -1024,
35 NFA_MATCH,
36 NFA_SKIP_CHAR, /* matches a 0-length char */
37 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
38
39 NFA_CONCAT,
40 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020041 NFA_STAR, /* greedy * */
42 NFA_STAR_NONGREEDY, /* non-greedy * */
43 NFA_QUEST, /* greedy \? */
44 NFA_QUEST_NONGREEDY, /* non-greedy \? */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020045 NFA_NOT, /* used for [^ab] negated char ranges */
46
47 NFA_BOL, /* ^ Begin line */
48 NFA_EOL, /* $ End line */
49 NFA_BOW, /* \< Begin word */
50 NFA_EOW, /* \> End word */
51 NFA_BOF, /* \%^ Begin file */
52 NFA_EOF, /* \%$ End file */
53 NFA_NEWL,
54 NFA_ZSTART, /* Used for \zs */
55 NFA_ZEND, /* Used for \ze */
56 NFA_NOPEN, /* Start of subexpression marked with \%( */
57 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
58 NFA_START_INVISIBLE,
Bram Moolenaar61602c52013-06-01 19:54:43 +020059 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020060 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020061 NFA_COMPOSING, /* Next nodes in NFA are part of the
62 composing multibyte char */
63 NFA_END_COMPOSING, /* End of a composing char in the NFA */
64
65 /* The following are used only in the postfix form, not in the NFA */
66 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
67 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
68 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
69 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
70 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
71
Bram Moolenaar5714b802013-05-28 22:03:20 +020072 NFA_BACKREF1, /* \1 */
73 NFA_BACKREF2, /* \2 */
74 NFA_BACKREF3, /* \3 */
75 NFA_BACKREF4, /* \4 */
76 NFA_BACKREF5, /* \5 */
77 NFA_BACKREF6, /* \6 */
78 NFA_BACKREF7, /* \7 */
79 NFA_BACKREF8, /* \8 */
80 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020081#ifdef FEAT_SYN_HL
82 NFA_ZREF1, /* \z1 */
83 NFA_ZREF2, /* \z2 */
84 NFA_ZREF3, /* \z3 */
85 NFA_ZREF4, /* \z4 */
86 NFA_ZREF5, /* \z5 */
87 NFA_ZREF6, /* \z6 */
88 NFA_ZREF7, /* \z7 */
89 NFA_ZREF8, /* \z8 */
90 NFA_ZREF9, /* \z9 */
91#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +020092 NFA_SKIP, /* Skip characters */
93
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020094 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +020095 NFA_MOPEN1,
96 NFA_MOPEN2,
97 NFA_MOPEN3,
98 NFA_MOPEN4,
99 NFA_MOPEN5,
100 NFA_MOPEN6,
101 NFA_MOPEN7,
102 NFA_MOPEN8,
103 NFA_MOPEN9,
104
105 NFA_MCLOSE,
106 NFA_MCLOSE1,
107 NFA_MCLOSE2,
108 NFA_MCLOSE3,
109 NFA_MCLOSE4,
110 NFA_MCLOSE5,
111 NFA_MCLOSE6,
112 NFA_MCLOSE7,
113 NFA_MCLOSE8,
114 NFA_MCLOSE9,
115
116#ifdef FEAT_SYN_HL
117 NFA_ZOPEN,
118 NFA_ZOPEN1,
119 NFA_ZOPEN2,
120 NFA_ZOPEN3,
121 NFA_ZOPEN4,
122 NFA_ZOPEN5,
123 NFA_ZOPEN6,
124 NFA_ZOPEN7,
125 NFA_ZOPEN8,
126 NFA_ZOPEN9,
127
128 NFA_ZCLOSE,
129 NFA_ZCLOSE1,
130 NFA_ZCLOSE2,
131 NFA_ZCLOSE3,
132 NFA_ZCLOSE4,
133 NFA_ZCLOSE5,
134 NFA_ZCLOSE6,
135 NFA_ZCLOSE7,
136 NFA_ZCLOSE8,
137 NFA_ZCLOSE9,
138#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200139
140 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200141 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200142 NFA_ANYOF, /* Match any character in this string. */
143 NFA_ANYBUT, /* Match any character not in this string. */
144 NFA_IDENT, /* Match identifier char */
145 NFA_SIDENT, /* Match identifier char but no digit */
146 NFA_KWORD, /* Match keyword char */
147 NFA_SKWORD, /* Match word char but no digit */
148 NFA_FNAME, /* Match file name char */
149 NFA_SFNAME, /* Match file name char but no digit */
150 NFA_PRINT, /* Match printable char */
151 NFA_SPRINT, /* Match printable char but no digit */
152 NFA_WHITE, /* Match whitespace char */
153 NFA_NWHITE, /* Match non-whitespace char */
154 NFA_DIGIT, /* Match digit char */
155 NFA_NDIGIT, /* Match non-digit char */
156 NFA_HEX, /* Match hex char */
157 NFA_NHEX, /* Match non-hex char */
158 NFA_OCTAL, /* Match octal char */
159 NFA_NOCTAL, /* Match non-octal char */
160 NFA_WORD, /* Match word char */
161 NFA_NWORD, /* Match non-word char */
162 NFA_HEAD, /* Match head char */
163 NFA_NHEAD, /* Match non-head char */
164 NFA_ALPHA, /* Match alpha char */
165 NFA_NALPHA, /* Match non-alpha char */
166 NFA_LOWER, /* Match lowercase char */
167 NFA_NLOWER, /* Match non-lowercase char */
168 NFA_UPPER, /* Match uppercase char */
169 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200170
171 NFA_CURSOR, /* Match cursor pos */
172 NFA_LNUM, /* Match line number */
173 NFA_LNUM_GT, /* Match > line number */
174 NFA_LNUM_LT, /* Match < line number */
175 NFA_COL, /* Match cursor column */
176 NFA_COL_GT, /* Match > cursor column */
177 NFA_COL_LT, /* Match < cursor column */
178 NFA_VCOL, /* Match cursor virtual column */
179 NFA_VCOL_GT, /* Match > cursor virtual column */
180 NFA_VCOL_LT, /* Match < cursor virtual column */
181
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200182 NFA_FIRST_NL = NFA_ANY + ADD_NL,
183 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
184
185 /* Character classes [:alnum:] etc */
186 NFA_CLASS_ALNUM,
187 NFA_CLASS_ALPHA,
188 NFA_CLASS_BLANK,
189 NFA_CLASS_CNTRL,
190 NFA_CLASS_DIGIT,
191 NFA_CLASS_GRAPH,
192 NFA_CLASS_LOWER,
193 NFA_CLASS_PRINT,
194 NFA_CLASS_PUNCT,
195 NFA_CLASS_SPACE,
196 NFA_CLASS_UPPER,
197 NFA_CLASS_XDIGIT,
198 NFA_CLASS_TAB,
199 NFA_CLASS_RETURN,
200 NFA_CLASS_BACKSPACE,
201 NFA_CLASS_ESCAPE
202};
203
204/* Keep in sync with classchars. */
205static int nfa_classcodes[] = {
206 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
207 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
208 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
209 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
210 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
211 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
212 NFA_UPPER, NFA_NUPPER
213};
214
215static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
216
217/*
218 * NFA errors can be of 3 types:
219 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
220 * silently and revert the to backtracking engine.
221 * syntax_error = FALSE;
222 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
223 * The NFA engine displays an error message, and nothing else happens.
224 * syntax_error = TRUE
225 * *** Unsupported features, when the input regexp uses an operator that is not
226 * implemented in the NFA. The NFA engine fails silently, and reverts to the
227 * old backtracking engine.
228 * syntax_error = FALSE
229 * "The NFA fails" means that "compiling the regexp with the NFA fails":
230 * nfa_regcomp() returns FAIL.
231 */
232static int syntax_error = FALSE;
233
234/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200235static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236
Bram Moolenaar428e9872013-05-30 17:05:39 +0200237/* NFA regexp \1 .. \9 encountered. */
238static int nfa_has_backref;
239
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200240#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200241/* NFA regexp has \z( ), set zsubexpr. */
242static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200243#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200244
Bram Moolenaar963fee22013-05-26 21:47:28 +0200245/* Number of sub expressions actually being used during execution. 1 if only
246 * the whole match (subexpr 0) is used. */
247static int nfa_nsubexpr;
248
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249static int *post_start; /* holds the postfix form of r.e. */
250static int *post_end;
251static int *post_ptr;
252
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200253static int nstate; /* Number of states in the NFA. Also used when
254 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200255static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200256
Bram Moolenaar307aa162013-06-02 16:34:21 +0200257/* If not NULL match must end at this position */
258static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200260/* listid is global, so that it increases on recursive calls to
261 * nfa_regmatch(), which means we don't have to clear the lastlist field of
262 * all the states. */
263static int nfa_listid;
264static int nfa_alt_listid;
265
266/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
267static int nfa_ll_index = 0;
268
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200269static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
270static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
271static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272static int nfa_regatom __ARGS((void));
273static int nfa_regpiece __ARGS((void));
274static int nfa_regconcat __ARGS((void));
275static int nfa_regbranch __ARGS((void));
276static int nfa_reg __ARGS((int paren));
277#ifdef DEBUG
278static void nfa_set_code __ARGS((int c));
279static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200280static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
281static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200282static void nfa_dump __ARGS((nfa_regprog_T *prog));
283#endif
284static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200285static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
287static int check_char_class __ARGS((int class, int c));
288static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200289static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
290static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200291static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200292static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200293static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
294static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
295static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
296static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
297
298/* helper functions used when doing re2post() ... regatom() parsing */
299#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200300 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200301 return FAIL; \
302 *post_ptr++ = c; \
303 } while (0)
304
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305/*
306 * Initialize internal variables before NFA compilation.
307 * Return OK on success, FAIL otherwise.
308 */
309 static int
310nfa_regcomp_start(expr, re_flags)
311 char_u *expr;
312 int re_flags; /* see vim_regcomp() */
313{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200314 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200315 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316
317 nstate = 0;
318 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200319 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200320 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200321
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200322 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200323 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200324 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200325
326 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200327 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200328
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200329 post_start = (int *)lalloc(postfix_size, TRUE);
330 if (post_start == NULL)
331 return FAIL;
332 vim_memset(post_start, 0, postfix_size);
333 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200334 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200335 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200336 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200337
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200338 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339 regcomp_start(expr, re_flags);
340
341 return OK;
342}
343
344/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200345 * Allocate more space for post_start. Called when
346 * running above the estimated number of states.
347 */
348 static int
349realloc_post_list()
350{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200351 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200352 int new_max = nstate_max + 1000;
353 int *new_start;
354 int *old_start;
355
356 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
357 if (new_start == NULL)
358 return FAIL;
359 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
360 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
361 old_start = post_start;
362 post_start = new_start;
363 post_ptr = new_start + (post_ptr - old_start);
364 post_end = post_start + new_max;
365 vim_free(old_start);
366 return OK;
367}
368
369/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200370 * Search between "start" and "end" and try to recognize a
371 * character class in expanded form. For example [0-9].
372 * On success, return the id the character class to be emitted.
373 * On failure, return 0 (=FAIL)
374 * Start points to the first char of the range, while end should point
375 * to the closing brace.
376 */
377 static int
378nfa_recognize_char_class(start, end, extra_newl)
379 char_u *start;
380 char_u *end;
381 int extra_newl;
382{
383 int i;
384 /* Each of these variables takes up a char in "config[]",
385 * in the order they are here. */
386 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
387 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
388 char_u *p;
389#define NCONFIGS 16
390 int classid[NCONFIGS] = {
391 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
392 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
393 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
394 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
395 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200396 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200397 char_u config[NCONFIGS][9] = {
398 "000000100", /* digit */
399 "100000100", /* non digit */
400 "011000100", /* hex-digit */
401 "111000100", /* non hex-digit */
402 "000001000", /* octal-digit */
403 "100001000", /* [^0-7] */
404 "000110110", /* [0-9A-Za-z_] */
405 "100110110", /* [^0-9A-Za-z_] */
406 "000110010", /* head of word */
407 "100110010", /* not head of word */
408 "000110000", /* alphabetic char a-z */
409 "100110000", /* non alphabetic char */
410 "000100000", /* lowercase letter */
411 "100100000", /* non lowercase */
412 "000010000", /* uppercase */
413 "100010000" /* non uppercase */
414 };
415
416 if (extra_newl == TRUE)
417 newl = TRUE;
418
419 if (*end != ']')
420 return FAIL;
421 p = start;
422 if (*p == '^')
423 {
424 not = TRUE;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200425 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200426 }
427
428 while (p < end)
429 {
430 if (p + 2 < end && *(p + 1) == '-')
431 {
432 switch (*p)
433 {
434 case '0':
435 if (*(p + 2) == '9')
436 {
437 o9 = TRUE;
438 break;
439 }
440 else
441 if (*(p + 2) == '7')
442 {
443 o7 = TRUE;
444 break;
445 }
446 case 'a':
447 if (*(p + 2) == 'z')
448 {
449 az = TRUE;
450 break;
451 }
452 else
453 if (*(p + 2) == 'f')
454 {
455 af = TRUE;
456 break;
457 }
458 case 'A':
459 if (*(p + 2) == 'Z')
460 {
461 AZ = TRUE;
462 break;
463 }
464 else
465 if (*(p + 2) == 'F')
466 {
467 AF = TRUE;
468 break;
469 }
470 /* FALLTHROUGH */
471 default:
472 return FAIL;
473 }
474 p += 3;
475 }
476 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
477 {
478 newl = TRUE;
479 p += 2;
480 }
481 else if (*p == '_')
482 {
483 underscore = TRUE;
484 p ++;
485 }
486 else if (*p == '\n')
487 {
488 newl = TRUE;
489 p ++;
490 }
491 else
492 return FAIL;
493 } /* while (p < end) */
494
495 if (p != end)
496 return FAIL;
497
498 /* build the config that represents the ranges we gathered */
499 STRCPY(myconfig, "000000000");
500 if (not == TRUE)
501 myconfig[0] = '1';
502 if (af == TRUE)
503 myconfig[1] = '1';
504 if (AF == TRUE)
505 myconfig[2] = '1';
506 if (az == TRUE)
507 myconfig[3] = '1';
508 if (AZ == TRUE)
509 myconfig[4] = '1';
510 if (o7 == TRUE)
511 myconfig[5] = '1';
512 if (o9 == TRUE)
513 myconfig[6] = '1';
514 if (underscore == TRUE)
515 myconfig[7] = '1';
516 if (newl == TRUE)
517 {
518 myconfig[8] = '1';
519 extra_newl = ADD_NL;
520 }
521 /* try to recognize character classes */
522 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200523 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200524 return classid[i] + extra_newl;
525
526 /* fallthrough => no success so far */
527 return FAIL;
528
529#undef NCONFIGS
530}
531
532/*
533 * Produce the bytes for equivalence class "c".
534 * Currently only handles latin1, latin9 and utf-8.
535 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
536 * equivalent to 'a OR b OR c'
537 *
538 * NOTE! When changing this function, also update reg_equi_class()
539 */
540 static int
541nfa_emit_equi_class(c, neg)
542 int c;
543 int neg;
544{
545 int first = TRUE;
546 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
547#define EMIT2(c) \
548 EMIT(c); \
549 if (neg == TRUE) { \
550 EMIT(NFA_NOT); \
551 } \
552 if (first == FALSE) \
553 EMIT(glue); \
554 else \
555 first = FALSE; \
556
557#ifdef FEAT_MBYTE
558 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
559 || STRCMP(p_enc, "iso-8859-15") == 0)
560#endif
561 {
562 switch (c)
563 {
564 case 'A': case '\300': case '\301': case '\302':
565 case '\303': case '\304': case '\305':
566 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
567 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
568 EMIT2('\305');
569 return OK;
570
571 case 'C': case '\307':
572 EMIT2('C'); EMIT2('\307');
573 return OK;
574
575 case 'E': case '\310': case '\311': case '\312': case '\313':
576 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
577 EMIT2('\312'); EMIT2('\313');
578 return OK;
579
580 case 'I': case '\314': case '\315': case '\316': case '\317':
581 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
582 EMIT2('\316'); EMIT2('\317');
583 return OK;
584
585 case 'N': case '\321':
586 EMIT2('N'); EMIT2('\321');
587 return OK;
588
589 case 'O': case '\322': case '\323': case '\324': case '\325':
590 case '\326':
591 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
592 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
593 return OK;
594
595 case 'U': case '\331': case '\332': case '\333': case '\334':
596 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
597 EMIT2('\333'); EMIT2('\334');
598 return OK;
599
600 case 'Y': case '\335':
601 EMIT2('Y'); EMIT2('\335');
602 return OK;
603
604 case 'a': case '\340': case '\341': case '\342':
605 case '\343': case '\344': case '\345':
606 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
607 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
608 EMIT2('\345');
609 return OK;
610
611 case 'c': case '\347':
612 EMIT2('c'); EMIT2('\347');
613 return OK;
614
615 case 'e': case '\350': case '\351': case '\352': case '\353':
616 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
617 EMIT2('\352'); EMIT2('\353');
618 return OK;
619
620 case 'i': case '\354': case '\355': case '\356': case '\357':
621 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
622 EMIT2('\356'); EMIT2('\357');
623 return OK;
624
625 case 'n': case '\361':
626 EMIT2('n'); EMIT2('\361');
627 return OK;
628
629 case 'o': case '\362': case '\363': case '\364': case '\365':
630 case '\366':
631 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
632 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
633 return OK;
634
635 case 'u': case '\371': case '\372': case '\373': case '\374':
636 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
637 EMIT2('\373'); EMIT2('\374');
638 return OK;
639
640 case 'y': case '\375': case '\377':
641 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
642 return OK;
643
644 default:
645 return FAIL;
646 }
647 }
648
649 EMIT(c);
650 return OK;
651#undef EMIT2
652}
653
654/*
655 * Code to parse regular expression.
656 *
657 * We try to reuse parsing functions in regexp.c to
658 * minimize surprise and keep the syntax consistent.
659 */
660
661/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200662 * Parse the lowest level.
663 *
664 * An atom can be one of a long list of items. Many atoms match one character
665 * in the text. It is often an ordinary character or a character class.
666 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
667 * is only for syntax highlighting.
668 *
669 * atom ::= ordinary-atom
670 * or \( pattern \)
671 * or \%( pattern \)
672 * or \z( pattern \)
673 */
674 static int
675nfa_regatom()
676{
677 int c;
678 int charclass;
679 int equiclass;
680 int collclass;
681 int got_coll_char;
682 char_u *p;
683 char_u *endp;
684#ifdef FEAT_MBYTE
685 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200686#endif
687 int extra = 0;
688 int first;
689 int emit_range;
690 int negated;
691 int result;
692 int startc = -1;
693 int endc = -1;
694 int oldstartc = -1;
695 int cpo_lit; /* 'cpoptions' contains 'l' flag */
696 int cpo_bsl; /* 'cpoptions' contains '\' flag */
697 int glue; /* ID that will "glue" nodes together */
698
699 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
700 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
701
702 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200703 switch (c)
704 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200705 case NUL:
706 syntax_error = TRUE;
707 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
708
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200709 case Magic('^'):
710 EMIT(NFA_BOL);
711 break;
712
713 case Magic('$'):
714 EMIT(NFA_EOL);
715#if defined(FEAT_SYN_HL) || defined(PROTO)
716 had_eol = TRUE;
717#endif
718 break;
719
720 case Magic('<'):
721 EMIT(NFA_BOW);
722 break;
723
724 case Magic('>'):
725 EMIT(NFA_EOW);
726 break;
727
728 case Magic('_'):
729 c = no_Magic(getchr());
730 if (c == '^') /* "\_^" is start-of-line */
731 {
732 EMIT(NFA_BOL);
733 break;
734 }
735 if (c == '$') /* "\_$" is end-of-line */
736 {
737 EMIT(NFA_EOL);
738#if defined(FEAT_SYN_HL) || defined(PROTO)
739 had_eol = TRUE;
740#endif
741 break;
742 }
743
744 extra = ADD_NL;
745
746 /* "\_[" is collection plus newline */
747 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200748 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749
750 /* "\_x" is character class plus newline */
751 /*FALLTHROUGH*/
752
753 /*
754 * Character classes.
755 */
756 case Magic('.'):
757 case Magic('i'):
758 case Magic('I'):
759 case Magic('k'):
760 case Magic('K'):
761 case Magic('f'):
762 case Magic('F'):
763 case Magic('p'):
764 case Magic('P'):
765 case Magic('s'):
766 case Magic('S'):
767 case Magic('d'):
768 case Magic('D'):
769 case Magic('x'):
770 case Magic('X'):
771 case Magic('o'):
772 case Magic('O'):
773 case Magic('w'):
774 case Magic('W'):
775 case Magic('h'):
776 case Magic('H'):
777 case Magic('a'):
778 case Magic('A'):
779 case Magic('l'):
780 case Magic('L'):
781 case Magic('u'):
782 case Magic('U'):
783 p = vim_strchr(classchars, no_Magic(c));
784 if (p == NULL)
785 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200786 EMSGN("INTERNAL: Unknown character class char: %ld", c);
787 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 }
789#ifdef FEAT_MBYTE
790 /* When '.' is followed by a composing char ignore the dot, so that
791 * the composing char is matched here. */
792 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
793 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200794 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200795 c = getchr();
796 goto nfa_do_multibyte;
797 }
798#endif
799 EMIT(nfa_classcodes[p - classchars]);
800 if (extra == ADD_NL)
801 {
802 EMIT(NFA_NEWL);
803 EMIT(NFA_OR);
804 regflags |= RF_HASNL;
805 }
806 break;
807
808 case Magic('n'):
809 if (reg_string)
810 /* In a string "\n" matches a newline character. */
811 EMIT(NL);
812 else
813 {
814 /* In buffer text "\n" matches the end of a line. */
815 EMIT(NFA_NEWL);
816 regflags |= RF_HASNL;
817 }
818 break;
819
820 case Magic('('):
821 if (nfa_reg(REG_PAREN) == FAIL)
822 return FAIL; /* cascaded error */
823 break;
824
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200825 case Magic('|'):
826 case Magic('&'):
827 case Magic(')'):
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
832 case Magic('='):
833 case Magic('?'):
834 case Magic('+'):
835 case Magic('@'):
836 case Magic('*'):
837 case Magic('{'):
838 /* these should follow an atom, not form an atom */
839 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200840 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200841 return FAIL;
842
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200843 case Magic('~'):
844 {
845 char_u *lp;
846
847 /* Previous substitute pattern.
848 * Generated as "\%(pattern\)". */
849 if (reg_prev_sub == NULL)
850 {
851 EMSG(_(e_nopresub));
852 return FAIL;
853 }
854 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
855 {
856 EMIT(PTR2CHAR(lp));
857 if (lp != reg_prev_sub)
858 EMIT(NFA_CONCAT);
859 }
860 EMIT(NFA_NOPEN);
861 break;
862 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200863
Bram Moolenaar428e9872013-05-30 17:05:39 +0200864 case Magic('1'):
865 case Magic('2'):
866 case Magic('3'):
867 case Magic('4'):
868 case Magic('5'):
869 case Magic('6'):
870 case Magic('7'):
871 case Magic('8'):
872 case Magic('9'):
873 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
874 nfa_has_backref = TRUE;
875 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200876
877 case Magic('z'):
878 c = no_Magic(getchr());
879 switch (c)
880 {
881 case 's':
882 EMIT(NFA_ZSTART);
883 break;
884 case 'e':
885 EMIT(NFA_ZEND);
886 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200887 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200888#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200889 case '1':
890 case '2':
891 case '3':
892 case '4':
893 case '5':
894 case '6':
895 case '7':
896 case '8':
897 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200898 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200899 if (reg_do_extmatch != REX_USE)
900 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200901 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
902 /* No need to set nfa_has_backref, the sub-matches don't
903 * change when \z1 .. \z9 maches or not. */
904 re_has_z = REX_USE;
905 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200907 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200908 if (reg_do_extmatch != REX_SET)
909 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200910 if (nfa_reg(REG_ZPAREN) == FAIL)
911 return FAIL; /* cascaded error */
912 re_has_z = REX_SET;
913 break;
914#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200915 default:
916 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200917 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200918 no_Magic(c));
919 return FAIL;
920 }
921 break;
922
923 case Magic('%'):
924 c = no_Magic(getchr());
925 switch (c)
926 {
927 /* () without a back reference */
928 case '(':
929 if (nfa_reg(REG_NPAREN) == FAIL)
930 return FAIL;
931 EMIT(NFA_NOPEN);
932 break;
933
934 case 'd': /* %d123 decimal */
935 case 'o': /* %o123 octal */
936 case 'x': /* %xab hex 2 */
937 case 'u': /* %uabcd hex 4 */
938 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200939 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200940 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200941
Bram Moolenaar47196582013-05-25 22:04:23 +0200942 switch (c)
943 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200944 case 'd': nr = getdecchrs(); break;
945 case 'o': nr = getoctchrs(); break;
946 case 'x': nr = gethexchrs(2); break;
947 case 'u': nr = gethexchrs(4); break;
948 case 'U': nr = gethexchrs(8); break;
949 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200950 }
951
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200952 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200953 EMSG2_RET_FAIL(
954 _("E678: Invalid character after %s%%[dxouU]"),
955 reg_magic == MAGIC_ALL);
956 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200957 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200958 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200959 break;
960
961 /* Catch \%^ and \%$ regardless of where they appear in the
962 * pattern -- regardless of whether or not it makes sense. */
963 case '^':
964 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 break;
966
967 case '$':
968 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200969 break;
970
971 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200972 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200973 break;
974
975 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200976 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200977 return FAIL;
978 break;
979
980 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200981 /* TODO: \%[abc] not supported yet */
982 return FAIL;
983
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200984 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200985 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200986 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200987 int cmp = c;
988
989 if (c == '<' || c == '>')
990 c = getchr();
991 while (VIM_ISDIGIT(c))
992 {
993 n = n * 10 + (c - '0');
994 c = getchr();
995 }
996 if (c == 'l' || c == 'c' || c == 'v')
997 {
998 EMIT(n);
999 if (c == 'l')
1000 EMIT(cmp == '<' ? NFA_LNUM_LT :
1001 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
1002 else if (c == 'c')
1003 EMIT(cmp == '<' ? NFA_COL_LT :
1004 cmp == '>' ? NFA_COL_GT : NFA_COL);
1005 else
1006 EMIT(cmp == '<' ? NFA_VCOL_LT :
1007 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
1008 break;
1009 }
1010 else if (c == '\'')
1011 /* TODO: \%'m not supported yet */
1012 return FAIL;
1013 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001014 syntax_error = TRUE;
1015 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1016 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001017 return FAIL;
1018 }
1019 break;
1020
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001021 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001022collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001023 /*
1024 * Glue is emitted between several atoms from the [].
1025 * It is either NFA_OR, or NFA_CONCAT.
1026 *
1027 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1028 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1029 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1030 * notation)
1031 *
1032 */
1033
1034
1035/* Emit negation atoms, if needed.
1036 * The CONCAT below merges the NOT with the previous node. */
1037#define TRY_NEG() \
1038 if (negated == TRUE) \
1039 { \
1040 EMIT(NFA_NOT); \
1041 }
1042
1043/* Emit glue between important nodes : CONCAT or OR. */
1044#define EMIT_GLUE() \
1045 if (first == FALSE) \
1046 EMIT(glue); \
1047 else \
1048 first = FALSE;
1049
1050 p = regparse;
1051 endp = skip_anyof(p);
1052 if (*endp == ']')
1053 {
1054 /*
1055 * Try to reverse engineer character classes. For example,
1056 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1057 * and perform the necessary substitutions in the NFA.
1058 */
1059 result = nfa_recognize_char_class(regparse, endp,
1060 extra == ADD_NL);
1061 if (result != FAIL)
1062 {
1063 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1064 EMIT(result);
1065 else /* must be char class + newline */
1066 {
1067 EMIT(result - ADD_NL);
1068 EMIT(NFA_NEWL);
1069 EMIT(NFA_OR);
1070 }
1071 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001072 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001073 return OK;
1074 }
1075 /*
1076 * Failed to recognize a character class. Use the simple
1077 * version that turns [abc] into 'a' OR 'b' OR 'c'
1078 */
1079 startc = endc = oldstartc = -1;
1080 first = TRUE; /* Emitting first atom in this sequence? */
1081 negated = FALSE;
1082 glue = NFA_OR;
1083 if (*regparse == '^') /* negated range */
1084 {
1085 negated = TRUE;
1086 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001087 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001088 }
1089 if (*regparse == '-')
1090 {
1091 startc = '-';
1092 EMIT(startc);
1093 TRY_NEG();
1094 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001095 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001096 }
1097 /* Emit the OR branches for each character in the [] */
1098 emit_range = FALSE;
1099 while (regparse < endp)
1100 {
1101 oldstartc = startc;
1102 startc = -1;
1103 got_coll_char = FALSE;
1104 if (*regparse == '[')
1105 {
1106 /* Check for [: :], [= =], [. .] */
1107 equiclass = collclass = 0;
1108 charclass = get_char_class(&regparse);
1109 if (charclass == CLASS_NONE)
1110 {
1111 equiclass = get_equi_class(&regparse);
1112 if (equiclass == 0)
1113 collclass = get_coll_element(&regparse);
1114 }
1115
1116 /* Character class like [:alpha:] */
1117 if (charclass != CLASS_NONE)
1118 {
1119 switch (charclass)
1120 {
1121 case CLASS_ALNUM:
1122 EMIT(NFA_CLASS_ALNUM);
1123 break;
1124 case CLASS_ALPHA:
1125 EMIT(NFA_CLASS_ALPHA);
1126 break;
1127 case CLASS_BLANK:
1128 EMIT(NFA_CLASS_BLANK);
1129 break;
1130 case CLASS_CNTRL:
1131 EMIT(NFA_CLASS_CNTRL);
1132 break;
1133 case CLASS_DIGIT:
1134 EMIT(NFA_CLASS_DIGIT);
1135 break;
1136 case CLASS_GRAPH:
1137 EMIT(NFA_CLASS_GRAPH);
1138 break;
1139 case CLASS_LOWER:
1140 EMIT(NFA_CLASS_LOWER);
1141 break;
1142 case CLASS_PRINT:
1143 EMIT(NFA_CLASS_PRINT);
1144 break;
1145 case CLASS_PUNCT:
1146 EMIT(NFA_CLASS_PUNCT);
1147 break;
1148 case CLASS_SPACE:
1149 EMIT(NFA_CLASS_SPACE);
1150 break;
1151 case CLASS_UPPER:
1152 EMIT(NFA_CLASS_UPPER);
1153 break;
1154 case CLASS_XDIGIT:
1155 EMIT(NFA_CLASS_XDIGIT);
1156 break;
1157 case CLASS_TAB:
1158 EMIT(NFA_CLASS_TAB);
1159 break;
1160 case CLASS_RETURN:
1161 EMIT(NFA_CLASS_RETURN);
1162 break;
1163 case CLASS_BACKSPACE:
1164 EMIT(NFA_CLASS_BACKSPACE);
1165 break;
1166 case CLASS_ESCAPE:
1167 EMIT(NFA_CLASS_ESCAPE);
1168 break;
1169 }
1170 TRY_NEG();
1171 EMIT_GLUE();
1172 continue;
1173 }
1174 /* Try equivalence class [=a=] and the like */
1175 if (equiclass != 0)
1176 {
1177 result = nfa_emit_equi_class(equiclass, negated);
1178 if (result == FAIL)
1179 {
1180 /* should never happen */
1181 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1182 }
1183 EMIT_GLUE();
1184 continue;
1185 }
1186 /* Try collating class like [. .] */
1187 if (collclass != 0)
1188 {
1189 startc = collclass; /* allow [.a.]-x as a range */
1190 /* Will emit the proper atom at the end of the
1191 * while loop. */
1192 }
1193 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001194 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1195 * start character. */
1196 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001197 {
1198 emit_range = TRUE;
1199 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001200 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 continue; /* reading the end of the range */
1202 }
1203
1204 /* Now handle simple and escaped characters.
1205 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1206 * accepts "\t", "\e", etc., but only when the 'l' flag in
1207 * 'cpoptions' is not included.
1208 * Posix doesn't recognize backslash at all.
1209 */
1210 if (*regparse == '\\'
1211 && !cpo_bsl
1212 && regparse + 1 <= endp
1213 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1214 || (!cpo_lit
1215 && vim_strchr(REGEXP_ABBR, regparse[1])
1216 != NULL)
1217 )
1218 )
1219 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001220 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001222 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 startc = reg_string ? NL : NFA_NEWL;
1224 else
1225 if (*regparse == 'd'
1226 || *regparse == 'o'
1227 || *regparse == 'x'
1228 || *regparse == 'u'
1229 || *regparse == 'U'
1230 )
1231 {
1232 /* TODO(RE) This needs more testing */
1233 startc = coll_get_char();
1234 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001235 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001236 }
1237 else
1238 {
1239 /* \r,\t,\e,\b */
1240 startc = backslash_trans(*regparse);
1241 }
1242 }
1243
1244 /* Normal printable char */
1245 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001246 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001247
1248 /* Previous char was '-', so this char is end of range. */
1249 if (emit_range)
1250 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001251 endc = startc;
1252 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253 if (startc > endc)
1254 EMSG_RET_FAIL(_(e_invrange));
1255#ifdef FEAT_MBYTE
1256 if (has_mbyte && ((*mb_char2len)(startc) > 1
1257 || (*mb_char2len)(endc) > 1))
1258 {
1259 if (endc > startc + 256)
1260 EMSG_RET_FAIL(_(e_invrange));
1261 /* Emit the range. "startc" was already emitted, so
1262 * skip it. */
1263 for (c = startc + 1; c <= endc; c++)
1264 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001265 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001266 TRY_NEG();
1267 EMIT_GLUE();
1268 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269 }
1270 else
1271#endif
1272 {
1273#ifdef EBCDIC
1274 int alpha_only = FALSE;
1275
1276 /* for alphabetical range skip the gaps
1277 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1278 if (isalpha(startc) && isalpha(endc))
1279 alpha_only = TRUE;
1280#endif
1281 /* Emit the range. "startc" was already emitted, so
1282 * skip it. */
1283 for (c = startc + 1; c <= endc; c++)
1284#ifdef EBCDIC
1285 if (!alpha_only || isalpha(startc))
1286#endif
1287 {
1288 EMIT(c);
1289 TRY_NEG();
1290 EMIT_GLUE();
1291 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001292 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001293 emit_range = FALSE;
1294 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001295 }
1296 else
1297 {
1298 /*
1299 * This char (startc) is not part of a range. Just
1300 * emit it.
1301 *
1302 * Normally, simply emit startc. But if we get char
1303 * code=0 from a collating char, then replace it with
1304 * 0x0a.
1305 *
1306 * This is needed to completely mimic the behaviour of
1307 * the backtracking engine.
1308 */
1309 if (got_coll_char == TRUE && startc == 0)
1310 EMIT(0x0a);
1311 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001312 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001313 TRY_NEG();
1314 EMIT_GLUE();
1315 }
1316
Bram Moolenaar51a29832013-05-28 22:30:35 +02001317 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 } /* while (p < endp) */
1319
Bram Moolenaar51a29832013-05-28 22:30:35 +02001320 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321 if (*regparse == '-') /* if last, '-' is just a char */
1322 {
1323 EMIT('-');
1324 TRY_NEG();
1325 EMIT_GLUE();
1326 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001327 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001328
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001329 /* skip the trailing ] */
1330 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001331 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001332 if (negated == TRUE)
1333 {
1334 /* Mark end of negated char range */
1335 EMIT(NFA_END_NEG_RANGE);
1336 EMIT(NFA_CONCAT);
1337 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001338
1339 /* \_[] also matches \n but it's not negated */
1340 if (extra == ADD_NL)
1341 {
1342 EMIT(reg_string ? NL : NFA_NEWL);
1343 EMIT(NFA_OR);
1344 }
1345
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001346 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001347 } /* if exists closing ] */
1348
1349 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001350 {
1351 syntax_error = TRUE;
1352 EMSG_RET_FAIL(_(e_missingbracket));
1353 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001354 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001356 default:
1357 {
1358#ifdef FEAT_MBYTE
1359 int plen;
1360
1361nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001362 /* plen is length of current char with composing chars */
1363 if (enc_utf8 && ((*mb_char2len)(c)
1364 != (plen = (*mb_ptr2len)(old_regparse))
1365 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001367 int i = 0;
1368
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001369 /* A base character plus composing characters, or just one
1370 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001371 * This requires creating a separate atom as if enclosing
1372 * the characters in (), where NFA_COMPOSING is the ( and
1373 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001374 * building the postfix form, not the NFA itself;
1375 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001376 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001377 for (;;)
1378 {
1379 EMIT(c);
1380 if (i > 0)
1381 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001382 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001383 break;
1384 c = utf_ptr2char(old_regparse + i);
1385 }
1386 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001387 regparse = old_regparse + plen;
1388 }
1389 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390#endif
1391 {
1392 c = no_Magic(c);
1393 EMIT(c);
1394 }
1395 return OK;
1396 }
1397 }
1398
1399#undef TRY_NEG
1400#undef EMIT_GLUE
1401
1402 return OK;
1403}
1404
1405/*
1406 * Parse something followed by possible [*+=].
1407 *
1408 * A piece is an atom, possibly followed by a multi, an indication of how many
1409 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1410 * characters: "", "a", "aa", etc.
1411 *
1412 * piece ::= atom
1413 * or atom multi
1414 */
1415 static int
1416nfa_regpiece()
1417{
1418 int i;
1419 int op;
1420 int ret;
1421 long minval, maxval;
1422 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001423 parse_state_T old_state;
1424 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001425 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001426 int old_post_pos;
1427 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001428 int quest;
1429
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001430 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1431 * next. */
1432 save_parse_state(&old_state);
1433
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001435 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001436
1437 ret = nfa_regatom();
1438 if (ret == FAIL)
1439 return FAIL; /* cascaded error */
1440
1441 op = peekchr();
1442 if (re_multi_type(op) == NOT_MULTI)
1443 return OK;
1444
1445 skipchr();
1446 switch (op)
1447 {
1448 case Magic('*'):
1449 EMIT(NFA_STAR);
1450 break;
1451
1452 case Magic('+'):
1453 /*
1454 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1455 * first and only submatch would be "aaa". But the backtracking
1456 * engine interprets the plus as "try matching one more time", and
1457 * a* matches a second time at the end of the input, the empty
1458 * string.
1459 * The submatch will the empty string.
1460 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001461 * In order to be consistent with the old engine, we replace
1462 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001463 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001464 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 curchr = -1;
1466 if (nfa_regatom() == FAIL)
1467 return FAIL;
1468 EMIT(NFA_STAR);
1469 EMIT(NFA_CONCAT);
1470 skipchr(); /* skip the \+ */
1471 break;
1472
1473 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001474 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001476 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 switch(op)
1478 {
1479 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001480 /* \@= */
1481 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001482 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001483 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001484 /* \@! */
1485 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001486 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001488 op = no_Magic(getchr());
1489 if (op == '=')
1490 /* \@<= */
1491 i = NFA_PREV_ATOM_JUST_BEFORE;
1492 else if (op == '!')
1493 /* \@<! */
1494 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1495 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001497 /* \@> Not supported yet */
1498 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 return FAIL;
1500 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001501 if (i == 0)
1502 {
1503 syntax_error = TRUE;
1504 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1505 return FAIL;
1506 }
1507 EMIT(i);
1508 if (i == NFA_PREV_ATOM_JUST_BEFORE
1509 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1510 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 break;
1512
1513 case Magic('?'):
1514 case Magic('='):
1515 EMIT(NFA_QUEST);
1516 break;
1517
1518 case Magic('{'):
1519 /* a{2,5} will expand to 'aaa?a?a?'
1520 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1521 * version of '?'
1522 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1523 * parenthesis have the same id
1524 */
1525
1526 greedy = TRUE;
1527 c2 = peekchr();
1528 if (c2 == '-' || c2 == Magic('-'))
1529 {
1530 skipchr();
1531 greedy = FALSE;
1532 }
1533 if (!read_limits(&minval, &maxval))
1534 {
1535 syntax_error = TRUE;
1536 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1537 }
1538 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1539 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001540 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001541 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001542 if (greedy)
1543 /* \{}, \{0,} */
1544 EMIT(NFA_STAR);
1545 else
1546 /* \{-}, \{-0,} */
1547 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001548 break;
1549 }
1550
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 /* Special case: x{0} or x{-0} */
1552 if (maxval == 0)
1553 {
1554 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001555 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001556 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1557 EMIT(NFA_SKIP_CHAR);
1558 return OK;
1559 }
1560
1561 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001562 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001563 /* Save parse state after the repeated atom and the \{} */
1564 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001565
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001566 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1567 for (i = 0; i < maxval; i++)
1568 {
1569 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001570 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001571 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001572 if (nfa_regatom() == FAIL)
1573 return FAIL;
1574 /* after "minval" times, atoms are optional */
1575 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001576 {
1577 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001578 {
1579 if (greedy)
1580 EMIT(NFA_STAR);
1581 else
1582 EMIT(NFA_STAR_NONGREEDY);
1583 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001584 else
1585 EMIT(quest);
1586 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001587 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001588 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001589 if (i + 1 > minval && maxval == MAX_LIMIT)
1590 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001591 }
1592
1593 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001594 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001595 curchr = -1;
1596
1597 break;
1598
1599
1600 default:
1601 break;
1602 } /* end switch */
1603
1604 if (re_multi_type(peekchr()) != NOT_MULTI)
1605 {
1606 /* Can't have a multi follow a multi. */
1607 syntax_error = TRUE;
1608 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1609 }
1610
1611 return OK;
1612}
1613
1614/*
1615 * Parse one or more pieces, concatenated. It matches a match for the
1616 * first piece, followed by a match for the second piece, etc. Example:
1617 * "f[0-9]b", first matches "f", then a digit and then "b".
1618 *
1619 * concat ::= piece
1620 * or piece piece
1621 * or piece piece piece
1622 * etc.
1623 */
1624 static int
1625nfa_regconcat()
1626{
1627 int cont = TRUE;
1628 int first = TRUE;
1629
1630 while (cont)
1631 {
1632 switch (peekchr())
1633 {
1634 case NUL:
1635 case Magic('|'):
1636 case Magic('&'):
1637 case Magic(')'):
1638 cont = FALSE;
1639 break;
1640
1641 case Magic('Z'):
1642#ifdef FEAT_MBYTE
1643 regflags |= RF_ICOMBINE;
1644#endif
1645 skipchr_keepstart();
1646 break;
1647 case Magic('c'):
1648 regflags |= RF_ICASE;
1649 skipchr_keepstart();
1650 break;
1651 case Magic('C'):
1652 regflags |= RF_NOICASE;
1653 skipchr_keepstart();
1654 break;
1655 case Magic('v'):
1656 reg_magic = MAGIC_ALL;
1657 skipchr_keepstart();
1658 curchr = -1;
1659 break;
1660 case Magic('m'):
1661 reg_magic = MAGIC_ON;
1662 skipchr_keepstart();
1663 curchr = -1;
1664 break;
1665 case Magic('M'):
1666 reg_magic = MAGIC_OFF;
1667 skipchr_keepstart();
1668 curchr = -1;
1669 break;
1670 case Magic('V'):
1671 reg_magic = MAGIC_NONE;
1672 skipchr_keepstart();
1673 curchr = -1;
1674 break;
1675
1676 default:
1677 if (nfa_regpiece() == FAIL)
1678 return FAIL;
1679 if (first == FALSE)
1680 EMIT(NFA_CONCAT);
1681 else
1682 first = FALSE;
1683 break;
1684 }
1685 }
1686
1687 return OK;
1688}
1689
1690/*
1691 * Parse a branch, one or more concats, separated by "\&". It matches the
1692 * last concat, but only if all the preceding concats also match at the same
1693 * position. Examples:
1694 * "foobeep\&..." matches "foo" in "foobeep".
1695 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1696 *
1697 * branch ::= concat
1698 * or concat \& concat
1699 * or concat \& concat \& concat
1700 * etc.
1701 */
1702 static int
1703nfa_regbranch()
1704{
1705 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001706 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707
Bram Moolenaar16299b52013-05-30 18:45:23 +02001708 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001709
1710 /* First branch, possibly the only one */
1711 if (nfa_regconcat() == FAIL)
1712 return FAIL;
1713
1714 ch = peekchr();
1715 /* Try next concats */
1716 while (ch == Magic('&'))
1717 {
1718 skipchr();
1719 EMIT(NFA_NOPEN);
1720 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001721 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001722 if (nfa_regconcat() == FAIL)
1723 return FAIL;
1724 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001725 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001726 EMIT(NFA_SKIP_CHAR);
1727 EMIT(NFA_CONCAT);
1728 ch = peekchr();
1729 }
1730
1731 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001732 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001733 EMIT(NFA_SKIP_CHAR);
1734
1735 return OK;
1736}
1737
1738/*
1739 * Parse a pattern, one or more branches, separated by "\|". It matches
1740 * anything that matches one of the branches. Example: "foo\|beep" matches
1741 * "foo" and matches "beep". If more than one branch matches, the first one
1742 * is used.
1743 *
1744 * pattern ::= branch
1745 * or branch \| branch
1746 * or branch \| branch \| branch
1747 * etc.
1748 */
1749 static int
1750nfa_reg(paren)
1751 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1752{
1753 int parno = 0;
1754
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001755 if (paren == REG_PAREN)
1756 {
1757 if (regnpar >= NSUBEXP) /* Too many `(' */
1758 {
1759 syntax_error = TRUE;
1760 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1761 }
1762 parno = regnpar++;
1763 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001764#ifdef FEAT_SYN_HL
1765 else if (paren == REG_ZPAREN)
1766 {
1767 /* Make a ZOPEN node. */
1768 if (regnzpar >= NSUBEXP)
1769 {
1770 syntax_error = TRUE;
1771 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1772 }
1773 parno = regnzpar++;
1774 }
1775#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001776
1777 if (nfa_regbranch() == FAIL)
1778 return FAIL; /* cascaded error */
1779
1780 while (peekchr() == Magic('|'))
1781 {
1782 skipchr();
1783 if (nfa_regbranch() == FAIL)
1784 return FAIL; /* cascaded error */
1785 EMIT(NFA_OR);
1786 }
1787
1788 /* Check for proper termination. */
1789 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1790 {
1791 syntax_error = TRUE;
1792 if (paren == REG_NPAREN)
1793 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1794 else
1795 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1796 }
1797 else if (paren == REG_NOPAREN && peekchr() != NUL)
1798 {
1799 syntax_error = TRUE;
1800 if (peekchr() == Magic(')'))
1801 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1802 else
1803 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1804 }
1805 /*
1806 * Here we set the flag allowing back references to this set of
1807 * parentheses.
1808 */
1809 if (paren == REG_PAREN)
1810 {
1811 had_endbrace[parno] = TRUE; /* have seen the close paren */
1812 EMIT(NFA_MOPEN + parno);
1813 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001814#ifdef FEAT_SYN_HL
1815 else if (paren == REG_ZPAREN)
1816 EMIT(NFA_ZOPEN + parno);
1817#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001818
1819 return OK;
1820}
1821
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001822#ifdef DEBUG
1823static char_u code[50];
1824
1825 static void
1826nfa_set_code(c)
1827 int c;
1828{
1829 int addnl = FALSE;
1830
1831 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1832 {
1833 addnl = TRUE;
1834 c -= ADD_NL;
1835 }
1836
1837 STRCPY(code, "");
1838 switch (c)
1839 {
1840 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1841 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1842 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1843 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1844 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1845 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1846
Bram Moolenaar5714b802013-05-28 22:03:20 +02001847 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1848 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1849 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1850 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1851 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1852 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1853 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1854 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1855 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001856#ifdef FEAT_SYN_HL
1857 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1858 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1859 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1860 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1861 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1862 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1863 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1864 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1865 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1866#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001867 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1868
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 case NFA_PREV_ATOM_NO_WIDTH:
1870 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001871 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1872 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001873 case NFA_PREV_ATOM_JUST_BEFORE:
1874 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1875 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1876 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001877 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1878 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001879 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001880 case NFA_START_INVISIBLE_BEFORE:
1881 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1883
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1885 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1886
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001887 case NFA_MOPEN:
1888 case NFA_MOPEN1:
1889 case NFA_MOPEN2:
1890 case NFA_MOPEN3:
1891 case NFA_MOPEN4:
1892 case NFA_MOPEN5:
1893 case NFA_MOPEN6:
1894 case NFA_MOPEN7:
1895 case NFA_MOPEN8:
1896 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 STRCPY(code, "NFA_MOPEN(x)");
1898 code[10] = c - NFA_MOPEN + '0';
1899 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001900 case NFA_MCLOSE:
1901 case NFA_MCLOSE1:
1902 case NFA_MCLOSE2:
1903 case NFA_MCLOSE3:
1904 case NFA_MCLOSE4:
1905 case NFA_MCLOSE5:
1906 case NFA_MCLOSE6:
1907 case NFA_MCLOSE7:
1908 case NFA_MCLOSE8:
1909 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 STRCPY(code, "NFA_MCLOSE(x)");
1911 code[11] = c - NFA_MCLOSE + '0';
1912 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001913#ifdef FEAT_SYN_HL
1914 case NFA_ZOPEN:
1915 case NFA_ZOPEN1:
1916 case NFA_ZOPEN2:
1917 case NFA_ZOPEN3:
1918 case NFA_ZOPEN4:
1919 case NFA_ZOPEN5:
1920 case NFA_ZOPEN6:
1921 case NFA_ZOPEN7:
1922 case NFA_ZOPEN8:
1923 case NFA_ZOPEN9:
1924 STRCPY(code, "NFA_ZOPEN(x)");
1925 code[10] = c - NFA_ZOPEN + '0';
1926 break;
1927 case NFA_ZCLOSE:
1928 case NFA_ZCLOSE1:
1929 case NFA_ZCLOSE2:
1930 case NFA_ZCLOSE3:
1931 case NFA_ZCLOSE4:
1932 case NFA_ZCLOSE5:
1933 case NFA_ZCLOSE6:
1934 case NFA_ZCLOSE7:
1935 case NFA_ZCLOSE8:
1936 case NFA_ZCLOSE9:
1937 STRCPY(code, "NFA_ZCLOSE(x)");
1938 code[11] = c - NFA_ZCLOSE + '0';
1939 break;
1940#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1942 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1943 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1944 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001945 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1946 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001947 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001948 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1949 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1950 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1952 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1953 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001954 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1955 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1956 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1957 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1958 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1959 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1960 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1961 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1962 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1963 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1964 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1965 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1966 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1967 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1968 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1969 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1970 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1971
1972 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1973 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1974 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1975 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1976 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1977 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1978 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1979 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1980 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1981 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1982 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1983 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1984 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1985 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1986 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1987 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1988 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1989 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1990 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1991 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1992 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1993 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1994 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1995 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1996 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1997 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1998 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1999
2000 default:
2001 STRCPY(code, "CHAR(x)");
2002 code[5] = c;
2003 }
2004
2005 if (addnl == TRUE)
2006 STRCAT(code, " + NEWLINE ");
2007
2008}
2009
2010#ifdef ENABLE_LOG
2011static FILE *log_fd;
2012
2013/*
2014 * Print the postfix notation of the current regexp.
2015 */
2016 static void
2017nfa_postfix_dump(expr, retval)
2018 char_u *expr;
2019 int retval;
2020{
2021 int *p;
2022 FILE *f;
2023
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002024 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002025 if (f != NULL)
2026 {
2027 fprintf(f, "\n-------------------------\n");
2028 if (retval == FAIL)
2029 fprintf(f, ">>> NFA engine failed ... \n");
2030 else if (retval == OK)
2031 fprintf(f, ">>> NFA engine succeeded !\n");
2032 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002033 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034 {
2035 nfa_set_code(*p);
2036 fprintf(f, "%s, ", code);
2037 }
2038 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002039 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040 fprintf(f, "%d ", *p);
2041 fprintf(f, "\n\n");
2042 fclose(f);
2043 }
2044}
2045
2046/*
2047 * Print the NFA starting with a root node "state".
2048 */
2049 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002050nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002051 FILE *debugf;
2052 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002054 garray_T indent;
2055
2056 ga_init2(&indent, 1, 64);
2057 ga_append(&indent, '\0');
2058 nfa_print_state2(debugf, state, &indent);
2059 ga_clear(&indent);
2060}
2061
2062 static void
2063nfa_print_state2(debugf, state, indent)
2064 FILE *debugf;
2065 nfa_state_T *state;
2066 garray_T *indent;
2067{
2068 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069
2070 if (state == NULL)
2071 return;
2072
2073 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002074
2075 /* Output indent */
2076 p = (char_u *)indent->ga_data;
2077 if (indent->ga_len >= 3)
2078 {
2079 int last = indent->ga_len - 3;
2080 char_u save[2];
2081
2082 STRNCPY(save, &p[last], 2);
2083 STRNCPY(&p[last], "+-", 2);
2084 fprintf(debugf, " %s", p);
2085 STRNCPY(&p[last], save, 2);
2086 }
2087 else
2088 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002089
2090 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002091 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2092 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002093 if (state->id < 0)
2094 return;
2095
2096 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002097
2098 /* grow indent for state->out */
2099 indent->ga_len -= 1;
2100 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002101 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002102 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002103 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002104 ga_append(indent, '\0');
2105
2106 nfa_print_state2(debugf, state->out, indent);
2107
2108 /* replace last part of indent for state->out1 */
2109 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002110 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002111 ga_append(indent, '\0');
2112
2113 nfa_print_state2(debugf, state->out1, indent);
2114
2115 /* shrink indent */
2116 indent->ga_len -= 3;
2117 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002118}
2119
2120/*
2121 * Print the NFA state machine.
2122 */
2123 static void
2124nfa_dump(prog)
2125 nfa_regprog_T *prog;
2126{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002127 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002128
2129 if (debugf != NULL)
2130 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002131 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002132 fclose(debugf);
2133 }
2134}
2135#endif /* ENABLE_LOG */
2136#endif /* DEBUG */
2137
2138/*
2139 * Parse r.e. @expr and convert it into postfix form.
2140 * Return the postfix string on success, NULL otherwise.
2141 */
2142 static int *
2143re2post()
2144{
2145 if (nfa_reg(REG_NOPAREN) == FAIL)
2146 return NULL;
2147 EMIT(NFA_MOPEN);
2148 return post_start;
2149}
2150
2151/* NB. Some of the code below is inspired by Russ's. */
2152
2153/*
2154 * Represents an NFA state plus zero or one or two arrows exiting.
2155 * if c == MATCH, no arrows out; matching state.
2156 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2157 * If c < 256, labeled arrow with character c to out.
2158 */
2159
2160static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2161
2162/*
2163 * Allocate and initialize nfa_state_T.
2164 */
2165 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002166alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002167 int c;
2168 nfa_state_T *out;
2169 nfa_state_T *out1;
2170{
2171 nfa_state_T *s;
2172
2173 if (istate >= nstate)
2174 return NULL;
2175
2176 s = &state_ptr[istate++];
2177
2178 s->c = c;
2179 s->out = out;
2180 s->out1 = out1;
2181
2182 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002183 s->lastlist[0] = 0;
2184 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002185 s->negated = FALSE;
2186
2187 return s;
2188}
2189
2190/*
2191 * A partially built NFA without the matching state filled in.
2192 * Frag_T.start points at the start state.
2193 * Frag_T.out is a list of places that need to be set to the
2194 * next state for this fragment.
2195 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002196
2197/* Since the out pointers in the list are always
2198 * uninitialized, we use the pointers themselves
2199 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002200typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002201union Ptrlist
2202{
2203 Ptrlist *next;
2204 nfa_state_T *s;
2205};
2206
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207struct Frag
2208{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002209 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002210 Ptrlist *out;
2211};
2212typedef struct Frag Frag_T;
2213
2214static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2215static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2216static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2217static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2218static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2219static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2220
2221/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002222 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002223 */
2224 static Frag_T
2225frag(start, out)
2226 nfa_state_T *start;
2227 Ptrlist *out;
2228{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002229 Frag_T n;
2230
2231 n.start = start;
2232 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233 return n;
2234}
2235
2236/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237 * Create singleton list containing just outp.
2238 */
2239 static Ptrlist *
2240list1(outp)
2241 nfa_state_T **outp;
2242{
2243 Ptrlist *l;
2244
2245 l = (Ptrlist *)outp;
2246 l->next = NULL;
2247 return l;
2248}
2249
2250/*
2251 * Patch the list of states at out to point to start.
2252 */
2253 static void
2254patch(l, s)
2255 Ptrlist *l;
2256 nfa_state_T *s;
2257{
2258 Ptrlist *next;
2259
2260 for (; l; l = next)
2261 {
2262 next = l->next;
2263 l->s = s;
2264 }
2265}
2266
2267
2268/*
2269 * Join the two lists l1 and l2, returning the combination.
2270 */
2271 static Ptrlist *
2272append(l1, l2)
2273 Ptrlist *l1;
2274 Ptrlist *l2;
2275{
2276 Ptrlist *oldl1;
2277
2278 oldl1 = l1;
2279 while (l1->next)
2280 l1 = l1->next;
2281 l1->next = l2;
2282 return oldl1;
2283}
2284
2285/*
2286 * Stack used for transforming postfix form into NFA.
2287 */
2288static Frag_T empty;
2289
2290 static void
2291st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002292 int *postfix UNUSED;
2293 int *end UNUSED;
2294 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002296#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002297 FILE *df;
2298 int *p2;
2299
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002300 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002301 if (df)
2302 {
2303 fprintf(df, "Error popping the stack!\n");
2304#ifdef DEBUG
2305 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2306#endif
2307 fprintf(df, "Postfix form is: ");
2308#ifdef DEBUG
2309 for (p2 = postfix; p2 < end; p2++)
2310 {
2311 nfa_set_code(*p2);
2312 fprintf(df, "%s, ", code);
2313 }
2314 nfa_set_code(*p);
2315 fprintf(df, "\nCurrent position is: ");
2316 for (p2 = postfix; p2 <= p; p2 ++)
2317 {
2318 nfa_set_code(*p2);
2319 fprintf(df, "%s, ", code);
2320 }
2321#else
2322 for (p2 = postfix; p2 < end; p2++)
2323 {
2324 fprintf(df, "%d, ", *p2);
2325 }
2326 fprintf(df, "\nCurrent position is: ");
2327 for (p2 = postfix; p2 <= p; p2 ++)
2328 {
2329 fprintf(df, "%d, ", *p2);
2330 }
2331#endif
2332 fprintf(df, "\n--------------------------\n");
2333 fclose(df);
2334 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002335#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002336 EMSG(_("E874: (NFA) Could not pop the stack !"));
2337}
2338
2339/*
2340 * Push an item onto the stack.
2341 */
2342 static void
2343st_push(s, p, stack_end)
2344 Frag_T s;
2345 Frag_T **p;
2346 Frag_T *stack_end;
2347{
2348 Frag_T *stackp = *p;
2349
2350 if (stackp >= stack_end)
2351 return;
2352 *stackp = s;
2353 *p = *p + 1;
2354}
2355
2356/*
2357 * Pop an item from the stack.
2358 */
2359 static Frag_T
2360st_pop(p, stack)
2361 Frag_T **p;
2362 Frag_T *stack;
2363{
2364 Frag_T *stackp;
2365
2366 *p = *p - 1;
2367 stackp = *p;
2368 if (stackp < stack)
2369 return empty;
2370 return **p;
2371}
2372
2373/*
2374 * Convert a postfix form into its equivalent NFA.
2375 * Return the NFA start state on success, NULL otherwise.
2376 */
2377 static nfa_state_T *
2378post2nfa(postfix, end, nfa_calc_size)
2379 int *postfix;
2380 int *end;
2381 int nfa_calc_size;
2382{
2383 int *p;
2384 int mopen;
2385 int mclose;
2386 Frag_T *stack = NULL;
2387 Frag_T *stackp = NULL;
2388 Frag_T *stack_end = NULL;
2389 Frag_T e1;
2390 Frag_T e2;
2391 Frag_T e;
2392 nfa_state_T *s;
2393 nfa_state_T *s1;
2394 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002395 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396
2397 if (postfix == NULL)
2398 return NULL;
2399
Bram Moolenaar053bb602013-05-20 13:55:21 +02002400#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002401#define POP() st_pop(&stackp, stack); \
2402 if (stackp < stack) \
2403 { \
2404 st_error(postfix, end, p); \
2405 return NULL; \
2406 }
2407
2408 if (nfa_calc_size == FALSE)
2409 {
2410 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002411 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002413 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002414 }
2415
2416 for (p = postfix; p < end; ++p)
2417 {
2418 switch (*p)
2419 {
2420 case NFA_CONCAT:
2421 /* Catenation.
2422 * Pay attention: this operator does not exist
2423 * in the r.e. itself (it is implicit, really).
2424 * It is added when r.e. is translated to postfix
2425 * form in re2post().
2426 *
2427 * No new state added here. */
2428 if (nfa_calc_size == TRUE)
2429 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002430 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 break;
2432 }
2433 e2 = POP();
2434 e1 = POP();
2435 patch(e1.out, e2.start);
2436 PUSH(frag(e1.start, e2.out));
2437 break;
2438
2439 case NFA_NOT:
2440 /* Negation of a character */
2441 if (nfa_calc_size == TRUE)
2442 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002443 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 break;
2445 }
2446 e1 = POP();
2447 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002448#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002449 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002451#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 PUSH(e1);
2453 break;
2454
2455 case NFA_OR:
2456 /* Alternation */
2457 if (nfa_calc_size == TRUE)
2458 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002459 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002460 break;
2461 }
2462 e2 = POP();
2463 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002464 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002466 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002467 PUSH(frag(s, append(e1.out, e2.out)));
2468 break;
2469
2470 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002471 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002472 if (nfa_calc_size == TRUE)
2473 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002474 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002475 break;
2476 }
2477 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002478 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002480 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002481 patch(e.out, s);
2482 PUSH(frag(s, list1(&s->out1)));
2483 break;
2484
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002485 case NFA_STAR_NONGREEDY:
2486 /* Zero or more, prefer zero */
2487 if (nfa_calc_size == TRUE)
2488 {
2489 nstate++;
2490 break;
2491 }
2492 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002493 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002494 if (s == NULL)
2495 goto theend;
2496 patch(e.out, s);
2497 PUSH(frag(s, list1(&s->out)));
2498 break;
2499
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002500 case NFA_QUEST:
2501 /* one or zero atoms=> greedy match */
2502 if (nfa_calc_size == TRUE)
2503 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002504 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505 break;
2506 }
2507 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002508 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002509 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002510 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002511 PUSH(frag(s, append(e.out, list1(&s->out1))));
2512 break;
2513
2514 case NFA_QUEST_NONGREEDY:
2515 /* zero or one atoms => non-greedy match */
2516 if (nfa_calc_size == TRUE)
2517 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002518 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 break;
2520 }
2521 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002522 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002523 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002524 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002525 PUSH(frag(s, append(e.out, list1(&s->out))));
2526 break;
2527
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002528 case NFA_SKIP_CHAR:
2529 /* Symbol of 0-length, Used in a repetition
2530 * with max/min count of 0 */
2531 if (nfa_calc_size == TRUE)
2532 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002533 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002534 break;
2535 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002536 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002537 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002538 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539 PUSH(frag(s, list1(&s->out)));
2540 break;
2541
2542 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002543 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002544 case NFA_PREV_ATOM_JUST_BEFORE:
2545 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002546 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002547 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002548 * The \@<= operator: match for the preceding atom.
2549 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002551 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002552
2553 if (nfa_calc_size == TRUE)
2554 {
2555 nstate += 2;
2556 break;
2557 }
2558 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002559 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002561 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562 patch(e.out, s1);
2563
Bram Moolenaar525666f2013-06-02 16:40:55 +02002564 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002565 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002566 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002567 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2568 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002569 {
2570 s->negated = TRUE;
2571 s1->negated = TRUE;
2572 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002573 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2574 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2575 {
2576 s->val = *++p; /* get the count */
2577 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2578 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002579
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002580 PUSH(frag(s, list1(&s1->out)));
2581 break;
2582
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002583#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002584 case NFA_COMPOSING: /* char with composing char */
2585#if 0
2586 /* TODO */
2587 if (regflags & RF_ICOMBINE)
2588 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002589 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002590 }
2591#endif
2592 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002593#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002594
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002595 case NFA_MOPEN: /* \( \) Submatch */
2596 case NFA_MOPEN1:
2597 case NFA_MOPEN2:
2598 case NFA_MOPEN3:
2599 case NFA_MOPEN4:
2600 case NFA_MOPEN5:
2601 case NFA_MOPEN6:
2602 case NFA_MOPEN7:
2603 case NFA_MOPEN8:
2604 case NFA_MOPEN9:
2605#ifdef FEAT_SYN_HL
2606 case NFA_ZOPEN: /* \z( \) Submatch */
2607 case NFA_ZOPEN1:
2608 case NFA_ZOPEN2:
2609 case NFA_ZOPEN3:
2610 case NFA_ZOPEN4:
2611 case NFA_ZOPEN5:
2612 case NFA_ZOPEN6:
2613 case NFA_ZOPEN7:
2614 case NFA_ZOPEN8:
2615 case NFA_ZOPEN9:
2616#endif
2617 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618 if (nfa_calc_size == TRUE)
2619 {
2620 nstate += 2;
2621 break;
2622 }
2623
2624 mopen = *p;
2625 switch (*p)
2626 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002627 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2628#ifdef FEAT_SYN_HL
2629 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2630 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2631 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2632 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2633 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2634 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2635 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2636 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2637 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2638 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2639#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002640#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002641 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002642#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002644 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002645 mclose = *p + NSUBEXP;
2646 break;
2647 }
2648
2649 /* Allow "NFA_MOPEN" as a valid postfix representation for
2650 * the empty regexp "". In this case, the NFA will be
2651 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2652 * empty groups of parenthesis, and empty mbyte chars */
2653 if (stackp == stack)
2654 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002655 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002656 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002657 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002658 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002660 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002661 patch(list1(&s->out), s1);
2662 PUSH(frag(s, list1(&s1->out)));
2663 break;
2664 }
2665
2666 /* At least one node was emitted before NFA_MOPEN, so
2667 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2668 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002669 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002671 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672
Bram Moolenaar525666f2013-06-02 16:40:55 +02002673 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002675 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002676 patch(e.out, s1);
2677
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002678#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002679 if (mopen == NFA_COMPOSING)
2680 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002681 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002682#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002683
2684 PUSH(frag(s, list1(&s1->out)));
2685 break;
2686
Bram Moolenaar5714b802013-05-28 22:03:20 +02002687 case NFA_BACKREF1:
2688 case NFA_BACKREF2:
2689 case NFA_BACKREF3:
2690 case NFA_BACKREF4:
2691 case NFA_BACKREF5:
2692 case NFA_BACKREF6:
2693 case NFA_BACKREF7:
2694 case NFA_BACKREF8:
2695 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002696#ifdef FEAT_SYN_HL
2697 case NFA_ZREF1:
2698 case NFA_ZREF2:
2699 case NFA_ZREF3:
2700 case NFA_ZREF4:
2701 case NFA_ZREF5:
2702 case NFA_ZREF6:
2703 case NFA_ZREF7:
2704 case NFA_ZREF8:
2705 case NFA_ZREF9:
2706#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002707 if (nfa_calc_size == TRUE)
2708 {
2709 nstate += 2;
2710 break;
2711 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002712 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002713 if (s == NULL)
2714 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002715 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002716 if (s1 == NULL)
2717 goto theend;
2718 patch(list1(&s->out), s1);
2719 PUSH(frag(s, list1(&s1->out)));
2720 break;
2721
Bram Moolenaar423532e2013-05-29 21:14:42 +02002722 case NFA_LNUM:
2723 case NFA_LNUM_GT:
2724 case NFA_LNUM_LT:
2725 case NFA_VCOL:
2726 case NFA_VCOL_GT:
2727 case NFA_VCOL_LT:
2728 case NFA_COL:
2729 case NFA_COL_GT:
2730 case NFA_COL_LT:
2731 if (nfa_calc_size == TRUE)
2732 {
2733 nstate += 1;
2734 break;
2735 }
2736 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002737 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002738 if (s == NULL)
2739 goto theend;
2740 s->val = e1.start->c;
2741 PUSH(frag(s, list1(&s->out)));
2742 break;
2743
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744 case NFA_ZSTART:
2745 case NFA_ZEND:
2746 default:
2747 /* Operands */
2748 if (nfa_calc_size == TRUE)
2749 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002750 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002751 break;
2752 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002753 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002754 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002755 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002756 PUSH(frag(s, list1(&s->out)));
2757 break;
2758
2759 } /* switch(*p) */
2760
2761 } /* for(p = postfix; *p; ++p) */
2762
2763 if (nfa_calc_size == TRUE)
2764 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002765 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002766 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002767 }
2768
2769 e = POP();
2770 if (stackp != stack)
2771 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2772
2773 if (istate >= nstate)
2774 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2775
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776 matchstate = &state_ptr[istate++]; /* the match state */
2777 matchstate->c = NFA_MATCH;
2778 matchstate->out = matchstate->out1 = NULL;
2779
2780 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002781 ret = e.start;
2782
2783theend:
2784 vim_free(stack);
2785 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002786
2787#undef POP1
2788#undef PUSH1
2789#undef POP2
2790#undef PUSH2
2791#undef POP
2792#undef PUSH
2793}
2794
2795/****************************************************************
2796 * NFA execution code.
2797 ****************************************************************/
2798
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002799typedef struct
2800{
2801 int in_use; /* number of subexpr with useful info */
2802
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002803 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002804 union
2805 {
2806 struct multipos
2807 {
2808 lpos_T start;
2809 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002810 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002811 struct linepos
2812 {
2813 char_u *start;
2814 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002815 } line[NSUBEXP];
2816 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002817} regsub_T;
2818
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002819typedef struct
2820{
2821 regsub_T norm; /* \( .. \) matches */
2822#ifdef FEAT_SYN_HL
2823 regsub_T synt; /* \z( .. \) matches */
2824#endif
2825} regsubs_T;
2826
Bram Moolenaara2d95102013-06-04 14:23:05 +02002827/* nfa_pim_T stores a Postponed Invisible Match. */
2828typedef struct nfa_pim_S nfa_pim_T;
2829struct nfa_pim_S
2830{
2831 nfa_state_T *state;
2832 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2833 nfa_pim_T *pim; /* another PIM at the same position */
2834 regsubs_T subs; /* submatch info, only party used */
2835};
2836
2837/* Values for done in nfa_pim_T. */
2838#define NFA_PIM_TODO 0
2839#define NFA_PIM_MATCH 1
2840#define NFA_PIM_NOMATCH -1
2841
2842
Bram Moolenaar963fee22013-05-26 21:47:28 +02002843/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002844typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845{
2846 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002847 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002848 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002849 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002850} nfa_thread_T;
2851
Bram Moolenaar963fee22013-05-26 21:47:28 +02002852/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002853typedef struct
2854{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002855 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002856 int n; /* nr of states currently in "t" */
2857 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002858 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002859} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860
Bram Moolenaar5714b802013-05-28 22:03:20 +02002861#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002862static void log_subsexpr __ARGS((regsubs_T *subs));
2863static void log_subexpr __ARGS((regsub_T *sub));
2864
2865 static void
2866log_subsexpr(subs)
2867 regsubs_T *subs;
2868{
2869 log_subexpr(&subs->norm);
2870# ifdef FEAT_SYN_HL
2871 log_subexpr(&subs->synt);
2872# endif
2873}
2874
Bram Moolenaar5714b802013-05-28 22:03:20 +02002875 static void
2876log_subexpr(sub)
2877 regsub_T *sub;
2878{
2879 int j;
2880
2881 for (j = 0; j < sub->in_use; j++)
2882 if (REG_MULTI)
2883 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2884 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002885 sub->list.multi[j].start.col,
2886 (int)sub->list.multi[j].start.lnum,
2887 sub->list.multi[j].end.col,
2888 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002889 else
2890 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2891 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002892 (char *)sub->list.line[j].start,
2893 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002894 fprintf(log_fd, "\n");
2895}
2896#endif
2897
Bram Moolenaar963fee22013-05-26 21:47:28 +02002898/* Used during execution: whether a match has been found. */
2899static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002900
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002901static void clear_sub __ARGS((regsub_T *sub));
2902static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2903static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002904static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002905static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02002906static 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 +02002907
2908 static void
2909clear_sub(sub)
2910 regsub_T *sub;
2911{
2912 if (REG_MULTI)
2913 /* Use 0xff to set lnum to -1 */
2914 vim_memset(sub->list.multi, 0xff,
2915 sizeof(struct multipos) * nfa_nsubexpr);
2916 else
2917 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2918 sub->in_use = 0;
2919}
2920
2921/*
2922 * Copy the submatches from "from" to "to".
2923 */
2924 static void
2925copy_sub(to, from)
2926 regsub_T *to;
2927 regsub_T *from;
2928{
2929 to->in_use = from->in_use;
2930 if (from->in_use > 0)
2931 {
2932 /* Copy the match start and end positions. */
2933 if (REG_MULTI)
2934 mch_memmove(&to->list.multi[0],
2935 &from->list.multi[0],
2936 sizeof(struct multipos) * from->in_use);
2937 else
2938 mch_memmove(&to->list.line[0],
2939 &from->list.line[0],
2940 sizeof(struct linepos) * from->in_use);
2941 }
2942}
2943
2944/*
2945 * Like copy_sub() but exclude the main match.
2946 */
2947 static void
2948copy_sub_off(to, from)
2949 regsub_T *to;
2950 regsub_T *from;
2951{
2952 if (to->in_use < from->in_use)
2953 to->in_use = from->in_use;
2954 if (from->in_use > 1)
2955 {
2956 /* Copy the match start and end positions. */
2957 if (REG_MULTI)
2958 mch_memmove(&to->list.multi[1],
2959 &from->list.multi[1],
2960 sizeof(struct multipos) * (from->in_use - 1));
2961 else
2962 mch_memmove(&to->list.line[1],
2963 &from->list.line[1],
2964 sizeof(struct linepos) * (from->in_use - 1));
2965 }
2966}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002967
Bram Moolenaar428e9872013-05-30 17:05:39 +02002968/*
2969 * Return TRUE if "sub1" and "sub2" have the same positions.
2970 */
2971 static int
2972sub_equal(sub1, sub2)
2973 regsub_T *sub1;
2974 regsub_T *sub2;
2975{
2976 int i;
2977 int todo;
2978 linenr_T s1, e1;
2979 linenr_T s2, e2;
2980 char_u *sp1, *ep1;
2981 char_u *sp2, *ep2;
2982
2983 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2984 if (REG_MULTI)
2985 {
2986 for (i = 0; i < todo; ++i)
2987 {
2988 if (i < sub1->in_use)
2989 {
2990 s1 = sub1->list.multi[i].start.lnum;
2991 e1 = sub1->list.multi[i].end.lnum;
2992 }
2993 else
2994 {
2995 s1 = 0;
2996 e1 = 0;
2997 }
2998 if (i < sub2->in_use)
2999 {
3000 s2 = sub2->list.multi[i].start.lnum;
3001 e2 = sub2->list.multi[i].end.lnum;
3002 }
3003 else
3004 {
3005 s2 = 0;
3006 e2 = 0;
3007 }
3008 if (s1 != s2 || e1 != e2)
3009 return FALSE;
3010 if (s1 != 0 && sub1->list.multi[i].start.col
3011 != sub2->list.multi[i].start.col)
3012 return FALSE;
3013 if (e1 != 0 && sub1->list.multi[i].end.col
3014 != sub2->list.multi[i].end.col)
3015 return FALSE;
3016 }
3017 }
3018 else
3019 {
3020 for (i = 0; i < todo; ++i)
3021 {
3022 if (i < sub1->in_use)
3023 {
3024 sp1 = sub1->list.line[i].start;
3025 ep1 = sub1->list.line[i].end;
3026 }
3027 else
3028 {
3029 sp1 = NULL;
3030 ep1 = NULL;
3031 }
3032 if (i < sub2->in_use)
3033 {
3034 sp2 = sub2->list.line[i].start;
3035 ep2 = sub2->list.line[i].end;
3036 }
3037 else
3038 {
3039 sp2 = NULL;
3040 ep2 = NULL;
3041 }
3042 if (sp1 != sp2 || ep1 != ep2)
3043 return FALSE;
3044 }
3045 }
3046
3047 return TRUE;
3048}
3049
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003050#ifdef ENABLE_LOG
3051 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003052report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003053{
3054 int col;
3055
3056 if (sub->in_use <= 0)
3057 col = -1;
3058 else if (REG_MULTI)
3059 col = sub->list.multi[0].start.col;
3060 else
3061 col = (int)(sub->list.line[0].start - regline);
3062 nfa_set_code(state->c);
3063 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3064 action, abs(state->id), lid, state->c, code, col);
3065}
3066#endif
3067
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003068 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003069addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003070 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003071 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003072 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003073 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003074{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003075 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003076 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003077 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003078 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003079 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003080 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003081 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003082#ifdef ENABLE_LOG
3083 int did_print = FALSE;
3084#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003085
3086 if (l == NULL || state == NULL)
3087 return;
3088
3089 switch (state->c)
3090 {
3091 case NFA_SPLIT:
3092 case NFA_NOT:
3093 case NFA_NOPEN:
3094 case NFA_NCLOSE:
3095 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003096 case NFA_MCLOSE1:
3097 case NFA_MCLOSE2:
3098 case NFA_MCLOSE3:
3099 case NFA_MCLOSE4:
3100 case NFA_MCLOSE5:
3101 case NFA_MCLOSE6:
3102 case NFA_MCLOSE7:
3103 case NFA_MCLOSE8:
3104 case NFA_MCLOSE9:
3105#ifdef FEAT_SYN_HL
3106 case NFA_ZCLOSE:
3107 case NFA_ZCLOSE1:
3108 case NFA_ZCLOSE2:
3109 case NFA_ZCLOSE3:
3110 case NFA_ZCLOSE4:
3111 case NFA_ZCLOSE5:
3112 case NFA_ZCLOSE6:
3113 case NFA_ZCLOSE7:
3114 case NFA_ZCLOSE8:
3115 case NFA_ZCLOSE9:
3116#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003117 /* These nodes are not added themselves but their "out" and/or
3118 * "out1" may be added below. */
3119 break;
3120
3121 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003122 case NFA_MOPEN1:
3123 case NFA_MOPEN2:
3124 case NFA_MOPEN3:
3125 case NFA_MOPEN4:
3126 case NFA_MOPEN5:
3127 case NFA_MOPEN6:
3128 case NFA_MOPEN7:
3129 case NFA_MOPEN8:
3130 case NFA_MOPEN9:
3131#ifdef FEAT_SYN_HL
3132 case NFA_ZOPEN:
3133 case NFA_ZOPEN1:
3134 case NFA_ZOPEN2:
3135 case NFA_ZOPEN3:
3136 case NFA_ZOPEN4:
3137 case NFA_ZOPEN5:
3138 case NFA_ZOPEN6:
3139 case NFA_ZOPEN7:
3140 case NFA_ZOPEN8:
3141 case NFA_ZOPEN9:
3142#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003143 /* These nodes do not need to be added, but we need to bail out
3144 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003145 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003146 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003147 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003148 break;
3149
Bram Moolenaar307aa162013-06-02 16:34:21 +02003150 case NFA_BOL:
3151 case NFA_BOF:
3152 /* "^" won't match past end-of-line, don't bother trying.
3153 * Except when we are going to the next line for a look-behind
3154 * match. */
3155 if (reginput > regline
3156 && (nfa_endp == NULL
3157 || !REG_MULTI
3158 || reglnum == nfa_endp->se_u.pos.lnum))
3159 goto skip_add;
3160 /* FALLTHROUGH */
3161
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003162 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003163 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003164 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003165 /* This state is already in the list, don't add it again,
3166 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003167 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003168 {
3169skip_add:
3170#ifdef ENABLE_LOG
3171 nfa_set_code(state->c);
3172 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3173 abs(state->id), l->id, state->c, code);
3174#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003175 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003176 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003177
3178 /* See if the same state is already in the list with the same
3179 * positions. */
3180 for (i = 0; i < l->n; ++i)
3181 {
3182 thread = &l->t[i];
3183 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003184 && sub_equal(&thread->subs.norm, &subs->norm)
3185#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003186 && (!nfa_has_zsubexpr ||
3187 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003188#endif
3189 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003190 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003191 }
3192 }
3193
Bram Moolenaara2d95102013-06-04 14:23:05 +02003194 /* when there are backreferences or look-behind matches the number
3195 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003196 if (nfa_has_backref && l->n == l->len)
3197 {
3198 int newlen = l->len * 3 / 2 + 50;
3199
3200 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3201 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003203
3204 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003205 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003206 thread = &l->t[l->n++];
3207 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003208 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003209 copy_sub(&thread->subs.norm, &subs->norm);
3210#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003211 if (nfa_has_zsubexpr)
3212 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003213#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003214#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003215 report_state("Adding", &thread->subs.norm, state, l->id);
3216 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003217#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 }
3219
3220#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003221 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003222 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223#endif
3224 switch (state->c)
3225 {
3226 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003227 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003228 break;
3229
3230 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003231 /* order matters here */
3232 addstate(l, state->out, subs, off);
3233 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 break;
3235
Bram Moolenaar5714b802013-05-28 22:03:20 +02003236 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237 case NFA_NOPEN:
3238 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003239 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 break;
3241
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003242 case NFA_MOPEN:
3243 case NFA_MOPEN1:
3244 case NFA_MOPEN2:
3245 case NFA_MOPEN3:
3246 case NFA_MOPEN4:
3247 case NFA_MOPEN5:
3248 case NFA_MOPEN6:
3249 case NFA_MOPEN7:
3250 case NFA_MOPEN8:
3251 case NFA_MOPEN9:
3252#ifdef FEAT_SYN_HL
3253 case NFA_ZOPEN:
3254 case NFA_ZOPEN1:
3255 case NFA_ZOPEN2:
3256 case NFA_ZOPEN3:
3257 case NFA_ZOPEN4:
3258 case NFA_ZOPEN5:
3259 case NFA_ZOPEN6:
3260 case NFA_ZOPEN7:
3261 case NFA_ZOPEN8:
3262 case NFA_ZOPEN9:
3263#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003264 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003265 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003266 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003267 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003268 sub = &subs->norm;
3269 }
3270#ifdef FEAT_SYN_HL
3271 else if (state->c >= NFA_ZOPEN)
3272 {
3273 subidx = state->c - NFA_ZOPEN;
3274 sub = &subs->synt;
3275 }
3276#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003277 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003278 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003279 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003280 sub = &subs->norm;
3281 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003282
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003283 /* Set the position (with "off") in the subexpression. Save and
3284 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003285 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 if (REG_MULTI)
3287 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003288 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003289 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003290 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003291 save_in_use = -1;
3292 }
3293 else
3294 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003295 save_in_use = sub->in_use;
3296 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003297 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003298 sub->list.multi[i].start.lnum = -1;
3299 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003300 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003301 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003302 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003303 if (off == -1)
3304 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003305 sub->list.multi[subidx].start.lnum = reglnum + 1;
3306 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003307 }
3308 else
3309 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003310 sub->list.multi[subidx].start.lnum = reglnum;
3311 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003312 (colnr_T)(reginput - regline + off);
3313 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 }
3315 else
3316 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003317 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003318 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003319 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003320 save_in_use = -1;
3321 }
3322 else
3323 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003324 save_in_use = sub->in_use;
3325 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003326 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003327 sub->list.line[i].start = NULL;
3328 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003329 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003330 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003331 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003332 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 }
3334
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003335 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003336
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003337 if (save_in_use == -1)
3338 {
3339 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003340 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003341 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003342 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003343 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003344 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003345 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346 break;
3347
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003348 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003349 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003351 /* Do not overwrite the position set by \ze. If no \ze
3352 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003353 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 break;
3355 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003356 case NFA_MCLOSE1:
3357 case NFA_MCLOSE2:
3358 case NFA_MCLOSE3:
3359 case NFA_MCLOSE4:
3360 case NFA_MCLOSE5:
3361 case NFA_MCLOSE6:
3362 case NFA_MCLOSE7:
3363 case NFA_MCLOSE8:
3364 case NFA_MCLOSE9:
3365#ifdef FEAT_SYN_HL
3366 case NFA_ZCLOSE:
3367 case NFA_ZCLOSE1:
3368 case NFA_ZCLOSE2:
3369 case NFA_ZCLOSE3:
3370 case NFA_ZCLOSE4:
3371 case NFA_ZCLOSE5:
3372 case NFA_ZCLOSE6:
3373 case NFA_ZCLOSE7:
3374 case NFA_ZCLOSE8:
3375 case NFA_ZCLOSE9:
3376#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003377 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003378 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003379 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003380 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003381 sub = &subs->norm;
3382 }
3383#ifdef FEAT_SYN_HL
3384 else if (state->c >= NFA_ZCLOSE)
3385 {
3386 subidx = state->c - NFA_ZCLOSE;
3387 sub = &subs->synt;
3388 }
3389#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003390 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003391 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003392 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003393 sub = &subs->norm;
3394 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003395
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003396 /* We don't fill in gaps here, there must have been an MOPEN that
3397 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003398 save_in_use = sub->in_use;
3399 if (sub->in_use <= subidx)
3400 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003401 if (REG_MULTI)
3402 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003403 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003404 if (off == -1)
3405 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003406 sub->list.multi[subidx].end.lnum = reglnum + 1;
3407 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003408 }
3409 else
3410 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003411 sub->list.multi[subidx].end.lnum = reglnum;
3412 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003413 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003414 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003415 }
3416 else
3417 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003418 save_ptr = sub->list.line[subidx].end;
3419 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003420 }
3421
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003422 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003423
3424 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003425 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003426 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003427 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003428 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429 break;
3430 }
3431}
3432
3433/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003434 * Like addstate(), but the new state(s) are put at position "*ip".
3435 * Used for zero-width matches, next state to use is the added one.
3436 * This makes sure the order of states to be tried does not change, which
3437 * matters for alternatives.
3438 */
3439 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003440addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003441 nfa_list_T *l; /* runtime state list */
3442 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003443 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003444 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003445 int *ip;
3446{
3447 int tlen = l->n;
3448 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003449 int listidx = *ip;
3450 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003451
3452 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003453 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003454
Bram Moolenaara2d95102013-06-04 14:23:05 +02003455 /* fill in the "pim" field in the new states */
3456 if (pim != NULL)
3457 for (i = tlen; i < l->n; ++i)
3458 l->t[i].pim = pim;
3459
Bram Moolenaar4b417062013-05-25 20:19:50 +02003460 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003461 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003462 return;
3463
3464 /* re-order to put the new state at the current position */
3465 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003466 if (count == 1)
3467 {
3468 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003469 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003470 }
3471 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003472 {
3473 /* make space for new states, then move them from the
3474 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003475 mch_memmove(&(l->t[listidx + count]),
3476 &(l->t[listidx + 1]),
3477 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3478 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003479 &(l->t[l->n - 1]),
3480 sizeof(nfa_thread_T) * count);
3481 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003482 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003483 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003484}
3485
3486/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 * Check character class "class" against current character c.
3488 */
3489 static int
3490check_char_class(class, c)
3491 int class;
3492 int c;
3493{
3494 switch (class)
3495 {
3496 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003497 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498 return OK;
3499 break;
3500 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003501 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502 return OK;
3503 break;
3504 case NFA_CLASS_BLANK:
3505 if (c == ' ' || c == '\t')
3506 return OK;
3507 break;
3508 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003509 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 return OK;
3511 break;
3512 case NFA_CLASS_DIGIT:
3513 if (VIM_ISDIGIT(c))
3514 return OK;
3515 break;
3516 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003517 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 return OK;
3519 break;
3520 case NFA_CLASS_LOWER:
3521 if (MB_ISLOWER(c))
3522 return OK;
3523 break;
3524 case NFA_CLASS_PRINT:
3525 if (vim_isprintc(c))
3526 return OK;
3527 break;
3528 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003529 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530 return OK;
3531 break;
3532 case NFA_CLASS_SPACE:
3533 if ((c >=9 && c <= 13) || (c == ' '))
3534 return OK;
3535 break;
3536 case NFA_CLASS_UPPER:
3537 if (MB_ISUPPER(c))
3538 return OK;
3539 break;
3540 case NFA_CLASS_XDIGIT:
3541 if (vim_isxdigit(c))
3542 return OK;
3543 break;
3544 case NFA_CLASS_TAB:
3545 if (c == '\t')
3546 return OK;
3547 break;
3548 case NFA_CLASS_RETURN:
3549 if (c == '\r')
3550 return OK;
3551 break;
3552 case NFA_CLASS_BACKSPACE:
3553 if (c == '\b')
3554 return OK;
3555 break;
3556 case NFA_CLASS_ESCAPE:
3557 if (c == '\033')
3558 return OK;
3559 break;
3560
3561 default:
3562 /* should not be here :P */
3563 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3564 }
3565 return FAIL;
3566}
3567
Bram Moolenaar5714b802013-05-28 22:03:20 +02003568static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3569
3570/*
3571 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003572 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003573 */
3574 static int
3575match_backref(sub, subidx, bytelen)
3576 regsub_T *sub; /* pointers to subexpressions */
3577 int subidx;
3578 int *bytelen; /* out: length of match in bytes */
3579{
3580 int len;
3581
3582 if (sub->in_use <= subidx)
3583 {
3584retempty:
3585 /* backref was not set, match an empty string */
3586 *bytelen = 0;
3587 return TRUE;
3588 }
3589
3590 if (REG_MULTI)
3591 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003592 if (sub->list.multi[subidx].start.lnum < 0
3593 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003594 goto retempty;
3595 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003596 len = sub->list.multi[subidx].end.col
3597 - sub->list.multi[subidx].start.col;
3598 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003599 reginput, &len) == 0)
3600 {
3601 *bytelen = len;
3602 return TRUE;
3603 }
3604 }
3605 else
3606 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003607 if (sub->list.line[subidx].start == NULL
3608 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003609 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003610 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3611 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003612 {
3613 *bytelen = len;
3614 return TRUE;
3615 }
3616 }
3617 return FALSE;
3618}
3619
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003620#ifdef FEAT_SYN_HL
3621
3622static int match_zref __ARGS((int subidx, int *bytelen));
3623
3624/*
3625 * Check for a match with \z subexpression "subidx".
3626 * Return TRUE if it matches.
3627 */
3628 static int
3629match_zref(subidx, bytelen)
3630 int subidx;
3631 int *bytelen; /* out: length of match in bytes */
3632{
3633 int len;
3634
3635 cleanup_zsubexpr();
3636 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3637 {
3638 /* backref was not set, match an empty string */
3639 *bytelen = 0;
3640 return TRUE;
3641 }
3642
3643 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3644 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3645 {
3646 *bytelen = len;
3647 return TRUE;
3648 }
3649 return FALSE;
3650}
3651#endif
3652
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003653/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003654 * Save list IDs for all NFA states of "prog" into "list".
3655 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003656 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003657 */
3658 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003659nfa_save_listids(prog, list)
3660 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003661 int *list;
3662{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003663 int i;
3664 nfa_state_T *p;
3665
3666 /* Order in the list is reverse, it's a bit faster that way. */
3667 p = &prog->state[0];
3668 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003670 list[i] = p->lastlist[1];
3671 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003672 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003673 }
3674}
3675
3676/*
3677 * Restore list IDs from "list" to all NFA states.
3678 */
3679 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003680nfa_restore_listids(prog, list)
3681 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003682 int *list;
3683{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003684 int i;
3685 nfa_state_T *p;
3686
3687 p = &prog->state[0];
3688 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003689 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003690 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003691 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003692 }
3693}
3694
Bram Moolenaar423532e2013-05-29 21:14:42 +02003695 static int
3696nfa_re_num_cmp(val, op, pos)
3697 long_u val;
3698 int op;
3699 long_u pos;
3700{
3701 if (op == 1) return pos > val;
3702 if (op == 2) return pos < val;
3703 return val == pos;
3704}
3705
Bram Moolenaarf46da702013-06-02 22:37:42 +02003706static 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 +02003707static 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 +02003708
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003710 * Recursively call nfa_regmatch()
3711 */
3712 static int
3713recursive_regmatch(state, prog, submatch, m, listids)
3714 nfa_state_T *state;
3715 nfa_regprog_T *prog;
3716 regsubs_T *submatch;
3717 regsubs_T *m;
3718 int **listids;
3719{
3720 char_u *save_reginput = reginput;
3721 char_u *save_regline = regline;
3722 int save_reglnum = reglnum;
3723 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003724 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003725 save_se_T *save_nfa_endp = nfa_endp;
3726 save_se_T endpos;
3727 save_se_T *endposp = NULL;
3728 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003729 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003730
3731 if (state->c == NFA_START_INVISIBLE_BEFORE)
3732 {
3733 /* The recursive match must end at the current position. */
3734 endposp = &endpos;
3735 if (REG_MULTI)
3736 {
3737 endpos.se_u.pos.col = (int)(reginput - regline);
3738 endpos.se_u.pos.lnum = reglnum;
3739 }
3740 else
3741 endpos.se_u.ptr = reginput;
3742
3743 /* Go back the specified number of bytes, or as far as the
3744 * start of the previous line, to try matching "\@<=" or
3745 * not matching "\@<!".
3746 * TODO: This is very inefficient! Would be better to
3747 * first check for a match with what follows. */
3748 if (state->val <= 0)
3749 {
3750 if (REG_MULTI)
3751 {
3752 regline = reg_getline(--reglnum);
3753 if (regline == NULL)
3754 /* can't go before the first line */
3755 regline = reg_getline(++reglnum);
3756 }
3757 reginput = regline;
3758 }
3759 else
3760 {
3761 if (REG_MULTI && (int)(reginput - regline) < state->val)
3762 {
3763 /* Not enough bytes in this line, go to end of
3764 * previous line. */
3765 regline = reg_getline(--reglnum);
3766 if (regline == NULL)
3767 {
3768 /* can't go before the first line */
3769 regline = reg_getline(++reglnum);
3770 reginput = regline;
3771 }
3772 else
3773 reginput = regline + STRLEN(regline);
3774 }
3775 if ((int)(reginput - regline) >= state->val)
3776 {
3777 reginput -= state->val;
3778#ifdef FEAT_MBYTE
3779 if (has_mbyte)
3780 reginput -= mb_head_off(regline, reginput);
3781#endif
3782 }
3783 else
3784 reginput = regline;
3785 }
3786 }
3787
Bram Moolenaarf46da702013-06-02 22:37:42 +02003788#ifdef ENABLE_LOG
3789 if (log_fd != stderr)
3790 fclose(log_fd);
3791 log_fd = NULL;
3792#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003793 /* Have to clear the lastlist field of the NFA nodes, so that
3794 * nfa_regmatch() and addstate() can run properly after recursion. */
3795 if (nfa_ll_index == 1)
3796 {
3797 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3798 * values and clear them. */
3799 if (*listids == NULL)
3800 {
3801 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3802 if (*listids == NULL)
3803 {
3804 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3805 return 0;
3806 }
3807 }
3808 nfa_save_listids(prog, *listids);
3809 need_restore = TRUE;
3810 /* any value of nfa_listid will do */
3811 }
3812 else
3813 {
3814 /* First recursive nfa_regmatch() call, switch to the second lastlist
3815 * entry. Make sure nfa_listid is different from a previous recursive
3816 * call, because some states may still have this ID. */
3817 ++nfa_ll_index;
3818 if (nfa_listid <= nfa_alt_listid)
3819 nfa_listid = nfa_alt_listid;
3820 }
3821
3822 /* Call nfa_regmatch() to check if the current concat matches at this
3823 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003824 nfa_endp = endposp;
3825 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003826
3827 if (need_restore)
3828 nfa_restore_listids(prog, *listids);
3829 else
3830 {
3831 --nfa_ll_index;
3832 nfa_alt_listid = nfa_listid;
3833 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003834
3835 /* restore position in input text */
3836 reginput = save_reginput;
3837 regline = save_regline;
3838 reglnum = save_reglnum;
3839 nfa_match = save_nfa_match;
3840 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003841 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003842
3843#ifdef ENABLE_LOG
3844 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3845 if (log_fd != NULL)
3846 {
3847 fprintf(log_fd, "****************************\n");
3848 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3849 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3850 fprintf(log_fd, "****************************\n");
3851 }
3852 else
3853 {
3854 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3855 log_fd = stderr;
3856 }
3857#endif
3858
3859 return result;
3860}
3861
Bram Moolenaara2d95102013-06-04 14:23:05 +02003862static int failure_chance __ARGS((nfa_state_T *state, int depth));
3863
3864/*
3865 * Estimate the chance of a match with "state" failing.
3866 * NFA_ANY: 1
3867 * specific character: 99
3868 */
3869 static int
3870failure_chance(state, depth)
3871 nfa_state_T *state;
3872 int depth;
3873{
3874 int c = state->c;
3875 int l, r;
3876
3877 /* detect looping */
3878 if (depth > 4)
3879 return 1;
3880
3881 if (c == NFA_SPLIT)
3882 {
3883 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3884 return 1;
3885 l = failure_chance(state->out, depth + 1);
3886 r = failure_chance(state->out1, depth + 1);
3887 return l < r ? l : r;
3888 }
3889 if (c == NFA_ANY)
3890 return 1;
3891 if (c > 0)
3892 return 99;
3893 if ((c >= NFA_MOPEN && c <= NFA_MOPEN9)
3894 || (c >= NFA_ZOPEN && c <= NFA_ZOPEN9)
3895 || c == NFA_NOPEN)
3896 return failure_chance(state->out, depth + 1);
3897 /* something else */
3898 return 50;
3899}
3900
Bram Moolenaarf46da702013-06-02 22:37:42 +02003901/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003902 * Main matching routine.
3903 *
3904 * Run NFA to determine whether it matches reginput.
3905 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003906 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003907 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003908 * Return TRUE if there is a match, FALSE otherwise.
3909 * Note: Caller must ensure that: start != NULL.
3910 */
3911 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003912nfa_regmatch(prog, start, submatch, m)
3913 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003914 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003915 regsubs_T *submatch;
3916 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918 int result;
3919 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003920 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003921 int go_to_nextline = FALSE;
3922 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003923 nfa_list_T list[3];
3924 nfa_list_T *listtbl[2][2];
3925 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003926 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003927 nfa_list_T *thislist;
3928 nfa_list_T *nextlist;
3929 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003930 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003931 nfa_state_T *add_state;
3932 int add_count;
3933 int add_off;
3934 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003935#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003936 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003937
3938 if (debug == NULL)
3939 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003940 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003941 return FALSE;
3942 }
3943#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003944 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003945 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003946
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003947 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003948 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003949 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3950 list[0].len = nstate + 1;
3951 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3952 list[1].len = nstate + 1;
3953 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3954 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003955 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3956 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003957
3958#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003959 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003960 if (log_fd != NULL)
3961 {
3962 fprintf(log_fd, "**********************************\n");
3963 nfa_set_code(start->c);
3964 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3965 abs(start->id), code);
3966 fprintf(log_fd, "**********************************\n");
3967 }
3968 else
3969 {
3970 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3971 log_fd = stderr;
3972 }
3973#endif
3974
3975 thislist = &list[0];
3976 thislist->n = 0;
3977 nextlist = &list[1];
3978 nextlist->n = 0;
3979 neglist = &list[2];
3980 neglist->n = 0;
3981#ifdef ENABLE_LOG
3982 fprintf(log_fd, "(---) STARTSTATE\n");
3983#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003984 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003985 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003986
3987 /* There are two cases when the NFA advances: 1. input char matches the
3988 * NFA node and 2. input char does not match the NFA node, but the next
3989 * node is NFA_NOT. The following macro calls addstate() according to
3990 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3991 listtbl[0][0] = NULL;
3992 listtbl[0][1] = neglist;
3993 listtbl[1][0] = nextlist;
3994 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003995#define ADD_POS_NEG_STATE(state) \
3996 ll = listtbl[result ? 1 : 0][state->negated]; \
3997 if (ll != NULL) { \
3998 add_state = state->out; \
3999 add_off = clen; \
4000 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004001
4002 /*
4003 * Run for each character.
4004 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004005 for (;;)
4006 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004007 int curc;
4008 int clen;
4009
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004010#ifdef FEAT_MBYTE
4011 if (has_mbyte)
4012 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004013 curc = (*mb_ptr2char)(reginput);
4014 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004015 }
4016 else
4017#endif
4018 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004019 curc = *reginput;
4020 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004021 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004022 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004023 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004024 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004025 go_to_nextline = FALSE;
4026 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004027
4028 /* swap lists */
4029 thislist = &list[flag];
4030 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004031 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004032 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004033 ++nfa_listid;
4034 thislist->id = nfa_listid;
4035 nextlist->id = nfa_listid + 1;
4036 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037
Bram Moolenaara2d95102013-06-04 14:23:05 +02004038 pimlist.ga_len = 0;
4039
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004040#ifdef ENABLE_LOG
4041 fprintf(log_fd, "------------------------------------------\n");
4042 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004043 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004044 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004045 {
4046 int i;
4047
4048 for (i = 0; i < thislist->n; i++)
4049 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4050 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004051 fprintf(log_fd, "\n");
4052#endif
4053
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004054#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004055 fprintf(debug, "\n-------------------\n");
4056#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004057 /*
4058 * If the state lists are empty we can stop.
4059 */
4060 if (thislist->n == 0 && neglist->n == 0)
4061 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004062
4063 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004064 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004065 {
4066 if (neglist->n > 0)
4067 {
4068 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004069 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004070 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004071 }
4072 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004073 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004074
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004075#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004076 nfa_set_code(t->state->c);
4077 fprintf(debug, "%s, ", code);
4078#endif
4079#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004080 {
4081 int col;
4082
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004083 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004084 col = -1;
4085 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004086 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004087 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004088 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004089 nfa_set_code(t->state->c);
4090 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4091 abs(t->state->id), (int)t->state->c, code, col);
4092 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004093#endif
4094
4095 /*
4096 * Handle the possible codes of the current state.
4097 * The most important is NFA_MATCH.
4098 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004099 add_state = NULL;
4100 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004101 switch (t->state->c)
4102 {
4103 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004104 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004105 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004106 copy_sub(&submatch->norm, &t->subs.norm);
4107#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004108 if (nfa_has_zsubexpr)
4109 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004110#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004111#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004112 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004114 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004115 * states at this position. When the list of states is going
4116 * to be empty quit without advancing, so that "reginput" is
4117 * correct. */
4118 if (nextlist->n == 0 && neglist->n == 0)
4119 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004120 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004121 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004122
4123 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004124 /*
4125 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004126 * NFA_START_INVISIBLE_BEFORE node.
4127 * They surround a zero-width group, used with "\@=", "\&",
4128 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004129 * If we got here, it means that the current "invisible" group
4130 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004131 * nfa_regmatch(). For a look-behind match only when it ends
4132 * in the position in "nfa_endp".
4133 * Submatches are stored in *m, and used in the parent call.
4134 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004135#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004136 if (nfa_endp != NULL)
4137 {
4138 if (REG_MULTI)
4139 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4140 (int)reglnum,
4141 (int)nfa_endp->se_u.pos.lnum,
4142 (int)(reginput - regline),
4143 nfa_endp->se_u.pos.col);
4144 else
4145 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4146 (int)(reginput - regline),
4147 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004148 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004149#endif
4150 /* It's only a match if it ends at "nfa_endp" */
4151 if (nfa_endp != NULL && (REG_MULTI
4152 ? (reglnum != nfa_endp->se_u.pos.lnum
4153 || (int)(reginput - regline)
4154 != nfa_endp->se_u.pos.col)
4155 : reginput != nfa_endp->se_u.ptr))
4156 break;
4157
4158 /* do not set submatches for \@! */
4159 if (!t->state->negated)
4160 {
4161 copy_sub(&m->norm, &t->subs.norm);
4162#ifdef FEAT_SYN_HL
4163 if (nfa_has_zsubexpr)
4164 copy_sub(&m->synt, &t->subs.synt);
4165#endif
4166 }
4167 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004168 break;
4169
4170 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004171 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2d95102013-06-04 14:23:05 +02004172 /* If invisible match has a higher chance to fail, do it
4173 * right away. Otherwise postpone it until what follows is
4174 * matching and causes addstate(nextlist, ..) to be called.
4175 * This is indicated by the "pim" field. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004176 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004177 nfa_pim_T *pim;
4178 int cout = t->state->out1->out->c;
4179
4180 /* Do it directly when what follows is possibly end of
4181 * match (closing paren).
4182 * Postpone when it is \@<= or \@<!, these are expensive.
4183 * TODO: remove the check for t->pim and check multiple
4184 * where it's used?
4185 * Otherwise first do the one that has the highest chance
4186 * of failing. */
4187 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
4188 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
4189 || cout == NFA_NCLOSE
4190 || t->pim != NULL
4191 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4192 && failure_chance(t->state->out1->out, 0)
4193 < failure_chance(t->state->out, 0)))
4194 {
4195 /*
4196 * First try matching the invisible match, then what
4197 * follows.
4198 */
4199 result = recursive_regmatch(t->state, prog,
4200 submatch, m, &listids);
4201
4202 /* for \@! it is a match when result is FALSE */
4203 if (result != t->state->negated)
4204 {
4205 /* Copy submatch info from the recursive call */
4206 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004207#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004208 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004209#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004210
Bram Moolenaara2d95102013-06-04 14:23:05 +02004211 /* t->state->out1 is the corresponding
4212 * END_INVISIBLE node; Add its out to the current
4213 * list (zero-width match). */
4214 addstate_here(thislist, t->state->out1->out,
4215 &t->subs, t->pim, &listidx);
4216 }
4217 }
4218 else
4219 {
4220 /*
4221 * First try matching what follows at the current
4222 * position. Only if a match is found, addstate() is
4223 * called, then verify the invisible match matches.
4224 * Add a nfa_pim_T to the following states, it
4225 * contains info about the invisible match.
4226 */
4227 if (ga_grow(&pimlist, 1) == FAIL)
4228 goto theend;
4229 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4230 ++pimlist.ga_len;
4231 pim->state = t->state;
4232 pim->pim = NULL;
4233 pim->result = NFA_PIM_TODO;
4234
4235 /* t->state->out1 is the corresponding END_INVISIBLE
4236 * node; Add its out to the current list (zero-width
4237 * match). */
4238 addstate_here(thislist, t->state->out1->out, &t->subs,
4239 pim, &listidx);
4240 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004241 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004242 break;
4243
4244 case NFA_BOL:
4245 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004246 addstate_here(thislist, t->state->out, &t->subs,
4247 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248 break;
4249
4250 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004251 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004252 addstate_here(thislist, t->state->out, &t->subs,
4253 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004254 break;
4255
4256 case NFA_BOW:
4257 {
4258 int bow = TRUE;
4259
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004260 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004261 bow = FALSE;
4262#ifdef FEAT_MBYTE
4263 else if (has_mbyte)
4264 {
4265 int this_class;
4266
4267 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004268 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269 if (this_class <= 1)
4270 bow = FALSE;
4271 else if (reg_prev_class() == this_class)
4272 bow = FALSE;
4273 }
4274#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004275 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004276 || (reginput > regline
4277 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004278 bow = FALSE;
4279 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004280 addstate_here(thislist, t->state->out, &t->subs,
4281 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004282 break;
4283 }
4284
4285 case NFA_EOW:
4286 {
4287 int eow = TRUE;
4288
4289 if (reginput == regline)
4290 eow = FALSE;
4291#ifdef FEAT_MBYTE
4292 else if (has_mbyte)
4293 {
4294 int this_class, prev_class;
4295
4296 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004297 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298 prev_class = reg_prev_class();
4299 if (this_class == prev_class
4300 || prev_class == 0 || prev_class == 1)
4301 eow = FALSE;
4302 }
4303#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004304 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004305 || (reginput[0] != NUL
4306 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004307 eow = FALSE;
4308 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004309 addstate_here(thislist, t->state->out, &t->subs,
4310 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004311 break;
4312 }
4313
Bram Moolenaar4b780632013-05-31 22:14:52 +02004314 case NFA_BOF:
4315 if (reglnum == 0 && reginput == regline
4316 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004317 addstate_here(thislist, t->state->out, &t->subs,
4318 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004319 break;
4320
4321 case NFA_EOF:
4322 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004323 addstate_here(thislist, t->state->out, &t->subs,
4324 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004325 break;
4326
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004327#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004328 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004329 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004330 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004331 int len = 0;
4332 nfa_state_T *end;
4333 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004334 int cchars[MAX_MCO];
4335 int ccount = 0;
4336 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004337
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004338 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004339 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004340 if (utf_iscomposing(sta->c))
4341 {
4342 /* Only match composing character(s), ignore base
4343 * character. Used for ".{composing}" and "{composing}"
4344 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004345 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004346 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004347 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004348 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004349 /* If \Z was present, then ignore composing characters.
4350 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004351 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004352 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004353 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004354 else
4355 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004356 while (sta->c != NFA_END_COMPOSING)
4357 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004358 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004359
4360 /* Check base character matches first, unless ignored. */
4361 else if (len > 0 || mc == sta->c)
4362 {
4363 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004364 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004365 len += mb_char2len(mc);
4366 sta = sta->out;
4367 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004368
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004369 /* We don't care about the order of composing characters.
4370 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004371 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004372 {
4373 mc = mb_ptr2char(reginput + len);
4374 cchars[ccount++] = mc;
4375 len += mb_char2len(mc);
4376 if (ccount == MAX_MCO)
4377 break;
4378 }
4379
4380 /* Check that each composing char in the pattern matches a
4381 * composing char in the text. We do not check if all
4382 * composing chars are matched. */
4383 result = OK;
4384 while (sta->c != NFA_END_COMPOSING)
4385 {
4386 for (j = 0; j < ccount; ++j)
4387 if (cchars[j] == sta->c)
4388 break;
4389 if (j == ccount)
4390 {
4391 result = FAIL;
4392 break;
4393 }
4394 sta = sta->out;
4395 }
4396 }
4397 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004398 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004399
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004400 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004401 ADD_POS_NEG_STATE(end);
4402 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004403 }
4404#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004405
4406 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004407 if (curc == NUL && !reg_line_lbr && REG_MULTI
4408 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004409 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004410 go_to_nextline = TRUE;
4411 /* Pass -1 for the offset, which means taking the position
4412 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004413 ll = nextlist;
4414 add_state = t->state->out;
4415 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004416 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004417 else if (curc == '\n' && reg_line_lbr)
4418 {
4419 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004420 ll = nextlist;
4421 add_state = t->state->out;
4422 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004423 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004424 break;
4425
4426 case NFA_CLASS_ALNUM:
4427 case NFA_CLASS_ALPHA:
4428 case NFA_CLASS_BLANK:
4429 case NFA_CLASS_CNTRL:
4430 case NFA_CLASS_DIGIT:
4431 case NFA_CLASS_GRAPH:
4432 case NFA_CLASS_LOWER:
4433 case NFA_CLASS_PRINT:
4434 case NFA_CLASS_PUNCT:
4435 case NFA_CLASS_SPACE:
4436 case NFA_CLASS_UPPER:
4437 case NFA_CLASS_XDIGIT:
4438 case NFA_CLASS_TAB:
4439 case NFA_CLASS_RETURN:
4440 case NFA_CLASS_BACKSPACE:
4441 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004442 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 ADD_POS_NEG_STATE(t->state);
4444 break;
4445
4446 case NFA_END_NEG_RANGE:
4447 /* This follows a series of negated nodes, like:
4448 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004449 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004450 {
4451 ll = nextlist;
4452 add_state = t->state->out;
4453 add_off = clen;
4454 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004455 break;
4456
4457 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004458 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004459 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004460 {
4461 ll = nextlist;
4462 add_state = t->state->out;
4463 add_off = clen;
4464 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004465 break;
4466
4467 /*
4468 * Character classes like \a for alpha, \d for digit etc.
4469 */
4470 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004471 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004472 ADD_POS_NEG_STATE(t->state);
4473 break;
4474
4475 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004476 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004477 ADD_POS_NEG_STATE(t->state);
4478 break;
4479
4480 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004481 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004482 ADD_POS_NEG_STATE(t->state);
4483 break;
4484
4485 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004486 result = !VIM_ISDIGIT(curc)
4487 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004488 ADD_POS_NEG_STATE(t->state);
4489 break;
4490
4491 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004492 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004493 ADD_POS_NEG_STATE(t->state);
4494 break;
4495
4496 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004497 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004498 ADD_POS_NEG_STATE(t->state);
4499 break;
4500
4501 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004502 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004503 ADD_POS_NEG_STATE(t->state);
4504 break;
4505
4506 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004507 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004508 ADD_POS_NEG_STATE(t->state);
4509 break;
4510
4511 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004512 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004513 ADD_POS_NEG_STATE(t->state);
4514 break;
4515
4516 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004517 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004518 ADD_POS_NEG_STATE(t->state);
4519 break;
4520
4521 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004522 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004523 ADD_POS_NEG_STATE(t->state);
4524 break;
4525
4526 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004527 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004528 ADD_POS_NEG_STATE(t->state);
4529 break;
4530
4531 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004532 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004533 ADD_POS_NEG_STATE(t->state);
4534 break;
4535
4536 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004537 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004538 ADD_POS_NEG_STATE(t->state);
4539 break;
4540
4541 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004542 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004543 ADD_POS_NEG_STATE(t->state);
4544 break;
4545
4546 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004547 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 ADD_POS_NEG_STATE(t->state);
4549 break;
4550
4551 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004552 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 ADD_POS_NEG_STATE(t->state);
4554 break;
4555
4556 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004557 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004558 ADD_POS_NEG_STATE(t->state);
4559 break;
4560
4561 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004562 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 ADD_POS_NEG_STATE(t->state);
4564 break;
4565
4566 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004567 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004568 ADD_POS_NEG_STATE(t->state);
4569 break;
4570
4571 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004572 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004573 ADD_POS_NEG_STATE(t->state);
4574 break;
4575
4576 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004577 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578 ADD_POS_NEG_STATE(t->state);
4579 break;
4580
4581 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004582 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004583 ADD_POS_NEG_STATE(t->state);
4584 break;
4585
4586 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004587 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588 ADD_POS_NEG_STATE(t->state);
4589 break;
4590
4591 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004592 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004593 ADD_POS_NEG_STATE(t->state);
4594 break;
4595
4596 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004597 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004598 ADD_POS_NEG_STATE(t->state);
4599 break;
4600
Bram Moolenaar5714b802013-05-28 22:03:20 +02004601 case NFA_BACKREF1:
4602 case NFA_BACKREF2:
4603 case NFA_BACKREF3:
4604 case NFA_BACKREF4:
4605 case NFA_BACKREF5:
4606 case NFA_BACKREF6:
4607 case NFA_BACKREF7:
4608 case NFA_BACKREF8:
4609 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004610#ifdef FEAT_SYN_HL
4611 case NFA_ZREF1:
4612 case NFA_ZREF2:
4613 case NFA_ZREF3:
4614 case NFA_ZREF4:
4615 case NFA_ZREF5:
4616 case NFA_ZREF6:
4617 case NFA_ZREF7:
4618 case NFA_ZREF8:
4619 case NFA_ZREF9:
4620#endif
4621 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004622 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004623 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004624 int bytelen;
4625
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004626 if (t->state->c <= NFA_BACKREF9)
4627 {
4628 subidx = t->state->c - NFA_BACKREF1 + 1;
4629 result = match_backref(&t->subs.norm, subidx, &bytelen);
4630 }
4631#ifdef FEAT_SYN_HL
4632 else
4633 {
4634 subidx = t->state->c - NFA_ZREF1 + 1;
4635 result = match_zref(subidx, &bytelen);
4636 }
4637#endif
4638
Bram Moolenaar5714b802013-05-28 22:03:20 +02004639 if (result)
4640 {
4641 if (bytelen == 0)
4642 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004643 /* empty match always works, output of NFA_SKIP to be
4644 * used next */
4645 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004646 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004647 }
4648 else if (bytelen <= clen)
4649 {
4650 /* match current character, jump ahead to out of
4651 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004652 ll = nextlist;
4653 add_state = t->state->out->out;
4654 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004655#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004656 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004657#endif
4658 }
4659 else
4660 {
4661 /* skip ofer the matched characters, set character
4662 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004663 ll = nextlist;
4664 add_state = t->state->out;
4665 add_off = bytelen;
4666 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004667#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004668 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004669#endif
4670 }
4671
4672 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004673 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004674 }
4675 case NFA_SKIP:
4676 /* charater of previous matching \1 .. \9 */
4677 if (t->count - clen <= 0)
4678 {
4679 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004680 ll = nextlist;
4681 add_state = t->state->out;
4682 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004683#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004684 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004685#endif
4686 }
4687 else
4688 {
4689 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004690 ll = nextlist;
4691 add_state = t->state;
4692 add_off = 0;
4693 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004694#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004695 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004696#endif
4697 }
4698 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004699
4700 case NFA_SKIP_CHAR:
4701 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004702 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004703 /* TODO: should not happen? */
4704 break;
4705
Bram Moolenaar423532e2013-05-29 21:14:42 +02004706 case NFA_LNUM:
4707 case NFA_LNUM_GT:
4708 case NFA_LNUM_LT:
4709 result = (REG_MULTI &&
4710 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4711 (long_u)(reglnum + reg_firstlnum)));
4712 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004713 addstate_here(thislist, t->state->out, &t->subs,
4714 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004715 break;
4716
4717 case NFA_COL:
4718 case NFA_COL_GT:
4719 case NFA_COL_LT:
4720 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4721 (long_u)(reginput - regline) + 1);
4722 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004723 addstate_here(thislist, t->state->out, &t->subs,
4724 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004725 break;
4726
4727 case NFA_VCOL:
4728 case NFA_VCOL_GT:
4729 case NFA_VCOL_LT:
4730 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4731 (long_u)win_linetabsize(
4732 reg_win == NULL ? curwin : reg_win,
4733 regline, (colnr_T)(reginput - regline)) + 1);
4734 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004735 addstate_here(thislist, t->state->out, &t->subs,
4736 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004737 break;
4738
4739 case NFA_CURSOR:
4740 result = (reg_win != NULL
4741 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4742 && ((colnr_T)(reginput - regline)
4743 == reg_win->w_cursor.col));
4744 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004745 addstate_here(thislist, t->state->out, &t->subs,
4746 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004747 break;
4748
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004749 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004750 {
4751 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004752
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004753 /* TODO: put this in #ifdef later */
4754 if (c < -256)
4755 EMSGN("INTERNAL: Negative state char: %ld", c);
4756 if (is_Magic(c))
4757 c = un_Magic(c);
4758 result = (c == curc);
4759
4760 if (!result && ireg_ic)
4761 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004762#ifdef FEAT_MBYTE
4763 /* If there is a composing character which is not being
4764 * ignored there can be no match. Match with composing
4765 * character uses NFA_COMPOSING above. */
4766 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004767 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004768 result = FALSE;
4769#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004770 ADD_POS_NEG_STATE(t->state);
4771 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004772 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02004773
4774 } /* switch (t->state->c) */
4775
4776 if (add_state != NULL)
4777 {
4778 if (t->pim != NULL)
4779 {
4780 /* postponed invisible match */
4781 /* TODO: also do t->pim->pim recursively? */
4782 if (t->pim->result == NFA_PIM_TODO)
4783 {
4784#ifdef ENABLE_LOG
4785 fprintf(log_fd, "\n");
4786 fprintf(log_fd, "==================================\n");
4787 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
4788 fprintf(log_fd, "\n");
4789#endif
4790 result = recursive_regmatch(t->pim->state,
4791 prog, submatch, m, &listids);
4792 t->pim->result = result ? NFA_PIM_MATCH
4793 : NFA_PIM_NOMATCH;
4794 /* for \@! it is a match when result is FALSE */
4795 if (result != t->pim->state->negated)
4796 {
4797 /* Copy submatch info from the recursive call */
4798 copy_sub_off(&t->pim->subs.norm, &m->norm);
4799#ifdef FEAT_SYN_HL
4800 copy_sub_off(&t->pim->subs.synt, &m->synt);
4801#endif
4802 }
4803 }
4804 else
4805 {
4806 result = (t->pim->result == NFA_PIM_MATCH);
4807#ifdef ENABLE_LOG
4808 fprintf(log_fd, "\n");
4809 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
4810 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4811 fprintf(log_fd, "\n");
4812#endif
4813 }
4814
4815 /* for \@! it is a match when result is FALSE */
4816 if (result != t->pim->state->negated)
4817 {
4818 /* Copy submatch info from the recursive call */
4819 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
4820#ifdef FEAT_SYN_HL
4821 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
4822#endif
4823 }
4824 else
4825 /* look-behind match failed, don't add the state */
4826 continue;
4827 }
4828
4829 addstate(ll, add_state, &t->subs, add_off);
4830 if (add_count > 0)
4831 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004832 }
4833
4834 } /* for (thislist = thislist; thislist->state; thislist++) */
4835
Bram Moolenaare23febd2013-05-26 18:40:14 +02004836 /* Look for the start of a match in the current position by adding the
4837 * start state to the list of states.
4838 * The first found match is the leftmost one, thus the order of states
4839 * matters!
4840 * Do not add the start state in recursive calls of nfa_regmatch(),
4841 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004842 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004843 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004844 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004845 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004846 && reglnum == 0
4847 && clen != 0
4848 && (ireg_maxcol == 0
4849 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004850 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004851 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004852 ? (reglnum < nfa_endp->se_u.pos.lnum
4853 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004854 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004855 < nfa_endp->se_u.pos.col))
4856 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004857 {
4858#ifdef ENABLE_LOG
4859 fprintf(log_fd, "(---) STARTSTATE\n");
4860#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004861 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004862 }
4863
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004864#ifdef ENABLE_LOG
4865 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004866 {
4867 int i;
4868
4869 for (i = 0; i < thislist->n; i++)
4870 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4871 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872 fprintf(log_fd, "\n");
4873#endif
4874
4875nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004876 /* Advance to the next character, or advance to the next line, or
4877 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004878 if (clen != 0)
4879 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004880 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4881 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004882 reg_nextline();
4883 else
4884 break;
4885 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004886
4887#ifdef ENABLE_LOG
4888 if (log_fd != stderr)
4889 fclose(log_fd);
4890 log_fd = NULL;
4891#endif
4892
4893theend:
4894 /* Free memory */
4895 vim_free(list[0].t);
4896 vim_free(list[1].t);
4897 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004898 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004899 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004900#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004901#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004902 fclose(debug);
4903#endif
4904
Bram Moolenaar963fee22013-05-26 21:47:28 +02004905 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004906}
4907
4908/*
4909 * Try match of "prog" with at regline["col"].
4910 * Returns 0 for failure, number of lines contained in the match otherwise.
4911 */
4912 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004913nfa_regtry(prog, col)
4914 nfa_regprog_T *prog;
4915 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004916{
4917 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004918 regsubs_T subs, m;
4919 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920#ifdef ENABLE_LOG
4921 FILE *f;
4922#endif
4923
4924 reginput = regline + col;
4925 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004926#ifdef FEAT_SYN_HL
4927 /* Clear the external match subpointers if necessary. */
4928 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004929 {
4930 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004931 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004932 }
4933 else
4934 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004935#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004936
4937#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004938 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004939 if (f != NULL)
4940 {
4941 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4942 fprintf(f, " =======================================================\n");
4943#ifdef DEBUG
4944 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4945#endif
4946 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004947 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004948 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004949 fprintf(f, "\n\n");
4950 fclose(f);
4951 }
4952 else
4953 EMSG(_("Could not open temporary log file for writing "));
4954#endif
4955
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004956 clear_sub(&subs.norm);
4957 clear_sub(&m.norm);
4958#ifdef FEAT_SYN_HL
4959 clear_sub(&subs.synt);
4960 clear_sub(&m.synt);
4961#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004962
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004963 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004964 return 0;
4965
4966 cleanup_subexpr();
4967 if (REG_MULTI)
4968 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004969 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004970 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004971 reg_startpos[i] = subs.norm.list.multi[i].start;
4972 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004973 }
4974
4975 if (reg_startpos[0].lnum < 0)
4976 {
4977 reg_startpos[0].lnum = 0;
4978 reg_startpos[0].col = col;
4979 }
4980 if (reg_endpos[0].lnum < 0)
4981 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004982 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004983 reg_endpos[0].lnum = reglnum;
4984 reg_endpos[0].col = (int)(reginput - regline);
4985 }
4986 else
4987 /* Use line number of "\ze". */
4988 reglnum = reg_endpos[0].lnum;
4989 }
4990 else
4991 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004992 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004993 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004994 reg_startp[i] = subs.norm.list.line[i].start;
4995 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004996 }
4997
4998 if (reg_startp[0] == NULL)
4999 reg_startp[0] = regline + col;
5000 if (reg_endp[0] == NULL)
5001 reg_endp[0] = reginput;
5002 }
5003
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005004#ifdef FEAT_SYN_HL
5005 /* Package any found \z(...\) matches for export. Default is none. */
5006 unref_extmatch(re_extmatch_out);
5007 re_extmatch_out = NULL;
5008
5009 if (prog->reghasz == REX_SET)
5010 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005011 cleanup_zsubexpr();
5012 re_extmatch_out = make_extmatch();
5013 for (i = 0; i < subs.synt.in_use; i++)
5014 {
5015 if (REG_MULTI)
5016 {
5017 struct multipos *mpos = &subs.synt.list.multi[i];
5018
5019 /* Only accept single line matches. */
5020 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5021 re_extmatch_out->matches[i] =
5022 vim_strnsave(reg_getline(mpos->start.lnum)
5023 + mpos->start.col,
5024 mpos->end.col - mpos->start.col);
5025 }
5026 else
5027 {
5028 struct linepos *lpos = &subs.synt.list.line[i];
5029
5030 if (lpos->start != NULL && lpos->end != NULL)
5031 re_extmatch_out->matches[i] =
5032 vim_strnsave(lpos->start,
5033 (int)(lpos->end - lpos->start));
5034 }
5035 }
5036 }
5037#endif
5038
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005039 return 1 + reglnum;
5040}
5041
5042/*
5043 * Match a regexp against a string ("line" points to the string) or multiple
5044 * lines ("line" is NULL, use reg_getline()).
5045 *
5046 * Returns 0 for failure, number of lines contained in the match otherwise.
5047 */
5048 static long
5049nfa_regexec_both(line, col)
5050 char_u *line;
5051 colnr_T col; /* column to start looking for match */
5052{
5053 nfa_regprog_T *prog;
5054 long retval = 0L;
5055 int i;
5056
5057 if (REG_MULTI)
5058 {
5059 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5060 line = reg_getline((linenr_T)0); /* relative to the cursor */
5061 reg_startpos = reg_mmatch->startpos;
5062 reg_endpos = reg_mmatch->endpos;
5063 }
5064 else
5065 {
5066 prog = (nfa_regprog_T *)reg_match->regprog;
5067 reg_startp = reg_match->startp;
5068 reg_endp = reg_match->endp;
5069 }
5070
5071 /* Be paranoid... */
5072 if (prog == NULL || line == NULL)
5073 {
5074 EMSG(_(e_null));
5075 goto theend;
5076 }
5077
5078 /* If the start column is past the maximum column: no need to try. */
5079 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5080 goto theend;
5081
5082 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5083 if (prog->regflags & RF_ICASE)
5084 ireg_ic = TRUE;
5085 else if (prog->regflags & RF_NOICASE)
5086 ireg_ic = FALSE;
5087
5088#ifdef FEAT_MBYTE
5089 /* If pattern contains "\Z" overrule value of ireg_icombine */
5090 if (prog->regflags & RF_ICOMBINE)
5091 ireg_icombine = TRUE;
5092#endif
5093
5094 regline = line;
5095 reglnum = 0; /* relative to line */
5096
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005097 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005098 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005099 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005100 nfa_listid = 1;
5101 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005102#ifdef DEBUG
5103 nfa_regengine.expr = prog->pattern;
5104#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005105
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005106 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005107 for (i = 0; i < nstate; ++i)
5108 {
5109 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005110 prog->state[i].lastlist[0] = 0;
5111 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005112 }
5113
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005114 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005115
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005116#ifdef DEBUG
5117 nfa_regengine.expr = NULL;
5118#endif
5119
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005120theend:
5121 return retval;
5122}
5123
5124/*
5125 * Compile a regular expression into internal code for the NFA matcher.
5126 * Returns the program in allocated space. Returns NULL for an error.
5127 */
5128 static regprog_T *
5129nfa_regcomp(expr, re_flags)
5130 char_u *expr;
5131 int re_flags;
5132{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005133 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005134 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005135 int *postfix;
5136
5137 if (expr == NULL)
5138 return NULL;
5139
5140#ifdef DEBUG
5141 nfa_regengine.expr = expr;
5142#endif
5143
5144 init_class_tab();
5145
5146 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5147 return NULL;
5148
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005149 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005150 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005151 postfix = re2post();
5152 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005153 {
5154 /* TODO: only give this error for debugging? */
5155 if (post_ptr >= post_end)
5156 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005157 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005158 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005159
5160 /*
5161 * In order to build the NFA, we parse the input regexp twice:
5162 * 1. first pass to count size (so we can allocate space)
5163 * 2. second to emit code
5164 */
5165#ifdef ENABLE_LOG
5166 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005167 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005168
5169 if (f != NULL)
5170 {
5171 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5172 fclose(f);
5173 }
5174 }
5175#endif
5176
5177 /*
5178 * PASS 1
5179 * Count number of NFA states in "nstate". Do not build the NFA.
5180 */
5181 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005182
5183 /* Space for compiled regexp */
5184 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5185 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5186 if (prog == NULL)
5187 goto fail;
5188 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005189 state_ptr = prog->state;
5190
5191 /*
5192 * PASS 2
5193 * Build the NFA
5194 */
5195 prog->start = post2nfa(postfix, post_ptr, FALSE);
5196 if (prog->start == NULL)
5197 goto fail;
5198
5199 prog->regflags = regflags;
5200 prog->engine = &nfa_regengine;
5201 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005202 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005203 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005204 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005205#ifdef ENABLE_LOG
5206 nfa_postfix_dump(expr, OK);
5207 nfa_dump(prog);
5208#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005209#ifdef FEAT_SYN_HL
5210 /* Remember whether this pattern has any \z specials in it. */
5211 prog->reghasz = re_has_z;
5212#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005213#ifdef DEBUG
5214 prog->pattern = vim_strsave(expr); /* memory will leak */
5215 nfa_regengine.expr = NULL;
5216#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005217
5218out:
5219 vim_free(post_start);
5220 post_start = post_ptr = post_end = NULL;
5221 state_ptr = NULL;
5222 return (regprog_T *)prog;
5223
5224fail:
5225 vim_free(prog);
5226 prog = NULL;
5227#ifdef ENABLE_LOG
5228 nfa_postfix_dump(expr, FAIL);
5229#endif
5230#ifdef DEBUG
5231 nfa_regengine.expr = NULL;
5232#endif
5233 goto out;
5234}
5235
5236
5237/*
5238 * Match a regexp against a string.
5239 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5240 * Uses curbuf for line count and 'iskeyword'.
5241 *
5242 * Return TRUE if there is a match, FALSE if not.
5243 */
5244 static int
5245nfa_regexec(rmp, line, col)
5246 regmatch_T *rmp;
5247 char_u *line; /* string to match against */
5248 colnr_T col; /* column to start looking for match */
5249{
5250 reg_match = rmp;
5251 reg_mmatch = NULL;
5252 reg_maxline = 0;
5253 reg_line_lbr = FALSE;
5254 reg_buf = curbuf;
5255 reg_win = NULL;
5256 ireg_ic = rmp->rm_ic;
5257#ifdef FEAT_MBYTE
5258 ireg_icombine = FALSE;
5259#endif
5260 ireg_maxcol = 0;
5261 return (nfa_regexec_both(line, col) != 0);
5262}
5263
5264#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5265 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5266
5267static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5268
5269/*
5270 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5271 */
5272 static int
5273nfa_regexec_nl(rmp, line, col)
5274 regmatch_T *rmp;
5275 char_u *line; /* string to match against */
5276 colnr_T col; /* column to start looking for match */
5277{
5278 reg_match = rmp;
5279 reg_mmatch = NULL;
5280 reg_maxline = 0;
5281 reg_line_lbr = TRUE;
5282 reg_buf = curbuf;
5283 reg_win = NULL;
5284 ireg_ic = rmp->rm_ic;
5285#ifdef FEAT_MBYTE
5286 ireg_icombine = FALSE;
5287#endif
5288 ireg_maxcol = 0;
5289 return (nfa_regexec_both(line, col) != 0);
5290}
5291#endif
5292
5293
5294/*
5295 * Match a regexp against multiple lines.
5296 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5297 * Uses curbuf for line count and 'iskeyword'.
5298 *
5299 * Return zero if there is no match. Return number of lines contained in the
5300 * match otherwise.
5301 *
5302 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5303 *
5304 * ! Also NOTE : match may actually be in another line. e.g.:
5305 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5306 *
5307 * +-------------------------+
5308 * |a |
5309 * |b |
5310 * |c |
5311 * | |
5312 * +-------------------------+
5313 *
5314 * then nfa_regexec_multi() returns 3. while the original
5315 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5316 *
5317 * FIXME if this behavior is not compatible.
5318 */
5319 static long
5320nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5321 regmmatch_T *rmp;
5322 win_T *win; /* window in which to search or NULL */
5323 buf_T *buf; /* buffer in which to search */
5324 linenr_T lnum; /* nr of line to start looking for match */
5325 colnr_T col; /* column to start looking for match */
5326 proftime_T *tm UNUSED; /* timeout limit or NULL */
5327{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005328 reg_match = NULL;
5329 reg_mmatch = rmp;
5330 reg_buf = buf;
5331 reg_win = win;
5332 reg_firstlnum = lnum;
5333 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5334 reg_line_lbr = FALSE;
5335 ireg_ic = rmp->rmm_ic;
5336#ifdef FEAT_MBYTE
5337 ireg_icombine = FALSE;
5338#endif
5339 ireg_maxcol = rmp->rmm_maxcol;
5340
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005341 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005342}
5343
5344#ifdef DEBUG
5345# undef ENABLE_LOG
5346#endif