blob: 75cbb2ae4527bfaef70808709ce233b377ece5a4 [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 Moolenaar963fee22013-05-26 21:47:28 +02002827/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002828typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002829{
2830 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002831 int count;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002832 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002833} nfa_thread_T;
2834
Bram Moolenaar963fee22013-05-26 21:47:28 +02002835/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836typedef struct
2837{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002838 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002839 int n; /* nr of states currently in "t" */
2840 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002841 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002842} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002843
Bram Moolenaar5714b802013-05-28 22:03:20 +02002844#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002845static void log_subsexpr __ARGS((regsubs_T *subs));
2846static void log_subexpr __ARGS((regsub_T *sub));
2847
2848 static void
2849log_subsexpr(subs)
2850 regsubs_T *subs;
2851{
2852 log_subexpr(&subs->norm);
2853# ifdef FEAT_SYN_HL
2854 log_subexpr(&subs->synt);
2855# endif
2856}
2857
Bram Moolenaar5714b802013-05-28 22:03:20 +02002858 static void
2859log_subexpr(sub)
2860 regsub_T *sub;
2861{
2862 int j;
2863
2864 for (j = 0; j < sub->in_use; j++)
2865 if (REG_MULTI)
2866 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2867 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002868 sub->list.multi[j].start.col,
2869 (int)sub->list.multi[j].start.lnum,
2870 sub->list.multi[j].end.col,
2871 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002872 else
2873 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2874 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002875 (char *)sub->list.line[j].start,
2876 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002877 fprintf(log_fd, "\n");
2878}
2879#endif
2880
Bram Moolenaar963fee22013-05-26 21:47:28 +02002881/* Used during execution: whether a match has been found. */
2882static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002883
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002884static void clear_sub __ARGS((regsub_T *sub));
2885static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2886static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002887static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002888static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
2889static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int *ip));
2890
2891 static void
2892clear_sub(sub)
2893 regsub_T *sub;
2894{
2895 if (REG_MULTI)
2896 /* Use 0xff to set lnum to -1 */
2897 vim_memset(sub->list.multi, 0xff,
2898 sizeof(struct multipos) * nfa_nsubexpr);
2899 else
2900 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2901 sub->in_use = 0;
2902}
2903
2904/*
2905 * Copy the submatches from "from" to "to".
2906 */
2907 static void
2908copy_sub(to, from)
2909 regsub_T *to;
2910 regsub_T *from;
2911{
2912 to->in_use = from->in_use;
2913 if (from->in_use > 0)
2914 {
2915 /* Copy the match start and end positions. */
2916 if (REG_MULTI)
2917 mch_memmove(&to->list.multi[0],
2918 &from->list.multi[0],
2919 sizeof(struct multipos) * from->in_use);
2920 else
2921 mch_memmove(&to->list.line[0],
2922 &from->list.line[0],
2923 sizeof(struct linepos) * from->in_use);
2924 }
2925}
2926
2927/*
2928 * Like copy_sub() but exclude the main match.
2929 */
2930 static void
2931copy_sub_off(to, from)
2932 regsub_T *to;
2933 regsub_T *from;
2934{
2935 if (to->in_use < from->in_use)
2936 to->in_use = from->in_use;
2937 if (from->in_use > 1)
2938 {
2939 /* Copy the match start and end positions. */
2940 if (REG_MULTI)
2941 mch_memmove(&to->list.multi[1],
2942 &from->list.multi[1],
2943 sizeof(struct multipos) * (from->in_use - 1));
2944 else
2945 mch_memmove(&to->list.line[1],
2946 &from->list.line[1],
2947 sizeof(struct linepos) * (from->in_use - 1));
2948 }
2949}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950
Bram Moolenaar428e9872013-05-30 17:05:39 +02002951/*
2952 * Return TRUE if "sub1" and "sub2" have the same positions.
2953 */
2954 static int
2955sub_equal(sub1, sub2)
2956 regsub_T *sub1;
2957 regsub_T *sub2;
2958{
2959 int i;
2960 int todo;
2961 linenr_T s1, e1;
2962 linenr_T s2, e2;
2963 char_u *sp1, *ep1;
2964 char_u *sp2, *ep2;
2965
2966 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2967 if (REG_MULTI)
2968 {
2969 for (i = 0; i < todo; ++i)
2970 {
2971 if (i < sub1->in_use)
2972 {
2973 s1 = sub1->list.multi[i].start.lnum;
2974 e1 = sub1->list.multi[i].end.lnum;
2975 }
2976 else
2977 {
2978 s1 = 0;
2979 e1 = 0;
2980 }
2981 if (i < sub2->in_use)
2982 {
2983 s2 = sub2->list.multi[i].start.lnum;
2984 e2 = sub2->list.multi[i].end.lnum;
2985 }
2986 else
2987 {
2988 s2 = 0;
2989 e2 = 0;
2990 }
2991 if (s1 != s2 || e1 != e2)
2992 return FALSE;
2993 if (s1 != 0 && sub1->list.multi[i].start.col
2994 != sub2->list.multi[i].start.col)
2995 return FALSE;
2996 if (e1 != 0 && sub1->list.multi[i].end.col
2997 != sub2->list.multi[i].end.col)
2998 return FALSE;
2999 }
3000 }
3001 else
3002 {
3003 for (i = 0; i < todo; ++i)
3004 {
3005 if (i < sub1->in_use)
3006 {
3007 sp1 = sub1->list.line[i].start;
3008 ep1 = sub1->list.line[i].end;
3009 }
3010 else
3011 {
3012 sp1 = NULL;
3013 ep1 = NULL;
3014 }
3015 if (i < sub2->in_use)
3016 {
3017 sp2 = sub2->list.line[i].start;
3018 ep2 = sub2->list.line[i].end;
3019 }
3020 else
3021 {
3022 sp2 = NULL;
3023 ep2 = NULL;
3024 }
3025 if (sp1 != sp2 || ep1 != ep2)
3026 return FALSE;
3027 }
3028 }
3029
3030 return TRUE;
3031}
3032
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003033#ifdef ENABLE_LOG
3034 static void
3035report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid);
3036{
3037 int col;
3038
3039 if (sub->in_use <= 0)
3040 col = -1;
3041 else if (REG_MULTI)
3042 col = sub->list.multi[0].start.col;
3043 else
3044 col = (int)(sub->list.line[0].start - regline);
3045 nfa_set_code(state->c);
3046 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3047 action, abs(state->id), lid, state->c, code, col);
3048}
3049#endif
3050
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003051 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003052addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003053 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003054 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003055 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003056 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003057{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003058 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003059 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003060 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003061 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003062 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003063 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003064 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003065#ifdef ENABLE_LOG
3066 int did_print = FALSE;
3067#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003068
3069 if (l == NULL || state == NULL)
3070 return;
3071
3072 switch (state->c)
3073 {
3074 case NFA_SPLIT:
3075 case NFA_NOT:
3076 case NFA_NOPEN:
3077 case NFA_NCLOSE:
3078 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003079 case NFA_MCLOSE1:
3080 case NFA_MCLOSE2:
3081 case NFA_MCLOSE3:
3082 case NFA_MCLOSE4:
3083 case NFA_MCLOSE5:
3084 case NFA_MCLOSE6:
3085 case NFA_MCLOSE7:
3086 case NFA_MCLOSE8:
3087 case NFA_MCLOSE9:
3088#ifdef FEAT_SYN_HL
3089 case NFA_ZCLOSE:
3090 case NFA_ZCLOSE1:
3091 case NFA_ZCLOSE2:
3092 case NFA_ZCLOSE3:
3093 case NFA_ZCLOSE4:
3094 case NFA_ZCLOSE5:
3095 case NFA_ZCLOSE6:
3096 case NFA_ZCLOSE7:
3097 case NFA_ZCLOSE8:
3098 case NFA_ZCLOSE9:
3099#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003100 /* These nodes are not added themselves but their "out" and/or
3101 * "out1" may be added below. */
3102 break;
3103
3104 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003105 case NFA_MOPEN1:
3106 case NFA_MOPEN2:
3107 case NFA_MOPEN3:
3108 case NFA_MOPEN4:
3109 case NFA_MOPEN5:
3110 case NFA_MOPEN6:
3111 case NFA_MOPEN7:
3112 case NFA_MOPEN8:
3113 case NFA_MOPEN9:
3114#ifdef FEAT_SYN_HL
3115 case NFA_ZOPEN:
3116 case NFA_ZOPEN1:
3117 case NFA_ZOPEN2:
3118 case NFA_ZOPEN3:
3119 case NFA_ZOPEN4:
3120 case NFA_ZOPEN5:
3121 case NFA_ZOPEN6:
3122 case NFA_ZOPEN7:
3123 case NFA_ZOPEN8:
3124 case NFA_ZOPEN9:
3125#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003126 /* These nodes do not need to be added, but we need to bail out
3127 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003128 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003129 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003130 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003131 break;
3132
Bram Moolenaar307aa162013-06-02 16:34:21 +02003133 case NFA_BOL:
3134 case NFA_BOF:
3135 /* "^" won't match past end-of-line, don't bother trying.
3136 * Except when we are going to the next line for a look-behind
3137 * match. */
3138 if (reginput > regline
3139 && (nfa_endp == NULL
3140 || !REG_MULTI
3141 || reglnum == nfa_endp->se_u.pos.lnum))
3142 goto skip_add;
3143 /* FALLTHROUGH */
3144
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003145 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003146 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003147 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003148 /* This state is already in the list, don't add it again,
3149 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003150 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003151 {
3152skip_add:
3153#ifdef ENABLE_LOG
3154 nfa_set_code(state->c);
3155 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3156 abs(state->id), l->id, state->c, code);
3157#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003158 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003159 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003160
3161 /* See if the same state is already in the list with the same
3162 * positions. */
3163 for (i = 0; i < l->n; ++i)
3164 {
3165 thread = &l->t[i];
3166 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003167 && sub_equal(&thread->subs.norm, &subs->norm)
3168#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003169 && (!nfa_has_zsubexpr ||
3170 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003171#endif
3172 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003173 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003174 }
3175 }
3176
3177 /* when there are backreferences the number of states may be (a
3178 * lot) bigger */
3179 if (nfa_has_backref && l->n == l->len)
3180 {
3181 int newlen = l->len * 3 / 2 + 50;
3182
3183 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3184 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003185 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003186
3187 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003188 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003189 thread = &l->t[l->n++];
3190 thread->state = state;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003191 copy_sub(&thread->subs.norm, &subs->norm);
3192#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003193 if (nfa_has_zsubexpr)
3194 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003195#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003196#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003197 report_state("Adding", &thread->subs.norm, state, l->id);
3198 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003199#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 }
3201
3202#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003203 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003204 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205#endif
3206 switch (state->c)
3207 {
3208 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003209 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003210 break;
3211
3212 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003213 /* order matters here */
3214 addstate(l, state->out, subs, off);
3215 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216 break;
3217
Bram Moolenaar5714b802013-05-28 22:03:20 +02003218 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219 case NFA_NOPEN:
3220 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003221 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003222 break;
3223
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003224 case NFA_MOPEN:
3225 case NFA_MOPEN1:
3226 case NFA_MOPEN2:
3227 case NFA_MOPEN3:
3228 case NFA_MOPEN4:
3229 case NFA_MOPEN5:
3230 case NFA_MOPEN6:
3231 case NFA_MOPEN7:
3232 case NFA_MOPEN8:
3233 case NFA_MOPEN9:
3234#ifdef FEAT_SYN_HL
3235 case NFA_ZOPEN:
3236 case NFA_ZOPEN1:
3237 case NFA_ZOPEN2:
3238 case NFA_ZOPEN3:
3239 case NFA_ZOPEN4:
3240 case NFA_ZOPEN5:
3241 case NFA_ZOPEN6:
3242 case NFA_ZOPEN7:
3243 case NFA_ZOPEN8:
3244 case NFA_ZOPEN9:
3245#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003246 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003248 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003250 sub = &subs->norm;
3251 }
3252#ifdef FEAT_SYN_HL
3253 else if (state->c >= NFA_ZOPEN)
3254 {
3255 subidx = state->c - NFA_ZOPEN;
3256 sub = &subs->synt;
3257 }
3258#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003259 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003260 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003261 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003262 sub = &subs->norm;
3263 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003264
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003265 /* Set the position (with "off") in the subexpression. Save and
3266 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003267 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 if (REG_MULTI)
3269 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003270 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003271 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003272 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003273 save_in_use = -1;
3274 }
3275 else
3276 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003277 save_in_use = sub->in_use;
3278 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003279 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003280 sub->list.multi[i].start.lnum = -1;
3281 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003282 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003283 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003284 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003285 if (off == -1)
3286 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003287 sub->list.multi[subidx].start.lnum = reglnum + 1;
3288 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003289 }
3290 else
3291 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003292 sub->list.multi[subidx].start.lnum = reglnum;
3293 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003294 (colnr_T)(reginput - regline + off);
3295 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 }
3297 else
3298 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003299 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003300 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003301 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003302 save_in_use = -1;
3303 }
3304 else
3305 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003306 save_in_use = sub->in_use;
3307 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003308 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003309 sub->list.line[i].start = NULL;
3310 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003311 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003312 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003313 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003314 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003315 }
3316
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003317 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003319 if (save_in_use == -1)
3320 {
3321 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003322 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003323 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003324 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003325 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003326 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003327 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328 break;
3329
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003330 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003331 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003333 /* Do not overwrite the position set by \ze. If no \ze
3334 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003335 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003336 break;
3337 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003338 case NFA_MCLOSE1:
3339 case NFA_MCLOSE2:
3340 case NFA_MCLOSE3:
3341 case NFA_MCLOSE4:
3342 case NFA_MCLOSE5:
3343 case NFA_MCLOSE6:
3344 case NFA_MCLOSE7:
3345 case NFA_MCLOSE8:
3346 case NFA_MCLOSE9:
3347#ifdef FEAT_SYN_HL
3348 case NFA_ZCLOSE:
3349 case NFA_ZCLOSE1:
3350 case NFA_ZCLOSE2:
3351 case NFA_ZCLOSE3:
3352 case NFA_ZCLOSE4:
3353 case NFA_ZCLOSE5:
3354 case NFA_ZCLOSE6:
3355 case NFA_ZCLOSE7:
3356 case NFA_ZCLOSE8:
3357 case NFA_ZCLOSE9:
3358#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003359 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003361 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003363 sub = &subs->norm;
3364 }
3365#ifdef FEAT_SYN_HL
3366 else if (state->c >= NFA_ZCLOSE)
3367 {
3368 subidx = state->c - NFA_ZCLOSE;
3369 sub = &subs->synt;
3370 }
3371#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003372 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003373 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003374 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003375 sub = &subs->norm;
3376 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003377
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003378 /* We don't fill in gaps here, there must have been an MOPEN that
3379 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003380 save_in_use = sub->in_use;
3381 if (sub->in_use <= subidx)
3382 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003383 if (REG_MULTI)
3384 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003385 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003386 if (off == -1)
3387 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003388 sub->list.multi[subidx].end.lnum = reglnum + 1;
3389 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003390 }
3391 else
3392 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003393 sub->list.multi[subidx].end.lnum = reglnum;
3394 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003395 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003396 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003397 }
3398 else
3399 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003400 save_ptr = sub->list.line[subidx].end;
3401 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402 }
3403
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003404 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003405
3406 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003407 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003408 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003409 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003410 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411 break;
3412 }
3413}
3414
3415/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003416 * Like addstate(), but the new state(s) are put at position "*ip".
3417 * Used for zero-width matches, next state to use is the added one.
3418 * This makes sure the order of states to be tried does not change, which
3419 * matters for alternatives.
3420 */
3421 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003422addstate_here(l, state, subs, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003423 nfa_list_T *l; /* runtime state list */
3424 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003425 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003426 int *ip;
3427{
3428 int tlen = l->n;
3429 int count;
3430 int i = *ip;
3431
3432 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003433 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003434
3435 /* when "*ip" was at the end of the list, nothing to do */
3436 if (i + 1 == tlen)
3437 return;
3438
3439 /* re-order to put the new state at the current position */
3440 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003441 if (count == 1)
3442 {
3443 /* overwrite the current state */
3444 l->t[i] = l->t[l->n - 1];
3445 }
3446 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003447 {
3448 /* make space for new states, then move them from the
3449 * end to the current position */
3450 mch_memmove(&(l->t[i + count]),
3451 &(l->t[i + 1]),
3452 sizeof(nfa_thread_T) * (l->n - i - 1));
3453 mch_memmove(&(l->t[i]),
3454 &(l->t[l->n - 1]),
3455 sizeof(nfa_thread_T) * count);
3456 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003457 --l->n;
3458 *ip = i - 1;
3459}
3460
3461/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462 * Check character class "class" against current character c.
3463 */
3464 static int
3465check_char_class(class, c)
3466 int class;
3467 int c;
3468{
3469 switch (class)
3470 {
3471 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003472 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003473 return OK;
3474 break;
3475 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003476 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003477 return OK;
3478 break;
3479 case NFA_CLASS_BLANK:
3480 if (c == ' ' || c == '\t')
3481 return OK;
3482 break;
3483 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003484 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 return OK;
3486 break;
3487 case NFA_CLASS_DIGIT:
3488 if (VIM_ISDIGIT(c))
3489 return OK;
3490 break;
3491 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003492 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 return OK;
3494 break;
3495 case NFA_CLASS_LOWER:
3496 if (MB_ISLOWER(c))
3497 return OK;
3498 break;
3499 case NFA_CLASS_PRINT:
3500 if (vim_isprintc(c))
3501 return OK;
3502 break;
3503 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003504 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003505 return OK;
3506 break;
3507 case NFA_CLASS_SPACE:
3508 if ((c >=9 && c <= 13) || (c == ' '))
3509 return OK;
3510 break;
3511 case NFA_CLASS_UPPER:
3512 if (MB_ISUPPER(c))
3513 return OK;
3514 break;
3515 case NFA_CLASS_XDIGIT:
3516 if (vim_isxdigit(c))
3517 return OK;
3518 break;
3519 case NFA_CLASS_TAB:
3520 if (c == '\t')
3521 return OK;
3522 break;
3523 case NFA_CLASS_RETURN:
3524 if (c == '\r')
3525 return OK;
3526 break;
3527 case NFA_CLASS_BACKSPACE:
3528 if (c == '\b')
3529 return OK;
3530 break;
3531 case NFA_CLASS_ESCAPE:
3532 if (c == '\033')
3533 return OK;
3534 break;
3535
3536 default:
3537 /* should not be here :P */
3538 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3539 }
3540 return FAIL;
3541}
3542
Bram Moolenaar5714b802013-05-28 22:03:20 +02003543static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3544
3545/*
3546 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003547 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003548 */
3549 static int
3550match_backref(sub, subidx, bytelen)
3551 regsub_T *sub; /* pointers to subexpressions */
3552 int subidx;
3553 int *bytelen; /* out: length of match in bytes */
3554{
3555 int len;
3556
3557 if (sub->in_use <= subidx)
3558 {
3559retempty:
3560 /* backref was not set, match an empty string */
3561 *bytelen = 0;
3562 return TRUE;
3563 }
3564
3565 if (REG_MULTI)
3566 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003567 if (sub->list.multi[subidx].start.lnum < 0
3568 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003569 goto retempty;
3570 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003571 len = sub->list.multi[subidx].end.col
3572 - sub->list.multi[subidx].start.col;
3573 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003574 reginput, &len) == 0)
3575 {
3576 *bytelen = len;
3577 return TRUE;
3578 }
3579 }
3580 else
3581 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003582 if (sub->list.line[subidx].start == NULL
3583 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003584 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003585 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3586 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003587 {
3588 *bytelen = len;
3589 return TRUE;
3590 }
3591 }
3592 return FALSE;
3593}
3594
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003595#ifdef FEAT_SYN_HL
3596
3597static int match_zref __ARGS((int subidx, int *bytelen));
3598
3599/*
3600 * Check for a match with \z subexpression "subidx".
3601 * Return TRUE if it matches.
3602 */
3603 static int
3604match_zref(subidx, bytelen)
3605 int subidx;
3606 int *bytelen; /* out: length of match in bytes */
3607{
3608 int len;
3609
3610 cleanup_zsubexpr();
3611 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3612 {
3613 /* backref was not set, match an empty string */
3614 *bytelen = 0;
3615 return TRUE;
3616 }
3617
3618 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3619 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3620 {
3621 *bytelen = len;
3622 return TRUE;
3623 }
3624 return FALSE;
3625}
3626#endif
3627
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003628/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003629 * Save list IDs for all NFA states of "prog" into "list".
3630 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003631 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632 */
3633 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003634nfa_save_listids(prog, list)
3635 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 int *list;
3637{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003638 int i;
3639 nfa_state_T *p;
3640
3641 /* Order in the list is reverse, it's a bit faster that way. */
3642 p = &prog->state[0];
3643 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003645 list[i] = p->lastlist[1];
3646 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003647 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003648 }
3649}
3650
3651/*
3652 * Restore list IDs from "list" to all NFA states.
3653 */
3654 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003655nfa_restore_listids(prog, list)
3656 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003657 int *list;
3658{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003659 int i;
3660 nfa_state_T *p;
3661
3662 p = &prog->state[0];
3663 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003664 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003665 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003666 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003667 }
3668}
3669
Bram Moolenaar423532e2013-05-29 21:14:42 +02003670 static int
3671nfa_re_num_cmp(val, op, pos)
3672 long_u val;
3673 int op;
3674 long_u pos;
3675{
3676 if (op == 1) return pos > val;
3677 if (op == 2) return pos < val;
3678 return val == pos;
3679}
3680
Bram Moolenaarf46da702013-06-02 22:37:42 +02003681static 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 +02003682static 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 +02003683
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003684/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003685 * Recursively call nfa_regmatch()
3686 */
3687 static int
3688recursive_regmatch(state, prog, submatch, m, listids)
3689 nfa_state_T *state;
3690 nfa_regprog_T *prog;
3691 regsubs_T *submatch;
3692 regsubs_T *m;
3693 int **listids;
3694{
3695 char_u *save_reginput = reginput;
3696 char_u *save_regline = regline;
3697 int save_reglnum = reglnum;
3698 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003699 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003700 save_se_T *save_nfa_endp = nfa_endp;
3701 save_se_T endpos;
3702 save_se_T *endposp = NULL;
3703 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003704 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003705
3706 if (state->c == NFA_START_INVISIBLE_BEFORE)
3707 {
3708 /* The recursive match must end at the current position. */
3709 endposp = &endpos;
3710 if (REG_MULTI)
3711 {
3712 endpos.se_u.pos.col = (int)(reginput - regline);
3713 endpos.se_u.pos.lnum = reglnum;
3714 }
3715 else
3716 endpos.se_u.ptr = reginput;
3717
3718 /* Go back the specified number of bytes, or as far as the
3719 * start of the previous line, to try matching "\@<=" or
3720 * not matching "\@<!".
3721 * TODO: This is very inefficient! Would be better to
3722 * first check for a match with what follows. */
3723 if (state->val <= 0)
3724 {
3725 if (REG_MULTI)
3726 {
3727 regline = reg_getline(--reglnum);
3728 if (regline == NULL)
3729 /* can't go before the first line */
3730 regline = reg_getline(++reglnum);
3731 }
3732 reginput = regline;
3733 }
3734 else
3735 {
3736 if (REG_MULTI && (int)(reginput - regline) < state->val)
3737 {
3738 /* Not enough bytes in this line, go to end of
3739 * previous line. */
3740 regline = reg_getline(--reglnum);
3741 if (regline == NULL)
3742 {
3743 /* can't go before the first line */
3744 regline = reg_getline(++reglnum);
3745 reginput = regline;
3746 }
3747 else
3748 reginput = regline + STRLEN(regline);
3749 }
3750 if ((int)(reginput - regline) >= state->val)
3751 {
3752 reginput -= state->val;
3753#ifdef FEAT_MBYTE
3754 if (has_mbyte)
3755 reginput -= mb_head_off(regline, reginput);
3756#endif
3757 }
3758 else
3759 reginput = regline;
3760 }
3761 }
3762
Bram Moolenaarf46da702013-06-02 22:37:42 +02003763#ifdef ENABLE_LOG
3764 if (log_fd != stderr)
3765 fclose(log_fd);
3766 log_fd = NULL;
3767#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003768 /* Have to clear the lastlist field of the NFA nodes, so that
3769 * nfa_regmatch() and addstate() can run properly after recursion. */
3770 if (nfa_ll_index == 1)
3771 {
3772 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3773 * values and clear them. */
3774 if (*listids == NULL)
3775 {
3776 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3777 if (*listids == NULL)
3778 {
3779 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3780 return 0;
3781 }
3782 }
3783 nfa_save_listids(prog, *listids);
3784 need_restore = TRUE;
3785 /* any value of nfa_listid will do */
3786 }
3787 else
3788 {
3789 /* First recursive nfa_regmatch() call, switch to the second lastlist
3790 * entry. Make sure nfa_listid is different from a previous recursive
3791 * call, because some states may still have this ID. */
3792 ++nfa_ll_index;
3793 if (nfa_listid <= nfa_alt_listid)
3794 nfa_listid = nfa_alt_listid;
3795 }
3796
3797 /* Call nfa_regmatch() to check if the current concat matches at this
3798 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003799 nfa_endp = endposp;
3800 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003801
3802 if (need_restore)
3803 nfa_restore_listids(prog, *listids);
3804 else
3805 {
3806 --nfa_ll_index;
3807 nfa_alt_listid = nfa_listid;
3808 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003809
3810 /* restore position in input text */
3811 reginput = save_reginput;
3812 regline = save_regline;
3813 reglnum = save_reglnum;
3814 nfa_match = save_nfa_match;
3815 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003816 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003817
3818#ifdef ENABLE_LOG
3819 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3820 if (log_fd != NULL)
3821 {
3822 fprintf(log_fd, "****************************\n");
3823 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3824 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3825 fprintf(log_fd, "****************************\n");
3826 }
3827 else
3828 {
3829 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3830 log_fd = stderr;
3831 }
3832#endif
3833
3834 return result;
3835}
3836
3837/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003838 * Main matching routine.
3839 *
3840 * Run NFA to determine whether it matches reginput.
3841 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003842 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003843 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003844 * Return TRUE if there is a match, FALSE otherwise.
3845 * Note: Caller must ensure that: start != NULL.
3846 */
3847 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003848nfa_regmatch(prog, start, submatch, m)
3849 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003850 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003851 regsubs_T *submatch;
3852 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003853{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 int result;
3855 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003856 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003857 int go_to_nextline = FALSE;
3858 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003859 nfa_list_T list[3];
3860 nfa_list_T *listtbl[2][2];
3861 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003862 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003863 nfa_list_T *thislist;
3864 nfa_list_T *nextlist;
3865 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003866 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003867#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003868 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003869
3870 if (debug == NULL)
3871 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003872 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003873 return FALSE;
3874 }
3875#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003876 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003877
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003878 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003879 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003880 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3881 list[0].len = nstate + 1;
3882 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3883 list[1].len = nstate + 1;
3884 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3885 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003886 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3887 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003888
3889#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003890 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003891 if (log_fd != NULL)
3892 {
3893 fprintf(log_fd, "**********************************\n");
3894 nfa_set_code(start->c);
3895 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3896 abs(start->id), code);
3897 fprintf(log_fd, "**********************************\n");
3898 }
3899 else
3900 {
3901 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3902 log_fd = stderr;
3903 }
3904#endif
3905
3906 thislist = &list[0];
3907 thislist->n = 0;
3908 nextlist = &list[1];
3909 nextlist->n = 0;
3910 neglist = &list[2];
3911 neglist->n = 0;
3912#ifdef ENABLE_LOG
3913 fprintf(log_fd, "(---) STARTSTATE\n");
3914#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003915 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003916 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917
3918 /* There are two cases when the NFA advances: 1. input char matches the
3919 * NFA node and 2. input char does not match the NFA node, but the next
3920 * node is NFA_NOT. The following macro calls addstate() according to
3921 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3922 listtbl[0][0] = NULL;
3923 listtbl[0][1] = neglist;
3924 listtbl[1][0] = nextlist;
3925 listtbl[1][1] = NULL;
3926#define ADD_POS_NEG_STATE(node) \
3927 ll = listtbl[result ? 1 : 0][node->negated]; \
3928 if (ll != NULL) \
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003929 addstate(ll, node->out , &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003930
3931
3932 /*
3933 * Run for each character.
3934 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003935 for (;;)
3936 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003937 int curc;
3938 int clen;
3939
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003940#ifdef FEAT_MBYTE
3941 if (has_mbyte)
3942 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003943 curc = (*mb_ptr2char)(reginput);
3944 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003945 }
3946 else
3947#endif
3948 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003949 curc = *reginput;
3950 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003951 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003952 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003953 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003954 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003955 go_to_nextline = FALSE;
3956 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003957
3958 /* swap lists */
3959 thislist = &list[flag];
3960 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003961 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003962 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003963 ++nfa_listid;
3964 thislist->id = nfa_listid;
3965 nextlist->id = nfa_listid + 1;
3966 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003967
3968#ifdef ENABLE_LOG
3969 fprintf(log_fd, "------------------------------------------\n");
3970 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003971 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003972 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003973 {
3974 int i;
3975
3976 for (i = 0; i < thislist->n; i++)
3977 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3978 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003979 fprintf(log_fd, "\n");
3980#endif
3981
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003982#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003983 fprintf(debug, "\n-------------------\n");
3984#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003985 /*
3986 * If the state lists are empty we can stop.
3987 */
3988 if (thislist->n == 0 && neglist->n == 0)
3989 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003990
3991 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003992 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003993 {
3994 if (neglist->n > 0)
3995 {
3996 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003997 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003998 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003999 }
4000 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004001 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004003#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004004 nfa_set_code(t->state->c);
4005 fprintf(debug, "%s, ", code);
4006#endif
4007#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004008 {
4009 int col;
4010
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004011 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004012 col = -1;
4013 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004014 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004015 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004016 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004017 nfa_set_code(t->state->c);
4018 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4019 abs(t->state->id), (int)t->state->c, code, col);
4020 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004021#endif
4022
4023 /*
4024 * Handle the possible codes of the current state.
4025 * The most important is NFA_MATCH.
4026 */
4027 switch (t->state->c)
4028 {
4029 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004030 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004031 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004032 copy_sub(&submatch->norm, &t->subs.norm);
4033#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004034 if (nfa_has_zsubexpr)
4035 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004036#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004038 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004039#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004040 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004041 * states at this position. When the list of states is going
4042 * to be empty quit without advancing, so that "reginput" is
4043 * correct. */
4044 if (nextlist->n == 0 && neglist->n == 0)
4045 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004046 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004047 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004048
4049 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004050 /*
4051 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004052 * NFA_START_INVISIBLE_BEFORE node.
4053 * They surround a zero-width group, used with "\@=", "\&",
4054 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004055 * If we got here, it means that the current "invisible" group
4056 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004057 * nfa_regmatch(). For a look-behind match only when it ends
4058 * in the position in "nfa_endp".
4059 * Submatches are stored in *m, and used in the parent call.
4060 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004061#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004062 if (nfa_endp != NULL)
4063 {
4064 if (REG_MULTI)
4065 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4066 (int)reglnum,
4067 (int)nfa_endp->se_u.pos.lnum,
4068 (int)(reginput - regline),
4069 nfa_endp->se_u.pos.col);
4070 else
4071 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4072 (int)(reginput - regline),
4073 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004074 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004075#endif
4076 /* It's only a match if it ends at "nfa_endp" */
4077 if (nfa_endp != NULL && (REG_MULTI
4078 ? (reglnum != nfa_endp->se_u.pos.lnum
4079 || (int)(reginput - regline)
4080 != nfa_endp->se_u.pos.col)
4081 : reginput != nfa_endp->se_u.ptr))
4082 break;
4083
4084 /* do not set submatches for \@! */
4085 if (!t->state->negated)
4086 {
4087 copy_sub(&m->norm, &t->subs.norm);
4088#ifdef FEAT_SYN_HL
4089 if (nfa_has_zsubexpr)
4090 copy_sub(&m->synt, &t->subs.synt);
4091#endif
4092 }
4093 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004094 break;
4095
4096 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004097 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004098 result = recursive_regmatch(t->state, prog, submatch, m,
4099 &listids);
Bram Moolenaar61602c52013-06-01 19:54:43 +02004100
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02004101 /* for \@! it is a match when result is FALSE */
4102 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004103 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004104 /* Copy submatch info from the recursive call */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004105 copy_sub_off(&t->subs.norm, &m->norm);
4106#ifdef FEAT_SYN_HL
4107 copy_sub_off(&t->subs.synt, &m->synt);
4108#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004109
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004110 /* t->state->out1 is the corresponding END_INVISIBLE node;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004111 * Add its out to the current list (zero-width match). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004112 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004113 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004114 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004115 break;
4116
4117 case NFA_BOL:
4118 if (reginput == regline)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004119 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004120 break;
4121
4122 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004123 if (curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004124 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004125 break;
4126
4127 case NFA_BOW:
4128 {
4129 int bow = TRUE;
4130
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004131 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004132 bow = FALSE;
4133#ifdef FEAT_MBYTE
4134 else if (has_mbyte)
4135 {
4136 int this_class;
4137
4138 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004139 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004140 if (this_class <= 1)
4141 bow = FALSE;
4142 else if (reg_prev_class() == this_class)
4143 bow = FALSE;
4144 }
4145#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004146 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004147 || (reginput > regline
4148 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004149 bow = FALSE;
4150 if (bow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004151 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004152 break;
4153 }
4154
4155 case NFA_EOW:
4156 {
4157 int eow = TRUE;
4158
4159 if (reginput == regline)
4160 eow = FALSE;
4161#ifdef FEAT_MBYTE
4162 else if (has_mbyte)
4163 {
4164 int this_class, prev_class;
4165
4166 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004167 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004168 prev_class = reg_prev_class();
4169 if (this_class == prev_class
4170 || prev_class == 0 || prev_class == 1)
4171 eow = FALSE;
4172 }
4173#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004174 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004175 || (reginput[0] != NUL
4176 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004177 eow = FALSE;
4178 if (eow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004179 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004180 break;
4181 }
4182
Bram Moolenaar4b780632013-05-31 22:14:52 +02004183 case NFA_BOF:
4184 if (reglnum == 0 && reginput == regline
4185 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004186 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004187 break;
4188
4189 case NFA_EOF:
4190 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004191 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004192 break;
4193
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004194#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004195 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004196 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004197 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004198 int len = 0;
4199 nfa_state_T *end;
4200 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004201 int cchars[MAX_MCO];
4202 int ccount = 0;
4203 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004204
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004205 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004206 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004207 if (utf_iscomposing(sta->c))
4208 {
4209 /* Only match composing character(s), ignore base
4210 * character. Used for ".{composing}" and "{composing}"
4211 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004212 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004213 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004214 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004215 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004216 /* If \Z was present, then ignore composing characters.
4217 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004218 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004219 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004220 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004221 else
4222 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004223 while (sta->c != NFA_END_COMPOSING)
4224 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004226
4227 /* Check base character matches first, unless ignored. */
4228 else if (len > 0 || mc == sta->c)
4229 {
4230 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004231 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004232 len += mb_char2len(mc);
4233 sta = sta->out;
4234 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004236 /* We don't care about the order of composing characters.
4237 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004238 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004239 {
4240 mc = mb_ptr2char(reginput + len);
4241 cchars[ccount++] = mc;
4242 len += mb_char2len(mc);
4243 if (ccount == MAX_MCO)
4244 break;
4245 }
4246
4247 /* Check that each composing char in the pattern matches a
4248 * composing char in the text. We do not check if all
4249 * composing chars are matched. */
4250 result = OK;
4251 while (sta->c != NFA_END_COMPOSING)
4252 {
4253 for (j = 0; j < ccount; ++j)
4254 if (cchars[j] == sta->c)
4255 break;
4256 if (j == ccount)
4257 {
4258 result = FAIL;
4259 break;
4260 }
4261 sta = sta->out;
4262 }
4263 }
4264 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004265 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004266
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004267 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004268 ADD_POS_NEG_STATE(end);
4269 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004270 }
4271#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004272
4273 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004274 if (curc == NUL && !reg_line_lbr && REG_MULTI
4275 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004276 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004277 go_to_nextline = TRUE;
4278 /* Pass -1 for the offset, which means taking the position
4279 * at the start of the next line. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004280 addstate(nextlist, t->state->out, &t->subs, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004281 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004282 else if (curc == '\n' && reg_line_lbr)
4283 {
4284 /* match \n as if it is an ordinary character */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004285 addstate(nextlist, t->state->out, &t->subs, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004286 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004287 break;
4288
4289 case NFA_CLASS_ALNUM:
4290 case NFA_CLASS_ALPHA:
4291 case NFA_CLASS_BLANK:
4292 case NFA_CLASS_CNTRL:
4293 case NFA_CLASS_DIGIT:
4294 case NFA_CLASS_GRAPH:
4295 case NFA_CLASS_LOWER:
4296 case NFA_CLASS_PRINT:
4297 case NFA_CLASS_PUNCT:
4298 case NFA_CLASS_SPACE:
4299 case NFA_CLASS_UPPER:
4300 case NFA_CLASS_XDIGIT:
4301 case NFA_CLASS_TAB:
4302 case NFA_CLASS_RETURN:
4303 case NFA_CLASS_BACKSPACE:
4304 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004305 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004306 ADD_POS_NEG_STATE(t->state);
4307 break;
4308
4309 case NFA_END_NEG_RANGE:
4310 /* This follows a series of negated nodes, like:
4311 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004312 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004313 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004314 break;
4315
4316 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004317 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004318 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004319 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004320 break;
4321
4322 /*
4323 * Character classes like \a for alpha, \d for digit etc.
4324 */
4325 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004326 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004327 ADD_POS_NEG_STATE(t->state);
4328 break;
4329
4330 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004331 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004332 ADD_POS_NEG_STATE(t->state);
4333 break;
4334
4335 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004336 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004337 ADD_POS_NEG_STATE(t->state);
4338 break;
4339
4340 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004341 result = !VIM_ISDIGIT(curc)
4342 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004343 ADD_POS_NEG_STATE(t->state);
4344 break;
4345
4346 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004347 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004348 ADD_POS_NEG_STATE(t->state);
4349 break;
4350
4351 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004352 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004353 ADD_POS_NEG_STATE(t->state);
4354 break;
4355
4356 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004357 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004358 ADD_POS_NEG_STATE(t->state);
4359 break;
4360
4361 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004362 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004363 ADD_POS_NEG_STATE(t->state);
4364 break;
4365
4366 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004367 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004368 ADD_POS_NEG_STATE(t->state);
4369 break;
4370
4371 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004372 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004373 ADD_POS_NEG_STATE(t->state);
4374 break;
4375
4376 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004377 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004378 ADD_POS_NEG_STATE(t->state);
4379 break;
4380
4381 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004382 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004383 ADD_POS_NEG_STATE(t->state);
4384 break;
4385
4386 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004387 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388 ADD_POS_NEG_STATE(t->state);
4389 break;
4390
4391 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004392 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 ADD_POS_NEG_STATE(t->state);
4394 break;
4395
4396 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004397 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004398 ADD_POS_NEG_STATE(t->state);
4399 break;
4400
4401 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004402 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004403 ADD_POS_NEG_STATE(t->state);
4404 break;
4405
4406 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004407 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408 ADD_POS_NEG_STATE(t->state);
4409 break;
4410
4411 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004412 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004413 ADD_POS_NEG_STATE(t->state);
4414 break;
4415
4416 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004417 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004418 ADD_POS_NEG_STATE(t->state);
4419 break;
4420
4421 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004422 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004423 ADD_POS_NEG_STATE(t->state);
4424 break;
4425
4426 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004427 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004428 ADD_POS_NEG_STATE(t->state);
4429 break;
4430
4431 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004432 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004433 ADD_POS_NEG_STATE(t->state);
4434 break;
4435
4436 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004437 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004438 ADD_POS_NEG_STATE(t->state);
4439 break;
4440
4441 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004442 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 ADD_POS_NEG_STATE(t->state);
4444 break;
4445
4446 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004447 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004448 ADD_POS_NEG_STATE(t->state);
4449 break;
4450
4451 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004452 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004453 ADD_POS_NEG_STATE(t->state);
4454 break;
4455
Bram Moolenaar5714b802013-05-28 22:03:20 +02004456 case NFA_BACKREF1:
4457 case NFA_BACKREF2:
4458 case NFA_BACKREF3:
4459 case NFA_BACKREF4:
4460 case NFA_BACKREF5:
4461 case NFA_BACKREF6:
4462 case NFA_BACKREF7:
4463 case NFA_BACKREF8:
4464 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004465#ifdef FEAT_SYN_HL
4466 case NFA_ZREF1:
4467 case NFA_ZREF2:
4468 case NFA_ZREF3:
4469 case NFA_ZREF4:
4470 case NFA_ZREF5:
4471 case NFA_ZREF6:
4472 case NFA_ZREF7:
4473 case NFA_ZREF8:
4474 case NFA_ZREF9:
4475#endif
4476 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004477 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004478 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004479 int bytelen;
4480
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004481 if (t->state->c <= NFA_BACKREF9)
4482 {
4483 subidx = t->state->c - NFA_BACKREF1 + 1;
4484 result = match_backref(&t->subs.norm, subidx, &bytelen);
4485 }
4486#ifdef FEAT_SYN_HL
4487 else
4488 {
4489 subidx = t->state->c - NFA_ZREF1 + 1;
4490 result = match_zref(subidx, &bytelen);
4491 }
4492#endif
4493
Bram Moolenaar5714b802013-05-28 22:03:20 +02004494 if (result)
4495 {
4496 if (bytelen == 0)
4497 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004498 /* empty match always works, output of NFA_SKIP to be
4499 * used next */
4500 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004501 &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004502 }
4503 else if (bytelen <= clen)
4504 {
4505 /* match current character, jump ahead to out of
4506 * NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004507 addstate(nextlist, t->state->out->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004508#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004509 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004510#endif
4511 }
4512 else
4513 {
4514 /* skip ofer the matched characters, set character
4515 * count in NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004516 addstate(nextlist, t->state->out, &t->subs, bytelen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004517 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4518#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004519 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004520#endif
4521 }
4522
4523 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004524 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004525 }
4526 case NFA_SKIP:
4527 /* charater of previous matching \1 .. \9 */
4528 if (t->count - clen <= 0)
4529 {
4530 /* end of match, go to what follows */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004531 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004532#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004533 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004534#endif
4535 }
4536 else
4537 {
4538 /* add state again with decremented count */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004539 addstate(nextlist, t->state, &t->subs, 0);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004540 nextlist->t[nextlist->n - 1].count = t->count - clen;
4541#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004542 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004543#endif
4544 }
4545 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004546
4547 case NFA_SKIP_CHAR:
4548 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004549 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004550 /* TODO: should not happen? */
4551 break;
4552
Bram Moolenaar423532e2013-05-29 21:14:42 +02004553 case NFA_LNUM:
4554 case NFA_LNUM_GT:
4555 case NFA_LNUM_LT:
4556 result = (REG_MULTI &&
4557 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4558 (long_u)(reglnum + reg_firstlnum)));
4559 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004560 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004561 break;
4562
4563 case NFA_COL:
4564 case NFA_COL_GT:
4565 case NFA_COL_LT:
4566 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4567 (long_u)(reginput - regline) + 1);
4568 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004569 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004570 break;
4571
4572 case NFA_VCOL:
4573 case NFA_VCOL_GT:
4574 case NFA_VCOL_LT:
4575 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4576 (long_u)win_linetabsize(
4577 reg_win == NULL ? curwin : reg_win,
4578 regline, (colnr_T)(reginput - regline)) + 1);
4579 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004580 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004581 break;
4582
4583 case NFA_CURSOR:
4584 result = (reg_win != NULL
4585 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4586 && ((colnr_T)(reginput - regline)
4587 == reg_win->w_cursor.col));
4588 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004589 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004590 break;
4591
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004592 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004593 {
4594 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004595
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004596 /* TODO: put this in #ifdef later */
4597 if (c < -256)
4598 EMSGN("INTERNAL: Negative state char: %ld", c);
4599 if (is_Magic(c))
4600 c = un_Magic(c);
4601 result = (c == curc);
4602
4603 if (!result && ireg_ic)
4604 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004605#ifdef FEAT_MBYTE
4606 /* If there is a composing character which is not being
4607 * ignored there can be no match. Match with composing
4608 * character uses NFA_COMPOSING above. */
4609 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004610 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004611 result = FALSE;
4612#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004613 ADD_POS_NEG_STATE(t->state);
4614 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004615 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004616 }
4617
4618 } /* for (thislist = thislist; thislist->state; thislist++) */
4619
Bram Moolenaare23febd2013-05-26 18:40:14 +02004620 /* Look for the start of a match in the current position by adding the
4621 * start state to the list of states.
4622 * The first found match is the leftmost one, thus the order of states
4623 * matters!
4624 * Do not add the start state in recursive calls of nfa_regmatch(),
4625 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004626 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004627 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004628 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004629 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004630 && reglnum == 0
4631 && clen != 0
4632 && (ireg_maxcol == 0
4633 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004634 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004635 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004636 ? (reglnum < nfa_endp->se_u.pos.lnum
4637 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004638 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004639 < nfa_endp->se_u.pos.col))
4640 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004641 {
4642#ifdef ENABLE_LOG
4643 fprintf(log_fd, "(---) STARTSTATE\n");
4644#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004645 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004646 }
4647
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004648#ifdef ENABLE_LOG
4649 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004650 {
4651 int i;
4652
4653 for (i = 0; i < thislist->n; i++)
4654 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4655 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004656 fprintf(log_fd, "\n");
4657#endif
4658
4659nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004660 /* Advance to the next character, or advance to the next line, or
4661 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004662 if (clen != 0)
4663 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004664 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4665 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004666 reg_nextline();
4667 else
4668 break;
4669 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004670
4671#ifdef ENABLE_LOG
4672 if (log_fd != stderr)
4673 fclose(log_fd);
4674 log_fd = NULL;
4675#endif
4676
4677theend:
4678 /* Free memory */
4679 vim_free(list[0].t);
4680 vim_free(list[1].t);
4681 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004682 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004684#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 fclose(debug);
4686#endif
4687
Bram Moolenaar963fee22013-05-26 21:47:28 +02004688 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004689}
4690
4691/*
4692 * Try match of "prog" with at regline["col"].
4693 * Returns 0 for failure, number of lines contained in the match otherwise.
4694 */
4695 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004696nfa_regtry(prog, col)
4697 nfa_regprog_T *prog;
4698 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699{
4700 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004701 regsubs_T subs, m;
4702 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004703#ifdef ENABLE_LOG
4704 FILE *f;
4705#endif
4706
4707 reginput = regline + col;
4708 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004709#ifdef FEAT_SYN_HL
4710 /* Clear the external match subpointers if necessary. */
4711 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004712 {
4713 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004714 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004715 }
4716 else
4717 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004718#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004719
4720#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004721 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004722 if (f != NULL)
4723 {
4724 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4725 fprintf(f, " =======================================================\n");
4726#ifdef DEBUG
4727 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4728#endif
4729 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004730 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004731 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004732 fprintf(f, "\n\n");
4733 fclose(f);
4734 }
4735 else
4736 EMSG(_("Could not open temporary log file for writing "));
4737#endif
4738
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004739 clear_sub(&subs.norm);
4740 clear_sub(&m.norm);
4741#ifdef FEAT_SYN_HL
4742 clear_sub(&subs.synt);
4743 clear_sub(&m.synt);
4744#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004745
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004746 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004747 return 0;
4748
4749 cleanup_subexpr();
4750 if (REG_MULTI)
4751 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004752 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004753 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004754 reg_startpos[i] = subs.norm.list.multi[i].start;
4755 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004756 }
4757
4758 if (reg_startpos[0].lnum < 0)
4759 {
4760 reg_startpos[0].lnum = 0;
4761 reg_startpos[0].col = col;
4762 }
4763 if (reg_endpos[0].lnum < 0)
4764 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004765 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004766 reg_endpos[0].lnum = reglnum;
4767 reg_endpos[0].col = (int)(reginput - regline);
4768 }
4769 else
4770 /* Use line number of "\ze". */
4771 reglnum = reg_endpos[0].lnum;
4772 }
4773 else
4774 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004775 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004776 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004777 reg_startp[i] = subs.norm.list.line[i].start;
4778 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004779 }
4780
4781 if (reg_startp[0] == NULL)
4782 reg_startp[0] = regline + col;
4783 if (reg_endp[0] == NULL)
4784 reg_endp[0] = reginput;
4785 }
4786
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004787#ifdef FEAT_SYN_HL
4788 /* Package any found \z(...\) matches for export. Default is none. */
4789 unref_extmatch(re_extmatch_out);
4790 re_extmatch_out = NULL;
4791
4792 if (prog->reghasz == REX_SET)
4793 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004794 cleanup_zsubexpr();
4795 re_extmatch_out = make_extmatch();
4796 for (i = 0; i < subs.synt.in_use; i++)
4797 {
4798 if (REG_MULTI)
4799 {
4800 struct multipos *mpos = &subs.synt.list.multi[i];
4801
4802 /* Only accept single line matches. */
4803 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
4804 re_extmatch_out->matches[i] =
4805 vim_strnsave(reg_getline(mpos->start.lnum)
4806 + mpos->start.col,
4807 mpos->end.col - mpos->start.col);
4808 }
4809 else
4810 {
4811 struct linepos *lpos = &subs.synt.list.line[i];
4812
4813 if (lpos->start != NULL && lpos->end != NULL)
4814 re_extmatch_out->matches[i] =
4815 vim_strnsave(lpos->start,
4816 (int)(lpos->end - lpos->start));
4817 }
4818 }
4819 }
4820#endif
4821
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004822 return 1 + reglnum;
4823}
4824
4825/*
4826 * Match a regexp against a string ("line" points to the string) or multiple
4827 * lines ("line" is NULL, use reg_getline()).
4828 *
4829 * Returns 0 for failure, number of lines contained in the match otherwise.
4830 */
4831 static long
4832nfa_regexec_both(line, col)
4833 char_u *line;
4834 colnr_T col; /* column to start looking for match */
4835{
4836 nfa_regprog_T *prog;
4837 long retval = 0L;
4838 int i;
4839
4840 if (REG_MULTI)
4841 {
4842 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4843 line = reg_getline((linenr_T)0); /* relative to the cursor */
4844 reg_startpos = reg_mmatch->startpos;
4845 reg_endpos = reg_mmatch->endpos;
4846 }
4847 else
4848 {
4849 prog = (nfa_regprog_T *)reg_match->regprog;
4850 reg_startp = reg_match->startp;
4851 reg_endp = reg_match->endp;
4852 }
4853
4854 /* Be paranoid... */
4855 if (prog == NULL || line == NULL)
4856 {
4857 EMSG(_(e_null));
4858 goto theend;
4859 }
4860
4861 /* If the start column is past the maximum column: no need to try. */
4862 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4863 goto theend;
4864
4865 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4866 if (prog->regflags & RF_ICASE)
4867 ireg_ic = TRUE;
4868 else if (prog->regflags & RF_NOICASE)
4869 ireg_ic = FALSE;
4870
4871#ifdef FEAT_MBYTE
4872 /* If pattern contains "\Z" overrule value of ireg_icombine */
4873 if (prog->regflags & RF_ICOMBINE)
4874 ireg_icombine = TRUE;
4875#endif
4876
4877 regline = line;
4878 reglnum = 0; /* relative to line */
4879
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004880 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004881 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004882 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004883 nfa_listid = 1;
4884 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004885#ifdef DEBUG
4886 nfa_regengine.expr = prog->pattern;
4887#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004888
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004889 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004890 for (i = 0; i < nstate; ++i)
4891 {
4892 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004893 prog->state[i].lastlist[0] = 0;
4894 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004895 }
4896
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004897 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004898
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004899#ifdef DEBUG
4900 nfa_regengine.expr = NULL;
4901#endif
4902
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004903theend:
4904 return retval;
4905}
4906
4907/*
4908 * Compile a regular expression into internal code for the NFA matcher.
4909 * Returns the program in allocated space. Returns NULL for an error.
4910 */
4911 static regprog_T *
4912nfa_regcomp(expr, re_flags)
4913 char_u *expr;
4914 int re_flags;
4915{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004916 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004917 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004918 int *postfix;
4919
4920 if (expr == NULL)
4921 return NULL;
4922
4923#ifdef DEBUG
4924 nfa_regengine.expr = expr;
4925#endif
4926
4927 init_class_tab();
4928
4929 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4930 return NULL;
4931
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004932 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004933 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004934 postfix = re2post();
4935 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004936 {
4937 /* TODO: only give this error for debugging? */
4938 if (post_ptr >= post_end)
4939 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004941 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004942
4943 /*
4944 * In order to build the NFA, we parse the input regexp twice:
4945 * 1. first pass to count size (so we can allocate space)
4946 * 2. second to emit code
4947 */
4948#ifdef ENABLE_LOG
4949 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004950 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004951
4952 if (f != NULL)
4953 {
4954 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4955 fclose(f);
4956 }
4957 }
4958#endif
4959
4960 /*
4961 * PASS 1
4962 * Count number of NFA states in "nstate". Do not build the NFA.
4963 */
4964 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004965
4966 /* Space for compiled regexp */
4967 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4968 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4969 if (prog == NULL)
4970 goto fail;
4971 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004972 state_ptr = prog->state;
4973
4974 /*
4975 * PASS 2
4976 * Build the NFA
4977 */
4978 prog->start = post2nfa(postfix, post_ptr, FALSE);
4979 if (prog->start == NULL)
4980 goto fail;
4981
4982 prog->regflags = regflags;
4983 prog->engine = &nfa_regengine;
4984 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004985 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004986 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004987 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004988#ifdef ENABLE_LOG
4989 nfa_postfix_dump(expr, OK);
4990 nfa_dump(prog);
4991#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004992#ifdef FEAT_SYN_HL
4993 /* Remember whether this pattern has any \z specials in it. */
4994 prog->reghasz = re_has_z;
4995#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004996#ifdef DEBUG
4997 prog->pattern = vim_strsave(expr); /* memory will leak */
4998 nfa_regengine.expr = NULL;
4999#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005000
5001out:
5002 vim_free(post_start);
5003 post_start = post_ptr = post_end = NULL;
5004 state_ptr = NULL;
5005 return (regprog_T *)prog;
5006
5007fail:
5008 vim_free(prog);
5009 prog = NULL;
5010#ifdef ENABLE_LOG
5011 nfa_postfix_dump(expr, FAIL);
5012#endif
5013#ifdef DEBUG
5014 nfa_regengine.expr = NULL;
5015#endif
5016 goto out;
5017}
5018
5019
5020/*
5021 * Match a regexp against a string.
5022 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5023 * Uses curbuf for line count and 'iskeyword'.
5024 *
5025 * Return TRUE if there is a match, FALSE if not.
5026 */
5027 static int
5028nfa_regexec(rmp, line, col)
5029 regmatch_T *rmp;
5030 char_u *line; /* string to match against */
5031 colnr_T col; /* column to start looking for match */
5032{
5033 reg_match = rmp;
5034 reg_mmatch = NULL;
5035 reg_maxline = 0;
5036 reg_line_lbr = FALSE;
5037 reg_buf = curbuf;
5038 reg_win = NULL;
5039 ireg_ic = rmp->rm_ic;
5040#ifdef FEAT_MBYTE
5041 ireg_icombine = FALSE;
5042#endif
5043 ireg_maxcol = 0;
5044 return (nfa_regexec_both(line, col) != 0);
5045}
5046
5047#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5048 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5049
5050static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5051
5052/*
5053 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5054 */
5055 static int
5056nfa_regexec_nl(rmp, line, col)
5057 regmatch_T *rmp;
5058 char_u *line; /* string to match against */
5059 colnr_T col; /* column to start looking for match */
5060{
5061 reg_match = rmp;
5062 reg_mmatch = NULL;
5063 reg_maxline = 0;
5064 reg_line_lbr = TRUE;
5065 reg_buf = curbuf;
5066 reg_win = NULL;
5067 ireg_ic = rmp->rm_ic;
5068#ifdef FEAT_MBYTE
5069 ireg_icombine = FALSE;
5070#endif
5071 ireg_maxcol = 0;
5072 return (nfa_regexec_both(line, col) != 0);
5073}
5074#endif
5075
5076
5077/*
5078 * Match a regexp against multiple lines.
5079 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5080 * Uses curbuf for line count and 'iskeyword'.
5081 *
5082 * Return zero if there is no match. Return number of lines contained in the
5083 * match otherwise.
5084 *
5085 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5086 *
5087 * ! Also NOTE : match may actually be in another line. e.g.:
5088 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5089 *
5090 * +-------------------------+
5091 * |a |
5092 * |b |
5093 * |c |
5094 * | |
5095 * +-------------------------+
5096 *
5097 * then nfa_regexec_multi() returns 3. while the original
5098 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5099 *
5100 * FIXME if this behavior is not compatible.
5101 */
5102 static long
5103nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5104 regmmatch_T *rmp;
5105 win_T *win; /* window in which to search or NULL */
5106 buf_T *buf; /* buffer in which to search */
5107 linenr_T lnum; /* nr of line to start looking for match */
5108 colnr_T col; /* column to start looking for match */
5109 proftime_T *tm UNUSED; /* timeout limit or NULL */
5110{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005111 reg_match = NULL;
5112 reg_mmatch = rmp;
5113 reg_buf = buf;
5114 reg_win = win;
5115 reg_firstlnum = lnum;
5116 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5117 reg_line_lbr = FALSE;
5118 ireg_ic = rmp->rmm_ic;
5119#ifdef FEAT_MBYTE
5120 ireg_icombine = FALSE;
5121#endif
5122 ireg_maxcol = rmp->rmm_maxcol;
5123
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005124 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005125}
5126
5127#ifdef DEBUG
5128# undef ENABLE_LOG
5129#endif