blob: 6a80bcc578a4a849f5572be7ecb0d1d6d07793e0 [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
32/* Upper limit allowed for {m,n} repetitions handled by NFA */
33#define NFA_BRACES_MAXLIMIT 10
34/* For allocating space for the postfix representation */
35#define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020036
37enum
38{
39 NFA_SPLIT = -1024,
40 NFA_MATCH,
41 NFA_SKIP_CHAR, /* matches a 0-length char */
42 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
43
44 NFA_CONCAT,
45 NFA_OR,
46 NFA_STAR,
47 NFA_PLUS,
48 NFA_QUEST,
49 NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */
50 NFA_NOT, /* used for [^ab] negated char ranges */
51
52 NFA_BOL, /* ^ Begin line */
53 NFA_EOL, /* $ End line */
54 NFA_BOW, /* \< Begin word */
55 NFA_EOW, /* \> End word */
56 NFA_BOF, /* \%^ Begin file */
57 NFA_EOF, /* \%$ End file */
58 NFA_NEWL,
59 NFA_ZSTART, /* Used for \zs */
60 NFA_ZEND, /* Used for \ze */
61 NFA_NOPEN, /* Start of subexpression marked with \%( */
62 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
63 NFA_START_INVISIBLE,
64 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020065 NFA_COMPOSING, /* Next nodes in NFA are part of the
66 composing multibyte char */
67 NFA_END_COMPOSING, /* End of a composing char in the NFA */
68
69 /* The following are used only in the postfix form, not in the NFA */
70 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
71 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
72 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
73 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
74 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
75
Bram Moolenaar5714b802013-05-28 22:03:20 +020076 NFA_BACKREF1, /* \1 */
77 NFA_BACKREF2, /* \2 */
78 NFA_BACKREF3, /* \3 */
79 NFA_BACKREF4, /* \4 */
80 NFA_BACKREF5, /* \5 */
81 NFA_BACKREF6, /* \6 */
82 NFA_BACKREF7, /* \7 */
83 NFA_BACKREF8, /* \8 */
84 NFA_BACKREF9, /* \9 */
85 NFA_SKIP, /* Skip characters */
86
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020087 NFA_MOPEN,
88 NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
89
90 /* NFA_FIRST_NL */
91 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
92 NFA_ANYOF, /* Match any character in this string. */
93 NFA_ANYBUT, /* Match any character not in this string. */
94 NFA_IDENT, /* Match identifier char */
95 NFA_SIDENT, /* Match identifier char but no digit */
96 NFA_KWORD, /* Match keyword char */
97 NFA_SKWORD, /* Match word char but no digit */
98 NFA_FNAME, /* Match file name char */
99 NFA_SFNAME, /* Match file name char but no digit */
100 NFA_PRINT, /* Match printable char */
101 NFA_SPRINT, /* Match printable char but no digit */
102 NFA_WHITE, /* Match whitespace char */
103 NFA_NWHITE, /* Match non-whitespace char */
104 NFA_DIGIT, /* Match digit char */
105 NFA_NDIGIT, /* Match non-digit char */
106 NFA_HEX, /* Match hex char */
107 NFA_NHEX, /* Match non-hex char */
108 NFA_OCTAL, /* Match octal char */
109 NFA_NOCTAL, /* Match non-octal char */
110 NFA_WORD, /* Match word char */
111 NFA_NWORD, /* Match non-word char */
112 NFA_HEAD, /* Match head char */
113 NFA_NHEAD, /* Match non-head char */
114 NFA_ALPHA, /* Match alpha char */
115 NFA_NALPHA, /* Match non-alpha char */
116 NFA_LOWER, /* Match lowercase char */
117 NFA_NLOWER, /* Match non-lowercase char */
118 NFA_UPPER, /* Match uppercase char */
119 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200120
121 NFA_CURSOR, /* Match cursor pos */
122 NFA_LNUM, /* Match line number */
123 NFA_LNUM_GT, /* Match > line number */
124 NFA_LNUM_LT, /* Match < line number */
125 NFA_COL, /* Match cursor column */
126 NFA_COL_GT, /* Match > cursor column */
127 NFA_COL_LT, /* Match < cursor column */
128 NFA_VCOL, /* Match cursor virtual column */
129 NFA_VCOL_GT, /* Match > cursor virtual column */
130 NFA_VCOL_LT, /* Match < cursor virtual column */
131
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200132 NFA_FIRST_NL = NFA_ANY + ADD_NL,
133 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
134
135 /* Character classes [:alnum:] etc */
136 NFA_CLASS_ALNUM,
137 NFA_CLASS_ALPHA,
138 NFA_CLASS_BLANK,
139 NFA_CLASS_CNTRL,
140 NFA_CLASS_DIGIT,
141 NFA_CLASS_GRAPH,
142 NFA_CLASS_LOWER,
143 NFA_CLASS_PRINT,
144 NFA_CLASS_PUNCT,
145 NFA_CLASS_SPACE,
146 NFA_CLASS_UPPER,
147 NFA_CLASS_XDIGIT,
148 NFA_CLASS_TAB,
149 NFA_CLASS_RETURN,
150 NFA_CLASS_BACKSPACE,
151 NFA_CLASS_ESCAPE
152};
153
154/* Keep in sync with classchars. */
155static int nfa_classcodes[] = {
156 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
157 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
158 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
159 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
160 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
161 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
162 NFA_UPPER, NFA_NUPPER
163};
164
165static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
166
167/*
168 * NFA errors can be of 3 types:
169 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
170 * silently and revert the to backtracking engine.
171 * syntax_error = FALSE;
172 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
173 * The NFA engine displays an error message, and nothing else happens.
174 * syntax_error = TRUE
175 * *** Unsupported features, when the input regexp uses an operator that is not
176 * implemented in the NFA. The NFA engine fails silently, and reverts to the
177 * old backtracking engine.
178 * syntax_error = FALSE
179 * "The NFA fails" means that "compiling the regexp with the NFA fails":
180 * nfa_regcomp() returns FAIL.
181 */
182static int syntax_error = FALSE;
183
184/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200185static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200186
Bram Moolenaar428e9872013-05-30 17:05:39 +0200187/* NFA regexp \1 .. \9 encountered. */
188static int nfa_has_backref;
189
Bram Moolenaar963fee22013-05-26 21:47:28 +0200190/* Number of sub expressions actually being used during execution. 1 if only
191 * the whole match (subexpr 0) is used. */
192static int nfa_nsubexpr;
193
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200194static int *post_start; /* holds the postfix form of r.e. */
195static int *post_end;
196static int *post_ptr;
197
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200198static int nstate; /* Number of states in the NFA. Also used when
199 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200200static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200201
202
203static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
204static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
205static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200206static int nfa_regatom __ARGS((void));
207static int nfa_regpiece __ARGS((void));
208static int nfa_regconcat __ARGS((void));
209static int nfa_regbranch __ARGS((void));
210static int nfa_reg __ARGS((int paren));
211#ifdef DEBUG
212static void nfa_set_code __ARGS((int c));
213static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200214static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
215static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200216static void nfa_dump __ARGS((nfa_regprog_T *prog));
217#endif
218static int *re2post __ARGS((void));
219static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
220static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
221static int check_char_class __ARGS((int class, int c));
222static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200223static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
224static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200225static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
226static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200227static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200228static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
229static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
230static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
231static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
232static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
233
234/* helper functions used when doing re2post() ... regatom() parsing */
235#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200236 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237 return FAIL; \
238 *post_ptr++ = c; \
239 } while (0)
240
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200241/*
242 * Initialize internal variables before NFA compilation.
243 * Return OK on success, FAIL otherwise.
244 */
245 static int
246nfa_regcomp_start(expr, re_flags)
247 char_u *expr;
248 int re_flags; /* see vim_regcomp() */
249{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200250 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200251 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
253 nstate = 0;
254 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200255 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200256 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200258 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200259 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200260 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200261
262 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200263 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265 post_start = (int *)lalloc(postfix_size, TRUE);
266 if (post_start == NULL)
267 return FAIL;
268 vim_memset(post_start, 0, postfix_size);
269 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200270 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200272 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273
274 regcomp_start(expr, re_flags);
275
276 return OK;
277}
278
279/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200280 * Allocate more space for post_start. Called when
281 * running above the estimated number of states.
282 */
283 static int
284realloc_post_list()
285{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200286 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200287 int new_max = nstate_max + 1000;
288 int *new_start;
289 int *old_start;
290
291 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
292 if (new_start == NULL)
293 return FAIL;
294 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
295 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
296 old_start = post_start;
297 post_start = new_start;
298 post_ptr = new_start + (post_ptr - old_start);
299 post_end = post_start + new_max;
300 vim_free(old_start);
301 return OK;
302}
303
304/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305 * Search between "start" and "end" and try to recognize a
306 * character class in expanded form. For example [0-9].
307 * On success, return the id the character class to be emitted.
308 * On failure, return 0 (=FAIL)
309 * Start points to the first char of the range, while end should point
310 * to the closing brace.
311 */
312 static int
313nfa_recognize_char_class(start, end, extra_newl)
314 char_u *start;
315 char_u *end;
316 int extra_newl;
317{
318 int i;
319 /* Each of these variables takes up a char in "config[]",
320 * in the order they are here. */
321 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
322 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
323 char_u *p;
324#define NCONFIGS 16
325 int classid[NCONFIGS] = {
326 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
327 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
328 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
329 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
330 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200331 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200332 char_u config[NCONFIGS][9] = {
333 "000000100", /* digit */
334 "100000100", /* non digit */
335 "011000100", /* hex-digit */
336 "111000100", /* non hex-digit */
337 "000001000", /* octal-digit */
338 "100001000", /* [^0-7] */
339 "000110110", /* [0-9A-Za-z_] */
340 "100110110", /* [^0-9A-Za-z_] */
341 "000110010", /* head of word */
342 "100110010", /* not head of word */
343 "000110000", /* alphabetic char a-z */
344 "100110000", /* non alphabetic char */
345 "000100000", /* lowercase letter */
346 "100100000", /* non lowercase */
347 "000010000", /* uppercase */
348 "100010000" /* non uppercase */
349 };
350
351 if (extra_newl == TRUE)
352 newl = TRUE;
353
354 if (*end != ']')
355 return FAIL;
356 p = start;
357 if (*p == '^')
358 {
359 not = TRUE;
360 p ++;
361 }
362
363 while (p < end)
364 {
365 if (p + 2 < end && *(p + 1) == '-')
366 {
367 switch (*p)
368 {
369 case '0':
370 if (*(p + 2) == '9')
371 {
372 o9 = TRUE;
373 break;
374 }
375 else
376 if (*(p + 2) == '7')
377 {
378 o7 = TRUE;
379 break;
380 }
381 case 'a':
382 if (*(p + 2) == 'z')
383 {
384 az = TRUE;
385 break;
386 }
387 else
388 if (*(p + 2) == 'f')
389 {
390 af = TRUE;
391 break;
392 }
393 case 'A':
394 if (*(p + 2) == 'Z')
395 {
396 AZ = TRUE;
397 break;
398 }
399 else
400 if (*(p + 2) == 'F')
401 {
402 AF = TRUE;
403 break;
404 }
405 /* FALLTHROUGH */
406 default:
407 return FAIL;
408 }
409 p += 3;
410 }
411 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
412 {
413 newl = TRUE;
414 p += 2;
415 }
416 else if (*p == '_')
417 {
418 underscore = TRUE;
419 p ++;
420 }
421 else if (*p == '\n')
422 {
423 newl = TRUE;
424 p ++;
425 }
426 else
427 return FAIL;
428 } /* while (p < end) */
429
430 if (p != end)
431 return FAIL;
432
433 /* build the config that represents the ranges we gathered */
434 STRCPY(myconfig, "000000000");
435 if (not == TRUE)
436 myconfig[0] = '1';
437 if (af == TRUE)
438 myconfig[1] = '1';
439 if (AF == TRUE)
440 myconfig[2] = '1';
441 if (az == TRUE)
442 myconfig[3] = '1';
443 if (AZ == TRUE)
444 myconfig[4] = '1';
445 if (o7 == TRUE)
446 myconfig[5] = '1';
447 if (o9 == TRUE)
448 myconfig[6] = '1';
449 if (underscore == TRUE)
450 myconfig[7] = '1';
451 if (newl == TRUE)
452 {
453 myconfig[8] = '1';
454 extra_newl = ADD_NL;
455 }
456 /* try to recognize character classes */
457 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200458 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200459 return classid[i] + extra_newl;
460
461 /* fallthrough => no success so far */
462 return FAIL;
463
464#undef NCONFIGS
465}
466
467/*
468 * Produce the bytes for equivalence class "c".
469 * Currently only handles latin1, latin9 and utf-8.
470 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
471 * equivalent to 'a OR b OR c'
472 *
473 * NOTE! When changing this function, also update reg_equi_class()
474 */
475 static int
476nfa_emit_equi_class(c, neg)
477 int c;
478 int neg;
479{
480 int first = TRUE;
481 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
482#define EMIT2(c) \
483 EMIT(c); \
484 if (neg == TRUE) { \
485 EMIT(NFA_NOT); \
486 } \
487 if (first == FALSE) \
488 EMIT(glue); \
489 else \
490 first = FALSE; \
491
492#ifdef FEAT_MBYTE
493 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
494 || STRCMP(p_enc, "iso-8859-15") == 0)
495#endif
496 {
497 switch (c)
498 {
499 case 'A': case '\300': case '\301': case '\302':
500 case '\303': case '\304': case '\305':
501 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
502 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
503 EMIT2('\305');
504 return OK;
505
506 case 'C': case '\307':
507 EMIT2('C'); EMIT2('\307');
508 return OK;
509
510 case 'E': case '\310': case '\311': case '\312': case '\313':
511 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
512 EMIT2('\312'); EMIT2('\313');
513 return OK;
514
515 case 'I': case '\314': case '\315': case '\316': case '\317':
516 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
517 EMIT2('\316'); EMIT2('\317');
518 return OK;
519
520 case 'N': case '\321':
521 EMIT2('N'); EMIT2('\321');
522 return OK;
523
524 case 'O': case '\322': case '\323': case '\324': case '\325':
525 case '\326':
526 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
527 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
528 return OK;
529
530 case 'U': case '\331': case '\332': case '\333': case '\334':
531 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
532 EMIT2('\333'); EMIT2('\334');
533 return OK;
534
535 case 'Y': case '\335':
536 EMIT2('Y'); EMIT2('\335');
537 return OK;
538
539 case 'a': case '\340': case '\341': case '\342':
540 case '\343': case '\344': case '\345':
541 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
542 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
543 EMIT2('\345');
544 return OK;
545
546 case 'c': case '\347':
547 EMIT2('c'); EMIT2('\347');
548 return OK;
549
550 case 'e': case '\350': case '\351': case '\352': case '\353':
551 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
552 EMIT2('\352'); EMIT2('\353');
553 return OK;
554
555 case 'i': case '\354': case '\355': case '\356': case '\357':
556 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
557 EMIT2('\356'); EMIT2('\357');
558 return OK;
559
560 case 'n': case '\361':
561 EMIT2('n'); EMIT2('\361');
562 return OK;
563
564 case 'o': case '\362': case '\363': case '\364': case '\365':
565 case '\366':
566 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
567 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
568 return OK;
569
570 case 'u': case '\371': case '\372': case '\373': case '\374':
571 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
572 EMIT2('\373'); EMIT2('\374');
573 return OK;
574
575 case 'y': case '\375': case '\377':
576 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
577 return OK;
578
579 default:
580 return FAIL;
581 }
582 }
583
584 EMIT(c);
585 return OK;
586#undef EMIT2
587}
588
589/*
590 * Code to parse regular expression.
591 *
592 * We try to reuse parsing functions in regexp.c to
593 * minimize surprise and keep the syntax consistent.
594 */
595
596/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 * Parse the lowest level.
598 *
599 * An atom can be one of a long list of items. Many atoms match one character
600 * in the text. It is often an ordinary character or a character class.
601 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
602 * is only for syntax highlighting.
603 *
604 * atom ::= ordinary-atom
605 * or \( pattern \)
606 * or \%( pattern \)
607 * or \z( pattern \)
608 */
609 static int
610nfa_regatom()
611{
612 int c;
613 int charclass;
614 int equiclass;
615 int collclass;
616 int got_coll_char;
617 char_u *p;
618 char_u *endp;
619#ifdef FEAT_MBYTE
620 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200621#endif
622 int extra = 0;
623 int first;
624 int emit_range;
625 int negated;
626 int result;
627 int startc = -1;
628 int endc = -1;
629 int oldstartc = -1;
630 int cpo_lit; /* 'cpoptions' contains 'l' flag */
631 int cpo_bsl; /* 'cpoptions' contains '\' flag */
632 int glue; /* ID that will "glue" nodes together */
633
634 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
635 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
636
637 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 switch (c)
639 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200640 case NUL:
641 syntax_error = TRUE;
642 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
643
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 case Magic('^'):
645 EMIT(NFA_BOL);
646 break;
647
648 case Magic('$'):
649 EMIT(NFA_EOL);
650#if defined(FEAT_SYN_HL) || defined(PROTO)
651 had_eol = TRUE;
652#endif
653 break;
654
655 case Magic('<'):
656 EMIT(NFA_BOW);
657 break;
658
659 case Magic('>'):
660 EMIT(NFA_EOW);
661 break;
662
663 case Magic('_'):
664 c = no_Magic(getchr());
665 if (c == '^') /* "\_^" is start-of-line */
666 {
667 EMIT(NFA_BOL);
668 break;
669 }
670 if (c == '$') /* "\_$" is end-of-line */
671 {
672 EMIT(NFA_EOL);
673#if defined(FEAT_SYN_HL) || defined(PROTO)
674 had_eol = TRUE;
675#endif
676 break;
677 }
678
679 extra = ADD_NL;
680
681 /* "\_[" is collection plus newline */
682 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200683 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200684
685 /* "\_x" is character class plus newline */
686 /*FALLTHROUGH*/
687
688 /*
689 * Character classes.
690 */
691 case Magic('.'):
692 case Magic('i'):
693 case Magic('I'):
694 case Magic('k'):
695 case Magic('K'):
696 case Magic('f'):
697 case Magic('F'):
698 case Magic('p'):
699 case Magic('P'):
700 case Magic('s'):
701 case Magic('S'):
702 case Magic('d'):
703 case Magic('D'):
704 case Magic('x'):
705 case Magic('X'):
706 case Magic('o'):
707 case Magic('O'):
708 case Magic('w'):
709 case Magic('W'):
710 case Magic('h'):
711 case Magic('H'):
712 case Magic('a'):
713 case Magic('A'):
714 case Magic('l'):
715 case Magic('L'):
716 case Magic('u'):
717 case Magic('U'):
718 p = vim_strchr(classchars, no_Magic(c));
719 if (p == NULL)
720 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200721 EMSGN("INTERNAL: Unknown character class char: %ld", c);
722 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200723 }
724#ifdef FEAT_MBYTE
725 /* When '.' is followed by a composing char ignore the dot, so that
726 * the composing char is matched here. */
727 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
728 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200729 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730 c = getchr();
731 goto nfa_do_multibyte;
732 }
733#endif
734 EMIT(nfa_classcodes[p - classchars]);
735 if (extra == ADD_NL)
736 {
737 EMIT(NFA_NEWL);
738 EMIT(NFA_OR);
739 regflags |= RF_HASNL;
740 }
741 break;
742
743 case Magic('n'):
744 if (reg_string)
745 /* In a string "\n" matches a newline character. */
746 EMIT(NL);
747 else
748 {
749 /* In buffer text "\n" matches the end of a line. */
750 EMIT(NFA_NEWL);
751 regflags |= RF_HASNL;
752 }
753 break;
754
755 case Magic('('):
756 if (nfa_reg(REG_PAREN) == FAIL)
757 return FAIL; /* cascaded error */
758 break;
759
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200760 case Magic('|'):
761 case Magic('&'):
762 case Magic(')'):
763 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200764 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200765 return FAIL;
766
767 case Magic('='):
768 case Magic('?'):
769 case Magic('+'):
770 case Magic('@'):
771 case Magic('*'):
772 case Magic('{'):
773 /* these should follow an atom, not form an atom */
774 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200775 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 return FAIL;
777
778 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200779 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200780 return FAIL;
781
Bram Moolenaar428e9872013-05-30 17:05:39 +0200782 case Magic('1'):
783 case Magic('2'):
784 case Magic('3'):
785 case Magic('4'):
786 case Magic('5'):
787 case Magic('6'):
788 case Magic('7'):
789 case Magic('8'):
790 case Magic('9'):
791 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
792 nfa_has_backref = TRUE;
793 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200794
795 case Magic('z'):
796 c = no_Magic(getchr());
797 switch (c)
798 {
799 case 's':
800 EMIT(NFA_ZSTART);
801 break;
802 case 'e':
803 EMIT(NFA_ZEND);
804 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200805 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200806 case '1':
807 case '2':
808 case '3':
809 case '4':
810 case '5':
811 case '6':
812 case '7':
813 case '8':
814 case '9':
815 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200816 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 return FAIL;
818 default:
819 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200820 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 no_Magic(c));
822 return FAIL;
823 }
824 break;
825
826 case Magic('%'):
827 c = no_Magic(getchr());
828 switch (c)
829 {
830 /* () without a back reference */
831 case '(':
832 if (nfa_reg(REG_NPAREN) == FAIL)
833 return FAIL;
834 EMIT(NFA_NOPEN);
835 break;
836
837 case 'd': /* %d123 decimal */
838 case 'o': /* %o123 octal */
839 case 'x': /* %xab hex 2 */
840 case 'u': /* %uabcd hex 4 */
841 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200842 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200843 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200844
Bram Moolenaar47196582013-05-25 22:04:23 +0200845 switch (c)
846 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200847 case 'd': nr = getdecchrs(); break;
848 case 'o': nr = getoctchrs(); break;
849 case 'x': nr = gethexchrs(2); break;
850 case 'u': nr = gethexchrs(4); break;
851 case 'U': nr = gethexchrs(8); break;
852 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200853 }
854
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200855 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200856 EMSG2_RET_FAIL(
857 _("E678: Invalid character after %s%%[dxouU]"),
858 reg_magic == MAGIC_ALL);
859 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200860 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200861 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200862 break;
863
864 /* Catch \%^ and \%$ regardless of where they appear in the
865 * pattern -- regardless of whether or not it makes sense. */
866 case '^':
867 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 break;
869
870 case '$':
871 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200872 break;
873
874 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200875 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200876 break;
877
878 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200879 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 return FAIL;
881 break;
882
883 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200884 /* TODO: \%[abc] not supported yet */
885 return FAIL;
886
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200887 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200888 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200889 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200890 int cmp = c;
891
892 if (c == '<' || c == '>')
893 c = getchr();
894 while (VIM_ISDIGIT(c))
895 {
896 n = n * 10 + (c - '0');
897 c = getchr();
898 }
899 if (c == 'l' || c == 'c' || c == 'v')
900 {
901 EMIT(n);
902 if (c == 'l')
903 EMIT(cmp == '<' ? NFA_LNUM_LT :
904 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
905 else if (c == 'c')
906 EMIT(cmp == '<' ? NFA_COL_LT :
907 cmp == '>' ? NFA_COL_GT : NFA_COL);
908 else
909 EMIT(cmp == '<' ? NFA_VCOL_LT :
910 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
911 break;
912 }
913 else if (c == '\'')
914 /* TODO: \%'m not supported yet */
915 return FAIL;
916 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200917 syntax_error = TRUE;
918 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
919 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200920 return FAIL;
921 }
922 break;
923
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200924 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200925collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200926 /*
927 * Glue is emitted between several atoms from the [].
928 * It is either NFA_OR, or NFA_CONCAT.
929 *
930 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
931 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
932 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
933 * notation)
934 *
935 */
936
937
938/* Emit negation atoms, if needed.
939 * The CONCAT below merges the NOT with the previous node. */
940#define TRY_NEG() \
941 if (negated == TRUE) \
942 { \
943 EMIT(NFA_NOT); \
944 }
945
946/* Emit glue between important nodes : CONCAT or OR. */
947#define EMIT_GLUE() \
948 if (first == FALSE) \
949 EMIT(glue); \
950 else \
951 first = FALSE;
952
953 p = regparse;
954 endp = skip_anyof(p);
955 if (*endp == ']')
956 {
957 /*
958 * Try to reverse engineer character classes. For example,
959 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
960 * and perform the necessary substitutions in the NFA.
961 */
962 result = nfa_recognize_char_class(regparse, endp,
963 extra == ADD_NL);
964 if (result != FAIL)
965 {
966 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
967 EMIT(result);
968 else /* must be char class + newline */
969 {
970 EMIT(result - ADD_NL);
971 EMIT(NFA_NEWL);
972 EMIT(NFA_OR);
973 }
974 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200975 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200976 return OK;
977 }
978 /*
979 * Failed to recognize a character class. Use the simple
980 * version that turns [abc] into 'a' OR 'b' OR 'c'
981 */
982 startc = endc = oldstartc = -1;
983 first = TRUE; /* Emitting first atom in this sequence? */
984 negated = FALSE;
985 glue = NFA_OR;
986 if (*regparse == '^') /* negated range */
987 {
988 negated = TRUE;
989 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200990 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200991 }
992 if (*regparse == '-')
993 {
994 startc = '-';
995 EMIT(startc);
996 TRY_NEG();
997 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +0200998 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200999 }
1000 /* Emit the OR branches for each character in the [] */
1001 emit_range = FALSE;
1002 while (regparse < endp)
1003 {
1004 oldstartc = startc;
1005 startc = -1;
1006 got_coll_char = FALSE;
1007 if (*regparse == '[')
1008 {
1009 /* Check for [: :], [= =], [. .] */
1010 equiclass = collclass = 0;
1011 charclass = get_char_class(&regparse);
1012 if (charclass == CLASS_NONE)
1013 {
1014 equiclass = get_equi_class(&regparse);
1015 if (equiclass == 0)
1016 collclass = get_coll_element(&regparse);
1017 }
1018
1019 /* Character class like [:alpha:] */
1020 if (charclass != CLASS_NONE)
1021 {
1022 switch (charclass)
1023 {
1024 case CLASS_ALNUM:
1025 EMIT(NFA_CLASS_ALNUM);
1026 break;
1027 case CLASS_ALPHA:
1028 EMIT(NFA_CLASS_ALPHA);
1029 break;
1030 case CLASS_BLANK:
1031 EMIT(NFA_CLASS_BLANK);
1032 break;
1033 case CLASS_CNTRL:
1034 EMIT(NFA_CLASS_CNTRL);
1035 break;
1036 case CLASS_DIGIT:
1037 EMIT(NFA_CLASS_DIGIT);
1038 break;
1039 case CLASS_GRAPH:
1040 EMIT(NFA_CLASS_GRAPH);
1041 break;
1042 case CLASS_LOWER:
1043 EMIT(NFA_CLASS_LOWER);
1044 break;
1045 case CLASS_PRINT:
1046 EMIT(NFA_CLASS_PRINT);
1047 break;
1048 case CLASS_PUNCT:
1049 EMIT(NFA_CLASS_PUNCT);
1050 break;
1051 case CLASS_SPACE:
1052 EMIT(NFA_CLASS_SPACE);
1053 break;
1054 case CLASS_UPPER:
1055 EMIT(NFA_CLASS_UPPER);
1056 break;
1057 case CLASS_XDIGIT:
1058 EMIT(NFA_CLASS_XDIGIT);
1059 break;
1060 case CLASS_TAB:
1061 EMIT(NFA_CLASS_TAB);
1062 break;
1063 case CLASS_RETURN:
1064 EMIT(NFA_CLASS_RETURN);
1065 break;
1066 case CLASS_BACKSPACE:
1067 EMIT(NFA_CLASS_BACKSPACE);
1068 break;
1069 case CLASS_ESCAPE:
1070 EMIT(NFA_CLASS_ESCAPE);
1071 break;
1072 }
1073 TRY_NEG();
1074 EMIT_GLUE();
1075 continue;
1076 }
1077 /* Try equivalence class [=a=] and the like */
1078 if (equiclass != 0)
1079 {
1080 result = nfa_emit_equi_class(equiclass, negated);
1081 if (result == FAIL)
1082 {
1083 /* should never happen */
1084 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1085 }
1086 EMIT_GLUE();
1087 continue;
1088 }
1089 /* Try collating class like [. .] */
1090 if (collclass != 0)
1091 {
1092 startc = collclass; /* allow [.a.]-x as a range */
1093 /* Will emit the proper atom at the end of the
1094 * while loop. */
1095 }
1096 }
1097 /* Try a range like 'a-x' or '\t-z' */
1098 if (*regparse == '-')
1099 {
1100 emit_range = TRUE;
1101 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001102 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001103 continue; /* reading the end of the range */
1104 }
1105
1106 /* Now handle simple and escaped characters.
1107 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1108 * accepts "\t", "\e", etc., but only when the 'l' flag in
1109 * 'cpoptions' is not included.
1110 * Posix doesn't recognize backslash at all.
1111 */
1112 if (*regparse == '\\'
1113 && !cpo_bsl
1114 && regparse + 1 <= endp
1115 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1116 || (!cpo_lit
1117 && vim_strchr(REGEXP_ABBR, regparse[1])
1118 != NULL)
1119 )
1120 )
1121 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001122 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001123
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001124 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001125 startc = reg_string ? NL : NFA_NEWL;
1126 else
1127 if (*regparse == 'd'
1128 || *regparse == 'o'
1129 || *regparse == 'x'
1130 || *regparse == 'u'
1131 || *regparse == 'U'
1132 )
1133 {
1134 /* TODO(RE) This needs more testing */
1135 startc = coll_get_char();
1136 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001137 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001138 }
1139 else
1140 {
1141 /* \r,\t,\e,\b */
1142 startc = backslash_trans(*regparse);
1143 }
1144 }
1145
1146 /* Normal printable char */
1147 if (startc == -1)
1148#ifdef FEAT_MBYTE
1149 startc = (*mb_ptr2char)(regparse);
1150#else
1151 startc = *regparse;
1152#endif
1153
1154 /* Previous char was '-', so this char is end of range. */
1155 if (emit_range)
1156 {
1157 endc = startc; startc = oldstartc;
1158 if (startc > endc)
1159 EMSG_RET_FAIL(_(e_invrange));
1160#ifdef FEAT_MBYTE
1161 if (has_mbyte && ((*mb_char2len)(startc) > 1
1162 || (*mb_char2len)(endc) > 1))
1163 {
1164 if (endc > startc + 256)
1165 EMSG_RET_FAIL(_(e_invrange));
1166 /* Emit the range. "startc" was already emitted, so
1167 * skip it. */
1168 for (c = startc + 1; c <= endc; c++)
1169 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001170 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001171 TRY_NEG();
1172 EMIT_GLUE();
1173 }
1174 emit_range = FALSE;
1175 }
1176 else
1177#endif
1178 {
1179#ifdef EBCDIC
1180 int alpha_only = FALSE;
1181
1182 /* for alphabetical range skip the gaps
1183 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1184 if (isalpha(startc) && isalpha(endc))
1185 alpha_only = TRUE;
1186#endif
1187 /* Emit the range. "startc" was already emitted, so
1188 * skip it. */
1189 for (c = startc + 1; c <= endc; c++)
1190#ifdef EBCDIC
1191 if (!alpha_only || isalpha(startc))
1192#endif
1193 {
1194 EMIT(c);
1195 TRY_NEG();
1196 EMIT_GLUE();
1197 }
1198 emit_range = FALSE;
1199 }
1200 }
1201 else
1202 {
1203 /*
1204 * This char (startc) is not part of a range. Just
1205 * emit it.
1206 *
1207 * Normally, simply emit startc. But if we get char
1208 * code=0 from a collating char, then replace it with
1209 * 0x0a.
1210 *
1211 * This is needed to completely mimic the behaviour of
1212 * the backtracking engine.
1213 */
1214 if (got_coll_char == TRUE && startc == 0)
1215 EMIT(0x0a);
1216 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001217 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001218 TRY_NEG();
1219 EMIT_GLUE();
1220 }
1221
Bram Moolenaar51a29832013-05-28 22:30:35 +02001222 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 } /* while (p < endp) */
1224
Bram Moolenaar51a29832013-05-28 22:30:35 +02001225 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226 if (*regparse == '-') /* if last, '-' is just a char */
1227 {
1228 EMIT('-');
1229 TRY_NEG();
1230 EMIT_GLUE();
1231 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001232 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001233
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 /* skip the trailing ] */
1235 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001236 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001237 if (negated == TRUE)
1238 {
1239 /* Mark end of negated char range */
1240 EMIT(NFA_END_NEG_RANGE);
1241 EMIT(NFA_CONCAT);
1242 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001243
1244 /* \_[] also matches \n but it's not negated */
1245 if (extra == ADD_NL)
1246 {
1247 EMIT(reg_string ? NL : NFA_NEWL);
1248 EMIT(NFA_OR);
1249 }
1250
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001252 } /* if exists closing ] */
1253
1254 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 {
1256 syntax_error = TRUE;
1257 EMSG_RET_FAIL(_(e_missingbracket));
1258 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001259 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001261 default:
1262 {
1263#ifdef FEAT_MBYTE
1264 int plen;
1265
1266nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001267 /* plen is length of current char with composing chars */
1268 if (enc_utf8 && ((*mb_char2len)(c)
1269 != (plen = (*mb_ptr2len)(old_regparse))
1270 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001272 int i = 0;
1273
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001274 /* A base character plus composing characters, or just one
1275 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001276 * This requires creating a separate atom as if enclosing
1277 * the characters in (), where NFA_COMPOSING is the ( and
1278 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001279 * building the postfix form, not the NFA itself;
1280 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001281 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001282 for (;;)
1283 {
1284 EMIT(c);
1285 if (i > 0)
1286 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001287 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001288 break;
1289 c = utf_ptr2char(old_regparse + i);
1290 }
1291 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001292 regparse = old_regparse + plen;
1293 }
1294 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001295#endif
1296 {
1297 c = no_Magic(c);
1298 EMIT(c);
1299 }
1300 return OK;
1301 }
1302 }
1303
1304#undef TRY_NEG
1305#undef EMIT_GLUE
1306
1307 return OK;
1308}
1309
1310/*
1311 * Parse something followed by possible [*+=].
1312 *
1313 * A piece is an atom, possibly followed by a multi, an indication of how many
1314 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1315 * characters: "", "a", "aa", etc.
1316 *
1317 * piece ::= atom
1318 * or atom multi
1319 */
1320 static int
1321nfa_regpiece()
1322{
1323 int i;
1324 int op;
1325 int ret;
1326 long minval, maxval;
1327 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1328 char_u *old_regparse, *new_regparse;
1329 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001330 int old_post_pos;
1331 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001332 int old_regnpar;
1333 int quest;
1334
1335 /* Save the current position in the regexp, so that we can use it if
1336 * <atom>{m,n} is next. */
1337 old_regparse = regparse;
1338 /* Save current number of open parenthesis, so we can use it if
1339 * <atom>{m,n} is next */
1340 old_regnpar = regnpar;
1341 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001342 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343
1344 ret = nfa_regatom();
1345 if (ret == FAIL)
1346 return FAIL; /* cascaded error */
1347
1348 op = peekchr();
1349 if (re_multi_type(op) == NOT_MULTI)
1350 return OK;
1351
1352 skipchr();
1353 switch (op)
1354 {
1355 case Magic('*'):
1356 EMIT(NFA_STAR);
1357 break;
1358
1359 case Magic('+'):
1360 /*
1361 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1362 * first and only submatch would be "aaa". But the backtracking
1363 * engine interprets the plus as "try matching one more time", and
1364 * a* matches a second time at the end of the input, the empty
1365 * string.
1366 * The submatch will the empty string.
1367 *
1368 * In order to be consistent with the old engine, we disable
1369 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1370 */
1371 /* EMIT(NFA_PLUS); */
1372 regnpar = old_regnpar;
1373 regparse = old_regparse;
1374 curchr = -1;
1375 if (nfa_regatom() == FAIL)
1376 return FAIL;
1377 EMIT(NFA_STAR);
1378 EMIT(NFA_CONCAT);
1379 skipchr(); /* skip the \+ */
1380 break;
1381
1382 case Magic('@'):
1383 op = no_Magic(getchr());
1384 switch(op)
1385 {
1386 case '=':
1387 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1388 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001389 case '!':
1390 EMIT(NFA_PREV_ATOM_NO_WIDTH_NEG);
1391 break;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001392 case '0':
1393 case '1':
1394 case '2':
1395 case '3':
1396 case '4':
1397 case '5':
1398 case '6':
1399 case '7':
1400 case '8':
1401 case '9':
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001402 case '<':
1403 case '>':
1404 /* Not supported yet */
1405 return FAIL;
1406 default:
1407 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001408 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001409 return FAIL;
1410 }
1411 break;
1412
1413 case Magic('?'):
1414 case Magic('='):
1415 EMIT(NFA_QUEST);
1416 break;
1417
1418 case Magic('{'):
1419 /* a{2,5} will expand to 'aaa?a?a?'
1420 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1421 * version of '?'
1422 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1423 * parenthesis have the same id
1424 */
1425
1426 greedy = TRUE;
1427 c2 = peekchr();
1428 if (c2 == '-' || c2 == Magic('-'))
1429 {
1430 skipchr();
1431 greedy = FALSE;
1432 }
1433 if (!read_limits(&minval, &maxval))
1434 {
1435 syntax_error = TRUE;
1436 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1437 }
1438 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1439 * <atom>* */
1440 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1441 {
1442 EMIT(NFA_STAR);
1443 break;
1444 }
1445
1446 if (maxval > NFA_BRACES_MAXLIMIT)
1447 {
1448 /* This would yield a huge automaton and use too much memory.
1449 * Revert to old engine */
1450 return FAIL;
1451 }
1452
1453 /* Special case: x{0} or x{-0} */
1454 if (maxval == 0)
1455 {
1456 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001457 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001458 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1459 EMIT(NFA_SKIP_CHAR);
1460 return OK;
1461 }
1462
1463 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001464 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 /* Save pos after the repeated atom and the \{} */
1466 new_regparse = regparse;
1467
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001468 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1469 for (i = 0; i < maxval; i++)
1470 {
1471 /* Goto beginning of the repeated atom */
1472 regparse = old_regparse;
1473 curchr = -1;
1474 /* Restore count of parenthesis */
1475 regnpar = old_regnpar;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001476 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 if (nfa_regatom() == FAIL)
1478 return FAIL;
1479 /* after "minval" times, atoms are optional */
1480 if (i + 1 > minval)
1481 EMIT(quest);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001482 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483 EMIT(NFA_CONCAT);
1484 }
1485
1486 /* Go to just after the repeated atom and the \{} */
1487 regparse = new_regparse;
1488 curchr = -1;
1489
1490 break;
1491
1492
1493 default:
1494 break;
1495 } /* end switch */
1496
1497 if (re_multi_type(peekchr()) != NOT_MULTI)
1498 {
1499 /* Can't have a multi follow a multi. */
1500 syntax_error = TRUE;
1501 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1502 }
1503
1504 return OK;
1505}
1506
1507/*
1508 * Parse one or more pieces, concatenated. It matches a match for the
1509 * first piece, followed by a match for the second piece, etc. Example:
1510 * "f[0-9]b", first matches "f", then a digit and then "b".
1511 *
1512 * concat ::= piece
1513 * or piece piece
1514 * or piece piece piece
1515 * etc.
1516 */
1517 static int
1518nfa_regconcat()
1519{
1520 int cont = TRUE;
1521 int first = TRUE;
1522
1523 while (cont)
1524 {
1525 switch (peekchr())
1526 {
1527 case NUL:
1528 case Magic('|'):
1529 case Magic('&'):
1530 case Magic(')'):
1531 cont = FALSE;
1532 break;
1533
1534 case Magic('Z'):
1535#ifdef FEAT_MBYTE
1536 regflags |= RF_ICOMBINE;
1537#endif
1538 skipchr_keepstart();
1539 break;
1540 case Magic('c'):
1541 regflags |= RF_ICASE;
1542 skipchr_keepstart();
1543 break;
1544 case Magic('C'):
1545 regflags |= RF_NOICASE;
1546 skipchr_keepstart();
1547 break;
1548 case Magic('v'):
1549 reg_magic = MAGIC_ALL;
1550 skipchr_keepstart();
1551 curchr = -1;
1552 break;
1553 case Magic('m'):
1554 reg_magic = MAGIC_ON;
1555 skipchr_keepstart();
1556 curchr = -1;
1557 break;
1558 case Magic('M'):
1559 reg_magic = MAGIC_OFF;
1560 skipchr_keepstart();
1561 curchr = -1;
1562 break;
1563 case Magic('V'):
1564 reg_magic = MAGIC_NONE;
1565 skipchr_keepstart();
1566 curchr = -1;
1567 break;
1568
1569 default:
1570 if (nfa_regpiece() == FAIL)
1571 return FAIL;
1572 if (first == FALSE)
1573 EMIT(NFA_CONCAT);
1574 else
1575 first = FALSE;
1576 break;
1577 }
1578 }
1579
1580 return OK;
1581}
1582
1583/*
1584 * Parse a branch, one or more concats, separated by "\&". It matches the
1585 * last concat, but only if all the preceding concats also match at the same
1586 * position. Examples:
1587 * "foobeep\&..." matches "foo" in "foobeep".
1588 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1589 *
1590 * branch ::= concat
1591 * or concat \& concat
1592 * or concat \& concat \& concat
1593 * etc.
1594 */
1595 static int
1596nfa_regbranch()
1597{
1598 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001599 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001600
Bram Moolenaar16299b52013-05-30 18:45:23 +02001601 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602
1603 /* First branch, possibly the only one */
1604 if (nfa_regconcat() == FAIL)
1605 return FAIL;
1606
1607 ch = peekchr();
1608 /* Try next concats */
1609 while (ch == Magic('&'))
1610 {
1611 skipchr();
1612 EMIT(NFA_NOPEN);
1613 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001614 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001615 if (nfa_regconcat() == FAIL)
1616 return FAIL;
1617 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001618 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 EMIT(NFA_SKIP_CHAR);
1620 EMIT(NFA_CONCAT);
1621 ch = peekchr();
1622 }
1623
1624 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001625 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001626 EMIT(NFA_SKIP_CHAR);
1627
1628 return OK;
1629}
1630
1631/*
1632 * Parse a pattern, one or more branches, separated by "\|". It matches
1633 * anything that matches one of the branches. Example: "foo\|beep" matches
1634 * "foo" and matches "beep". If more than one branch matches, the first one
1635 * is used.
1636 *
1637 * pattern ::= branch
1638 * or branch \| branch
1639 * or branch \| branch \| branch
1640 * etc.
1641 */
1642 static int
1643nfa_reg(paren)
1644 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1645{
1646 int parno = 0;
1647
1648#ifdef FEAT_SYN_HL
1649#endif
1650 if (paren == REG_PAREN)
1651 {
1652 if (regnpar >= NSUBEXP) /* Too many `(' */
1653 {
1654 syntax_error = TRUE;
1655 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1656 }
1657 parno = regnpar++;
1658 }
1659
1660 if (nfa_regbranch() == FAIL)
1661 return FAIL; /* cascaded error */
1662
1663 while (peekchr() == Magic('|'))
1664 {
1665 skipchr();
1666 if (nfa_regbranch() == FAIL)
1667 return FAIL; /* cascaded error */
1668 EMIT(NFA_OR);
1669 }
1670
1671 /* Check for proper termination. */
1672 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1673 {
1674 syntax_error = TRUE;
1675 if (paren == REG_NPAREN)
1676 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1677 else
1678 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1679 }
1680 else if (paren == REG_NOPAREN && peekchr() != NUL)
1681 {
1682 syntax_error = TRUE;
1683 if (peekchr() == Magic(')'))
1684 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1685 else
1686 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1687 }
1688 /*
1689 * Here we set the flag allowing back references to this set of
1690 * parentheses.
1691 */
1692 if (paren == REG_PAREN)
1693 {
1694 had_endbrace[parno] = TRUE; /* have seen the close paren */
1695 EMIT(NFA_MOPEN + parno);
1696 }
1697
1698 return OK;
1699}
1700
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001701#ifdef DEBUG
1702static char_u code[50];
1703
1704 static void
1705nfa_set_code(c)
1706 int c;
1707{
1708 int addnl = FALSE;
1709
1710 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1711 {
1712 addnl = TRUE;
1713 c -= ADD_NL;
1714 }
1715
1716 STRCPY(code, "");
1717 switch (c)
1718 {
1719 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1720 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1721 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1722 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1723 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1724 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1725
Bram Moolenaar5714b802013-05-28 22:03:20 +02001726 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1727 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1728 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1729 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1730 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1731 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1732 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1733 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1734 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1735 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1736
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001737 case NFA_PREV_ATOM_NO_WIDTH:
1738 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001739 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1740 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001741 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1742 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1744 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1745
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001746 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1747 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1748
1749 case NFA_MOPEN + 0:
1750 case NFA_MOPEN + 1:
1751 case NFA_MOPEN + 2:
1752 case NFA_MOPEN + 3:
1753 case NFA_MOPEN + 4:
1754 case NFA_MOPEN + 5:
1755 case NFA_MOPEN + 6:
1756 case NFA_MOPEN + 7:
1757 case NFA_MOPEN + 8:
1758 case NFA_MOPEN + 9:
1759 STRCPY(code, "NFA_MOPEN(x)");
1760 code[10] = c - NFA_MOPEN + '0';
1761 break;
1762 case NFA_MCLOSE + 0:
1763 case NFA_MCLOSE + 1:
1764 case NFA_MCLOSE + 2:
1765 case NFA_MCLOSE + 3:
1766 case NFA_MCLOSE + 4:
1767 case NFA_MCLOSE + 5:
1768 case NFA_MCLOSE + 6:
1769 case NFA_MCLOSE + 7:
1770 case NFA_MCLOSE + 8:
1771 case NFA_MCLOSE + 9:
1772 STRCPY(code, "NFA_MCLOSE(x)");
1773 code[11] = c - NFA_MCLOSE + '0';
1774 break;
1775 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1776 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1777 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1778 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001779 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1780 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001781 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1782 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1783 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1784 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1785 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1786 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1787 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1788 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1789 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1790 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1791 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1792 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1793 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1794 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1795 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1796 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1797 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1798 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1799 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1800 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1801 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1802 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1803 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1804 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1805
1806 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1807 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1808 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1809 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1810 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1811 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1812 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1813 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1814 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1815 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1816 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1817 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1818 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1819 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1820 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1821 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1822 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1823 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1824 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1825 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1826 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1827 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1828 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1829 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1830 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1831 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1832 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1833
1834 default:
1835 STRCPY(code, "CHAR(x)");
1836 code[5] = c;
1837 }
1838
1839 if (addnl == TRUE)
1840 STRCAT(code, " + NEWLINE ");
1841
1842}
1843
1844#ifdef ENABLE_LOG
1845static FILE *log_fd;
1846
1847/*
1848 * Print the postfix notation of the current regexp.
1849 */
1850 static void
1851nfa_postfix_dump(expr, retval)
1852 char_u *expr;
1853 int retval;
1854{
1855 int *p;
1856 FILE *f;
1857
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001858 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859 if (f != NULL)
1860 {
1861 fprintf(f, "\n-------------------------\n");
1862 if (retval == FAIL)
1863 fprintf(f, ">>> NFA engine failed ... \n");
1864 else if (retval == OK)
1865 fprintf(f, ">>> NFA engine succeeded !\n");
1866 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001867 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 {
1869 nfa_set_code(*p);
1870 fprintf(f, "%s, ", code);
1871 }
1872 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001873 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 fprintf(f, "%d ", *p);
1875 fprintf(f, "\n\n");
1876 fclose(f);
1877 }
1878}
1879
1880/*
1881 * Print the NFA starting with a root node "state".
1882 */
1883 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001884nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885 FILE *debugf;
1886 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001888 garray_T indent;
1889
1890 ga_init2(&indent, 1, 64);
1891 ga_append(&indent, '\0');
1892 nfa_print_state2(debugf, state, &indent);
1893 ga_clear(&indent);
1894}
1895
1896 static void
1897nfa_print_state2(debugf, state, indent)
1898 FILE *debugf;
1899 nfa_state_T *state;
1900 garray_T *indent;
1901{
1902 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903
1904 if (state == NULL)
1905 return;
1906
1907 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001908
1909 /* Output indent */
1910 p = (char_u *)indent->ga_data;
1911 if (indent->ga_len >= 3)
1912 {
1913 int last = indent->ga_len - 3;
1914 char_u save[2];
1915
1916 STRNCPY(save, &p[last], 2);
1917 STRNCPY(&p[last], "+-", 2);
1918 fprintf(debugf, " %s", p);
1919 STRNCPY(&p[last], save, 2);
1920 }
1921 else
1922 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923
1924 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001925 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1926 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 if (state->id < 0)
1928 return;
1929
1930 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001931
1932 /* grow indent for state->out */
1933 indent->ga_len -= 1;
1934 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001935 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001936 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001937 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001938 ga_append(indent, '\0');
1939
1940 nfa_print_state2(debugf, state->out, indent);
1941
1942 /* replace last part of indent for state->out1 */
1943 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001944 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001945 ga_append(indent, '\0');
1946
1947 nfa_print_state2(debugf, state->out1, indent);
1948
1949 /* shrink indent */
1950 indent->ga_len -= 3;
1951 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952}
1953
1954/*
1955 * Print the NFA state machine.
1956 */
1957 static void
1958nfa_dump(prog)
1959 nfa_regprog_T *prog;
1960{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001961 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962
1963 if (debugf != NULL)
1964 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001965 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966 fclose(debugf);
1967 }
1968}
1969#endif /* ENABLE_LOG */
1970#endif /* DEBUG */
1971
1972/*
1973 * Parse r.e. @expr and convert it into postfix form.
1974 * Return the postfix string on success, NULL otherwise.
1975 */
1976 static int *
1977re2post()
1978{
1979 if (nfa_reg(REG_NOPAREN) == FAIL)
1980 return NULL;
1981 EMIT(NFA_MOPEN);
1982 return post_start;
1983}
1984
1985/* NB. Some of the code below is inspired by Russ's. */
1986
1987/*
1988 * Represents an NFA state plus zero or one or two arrows exiting.
1989 * if c == MATCH, no arrows out; matching state.
1990 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1991 * If c < 256, labeled arrow with character c to out.
1992 */
1993
1994static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1995
1996/*
1997 * Allocate and initialize nfa_state_T.
1998 */
1999 static nfa_state_T *
2000new_state(c, out, out1)
2001 int c;
2002 nfa_state_T *out;
2003 nfa_state_T *out1;
2004{
2005 nfa_state_T *s;
2006
2007 if (istate >= nstate)
2008 return NULL;
2009
2010 s = &state_ptr[istate++];
2011
2012 s->c = c;
2013 s->out = out;
2014 s->out1 = out1;
2015
2016 s->id = istate;
2017 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018 s->negated = FALSE;
2019
2020 return s;
2021}
2022
2023/*
2024 * A partially built NFA without the matching state filled in.
2025 * Frag_T.start points at the start state.
2026 * Frag_T.out is a list of places that need to be set to the
2027 * next state for this fragment.
2028 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002029
2030/* Since the out pointers in the list are always
2031 * uninitialized, we use the pointers themselves
2032 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002034union Ptrlist
2035{
2036 Ptrlist *next;
2037 nfa_state_T *s;
2038};
2039
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040struct Frag
2041{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002042 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 Ptrlist *out;
2044};
2045typedef struct Frag Frag_T;
2046
2047static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2048static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2049static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2050static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2051static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2052static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2053
2054/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002055 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056 */
2057 static Frag_T
2058frag(start, out)
2059 nfa_state_T *start;
2060 Ptrlist *out;
2061{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002062 Frag_T n;
2063
2064 n.start = start;
2065 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002066 return n;
2067}
2068
2069/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 * Create singleton list containing just outp.
2071 */
2072 static Ptrlist *
2073list1(outp)
2074 nfa_state_T **outp;
2075{
2076 Ptrlist *l;
2077
2078 l = (Ptrlist *)outp;
2079 l->next = NULL;
2080 return l;
2081}
2082
2083/*
2084 * Patch the list of states at out to point to start.
2085 */
2086 static void
2087patch(l, s)
2088 Ptrlist *l;
2089 nfa_state_T *s;
2090{
2091 Ptrlist *next;
2092
2093 for (; l; l = next)
2094 {
2095 next = l->next;
2096 l->s = s;
2097 }
2098}
2099
2100
2101/*
2102 * Join the two lists l1 and l2, returning the combination.
2103 */
2104 static Ptrlist *
2105append(l1, l2)
2106 Ptrlist *l1;
2107 Ptrlist *l2;
2108{
2109 Ptrlist *oldl1;
2110
2111 oldl1 = l1;
2112 while (l1->next)
2113 l1 = l1->next;
2114 l1->next = l2;
2115 return oldl1;
2116}
2117
2118/*
2119 * Stack used for transforming postfix form into NFA.
2120 */
2121static Frag_T empty;
2122
2123 static void
2124st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002125 int *postfix UNUSED;
2126 int *end UNUSED;
2127 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002128{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002129#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002130 FILE *df;
2131 int *p2;
2132
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002133 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002134 if (df)
2135 {
2136 fprintf(df, "Error popping the stack!\n");
2137#ifdef DEBUG
2138 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2139#endif
2140 fprintf(df, "Postfix form is: ");
2141#ifdef DEBUG
2142 for (p2 = postfix; p2 < end; p2++)
2143 {
2144 nfa_set_code(*p2);
2145 fprintf(df, "%s, ", code);
2146 }
2147 nfa_set_code(*p);
2148 fprintf(df, "\nCurrent position is: ");
2149 for (p2 = postfix; p2 <= p; p2 ++)
2150 {
2151 nfa_set_code(*p2);
2152 fprintf(df, "%s, ", code);
2153 }
2154#else
2155 for (p2 = postfix; p2 < end; p2++)
2156 {
2157 fprintf(df, "%d, ", *p2);
2158 }
2159 fprintf(df, "\nCurrent position is: ");
2160 for (p2 = postfix; p2 <= p; p2 ++)
2161 {
2162 fprintf(df, "%d, ", *p2);
2163 }
2164#endif
2165 fprintf(df, "\n--------------------------\n");
2166 fclose(df);
2167 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002168#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002169 EMSG(_("E874: (NFA) Could not pop the stack !"));
2170}
2171
2172/*
2173 * Push an item onto the stack.
2174 */
2175 static void
2176st_push(s, p, stack_end)
2177 Frag_T s;
2178 Frag_T **p;
2179 Frag_T *stack_end;
2180{
2181 Frag_T *stackp = *p;
2182
2183 if (stackp >= stack_end)
2184 return;
2185 *stackp = s;
2186 *p = *p + 1;
2187}
2188
2189/*
2190 * Pop an item from the stack.
2191 */
2192 static Frag_T
2193st_pop(p, stack)
2194 Frag_T **p;
2195 Frag_T *stack;
2196{
2197 Frag_T *stackp;
2198
2199 *p = *p - 1;
2200 stackp = *p;
2201 if (stackp < stack)
2202 return empty;
2203 return **p;
2204}
2205
2206/*
2207 * Convert a postfix form into its equivalent NFA.
2208 * Return the NFA start state on success, NULL otherwise.
2209 */
2210 static nfa_state_T *
2211post2nfa(postfix, end, nfa_calc_size)
2212 int *postfix;
2213 int *end;
2214 int nfa_calc_size;
2215{
2216 int *p;
2217 int mopen;
2218 int mclose;
2219 Frag_T *stack = NULL;
2220 Frag_T *stackp = NULL;
2221 Frag_T *stack_end = NULL;
2222 Frag_T e1;
2223 Frag_T e2;
2224 Frag_T e;
2225 nfa_state_T *s;
2226 nfa_state_T *s1;
2227 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002228 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229
2230 if (postfix == NULL)
2231 return NULL;
2232
Bram Moolenaar053bb602013-05-20 13:55:21 +02002233#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234#define POP() st_pop(&stackp, stack); \
2235 if (stackp < stack) \
2236 { \
2237 st_error(postfix, end, p); \
2238 return NULL; \
2239 }
2240
2241 if (nfa_calc_size == FALSE)
2242 {
2243 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002244 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002245 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002246 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002247 }
2248
2249 for (p = postfix; p < end; ++p)
2250 {
2251 switch (*p)
2252 {
2253 case NFA_CONCAT:
2254 /* Catenation.
2255 * Pay attention: this operator does not exist
2256 * in the r.e. itself (it is implicit, really).
2257 * It is added when r.e. is translated to postfix
2258 * form in re2post().
2259 *
2260 * No new state added here. */
2261 if (nfa_calc_size == TRUE)
2262 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002263 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002264 break;
2265 }
2266 e2 = POP();
2267 e1 = POP();
2268 patch(e1.out, e2.start);
2269 PUSH(frag(e1.start, e2.out));
2270 break;
2271
2272 case NFA_NOT:
2273 /* Negation of a character */
2274 if (nfa_calc_size == TRUE)
2275 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002276 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277 break;
2278 }
2279 e1 = POP();
2280 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002281#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002282 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002284#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 PUSH(e1);
2286 break;
2287
2288 case NFA_OR:
2289 /* Alternation */
2290 if (nfa_calc_size == TRUE)
2291 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002292 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293 break;
2294 }
2295 e2 = POP();
2296 e1 = POP();
2297 s = new_state(NFA_SPLIT, e1.start, e2.start);
2298 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002299 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300 PUSH(frag(s, append(e1.out, e2.out)));
2301 break;
2302
2303 case NFA_STAR:
2304 /* Zero or more */
2305 if (nfa_calc_size == TRUE)
2306 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002307 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002308 break;
2309 }
2310 e = POP();
2311 s = new_state(NFA_SPLIT, e.start, NULL);
2312 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002313 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314 patch(e.out, s);
2315 PUSH(frag(s, list1(&s->out1)));
2316 break;
2317
2318 case NFA_QUEST:
2319 /* one or zero atoms=> greedy match */
2320 if (nfa_calc_size == TRUE)
2321 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002322 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002323 break;
2324 }
2325 e = POP();
2326 s = new_state(NFA_SPLIT, e.start, NULL);
2327 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002328 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329 PUSH(frag(s, append(e.out, list1(&s->out1))));
2330 break;
2331
2332 case NFA_QUEST_NONGREEDY:
2333 /* zero or one atoms => non-greedy match */
2334 if (nfa_calc_size == TRUE)
2335 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002336 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002337 break;
2338 }
2339 e = POP();
2340 s = new_state(NFA_SPLIT, NULL, e.start);
2341 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002342 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002343 PUSH(frag(s, append(e.out, list1(&s->out))));
2344 break;
2345
2346 case NFA_PLUS:
2347 /* One or more */
2348 if (nfa_calc_size == TRUE)
2349 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002350 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002351 break;
2352 }
2353 e = POP();
2354 s = new_state(NFA_SPLIT, e.start, NULL);
2355 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002356 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357 patch(e.out, s);
2358 PUSH(frag(e.start, list1(&s->out1)));
2359 break;
2360
2361 case NFA_SKIP_CHAR:
2362 /* Symbol of 0-length, Used in a repetition
2363 * with max/min count of 0 */
2364 if (nfa_calc_size == TRUE)
2365 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002366 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002367 break;
2368 }
2369 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2370 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002371 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 PUSH(frag(s, list1(&s->out)));
2373 break;
2374
2375 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002376 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002377 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002378 * The \@! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002380 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381
2382 if (nfa_calc_size == TRUE)
2383 {
2384 nstate += 2;
2385 break;
2386 }
2387 e = POP();
2388 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2389 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002390 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002391 patch(e.out, s1);
2392
2393 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2394 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002395 goto theend;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002396 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG)
2397 {
2398 s->negated = TRUE;
2399 s1->negated = TRUE;
2400 }
2401
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002402 PUSH(frag(s, list1(&s1->out)));
2403 break;
2404
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002405#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002406 case NFA_COMPOSING: /* char with composing char */
2407#if 0
2408 /* TODO */
2409 if (regflags & RF_ICOMBINE)
2410 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002411 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002412 }
2413#endif
2414 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002415#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002416
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002417 case NFA_MOPEN + 0: /* Submatch */
2418 case NFA_MOPEN + 1:
2419 case NFA_MOPEN + 2:
2420 case NFA_MOPEN + 3:
2421 case NFA_MOPEN + 4:
2422 case NFA_MOPEN + 5:
2423 case NFA_MOPEN + 6:
2424 case NFA_MOPEN + 7:
2425 case NFA_MOPEN + 8:
2426 case NFA_MOPEN + 9:
2427 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002428 if (nfa_calc_size == TRUE)
2429 {
2430 nstate += 2;
2431 break;
2432 }
2433
2434 mopen = *p;
2435 switch (*p)
2436 {
2437 case NFA_NOPEN:
2438 mclose = NFA_NCLOSE;
2439 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002440#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002441 case NFA_COMPOSING:
2442 mclose = NFA_END_COMPOSING;
2443 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002444#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 default:
2446 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2447 mclose = *p + NSUBEXP;
2448 break;
2449 }
2450
2451 /* Allow "NFA_MOPEN" as a valid postfix representation for
2452 * the empty regexp "". In this case, the NFA will be
2453 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2454 * empty groups of parenthesis, and empty mbyte chars */
2455 if (stackp == stack)
2456 {
2457 s = new_state(mopen, NULL, NULL);
2458 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002459 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002460 s1 = new_state(mclose, NULL, NULL);
2461 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002462 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463 patch(list1(&s->out), s1);
2464 PUSH(frag(s, list1(&s1->out)));
2465 break;
2466 }
2467
2468 /* At least one node was emitted before NFA_MOPEN, so
2469 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2470 e = POP();
2471 s = new_state(mopen, e.start, NULL); /* `(' */
2472 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002473 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002474
2475 s1 = new_state(mclose, NULL, NULL); /* `)' */
2476 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002477 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002478 patch(e.out, s1);
2479
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002480#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002481 if (mopen == NFA_COMPOSING)
2482 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002483 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002484#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002485
2486 PUSH(frag(s, list1(&s1->out)));
2487 break;
2488
Bram Moolenaar5714b802013-05-28 22:03:20 +02002489 case NFA_BACKREF1:
2490 case NFA_BACKREF2:
2491 case NFA_BACKREF3:
2492 case NFA_BACKREF4:
2493 case NFA_BACKREF5:
2494 case NFA_BACKREF6:
2495 case NFA_BACKREF7:
2496 case NFA_BACKREF8:
2497 case NFA_BACKREF9:
2498 if (nfa_calc_size == TRUE)
2499 {
2500 nstate += 2;
2501 break;
2502 }
2503 s = new_state(*p, NULL, NULL);
2504 if (s == NULL)
2505 goto theend;
2506 s1 = new_state(NFA_SKIP, NULL, NULL);
2507 if (s1 == NULL)
2508 goto theend;
2509 patch(list1(&s->out), s1);
2510 PUSH(frag(s, list1(&s1->out)));
2511 break;
2512
Bram Moolenaar423532e2013-05-29 21:14:42 +02002513 case NFA_LNUM:
2514 case NFA_LNUM_GT:
2515 case NFA_LNUM_LT:
2516 case NFA_VCOL:
2517 case NFA_VCOL_GT:
2518 case NFA_VCOL_LT:
2519 case NFA_COL:
2520 case NFA_COL_GT:
2521 case NFA_COL_LT:
2522 if (nfa_calc_size == TRUE)
2523 {
2524 nstate += 1;
2525 break;
2526 }
2527 e1 = POP();
2528 s = new_state(*p, NULL, NULL);
2529 if (s == NULL)
2530 goto theend;
2531 s->val = e1.start->c;
2532 PUSH(frag(s, list1(&s->out)));
2533 break;
2534
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002535 case NFA_ZSTART:
2536 case NFA_ZEND:
2537 default:
2538 /* Operands */
2539 if (nfa_calc_size == TRUE)
2540 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002541 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002542 break;
2543 }
2544 s = new_state(*p, NULL, NULL);
2545 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002546 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 PUSH(frag(s, list1(&s->out)));
2548 break;
2549
2550 } /* switch(*p) */
2551
2552 } /* for(p = postfix; *p; ++p) */
2553
2554 if (nfa_calc_size == TRUE)
2555 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002556 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002557 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 }
2559
2560 e = POP();
2561 if (stackp != stack)
2562 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2563
2564 if (istate >= nstate)
2565 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2566
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002567 matchstate = &state_ptr[istate++]; /* the match state */
2568 matchstate->c = NFA_MATCH;
2569 matchstate->out = matchstate->out1 = NULL;
2570
2571 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002572 ret = e.start;
2573
2574theend:
2575 vim_free(stack);
2576 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002577
2578#undef POP1
2579#undef PUSH1
2580#undef POP2
2581#undef PUSH2
2582#undef POP
2583#undef PUSH
2584}
2585
2586/****************************************************************
2587 * NFA execution code.
2588 ****************************************************************/
2589
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002590typedef struct
2591{
2592 int in_use; /* number of subexpr with useful info */
2593
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002594 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002595 union
2596 {
2597 struct multipos
2598 {
2599 lpos_T start;
2600 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002601 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002602 struct linepos
2603 {
2604 char_u *start;
2605 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002606 } line[NSUBEXP];
2607 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002608} regsub_T;
2609
Bram Moolenaar963fee22013-05-26 21:47:28 +02002610/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002611typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002612{
2613 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002614 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002615 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002616} nfa_thread_T;
2617
Bram Moolenaar963fee22013-05-26 21:47:28 +02002618/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002619typedef struct
2620{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002621 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002622 int n; /* nr of states currently in "t" */
2623 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002624 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002625} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002626
Bram Moolenaar5714b802013-05-28 22:03:20 +02002627#ifdef ENABLE_LOG
2628 static void
2629log_subexpr(sub)
2630 regsub_T *sub;
2631{
2632 int j;
2633
2634 for (j = 0; j < sub->in_use; j++)
2635 if (REG_MULTI)
2636 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2637 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002638 sub->list.multi[j].start.col,
2639 (int)sub->list.multi[j].start.lnum,
2640 sub->list.multi[j].end.col,
2641 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002642 else
2643 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2644 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002645 (char *)sub->list.line[j].start,
2646 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002647 fprintf(log_fd, "\n");
2648}
2649#endif
2650
Bram Moolenaar963fee22013-05-26 21:47:28 +02002651/* Used during execution: whether a match has been found. */
2652static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002653
Bram Moolenaar428e9872013-05-30 17:05:39 +02002654static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002655static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2656static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002657
Bram Moolenaar428e9872013-05-30 17:05:39 +02002658/*
2659 * Return TRUE if "sub1" and "sub2" have the same positions.
2660 */
2661 static int
2662sub_equal(sub1, sub2)
2663 regsub_T *sub1;
2664 regsub_T *sub2;
2665{
2666 int i;
2667 int todo;
2668 linenr_T s1, e1;
2669 linenr_T s2, e2;
2670 char_u *sp1, *ep1;
2671 char_u *sp2, *ep2;
2672
2673 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2674 if (REG_MULTI)
2675 {
2676 for (i = 0; i < todo; ++i)
2677 {
2678 if (i < sub1->in_use)
2679 {
2680 s1 = sub1->list.multi[i].start.lnum;
2681 e1 = sub1->list.multi[i].end.lnum;
2682 }
2683 else
2684 {
2685 s1 = 0;
2686 e1 = 0;
2687 }
2688 if (i < sub2->in_use)
2689 {
2690 s2 = sub2->list.multi[i].start.lnum;
2691 e2 = sub2->list.multi[i].end.lnum;
2692 }
2693 else
2694 {
2695 s2 = 0;
2696 e2 = 0;
2697 }
2698 if (s1 != s2 || e1 != e2)
2699 return FALSE;
2700 if (s1 != 0 && sub1->list.multi[i].start.col
2701 != sub2->list.multi[i].start.col)
2702 return FALSE;
2703 if (e1 != 0 && sub1->list.multi[i].end.col
2704 != sub2->list.multi[i].end.col)
2705 return FALSE;
2706 }
2707 }
2708 else
2709 {
2710 for (i = 0; i < todo; ++i)
2711 {
2712 if (i < sub1->in_use)
2713 {
2714 sp1 = sub1->list.line[i].start;
2715 ep1 = sub1->list.line[i].end;
2716 }
2717 else
2718 {
2719 sp1 = NULL;
2720 ep1 = NULL;
2721 }
2722 if (i < sub2->in_use)
2723 {
2724 sp2 = sub2->list.line[i].start;
2725 ep2 = sub2->list.line[i].end;
2726 }
2727 else
2728 {
2729 sp2 = NULL;
2730 ep2 = NULL;
2731 }
2732 if (sp1 != sp2 || ep1 != ep2)
2733 return FALSE;
2734 }
2735 }
2736
2737 return TRUE;
2738}
2739
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002740 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002741addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002742 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002743 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002744 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002745 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002746{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002747 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002748 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002749 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002750 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002751 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002752 int i;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002753#ifdef ENABLE_LOG
2754 int did_print = FALSE;
2755#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002756
2757 if (l == NULL || state == NULL)
2758 return;
2759
2760 switch (state->c)
2761 {
2762 case NFA_SPLIT:
2763 case NFA_NOT:
2764 case NFA_NOPEN:
2765 case NFA_NCLOSE:
2766 case NFA_MCLOSE:
2767 case NFA_MCLOSE + 1:
2768 case NFA_MCLOSE + 2:
2769 case NFA_MCLOSE + 3:
2770 case NFA_MCLOSE + 4:
2771 case NFA_MCLOSE + 5:
2772 case NFA_MCLOSE + 6:
2773 case NFA_MCLOSE + 7:
2774 case NFA_MCLOSE + 8:
2775 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002776 /* These nodes are not added themselves but their "out" and/or
2777 * "out1" may be added below. */
2778 break;
2779
2780 case NFA_MOPEN:
2781 case NFA_MOPEN + 1:
2782 case NFA_MOPEN + 2:
2783 case NFA_MOPEN + 3:
2784 case NFA_MOPEN + 4:
2785 case NFA_MOPEN + 5:
2786 case NFA_MOPEN + 6:
2787 case NFA_MOPEN + 7:
2788 case NFA_MOPEN + 8:
2789 case NFA_MOPEN + 9:
2790 /* These nodes do not need to be added, but we need to bail out
2791 * when it was tried to be added to this list before. */
2792 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002793 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002794 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002795 break;
2796
2797 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002798 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002799 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002800 /* This state is already in the list, don't add it again,
2801 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002802 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002803 {
2804skip_add:
2805#ifdef ENABLE_LOG
2806 nfa_set_code(state->c);
2807 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
2808 abs(state->id), l->id, state->c, code);
2809#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02002810 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002811 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02002812
2813 /* See if the same state is already in the list with the same
2814 * positions. */
2815 for (i = 0; i < l->n; ++i)
2816 {
2817 thread = &l->t[i];
2818 if (thread->state->id == state->id
2819 && sub_equal(&thread->sub, sub))
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002820 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002821 }
2822 }
2823
2824 /* when there are backreferences the number of states may be (a
2825 * lot) bigger */
2826 if (nfa_has_backref && l->n == l->len)
2827 {
2828 int newlen = l->len * 3 / 2 + 50;
2829
2830 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2831 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002833
2834 /* add the state to the list */
2835 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002836 thread = &l->t[l->n++];
2837 thread->state = state;
2838 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002839 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002841 /* Copy the match start and end positions. */
2842 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002843 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002844 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002845 sizeof(struct multipos) * sub->in_use);
2846 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002847 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002848 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002849 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002850 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002851#ifdef ENABLE_LOG
2852 {
2853 int col;
2854
2855 if (thread->sub.in_use <= 0)
2856 col = -1;
2857 else if (REG_MULTI)
2858 col = thread->sub.list.multi[0].start.col;
2859 else
2860 col = (int)(thread->sub.list.line[0].start - regline);
2861 nfa_set_code(state->c);
2862 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
2863 abs(state->id), l->id, state->c, code, col);
2864 did_print = TRUE;
2865 }
2866#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002867 }
2868
2869#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002870 if (!did_print)
2871 {
2872 int col;
2873
2874 if (sub->in_use <= 0)
2875 col = -1;
2876 else if (REG_MULTI)
2877 col = sub->list.multi[0].start.col;
2878 else
2879 col = (int)(sub->list.line[0].start - regline);
2880 nfa_set_code(state->c);
2881 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
2882 abs(state->id), l->id, state->c, code, col);
2883 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884#endif
2885 switch (state->c)
2886 {
2887 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002888 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002889 break;
2890
2891 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002892 addstate(l, state->out, sub, off);
2893 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002894 break;
2895
2896#if 0
2897 case NFA_END_NEG_RANGE:
2898 /* Nothing to handle here. nfa_regmatch() will take care of it */
2899 break;
2900
2901 case NFA_NOT:
2902 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2903#ifdef ENABLE_LOG
2904 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2905#endif
2906 break;
2907
2908 case NFA_COMPOSING:
2909 /* nfa_regmatch() will match all the bytes of this composing char. */
2910 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002911#endif
2912
Bram Moolenaar5714b802013-05-28 22:03:20 +02002913 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914 case NFA_NOPEN:
2915 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002916 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917 break;
2918
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002919 case NFA_MOPEN + 0:
2920 case NFA_MOPEN + 1:
2921 case NFA_MOPEN + 2:
2922 case NFA_MOPEN + 3:
2923 case NFA_MOPEN + 4:
2924 case NFA_MOPEN + 5:
2925 case NFA_MOPEN + 6:
2926 case NFA_MOPEN + 7:
2927 case NFA_MOPEN + 8:
2928 case NFA_MOPEN + 9:
2929 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002930 if (state->c == NFA_ZSTART)
2931 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002932 else
2933 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002935 /* Set the position (with "off") in the subexpression. Save and
2936 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02002937 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938 if (REG_MULTI)
2939 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002940 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002941 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002942 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002943 save_in_use = -1;
2944 }
2945 else
2946 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002947 save_in_use = sub->in_use;
2948 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002949 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002950 sub->list.multi[i].start.lnum = -1;
2951 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002952 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002953 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002954 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002955 if (off == -1)
2956 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002957 sub->list.multi[subidx].start.lnum = reglnum + 1;
2958 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002959 }
2960 else
2961 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002962 sub->list.multi[subidx].start.lnum = reglnum;
2963 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002964 (colnr_T)(reginput - regline + off);
2965 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002966 }
2967 else
2968 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002969 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002970 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002971 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002972 save_in_use = -1;
2973 }
2974 else
2975 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002976 save_in_use = sub->in_use;
2977 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002978 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002979 sub->list.line[i].start = NULL;
2980 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002981 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002982 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002983 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002984 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002985 }
2986
Bram Moolenaar5714b802013-05-28 22:03:20 +02002987 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002988
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002989 if (save_in_use == -1)
2990 {
2991 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002992 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002993 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002994 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002995 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002996 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002997 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002998 break;
2999
3000 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003001 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003002 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003003 /* Do not overwrite the position set by \ze. If no \ze
3004 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003005 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003006 break;
3007 }
3008 case NFA_MCLOSE + 1:
3009 case NFA_MCLOSE + 2:
3010 case NFA_MCLOSE + 3:
3011 case NFA_MCLOSE + 4:
3012 case NFA_MCLOSE + 5:
3013 case NFA_MCLOSE + 6:
3014 case NFA_MCLOSE + 7:
3015 case NFA_MCLOSE + 8:
3016 case NFA_MCLOSE + 9:
3017 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003018 if (state->c == NFA_ZEND)
3019 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003020 else
3021 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003022
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003023 /* We don't fill in gaps here, there must have been an MOPEN that
3024 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003025 save_in_use = sub->in_use;
3026 if (sub->in_use <= subidx)
3027 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003028 if (REG_MULTI)
3029 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003030 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003031 if (off == -1)
3032 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003033 sub->list.multi[subidx].end.lnum = reglnum + 1;
3034 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003035 }
3036 else
3037 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003038 sub->list.multi[subidx].end.lnum = reglnum;
3039 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003040 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003041 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003042 }
3043 else
3044 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003045 save_ptr = sub->list.line[subidx].end;
3046 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003047 }
3048
Bram Moolenaar5714b802013-05-28 22:03:20 +02003049 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003050
3051 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003052 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003053 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003054 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003055 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003056 break;
3057 }
3058}
3059
3060/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003061 * Like addstate(), but the new state(s) are put at position "*ip".
3062 * Used for zero-width matches, next state to use is the added one.
3063 * This makes sure the order of states to be tried does not change, which
3064 * matters for alternatives.
3065 */
3066 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003067addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003068 nfa_list_T *l; /* runtime state list */
3069 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003070 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003071 int *ip;
3072{
3073 int tlen = l->n;
3074 int count;
3075 int i = *ip;
3076
3077 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003078 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003079
3080 /* when "*ip" was at the end of the list, nothing to do */
3081 if (i + 1 == tlen)
3082 return;
3083
3084 /* re-order to put the new state at the current position */
3085 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003086 if (count == 1)
3087 {
3088 /* overwrite the current state */
3089 l->t[i] = l->t[l->n - 1];
3090 }
3091 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003092 {
3093 /* make space for new states, then move them from the
3094 * end to the current position */
3095 mch_memmove(&(l->t[i + count]),
3096 &(l->t[i + 1]),
3097 sizeof(nfa_thread_T) * (l->n - i - 1));
3098 mch_memmove(&(l->t[i]),
3099 &(l->t[l->n - 1]),
3100 sizeof(nfa_thread_T) * count);
3101 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003102 --l->n;
3103 *ip = i - 1;
3104}
3105
3106/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003107 * Check character class "class" against current character c.
3108 */
3109 static int
3110check_char_class(class, c)
3111 int class;
3112 int c;
3113{
3114 switch (class)
3115 {
3116 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003117 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118 return OK;
3119 break;
3120 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003121 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122 return OK;
3123 break;
3124 case NFA_CLASS_BLANK:
3125 if (c == ' ' || c == '\t')
3126 return OK;
3127 break;
3128 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003129 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003130 return OK;
3131 break;
3132 case NFA_CLASS_DIGIT:
3133 if (VIM_ISDIGIT(c))
3134 return OK;
3135 break;
3136 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003137 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003138 return OK;
3139 break;
3140 case NFA_CLASS_LOWER:
3141 if (MB_ISLOWER(c))
3142 return OK;
3143 break;
3144 case NFA_CLASS_PRINT:
3145 if (vim_isprintc(c))
3146 return OK;
3147 break;
3148 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003149 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003150 return OK;
3151 break;
3152 case NFA_CLASS_SPACE:
3153 if ((c >=9 && c <= 13) || (c == ' '))
3154 return OK;
3155 break;
3156 case NFA_CLASS_UPPER:
3157 if (MB_ISUPPER(c))
3158 return OK;
3159 break;
3160 case NFA_CLASS_XDIGIT:
3161 if (vim_isxdigit(c))
3162 return OK;
3163 break;
3164 case NFA_CLASS_TAB:
3165 if (c == '\t')
3166 return OK;
3167 break;
3168 case NFA_CLASS_RETURN:
3169 if (c == '\r')
3170 return OK;
3171 break;
3172 case NFA_CLASS_BACKSPACE:
3173 if (c == '\b')
3174 return OK;
3175 break;
3176 case NFA_CLASS_ESCAPE:
3177 if (c == '\033')
3178 return OK;
3179 break;
3180
3181 default:
3182 /* should not be here :P */
3183 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3184 }
3185 return FAIL;
3186}
3187
Bram Moolenaar5714b802013-05-28 22:03:20 +02003188static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3189
3190/*
3191 * Check for a match with subexpression "subidx".
3192 * return TRUE if it matches.
3193 */
3194 static int
3195match_backref(sub, subidx, bytelen)
3196 regsub_T *sub; /* pointers to subexpressions */
3197 int subidx;
3198 int *bytelen; /* out: length of match in bytes */
3199{
3200 int len;
3201
3202 if (sub->in_use <= subidx)
3203 {
3204retempty:
3205 /* backref was not set, match an empty string */
3206 *bytelen = 0;
3207 return TRUE;
3208 }
3209
3210 if (REG_MULTI)
3211 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003212 if (sub->list.multi[subidx].start.lnum < 0
3213 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003214 goto retempty;
3215 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003216 len = sub->list.multi[subidx].end.col
3217 - sub->list.multi[subidx].start.col;
3218 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003219 reginput, &len) == 0)
3220 {
3221 *bytelen = len;
3222 return TRUE;
3223 }
3224 }
3225 else
3226 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003227 if (sub->list.line[subidx].start == NULL
3228 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003229 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003230 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3231 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003232 {
3233 *bytelen = len;
3234 return TRUE;
3235 }
3236 }
3237 return FALSE;
3238}
3239
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240/*
3241 * Set all NFA nodes' list ID equal to -1.
3242 */
3243 static void
3244nfa_set_neg_listids(start)
3245 nfa_state_T *start;
3246{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003247 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003248 {
3249 start->lastlist = -1;
3250 nfa_set_neg_listids(start->out);
3251 nfa_set_neg_listids(start->out1);
3252 }
3253}
3254
3255/*
3256 * Set all NFA nodes' list ID equal to 0.
3257 */
3258 static void
3259nfa_set_null_listids(start)
3260 nfa_state_T *start;
3261{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003262 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263 {
3264 start->lastlist = 0;
3265 nfa_set_null_listids(start->out);
3266 nfa_set_null_listids(start->out1);
3267 }
3268}
3269
3270/*
3271 * Save list IDs for all NFA states in "list".
3272 */
3273 static void
3274nfa_save_listids(start, list)
3275 nfa_state_T *start;
3276 int *list;
3277{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003278 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003279 {
3280 list[abs(start->id)] = start->lastlist;
3281 start->lastlist = -1;
3282 nfa_save_listids(start->out, list);
3283 nfa_save_listids(start->out1, list);
3284 }
3285}
3286
3287/*
3288 * Restore list IDs from "list" to all NFA states.
3289 */
3290 static void
3291nfa_restore_listids(start, list)
3292 nfa_state_T *start;
3293 int *list;
3294{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003295 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 {
3297 start->lastlist = list[abs(start->id)];
3298 nfa_restore_listids(start->out, list);
3299 nfa_restore_listids(start->out1, list);
3300 }
3301}
3302
Bram Moolenaar423532e2013-05-29 21:14:42 +02003303 static int
3304nfa_re_num_cmp(val, op, pos)
3305 long_u val;
3306 int op;
3307 long_u pos;
3308{
3309 if (op == 1) return pos > val;
3310 if (op == 2) return pos < val;
3311 return val == pos;
3312}
3313
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003314static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3315
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316/*
3317 * Main matching routine.
3318 *
3319 * Run NFA to determine whether it matches reginput.
3320 *
3321 * Return TRUE if there is a match, FALSE otherwise.
3322 * Note: Caller must ensure that: start != NULL.
3323 */
3324 static int
3325nfa_regmatch(start, submatch, m)
3326 nfa_state_T *start;
3327 regsub_T *submatch;
3328 regsub_T *m;
3329{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003330 int result;
3331 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003333 int go_to_nextline = FALSE;
3334 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003335 nfa_list_T list[3];
3336 nfa_list_T *listtbl[2][2];
3337 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003338 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003339 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003340 nfa_list_T *thislist;
3341 nfa_list_T *nextlist;
3342 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003343 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003344#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003345 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346
3347 if (debug == NULL)
3348 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003349 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350 return FALSE;
3351 }
3352#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003353 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003355 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003356 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003357 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3358 list[0].len = nstate + 1;
3359 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3360 list[1].len = nstate + 1;
3361 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3362 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3364 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365
3366#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003367 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368 if (log_fd != NULL)
3369 {
3370 fprintf(log_fd, "**********************************\n");
3371 nfa_set_code(start->c);
3372 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3373 abs(start->id), code);
3374 fprintf(log_fd, "**********************************\n");
3375 }
3376 else
3377 {
3378 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3379 log_fd = stderr;
3380 }
3381#endif
3382
3383 thislist = &list[0];
3384 thislist->n = 0;
3385 nextlist = &list[1];
3386 nextlist->n = 0;
3387 neglist = &list[2];
3388 neglist->n = 0;
3389#ifdef ENABLE_LOG
3390 fprintf(log_fd, "(---) STARTSTATE\n");
3391#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003392 thislist->id = listid;
3393 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003394
3395 /* There are two cases when the NFA advances: 1. input char matches the
3396 * NFA node and 2. input char does not match the NFA node, but the next
3397 * node is NFA_NOT. The following macro calls addstate() according to
3398 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3399 listtbl[0][0] = NULL;
3400 listtbl[0][1] = neglist;
3401 listtbl[1][0] = nextlist;
3402 listtbl[1][1] = NULL;
3403#define ADD_POS_NEG_STATE(node) \
3404 ll = listtbl[result ? 1 : 0][node->negated]; \
3405 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003406 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003407
3408
3409 /*
3410 * Run for each character.
3411 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003412 for (;;)
3413 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003414 int curc;
3415 int clen;
3416
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417#ifdef FEAT_MBYTE
3418 if (has_mbyte)
3419 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003420 curc = (*mb_ptr2char)(reginput);
3421 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 }
3423 else
3424#endif
3425 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003426 curc = *reginput;
3427 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003429 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003430 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003431 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003432 go_to_nextline = FALSE;
3433 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003434
3435 /* swap lists */
3436 thislist = &list[flag];
3437 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003438 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439 listtbl[1][0] = nextlist;
3440 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003441 thislist->id = listid;
3442 nextlist->id = listid + 1;
3443 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444
3445#ifdef ENABLE_LOG
3446 fprintf(log_fd, "------------------------------------------\n");
3447 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003448 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003450 {
3451 int i;
3452
3453 for (i = 0; i < thislist->n; i++)
3454 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3455 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 fprintf(log_fd, "\n");
3457#endif
3458
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003459#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460 fprintf(debug, "\n-------------------\n");
3461#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003462 /*
3463 * If the state lists are empty we can stop.
3464 */
3465 if (thislist->n == 0 && neglist->n == 0)
3466 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003467
3468 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003469 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003470 {
3471 if (neglist->n > 0)
3472 {
3473 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003474 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003475 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003476 }
3477 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003478 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003480#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003481 nfa_set_code(t->state->c);
3482 fprintf(debug, "%s, ", code);
3483#endif
3484#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003485 {
3486 int col;
3487
3488 if (t->sub.in_use <= 0)
3489 col = -1;
3490 else if (REG_MULTI)
3491 col = t->sub.list.multi[0].start.col;
3492 else
3493 col = (int)(t->sub.list.line[0].start - regline);
3494 nfa_set_code(t->state->c);
3495 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3496 abs(t->state->id), (int)t->state->c, code, col);
3497 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498#endif
3499
3500 /*
3501 * Handle the possible codes of the current state.
3502 * The most important is NFA_MATCH.
3503 */
3504 switch (t->state->c)
3505 {
3506 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003507 {
3508 int j;
3509
Bram Moolenaar963fee22013-05-26 21:47:28 +02003510 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003511 submatch->in_use = t->sub.in_use;
3512 if (REG_MULTI)
3513 for (j = 0; j < submatch->in_use; j++)
3514 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003515 submatch->list.multi[j].start =
3516 t->sub.list.multi[j].start;
3517 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003518 }
3519 else
3520 for (j = 0; j < submatch->in_use; j++)
3521 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003522 submatch->list.line[j].start =
3523 t->sub.list.line[j].start;
3524 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003525 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003526#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003527 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003528#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003529 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003530 * states at this position. When the list of states is going
3531 * to be empty quit without advancing, so that "reginput" is
3532 * correct. */
3533 if (nextlist->n == 0 && neglist->n == 0)
3534 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003535 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003536 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003537
3538 case NFA_END_INVISIBLE:
3539 /* This is only encountered after a NFA_START_INVISIBLE node.
3540 * They surround a zero-width group, used with "\@=" and "\&".
3541 * If we got here, it means that the current "invisible" group
3542 * finished successfully, so return control to the parent
3543 * nfa_regmatch(). Submatches are stored in *m, and used in
3544 * the parent call. */
3545 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003546 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003547 else
3548 {
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003549 /* do not set submatches for \@! */
3550 if (!t->state->negated)
3551 /* TODO: only copy positions in use. */
3552 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003553 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003554 }
3555 break;
3556
3557 case NFA_START_INVISIBLE:
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003558 {
3559 char_u *save_reginput = reginput;
3560 char_u *save_regline = regline;
3561 int save_reglnum = reglnum;
3562 int save_nfa_match = nfa_match;
3563
3564 /* Call nfa_regmatch() to check if the current concat matches
3565 * at this position. The concat ends with the node
3566 * NFA_END_INVISIBLE */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003567 if (listids == NULL)
3568 {
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003569 listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003570 if (listids == NULL)
3571 {
3572 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3573 return 0;
3574 }
3575 }
3576#ifdef ENABLE_LOG
3577 if (log_fd != stderr)
3578 fclose(log_fd);
3579 log_fd = NULL;
3580#endif
3581 /* Have to clear the listid field of the NFA nodes, so that
3582 * nfa_regmatch() and addstate() can run properly after
3583 * recursion. */
3584 nfa_save_listids(start, listids);
3585 nfa_set_null_listids(start);
3586 result = nfa_regmatch(t->state->out, submatch, m);
3587 nfa_set_neg_listids(start);
3588 nfa_restore_listids(start, listids);
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003589
3590 /* restore position in input text */
3591 reginput = save_reginput;
3592 regline = save_regline;
3593 reglnum = save_reglnum;
3594 nfa_match = save_nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595
3596#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003597 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003598 if (log_fd != NULL)
3599 {
3600 fprintf(log_fd, "****************************\n");
3601 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3602 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3603 fprintf(log_fd, "****************************\n");
3604 }
3605 else
3606 {
3607 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3608 log_fd = stderr;
3609 }
3610#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003611 /* for \@! it is a match when result is FALSE */
3612 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003614 int j;
3615
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003616 /* Copy submatch info from the recursive call */
3617 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003618 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003620 t->sub.list.multi[j].start = m->list.multi[j].start;
3621 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003622 }
3623 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003624 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003625 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003626 t->sub.list.line[j].start = m->list.line[j].start;
3627 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003628 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003629 if (m->in_use > t->sub.in_use)
3630 t->sub.in_use = m->in_use;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003631
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003632 /* t->state->out1 is the corresponding END_INVISIBLE node;
3633 * Add it to the current list (zero-width match). */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003634 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003635 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637 break;
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003638 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639
3640 case NFA_BOL:
3641 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003642 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643 break;
3644
3645 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003646 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003647 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003648 break;
3649
3650 case NFA_BOW:
3651 {
3652 int bow = TRUE;
3653
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003654 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655 bow = FALSE;
3656#ifdef FEAT_MBYTE
3657 else if (has_mbyte)
3658 {
3659 int this_class;
3660
3661 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003662 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003663 if (this_class <= 1)
3664 bow = FALSE;
3665 else if (reg_prev_class() == this_class)
3666 bow = FALSE;
3667 }
3668#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003669 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003670 || (reginput > regline
3671 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 bow = FALSE;
3673 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003674 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003675 break;
3676 }
3677
3678 case NFA_EOW:
3679 {
3680 int eow = TRUE;
3681
3682 if (reginput == regline)
3683 eow = FALSE;
3684#ifdef FEAT_MBYTE
3685 else if (has_mbyte)
3686 {
3687 int this_class, prev_class;
3688
3689 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003690 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003691 prev_class = reg_prev_class();
3692 if (this_class == prev_class
3693 || prev_class == 0 || prev_class == 1)
3694 eow = FALSE;
3695 }
3696#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003697 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003698 || (reginput[0] != NUL
3699 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 eow = FALSE;
3701 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003702 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 break;
3704 }
3705
Bram Moolenaar4b780632013-05-31 22:14:52 +02003706 case NFA_BOF:
3707 if (reglnum == 0 && reginput == regline
3708 && (!REG_MULTI || reg_firstlnum == 1))
3709 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3710 break;
3711
3712 case NFA_EOF:
3713 if (reglnum == reg_maxline && curc == NUL)
3714 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3715 break;
3716
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003717#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003718 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003719 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003720 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003721 int len = 0;
3722 nfa_state_T *end;
3723 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003724 int cchars[MAX_MCO];
3725 int ccount = 0;
3726 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003727
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003728 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003729 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003730 if (utf_iscomposing(sta->c))
3731 {
3732 /* Only match composing character(s), ignore base
3733 * character. Used for ".{composing}" and "{composing}"
3734 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003735 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003736 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003737 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003738 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003739 /* If \Z was present, then ignore composing characters.
3740 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003741 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003742 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003743 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003744 else
3745 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003746 while (sta->c != NFA_END_COMPOSING)
3747 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003748 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003749
3750 /* Check base character matches first, unless ignored. */
3751 else if (len > 0 || mc == sta->c)
3752 {
3753 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003754 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003755 len += mb_char2len(mc);
3756 sta = sta->out;
3757 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003758
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003759 /* We don't care about the order of composing characters.
3760 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003761 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003762 {
3763 mc = mb_ptr2char(reginput + len);
3764 cchars[ccount++] = mc;
3765 len += mb_char2len(mc);
3766 if (ccount == MAX_MCO)
3767 break;
3768 }
3769
3770 /* Check that each composing char in the pattern matches a
3771 * composing char in the text. We do not check if all
3772 * composing chars are matched. */
3773 result = OK;
3774 while (sta->c != NFA_END_COMPOSING)
3775 {
3776 for (j = 0; j < ccount; ++j)
3777 if (cchars[j] == sta->c)
3778 break;
3779 if (j == ccount)
3780 {
3781 result = FAIL;
3782 break;
3783 }
3784 sta = sta->out;
3785 }
3786 }
3787 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003788 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003789
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003790 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003791 ADD_POS_NEG_STATE(end);
3792 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003793 }
3794#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795
3796 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003797 if (curc == NUL && !reg_line_lbr && REG_MULTI
3798 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003799 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003800 go_to_nextline = TRUE;
3801 /* Pass -1 for the offset, which means taking the position
3802 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003803 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003804 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003805 else if (curc == '\n' && reg_line_lbr)
3806 {
3807 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003808 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003809 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003810 break;
3811
3812 case NFA_CLASS_ALNUM:
3813 case NFA_CLASS_ALPHA:
3814 case NFA_CLASS_BLANK:
3815 case NFA_CLASS_CNTRL:
3816 case NFA_CLASS_DIGIT:
3817 case NFA_CLASS_GRAPH:
3818 case NFA_CLASS_LOWER:
3819 case NFA_CLASS_PRINT:
3820 case NFA_CLASS_PUNCT:
3821 case NFA_CLASS_SPACE:
3822 case NFA_CLASS_UPPER:
3823 case NFA_CLASS_XDIGIT:
3824 case NFA_CLASS_TAB:
3825 case NFA_CLASS_RETURN:
3826 case NFA_CLASS_BACKSPACE:
3827 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003828 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003829 ADD_POS_NEG_STATE(t->state);
3830 break;
3831
3832 case NFA_END_NEG_RANGE:
3833 /* This follows a series of negated nodes, like:
3834 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003835 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003836 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003837 break;
3838
3839 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003840 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003841 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003842 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003843 break;
3844
3845 /*
3846 * Character classes like \a for alpha, \d for digit etc.
3847 */
3848 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003849 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003850 ADD_POS_NEG_STATE(t->state);
3851 break;
3852
3853 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003854 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003855 ADD_POS_NEG_STATE(t->state);
3856 break;
3857
3858 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003859 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003860 ADD_POS_NEG_STATE(t->state);
3861 break;
3862
3863 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003864 result = !VIM_ISDIGIT(curc)
3865 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003866 ADD_POS_NEG_STATE(t->state);
3867 break;
3868
3869 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003870 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003871 ADD_POS_NEG_STATE(t->state);
3872 break;
3873
3874 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003875 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003876 ADD_POS_NEG_STATE(t->state);
3877 break;
3878
3879 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003880 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881 ADD_POS_NEG_STATE(t->state);
3882 break;
3883
3884 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003885 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003886 ADD_POS_NEG_STATE(t->state);
3887 break;
3888
3889 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003890 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003891 ADD_POS_NEG_STATE(t->state);
3892 break;
3893
3894 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003895 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003896 ADD_POS_NEG_STATE(t->state);
3897 break;
3898
3899 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003900 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003901 ADD_POS_NEG_STATE(t->state);
3902 break;
3903
3904 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003905 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003906 ADD_POS_NEG_STATE(t->state);
3907 break;
3908
3909 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003910 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003911 ADD_POS_NEG_STATE(t->state);
3912 break;
3913
3914 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003915 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003916 ADD_POS_NEG_STATE(t->state);
3917 break;
3918
3919 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003920 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003921 ADD_POS_NEG_STATE(t->state);
3922 break;
3923
3924 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003925 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003926 ADD_POS_NEG_STATE(t->state);
3927 break;
3928
3929 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003930 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003931 ADD_POS_NEG_STATE(t->state);
3932 break;
3933
3934 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003935 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003936 ADD_POS_NEG_STATE(t->state);
3937 break;
3938
3939 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003940 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003941 ADD_POS_NEG_STATE(t->state);
3942 break;
3943
3944 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003945 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003946 ADD_POS_NEG_STATE(t->state);
3947 break;
3948
3949 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003950 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003951 ADD_POS_NEG_STATE(t->state);
3952 break;
3953
3954 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003955 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003956 ADD_POS_NEG_STATE(t->state);
3957 break;
3958
3959 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003960 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003961 ADD_POS_NEG_STATE(t->state);
3962 break;
3963
3964 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003965 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003966 ADD_POS_NEG_STATE(t->state);
3967 break;
3968
3969 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003970 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003971 ADD_POS_NEG_STATE(t->state);
3972 break;
3973
3974 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003975 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003976 ADD_POS_NEG_STATE(t->state);
3977 break;
3978
Bram Moolenaar5714b802013-05-28 22:03:20 +02003979 case NFA_BACKREF1:
3980 case NFA_BACKREF2:
3981 case NFA_BACKREF3:
3982 case NFA_BACKREF4:
3983 case NFA_BACKREF5:
3984 case NFA_BACKREF6:
3985 case NFA_BACKREF7:
3986 case NFA_BACKREF8:
3987 case NFA_BACKREF9:
3988 /* \1 .. \9 */
3989 {
3990 int subidx = t->state->c - NFA_BACKREF1 + 1;
3991 int bytelen;
3992
3993 result = match_backref(&t->sub, subidx, &bytelen);
3994 if (result)
3995 {
3996 if (bytelen == 0)
3997 {
3998 /* empty match always works, add NFA_SKIP with zero to
3999 * be used next */
4000 addstate_here(thislist, t->state->out, &t->sub,
4001 &listidx);
4002 thislist->t[listidx + 1].count = 0;
4003 }
4004 else if (bytelen <= clen)
4005 {
4006 /* match current character, jump ahead to out of
4007 * NFA_SKIP */
4008 addstate(nextlist, t->state->out->out, &t->sub, clen);
4009#ifdef ENABLE_LOG
4010 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4011#endif
4012 }
4013 else
4014 {
4015 /* skip ofer the matched characters, set character
4016 * count in NFA_SKIP */
4017 addstate(nextlist, t->state->out, &t->sub, bytelen);
4018 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4019#ifdef ENABLE_LOG
4020 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4021#endif
4022 }
4023
4024 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004025 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004026 }
4027 case NFA_SKIP:
4028 /* charater of previous matching \1 .. \9 */
4029 if (t->count - clen <= 0)
4030 {
4031 /* end of match, go to what follows */
4032 addstate(nextlist, t->state->out, &t->sub, clen);
4033#ifdef ENABLE_LOG
4034 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4035#endif
4036 }
4037 else
4038 {
4039 /* add state again with decremented count */
4040 addstate(nextlist, t->state, &t->sub, 0);
4041 nextlist->t[nextlist->n - 1].count = t->count - clen;
4042#ifdef ENABLE_LOG
4043 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4044#endif
4045 }
4046 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004047
4048 case NFA_SKIP_CHAR:
4049 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004050 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004051 /* TODO: should not happen? */
4052 break;
4053
Bram Moolenaar423532e2013-05-29 21:14:42 +02004054 case NFA_LNUM:
4055 case NFA_LNUM_GT:
4056 case NFA_LNUM_LT:
4057 result = (REG_MULTI &&
4058 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4059 (long_u)(reglnum + reg_firstlnum)));
4060 if (result)
4061 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4062 break;
4063
4064 case NFA_COL:
4065 case NFA_COL_GT:
4066 case NFA_COL_LT:
4067 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4068 (long_u)(reginput - regline) + 1);
4069 if (result)
4070 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4071 break;
4072
4073 case NFA_VCOL:
4074 case NFA_VCOL_GT:
4075 case NFA_VCOL_LT:
4076 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4077 (long_u)win_linetabsize(
4078 reg_win == NULL ? curwin : reg_win,
4079 regline, (colnr_T)(reginput - regline)) + 1);
4080 if (result)
4081 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4082 break;
4083
4084 case NFA_CURSOR:
4085 result = (reg_win != NULL
4086 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4087 && ((colnr_T)(reginput - regline)
4088 == reg_win->w_cursor.col));
4089 if (result)
4090 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4091 break;
4092
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004093 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004094 {
4095 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004096
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004097 /* TODO: put this in #ifdef later */
4098 if (c < -256)
4099 EMSGN("INTERNAL: Negative state char: %ld", c);
4100 if (is_Magic(c))
4101 c = un_Magic(c);
4102 result = (c == curc);
4103
4104 if (!result && ireg_ic)
4105 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004106#ifdef FEAT_MBYTE
4107 /* If there is a composing character which is not being
4108 * ignored there can be no match. Match with composing
4109 * character uses NFA_COMPOSING above. */
4110 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004111 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004112 result = FALSE;
4113#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004114 ADD_POS_NEG_STATE(t->state);
4115 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004116 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117 }
4118
4119 } /* for (thislist = thislist; thislist->state; thislist++) */
4120
Bram Moolenaare23febd2013-05-26 18:40:14 +02004121 /* Look for the start of a match in the current position by adding the
4122 * start state to the list of states.
4123 * The first found match is the leftmost one, thus the order of states
4124 * matters!
4125 * Do not add the start state in recursive calls of nfa_regmatch(),
4126 * because recursive calls should only start in the first position.
4127 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004128 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004129 && reglnum == 0 && clen != 0
4130 && (ireg_maxcol == 0
4131 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004132 {
4133#ifdef ENABLE_LOG
4134 fprintf(log_fd, "(---) STARTSTATE\n");
4135#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004136 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004137 }
4138
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004139#ifdef ENABLE_LOG
4140 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004141 {
4142 int i;
4143
4144 for (i = 0; i < thislist->n; i++)
4145 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4146 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004147 fprintf(log_fd, "\n");
4148#endif
4149
4150nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004151 /* Advance to the next character, or advance to the next line, or
4152 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004153 if (clen != 0)
4154 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004155 else if (go_to_nextline)
4156 reg_nextline();
4157 else
4158 break;
4159 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004160
4161#ifdef ENABLE_LOG
4162 if (log_fd != stderr)
4163 fclose(log_fd);
4164 log_fd = NULL;
4165#endif
4166
4167theend:
4168 /* Free memory */
4169 vim_free(list[0].t);
4170 vim_free(list[1].t);
4171 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004172 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004173#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004174#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004175 fclose(debug);
4176#endif
4177
Bram Moolenaar963fee22013-05-26 21:47:28 +02004178 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004179}
4180
4181/*
4182 * Try match of "prog" with at regline["col"].
4183 * Returns 0 for failure, number of lines contained in the match otherwise.
4184 */
4185 static long
4186nfa_regtry(start, col)
4187 nfa_state_T *start;
4188 colnr_T col;
4189{
4190 int i;
4191 regsub_T sub, m;
4192#ifdef ENABLE_LOG
4193 FILE *f;
4194#endif
4195
4196 reginput = regline + col;
4197 need_clear_subexpr = TRUE;
4198
4199#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004200 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004201 if (f != NULL)
4202 {
4203 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4204 fprintf(f, " =======================================================\n");
4205#ifdef DEBUG
4206 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4207#endif
4208 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004209 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004210 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004211 fprintf(f, "\n\n");
4212 fclose(f);
4213 }
4214 else
4215 EMSG(_("Could not open temporary log file for writing "));
4216#endif
4217
4218 if (REG_MULTI)
4219 {
4220 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004221 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4222 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004223 }
4224 else
4225 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004226 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4227 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004228 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004229 sub.in_use = 0;
4230 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004231
4232 if (nfa_regmatch(start, &sub, &m) == FALSE)
4233 return 0;
4234
4235 cleanup_subexpr();
4236 if (REG_MULTI)
4237 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004238 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004239 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004240 reg_startpos[i] = sub.list.multi[i].start;
4241 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004242 }
4243
4244 if (reg_startpos[0].lnum < 0)
4245 {
4246 reg_startpos[0].lnum = 0;
4247 reg_startpos[0].col = col;
4248 }
4249 if (reg_endpos[0].lnum < 0)
4250 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004251 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004252 reg_endpos[0].lnum = reglnum;
4253 reg_endpos[0].col = (int)(reginput - regline);
4254 }
4255 else
4256 /* Use line number of "\ze". */
4257 reglnum = reg_endpos[0].lnum;
4258 }
4259 else
4260 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004261 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004262 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004263 reg_startp[i] = sub.list.line[i].start;
4264 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004265 }
4266
4267 if (reg_startp[0] == NULL)
4268 reg_startp[0] = regline + col;
4269 if (reg_endp[0] == NULL)
4270 reg_endp[0] = reginput;
4271 }
4272
4273 return 1 + reglnum;
4274}
4275
4276/*
4277 * Match a regexp against a string ("line" points to the string) or multiple
4278 * lines ("line" is NULL, use reg_getline()).
4279 *
4280 * Returns 0 for failure, number of lines contained in the match otherwise.
4281 */
4282 static long
4283nfa_regexec_both(line, col)
4284 char_u *line;
4285 colnr_T col; /* column to start looking for match */
4286{
4287 nfa_regprog_T *prog;
4288 long retval = 0L;
4289 int i;
4290
4291 if (REG_MULTI)
4292 {
4293 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4294 line = reg_getline((linenr_T)0); /* relative to the cursor */
4295 reg_startpos = reg_mmatch->startpos;
4296 reg_endpos = reg_mmatch->endpos;
4297 }
4298 else
4299 {
4300 prog = (nfa_regprog_T *)reg_match->regprog;
4301 reg_startp = reg_match->startp;
4302 reg_endp = reg_match->endp;
4303 }
4304
4305 /* Be paranoid... */
4306 if (prog == NULL || line == NULL)
4307 {
4308 EMSG(_(e_null));
4309 goto theend;
4310 }
4311
4312 /* If the start column is past the maximum column: no need to try. */
4313 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4314 goto theend;
4315
4316 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4317 if (prog->regflags & RF_ICASE)
4318 ireg_ic = TRUE;
4319 else if (prog->regflags & RF_NOICASE)
4320 ireg_ic = FALSE;
4321
4322#ifdef FEAT_MBYTE
4323 /* If pattern contains "\Z" overrule value of ireg_icombine */
4324 if (prog->regflags & RF_ICOMBINE)
4325 ireg_icombine = TRUE;
4326#endif
4327
4328 regline = line;
4329 reglnum = 0; /* relative to line */
4330
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004331 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004332 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004333 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004334
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004335 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336 for (i = 0; i < nstate; ++i)
4337 {
4338 prog->state[i].id = i;
4339 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004340 }
4341
4342 retval = nfa_regtry(prog->start, col);
4343
4344theend:
4345 return retval;
4346}
4347
4348/*
4349 * Compile a regular expression into internal code for the NFA matcher.
4350 * Returns the program in allocated space. Returns NULL for an error.
4351 */
4352 static regprog_T *
4353nfa_regcomp(expr, re_flags)
4354 char_u *expr;
4355 int re_flags;
4356{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004357 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004358 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004359 int *postfix;
4360
4361 if (expr == NULL)
4362 return NULL;
4363
4364#ifdef DEBUG
4365 nfa_regengine.expr = expr;
4366#endif
4367
4368 init_class_tab();
4369
4370 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4371 return NULL;
4372
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004373 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004374 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004375 postfix = re2post();
4376 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004377 {
4378 /* TODO: only give this error for debugging? */
4379 if (post_ptr >= post_end)
4380 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004381 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004382 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004383
4384 /*
4385 * In order to build the NFA, we parse the input regexp twice:
4386 * 1. first pass to count size (so we can allocate space)
4387 * 2. second to emit code
4388 */
4389#ifdef ENABLE_LOG
4390 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004391 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004392
4393 if (f != NULL)
4394 {
4395 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4396 fclose(f);
4397 }
4398 }
4399#endif
4400
4401 /*
4402 * PASS 1
4403 * Count number of NFA states in "nstate". Do not build the NFA.
4404 */
4405 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004406
4407 /* Space for compiled regexp */
4408 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4409 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4410 if (prog == NULL)
4411 goto fail;
4412 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004413 state_ptr = prog->state;
4414
4415 /*
4416 * PASS 2
4417 * Build the NFA
4418 */
4419 prog->start = post2nfa(postfix, post_ptr, FALSE);
4420 if (prog->start == NULL)
4421 goto fail;
4422
4423 prog->regflags = regflags;
4424 prog->engine = &nfa_regengine;
4425 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004426 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004427 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004428 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004429#ifdef ENABLE_LOG
4430 nfa_postfix_dump(expr, OK);
4431 nfa_dump(prog);
4432#endif
4433
4434out:
4435 vim_free(post_start);
4436 post_start = post_ptr = post_end = NULL;
4437 state_ptr = NULL;
4438 return (regprog_T *)prog;
4439
4440fail:
4441 vim_free(prog);
4442 prog = NULL;
4443#ifdef ENABLE_LOG
4444 nfa_postfix_dump(expr, FAIL);
4445#endif
4446#ifdef DEBUG
4447 nfa_regengine.expr = NULL;
4448#endif
4449 goto out;
4450}
4451
4452
4453/*
4454 * Match a regexp against a string.
4455 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4456 * Uses curbuf for line count and 'iskeyword'.
4457 *
4458 * Return TRUE if there is a match, FALSE if not.
4459 */
4460 static int
4461nfa_regexec(rmp, line, col)
4462 regmatch_T *rmp;
4463 char_u *line; /* string to match against */
4464 colnr_T col; /* column to start looking for match */
4465{
4466 reg_match = rmp;
4467 reg_mmatch = NULL;
4468 reg_maxline = 0;
4469 reg_line_lbr = FALSE;
4470 reg_buf = curbuf;
4471 reg_win = NULL;
4472 ireg_ic = rmp->rm_ic;
4473#ifdef FEAT_MBYTE
4474 ireg_icombine = FALSE;
4475#endif
4476 ireg_maxcol = 0;
4477 return (nfa_regexec_both(line, col) != 0);
4478}
4479
4480#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4481 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4482
4483static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4484
4485/*
4486 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4487 */
4488 static int
4489nfa_regexec_nl(rmp, line, col)
4490 regmatch_T *rmp;
4491 char_u *line; /* string to match against */
4492 colnr_T col; /* column to start looking for match */
4493{
4494 reg_match = rmp;
4495 reg_mmatch = NULL;
4496 reg_maxline = 0;
4497 reg_line_lbr = TRUE;
4498 reg_buf = curbuf;
4499 reg_win = NULL;
4500 ireg_ic = rmp->rm_ic;
4501#ifdef FEAT_MBYTE
4502 ireg_icombine = FALSE;
4503#endif
4504 ireg_maxcol = 0;
4505 return (nfa_regexec_both(line, col) != 0);
4506}
4507#endif
4508
4509
4510/*
4511 * Match a regexp against multiple lines.
4512 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4513 * Uses curbuf for line count and 'iskeyword'.
4514 *
4515 * Return zero if there is no match. Return number of lines contained in the
4516 * match otherwise.
4517 *
4518 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4519 *
4520 * ! Also NOTE : match may actually be in another line. e.g.:
4521 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4522 *
4523 * +-------------------------+
4524 * |a |
4525 * |b |
4526 * |c |
4527 * | |
4528 * +-------------------------+
4529 *
4530 * then nfa_regexec_multi() returns 3. while the original
4531 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4532 *
4533 * FIXME if this behavior is not compatible.
4534 */
4535 static long
4536nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4537 regmmatch_T *rmp;
4538 win_T *win; /* window in which to search or NULL */
4539 buf_T *buf; /* buffer in which to search */
4540 linenr_T lnum; /* nr of line to start looking for match */
4541 colnr_T col; /* column to start looking for match */
4542 proftime_T *tm UNUSED; /* timeout limit or NULL */
4543{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004544 reg_match = NULL;
4545 reg_mmatch = rmp;
4546 reg_buf = buf;
4547 reg_win = win;
4548 reg_firstlnum = lnum;
4549 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4550 reg_line_lbr = FALSE;
4551 ireg_ic = rmp->rmm_ic;
4552#ifdef FEAT_MBYTE
4553 ireg_icombine = FALSE;
4554#endif
4555 ireg_maxcol = rmp->rmm_maxcol;
4556
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004557 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004558}
4559
4560#ifdef DEBUG
4561# undef ENABLE_LOG
4562#endif