blob: 0a1dec697dfff9310b6f374a93195494b989c202 [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 */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200181 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200182
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200183 NFA_FIRST_NL = NFA_ANY + ADD_NL,
184 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
185
186 /* Character classes [:alnum:] etc */
187 NFA_CLASS_ALNUM,
188 NFA_CLASS_ALPHA,
189 NFA_CLASS_BLANK,
190 NFA_CLASS_CNTRL,
191 NFA_CLASS_DIGIT,
192 NFA_CLASS_GRAPH,
193 NFA_CLASS_LOWER,
194 NFA_CLASS_PRINT,
195 NFA_CLASS_PUNCT,
196 NFA_CLASS_SPACE,
197 NFA_CLASS_UPPER,
198 NFA_CLASS_XDIGIT,
199 NFA_CLASS_TAB,
200 NFA_CLASS_RETURN,
201 NFA_CLASS_BACKSPACE,
202 NFA_CLASS_ESCAPE
203};
204
205/* Keep in sync with classchars. */
206static int nfa_classcodes[] = {
207 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
208 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
209 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
210 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
211 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
212 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
213 NFA_UPPER, NFA_NUPPER
214};
215
216static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
217
218/*
219 * NFA errors can be of 3 types:
220 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
221 * silently and revert the to backtracking engine.
222 * syntax_error = FALSE;
223 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
224 * The NFA engine displays an error message, and nothing else happens.
225 * syntax_error = TRUE
226 * *** Unsupported features, when the input regexp uses an operator that is not
227 * implemented in the NFA. The NFA engine fails silently, and reverts to the
228 * old backtracking engine.
229 * syntax_error = FALSE
230 * "The NFA fails" means that "compiling the regexp with the NFA fails":
231 * nfa_regcomp() returns FAIL.
232 */
233static int syntax_error = FALSE;
234
235/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200236static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237
Bram Moolenaar428e9872013-05-30 17:05:39 +0200238/* NFA regexp \1 .. \9 encountered. */
239static int nfa_has_backref;
240
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200241#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200242/* NFA regexp has \z( ), set zsubexpr. */
243static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200244#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200245
Bram Moolenaar963fee22013-05-26 21:47:28 +0200246/* Number of sub expressions actually being used during execution. 1 if only
247 * the whole match (subexpr 0) is used. */
248static int nfa_nsubexpr;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250static int *post_start; /* holds the postfix form of r.e. */
251static int *post_end;
252static int *post_ptr;
253
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200254static int nstate; /* Number of states in the NFA. Also used when
255 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200256static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar307aa162013-06-02 16:34:21 +0200258/* If not NULL match must end at this position */
259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200261/* listid is global, so that it increases on recursive calls to
262 * nfa_regmatch(), which means we don't have to clear the lastlist field of
263 * all the states. */
264static int nfa_listid;
265static int nfa_alt_listid;
266
267/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
268static int nfa_ll_index = 0;
269
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200270static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
271static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
272static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273static int nfa_regatom __ARGS((void));
274static int nfa_regpiece __ARGS((void));
275static int nfa_regconcat __ARGS((void));
276static int nfa_regbranch __ARGS((void));
277static int nfa_reg __ARGS((int paren));
278#ifdef DEBUG
279static void nfa_set_code __ARGS((int c));
280static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200281static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
282static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200283static void nfa_dump __ARGS((nfa_regprog_T *prog));
284#endif
285static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200286static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
288static int check_char_class __ARGS((int class, int c));
289static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200290static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
291static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200292static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200293static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200294static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
295static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
296static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
297static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
298
299/* helper functions used when doing re2post() ... regatom() parsing */
300#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200301 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302 return FAIL; \
303 *post_ptr++ = c; \
304 } while (0)
305
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306/*
307 * Initialize internal variables before NFA compilation.
308 * Return OK on success, FAIL otherwise.
309 */
310 static int
311nfa_regcomp_start(expr, re_flags)
312 char_u *expr;
313 int re_flags; /* see vim_regcomp() */
314{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200315 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200316 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200317
318 nstate = 0;
319 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200320 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200321 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200323 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200324 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200325 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200326
327 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200328 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330 post_start = (int *)lalloc(postfix_size, TRUE);
331 if (post_start == NULL)
332 return FAIL;
333 vim_memset(post_start, 0, postfix_size);
334 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200335 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200336 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200337 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200339 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200340 regcomp_start(expr, re_flags);
341
342 return OK;
343}
344
345/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200346 * Allocate more space for post_start. Called when
347 * running above the estimated number of states.
348 */
349 static int
350realloc_post_list()
351{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200352 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200353 int new_max = nstate_max + 1000;
354 int *new_start;
355 int *old_start;
356
357 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
358 if (new_start == NULL)
359 return FAIL;
360 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
361 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
362 old_start = post_start;
363 post_start = new_start;
364 post_ptr = new_start + (post_ptr - old_start);
365 post_end = post_start + new_max;
366 vim_free(old_start);
367 return OK;
368}
369
370/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200371 * Search between "start" and "end" and try to recognize a
372 * character class in expanded form. For example [0-9].
373 * On success, return the id the character class to be emitted.
374 * On failure, return 0 (=FAIL)
375 * Start points to the first char of the range, while end should point
376 * to the closing brace.
377 */
378 static int
379nfa_recognize_char_class(start, end, extra_newl)
380 char_u *start;
381 char_u *end;
382 int extra_newl;
383{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200384# define CLASS_not 0x80
385# define CLASS_af 0x40
386# define CLASS_AF 0x20
387# define CLASS_az 0x10
388# define CLASS_AZ 0x08
389# define CLASS_o7 0x04
390# define CLASS_o9 0x02
391# define CLASS_underscore 0x01
392
393 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200394 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200395 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200396
397 if (extra_newl == TRUE)
398 newl = TRUE;
399
400 if (*end != ']')
401 return FAIL;
402 p = start;
403 if (*p == '^')
404 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200405 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200406 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200407 }
408
409 while (p < end)
410 {
411 if (p + 2 < end && *(p + 1) == '-')
412 {
413 switch (*p)
414 {
415 case '0':
416 if (*(p + 2) == '9')
417 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200418 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200419 break;
420 }
421 else
422 if (*(p + 2) == '7')
423 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200424 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200425 break;
426 }
427 case 'a':
428 if (*(p + 2) == 'z')
429 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200430 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200431 break;
432 }
433 else
434 if (*(p + 2) == 'f')
435 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200436 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200437 break;
438 }
439 case 'A':
440 if (*(p + 2) == 'Z')
441 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200442 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200443 break;
444 }
445 else
446 if (*(p + 2) == 'F')
447 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200448 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200449 break;
450 }
451 /* FALLTHROUGH */
452 default:
453 return FAIL;
454 }
455 p += 3;
456 }
457 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
458 {
459 newl = TRUE;
460 p += 2;
461 }
462 else if (*p == '_')
463 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200464 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200465 p ++;
466 }
467 else if (*p == '\n')
468 {
469 newl = TRUE;
470 p ++;
471 }
472 else
473 return FAIL;
474 } /* while (p < end) */
475
476 if (p != end)
477 return FAIL;
478
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200479 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200480 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200481
482 switch (config)
483 {
484 case CLASS_o9:
485 return extra_newl + NFA_DIGIT;
486 case CLASS_not | CLASS_o9:
487 return extra_newl + NFA_NDIGIT;
488 case CLASS_af | CLASS_AF | CLASS_o9:
489 return extra_newl + NFA_HEX;
490 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
491 return extra_newl + NFA_NHEX;
492 case CLASS_o7:
493 return extra_newl + NFA_OCTAL;
494 case CLASS_not | CLASS_o7:
495 return extra_newl + NFA_NOCTAL;
496 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
497 return extra_newl + NFA_WORD;
498 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
499 return extra_newl + NFA_NWORD;
500 case CLASS_az | CLASS_AZ | CLASS_underscore:
501 return extra_newl + NFA_HEAD;
502 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
503 return extra_newl + NFA_NHEAD;
504 case CLASS_az | CLASS_AZ:
505 return extra_newl + NFA_ALPHA;
506 case CLASS_not | CLASS_az | CLASS_AZ:
507 return extra_newl + NFA_NALPHA;
508 case CLASS_az:
509 return extra_newl + NFA_LOWER;
510 case CLASS_not | CLASS_az:
511 return extra_newl + NFA_NLOWER;
512 case CLASS_AZ:
513 return extra_newl + NFA_UPPER;
514 case CLASS_not | CLASS_AZ:
515 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200516 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200517 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200518}
519
520/*
521 * Produce the bytes for equivalence class "c".
522 * Currently only handles latin1, latin9 and utf-8.
523 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
524 * equivalent to 'a OR b OR c'
525 *
526 * NOTE! When changing this function, also update reg_equi_class()
527 */
528 static int
529nfa_emit_equi_class(c, neg)
530 int c;
531 int neg;
532{
533 int first = TRUE;
534 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
535#define EMIT2(c) \
536 EMIT(c); \
537 if (neg == TRUE) { \
538 EMIT(NFA_NOT); \
539 } \
540 if (first == FALSE) \
541 EMIT(glue); \
542 else \
543 first = FALSE; \
544
545#ifdef FEAT_MBYTE
546 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
547 || STRCMP(p_enc, "iso-8859-15") == 0)
548#endif
549 {
550 switch (c)
551 {
552 case 'A': case '\300': case '\301': case '\302':
553 case '\303': case '\304': case '\305':
554 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
555 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
556 EMIT2('\305');
557 return OK;
558
559 case 'C': case '\307':
560 EMIT2('C'); EMIT2('\307');
561 return OK;
562
563 case 'E': case '\310': case '\311': case '\312': case '\313':
564 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
565 EMIT2('\312'); EMIT2('\313');
566 return OK;
567
568 case 'I': case '\314': case '\315': case '\316': case '\317':
569 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
570 EMIT2('\316'); EMIT2('\317');
571 return OK;
572
573 case 'N': case '\321':
574 EMIT2('N'); EMIT2('\321');
575 return OK;
576
577 case 'O': case '\322': case '\323': case '\324': case '\325':
578 case '\326':
579 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
580 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
581 return OK;
582
583 case 'U': case '\331': case '\332': case '\333': case '\334':
584 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
585 EMIT2('\333'); EMIT2('\334');
586 return OK;
587
588 case 'Y': case '\335':
589 EMIT2('Y'); EMIT2('\335');
590 return OK;
591
592 case 'a': case '\340': case '\341': case '\342':
593 case '\343': case '\344': case '\345':
594 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
595 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
596 EMIT2('\345');
597 return OK;
598
599 case 'c': case '\347':
600 EMIT2('c'); EMIT2('\347');
601 return OK;
602
603 case 'e': case '\350': case '\351': case '\352': case '\353':
604 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
605 EMIT2('\352'); EMIT2('\353');
606 return OK;
607
608 case 'i': case '\354': case '\355': case '\356': case '\357':
609 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
610 EMIT2('\356'); EMIT2('\357');
611 return OK;
612
613 case 'n': case '\361':
614 EMIT2('n'); EMIT2('\361');
615 return OK;
616
617 case 'o': case '\362': case '\363': case '\364': case '\365':
618 case '\366':
619 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
620 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
621 return OK;
622
623 case 'u': case '\371': case '\372': case '\373': case '\374':
624 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
625 EMIT2('\373'); EMIT2('\374');
626 return OK;
627
628 case 'y': case '\375': case '\377':
629 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
630 return OK;
631
632 default:
633 return FAIL;
634 }
635 }
636
637 EMIT(c);
638 return OK;
639#undef EMIT2
640}
641
642/*
643 * Code to parse regular expression.
644 *
645 * We try to reuse parsing functions in regexp.c to
646 * minimize surprise and keep the syntax consistent.
647 */
648
649/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200650 * Parse the lowest level.
651 *
652 * An atom can be one of a long list of items. Many atoms match one character
653 * in the text. It is often an ordinary character or a character class.
654 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
655 * is only for syntax highlighting.
656 *
657 * atom ::= ordinary-atom
658 * or \( pattern \)
659 * or \%( pattern \)
660 * or \z( pattern \)
661 */
662 static int
663nfa_regatom()
664{
665 int c;
666 int charclass;
667 int equiclass;
668 int collclass;
669 int got_coll_char;
670 char_u *p;
671 char_u *endp;
672#ifdef FEAT_MBYTE
673 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200674#endif
675 int extra = 0;
676 int first;
677 int emit_range;
678 int negated;
679 int result;
680 int startc = -1;
681 int endc = -1;
682 int oldstartc = -1;
683 int cpo_lit; /* 'cpoptions' contains 'l' flag */
684 int cpo_bsl; /* 'cpoptions' contains '\' flag */
685 int glue; /* ID that will "glue" nodes together */
686
687 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
688 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
689
690 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200691 switch (c)
692 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200693 case NUL:
694 syntax_error = TRUE;
695 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
696
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200697 case Magic('^'):
698 EMIT(NFA_BOL);
699 break;
700
701 case Magic('$'):
702 EMIT(NFA_EOL);
703#if defined(FEAT_SYN_HL) || defined(PROTO)
704 had_eol = TRUE;
705#endif
706 break;
707
708 case Magic('<'):
709 EMIT(NFA_BOW);
710 break;
711
712 case Magic('>'):
713 EMIT(NFA_EOW);
714 break;
715
716 case Magic('_'):
717 c = no_Magic(getchr());
718 if (c == '^') /* "\_^" is start-of-line */
719 {
720 EMIT(NFA_BOL);
721 break;
722 }
723 if (c == '$') /* "\_$" is end-of-line */
724 {
725 EMIT(NFA_EOL);
726#if defined(FEAT_SYN_HL) || defined(PROTO)
727 had_eol = TRUE;
728#endif
729 break;
730 }
731
732 extra = ADD_NL;
733
734 /* "\_[" is collection plus newline */
735 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200736 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200737
738 /* "\_x" is character class plus newline */
739 /*FALLTHROUGH*/
740
741 /*
742 * Character classes.
743 */
744 case Magic('.'):
745 case Magic('i'):
746 case Magic('I'):
747 case Magic('k'):
748 case Magic('K'):
749 case Magic('f'):
750 case Magic('F'):
751 case Magic('p'):
752 case Magic('P'):
753 case Magic('s'):
754 case Magic('S'):
755 case Magic('d'):
756 case Magic('D'):
757 case Magic('x'):
758 case Magic('X'):
759 case Magic('o'):
760 case Magic('O'):
761 case Magic('w'):
762 case Magic('W'):
763 case Magic('h'):
764 case Magic('H'):
765 case Magic('a'):
766 case Magic('A'):
767 case Magic('l'):
768 case Magic('L'):
769 case Magic('u'):
770 case Magic('U'):
771 p = vim_strchr(classchars, no_Magic(c));
772 if (p == NULL)
773 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200774 EMSGN("INTERNAL: Unknown character class char: %ld", c);
775 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 }
777#ifdef FEAT_MBYTE
778 /* When '.' is followed by a composing char ignore the dot, so that
779 * the composing char is matched here. */
780 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
781 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200782 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200783 c = getchr();
784 goto nfa_do_multibyte;
785 }
786#endif
787 EMIT(nfa_classcodes[p - classchars]);
788 if (extra == ADD_NL)
789 {
790 EMIT(NFA_NEWL);
791 EMIT(NFA_OR);
792 regflags |= RF_HASNL;
793 }
794 break;
795
796 case Magic('n'):
797 if (reg_string)
798 /* In a string "\n" matches a newline character. */
799 EMIT(NL);
800 else
801 {
802 /* In buffer text "\n" matches the end of a line. */
803 EMIT(NFA_NEWL);
804 regflags |= RF_HASNL;
805 }
806 break;
807
808 case Magic('('):
809 if (nfa_reg(REG_PAREN) == FAIL)
810 return FAIL; /* cascaded error */
811 break;
812
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200813 case Magic('|'):
814 case Magic('&'):
815 case Magic(')'):
816 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200817 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200818 return FAIL;
819
820 case Magic('='):
821 case Magic('?'):
822 case Magic('+'):
823 case Magic('@'):
824 case Magic('*'):
825 case Magic('{'):
826 /* these should follow an atom, not form an atom */
827 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200828 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200829 return FAIL;
830
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200831 case Magic('~'):
832 {
833 char_u *lp;
834
835 /* Previous substitute pattern.
836 * Generated as "\%(pattern\)". */
837 if (reg_prev_sub == NULL)
838 {
839 EMSG(_(e_nopresub));
840 return FAIL;
841 }
842 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
843 {
844 EMIT(PTR2CHAR(lp));
845 if (lp != reg_prev_sub)
846 EMIT(NFA_CONCAT);
847 }
848 EMIT(NFA_NOPEN);
849 break;
850 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200851
Bram Moolenaar428e9872013-05-30 17:05:39 +0200852 case Magic('1'):
853 case Magic('2'):
854 case Magic('3'):
855 case Magic('4'):
856 case Magic('5'):
857 case Magic('6'):
858 case Magic('7'):
859 case Magic('8'):
860 case Magic('9'):
861 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
862 nfa_has_backref = TRUE;
863 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200864
865 case Magic('z'):
866 c = no_Magic(getchr());
867 switch (c)
868 {
869 case 's':
870 EMIT(NFA_ZSTART);
871 break;
872 case 'e':
873 EMIT(NFA_ZEND);
874 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200875 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200876#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200877 case '1':
878 case '2':
879 case '3':
880 case '4':
881 case '5':
882 case '6':
883 case '7':
884 case '8':
885 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200886 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200887 if (reg_do_extmatch != REX_USE)
888 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200889 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
890 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +0200891 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200892 re_has_z = REX_USE;
893 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200894 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200895 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200896 if (reg_do_extmatch != REX_SET)
897 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200898 if (nfa_reg(REG_ZPAREN) == FAIL)
899 return FAIL; /* cascaded error */
900 re_has_z = REX_SET;
901 break;
902#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200903 default:
904 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200905 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906 no_Magic(c));
907 return FAIL;
908 }
909 break;
910
911 case Magic('%'):
912 c = no_Magic(getchr());
913 switch (c)
914 {
915 /* () without a back reference */
916 case '(':
917 if (nfa_reg(REG_NPAREN) == FAIL)
918 return FAIL;
919 EMIT(NFA_NOPEN);
920 break;
921
922 case 'd': /* %d123 decimal */
923 case 'o': /* %o123 octal */
924 case 'x': /* %xab hex 2 */
925 case 'u': /* %uabcd hex 4 */
926 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200927 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200928 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200929
Bram Moolenaar47196582013-05-25 22:04:23 +0200930 switch (c)
931 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200932 case 'd': nr = getdecchrs(); break;
933 case 'o': nr = getoctchrs(); break;
934 case 'x': nr = gethexchrs(2); break;
935 case 'u': nr = gethexchrs(4); break;
936 case 'U': nr = gethexchrs(8); break;
937 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200938 }
939
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200940 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200941 EMSG2_RET_FAIL(
942 _("E678: Invalid character after %s%%[dxouU]"),
943 reg_magic == MAGIC_ALL);
944 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200945 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200946 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200947 break;
948
949 /* Catch \%^ and \%$ regardless of where they appear in the
950 * pattern -- regardless of whether or not it makes sense. */
951 case '^':
952 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200953 break;
954
955 case '$':
956 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200957 break;
958
959 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200960 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200961 break;
962
963 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200964 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 break;
966
967 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200968 /* TODO: \%[abc] not supported yet */
969 return FAIL;
970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200972 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200973 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200974 int cmp = c;
975
976 if (c == '<' || c == '>')
977 c = getchr();
978 while (VIM_ISDIGIT(c))
979 {
980 n = n * 10 + (c - '0');
981 c = getchr();
982 }
983 if (c == 'l' || c == 'c' || c == 'v')
984 {
985 EMIT(n);
986 if (c == 'l')
987 EMIT(cmp == '<' ? NFA_LNUM_LT :
988 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
989 else if (c == 'c')
990 EMIT(cmp == '<' ? NFA_COL_LT :
991 cmp == '>' ? NFA_COL_GT : NFA_COL);
992 else
993 EMIT(cmp == '<' ? NFA_VCOL_LT :
994 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
995 break;
996 }
997 else if (c == '\'')
998 /* TODO: \%'m not supported yet */
999 return FAIL;
1000 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001001 syntax_error = TRUE;
1002 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1003 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001004 return FAIL;
1005 }
1006 break;
1007
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001008 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001009collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001010 /*
1011 * Glue is emitted between several atoms from the [].
1012 * It is either NFA_OR, or NFA_CONCAT.
1013 *
1014 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1015 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1016 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1017 * notation)
1018 *
1019 */
1020
1021
1022/* Emit negation atoms, if needed.
1023 * The CONCAT below merges the NOT with the previous node. */
1024#define TRY_NEG() \
1025 if (negated == TRUE) \
1026 { \
1027 EMIT(NFA_NOT); \
1028 }
1029
1030/* Emit glue between important nodes : CONCAT or OR. */
1031#define EMIT_GLUE() \
1032 if (first == FALSE) \
1033 EMIT(glue); \
1034 else \
1035 first = FALSE;
1036
1037 p = regparse;
1038 endp = skip_anyof(p);
1039 if (*endp == ']')
1040 {
1041 /*
1042 * Try to reverse engineer character classes. For example,
1043 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1044 * and perform the necessary substitutions in the NFA.
1045 */
1046 result = nfa_recognize_char_class(regparse, endp,
1047 extra == ADD_NL);
1048 if (result != FAIL)
1049 {
1050 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1051 EMIT(result);
1052 else /* must be char class + newline */
1053 {
1054 EMIT(result - ADD_NL);
1055 EMIT(NFA_NEWL);
1056 EMIT(NFA_OR);
1057 }
1058 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001059 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001060 return OK;
1061 }
1062 /*
1063 * Failed to recognize a character class. Use the simple
1064 * version that turns [abc] into 'a' OR 'b' OR 'c'
1065 */
1066 startc = endc = oldstartc = -1;
1067 first = TRUE; /* Emitting first atom in this sequence? */
1068 negated = FALSE;
1069 glue = NFA_OR;
1070 if (*regparse == '^') /* negated range */
1071 {
1072 negated = TRUE;
1073 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001074 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001075 }
1076 if (*regparse == '-')
1077 {
1078 startc = '-';
1079 EMIT(startc);
1080 TRY_NEG();
1081 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001082 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001083 }
1084 /* Emit the OR branches for each character in the [] */
1085 emit_range = FALSE;
1086 while (regparse < endp)
1087 {
1088 oldstartc = startc;
1089 startc = -1;
1090 got_coll_char = FALSE;
1091 if (*regparse == '[')
1092 {
1093 /* Check for [: :], [= =], [. .] */
1094 equiclass = collclass = 0;
1095 charclass = get_char_class(&regparse);
1096 if (charclass == CLASS_NONE)
1097 {
1098 equiclass = get_equi_class(&regparse);
1099 if (equiclass == 0)
1100 collclass = get_coll_element(&regparse);
1101 }
1102
1103 /* Character class like [:alpha:] */
1104 if (charclass != CLASS_NONE)
1105 {
1106 switch (charclass)
1107 {
1108 case CLASS_ALNUM:
1109 EMIT(NFA_CLASS_ALNUM);
1110 break;
1111 case CLASS_ALPHA:
1112 EMIT(NFA_CLASS_ALPHA);
1113 break;
1114 case CLASS_BLANK:
1115 EMIT(NFA_CLASS_BLANK);
1116 break;
1117 case CLASS_CNTRL:
1118 EMIT(NFA_CLASS_CNTRL);
1119 break;
1120 case CLASS_DIGIT:
1121 EMIT(NFA_CLASS_DIGIT);
1122 break;
1123 case CLASS_GRAPH:
1124 EMIT(NFA_CLASS_GRAPH);
1125 break;
1126 case CLASS_LOWER:
1127 EMIT(NFA_CLASS_LOWER);
1128 break;
1129 case CLASS_PRINT:
1130 EMIT(NFA_CLASS_PRINT);
1131 break;
1132 case CLASS_PUNCT:
1133 EMIT(NFA_CLASS_PUNCT);
1134 break;
1135 case CLASS_SPACE:
1136 EMIT(NFA_CLASS_SPACE);
1137 break;
1138 case CLASS_UPPER:
1139 EMIT(NFA_CLASS_UPPER);
1140 break;
1141 case CLASS_XDIGIT:
1142 EMIT(NFA_CLASS_XDIGIT);
1143 break;
1144 case CLASS_TAB:
1145 EMIT(NFA_CLASS_TAB);
1146 break;
1147 case CLASS_RETURN:
1148 EMIT(NFA_CLASS_RETURN);
1149 break;
1150 case CLASS_BACKSPACE:
1151 EMIT(NFA_CLASS_BACKSPACE);
1152 break;
1153 case CLASS_ESCAPE:
1154 EMIT(NFA_CLASS_ESCAPE);
1155 break;
1156 }
1157 TRY_NEG();
1158 EMIT_GLUE();
1159 continue;
1160 }
1161 /* Try equivalence class [=a=] and the like */
1162 if (equiclass != 0)
1163 {
1164 result = nfa_emit_equi_class(equiclass, negated);
1165 if (result == FAIL)
1166 {
1167 /* should never happen */
1168 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1169 }
1170 EMIT_GLUE();
1171 continue;
1172 }
1173 /* Try collating class like [. .] */
1174 if (collclass != 0)
1175 {
1176 startc = collclass; /* allow [.a.]-x as a range */
1177 /* Will emit the proper atom at the end of the
1178 * while loop. */
1179 }
1180 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001181 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1182 * start character. */
1183 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001184 {
1185 emit_range = TRUE;
1186 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001187 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001188 continue; /* reading the end of the range */
1189 }
1190
1191 /* Now handle simple and escaped characters.
1192 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1193 * accepts "\t", "\e", etc., but only when the 'l' flag in
1194 * 'cpoptions' is not included.
1195 * Posix doesn't recognize backslash at all.
1196 */
1197 if (*regparse == '\\'
1198 && !cpo_bsl
1199 && regparse + 1 <= endp
1200 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1201 || (!cpo_lit
1202 && vim_strchr(REGEXP_ABBR, regparse[1])
1203 != NULL)
1204 )
1205 )
1206 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001207 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001209 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210 startc = reg_string ? NL : NFA_NEWL;
1211 else
1212 if (*regparse == 'd'
1213 || *regparse == 'o'
1214 || *regparse == 'x'
1215 || *regparse == 'u'
1216 || *regparse == 'U'
1217 )
1218 {
1219 /* TODO(RE) This needs more testing */
1220 startc = coll_get_char();
1221 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001222 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 }
1224 else
1225 {
1226 /* \r,\t,\e,\b */
1227 startc = backslash_trans(*regparse);
1228 }
1229 }
1230
1231 /* Normal printable char */
1232 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001233 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234
1235 /* Previous char was '-', so this char is end of range. */
1236 if (emit_range)
1237 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001238 endc = startc;
1239 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001240 if (startc > endc)
1241 EMSG_RET_FAIL(_(e_invrange));
1242#ifdef FEAT_MBYTE
1243 if (has_mbyte && ((*mb_char2len)(startc) > 1
1244 || (*mb_char2len)(endc) > 1))
1245 {
1246 if (endc > startc + 256)
1247 EMSG_RET_FAIL(_(e_invrange));
1248 /* Emit the range. "startc" was already emitted, so
1249 * skip it. */
1250 for (c = startc + 1; c <= endc; c++)
1251 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001252 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253 TRY_NEG();
1254 EMIT_GLUE();
1255 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256 }
1257 else
1258#endif
1259 {
1260#ifdef EBCDIC
1261 int alpha_only = FALSE;
1262
1263 /* for alphabetical range skip the gaps
1264 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1265 if (isalpha(startc) && isalpha(endc))
1266 alpha_only = TRUE;
1267#endif
1268 /* Emit the range. "startc" was already emitted, so
1269 * skip it. */
1270 for (c = startc + 1; c <= endc; c++)
1271#ifdef EBCDIC
1272 if (!alpha_only || isalpha(startc))
1273#endif
1274 {
1275 EMIT(c);
1276 TRY_NEG();
1277 EMIT_GLUE();
1278 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001279 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001280 emit_range = FALSE;
1281 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 }
1283 else
1284 {
1285 /*
1286 * This char (startc) is not part of a range. Just
1287 * emit it.
1288 *
1289 * Normally, simply emit startc. But if we get char
1290 * code=0 from a collating char, then replace it with
1291 * 0x0a.
1292 *
1293 * This is needed to completely mimic the behaviour of
1294 * the backtracking engine.
1295 */
1296 if (got_coll_char == TRUE && startc == 0)
1297 EMIT(0x0a);
1298 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001299 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001300 TRY_NEG();
1301 EMIT_GLUE();
1302 }
1303
Bram Moolenaar51a29832013-05-28 22:30:35 +02001304 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001305 } /* while (p < endp) */
1306
Bram Moolenaar51a29832013-05-28 22:30:35 +02001307 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001308 if (*regparse == '-') /* if last, '-' is just a char */
1309 {
1310 EMIT('-');
1311 TRY_NEG();
1312 EMIT_GLUE();
1313 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001314 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001316 /* skip the trailing ] */
1317 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001318 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 if (negated == TRUE)
1320 {
1321 /* Mark end of negated char range */
1322 EMIT(NFA_END_NEG_RANGE);
1323 EMIT(NFA_CONCAT);
1324 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001325
1326 /* \_[] also matches \n but it's not negated */
1327 if (extra == ADD_NL)
1328 {
1329 EMIT(reg_string ? NL : NFA_NEWL);
1330 EMIT(NFA_OR);
1331 }
1332
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001333 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001334 } /* if exists closing ] */
1335
1336 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001337 {
1338 syntax_error = TRUE;
1339 EMSG_RET_FAIL(_(e_missingbracket));
1340 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001341 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 default:
1344 {
1345#ifdef FEAT_MBYTE
1346 int plen;
1347
1348nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001349 /* plen is length of current char with composing chars */
1350 if (enc_utf8 && ((*mb_char2len)(c)
1351 != (plen = (*mb_ptr2len)(old_regparse))
1352 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001353 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001354 int i = 0;
1355
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001356 /* A base character plus composing characters, or just one
1357 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001358 * This requires creating a separate atom as if enclosing
1359 * the characters in (), where NFA_COMPOSING is the ( and
1360 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001361 * building the postfix form, not the NFA itself;
1362 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001363 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001364 for (;;)
1365 {
1366 EMIT(c);
1367 if (i > 0)
1368 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001369 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001370 break;
1371 c = utf_ptr2char(old_regparse + i);
1372 }
1373 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001374 regparse = old_regparse + plen;
1375 }
1376 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377#endif
1378 {
1379 c = no_Magic(c);
1380 EMIT(c);
1381 }
1382 return OK;
1383 }
1384 }
1385
1386#undef TRY_NEG
1387#undef EMIT_GLUE
1388
1389 return OK;
1390}
1391
1392/*
1393 * Parse something followed by possible [*+=].
1394 *
1395 * A piece is an atom, possibly followed by a multi, an indication of how many
1396 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1397 * characters: "", "a", "aa", etc.
1398 *
1399 * piece ::= atom
1400 * or atom multi
1401 */
1402 static int
1403nfa_regpiece()
1404{
1405 int i;
1406 int op;
1407 int ret;
1408 long minval, maxval;
1409 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001410 parse_state_T old_state;
1411 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001412 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001413 int old_post_pos;
1414 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001415 int quest;
1416
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001417 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1418 * next. */
1419 save_parse_state(&old_state);
1420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001421 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001422 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423
1424 ret = nfa_regatom();
1425 if (ret == FAIL)
1426 return FAIL; /* cascaded error */
1427
1428 op = peekchr();
1429 if (re_multi_type(op) == NOT_MULTI)
1430 return OK;
1431
1432 skipchr();
1433 switch (op)
1434 {
1435 case Magic('*'):
1436 EMIT(NFA_STAR);
1437 break;
1438
1439 case Magic('+'):
1440 /*
1441 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1442 * first and only submatch would be "aaa". But the backtracking
1443 * engine interprets the plus as "try matching one more time", and
1444 * a* matches a second time at the end of the input, the empty
1445 * string.
1446 * The submatch will the empty string.
1447 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001448 * In order to be consistent with the old engine, we replace
1449 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001451 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 curchr = -1;
1453 if (nfa_regatom() == FAIL)
1454 return FAIL;
1455 EMIT(NFA_STAR);
1456 EMIT(NFA_CONCAT);
1457 skipchr(); /* skip the \+ */
1458 break;
1459
1460 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001461 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001463 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001464 switch(op)
1465 {
1466 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001467 /* \@= */
1468 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001469 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001470 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001471 /* \@! */
1472 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001473 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001475 op = no_Magic(getchr());
1476 if (op == '=')
1477 /* \@<= */
1478 i = NFA_PREV_ATOM_JUST_BEFORE;
1479 else if (op == '!')
1480 /* \@<! */
1481 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1482 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001484 /* \@> Not supported yet */
1485 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 return FAIL;
1487 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001488 if (i == 0)
1489 {
1490 syntax_error = TRUE;
1491 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1492 return FAIL;
1493 }
1494 EMIT(i);
1495 if (i == NFA_PREV_ATOM_JUST_BEFORE
1496 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1497 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 break;
1499
1500 case Magic('?'):
1501 case Magic('='):
1502 EMIT(NFA_QUEST);
1503 break;
1504
1505 case Magic('{'):
1506 /* a{2,5} will expand to 'aaa?a?a?'
1507 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1508 * version of '?'
1509 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1510 * parenthesis have the same id
1511 */
1512
1513 greedy = TRUE;
1514 c2 = peekchr();
1515 if (c2 == '-' || c2 == Magic('-'))
1516 {
1517 skipchr();
1518 greedy = FALSE;
1519 }
1520 if (!read_limits(&minval, &maxval))
1521 {
1522 syntax_error = TRUE;
1523 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1524 }
1525 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1526 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001527 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001528 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001529 if (greedy)
1530 /* \{}, \{0,} */
1531 EMIT(NFA_STAR);
1532 else
1533 /* \{-}, \{-0,} */
1534 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001535 break;
1536 }
1537
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001538 /* Special case: x{0} or x{-0} */
1539 if (maxval == 0)
1540 {
1541 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001542 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001543 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1544 EMIT(NFA_SKIP_CHAR);
1545 return OK;
1546 }
1547
1548 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001549 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001550 /* Save parse state after the repeated atom and the \{} */
1551 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001552
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1554 for (i = 0; i < maxval; i++)
1555 {
1556 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001557 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001558 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 if (nfa_regatom() == FAIL)
1560 return FAIL;
1561 /* after "minval" times, atoms are optional */
1562 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001563 {
1564 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001565 {
1566 if (greedy)
1567 EMIT(NFA_STAR);
1568 else
1569 EMIT(NFA_STAR_NONGREEDY);
1570 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001571 else
1572 EMIT(quest);
1573 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001574 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001575 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001576 if (i + 1 > minval && maxval == MAX_LIMIT)
1577 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001578 }
1579
1580 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001581 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001582 curchr = -1;
1583
1584 break;
1585
1586
1587 default:
1588 break;
1589 } /* end switch */
1590
1591 if (re_multi_type(peekchr()) != NOT_MULTI)
1592 {
1593 /* Can't have a multi follow a multi. */
1594 syntax_error = TRUE;
1595 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1596 }
1597
1598 return OK;
1599}
1600
1601/*
1602 * Parse one or more pieces, concatenated. It matches a match for the
1603 * first piece, followed by a match for the second piece, etc. Example:
1604 * "f[0-9]b", first matches "f", then a digit and then "b".
1605 *
1606 * concat ::= piece
1607 * or piece piece
1608 * or piece piece piece
1609 * etc.
1610 */
1611 static int
1612nfa_regconcat()
1613{
1614 int cont = TRUE;
1615 int first = TRUE;
1616
1617 while (cont)
1618 {
1619 switch (peekchr())
1620 {
1621 case NUL:
1622 case Magic('|'):
1623 case Magic('&'):
1624 case Magic(')'):
1625 cont = FALSE;
1626 break;
1627
1628 case Magic('Z'):
1629#ifdef FEAT_MBYTE
1630 regflags |= RF_ICOMBINE;
1631#endif
1632 skipchr_keepstart();
1633 break;
1634 case Magic('c'):
1635 regflags |= RF_ICASE;
1636 skipchr_keepstart();
1637 break;
1638 case Magic('C'):
1639 regflags |= RF_NOICASE;
1640 skipchr_keepstart();
1641 break;
1642 case Magic('v'):
1643 reg_magic = MAGIC_ALL;
1644 skipchr_keepstart();
1645 curchr = -1;
1646 break;
1647 case Magic('m'):
1648 reg_magic = MAGIC_ON;
1649 skipchr_keepstart();
1650 curchr = -1;
1651 break;
1652 case Magic('M'):
1653 reg_magic = MAGIC_OFF;
1654 skipchr_keepstart();
1655 curchr = -1;
1656 break;
1657 case Magic('V'):
1658 reg_magic = MAGIC_NONE;
1659 skipchr_keepstart();
1660 curchr = -1;
1661 break;
1662
1663 default:
1664 if (nfa_regpiece() == FAIL)
1665 return FAIL;
1666 if (first == FALSE)
1667 EMIT(NFA_CONCAT);
1668 else
1669 first = FALSE;
1670 break;
1671 }
1672 }
1673
1674 return OK;
1675}
1676
1677/*
1678 * Parse a branch, one or more concats, separated by "\&". It matches the
1679 * last concat, but only if all the preceding concats also match at the same
1680 * position. Examples:
1681 * "foobeep\&..." matches "foo" in "foobeep".
1682 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1683 *
1684 * branch ::= concat
1685 * or concat \& concat
1686 * or concat \& concat \& concat
1687 * etc.
1688 */
1689 static int
1690nfa_regbranch()
1691{
1692 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001693 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694
Bram Moolenaar16299b52013-05-30 18:45:23 +02001695 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001696
1697 /* First branch, possibly the only one */
1698 if (nfa_regconcat() == FAIL)
1699 return FAIL;
1700
1701 ch = peekchr();
1702 /* Try next concats */
1703 while (ch == Magic('&'))
1704 {
1705 skipchr();
1706 EMIT(NFA_NOPEN);
1707 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001708 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001709 if (nfa_regconcat() == FAIL)
1710 return FAIL;
1711 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001712 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713 EMIT(NFA_SKIP_CHAR);
1714 EMIT(NFA_CONCAT);
1715 ch = peekchr();
1716 }
1717
1718 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001719 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720 EMIT(NFA_SKIP_CHAR);
1721
1722 return OK;
1723}
1724
1725/*
1726 * Parse a pattern, one or more branches, separated by "\|". It matches
1727 * anything that matches one of the branches. Example: "foo\|beep" matches
1728 * "foo" and matches "beep". If more than one branch matches, the first one
1729 * is used.
1730 *
1731 * pattern ::= branch
1732 * or branch \| branch
1733 * or branch \| branch \| branch
1734 * etc.
1735 */
1736 static int
1737nfa_reg(paren)
1738 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1739{
1740 int parno = 0;
1741
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001742 if (paren == REG_PAREN)
1743 {
1744 if (regnpar >= NSUBEXP) /* Too many `(' */
1745 {
1746 syntax_error = TRUE;
1747 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1748 }
1749 parno = regnpar++;
1750 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001751#ifdef FEAT_SYN_HL
1752 else if (paren == REG_ZPAREN)
1753 {
1754 /* Make a ZOPEN node. */
1755 if (regnzpar >= NSUBEXP)
1756 {
1757 syntax_error = TRUE;
1758 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1759 }
1760 parno = regnzpar++;
1761 }
1762#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001763
1764 if (nfa_regbranch() == FAIL)
1765 return FAIL; /* cascaded error */
1766
1767 while (peekchr() == Magic('|'))
1768 {
1769 skipchr();
1770 if (nfa_regbranch() == FAIL)
1771 return FAIL; /* cascaded error */
1772 EMIT(NFA_OR);
1773 }
1774
1775 /* Check for proper termination. */
1776 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1777 {
1778 syntax_error = TRUE;
1779 if (paren == REG_NPAREN)
1780 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1781 else
1782 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1783 }
1784 else if (paren == REG_NOPAREN && peekchr() != NUL)
1785 {
1786 syntax_error = TRUE;
1787 if (peekchr() == Magic(')'))
1788 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1789 else
1790 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1791 }
1792 /*
1793 * Here we set the flag allowing back references to this set of
1794 * parentheses.
1795 */
1796 if (paren == REG_PAREN)
1797 {
1798 had_endbrace[parno] = TRUE; /* have seen the close paren */
1799 EMIT(NFA_MOPEN + parno);
1800 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001801#ifdef FEAT_SYN_HL
1802 else if (paren == REG_ZPAREN)
1803 EMIT(NFA_ZOPEN + parno);
1804#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001805
1806 return OK;
1807}
1808
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809#ifdef DEBUG
1810static char_u code[50];
1811
1812 static void
1813nfa_set_code(c)
1814 int c;
1815{
1816 int addnl = FALSE;
1817
1818 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1819 {
1820 addnl = TRUE;
1821 c -= ADD_NL;
1822 }
1823
1824 STRCPY(code, "");
1825 switch (c)
1826 {
1827 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1828 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1829 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1830 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1831 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1832 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1833
Bram Moolenaar5714b802013-05-28 22:03:20 +02001834 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1835 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1836 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1837 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1838 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1839 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1840 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1841 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1842 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001843#ifdef FEAT_SYN_HL
1844 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1845 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1846 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1847 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1848 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1849 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1850 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1851 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1852 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1853#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001854 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1855
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856 case NFA_PREV_ATOM_NO_WIDTH:
1857 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001858 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1859 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001860 case NFA_PREV_ATOM_JUST_BEFORE:
1861 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1862 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1863 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001864 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1865 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001867 case NFA_START_INVISIBLE_BEFORE:
1868 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1870
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1872 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1873
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001874 case NFA_MOPEN:
1875 case NFA_MOPEN1:
1876 case NFA_MOPEN2:
1877 case NFA_MOPEN3:
1878 case NFA_MOPEN4:
1879 case NFA_MOPEN5:
1880 case NFA_MOPEN6:
1881 case NFA_MOPEN7:
1882 case NFA_MOPEN8:
1883 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884 STRCPY(code, "NFA_MOPEN(x)");
1885 code[10] = c - NFA_MOPEN + '0';
1886 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001887 case NFA_MCLOSE:
1888 case NFA_MCLOSE1:
1889 case NFA_MCLOSE2:
1890 case NFA_MCLOSE3:
1891 case NFA_MCLOSE4:
1892 case NFA_MCLOSE5:
1893 case NFA_MCLOSE6:
1894 case NFA_MCLOSE7:
1895 case NFA_MCLOSE8:
1896 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 STRCPY(code, "NFA_MCLOSE(x)");
1898 code[11] = c - NFA_MCLOSE + '0';
1899 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001900#ifdef FEAT_SYN_HL
1901 case NFA_ZOPEN:
1902 case NFA_ZOPEN1:
1903 case NFA_ZOPEN2:
1904 case NFA_ZOPEN3:
1905 case NFA_ZOPEN4:
1906 case NFA_ZOPEN5:
1907 case NFA_ZOPEN6:
1908 case NFA_ZOPEN7:
1909 case NFA_ZOPEN8:
1910 case NFA_ZOPEN9:
1911 STRCPY(code, "NFA_ZOPEN(x)");
1912 code[10] = c - NFA_ZOPEN + '0';
1913 break;
1914 case NFA_ZCLOSE:
1915 case NFA_ZCLOSE1:
1916 case NFA_ZCLOSE2:
1917 case NFA_ZCLOSE3:
1918 case NFA_ZCLOSE4:
1919 case NFA_ZCLOSE5:
1920 case NFA_ZCLOSE6:
1921 case NFA_ZCLOSE7:
1922 case NFA_ZCLOSE8:
1923 case NFA_ZCLOSE9:
1924 STRCPY(code, "NFA_ZCLOSE(x)");
1925 code[11] = c - NFA_ZCLOSE + '0';
1926 break;
1927#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1929 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1930 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1931 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001932 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1933 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001935 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1936 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1937 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1939 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1940 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1942 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1943 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1944 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1945 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1946 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1947 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1948 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1949 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1950 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1951 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1952 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1953 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1954 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1955 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1956 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1957 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1958
1959 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1960 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1961 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1962 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1963 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1964 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1965 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1966 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1967 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1968 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1969 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1970 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1971 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1972 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1973 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1974 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1975 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1976 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1977 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1978 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1979 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1980 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1981 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1982 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1983 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1984 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1985 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1986
1987 default:
1988 STRCPY(code, "CHAR(x)");
1989 code[5] = c;
1990 }
1991
1992 if (addnl == TRUE)
1993 STRCAT(code, " + NEWLINE ");
1994
1995}
1996
1997#ifdef ENABLE_LOG
1998static FILE *log_fd;
1999
2000/*
2001 * Print the postfix notation of the current regexp.
2002 */
2003 static void
2004nfa_postfix_dump(expr, retval)
2005 char_u *expr;
2006 int retval;
2007{
2008 int *p;
2009 FILE *f;
2010
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002011 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002012 if (f != NULL)
2013 {
2014 fprintf(f, "\n-------------------------\n");
2015 if (retval == FAIL)
2016 fprintf(f, ">>> NFA engine failed ... \n");
2017 else if (retval == OK)
2018 fprintf(f, ">>> NFA engine succeeded !\n");
2019 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002020 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002021 {
2022 nfa_set_code(*p);
2023 fprintf(f, "%s, ", code);
2024 }
2025 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002026 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002027 fprintf(f, "%d ", *p);
2028 fprintf(f, "\n\n");
2029 fclose(f);
2030 }
2031}
2032
2033/*
2034 * Print the NFA starting with a root node "state".
2035 */
2036 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002037nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 FILE *debugf;
2039 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002041 garray_T indent;
2042
2043 ga_init2(&indent, 1, 64);
2044 ga_append(&indent, '\0');
2045 nfa_print_state2(debugf, state, &indent);
2046 ga_clear(&indent);
2047}
2048
2049 static void
2050nfa_print_state2(debugf, state, indent)
2051 FILE *debugf;
2052 nfa_state_T *state;
2053 garray_T *indent;
2054{
2055 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056
2057 if (state == NULL)
2058 return;
2059
2060 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002061
2062 /* Output indent */
2063 p = (char_u *)indent->ga_data;
2064 if (indent->ga_len >= 3)
2065 {
2066 int last = indent->ga_len - 3;
2067 char_u save[2];
2068
2069 STRNCPY(save, &p[last], 2);
2070 STRNCPY(&p[last], "+-", 2);
2071 fprintf(debugf, " %s", p);
2072 STRNCPY(&p[last], save, 2);
2073 }
2074 else
2075 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002076
2077 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002078 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2079 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002080 if (state->id < 0)
2081 return;
2082
2083 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002084
2085 /* grow indent for state->out */
2086 indent->ga_len -= 1;
2087 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002088 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002089 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002090 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002091 ga_append(indent, '\0');
2092
2093 nfa_print_state2(debugf, state->out, indent);
2094
2095 /* replace last part of indent for state->out1 */
2096 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002097 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002098 ga_append(indent, '\0');
2099
2100 nfa_print_state2(debugf, state->out1, indent);
2101
2102 /* shrink indent */
2103 indent->ga_len -= 3;
2104 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002105}
2106
2107/*
2108 * Print the NFA state machine.
2109 */
2110 static void
2111nfa_dump(prog)
2112 nfa_regprog_T *prog;
2113{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002114 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002115
2116 if (debugf != NULL)
2117 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002118 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002119 fclose(debugf);
2120 }
2121}
2122#endif /* ENABLE_LOG */
2123#endif /* DEBUG */
2124
2125/*
2126 * Parse r.e. @expr and convert it into postfix form.
2127 * Return the postfix string on success, NULL otherwise.
2128 */
2129 static int *
2130re2post()
2131{
2132 if (nfa_reg(REG_NOPAREN) == FAIL)
2133 return NULL;
2134 EMIT(NFA_MOPEN);
2135 return post_start;
2136}
2137
2138/* NB. Some of the code below is inspired by Russ's. */
2139
2140/*
2141 * Represents an NFA state plus zero or one or two arrows exiting.
2142 * if c == MATCH, no arrows out; matching state.
2143 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2144 * If c < 256, labeled arrow with character c to out.
2145 */
2146
2147static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2148
2149/*
2150 * Allocate and initialize nfa_state_T.
2151 */
2152 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002153alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002154 int c;
2155 nfa_state_T *out;
2156 nfa_state_T *out1;
2157{
2158 nfa_state_T *s;
2159
2160 if (istate >= nstate)
2161 return NULL;
2162
2163 s = &state_ptr[istate++];
2164
2165 s->c = c;
2166 s->out = out;
2167 s->out1 = out1;
2168
2169 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002170 s->lastlist[0] = 0;
2171 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002172 s->negated = FALSE;
2173
2174 return s;
2175}
2176
2177/*
2178 * A partially built NFA without the matching state filled in.
2179 * Frag_T.start points at the start state.
2180 * Frag_T.out is a list of places that need to be set to the
2181 * next state for this fragment.
2182 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002183
2184/* Since the out pointers in the list are always
2185 * uninitialized, we use the pointers themselves
2186 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002188union Ptrlist
2189{
2190 Ptrlist *next;
2191 nfa_state_T *s;
2192};
2193
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194struct Frag
2195{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002196 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002197 Ptrlist *out;
2198};
2199typedef struct Frag Frag_T;
2200
2201static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2202static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2203static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2204static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2205static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2206static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2207
2208/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002209 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002210 */
2211 static Frag_T
2212frag(start, out)
2213 nfa_state_T *start;
2214 Ptrlist *out;
2215{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002216 Frag_T n;
2217
2218 n.start = start;
2219 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 return n;
2221}
2222
2223/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 * Create singleton list containing just outp.
2225 */
2226 static Ptrlist *
2227list1(outp)
2228 nfa_state_T **outp;
2229{
2230 Ptrlist *l;
2231
2232 l = (Ptrlist *)outp;
2233 l->next = NULL;
2234 return l;
2235}
2236
2237/*
2238 * Patch the list of states at out to point to start.
2239 */
2240 static void
2241patch(l, s)
2242 Ptrlist *l;
2243 nfa_state_T *s;
2244{
2245 Ptrlist *next;
2246
2247 for (; l; l = next)
2248 {
2249 next = l->next;
2250 l->s = s;
2251 }
2252}
2253
2254
2255/*
2256 * Join the two lists l1 and l2, returning the combination.
2257 */
2258 static Ptrlist *
2259append(l1, l2)
2260 Ptrlist *l1;
2261 Ptrlist *l2;
2262{
2263 Ptrlist *oldl1;
2264
2265 oldl1 = l1;
2266 while (l1->next)
2267 l1 = l1->next;
2268 l1->next = l2;
2269 return oldl1;
2270}
2271
2272/*
2273 * Stack used for transforming postfix form into NFA.
2274 */
2275static Frag_T empty;
2276
2277 static void
2278st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002279 int *postfix UNUSED;
2280 int *end UNUSED;
2281 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002283#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002284 FILE *df;
2285 int *p2;
2286
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002287 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288 if (df)
2289 {
2290 fprintf(df, "Error popping the stack!\n");
2291#ifdef DEBUG
2292 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2293#endif
2294 fprintf(df, "Postfix form is: ");
2295#ifdef DEBUG
2296 for (p2 = postfix; p2 < end; p2++)
2297 {
2298 nfa_set_code(*p2);
2299 fprintf(df, "%s, ", code);
2300 }
2301 nfa_set_code(*p);
2302 fprintf(df, "\nCurrent position is: ");
2303 for (p2 = postfix; p2 <= p; p2 ++)
2304 {
2305 nfa_set_code(*p2);
2306 fprintf(df, "%s, ", code);
2307 }
2308#else
2309 for (p2 = postfix; p2 < end; p2++)
2310 {
2311 fprintf(df, "%d, ", *p2);
2312 }
2313 fprintf(df, "\nCurrent position is: ");
2314 for (p2 = postfix; p2 <= p; p2 ++)
2315 {
2316 fprintf(df, "%d, ", *p2);
2317 }
2318#endif
2319 fprintf(df, "\n--------------------------\n");
2320 fclose(df);
2321 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002322#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002323 EMSG(_("E874: (NFA) Could not pop the stack !"));
2324}
2325
2326/*
2327 * Push an item onto the stack.
2328 */
2329 static void
2330st_push(s, p, stack_end)
2331 Frag_T s;
2332 Frag_T **p;
2333 Frag_T *stack_end;
2334{
2335 Frag_T *stackp = *p;
2336
2337 if (stackp >= stack_end)
2338 return;
2339 *stackp = s;
2340 *p = *p + 1;
2341}
2342
2343/*
2344 * Pop an item from the stack.
2345 */
2346 static Frag_T
2347st_pop(p, stack)
2348 Frag_T **p;
2349 Frag_T *stack;
2350{
2351 Frag_T *stackp;
2352
2353 *p = *p - 1;
2354 stackp = *p;
2355 if (stackp < stack)
2356 return empty;
2357 return **p;
2358}
2359
2360/*
2361 * Convert a postfix form into its equivalent NFA.
2362 * Return the NFA start state on success, NULL otherwise.
2363 */
2364 static nfa_state_T *
2365post2nfa(postfix, end, nfa_calc_size)
2366 int *postfix;
2367 int *end;
2368 int nfa_calc_size;
2369{
2370 int *p;
2371 int mopen;
2372 int mclose;
2373 Frag_T *stack = NULL;
2374 Frag_T *stackp = NULL;
2375 Frag_T *stack_end = NULL;
2376 Frag_T e1;
2377 Frag_T e2;
2378 Frag_T e;
2379 nfa_state_T *s;
2380 nfa_state_T *s1;
2381 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002382 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383
2384 if (postfix == NULL)
2385 return NULL;
2386
Bram Moolenaar053bb602013-05-20 13:55:21 +02002387#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002388#define POP() st_pop(&stackp, stack); \
2389 if (stackp < stack) \
2390 { \
2391 st_error(postfix, end, p); \
2392 return NULL; \
2393 }
2394
2395 if (nfa_calc_size == FALSE)
2396 {
2397 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002398 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002399 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002400 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002401 }
2402
2403 for (p = postfix; p < end; ++p)
2404 {
2405 switch (*p)
2406 {
2407 case NFA_CONCAT:
2408 /* Catenation.
2409 * Pay attention: this operator does not exist
2410 * in the r.e. itself (it is implicit, really).
2411 * It is added when r.e. is translated to postfix
2412 * form in re2post().
2413 *
2414 * No new state added here. */
2415 if (nfa_calc_size == TRUE)
2416 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002417 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002418 break;
2419 }
2420 e2 = POP();
2421 e1 = POP();
2422 patch(e1.out, e2.start);
2423 PUSH(frag(e1.start, e2.out));
2424 break;
2425
2426 case NFA_NOT:
2427 /* Negation of a character */
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 e1 = POP();
2434 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002435#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002436 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002437 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002438#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 PUSH(e1);
2440 break;
2441
2442 case NFA_OR:
2443 /* Alternation */
2444 if (nfa_calc_size == TRUE)
2445 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002446 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447 break;
2448 }
2449 e2 = POP();
2450 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002451 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002453 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002454 PUSH(frag(s, append(e1.out, e2.out)));
2455 break;
2456
2457 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002458 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 if (nfa_calc_size == TRUE)
2460 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002461 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462 break;
2463 }
2464 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002465 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002467 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002468 patch(e.out, s);
2469 PUSH(frag(s, list1(&s->out1)));
2470 break;
2471
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002472 case NFA_STAR_NONGREEDY:
2473 /* Zero or more, prefer zero */
2474 if (nfa_calc_size == TRUE)
2475 {
2476 nstate++;
2477 break;
2478 }
2479 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002480 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002481 if (s == NULL)
2482 goto theend;
2483 patch(e.out, s);
2484 PUSH(frag(s, list1(&s->out)));
2485 break;
2486
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002487 case NFA_QUEST:
2488 /* one or zero atoms=> greedy match */
2489 if (nfa_calc_size == TRUE)
2490 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002491 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002492 break;
2493 }
2494 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002495 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002496 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002497 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002498 PUSH(frag(s, append(e.out, list1(&s->out1))));
2499 break;
2500
2501 case NFA_QUEST_NONGREEDY:
2502 /* zero or one atoms => non-greedy match */
2503 if (nfa_calc_size == TRUE)
2504 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002505 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 break;
2507 }
2508 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002509 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002510 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002511 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002512 PUSH(frag(s, append(e.out, list1(&s->out))));
2513 break;
2514
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515 case NFA_SKIP_CHAR:
2516 /* Symbol of 0-length, Used in a repetition
2517 * with max/min count of 0 */
2518 if (nfa_calc_size == TRUE)
2519 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002520 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521 break;
2522 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002523 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002524 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002525 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002526 PUSH(frag(s, list1(&s->out)));
2527 break;
2528
2529 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002530 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002531 case NFA_PREV_ATOM_JUST_BEFORE:
2532 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002533 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002534 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002535 * The \@<= operator: match for the preceding atom.
2536 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002537 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002538 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539
2540 if (nfa_calc_size == TRUE)
2541 {
2542 nstate += 2;
2543 break;
2544 }
2545 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002546 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002548 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002549 patch(e.out, s1);
2550
Bram Moolenaar525666f2013-06-02 16:40:55 +02002551 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002552 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002553 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002554 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2555 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002556 {
2557 s->negated = TRUE;
2558 s1->negated = TRUE;
2559 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002560 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2561 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2562 {
2563 s->val = *++p; /* get the count */
2564 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2565 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002566
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002567 PUSH(frag(s, list1(&s1->out)));
2568 break;
2569
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002570#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002571 case NFA_COMPOSING: /* char with composing char */
2572#if 0
2573 /* TODO */
2574 if (regflags & RF_ICOMBINE)
2575 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002576 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002577 }
2578#endif
2579 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002580#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002581
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002582 case NFA_MOPEN: /* \( \) Submatch */
2583 case NFA_MOPEN1:
2584 case NFA_MOPEN2:
2585 case NFA_MOPEN3:
2586 case NFA_MOPEN4:
2587 case NFA_MOPEN5:
2588 case NFA_MOPEN6:
2589 case NFA_MOPEN7:
2590 case NFA_MOPEN8:
2591 case NFA_MOPEN9:
2592#ifdef FEAT_SYN_HL
2593 case NFA_ZOPEN: /* \z( \) Submatch */
2594 case NFA_ZOPEN1:
2595 case NFA_ZOPEN2:
2596 case NFA_ZOPEN3:
2597 case NFA_ZOPEN4:
2598 case NFA_ZOPEN5:
2599 case NFA_ZOPEN6:
2600 case NFA_ZOPEN7:
2601 case NFA_ZOPEN8:
2602 case NFA_ZOPEN9:
2603#endif
2604 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002605 if (nfa_calc_size == TRUE)
2606 {
2607 nstate += 2;
2608 break;
2609 }
2610
2611 mopen = *p;
2612 switch (*p)
2613 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002614 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2615#ifdef FEAT_SYN_HL
2616 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2617 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2618 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2619 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2620 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2621 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2622 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2623 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2624 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2625 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2626#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002627#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002628 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002629#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002630 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002631 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002632 mclose = *p + NSUBEXP;
2633 break;
2634 }
2635
2636 /* Allow "NFA_MOPEN" as a valid postfix representation for
2637 * the empty regexp "". In this case, the NFA will be
2638 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2639 * empty groups of parenthesis, and empty mbyte chars */
2640 if (stackp == stack)
2641 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002642 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002644 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002645 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002646 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002647 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 patch(list1(&s->out), s1);
2649 PUSH(frag(s, list1(&s1->out)));
2650 break;
2651 }
2652
2653 /* At least one node was emitted before NFA_MOPEN, so
2654 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2655 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002656 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002657 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002658 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659
Bram Moolenaar525666f2013-06-02 16:40:55 +02002660 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002661 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002662 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002663 patch(e.out, s1);
2664
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002665#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002666 if (mopen == NFA_COMPOSING)
2667 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002668 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002669#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670
2671 PUSH(frag(s, list1(&s1->out)));
2672 break;
2673
Bram Moolenaar5714b802013-05-28 22:03:20 +02002674 case NFA_BACKREF1:
2675 case NFA_BACKREF2:
2676 case NFA_BACKREF3:
2677 case NFA_BACKREF4:
2678 case NFA_BACKREF5:
2679 case NFA_BACKREF6:
2680 case NFA_BACKREF7:
2681 case NFA_BACKREF8:
2682 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002683#ifdef FEAT_SYN_HL
2684 case NFA_ZREF1:
2685 case NFA_ZREF2:
2686 case NFA_ZREF3:
2687 case NFA_ZREF4:
2688 case NFA_ZREF5:
2689 case NFA_ZREF6:
2690 case NFA_ZREF7:
2691 case NFA_ZREF8:
2692 case NFA_ZREF9:
2693#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002694 if (nfa_calc_size == TRUE)
2695 {
2696 nstate += 2;
2697 break;
2698 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002699 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002700 if (s == NULL)
2701 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002702 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002703 if (s1 == NULL)
2704 goto theend;
2705 patch(list1(&s->out), s1);
2706 PUSH(frag(s, list1(&s1->out)));
2707 break;
2708
Bram Moolenaar423532e2013-05-29 21:14:42 +02002709 case NFA_LNUM:
2710 case NFA_LNUM_GT:
2711 case NFA_LNUM_LT:
2712 case NFA_VCOL:
2713 case NFA_VCOL_GT:
2714 case NFA_VCOL_LT:
2715 case NFA_COL:
2716 case NFA_COL_GT:
2717 case NFA_COL_LT:
2718 if (nfa_calc_size == TRUE)
2719 {
2720 nstate += 1;
2721 break;
2722 }
2723 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002724 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002725 if (s == NULL)
2726 goto theend;
2727 s->val = e1.start->c;
2728 PUSH(frag(s, list1(&s->out)));
2729 break;
2730
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731 case NFA_ZSTART:
2732 case NFA_ZEND:
2733 default:
2734 /* Operands */
2735 if (nfa_calc_size == TRUE)
2736 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002737 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 break;
2739 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002740 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002741 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002742 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002743 PUSH(frag(s, list1(&s->out)));
2744 break;
2745
2746 } /* switch(*p) */
2747
2748 } /* for(p = postfix; *p; ++p) */
2749
2750 if (nfa_calc_size == TRUE)
2751 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002752 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002753 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002754 }
2755
2756 e = POP();
2757 if (stackp != stack)
2758 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2759
2760 if (istate >= nstate)
2761 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2762
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002763 matchstate = &state_ptr[istate++]; /* the match state */
2764 matchstate->c = NFA_MATCH;
2765 matchstate->out = matchstate->out1 = NULL;
2766
2767 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002768 ret = e.start;
2769
2770theend:
2771 vim_free(stack);
2772 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002773
2774#undef POP1
2775#undef PUSH1
2776#undef POP2
2777#undef PUSH2
2778#undef POP
2779#undef PUSH
2780}
2781
2782/****************************************************************
2783 * NFA execution code.
2784 ****************************************************************/
2785
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002786typedef struct
2787{
2788 int in_use; /* number of subexpr with useful info */
2789
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002790 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002791 union
2792 {
2793 struct multipos
2794 {
2795 lpos_T start;
2796 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002797 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002798 struct linepos
2799 {
2800 char_u *start;
2801 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002802 } line[NSUBEXP];
2803 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002804} regsub_T;
2805
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002806typedef struct
2807{
2808 regsub_T norm; /* \( .. \) matches */
2809#ifdef FEAT_SYN_HL
2810 regsub_T synt; /* \z( .. \) matches */
2811#endif
2812} regsubs_T;
2813
Bram Moolenaara2d95102013-06-04 14:23:05 +02002814/* nfa_pim_T stores a Postponed Invisible Match. */
2815typedef struct nfa_pim_S nfa_pim_T;
2816struct nfa_pim_S
2817{
2818 nfa_state_T *state;
2819 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2820 nfa_pim_T *pim; /* another PIM at the same position */
2821 regsubs_T subs; /* submatch info, only party used */
2822};
2823
2824/* Values for done in nfa_pim_T. */
2825#define NFA_PIM_TODO 0
2826#define NFA_PIM_MATCH 1
2827#define NFA_PIM_NOMATCH -1
2828
2829
Bram Moolenaar963fee22013-05-26 21:47:28 +02002830/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002831typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832{
2833 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002834 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002835 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002836 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002837} nfa_thread_T;
2838
Bram Moolenaar963fee22013-05-26 21:47:28 +02002839/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840typedef struct
2841{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002842 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002843 int n; /* nr of states currently in "t" */
2844 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002845 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002846} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002847
Bram Moolenaar5714b802013-05-28 22:03:20 +02002848#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002849static void log_subsexpr __ARGS((regsubs_T *subs));
2850static void log_subexpr __ARGS((regsub_T *sub));
2851
2852 static void
2853log_subsexpr(subs)
2854 regsubs_T *subs;
2855{
2856 log_subexpr(&subs->norm);
2857# ifdef FEAT_SYN_HL
2858 log_subexpr(&subs->synt);
2859# endif
2860}
2861
Bram Moolenaar5714b802013-05-28 22:03:20 +02002862 static void
2863log_subexpr(sub)
2864 regsub_T *sub;
2865{
2866 int j;
2867
2868 for (j = 0; j < sub->in_use; j++)
2869 if (REG_MULTI)
2870 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2871 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002872 sub->list.multi[j].start.col,
2873 (int)sub->list.multi[j].start.lnum,
2874 sub->list.multi[j].end.col,
2875 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002876 else
2877 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2878 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002879 (char *)sub->list.line[j].start,
2880 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002881 fprintf(log_fd, "\n");
2882}
2883#endif
2884
Bram Moolenaar963fee22013-05-26 21:47:28 +02002885/* Used during execution: whether a match has been found. */
2886static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002887
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002888static void clear_sub __ARGS((regsub_T *sub));
2889static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2890static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002891static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002892static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02002893static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002894
2895 static void
2896clear_sub(sub)
2897 regsub_T *sub;
2898{
2899 if (REG_MULTI)
2900 /* Use 0xff to set lnum to -1 */
2901 vim_memset(sub->list.multi, 0xff,
2902 sizeof(struct multipos) * nfa_nsubexpr);
2903 else
2904 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2905 sub->in_use = 0;
2906}
2907
2908/*
2909 * Copy the submatches from "from" to "to".
2910 */
2911 static void
2912copy_sub(to, from)
2913 regsub_T *to;
2914 regsub_T *from;
2915{
2916 to->in_use = from->in_use;
2917 if (from->in_use > 0)
2918 {
2919 /* Copy the match start and end positions. */
2920 if (REG_MULTI)
2921 mch_memmove(&to->list.multi[0],
2922 &from->list.multi[0],
2923 sizeof(struct multipos) * from->in_use);
2924 else
2925 mch_memmove(&to->list.line[0],
2926 &from->list.line[0],
2927 sizeof(struct linepos) * from->in_use);
2928 }
2929}
2930
2931/*
2932 * Like copy_sub() but exclude the main match.
2933 */
2934 static void
2935copy_sub_off(to, from)
2936 regsub_T *to;
2937 regsub_T *from;
2938{
2939 if (to->in_use < from->in_use)
2940 to->in_use = from->in_use;
2941 if (from->in_use > 1)
2942 {
2943 /* Copy the match start and end positions. */
2944 if (REG_MULTI)
2945 mch_memmove(&to->list.multi[1],
2946 &from->list.multi[1],
2947 sizeof(struct multipos) * (from->in_use - 1));
2948 else
2949 mch_memmove(&to->list.line[1],
2950 &from->list.line[1],
2951 sizeof(struct linepos) * (from->in_use - 1));
2952 }
2953}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002954
Bram Moolenaar428e9872013-05-30 17:05:39 +02002955/*
2956 * Return TRUE if "sub1" and "sub2" have the same positions.
2957 */
2958 static int
2959sub_equal(sub1, sub2)
2960 regsub_T *sub1;
2961 regsub_T *sub2;
2962{
2963 int i;
2964 int todo;
2965 linenr_T s1, e1;
2966 linenr_T s2, e2;
2967 char_u *sp1, *ep1;
2968 char_u *sp2, *ep2;
2969
2970 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2971 if (REG_MULTI)
2972 {
2973 for (i = 0; i < todo; ++i)
2974 {
2975 if (i < sub1->in_use)
2976 {
2977 s1 = sub1->list.multi[i].start.lnum;
2978 e1 = sub1->list.multi[i].end.lnum;
2979 }
2980 else
2981 {
2982 s1 = 0;
2983 e1 = 0;
2984 }
2985 if (i < sub2->in_use)
2986 {
2987 s2 = sub2->list.multi[i].start.lnum;
2988 e2 = sub2->list.multi[i].end.lnum;
2989 }
2990 else
2991 {
2992 s2 = 0;
2993 e2 = 0;
2994 }
2995 if (s1 != s2 || e1 != e2)
2996 return FALSE;
2997 if (s1 != 0 && sub1->list.multi[i].start.col
2998 != sub2->list.multi[i].start.col)
2999 return FALSE;
3000 if (e1 != 0 && sub1->list.multi[i].end.col
3001 != sub2->list.multi[i].end.col)
3002 return FALSE;
3003 }
3004 }
3005 else
3006 {
3007 for (i = 0; i < todo; ++i)
3008 {
3009 if (i < sub1->in_use)
3010 {
3011 sp1 = sub1->list.line[i].start;
3012 ep1 = sub1->list.line[i].end;
3013 }
3014 else
3015 {
3016 sp1 = NULL;
3017 ep1 = NULL;
3018 }
3019 if (i < sub2->in_use)
3020 {
3021 sp2 = sub2->list.line[i].start;
3022 ep2 = sub2->list.line[i].end;
3023 }
3024 else
3025 {
3026 sp2 = NULL;
3027 ep2 = NULL;
3028 }
3029 if (sp1 != sp2 || ep1 != ep2)
3030 return FALSE;
3031 }
3032 }
3033
3034 return TRUE;
3035}
3036
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003037#ifdef ENABLE_LOG
3038 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003039report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003040{
3041 int col;
3042
3043 if (sub->in_use <= 0)
3044 col = -1;
3045 else if (REG_MULTI)
3046 col = sub->list.multi[0].start.col;
3047 else
3048 col = (int)(sub->list.line[0].start - regline);
3049 nfa_set_code(state->c);
3050 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3051 action, abs(state->id), lid, state->c, code, col);
3052}
3053#endif
3054
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003055 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003056addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003057 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003058 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003059 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003060 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003061{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003062 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003063 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003064 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003065 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003066 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003067 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003068 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003069#ifdef ENABLE_LOG
3070 int did_print = FALSE;
3071#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003072
3073 if (l == NULL || state == NULL)
3074 return;
3075
3076 switch (state->c)
3077 {
3078 case NFA_SPLIT:
3079 case NFA_NOT:
3080 case NFA_NOPEN:
3081 case NFA_NCLOSE:
3082 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003083 case NFA_MCLOSE1:
3084 case NFA_MCLOSE2:
3085 case NFA_MCLOSE3:
3086 case NFA_MCLOSE4:
3087 case NFA_MCLOSE5:
3088 case NFA_MCLOSE6:
3089 case NFA_MCLOSE7:
3090 case NFA_MCLOSE8:
3091 case NFA_MCLOSE9:
3092#ifdef FEAT_SYN_HL
3093 case NFA_ZCLOSE:
3094 case NFA_ZCLOSE1:
3095 case NFA_ZCLOSE2:
3096 case NFA_ZCLOSE3:
3097 case NFA_ZCLOSE4:
3098 case NFA_ZCLOSE5:
3099 case NFA_ZCLOSE6:
3100 case NFA_ZCLOSE7:
3101 case NFA_ZCLOSE8:
3102 case NFA_ZCLOSE9:
3103#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003104 /* These nodes are not added themselves but their "out" and/or
3105 * "out1" may be added below. */
3106 break;
3107
3108 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003109 case NFA_MOPEN1:
3110 case NFA_MOPEN2:
3111 case NFA_MOPEN3:
3112 case NFA_MOPEN4:
3113 case NFA_MOPEN5:
3114 case NFA_MOPEN6:
3115 case NFA_MOPEN7:
3116 case NFA_MOPEN8:
3117 case NFA_MOPEN9:
3118#ifdef FEAT_SYN_HL
3119 case NFA_ZOPEN:
3120 case NFA_ZOPEN1:
3121 case NFA_ZOPEN2:
3122 case NFA_ZOPEN3:
3123 case NFA_ZOPEN4:
3124 case NFA_ZOPEN5:
3125 case NFA_ZOPEN6:
3126 case NFA_ZOPEN7:
3127 case NFA_ZOPEN8:
3128 case NFA_ZOPEN9:
3129#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003130 /* These nodes do not need to be added, but we need to bail out
3131 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003132 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003133 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003134 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003135 break;
3136
Bram Moolenaar307aa162013-06-02 16:34:21 +02003137 case NFA_BOL:
3138 case NFA_BOF:
3139 /* "^" won't match past end-of-line, don't bother trying.
3140 * Except when we are going to the next line for a look-behind
3141 * match. */
3142 if (reginput > regline
3143 && (nfa_endp == NULL
3144 || !REG_MULTI
3145 || reglnum == nfa_endp->se_u.pos.lnum))
3146 goto skip_add;
3147 /* FALLTHROUGH */
3148
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003149 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003150 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003151 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003152 /* This state is already in the list, don't add it again,
3153 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003154 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003155 {
3156skip_add:
3157#ifdef ENABLE_LOG
3158 nfa_set_code(state->c);
3159 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3160 abs(state->id), l->id, state->c, code);
3161#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003162 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003163 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003164
3165 /* See if the same state is already in the list with the same
3166 * positions. */
3167 for (i = 0; i < l->n; ++i)
3168 {
3169 thread = &l->t[i];
3170 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003171 && sub_equal(&thread->subs.norm, &subs->norm)
3172#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003173 && (!nfa_has_zsubexpr ||
3174 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003175#endif
3176 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003177 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003178 }
3179 }
3180
Bram Moolenaara2d95102013-06-04 14:23:05 +02003181 /* when there are backreferences or look-behind matches the number
3182 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003183 if (nfa_has_backref && l->n == l->len)
3184 {
3185 int newlen = l->len * 3 / 2 + 50;
3186
3187 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3188 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003190
3191 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003192 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003193 thread = &l->t[l->n++];
3194 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003195 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003196 copy_sub(&thread->subs.norm, &subs->norm);
3197#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003198 if (nfa_has_zsubexpr)
3199 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003200#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003201#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003202 report_state("Adding", &thread->subs.norm, state, l->id);
3203 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003204#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205 }
3206
3207#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003208 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003209 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003210#endif
3211 switch (state->c)
3212 {
3213 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003214 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003215 break;
3216
3217 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003218 /* order matters here */
3219 addstate(l, state->out, subs, off);
3220 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003221 break;
3222
Bram Moolenaar5714b802013-05-28 22:03:20 +02003223 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003224 case NFA_NOPEN:
3225 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003226 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003227 break;
3228
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003229 case NFA_MOPEN:
3230 case NFA_MOPEN1:
3231 case NFA_MOPEN2:
3232 case NFA_MOPEN3:
3233 case NFA_MOPEN4:
3234 case NFA_MOPEN5:
3235 case NFA_MOPEN6:
3236 case NFA_MOPEN7:
3237 case NFA_MOPEN8:
3238 case NFA_MOPEN9:
3239#ifdef FEAT_SYN_HL
3240 case NFA_ZOPEN:
3241 case NFA_ZOPEN1:
3242 case NFA_ZOPEN2:
3243 case NFA_ZOPEN3:
3244 case NFA_ZOPEN4:
3245 case NFA_ZOPEN5:
3246 case NFA_ZOPEN6:
3247 case NFA_ZOPEN7:
3248 case NFA_ZOPEN8:
3249 case NFA_ZOPEN9:
3250#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003252 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003253 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003255 sub = &subs->norm;
3256 }
3257#ifdef FEAT_SYN_HL
3258 else if (state->c >= NFA_ZOPEN)
3259 {
3260 subidx = state->c - NFA_ZOPEN;
3261 sub = &subs->synt;
3262 }
3263#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003264 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003265 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003266 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003267 sub = &subs->norm;
3268 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003270 /* Set the position (with "off") in the subexpression. Save and
3271 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003272 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 if (REG_MULTI)
3274 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003275 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003276 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003277 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003278 save_in_use = -1;
3279 }
3280 else
3281 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003282 save_in_use = sub->in_use;
3283 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003284 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003285 sub->list.multi[i].start.lnum = -1;
3286 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003287 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003288 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003289 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003290 if (off == -1)
3291 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003292 sub->list.multi[subidx].start.lnum = reglnum + 1;
3293 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003294 }
3295 else
3296 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003297 sub->list.multi[subidx].start.lnum = reglnum;
3298 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003299 (colnr_T)(reginput - regline + off);
3300 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 }
3302 else
3303 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003304 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003305 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003306 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003307 save_in_use = -1;
3308 }
3309 else
3310 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003311 save_in_use = sub->in_use;
3312 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003313 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003314 sub->list.line[i].start = NULL;
3315 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003316 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003317 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003318 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003319 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 }
3321
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003322 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003323
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003324 if (save_in_use == -1)
3325 {
3326 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003327 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003328 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003329 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003330 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003331 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003332 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 break;
3334
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003335 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003336 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003337 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003338 /* Do not overwrite the position set by \ze. If no \ze
3339 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003340 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003341 break;
3342 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003343 case NFA_MCLOSE1:
3344 case NFA_MCLOSE2:
3345 case NFA_MCLOSE3:
3346 case NFA_MCLOSE4:
3347 case NFA_MCLOSE5:
3348 case NFA_MCLOSE6:
3349 case NFA_MCLOSE7:
3350 case NFA_MCLOSE8:
3351 case NFA_MCLOSE9:
3352#ifdef FEAT_SYN_HL
3353 case NFA_ZCLOSE:
3354 case NFA_ZCLOSE1:
3355 case NFA_ZCLOSE2:
3356 case NFA_ZCLOSE3:
3357 case NFA_ZCLOSE4:
3358 case NFA_ZCLOSE5:
3359 case NFA_ZCLOSE6:
3360 case NFA_ZCLOSE7:
3361 case NFA_ZCLOSE8:
3362 case NFA_ZCLOSE9:
3363#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003366 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003368 sub = &subs->norm;
3369 }
3370#ifdef FEAT_SYN_HL
3371 else if (state->c >= NFA_ZCLOSE)
3372 {
3373 subidx = state->c - NFA_ZCLOSE;
3374 sub = &subs->synt;
3375 }
3376#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003377 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003378 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003379 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003380 sub = &subs->norm;
3381 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003382
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003383 /* We don't fill in gaps here, there must have been an MOPEN that
3384 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003385 save_in_use = sub->in_use;
3386 if (sub->in_use <= subidx)
3387 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003388 if (REG_MULTI)
3389 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003390 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003391 if (off == -1)
3392 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003393 sub->list.multi[subidx].end.lnum = reglnum + 1;
3394 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003395 }
3396 else
3397 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003398 sub->list.multi[subidx].end.lnum = reglnum;
3399 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003400 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003401 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402 }
3403 else
3404 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003405 save_ptr = sub->list.line[subidx].end;
3406 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003407 }
3408
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003409 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003410
3411 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003412 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003414 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003415 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003416 break;
3417 }
3418}
3419
3420/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003421 * Like addstate(), but the new state(s) are put at position "*ip".
3422 * Used for zero-width matches, next state to use is the added one.
3423 * This makes sure the order of states to be tried does not change, which
3424 * matters for alternatives.
3425 */
3426 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003427addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003428 nfa_list_T *l; /* runtime state list */
3429 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003430 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003431 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003432 int *ip;
3433{
3434 int tlen = l->n;
3435 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003436 int listidx = *ip;
3437 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003438
3439 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003440 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003441
Bram Moolenaara2d95102013-06-04 14:23:05 +02003442 /* fill in the "pim" field in the new states */
3443 if (pim != NULL)
3444 for (i = tlen; i < l->n; ++i)
3445 l->t[i].pim = pim;
3446
Bram Moolenaar4b417062013-05-25 20:19:50 +02003447 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003448 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003449 return;
3450
3451 /* re-order to put the new state at the current position */
3452 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003453 if (count == 1)
3454 {
3455 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003456 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003457 }
3458 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003459 {
3460 /* make space for new states, then move them from the
3461 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003462 mch_memmove(&(l->t[listidx + count]),
3463 &(l->t[listidx + 1]),
3464 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3465 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003466 &(l->t[l->n - 1]),
3467 sizeof(nfa_thread_T) * count);
3468 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003469 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003470 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003471}
3472
3473/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 * Check character class "class" against current character c.
3475 */
3476 static int
3477check_char_class(class, c)
3478 int class;
3479 int c;
3480{
3481 switch (class)
3482 {
3483 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003484 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 return OK;
3486 break;
3487 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003488 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 return OK;
3490 break;
3491 case NFA_CLASS_BLANK:
3492 if (c == ' ' || c == '\t')
3493 return OK;
3494 break;
3495 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003496 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003497 return OK;
3498 break;
3499 case NFA_CLASS_DIGIT:
3500 if (VIM_ISDIGIT(c))
3501 return OK;
3502 break;
3503 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003504 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003505 return OK;
3506 break;
3507 case NFA_CLASS_LOWER:
3508 if (MB_ISLOWER(c))
3509 return OK;
3510 break;
3511 case NFA_CLASS_PRINT:
3512 if (vim_isprintc(c))
3513 return OK;
3514 break;
3515 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003516 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003517 return OK;
3518 break;
3519 case NFA_CLASS_SPACE:
3520 if ((c >=9 && c <= 13) || (c == ' '))
3521 return OK;
3522 break;
3523 case NFA_CLASS_UPPER:
3524 if (MB_ISUPPER(c))
3525 return OK;
3526 break;
3527 case NFA_CLASS_XDIGIT:
3528 if (vim_isxdigit(c))
3529 return OK;
3530 break;
3531 case NFA_CLASS_TAB:
3532 if (c == '\t')
3533 return OK;
3534 break;
3535 case NFA_CLASS_RETURN:
3536 if (c == '\r')
3537 return OK;
3538 break;
3539 case NFA_CLASS_BACKSPACE:
3540 if (c == '\b')
3541 return OK;
3542 break;
3543 case NFA_CLASS_ESCAPE:
3544 if (c == '\033')
3545 return OK;
3546 break;
3547
3548 default:
3549 /* should not be here :P */
3550 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3551 }
3552 return FAIL;
3553}
3554
Bram Moolenaar5714b802013-05-28 22:03:20 +02003555static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3556
3557/*
3558 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003559 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003560 */
3561 static int
3562match_backref(sub, subidx, bytelen)
3563 regsub_T *sub; /* pointers to subexpressions */
3564 int subidx;
3565 int *bytelen; /* out: length of match in bytes */
3566{
3567 int len;
3568
3569 if (sub->in_use <= subidx)
3570 {
3571retempty:
3572 /* backref was not set, match an empty string */
3573 *bytelen = 0;
3574 return TRUE;
3575 }
3576
3577 if (REG_MULTI)
3578 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003579 if (sub->list.multi[subidx].start.lnum < 0
3580 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003581 goto retempty;
3582 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003583 len = sub->list.multi[subidx].end.col
3584 - sub->list.multi[subidx].start.col;
3585 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003586 reginput, &len) == 0)
3587 {
3588 *bytelen = len;
3589 return TRUE;
3590 }
3591 }
3592 else
3593 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003594 if (sub->list.line[subidx].start == NULL
3595 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003596 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003597 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3598 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003599 {
3600 *bytelen = len;
3601 return TRUE;
3602 }
3603 }
3604 return FALSE;
3605}
3606
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003607#ifdef FEAT_SYN_HL
3608
3609static int match_zref __ARGS((int subidx, int *bytelen));
3610
3611/*
3612 * Check for a match with \z subexpression "subidx".
3613 * Return TRUE if it matches.
3614 */
3615 static int
3616match_zref(subidx, bytelen)
3617 int subidx;
3618 int *bytelen; /* out: length of match in bytes */
3619{
3620 int len;
3621
3622 cleanup_zsubexpr();
3623 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3624 {
3625 /* backref was not set, match an empty string */
3626 *bytelen = 0;
3627 return TRUE;
3628 }
3629
3630 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3631 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3632 {
3633 *bytelen = len;
3634 return TRUE;
3635 }
3636 return FALSE;
3637}
3638#endif
3639
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003641 * Save list IDs for all NFA states of "prog" into "list".
3642 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003643 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 */
3645 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003646nfa_save_listids(prog, list)
3647 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003648 int *list;
3649{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003650 int i;
3651 nfa_state_T *p;
3652
3653 /* Order in the list is reverse, it's a bit faster that way. */
3654 p = &prog->state[0];
3655 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003656 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003657 list[i] = p->lastlist[1];
3658 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003659 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660 }
3661}
3662
3663/*
3664 * Restore list IDs from "list" to all NFA states.
3665 */
3666 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003667nfa_restore_listids(prog, list)
3668 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669 int *list;
3670{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003671 int i;
3672 nfa_state_T *p;
3673
3674 p = &prog->state[0];
3675 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003676 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003677 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003678 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679 }
3680}
3681
Bram Moolenaar423532e2013-05-29 21:14:42 +02003682 static int
3683nfa_re_num_cmp(val, op, pos)
3684 long_u val;
3685 int op;
3686 long_u pos;
3687{
3688 if (op == 1) return pos > val;
3689 if (op == 2) return pos < val;
3690 return val == pos;
3691}
3692
Bram Moolenaarf46da702013-06-02 22:37:42 +02003693static 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 +02003694static 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 +02003695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003697 * Recursively call nfa_regmatch()
3698 */
3699 static int
3700recursive_regmatch(state, prog, submatch, m, listids)
3701 nfa_state_T *state;
3702 nfa_regprog_T *prog;
3703 regsubs_T *submatch;
3704 regsubs_T *m;
3705 int **listids;
3706{
3707 char_u *save_reginput = reginput;
3708 char_u *save_regline = regline;
3709 int save_reglnum = reglnum;
3710 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003711 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003712 save_se_T *save_nfa_endp = nfa_endp;
3713 save_se_T endpos;
3714 save_se_T *endposp = NULL;
3715 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003716 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003717
3718 if (state->c == NFA_START_INVISIBLE_BEFORE)
3719 {
3720 /* The recursive match must end at the current position. */
3721 endposp = &endpos;
3722 if (REG_MULTI)
3723 {
3724 endpos.se_u.pos.col = (int)(reginput - regline);
3725 endpos.se_u.pos.lnum = reglnum;
3726 }
3727 else
3728 endpos.se_u.ptr = reginput;
3729
3730 /* Go back the specified number of bytes, or as far as the
3731 * start of the previous line, to try matching "\@<=" or
3732 * not matching "\@<!".
3733 * TODO: This is very inefficient! Would be better to
3734 * first check for a match with what follows. */
3735 if (state->val <= 0)
3736 {
3737 if (REG_MULTI)
3738 {
3739 regline = reg_getline(--reglnum);
3740 if (regline == NULL)
3741 /* can't go before the first line */
3742 regline = reg_getline(++reglnum);
3743 }
3744 reginput = regline;
3745 }
3746 else
3747 {
3748 if (REG_MULTI && (int)(reginput - regline) < state->val)
3749 {
3750 /* Not enough bytes in this line, go to end of
3751 * previous line. */
3752 regline = reg_getline(--reglnum);
3753 if (regline == NULL)
3754 {
3755 /* can't go before the first line */
3756 regline = reg_getline(++reglnum);
3757 reginput = regline;
3758 }
3759 else
3760 reginput = regline + STRLEN(regline);
3761 }
3762 if ((int)(reginput - regline) >= state->val)
3763 {
3764 reginput -= state->val;
3765#ifdef FEAT_MBYTE
3766 if (has_mbyte)
3767 reginput -= mb_head_off(regline, reginput);
3768#endif
3769 }
3770 else
3771 reginput = regline;
3772 }
3773 }
3774
Bram Moolenaarf46da702013-06-02 22:37:42 +02003775#ifdef ENABLE_LOG
3776 if (log_fd != stderr)
3777 fclose(log_fd);
3778 log_fd = NULL;
3779#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003780 /* Have to clear the lastlist field of the NFA nodes, so that
3781 * nfa_regmatch() and addstate() can run properly after recursion. */
3782 if (nfa_ll_index == 1)
3783 {
3784 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3785 * values and clear them. */
3786 if (*listids == NULL)
3787 {
3788 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3789 if (*listids == NULL)
3790 {
3791 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3792 return 0;
3793 }
3794 }
3795 nfa_save_listids(prog, *listids);
3796 need_restore = TRUE;
3797 /* any value of nfa_listid will do */
3798 }
3799 else
3800 {
3801 /* First recursive nfa_regmatch() call, switch to the second lastlist
3802 * entry. Make sure nfa_listid is different from a previous recursive
3803 * call, because some states may still have this ID. */
3804 ++nfa_ll_index;
3805 if (nfa_listid <= nfa_alt_listid)
3806 nfa_listid = nfa_alt_listid;
3807 }
3808
3809 /* Call nfa_regmatch() to check if the current concat matches at this
3810 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003811 nfa_endp = endposp;
3812 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003813
3814 if (need_restore)
3815 nfa_restore_listids(prog, *listids);
3816 else
3817 {
3818 --nfa_ll_index;
3819 nfa_alt_listid = nfa_listid;
3820 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003821
3822 /* restore position in input text */
3823 reginput = save_reginput;
3824 regline = save_regline;
3825 reglnum = save_reglnum;
3826 nfa_match = save_nfa_match;
3827 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003828 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003829
3830#ifdef ENABLE_LOG
3831 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3832 if (log_fd != NULL)
3833 {
3834 fprintf(log_fd, "****************************\n");
3835 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3836 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3837 fprintf(log_fd, "****************************\n");
3838 }
3839 else
3840 {
3841 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3842 log_fd = stderr;
3843 }
3844#endif
3845
3846 return result;
3847}
3848
Bram Moolenaara2d95102013-06-04 14:23:05 +02003849static int failure_chance __ARGS((nfa_state_T *state, int depth));
3850
3851/*
3852 * Estimate the chance of a match with "state" failing.
3853 * NFA_ANY: 1
3854 * specific character: 99
3855 */
3856 static int
3857failure_chance(state, depth)
3858 nfa_state_T *state;
3859 int depth;
3860{
3861 int c = state->c;
3862 int l, r;
3863
3864 /* detect looping */
3865 if (depth > 4)
3866 return 1;
3867
3868 if (c == NFA_SPLIT)
3869 {
3870 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3871 return 1;
3872 l = failure_chance(state->out, depth + 1);
3873 r = failure_chance(state->out1, depth + 1);
3874 return l < r ? l : r;
3875 }
3876 if (c == NFA_ANY)
3877 return 1;
3878 if (c > 0)
3879 return 99;
3880 if ((c >= NFA_MOPEN && c <= NFA_MOPEN9)
3881 || (c >= NFA_ZOPEN && c <= NFA_ZOPEN9)
3882 || c == NFA_NOPEN)
3883 return failure_chance(state->out, depth + 1);
3884 /* something else */
3885 return 50;
3886}
3887
Bram Moolenaarf46da702013-06-02 22:37:42 +02003888/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003889 * Main matching routine.
3890 *
3891 * Run NFA to determine whether it matches reginput.
3892 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003893 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003894 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003895 * Return TRUE if there is a match, FALSE otherwise.
3896 * Note: Caller must ensure that: start != NULL.
3897 */
3898 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003899nfa_regmatch(prog, start, submatch, m)
3900 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003901 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003902 regsubs_T *submatch;
3903 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003904{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003905 int result;
3906 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003907 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003908 int go_to_nextline = FALSE;
3909 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003910 nfa_list_T list[3];
3911 nfa_list_T *listtbl[2][2];
3912 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003913 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003914 nfa_list_T *thislist;
3915 nfa_list_T *nextlist;
3916 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003918 nfa_state_T *add_state;
3919 int add_count;
3920 int add_off;
3921 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003922#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003923 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003924
3925 if (debug == NULL)
3926 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003927 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003928 return FALSE;
3929 }
3930#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003931 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003932 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003933
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003934 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003935 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003936 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3937 list[0].len = nstate + 1;
3938 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3939 list[1].len = nstate + 1;
3940 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3941 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003942 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3943 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003944
3945#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003946 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003947 if (log_fd != NULL)
3948 {
3949 fprintf(log_fd, "**********************************\n");
3950 nfa_set_code(start->c);
3951 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3952 abs(start->id), code);
3953 fprintf(log_fd, "**********************************\n");
3954 }
3955 else
3956 {
3957 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3958 log_fd = stderr;
3959 }
3960#endif
3961
3962 thislist = &list[0];
3963 thislist->n = 0;
3964 nextlist = &list[1];
3965 nextlist->n = 0;
3966 neglist = &list[2];
3967 neglist->n = 0;
3968#ifdef ENABLE_LOG
3969 fprintf(log_fd, "(---) STARTSTATE\n");
3970#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003971 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003972 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003973
3974 /* There are two cases when the NFA advances: 1. input char matches the
3975 * NFA node and 2. input char does not match the NFA node, but the next
3976 * node is NFA_NOT. The following macro calls addstate() according to
3977 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3978 listtbl[0][0] = NULL;
3979 listtbl[0][1] = neglist;
3980 listtbl[1][0] = nextlist;
3981 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003982#define ADD_POS_NEG_STATE(state) \
3983 ll = listtbl[result ? 1 : 0][state->negated]; \
3984 if (ll != NULL) { \
3985 add_state = state->out; \
3986 add_off = clen; \
3987 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003988
3989 /*
3990 * Run for each character.
3991 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003992 for (;;)
3993 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003994 int curc;
3995 int clen;
3996
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003997#ifdef FEAT_MBYTE
3998 if (has_mbyte)
3999 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004000 curc = (*mb_ptr2char)(reginput);
4001 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002 }
4003 else
4004#endif
4005 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004006 curc = *reginput;
4007 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004008 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004009 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004010 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004011 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004012 go_to_nextline = FALSE;
4013 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004014
4015 /* swap lists */
4016 thislist = &list[flag];
4017 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004018 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004019 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004020 ++nfa_listid;
4021 thislist->id = nfa_listid;
4022 nextlist->id = nfa_listid + 1;
4023 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004024
Bram Moolenaara2d95102013-06-04 14:23:05 +02004025 pimlist.ga_len = 0;
4026
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004027#ifdef ENABLE_LOG
4028 fprintf(log_fd, "------------------------------------------\n");
4029 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004030 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004031 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004032 {
4033 int i;
4034
4035 for (i = 0; i < thislist->n; i++)
4036 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4037 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004038 fprintf(log_fd, "\n");
4039#endif
4040
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004041#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004042 fprintf(debug, "\n-------------------\n");
4043#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004044 /*
4045 * If the state lists are empty we can stop.
4046 */
4047 if (thislist->n == 0 && neglist->n == 0)
4048 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004049
4050 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004051 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004052 {
4053 if (neglist->n > 0)
4054 {
4055 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004056 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004057 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004058 }
4059 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004060 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004061
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004062#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004063 nfa_set_code(t->state->c);
4064 fprintf(debug, "%s, ", code);
4065#endif
4066#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004067 {
4068 int col;
4069
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004070 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004071 col = -1;
4072 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004073 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004074 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004075 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004076 nfa_set_code(t->state->c);
4077 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4078 abs(t->state->id), (int)t->state->c, code, col);
4079 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004080#endif
4081
4082 /*
4083 * Handle the possible codes of the current state.
4084 * The most important is NFA_MATCH.
4085 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004086 add_state = NULL;
4087 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004088 switch (t->state->c)
4089 {
4090 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004091 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004092 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004093 copy_sub(&submatch->norm, &t->subs.norm);
4094#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004095 if (nfa_has_zsubexpr)
4096 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004097#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004098#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004099 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004100#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004101 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004102 * states at this position. When the list of states is going
4103 * to be empty quit without advancing, so that "reginput" is
4104 * correct. */
4105 if (nextlist->n == 0 && neglist->n == 0)
4106 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004107 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004108 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004109
4110 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004111 /*
4112 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004113 * NFA_START_INVISIBLE_BEFORE node.
4114 * They surround a zero-width group, used with "\@=", "\&",
4115 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004116 * If we got here, it means that the current "invisible" group
4117 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004118 * nfa_regmatch(). For a look-behind match only when it ends
4119 * in the position in "nfa_endp".
4120 * Submatches are stored in *m, and used in the parent call.
4121 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004122#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004123 if (nfa_endp != NULL)
4124 {
4125 if (REG_MULTI)
4126 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4127 (int)reglnum,
4128 (int)nfa_endp->se_u.pos.lnum,
4129 (int)(reginput - regline),
4130 nfa_endp->se_u.pos.col);
4131 else
4132 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4133 (int)(reginput - regline),
4134 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004136#endif
4137 /* It's only a match if it ends at "nfa_endp" */
4138 if (nfa_endp != NULL && (REG_MULTI
4139 ? (reglnum != nfa_endp->se_u.pos.lnum
4140 || (int)(reginput - regline)
4141 != nfa_endp->se_u.pos.col)
4142 : reginput != nfa_endp->se_u.ptr))
4143 break;
4144
4145 /* do not set submatches for \@! */
4146 if (!t->state->negated)
4147 {
4148 copy_sub(&m->norm, &t->subs.norm);
4149#ifdef FEAT_SYN_HL
4150 if (nfa_has_zsubexpr)
4151 copy_sub(&m->synt, &t->subs.synt);
4152#endif
4153 }
4154 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004155 break;
4156
4157 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004158 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2d95102013-06-04 14:23:05 +02004159 /* If invisible match has a higher chance to fail, do it
4160 * right away. Otherwise postpone it until what follows is
4161 * matching and causes addstate(nextlist, ..) to be called.
4162 * This is indicated by the "pim" field. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004163 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004164 nfa_pim_T *pim;
4165 int cout = t->state->out1->out->c;
4166
4167 /* Do it directly when what follows is possibly end of
4168 * match (closing paren).
4169 * Postpone when it is \@<= or \@<!, these are expensive.
4170 * TODO: remove the check for t->pim and check multiple
4171 * where it's used?
4172 * Otherwise first do the one that has the highest chance
4173 * of failing. */
4174 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
4175 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
4176 || cout == NFA_NCLOSE
4177 || t->pim != NULL
4178 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4179 && failure_chance(t->state->out1->out, 0)
4180 < failure_chance(t->state->out, 0)))
4181 {
4182 /*
4183 * First try matching the invisible match, then what
4184 * follows.
4185 */
4186 result = recursive_regmatch(t->state, prog,
4187 submatch, m, &listids);
4188
4189 /* for \@! it is a match when result is FALSE */
4190 if (result != t->state->negated)
4191 {
4192 /* Copy submatch info from the recursive call */
4193 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004194#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004195 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004196#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004197
Bram Moolenaara2d95102013-06-04 14:23:05 +02004198 /* t->state->out1 is the corresponding
4199 * END_INVISIBLE node; Add its out to the current
4200 * list (zero-width match). */
4201 addstate_here(thislist, t->state->out1->out,
4202 &t->subs, t->pim, &listidx);
4203 }
4204 }
4205 else
4206 {
4207 /*
4208 * First try matching what follows at the current
4209 * position. Only if a match is found, addstate() is
4210 * called, then verify the invisible match matches.
4211 * Add a nfa_pim_T to the following states, it
4212 * contains info about the invisible match.
4213 */
4214 if (ga_grow(&pimlist, 1) == FAIL)
4215 goto theend;
4216 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4217 ++pimlist.ga_len;
4218 pim->state = t->state;
4219 pim->pim = NULL;
4220 pim->result = NFA_PIM_TODO;
4221
4222 /* t->state->out1 is the corresponding END_INVISIBLE
4223 * node; Add its out to the current list (zero-width
4224 * match). */
4225 addstate_here(thislist, t->state->out1->out, &t->subs,
4226 pim, &listidx);
4227 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004228 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004229 break;
4230
4231 case NFA_BOL:
4232 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004233 addstate_here(thislist, t->state->out, &t->subs,
4234 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235 break;
4236
4237 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004238 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004239 addstate_here(thislist, t->state->out, &t->subs,
4240 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004241 break;
4242
4243 case NFA_BOW:
4244 {
4245 int bow = TRUE;
4246
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004247 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248 bow = FALSE;
4249#ifdef FEAT_MBYTE
4250 else if (has_mbyte)
4251 {
4252 int this_class;
4253
4254 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004255 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 if (this_class <= 1)
4257 bow = FALSE;
4258 else if (reg_prev_class() == this_class)
4259 bow = FALSE;
4260 }
4261#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004262 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004263 || (reginput > regline
4264 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004265 bow = FALSE;
4266 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004267 addstate_here(thislist, t->state->out, &t->subs,
4268 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269 break;
4270 }
4271
4272 case NFA_EOW:
4273 {
4274 int eow = TRUE;
4275
4276 if (reginput == regline)
4277 eow = FALSE;
4278#ifdef FEAT_MBYTE
4279 else if (has_mbyte)
4280 {
4281 int this_class, prev_class;
4282
4283 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004284 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004285 prev_class = reg_prev_class();
4286 if (this_class == prev_class
4287 || prev_class == 0 || prev_class == 1)
4288 eow = FALSE;
4289 }
4290#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004291 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004292 || (reginput[0] != NUL
4293 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294 eow = FALSE;
4295 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004296 addstate_here(thislist, t->state->out, &t->subs,
4297 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298 break;
4299 }
4300
Bram Moolenaar4b780632013-05-31 22:14:52 +02004301 case NFA_BOF:
4302 if (reglnum == 0 && reginput == regline
4303 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004304 addstate_here(thislist, t->state->out, &t->subs,
4305 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004306 break;
4307
4308 case NFA_EOF:
4309 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004310 addstate_here(thislist, t->state->out, &t->subs,
4311 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004312 break;
4313
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004314#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004315 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004316 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004317 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004318 int len = 0;
4319 nfa_state_T *end;
4320 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004321 int cchars[MAX_MCO];
4322 int ccount = 0;
4323 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004324
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004325 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004326 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004327 if (utf_iscomposing(sta->c))
4328 {
4329 /* Only match composing character(s), ignore base
4330 * character. Used for ".{composing}" and "{composing}"
4331 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004332 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004333 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004334 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004335 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004336 /* If \Z was present, then ignore composing characters.
4337 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004338 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004339 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004340 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004341 else
4342 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004343 while (sta->c != NFA_END_COMPOSING)
4344 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004345 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004346
4347 /* Check base character matches first, unless ignored. */
4348 else if (len > 0 || mc == sta->c)
4349 {
4350 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004351 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004352 len += mb_char2len(mc);
4353 sta = sta->out;
4354 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004355
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004356 /* We don't care about the order of composing characters.
4357 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004358 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004359 {
4360 mc = mb_ptr2char(reginput + len);
4361 cchars[ccount++] = mc;
4362 len += mb_char2len(mc);
4363 if (ccount == MAX_MCO)
4364 break;
4365 }
4366
4367 /* Check that each composing char in the pattern matches a
4368 * composing char in the text. We do not check if all
4369 * composing chars are matched. */
4370 result = OK;
4371 while (sta->c != NFA_END_COMPOSING)
4372 {
4373 for (j = 0; j < ccount; ++j)
4374 if (cchars[j] == sta->c)
4375 break;
4376 if (j == ccount)
4377 {
4378 result = FAIL;
4379 break;
4380 }
4381 sta = sta->out;
4382 }
4383 }
4384 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004385 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004386
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004387 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388 ADD_POS_NEG_STATE(end);
4389 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004390 }
4391#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004392
4393 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004394 if (curc == NUL && !reg_line_lbr && REG_MULTI
4395 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004396 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004397 go_to_nextline = TRUE;
4398 /* Pass -1 for the offset, which means taking the position
4399 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004400 ll = nextlist;
4401 add_state = t->state->out;
4402 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004403 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004404 else if (curc == '\n' && reg_line_lbr)
4405 {
4406 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004407 ll = nextlist;
4408 add_state = t->state->out;
4409 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004410 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004411 break;
4412
4413 case NFA_CLASS_ALNUM:
4414 case NFA_CLASS_ALPHA:
4415 case NFA_CLASS_BLANK:
4416 case NFA_CLASS_CNTRL:
4417 case NFA_CLASS_DIGIT:
4418 case NFA_CLASS_GRAPH:
4419 case NFA_CLASS_LOWER:
4420 case NFA_CLASS_PRINT:
4421 case NFA_CLASS_PUNCT:
4422 case NFA_CLASS_SPACE:
4423 case NFA_CLASS_UPPER:
4424 case NFA_CLASS_XDIGIT:
4425 case NFA_CLASS_TAB:
4426 case NFA_CLASS_RETURN:
4427 case NFA_CLASS_BACKSPACE:
4428 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004429 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004430 ADD_POS_NEG_STATE(t->state);
4431 break;
4432
4433 case NFA_END_NEG_RANGE:
4434 /* This follows a series of negated nodes, like:
4435 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004436 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004437 {
4438 ll = nextlist;
4439 add_state = t->state->out;
4440 add_off = clen;
4441 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004442 break;
4443
4444 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004445 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004446 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004447 {
4448 ll = nextlist;
4449 add_state = t->state->out;
4450 add_off = clen;
4451 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004452 break;
4453
4454 /*
4455 * Character classes like \a for alpha, \d for digit etc.
4456 */
4457 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004458 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004459 ADD_POS_NEG_STATE(t->state);
4460 break;
4461
4462 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004463 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004464 ADD_POS_NEG_STATE(t->state);
4465 break;
4466
4467 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004468 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 ADD_POS_NEG_STATE(t->state);
4470 break;
4471
4472 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004473 result = !VIM_ISDIGIT(curc)
4474 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004475 ADD_POS_NEG_STATE(t->state);
4476 break;
4477
4478 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004479 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004480 ADD_POS_NEG_STATE(t->state);
4481 break;
4482
4483 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004484 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004485 ADD_POS_NEG_STATE(t->state);
4486 break;
4487
4488 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004489 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004490 ADD_POS_NEG_STATE(t->state);
4491 break;
4492
4493 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004494 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004495 ADD_POS_NEG_STATE(t->state);
4496 break;
4497
4498 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004499 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004500 ADD_POS_NEG_STATE(t->state);
4501 break;
4502
4503 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004504 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004505 ADD_POS_NEG_STATE(t->state);
4506 break;
4507
4508 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004509 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004510 ADD_POS_NEG_STATE(t->state);
4511 break;
4512
4513 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004514 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004515 ADD_POS_NEG_STATE(t->state);
4516 break;
4517
4518 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004519 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004520 ADD_POS_NEG_STATE(t->state);
4521 break;
4522
4523 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004524 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 ADD_POS_NEG_STATE(t->state);
4526 break;
4527
4528 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004529 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004530 ADD_POS_NEG_STATE(t->state);
4531 break;
4532
4533 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004534 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004535 ADD_POS_NEG_STATE(t->state);
4536 break;
4537
4538 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004539 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004540 ADD_POS_NEG_STATE(t->state);
4541 break;
4542
4543 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004544 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004545 ADD_POS_NEG_STATE(t->state);
4546 break;
4547
4548 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004549 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550 ADD_POS_NEG_STATE(t->state);
4551 break;
4552
4553 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004554 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004555 ADD_POS_NEG_STATE(t->state);
4556 break;
4557
4558 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004559 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 ADD_POS_NEG_STATE(t->state);
4561 break;
4562
4563 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004564 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004565 ADD_POS_NEG_STATE(t->state);
4566 break;
4567
4568 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004569 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004570 ADD_POS_NEG_STATE(t->state);
4571 break;
4572
4573 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004574 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004575 ADD_POS_NEG_STATE(t->state);
4576 break;
4577
4578 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004579 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580 ADD_POS_NEG_STATE(t->state);
4581 break;
4582
4583 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004584 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004585 ADD_POS_NEG_STATE(t->state);
4586 break;
4587
Bram Moolenaar5714b802013-05-28 22:03:20 +02004588 case NFA_BACKREF1:
4589 case NFA_BACKREF2:
4590 case NFA_BACKREF3:
4591 case NFA_BACKREF4:
4592 case NFA_BACKREF5:
4593 case NFA_BACKREF6:
4594 case NFA_BACKREF7:
4595 case NFA_BACKREF8:
4596 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004597#ifdef FEAT_SYN_HL
4598 case NFA_ZREF1:
4599 case NFA_ZREF2:
4600 case NFA_ZREF3:
4601 case NFA_ZREF4:
4602 case NFA_ZREF5:
4603 case NFA_ZREF6:
4604 case NFA_ZREF7:
4605 case NFA_ZREF8:
4606 case NFA_ZREF9:
4607#endif
4608 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004609 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004610 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004611 int bytelen;
4612
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004613 if (t->state->c <= NFA_BACKREF9)
4614 {
4615 subidx = t->state->c - NFA_BACKREF1 + 1;
4616 result = match_backref(&t->subs.norm, subidx, &bytelen);
4617 }
4618#ifdef FEAT_SYN_HL
4619 else
4620 {
4621 subidx = t->state->c - NFA_ZREF1 + 1;
4622 result = match_zref(subidx, &bytelen);
4623 }
4624#endif
4625
Bram Moolenaar5714b802013-05-28 22:03:20 +02004626 if (result)
4627 {
4628 if (bytelen == 0)
4629 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004630 /* empty match always works, output of NFA_SKIP to be
4631 * used next */
4632 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004633 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004634 }
4635 else if (bytelen <= clen)
4636 {
4637 /* match current character, jump ahead to out of
4638 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004639 ll = nextlist;
4640 add_state = t->state->out->out;
4641 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004642#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004643 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004644#endif
4645 }
4646 else
4647 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004648 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004649 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004650 ll = nextlist;
4651 add_state = t->state->out;
4652 add_off = bytelen;
4653 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004654#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004655 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004656#endif
4657 }
4658
4659 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004660 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004661 }
4662 case NFA_SKIP:
4663 /* charater of previous matching \1 .. \9 */
4664 if (t->count - clen <= 0)
4665 {
4666 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004667 ll = nextlist;
4668 add_state = t->state->out;
4669 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004670#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004671 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004672#endif
4673 }
4674 else
4675 {
4676 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004677 ll = nextlist;
4678 add_state = t->state;
4679 add_off = 0;
4680 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004681#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004682 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004683#endif
4684 }
4685 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004686
4687 case NFA_SKIP_CHAR:
4688 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004689 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004690 /* TODO: should not happen? */
4691 break;
4692
Bram Moolenaar423532e2013-05-29 21:14:42 +02004693 case NFA_LNUM:
4694 case NFA_LNUM_GT:
4695 case NFA_LNUM_LT:
4696 result = (REG_MULTI &&
4697 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4698 (long_u)(reglnum + reg_firstlnum)));
4699 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004700 addstate_here(thislist, t->state->out, &t->subs,
4701 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004702 break;
4703
4704 case NFA_COL:
4705 case NFA_COL_GT:
4706 case NFA_COL_LT:
4707 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4708 (long_u)(reginput - regline) + 1);
4709 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004710 addstate_here(thislist, t->state->out, &t->subs,
4711 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004712 break;
4713
4714 case NFA_VCOL:
4715 case NFA_VCOL_GT:
4716 case NFA_VCOL_LT:
4717 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4718 (long_u)win_linetabsize(
4719 reg_win == NULL ? curwin : reg_win,
4720 regline, (colnr_T)(reginput - regline)) + 1);
4721 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004722 addstate_here(thislist, t->state->out, &t->subs,
4723 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004724 break;
4725
4726 case NFA_CURSOR:
4727 result = (reg_win != NULL
4728 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4729 && ((colnr_T)(reginput - regline)
4730 == reg_win->w_cursor.col));
4731 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004732 addstate_here(thislist, t->state->out, &t->subs,
4733 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004734 break;
4735
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004736 case NFA_VISUAL:
4737 result = reg_match_visual();
4738 if (result)
4739 addstate_here(thislist, t->state->out, &t->subs,
4740 t->pim, &listidx);
4741 break;
4742
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004743 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004744 {
4745 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004746
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004747 /* TODO: put this in #ifdef later */
4748 if (c < -256)
4749 EMSGN("INTERNAL: Negative state char: %ld", c);
4750 if (is_Magic(c))
4751 c = un_Magic(c);
4752 result = (c == curc);
4753
4754 if (!result && ireg_ic)
4755 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004756#ifdef FEAT_MBYTE
4757 /* If there is a composing character which is not being
4758 * ignored there can be no match. Match with composing
4759 * character uses NFA_COMPOSING above. */
4760 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004761 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004762 result = FALSE;
4763#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004764 ADD_POS_NEG_STATE(t->state);
4765 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004766 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02004767
4768 } /* switch (t->state->c) */
4769
4770 if (add_state != NULL)
4771 {
4772 if (t->pim != NULL)
4773 {
4774 /* postponed invisible match */
4775 /* TODO: also do t->pim->pim recursively? */
4776 if (t->pim->result == NFA_PIM_TODO)
4777 {
4778#ifdef ENABLE_LOG
4779 fprintf(log_fd, "\n");
4780 fprintf(log_fd, "==================================\n");
4781 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
4782 fprintf(log_fd, "\n");
4783#endif
4784 result = recursive_regmatch(t->pim->state,
4785 prog, submatch, m, &listids);
4786 t->pim->result = result ? NFA_PIM_MATCH
4787 : NFA_PIM_NOMATCH;
4788 /* for \@! it is a match when result is FALSE */
4789 if (result != t->pim->state->negated)
4790 {
4791 /* Copy submatch info from the recursive call */
4792 copy_sub_off(&t->pim->subs.norm, &m->norm);
4793#ifdef FEAT_SYN_HL
4794 copy_sub_off(&t->pim->subs.synt, &m->synt);
4795#endif
4796 }
4797 }
4798 else
4799 {
4800 result = (t->pim->result == NFA_PIM_MATCH);
4801#ifdef ENABLE_LOG
4802 fprintf(log_fd, "\n");
4803 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
4804 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4805 fprintf(log_fd, "\n");
4806#endif
4807 }
4808
4809 /* for \@! it is a match when result is FALSE */
4810 if (result != t->pim->state->negated)
4811 {
4812 /* Copy submatch info from the recursive call */
4813 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
4814#ifdef FEAT_SYN_HL
4815 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
4816#endif
4817 }
4818 else
4819 /* look-behind match failed, don't add the state */
4820 continue;
4821 }
4822
4823 addstate(ll, add_state, &t->subs, add_off);
4824 if (add_count > 0)
4825 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004826 }
4827
4828 } /* for (thislist = thislist; thislist->state; thislist++) */
4829
Bram Moolenaare23febd2013-05-26 18:40:14 +02004830 /* Look for the start of a match in the current position by adding the
4831 * start state to the list of states.
4832 * The first found match is the leftmost one, thus the order of states
4833 * matters!
4834 * Do not add the start state in recursive calls of nfa_regmatch(),
4835 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004836 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004837 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004838 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004839 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004840 && reglnum == 0
4841 && clen != 0
4842 && (ireg_maxcol == 0
4843 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004844 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004845 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004846 ? (reglnum < nfa_endp->se_u.pos.lnum
4847 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004848 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004849 < nfa_endp->se_u.pos.col))
4850 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004851 {
4852#ifdef ENABLE_LOG
4853 fprintf(log_fd, "(---) STARTSTATE\n");
4854#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004855 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004856 }
4857
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858#ifdef ENABLE_LOG
4859 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004860 {
4861 int i;
4862
4863 for (i = 0; i < thislist->n; i++)
4864 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4865 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866 fprintf(log_fd, "\n");
4867#endif
4868
4869nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004870 /* Advance to the next character, or advance to the next line, or
4871 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004872 if (clen != 0)
4873 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004874 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4875 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004876 reg_nextline();
4877 else
4878 break;
4879 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004880
4881#ifdef ENABLE_LOG
4882 if (log_fd != stderr)
4883 fclose(log_fd);
4884 log_fd = NULL;
4885#endif
4886
4887theend:
4888 /* Free memory */
4889 vim_free(list[0].t);
4890 vim_free(list[1].t);
4891 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004892 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004893 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004894#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004895#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004896 fclose(debug);
4897#endif
4898
Bram Moolenaar963fee22013-05-26 21:47:28 +02004899 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004900}
4901
4902/*
4903 * Try match of "prog" with at regline["col"].
4904 * Returns 0 for failure, number of lines contained in the match otherwise.
4905 */
4906 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004907nfa_regtry(prog, col)
4908 nfa_regprog_T *prog;
4909 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004910{
4911 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004912 regsubs_T subs, m;
4913 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004914#ifdef ENABLE_LOG
4915 FILE *f;
4916#endif
4917
4918 reginput = regline + col;
4919 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004920#ifdef FEAT_SYN_HL
4921 /* Clear the external match subpointers if necessary. */
4922 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004923 {
4924 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004925 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004926 }
4927 else
4928 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004929#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004930
4931#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004932 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004933 if (f != NULL)
4934 {
4935 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4936 fprintf(f, " =======================================================\n");
4937#ifdef DEBUG
4938 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4939#endif
4940 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004941 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004942 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004943 fprintf(f, "\n\n");
4944 fclose(f);
4945 }
4946 else
4947 EMSG(_("Could not open temporary log file for writing "));
4948#endif
4949
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004950 clear_sub(&subs.norm);
4951 clear_sub(&m.norm);
4952#ifdef FEAT_SYN_HL
4953 clear_sub(&subs.synt);
4954 clear_sub(&m.synt);
4955#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004956
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004957 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004958 return 0;
4959
4960 cleanup_subexpr();
4961 if (REG_MULTI)
4962 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004963 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004964 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004965 reg_startpos[i] = subs.norm.list.multi[i].start;
4966 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004967 }
4968
4969 if (reg_startpos[0].lnum < 0)
4970 {
4971 reg_startpos[0].lnum = 0;
4972 reg_startpos[0].col = col;
4973 }
4974 if (reg_endpos[0].lnum < 0)
4975 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004976 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004977 reg_endpos[0].lnum = reglnum;
4978 reg_endpos[0].col = (int)(reginput - regline);
4979 }
4980 else
4981 /* Use line number of "\ze". */
4982 reglnum = reg_endpos[0].lnum;
4983 }
4984 else
4985 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004986 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004987 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004988 reg_startp[i] = subs.norm.list.line[i].start;
4989 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004990 }
4991
4992 if (reg_startp[0] == NULL)
4993 reg_startp[0] = regline + col;
4994 if (reg_endp[0] == NULL)
4995 reg_endp[0] = reginput;
4996 }
4997
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004998#ifdef FEAT_SYN_HL
4999 /* Package any found \z(...\) matches for export. Default is none. */
5000 unref_extmatch(re_extmatch_out);
5001 re_extmatch_out = NULL;
5002
5003 if (prog->reghasz == REX_SET)
5004 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005005 cleanup_zsubexpr();
5006 re_extmatch_out = make_extmatch();
5007 for (i = 0; i < subs.synt.in_use; i++)
5008 {
5009 if (REG_MULTI)
5010 {
5011 struct multipos *mpos = &subs.synt.list.multi[i];
5012
5013 /* Only accept single line matches. */
5014 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5015 re_extmatch_out->matches[i] =
5016 vim_strnsave(reg_getline(mpos->start.lnum)
5017 + mpos->start.col,
5018 mpos->end.col - mpos->start.col);
5019 }
5020 else
5021 {
5022 struct linepos *lpos = &subs.synt.list.line[i];
5023
5024 if (lpos->start != NULL && lpos->end != NULL)
5025 re_extmatch_out->matches[i] =
5026 vim_strnsave(lpos->start,
5027 (int)(lpos->end - lpos->start));
5028 }
5029 }
5030 }
5031#endif
5032
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005033 return 1 + reglnum;
5034}
5035
5036/*
5037 * Match a regexp against a string ("line" points to the string) or multiple
5038 * lines ("line" is NULL, use reg_getline()).
5039 *
5040 * Returns 0 for failure, number of lines contained in the match otherwise.
5041 */
5042 static long
5043nfa_regexec_both(line, col)
5044 char_u *line;
5045 colnr_T col; /* column to start looking for match */
5046{
5047 nfa_regprog_T *prog;
5048 long retval = 0L;
5049 int i;
5050
5051 if (REG_MULTI)
5052 {
5053 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5054 line = reg_getline((linenr_T)0); /* relative to the cursor */
5055 reg_startpos = reg_mmatch->startpos;
5056 reg_endpos = reg_mmatch->endpos;
5057 }
5058 else
5059 {
5060 prog = (nfa_regprog_T *)reg_match->regprog;
5061 reg_startp = reg_match->startp;
5062 reg_endp = reg_match->endp;
5063 }
5064
5065 /* Be paranoid... */
5066 if (prog == NULL || line == NULL)
5067 {
5068 EMSG(_(e_null));
5069 goto theend;
5070 }
5071
5072 /* If the start column is past the maximum column: no need to try. */
5073 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5074 goto theend;
5075
5076 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5077 if (prog->regflags & RF_ICASE)
5078 ireg_ic = TRUE;
5079 else if (prog->regflags & RF_NOICASE)
5080 ireg_ic = FALSE;
5081
5082#ifdef FEAT_MBYTE
5083 /* If pattern contains "\Z" overrule value of ireg_icombine */
5084 if (prog->regflags & RF_ICOMBINE)
5085 ireg_icombine = TRUE;
5086#endif
5087
5088 regline = line;
5089 reglnum = 0; /* relative to line */
5090
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005091 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005092 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005093 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005094 nfa_listid = 1;
5095 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005096#ifdef DEBUG
5097 nfa_regengine.expr = prog->pattern;
5098#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005099
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005100 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005101 for (i = 0; i < nstate; ++i)
5102 {
5103 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005104 prog->state[i].lastlist[0] = 0;
5105 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005106 }
5107
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005108 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005109
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005110#ifdef DEBUG
5111 nfa_regengine.expr = NULL;
5112#endif
5113
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005114theend:
5115 return retval;
5116}
5117
5118/*
5119 * Compile a regular expression into internal code for the NFA matcher.
5120 * Returns the program in allocated space. Returns NULL for an error.
5121 */
5122 static regprog_T *
5123nfa_regcomp(expr, re_flags)
5124 char_u *expr;
5125 int re_flags;
5126{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005127 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005128 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005129 int *postfix;
5130
5131 if (expr == NULL)
5132 return NULL;
5133
5134#ifdef DEBUG
5135 nfa_regengine.expr = expr;
5136#endif
5137
5138 init_class_tab();
5139
5140 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5141 return NULL;
5142
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005143 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005144 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005145 postfix = re2post();
5146 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005147 {
5148 /* TODO: only give this error for debugging? */
5149 if (post_ptr >= post_end)
5150 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005151 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005152 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005153
5154 /*
5155 * In order to build the NFA, we parse the input regexp twice:
5156 * 1. first pass to count size (so we can allocate space)
5157 * 2. second to emit code
5158 */
5159#ifdef ENABLE_LOG
5160 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005161 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005162
5163 if (f != NULL)
5164 {
5165 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5166 fclose(f);
5167 }
5168 }
5169#endif
5170
5171 /*
5172 * PASS 1
5173 * Count number of NFA states in "nstate". Do not build the NFA.
5174 */
5175 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005176
5177 /* Space for compiled regexp */
5178 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5179 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5180 if (prog == NULL)
5181 goto fail;
5182 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005183 state_ptr = prog->state;
5184
5185 /*
5186 * PASS 2
5187 * Build the NFA
5188 */
5189 prog->start = post2nfa(postfix, post_ptr, FALSE);
5190 if (prog->start == NULL)
5191 goto fail;
5192
5193 prog->regflags = regflags;
5194 prog->engine = &nfa_regengine;
5195 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005196 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005197 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005198 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005199#ifdef ENABLE_LOG
5200 nfa_postfix_dump(expr, OK);
5201 nfa_dump(prog);
5202#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005203#ifdef FEAT_SYN_HL
5204 /* Remember whether this pattern has any \z specials in it. */
5205 prog->reghasz = re_has_z;
5206#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005207#ifdef DEBUG
5208 prog->pattern = vim_strsave(expr); /* memory will leak */
5209 nfa_regengine.expr = NULL;
5210#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005211
5212out:
5213 vim_free(post_start);
5214 post_start = post_ptr = post_end = NULL;
5215 state_ptr = NULL;
5216 return (regprog_T *)prog;
5217
5218fail:
5219 vim_free(prog);
5220 prog = NULL;
5221#ifdef ENABLE_LOG
5222 nfa_postfix_dump(expr, FAIL);
5223#endif
5224#ifdef DEBUG
5225 nfa_regengine.expr = NULL;
5226#endif
5227 goto out;
5228}
5229
5230
5231/*
5232 * Match a regexp against a string.
5233 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5234 * Uses curbuf for line count and 'iskeyword'.
5235 *
5236 * Return TRUE if there is a match, FALSE if not.
5237 */
5238 static int
5239nfa_regexec(rmp, line, col)
5240 regmatch_T *rmp;
5241 char_u *line; /* string to match against */
5242 colnr_T col; /* column to start looking for match */
5243{
5244 reg_match = rmp;
5245 reg_mmatch = NULL;
5246 reg_maxline = 0;
5247 reg_line_lbr = FALSE;
5248 reg_buf = curbuf;
5249 reg_win = NULL;
5250 ireg_ic = rmp->rm_ic;
5251#ifdef FEAT_MBYTE
5252 ireg_icombine = FALSE;
5253#endif
5254 ireg_maxcol = 0;
5255 return (nfa_regexec_both(line, col) != 0);
5256}
5257
5258#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5259 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5260
5261static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5262
5263/*
5264 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5265 */
5266 static int
5267nfa_regexec_nl(rmp, line, col)
5268 regmatch_T *rmp;
5269 char_u *line; /* string to match against */
5270 colnr_T col; /* column to start looking for match */
5271{
5272 reg_match = rmp;
5273 reg_mmatch = NULL;
5274 reg_maxline = 0;
5275 reg_line_lbr = TRUE;
5276 reg_buf = curbuf;
5277 reg_win = NULL;
5278 ireg_ic = rmp->rm_ic;
5279#ifdef FEAT_MBYTE
5280 ireg_icombine = FALSE;
5281#endif
5282 ireg_maxcol = 0;
5283 return (nfa_regexec_both(line, col) != 0);
5284}
5285#endif
5286
5287
5288/*
5289 * Match a regexp against multiple lines.
5290 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5291 * Uses curbuf for line count and 'iskeyword'.
5292 *
5293 * Return zero if there is no match. Return number of lines contained in the
5294 * match otherwise.
5295 *
5296 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5297 *
5298 * ! Also NOTE : match may actually be in another line. e.g.:
5299 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5300 *
5301 * +-------------------------+
5302 * |a |
5303 * |b |
5304 * |c |
5305 * | |
5306 * +-------------------------+
5307 *
5308 * then nfa_regexec_multi() returns 3. while the original
5309 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5310 *
5311 * FIXME if this behavior is not compatible.
5312 */
5313 static long
5314nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5315 regmmatch_T *rmp;
5316 win_T *win; /* window in which to search or NULL */
5317 buf_T *buf; /* buffer in which to search */
5318 linenr_T lnum; /* nr of line to start looking for match */
5319 colnr_T col; /* column to start looking for match */
5320 proftime_T *tm UNUSED; /* timeout limit or NULL */
5321{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005322 reg_match = NULL;
5323 reg_mmatch = rmp;
5324 reg_buf = buf;
5325 reg_win = win;
5326 reg_firstlnum = lnum;
5327 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5328 reg_line_lbr = FALSE;
5329 ireg_ic = rmp->rmm_ic;
5330#ifdef FEAT_MBYTE
5331 ireg_icombine = FALSE;
5332#endif
5333 ireg_maxcol = rmp->rmm_maxcol;
5334
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005335 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005336}
5337
5338#ifdef DEBUG
5339# undef ENABLE_LOG
5340#endif