blob: fffe4a0cc297c51875422bdb68ff40693b7f2f44 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020032enum
33{
34 NFA_SPLIT = -1024,
35 NFA_MATCH,
36 NFA_SKIP_CHAR, /* matches a 0-length char */
37 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
38
39 NFA_CONCAT,
40 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020041 NFA_STAR, /* greedy * */
42 NFA_STAR_NONGREEDY, /* non-greedy * */
43 NFA_QUEST, /* greedy \? */
44 NFA_QUEST_NONGREEDY, /* non-greedy \? */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020045 NFA_NOT, /* used for [^ab] negated char ranges */
46
47 NFA_BOL, /* ^ Begin line */
48 NFA_EOL, /* $ End line */
49 NFA_BOW, /* \< Begin word */
50 NFA_EOW, /* \> End word */
51 NFA_BOF, /* \%^ Begin file */
52 NFA_EOF, /* \%$ End file */
53 NFA_NEWL,
54 NFA_ZSTART, /* Used for \zs */
55 NFA_ZEND, /* Used for \ze */
56 NFA_NOPEN, /* Start of subexpression marked with \%( */
57 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
58 NFA_START_INVISIBLE,
59 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020060 NFA_COMPOSING, /* Next nodes in NFA are part of the
61 composing multibyte char */
62 NFA_END_COMPOSING, /* End of a composing char in the NFA */
63
64 /* The following are used only in the postfix form, not in the NFA */
65 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
66 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
67 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
68 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
69 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
70
Bram Moolenaar5714b802013-05-28 22:03:20 +020071 NFA_BACKREF1, /* \1 */
72 NFA_BACKREF2, /* \2 */
73 NFA_BACKREF3, /* \3 */
74 NFA_BACKREF4, /* \4 */
75 NFA_BACKREF5, /* \5 */
76 NFA_BACKREF6, /* \6 */
77 NFA_BACKREF7, /* \7 */
78 NFA_BACKREF8, /* \8 */
79 NFA_BACKREF9, /* \9 */
80 NFA_SKIP, /* Skip characters */
81
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020082 NFA_MOPEN,
83 NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
84
85 /* NFA_FIRST_NL */
86 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
87 NFA_ANYOF, /* Match any character in this string. */
88 NFA_ANYBUT, /* Match any character not in this string. */
89 NFA_IDENT, /* Match identifier char */
90 NFA_SIDENT, /* Match identifier char but no digit */
91 NFA_KWORD, /* Match keyword char */
92 NFA_SKWORD, /* Match word char but no digit */
93 NFA_FNAME, /* Match file name char */
94 NFA_SFNAME, /* Match file name char but no digit */
95 NFA_PRINT, /* Match printable char */
96 NFA_SPRINT, /* Match printable char but no digit */
97 NFA_WHITE, /* Match whitespace char */
98 NFA_NWHITE, /* Match non-whitespace char */
99 NFA_DIGIT, /* Match digit char */
100 NFA_NDIGIT, /* Match non-digit char */
101 NFA_HEX, /* Match hex char */
102 NFA_NHEX, /* Match non-hex char */
103 NFA_OCTAL, /* Match octal char */
104 NFA_NOCTAL, /* Match non-octal char */
105 NFA_WORD, /* Match word char */
106 NFA_NWORD, /* Match non-word char */
107 NFA_HEAD, /* Match head char */
108 NFA_NHEAD, /* Match non-head char */
109 NFA_ALPHA, /* Match alpha char */
110 NFA_NALPHA, /* Match non-alpha char */
111 NFA_LOWER, /* Match lowercase char */
112 NFA_NLOWER, /* Match non-lowercase char */
113 NFA_UPPER, /* Match uppercase char */
114 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200115
116 NFA_CURSOR, /* Match cursor pos */
117 NFA_LNUM, /* Match line number */
118 NFA_LNUM_GT, /* Match > line number */
119 NFA_LNUM_LT, /* Match < line number */
120 NFA_COL, /* Match cursor column */
121 NFA_COL_GT, /* Match > cursor column */
122 NFA_COL_LT, /* Match < cursor column */
123 NFA_VCOL, /* Match cursor virtual column */
124 NFA_VCOL_GT, /* Match > cursor virtual column */
125 NFA_VCOL_LT, /* Match < cursor virtual column */
126
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200127 NFA_FIRST_NL = NFA_ANY + ADD_NL,
128 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
129
130 /* Character classes [:alnum:] etc */
131 NFA_CLASS_ALNUM,
132 NFA_CLASS_ALPHA,
133 NFA_CLASS_BLANK,
134 NFA_CLASS_CNTRL,
135 NFA_CLASS_DIGIT,
136 NFA_CLASS_GRAPH,
137 NFA_CLASS_LOWER,
138 NFA_CLASS_PRINT,
139 NFA_CLASS_PUNCT,
140 NFA_CLASS_SPACE,
141 NFA_CLASS_UPPER,
142 NFA_CLASS_XDIGIT,
143 NFA_CLASS_TAB,
144 NFA_CLASS_RETURN,
145 NFA_CLASS_BACKSPACE,
146 NFA_CLASS_ESCAPE
147};
148
149/* Keep in sync with classchars. */
150static int nfa_classcodes[] = {
151 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
152 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
153 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
154 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
155 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
156 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
157 NFA_UPPER, NFA_NUPPER
158};
159
160static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
161
162/*
163 * NFA errors can be of 3 types:
164 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
165 * silently and revert the to backtracking engine.
166 * syntax_error = FALSE;
167 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
168 * The NFA engine displays an error message, and nothing else happens.
169 * syntax_error = TRUE
170 * *** Unsupported features, when the input regexp uses an operator that is not
171 * implemented in the NFA. The NFA engine fails silently, and reverts to the
172 * old backtracking engine.
173 * syntax_error = FALSE
174 * "The NFA fails" means that "compiling the regexp with the NFA fails":
175 * nfa_regcomp() returns FAIL.
176 */
177static int syntax_error = FALSE;
178
179/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200180static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200181
Bram Moolenaar428e9872013-05-30 17:05:39 +0200182/* NFA regexp \1 .. \9 encountered. */
183static int nfa_has_backref;
184
Bram Moolenaar963fee22013-05-26 21:47:28 +0200185/* Number of sub expressions actually being used during execution. 1 if only
186 * the whole match (subexpr 0) is used. */
187static int nfa_nsubexpr;
188
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200189static int *post_start; /* holds the postfix form of r.e. */
190static int *post_end;
191static int *post_ptr;
192
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200193static int nstate; /* Number of states in the NFA. Also used when
194 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200195static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200196
197
198static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
199static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
200static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200201static int nfa_regatom __ARGS((void));
202static int nfa_regpiece __ARGS((void));
203static int nfa_regconcat __ARGS((void));
204static int nfa_regbranch __ARGS((void));
205static int nfa_reg __ARGS((int paren));
206#ifdef DEBUG
207static void nfa_set_code __ARGS((int c));
208static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200209static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
210static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200211static void nfa_dump __ARGS((nfa_regprog_T *prog));
212#endif
213static int *re2post __ARGS((void));
214static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
215static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
216static int check_char_class __ARGS((int class, int c));
217static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200218static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
219static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200220static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
221static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200222static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200223static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
224static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
225static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
226static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
227static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
228
229/* helper functions used when doing re2post() ... regatom() parsing */
230#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200231 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200232 return FAIL; \
233 *post_ptr++ = c; \
234 } while (0)
235
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236/*
237 * Initialize internal variables before NFA compilation.
238 * Return OK on success, FAIL otherwise.
239 */
240 static int
241nfa_regcomp_start(expr, re_flags)
242 char_u *expr;
243 int re_flags; /* see vim_regcomp() */
244{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200245 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200246 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247
248 nstate = 0;
249 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200250 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200251 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200253 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200254 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200255 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200256
257 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200258 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200259
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260 post_start = (int *)lalloc(postfix_size, TRUE);
261 if (post_start == NULL)
262 return FAIL;
263 vim_memset(post_start, 0, postfix_size);
264 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200265 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200267 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268
269 regcomp_start(expr, re_flags);
270
271 return OK;
272}
273
274/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200275 * Allocate more space for post_start. Called when
276 * running above the estimated number of states.
277 */
278 static int
279realloc_post_list()
280{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200281 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200282 int new_max = nstate_max + 1000;
283 int *new_start;
284 int *old_start;
285
286 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
287 if (new_start == NULL)
288 return FAIL;
289 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
290 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
291 old_start = post_start;
292 post_start = new_start;
293 post_ptr = new_start + (post_ptr - old_start);
294 post_end = post_start + new_max;
295 vim_free(old_start);
296 return OK;
297}
298
299/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300 * Search between "start" and "end" and try to recognize a
301 * character class in expanded form. For example [0-9].
302 * On success, return the id the character class to be emitted.
303 * On failure, return 0 (=FAIL)
304 * Start points to the first char of the range, while end should point
305 * to the closing brace.
306 */
307 static int
308nfa_recognize_char_class(start, end, extra_newl)
309 char_u *start;
310 char_u *end;
311 int extra_newl;
312{
313 int i;
314 /* Each of these variables takes up a char in "config[]",
315 * in the order they are here. */
316 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
317 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
318 char_u *p;
319#define NCONFIGS 16
320 int classid[NCONFIGS] = {
321 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
322 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
323 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
324 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
325 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200326 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200327 char_u config[NCONFIGS][9] = {
328 "000000100", /* digit */
329 "100000100", /* non digit */
330 "011000100", /* hex-digit */
331 "111000100", /* non hex-digit */
332 "000001000", /* octal-digit */
333 "100001000", /* [^0-7] */
334 "000110110", /* [0-9A-Za-z_] */
335 "100110110", /* [^0-9A-Za-z_] */
336 "000110010", /* head of word */
337 "100110010", /* not head of word */
338 "000110000", /* alphabetic char a-z */
339 "100110000", /* non alphabetic char */
340 "000100000", /* lowercase letter */
341 "100100000", /* non lowercase */
342 "000010000", /* uppercase */
343 "100010000" /* non uppercase */
344 };
345
346 if (extra_newl == TRUE)
347 newl = TRUE;
348
349 if (*end != ']')
350 return FAIL;
351 p = start;
352 if (*p == '^')
353 {
354 not = TRUE;
355 p ++;
356 }
357
358 while (p < end)
359 {
360 if (p + 2 < end && *(p + 1) == '-')
361 {
362 switch (*p)
363 {
364 case '0':
365 if (*(p + 2) == '9')
366 {
367 o9 = TRUE;
368 break;
369 }
370 else
371 if (*(p + 2) == '7')
372 {
373 o7 = TRUE;
374 break;
375 }
376 case 'a':
377 if (*(p + 2) == 'z')
378 {
379 az = TRUE;
380 break;
381 }
382 else
383 if (*(p + 2) == 'f')
384 {
385 af = TRUE;
386 break;
387 }
388 case 'A':
389 if (*(p + 2) == 'Z')
390 {
391 AZ = TRUE;
392 break;
393 }
394 else
395 if (*(p + 2) == 'F')
396 {
397 AF = TRUE;
398 break;
399 }
400 /* FALLTHROUGH */
401 default:
402 return FAIL;
403 }
404 p += 3;
405 }
406 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
407 {
408 newl = TRUE;
409 p += 2;
410 }
411 else if (*p == '_')
412 {
413 underscore = TRUE;
414 p ++;
415 }
416 else if (*p == '\n')
417 {
418 newl = TRUE;
419 p ++;
420 }
421 else
422 return FAIL;
423 } /* while (p < end) */
424
425 if (p != end)
426 return FAIL;
427
428 /* build the config that represents the ranges we gathered */
429 STRCPY(myconfig, "000000000");
430 if (not == TRUE)
431 myconfig[0] = '1';
432 if (af == TRUE)
433 myconfig[1] = '1';
434 if (AF == TRUE)
435 myconfig[2] = '1';
436 if (az == TRUE)
437 myconfig[3] = '1';
438 if (AZ == TRUE)
439 myconfig[4] = '1';
440 if (o7 == TRUE)
441 myconfig[5] = '1';
442 if (o9 == TRUE)
443 myconfig[6] = '1';
444 if (underscore == TRUE)
445 myconfig[7] = '1';
446 if (newl == TRUE)
447 {
448 myconfig[8] = '1';
449 extra_newl = ADD_NL;
450 }
451 /* try to recognize character classes */
452 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200453 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200454 return classid[i] + extra_newl;
455
456 /* fallthrough => no success so far */
457 return FAIL;
458
459#undef NCONFIGS
460}
461
462/*
463 * Produce the bytes for equivalence class "c".
464 * Currently only handles latin1, latin9 and utf-8.
465 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
466 * equivalent to 'a OR b OR c'
467 *
468 * NOTE! When changing this function, also update reg_equi_class()
469 */
470 static int
471nfa_emit_equi_class(c, neg)
472 int c;
473 int neg;
474{
475 int first = TRUE;
476 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
477#define EMIT2(c) \
478 EMIT(c); \
479 if (neg == TRUE) { \
480 EMIT(NFA_NOT); \
481 } \
482 if (first == FALSE) \
483 EMIT(glue); \
484 else \
485 first = FALSE; \
486
487#ifdef FEAT_MBYTE
488 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
489 || STRCMP(p_enc, "iso-8859-15") == 0)
490#endif
491 {
492 switch (c)
493 {
494 case 'A': case '\300': case '\301': case '\302':
495 case '\303': case '\304': case '\305':
496 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
497 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
498 EMIT2('\305');
499 return OK;
500
501 case 'C': case '\307':
502 EMIT2('C'); EMIT2('\307');
503 return OK;
504
505 case 'E': case '\310': case '\311': case '\312': case '\313':
506 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
507 EMIT2('\312'); EMIT2('\313');
508 return OK;
509
510 case 'I': case '\314': case '\315': case '\316': case '\317':
511 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
512 EMIT2('\316'); EMIT2('\317');
513 return OK;
514
515 case 'N': case '\321':
516 EMIT2('N'); EMIT2('\321');
517 return OK;
518
519 case 'O': case '\322': case '\323': case '\324': case '\325':
520 case '\326':
521 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
522 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
523 return OK;
524
525 case 'U': case '\331': case '\332': case '\333': case '\334':
526 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
527 EMIT2('\333'); EMIT2('\334');
528 return OK;
529
530 case 'Y': case '\335':
531 EMIT2('Y'); EMIT2('\335');
532 return OK;
533
534 case 'a': case '\340': case '\341': case '\342':
535 case '\343': case '\344': case '\345':
536 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
537 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
538 EMIT2('\345');
539 return OK;
540
541 case 'c': case '\347':
542 EMIT2('c'); EMIT2('\347');
543 return OK;
544
545 case 'e': case '\350': case '\351': case '\352': case '\353':
546 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
547 EMIT2('\352'); EMIT2('\353');
548 return OK;
549
550 case 'i': case '\354': case '\355': case '\356': case '\357':
551 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
552 EMIT2('\356'); EMIT2('\357');
553 return OK;
554
555 case 'n': case '\361':
556 EMIT2('n'); EMIT2('\361');
557 return OK;
558
559 case 'o': case '\362': case '\363': case '\364': case '\365':
560 case '\366':
561 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
562 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
563 return OK;
564
565 case 'u': case '\371': case '\372': case '\373': case '\374':
566 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
567 EMIT2('\373'); EMIT2('\374');
568 return OK;
569
570 case 'y': case '\375': case '\377':
571 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
572 return OK;
573
574 default:
575 return FAIL;
576 }
577 }
578
579 EMIT(c);
580 return OK;
581#undef EMIT2
582}
583
584/*
585 * Code to parse regular expression.
586 *
587 * We try to reuse parsing functions in regexp.c to
588 * minimize surprise and keep the syntax consistent.
589 */
590
591/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592 * Parse the lowest level.
593 *
594 * An atom can be one of a long list of items. Many atoms match one character
595 * in the text. It is often an ordinary character or a character class.
596 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
597 * is only for syntax highlighting.
598 *
599 * atom ::= ordinary-atom
600 * or \( pattern \)
601 * or \%( pattern \)
602 * or \z( pattern \)
603 */
604 static int
605nfa_regatom()
606{
607 int c;
608 int charclass;
609 int equiclass;
610 int collclass;
611 int got_coll_char;
612 char_u *p;
613 char_u *endp;
614#ifdef FEAT_MBYTE
615 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200616#endif
617 int extra = 0;
618 int first;
619 int emit_range;
620 int negated;
621 int result;
622 int startc = -1;
623 int endc = -1;
624 int oldstartc = -1;
625 int cpo_lit; /* 'cpoptions' contains 'l' flag */
626 int cpo_bsl; /* 'cpoptions' contains '\' flag */
627 int glue; /* ID that will "glue" nodes together */
628
629 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
630 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
631
632 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200633 switch (c)
634 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200635 case NUL:
636 syntax_error = TRUE;
637 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
638
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200639 case Magic('^'):
640 EMIT(NFA_BOL);
641 break;
642
643 case Magic('$'):
644 EMIT(NFA_EOL);
645#if defined(FEAT_SYN_HL) || defined(PROTO)
646 had_eol = TRUE;
647#endif
648 break;
649
650 case Magic('<'):
651 EMIT(NFA_BOW);
652 break;
653
654 case Magic('>'):
655 EMIT(NFA_EOW);
656 break;
657
658 case Magic('_'):
659 c = no_Magic(getchr());
660 if (c == '^') /* "\_^" is start-of-line */
661 {
662 EMIT(NFA_BOL);
663 break;
664 }
665 if (c == '$') /* "\_$" is end-of-line */
666 {
667 EMIT(NFA_EOL);
668#if defined(FEAT_SYN_HL) || defined(PROTO)
669 had_eol = TRUE;
670#endif
671 break;
672 }
673
674 extra = ADD_NL;
675
676 /* "\_[" is collection plus newline */
677 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200678 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679
680 /* "\_x" is character class plus newline */
681 /*FALLTHROUGH*/
682
683 /*
684 * Character classes.
685 */
686 case Magic('.'):
687 case Magic('i'):
688 case Magic('I'):
689 case Magic('k'):
690 case Magic('K'):
691 case Magic('f'):
692 case Magic('F'):
693 case Magic('p'):
694 case Magic('P'):
695 case Magic('s'):
696 case Magic('S'):
697 case Magic('d'):
698 case Magic('D'):
699 case Magic('x'):
700 case Magic('X'):
701 case Magic('o'):
702 case Magic('O'):
703 case Magic('w'):
704 case Magic('W'):
705 case Magic('h'):
706 case Magic('H'):
707 case Magic('a'):
708 case Magic('A'):
709 case Magic('l'):
710 case Magic('L'):
711 case Magic('u'):
712 case Magic('U'):
713 p = vim_strchr(classchars, no_Magic(c));
714 if (p == NULL)
715 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200716 EMSGN("INTERNAL: Unknown character class char: %ld", c);
717 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200718 }
719#ifdef FEAT_MBYTE
720 /* When '.' is followed by a composing char ignore the dot, so that
721 * the composing char is matched here. */
722 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
723 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200724 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200725 c = getchr();
726 goto nfa_do_multibyte;
727 }
728#endif
729 EMIT(nfa_classcodes[p - classchars]);
730 if (extra == ADD_NL)
731 {
732 EMIT(NFA_NEWL);
733 EMIT(NFA_OR);
734 regflags |= RF_HASNL;
735 }
736 break;
737
738 case Magic('n'):
739 if (reg_string)
740 /* In a string "\n" matches a newline character. */
741 EMIT(NL);
742 else
743 {
744 /* In buffer text "\n" matches the end of a line. */
745 EMIT(NFA_NEWL);
746 regflags |= RF_HASNL;
747 }
748 break;
749
750 case Magic('('):
751 if (nfa_reg(REG_PAREN) == FAIL)
752 return FAIL; /* cascaded error */
753 break;
754
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200755 case Magic('|'):
756 case Magic('&'):
757 case Magic(')'):
758 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200759 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200760 return FAIL;
761
762 case Magic('='):
763 case Magic('?'):
764 case Magic('+'):
765 case Magic('@'):
766 case Magic('*'):
767 case Magic('{'):
768 /* these should follow an atom, not form an atom */
769 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200770 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200771 return FAIL;
772
773 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200774 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200775 return FAIL;
776
Bram Moolenaar428e9872013-05-30 17:05:39 +0200777 case Magic('1'):
778 case Magic('2'):
779 case Magic('3'):
780 case Magic('4'):
781 case Magic('5'):
782 case Magic('6'):
783 case Magic('7'):
784 case Magic('8'):
785 case Magic('9'):
786 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
787 nfa_has_backref = TRUE;
788 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200789
790 case Magic('z'):
791 c = no_Magic(getchr());
792 switch (c)
793 {
794 case 's':
795 EMIT(NFA_ZSTART);
796 break;
797 case 'e':
798 EMIT(NFA_ZEND);
799 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200800 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200801 case '1':
802 case '2':
803 case '3':
804 case '4':
805 case '5':
806 case '6':
807 case '7':
808 case '8':
809 case '9':
810 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200811 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 return FAIL;
813 default:
814 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200815 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 no_Magic(c));
817 return FAIL;
818 }
819 break;
820
821 case Magic('%'):
822 c = no_Magic(getchr());
823 switch (c)
824 {
825 /* () without a back reference */
826 case '(':
827 if (nfa_reg(REG_NPAREN) == FAIL)
828 return FAIL;
829 EMIT(NFA_NOPEN);
830 break;
831
832 case 'd': /* %d123 decimal */
833 case 'o': /* %o123 octal */
834 case 'x': /* %xab hex 2 */
835 case 'u': /* %uabcd hex 4 */
836 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200837 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200838 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200839
Bram Moolenaar47196582013-05-25 22:04:23 +0200840 switch (c)
841 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200842 case 'd': nr = getdecchrs(); break;
843 case 'o': nr = getoctchrs(); break;
844 case 'x': nr = gethexchrs(2); break;
845 case 'u': nr = gethexchrs(4); break;
846 case 'U': nr = gethexchrs(8); break;
847 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200848 }
849
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200850 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200851 EMSG2_RET_FAIL(
852 _("E678: Invalid character after %s%%[dxouU]"),
853 reg_magic == MAGIC_ALL);
854 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200855 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200856 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200857 break;
858
859 /* Catch \%^ and \%$ regardless of where they appear in the
860 * pattern -- regardless of whether or not it makes sense. */
861 case '^':
862 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200863 break;
864
865 case '$':
866 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867 break;
868
869 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200870 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200871 break;
872
873 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200874 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 return FAIL;
876 break;
877
878 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200879 /* TODO: \%[abc] not supported yet */
880 return FAIL;
881
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200882 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200883 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200884 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200885 int cmp = c;
886
887 if (c == '<' || c == '>')
888 c = getchr();
889 while (VIM_ISDIGIT(c))
890 {
891 n = n * 10 + (c - '0');
892 c = getchr();
893 }
894 if (c == 'l' || c == 'c' || c == 'v')
895 {
896 EMIT(n);
897 if (c == 'l')
898 EMIT(cmp == '<' ? NFA_LNUM_LT :
899 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
900 else if (c == 'c')
901 EMIT(cmp == '<' ? NFA_COL_LT :
902 cmp == '>' ? NFA_COL_GT : NFA_COL);
903 else
904 EMIT(cmp == '<' ? NFA_VCOL_LT :
905 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
906 break;
907 }
908 else if (c == '\'')
909 /* TODO: \%'m not supported yet */
910 return FAIL;
911 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200912 syntax_error = TRUE;
913 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
914 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200915 return FAIL;
916 }
917 break;
918
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200919 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200920collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200921 /*
922 * Glue is emitted between several atoms from the [].
923 * It is either NFA_OR, or NFA_CONCAT.
924 *
925 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
926 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
927 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
928 * notation)
929 *
930 */
931
932
933/* Emit negation atoms, if needed.
934 * The CONCAT below merges the NOT with the previous node. */
935#define TRY_NEG() \
936 if (negated == TRUE) \
937 { \
938 EMIT(NFA_NOT); \
939 }
940
941/* Emit glue between important nodes : CONCAT or OR. */
942#define EMIT_GLUE() \
943 if (first == FALSE) \
944 EMIT(glue); \
945 else \
946 first = FALSE;
947
948 p = regparse;
949 endp = skip_anyof(p);
950 if (*endp == ']')
951 {
952 /*
953 * Try to reverse engineer character classes. For example,
954 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
955 * and perform the necessary substitutions in the NFA.
956 */
957 result = nfa_recognize_char_class(regparse, endp,
958 extra == ADD_NL);
959 if (result != FAIL)
960 {
961 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
962 EMIT(result);
963 else /* must be char class + newline */
964 {
965 EMIT(result - ADD_NL);
966 EMIT(NFA_NEWL);
967 EMIT(NFA_OR);
968 }
969 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200970 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 return OK;
972 }
973 /*
974 * Failed to recognize a character class. Use the simple
975 * version that turns [abc] into 'a' OR 'b' OR 'c'
976 */
977 startc = endc = oldstartc = -1;
978 first = TRUE; /* Emitting first atom in this sequence? */
979 negated = FALSE;
980 glue = NFA_OR;
981 if (*regparse == '^') /* negated range */
982 {
983 negated = TRUE;
984 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200985 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200986 }
987 if (*regparse == '-')
988 {
989 startc = '-';
990 EMIT(startc);
991 TRY_NEG();
992 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +0200993 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200994 }
995 /* Emit the OR branches for each character in the [] */
996 emit_range = FALSE;
997 while (regparse < endp)
998 {
999 oldstartc = startc;
1000 startc = -1;
1001 got_coll_char = FALSE;
1002 if (*regparse == '[')
1003 {
1004 /* Check for [: :], [= =], [. .] */
1005 equiclass = collclass = 0;
1006 charclass = get_char_class(&regparse);
1007 if (charclass == CLASS_NONE)
1008 {
1009 equiclass = get_equi_class(&regparse);
1010 if (equiclass == 0)
1011 collclass = get_coll_element(&regparse);
1012 }
1013
1014 /* Character class like [:alpha:] */
1015 if (charclass != CLASS_NONE)
1016 {
1017 switch (charclass)
1018 {
1019 case CLASS_ALNUM:
1020 EMIT(NFA_CLASS_ALNUM);
1021 break;
1022 case CLASS_ALPHA:
1023 EMIT(NFA_CLASS_ALPHA);
1024 break;
1025 case CLASS_BLANK:
1026 EMIT(NFA_CLASS_BLANK);
1027 break;
1028 case CLASS_CNTRL:
1029 EMIT(NFA_CLASS_CNTRL);
1030 break;
1031 case CLASS_DIGIT:
1032 EMIT(NFA_CLASS_DIGIT);
1033 break;
1034 case CLASS_GRAPH:
1035 EMIT(NFA_CLASS_GRAPH);
1036 break;
1037 case CLASS_LOWER:
1038 EMIT(NFA_CLASS_LOWER);
1039 break;
1040 case CLASS_PRINT:
1041 EMIT(NFA_CLASS_PRINT);
1042 break;
1043 case CLASS_PUNCT:
1044 EMIT(NFA_CLASS_PUNCT);
1045 break;
1046 case CLASS_SPACE:
1047 EMIT(NFA_CLASS_SPACE);
1048 break;
1049 case CLASS_UPPER:
1050 EMIT(NFA_CLASS_UPPER);
1051 break;
1052 case CLASS_XDIGIT:
1053 EMIT(NFA_CLASS_XDIGIT);
1054 break;
1055 case CLASS_TAB:
1056 EMIT(NFA_CLASS_TAB);
1057 break;
1058 case CLASS_RETURN:
1059 EMIT(NFA_CLASS_RETURN);
1060 break;
1061 case CLASS_BACKSPACE:
1062 EMIT(NFA_CLASS_BACKSPACE);
1063 break;
1064 case CLASS_ESCAPE:
1065 EMIT(NFA_CLASS_ESCAPE);
1066 break;
1067 }
1068 TRY_NEG();
1069 EMIT_GLUE();
1070 continue;
1071 }
1072 /* Try equivalence class [=a=] and the like */
1073 if (equiclass != 0)
1074 {
1075 result = nfa_emit_equi_class(equiclass, negated);
1076 if (result == FAIL)
1077 {
1078 /* should never happen */
1079 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1080 }
1081 EMIT_GLUE();
1082 continue;
1083 }
1084 /* Try collating class like [. .] */
1085 if (collclass != 0)
1086 {
1087 startc = collclass; /* allow [.a.]-x as a range */
1088 /* Will emit the proper atom at the end of the
1089 * while loop. */
1090 }
1091 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001092 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1093 * start character. */
1094 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001095 {
1096 emit_range = TRUE;
1097 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001098 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001099 continue; /* reading the end of the range */
1100 }
1101
1102 /* Now handle simple and escaped characters.
1103 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1104 * accepts "\t", "\e", etc., but only when the 'l' flag in
1105 * 'cpoptions' is not included.
1106 * Posix doesn't recognize backslash at all.
1107 */
1108 if (*regparse == '\\'
1109 && !cpo_bsl
1110 && regparse + 1 <= endp
1111 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1112 || (!cpo_lit
1113 && vim_strchr(REGEXP_ABBR, regparse[1])
1114 != NULL)
1115 )
1116 )
1117 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001118 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001119
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001120 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001121 startc = reg_string ? NL : NFA_NEWL;
1122 else
1123 if (*regparse == 'd'
1124 || *regparse == 'o'
1125 || *regparse == 'x'
1126 || *regparse == 'u'
1127 || *regparse == 'U'
1128 )
1129 {
1130 /* TODO(RE) This needs more testing */
1131 startc = coll_get_char();
1132 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001133 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001134 }
1135 else
1136 {
1137 /* \r,\t,\e,\b */
1138 startc = backslash_trans(*regparse);
1139 }
1140 }
1141
1142 /* Normal printable char */
1143 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001144 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001145
1146 /* Previous char was '-', so this char is end of range. */
1147 if (emit_range)
1148 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001149 endc = startc;
1150 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001151 if (startc > endc)
1152 EMSG_RET_FAIL(_(e_invrange));
1153#ifdef FEAT_MBYTE
1154 if (has_mbyte && ((*mb_char2len)(startc) > 1
1155 || (*mb_char2len)(endc) > 1))
1156 {
1157 if (endc > startc + 256)
1158 EMSG_RET_FAIL(_(e_invrange));
1159 /* Emit the range. "startc" was already emitted, so
1160 * skip it. */
1161 for (c = startc + 1; c <= endc; c++)
1162 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001163 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001164 TRY_NEG();
1165 EMIT_GLUE();
1166 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001167 }
1168 else
1169#endif
1170 {
1171#ifdef EBCDIC
1172 int alpha_only = FALSE;
1173
1174 /* for alphabetical range skip the gaps
1175 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1176 if (isalpha(startc) && isalpha(endc))
1177 alpha_only = TRUE;
1178#endif
1179 /* Emit the range. "startc" was already emitted, so
1180 * skip it. */
1181 for (c = startc + 1; c <= endc; c++)
1182#ifdef EBCDIC
1183 if (!alpha_only || isalpha(startc))
1184#endif
1185 {
1186 EMIT(c);
1187 TRY_NEG();
1188 EMIT_GLUE();
1189 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001191 emit_range = FALSE;
1192 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001193 }
1194 else
1195 {
1196 /*
1197 * This char (startc) is not part of a range. Just
1198 * emit it.
1199 *
1200 * Normally, simply emit startc. But if we get char
1201 * code=0 from a collating char, then replace it with
1202 * 0x0a.
1203 *
1204 * This is needed to completely mimic the behaviour of
1205 * the backtracking engine.
1206 */
1207 if (got_coll_char == TRUE && startc == 0)
1208 EMIT(0x0a);
1209 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001210 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001211 TRY_NEG();
1212 EMIT_GLUE();
1213 }
1214
Bram Moolenaar51a29832013-05-28 22:30:35 +02001215 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001216 } /* while (p < endp) */
1217
Bram Moolenaar51a29832013-05-28 22:30:35 +02001218 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219 if (*regparse == '-') /* if last, '-' is just a char */
1220 {
1221 EMIT('-');
1222 TRY_NEG();
1223 EMIT_GLUE();
1224 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001225 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 /* skip the trailing ] */
1228 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001229 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001230 if (negated == TRUE)
1231 {
1232 /* Mark end of negated char range */
1233 EMIT(NFA_END_NEG_RANGE);
1234 EMIT(NFA_CONCAT);
1235 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001236
1237 /* \_[] also matches \n but it's not negated */
1238 if (extra == ADD_NL)
1239 {
1240 EMIT(reg_string ? NL : NFA_NEWL);
1241 EMIT(NFA_OR);
1242 }
1243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001244 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001245 } /* if exists closing ] */
1246
1247 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001248 {
1249 syntax_error = TRUE;
1250 EMSG_RET_FAIL(_(e_missingbracket));
1251 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001252 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254 default:
1255 {
1256#ifdef FEAT_MBYTE
1257 int plen;
1258
1259nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001260 /* plen is length of current char with composing chars */
1261 if (enc_utf8 && ((*mb_char2len)(c)
1262 != (plen = (*mb_ptr2len)(old_regparse))
1263 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001265 int i = 0;
1266
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001267 /* A base character plus composing characters, or just one
1268 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001269 * This requires creating a separate atom as if enclosing
1270 * the characters in (), where NFA_COMPOSING is the ( and
1271 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001272 * building the postfix form, not the NFA itself;
1273 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001274 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001275 for (;;)
1276 {
1277 EMIT(c);
1278 if (i > 0)
1279 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001280 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001281 break;
1282 c = utf_ptr2char(old_regparse + i);
1283 }
1284 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001285 regparse = old_regparse + plen;
1286 }
1287 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001288#endif
1289 {
1290 c = no_Magic(c);
1291 EMIT(c);
1292 }
1293 return OK;
1294 }
1295 }
1296
1297#undef TRY_NEG
1298#undef EMIT_GLUE
1299
1300 return OK;
1301}
1302
1303/*
1304 * Parse something followed by possible [*+=].
1305 *
1306 * A piece is an atom, possibly followed by a multi, an indication of how many
1307 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1308 * characters: "", "a", "aa", etc.
1309 *
1310 * piece ::= atom
1311 * or atom multi
1312 */
1313 static int
1314nfa_regpiece()
1315{
1316 int i;
1317 int op;
1318 int ret;
1319 long minval, maxval;
1320 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001321 parse_state_T old_state;
1322 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001323 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001324 int old_post_pos;
1325 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001326 int quest;
1327
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001328 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1329 * next. */
1330 save_parse_state(&old_state);
1331
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001332 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001333 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001334
1335 ret = nfa_regatom();
1336 if (ret == FAIL)
1337 return FAIL; /* cascaded error */
1338
1339 op = peekchr();
1340 if (re_multi_type(op) == NOT_MULTI)
1341 return OK;
1342
1343 skipchr();
1344 switch (op)
1345 {
1346 case Magic('*'):
1347 EMIT(NFA_STAR);
1348 break;
1349
1350 case Magic('+'):
1351 /*
1352 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1353 * first and only submatch would be "aaa". But the backtracking
1354 * engine interprets the plus as "try matching one more time", and
1355 * a* matches a second time at the end of the input, the empty
1356 * string.
1357 * The submatch will the empty string.
1358 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001359 * In order to be consistent with the old engine, we replace
1360 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001361 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001362 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001363 curchr = -1;
1364 if (nfa_regatom() == FAIL)
1365 return FAIL;
1366 EMIT(NFA_STAR);
1367 EMIT(NFA_CONCAT);
1368 skipchr(); /* skip the \+ */
1369 break;
1370
1371 case Magic('@'):
1372 op = no_Magic(getchr());
1373 switch(op)
1374 {
1375 case '=':
1376 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1377 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001378 case '!':
1379 EMIT(NFA_PREV_ATOM_NO_WIDTH_NEG);
1380 break;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001381 case '0':
1382 case '1':
1383 case '2':
1384 case '3':
1385 case '4':
1386 case '5':
1387 case '6':
1388 case '7':
1389 case '8':
1390 case '9':
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001391 case '<':
1392 case '>':
1393 /* Not supported yet */
1394 return FAIL;
1395 default:
1396 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001397 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001398 return FAIL;
1399 }
1400 break;
1401
1402 case Magic('?'):
1403 case Magic('='):
1404 EMIT(NFA_QUEST);
1405 break;
1406
1407 case Magic('{'):
1408 /* a{2,5} will expand to 'aaa?a?a?'
1409 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1410 * version of '?'
1411 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1412 * parenthesis have the same id
1413 */
1414
1415 greedy = TRUE;
1416 c2 = peekchr();
1417 if (c2 == '-' || c2 == Magic('-'))
1418 {
1419 skipchr();
1420 greedy = FALSE;
1421 }
1422 if (!read_limits(&minval, &maxval))
1423 {
1424 syntax_error = TRUE;
1425 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1426 }
1427 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1428 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001429 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001430 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001431 if (greedy)
1432 /* \{}, \{0,} */
1433 EMIT(NFA_STAR);
1434 else
1435 /* \{-}, \{-0,} */
1436 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 break;
1438 }
1439
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440 /* Special case: x{0} or x{-0} */
1441 if (maxval == 0)
1442 {
1443 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001444 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1446 EMIT(NFA_SKIP_CHAR);
1447 return OK;
1448 }
1449
1450 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001451 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001452 /* Save parse state after the repeated atom and the \{} */
1453 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001454
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001455 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1456 for (i = 0; i < maxval; i++)
1457 {
1458 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001459 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001460 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461 if (nfa_regatom() == FAIL)
1462 return FAIL;
1463 /* after "minval" times, atoms are optional */
1464 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001465 {
1466 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001467 {
1468 if (greedy)
1469 EMIT(NFA_STAR);
1470 else
1471 EMIT(NFA_STAR_NONGREEDY);
1472 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001473 else
1474 EMIT(quest);
1475 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001476 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001478 if (i + 1 > minval && maxval == MAX_LIMIT)
1479 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001480 }
1481
1482 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001483 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001484 curchr = -1;
1485
1486 break;
1487
1488
1489 default:
1490 break;
1491 } /* end switch */
1492
1493 if (re_multi_type(peekchr()) != NOT_MULTI)
1494 {
1495 /* Can't have a multi follow a multi. */
1496 syntax_error = TRUE;
1497 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1498 }
1499
1500 return OK;
1501}
1502
1503/*
1504 * Parse one or more pieces, concatenated. It matches a match for the
1505 * first piece, followed by a match for the second piece, etc. Example:
1506 * "f[0-9]b", first matches "f", then a digit and then "b".
1507 *
1508 * concat ::= piece
1509 * or piece piece
1510 * or piece piece piece
1511 * etc.
1512 */
1513 static int
1514nfa_regconcat()
1515{
1516 int cont = TRUE;
1517 int first = TRUE;
1518
1519 while (cont)
1520 {
1521 switch (peekchr())
1522 {
1523 case NUL:
1524 case Magic('|'):
1525 case Magic('&'):
1526 case Magic(')'):
1527 cont = FALSE;
1528 break;
1529
1530 case Magic('Z'):
1531#ifdef FEAT_MBYTE
1532 regflags |= RF_ICOMBINE;
1533#endif
1534 skipchr_keepstart();
1535 break;
1536 case Magic('c'):
1537 regflags |= RF_ICASE;
1538 skipchr_keepstart();
1539 break;
1540 case Magic('C'):
1541 regflags |= RF_NOICASE;
1542 skipchr_keepstart();
1543 break;
1544 case Magic('v'):
1545 reg_magic = MAGIC_ALL;
1546 skipchr_keepstart();
1547 curchr = -1;
1548 break;
1549 case Magic('m'):
1550 reg_magic = MAGIC_ON;
1551 skipchr_keepstart();
1552 curchr = -1;
1553 break;
1554 case Magic('M'):
1555 reg_magic = MAGIC_OFF;
1556 skipchr_keepstart();
1557 curchr = -1;
1558 break;
1559 case Magic('V'):
1560 reg_magic = MAGIC_NONE;
1561 skipchr_keepstart();
1562 curchr = -1;
1563 break;
1564
1565 default:
1566 if (nfa_regpiece() == FAIL)
1567 return FAIL;
1568 if (first == FALSE)
1569 EMIT(NFA_CONCAT);
1570 else
1571 first = FALSE;
1572 break;
1573 }
1574 }
1575
1576 return OK;
1577}
1578
1579/*
1580 * Parse a branch, one or more concats, separated by "\&". It matches the
1581 * last concat, but only if all the preceding concats also match at the same
1582 * position. Examples:
1583 * "foobeep\&..." matches "foo" in "foobeep".
1584 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1585 *
1586 * branch ::= concat
1587 * or concat \& concat
1588 * or concat \& concat \& concat
1589 * etc.
1590 */
1591 static int
1592nfa_regbranch()
1593{
1594 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001595 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001596
Bram Moolenaar16299b52013-05-30 18:45:23 +02001597 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001598
1599 /* First branch, possibly the only one */
1600 if (nfa_regconcat() == FAIL)
1601 return FAIL;
1602
1603 ch = peekchr();
1604 /* Try next concats */
1605 while (ch == Magic('&'))
1606 {
1607 skipchr();
1608 EMIT(NFA_NOPEN);
1609 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001610 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001611 if (nfa_regconcat() == FAIL)
1612 return FAIL;
1613 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001614 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001615 EMIT(NFA_SKIP_CHAR);
1616 EMIT(NFA_CONCAT);
1617 ch = peekchr();
1618 }
1619
1620 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001621 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001622 EMIT(NFA_SKIP_CHAR);
1623
1624 return OK;
1625}
1626
1627/*
1628 * Parse a pattern, one or more branches, separated by "\|". It matches
1629 * anything that matches one of the branches. Example: "foo\|beep" matches
1630 * "foo" and matches "beep". If more than one branch matches, the first one
1631 * is used.
1632 *
1633 * pattern ::= branch
1634 * or branch \| branch
1635 * or branch \| branch \| branch
1636 * etc.
1637 */
1638 static int
1639nfa_reg(paren)
1640 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1641{
1642 int parno = 0;
1643
1644#ifdef FEAT_SYN_HL
1645#endif
1646 if (paren == REG_PAREN)
1647 {
1648 if (regnpar >= NSUBEXP) /* Too many `(' */
1649 {
1650 syntax_error = TRUE;
1651 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1652 }
1653 parno = regnpar++;
1654 }
1655
1656 if (nfa_regbranch() == FAIL)
1657 return FAIL; /* cascaded error */
1658
1659 while (peekchr() == Magic('|'))
1660 {
1661 skipchr();
1662 if (nfa_regbranch() == FAIL)
1663 return FAIL; /* cascaded error */
1664 EMIT(NFA_OR);
1665 }
1666
1667 /* Check for proper termination. */
1668 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1669 {
1670 syntax_error = TRUE;
1671 if (paren == REG_NPAREN)
1672 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1673 else
1674 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1675 }
1676 else if (paren == REG_NOPAREN && peekchr() != NUL)
1677 {
1678 syntax_error = TRUE;
1679 if (peekchr() == Magic(')'))
1680 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1681 else
1682 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1683 }
1684 /*
1685 * Here we set the flag allowing back references to this set of
1686 * parentheses.
1687 */
1688 if (paren == REG_PAREN)
1689 {
1690 had_endbrace[parno] = TRUE; /* have seen the close paren */
1691 EMIT(NFA_MOPEN + parno);
1692 }
1693
1694 return OK;
1695}
1696
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001697#ifdef DEBUG
1698static char_u code[50];
1699
1700 static void
1701nfa_set_code(c)
1702 int c;
1703{
1704 int addnl = FALSE;
1705
1706 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1707 {
1708 addnl = TRUE;
1709 c -= ADD_NL;
1710 }
1711
1712 STRCPY(code, "");
1713 switch (c)
1714 {
1715 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1716 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1717 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1718 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1719 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1720 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1721
Bram Moolenaar5714b802013-05-28 22:03:20 +02001722 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1723 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1724 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1725 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1726 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1727 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1728 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1729 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1730 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1731 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1732
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001733 case NFA_PREV_ATOM_NO_WIDTH:
1734 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001735 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1736 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001737 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1738 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1740 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1741
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001742 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1743 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1744
1745 case NFA_MOPEN + 0:
1746 case NFA_MOPEN + 1:
1747 case NFA_MOPEN + 2:
1748 case NFA_MOPEN + 3:
1749 case NFA_MOPEN + 4:
1750 case NFA_MOPEN + 5:
1751 case NFA_MOPEN + 6:
1752 case NFA_MOPEN + 7:
1753 case NFA_MOPEN + 8:
1754 case NFA_MOPEN + 9:
1755 STRCPY(code, "NFA_MOPEN(x)");
1756 code[10] = c - NFA_MOPEN + '0';
1757 break;
1758 case NFA_MCLOSE + 0:
1759 case NFA_MCLOSE + 1:
1760 case NFA_MCLOSE + 2:
1761 case NFA_MCLOSE + 3:
1762 case NFA_MCLOSE + 4:
1763 case NFA_MCLOSE + 5:
1764 case NFA_MCLOSE + 6:
1765 case NFA_MCLOSE + 7:
1766 case NFA_MCLOSE + 8:
1767 case NFA_MCLOSE + 9:
1768 STRCPY(code, "NFA_MCLOSE(x)");
1769 code[11] = c - NFA_MCLOSE + '0';
1770 break;
1771 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1772 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1773 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1774 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001775 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1776 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001778 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1779 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1780 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001781 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1782 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1783 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001784 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1785 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1786 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1787 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1788 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1789 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1790 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1791 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1792 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1793 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1794 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1795 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1796 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1797 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1798 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1799 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1800 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1801
1802 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1803 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1804 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1805 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1806 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1807 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1808 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1809 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1810 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1811 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1812 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1813 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1814 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1815 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1816 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1817 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1818 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1819 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1820 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1821 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1822 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1823 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1824 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1825 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1826 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1827 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1828 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1829
1830 default:
1831 STRCPY(code, "CHAR(x)");
1832 code[5] = c;
1833 }
1834
1835 if (addnl == TRUE)
1836 STRCAT(code, " + NEWLINE ");
1837
1838}
1839
1840#ifdef ENABLE_LOG
1841static FILE *log_fd;
1842
1843/*
1844 * Print the postfix notation of the current regexp.
1845 */
1846 static void
1847nfa_postfix_dump(expr, retval)
1848 char_u *expr;
1849 int retval;
1850{
1851 int *p;
1852 FILE *f;
1853
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001854 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 if (f != NULL)
1856 {
1857 fprintf(f, "\n-------------------------\n");
1858 if (retval == FAIL)
1859 fprintf(f, ">>> NFA engine failed ... \n");
1860 else if (retval == OK)
1861 fprintf(f, ">>> NFA engine succeeded !\n");
1862 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001863 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001864 {
1865 nfa_set_code(*p);
1866 fprintf(f, "%s, ", code);
1867 }
1868 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001869 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870 fprintf(f, "%d ", *p);
1871 fprintf(f, "\n\n");
1872 fclose(f);
1873 }
1874}
1875
1876/*
1877 * Print the NFA starting with a root node "state".
1878 */
1879 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001880nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881 FILE *debugf;
1882 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001883{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001884 garray_T indent;
1885
1886 ga_init2(&indent, 1, 64);
1887 ga_append(&indent, '\0');
1888 nfa_print_state2(debugf, state, &indent);
1889 ga_clear(&indent);
1890}
1891
1892 static void
1893nfa_print_state2(debugf, state, indent)
1894 FILE *debugf;
1895 nfa_state_T *state;
1896 garray_T *indent;
1897{
1898 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899
1900 if (state == NULL)
1901 return;
1902
1903 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001904
1905 /* Output indent */
1906 p = (char_u *)indent->ga_data;
1907 if (indent->ga_len >= 3)
1908 {
1909 int last = indent->ga_len - 3;
1910 char_u save[2];
1911
1912 STRNCPY(save, &p[last], 2);
1913 STRNCPY(&p[last], "+-", 2);
1914 fprintf(debugf, " %s", p);
1915 STRNCPY(&p[last], save, 2);
1916 }
1917 else
1918 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919
1920 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001921 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1922 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 if (state->id < 0)
1924 return;
1925
1926 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001927
1928 /* grow indent for state->out */
1929 indent->ga_len -= 1;
1930 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001931 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001932 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001933 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001934 ga_append(indent, '\0');
1935
1936 nfa_print_state2(debugf, state->out, indent);
1937
1938 /* replace last part of indent for state->out1 */
1939 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001940 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001941 ga_append(indent, '\0');
1942
1943 nfa_print_state2(debugf, state->out1, indent);
1944
1945 /* shrink indent */
1946 indent->ga_len -= 3;
1947 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948}
1949
1950/*
1951 * Print the NFA state machine.
1952 */
1953 static void
1954nfa_dump(prog)
1955 nfa_regprog_T *prog;
1956{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001957 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958
1959 if (debugf != NULL)
1960 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001961 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 fclose(debugf);
1963 }
1964}
1965#endif /* ENABLE_LOG */
1966#endif /* DEBUG */
1967
1968/*
1969 * Parse r.e. @expr and convert it into postfix form.
1970 * Return the postfix string on success, NULL otherwise.
1971 */
1972 static int *
1973re2post()
1974{
1975 if (nfa_reg(REG_NOPAREN) == FAIL)
1976 return NULL;
1977 EMIT(NFA_MOPEN);
1978 return post_start;
1979}
1980
1981/* NB. Some of the code below is inspired by Russ's. */
1982
1983/*
1984 * Represents an NFA state plus zero or one or two arrows exiting.
1985 * if c == MATCH, no arrows out; matching state.
1986 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1987 * If c < 256, labeled arrow with character c to out.
1988 */
1989
1990static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1991
1992/*
1993 * Allocate and initialize nfa_state_T.
1994 */
1995 static nfa_state_T *
1996new_state(c, out, out1)
1997 int c;
1998 nfa_state_T *out;
1999 nfa_state_T *out1;
2000{
2001 nfa_state_T *s;
2002
2003 if (istate >= nstate)
2004 return NULL;
2005
2006 s = &state_ptr[istate++];
2007
2008 s->c = c;
2009 s->out = out;
2010 s->out1 = out1;
2011
2012 s->id = istate;
2013 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014 s->negated = FALSE;
2015
2016 return s;
2017}
2018
2019/*
2020 * A partially built NFA without the matching state filled in.
2021 * Frag_T.start points at the start state.
2022 * Frag_T.out is a list of places that need to be set to the
2023 * next state for this fragment.
2024 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002025
2026/* Since the out pointers in the list are always
2027 * uninitialized, we use the pointers themselves
2028 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002030union Ptrlist
2031{
2032 Ptrlist *next;
2033 nfa_state_T *s;
2034};
2035
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002036struct Frag
2037{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002038 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002039 Ptrlist *out;
2040};
2041typedef struct Frag Frag_T;
2042
2043static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2044static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2045static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2046static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2047static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2048static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2049
2050/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002051 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002052 */
2053 static Frag_T
2054frag(start, out)
2055 nfa_state_T *start;
2056 Ptrlist *out;
2057{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002058 Frag_T n;
2059
2060 n.start = start;
2061 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002062 return n;
2063}
2064
2065/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002066 * Create singleton list containing just outp.
2067 */
2068 static Ptrlist *
2069list1(outp)
2070 nfa_state_T **outp;
2071{
2072 Ptrlist *l;
2073
2074 l = (Ptrlist *)outp;
2075 l->next = NULL;
2076 return l;
2077}
2078
2079/*
2080 * Patch the list of states at out to point to start.
2081 */
2082 static void
2083patch(l, s)
2084 Ptrlist *l;
2085 nfa_state_T *s;
2086{
2087 Ptrlist *next;
2088
2089 for (; l; l = next)
2090 {
2091 next = l->next;
2092 l->s = s;
2093 }
2094}
2095
2096
2097/*
2098 * Join the two lists l1 and l2, returning the combination.
2099 */
2100 static Ptrlist *
2101append(l1, l2)
2102 Ptrlist *l1;
2103 Ptrlist *l2;
2104{
2105 Ptrlist *oldl1;
2106
2107 oldl1 = l1;
2108 while (l1->next)
2109 l1 = l1->next;
2110 l1->next = l2;
2111 return oldl1;
2112}
2113
2114/*
2115 * Stack used for transforming postfix form into NFA.
2116 */
2117static Frag_T empty;
2118
2119 static void
2120st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002121 int *postfix UNUSED;
2122 int *end UNUSED;
2123 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002124{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002125#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002126 FILE *df;
2127 int *p2;
2128
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002129 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002130 if (df)
2131 {
2132 fprintf(df, "Error popping the stack!\n");
2133#ifdef DEBUG
2134 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2135#endif
2136 fprintf(df, "Postfix form is: ");
2137#ifdef DEBUG
2138 for (p2 = postfix; p2 < end; p2++)
2139 {
2140 nfa_set_code(*p2);
2141 fprintf(df, "%s, ", code);
2142 }
2143 nfa_set_code(*p);
2144 fprintf(df, "\nCurrent position is: ");
2145 for (p2 = postfix; p2 <= p; p2 ++)
2146 {
2147 nfa_set_code(*p2);
2148 fprintf(df, "%s, ", code);
2149 }
2150#else
2151 for (p2 = postfix; p2 < end; p2++)
2152 {
2153 fprintf(df, "%d, ", *p2);
2154 }
2155 fprintf(df, "\nCurrent position is: ");
2156 for (p2 = postfix; p2 <= p; p2 ++)
2157 {
2158 fprintf(df, "%d, ", *p2);
2159 }
2160#endif
2161 fprintf(df, "\n--------------------------\n");
2162 fclose(df);
2163 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002164#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 EMSG(_("E874: (NFA) Could not pop the stack !"));
2166}
2167
2168/*
2169 * Push an item onto the stack.
2170 */
2171 static void
2172st_push(s, p, stack_end)
2173 Frag_T s;
2174 Frag_T **p;
2175 Frag_T *stack_end;
2176{
2177 Frag_T *stackp = *p;
2178
2179 if (stackp >= stack_end)
2180 return;
2181 *stackp = s;
2182 *p = *p + 1;
2183}
2184
2185/*
2186 * Pop an item from the stack.
2187 */
2188 static Frag_T
2189st_pop(p, stack)
2190 Frag_T **p;
2191 Frag_T *stack;
2192{
2193 Frag_T *stackp;
2194
2195 *p = *p - 1;
2196 stackp = *p;
2197 if (stackp < stack)
2198 return empty;
2199 return **p;
2200}
2201
2202/*
2203 * Convert a postfix form into its equivalent NFA.
2204 * Return the NFA start state on success, NULL otherwise.
2205 */
2206 static nfa_state_T *
2207post2nfa(postfix, end, nfa_calc_size)
2208 int *postfix;
2209 int *end;
2210 int nfa_calc_size;
2211{
2212 int *p;
2213 int mopen;
2214 int mclose;
2215 Frag_T *stack = NULL;
2216 Frag_T *stackp = NULL;
2217 Frag_T *stack_end = NULL;
2218 Frag_T e1;
2219 Frag_T e2;
2220 Frag_T e;
2221 nfa_state_T *s;
2222 nfa_state_T *s1;
2223 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002224 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002225
2226 if (postfix == NULL)
2227 return NULL;
2228
Bram Moolenaar053bb602013-05-20 13:55:21 +02002229#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002230#define POP() st_pop(&stackp, stack); \
2231 if (stackp < stack) \
2232 { \
2233 st_error(postfix, end, p); \
2234 return NULL; \
2235 }
2236
2237 if (nfa_calc_size == FALSE)
2238 {
2239 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002240 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002241 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002242 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 }
2244
2245 for (p = postfix; p < end; ++p)
2246 {
2247 switch (*p)
2248 {
2249 case NFA_CONCAT:
2250 /* Catenation.
2251 * Pay attention: this operator does not exist
2252 * in the r.e. itself (it is implicit, really).
2253 * It is added when r.e. is translated to postfix
2254 * form in re2post().
2255 *
2256 * No new state added here. */
2257 if (nfa_calc_size == TRUE)
2258 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002259 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002260 break;
2261 }
2262 e2 = POP();
2263 e1 = POP();
2264 patch(e1.out, e2.start);
2265 PUSH(frag(e1.start, e2.out));
2266 break;
2267
2268 case NFA_NOT:
2269 /* Negation of a character */
2270 if (nfa_calc_size == TRUE)
2271 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002272 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002273 break;
2274 }
2275 e1 = POP();
2276 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002277#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002278 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002280#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002281 PUSH(e1);
2282 break;
2283
2284 case NFA_OR:
2285 /* Alternation */
2286 if (nfa_calc_size == TRUE)
2287 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002288 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 break;
2290 }
2291 e2 = POP();
2292 e1 = POP();
2293 s = new_state(NFA_SPLIT, e1.start, e2.start);
2294 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002295 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002296 PUSH(frag(s, append(e1.out, e2.out)));
2297 break;
2298
2299 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002300 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002301 if (nfa_calc_size == TRUE)
2302 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002303 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 break;
2305 }
2306 e = POP();
2307 s = new_state(NFA_SPLIT, e.start, NULL);
2308 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002309 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002310 patch(e.out, s);
2311 PUSH(frag(s, list1(&s->out1)));
2312 break;
2313
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002314 case NFA_STAR_NONGREEDY:
2315 /* Zero or more, prefer zero */
2316 if (nfa_calc_size == TRUE)
2317 {
2318 nstate++;
2319 break;
2320 }
2321 e = POP();
2322 s = new_state(NFA_SPLIT, NULL, e.start);
2323 if (s == NULL)
2324 goto theend;
2325 patch(e.out, s);
2326 PUSH(frag(s, list1(&s->out)));
2327 break;
2328
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329 case NFA_QUEST:
2330 /* one or zero atoms=> greedy match */
2331 if (nfa_calc_size == TRUE)
2332 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002333 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 break;
2335 }
2336 e = POP();
2337 s = new_state(NFA_SPLIT, e.start, NULL);
2338 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002339 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340 PUSH(frag(s, append(e.out, list1(&s->out1))));
2341 break;
2342
2343 case NFA_QUEST_NONGREEDY:
2344 /* zero or one atoms => non-greedy match */
2345 if (nfa_calc_size == TRUE)
2346 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002347 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002348 break;
2349 }
2350 e = POP();
2351 s = new_state(NFA_SPLIT, NULL, e.start);
2352 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002353 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354 PUSH(frag(s, append(e.out, list1(&s->out))));
2355 break;
2356
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357 case NFA_SKIP_CHAR:
2358 /* Symbol of 0-length, Used in a repetition
2359 * with max/min count of 0 */
2360 if (nfa_calc_size == TRUE)
2361 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002362 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002363 break;
2364 }
2365 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2366 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002367 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002368 PUSH(frag(s, list1(&s->out)));
2369 break;
2370
2371 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002372 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002373 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002374 * The \@! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002375 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002376 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002377
2378 if (nfa_calc_size == TRUE)
2379 {
2380 nstate += 2;
2381 break;
2382 }
2383 e = POP();
2384 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2385 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002386 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387 patch(e.out, s1);
2388
2389 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2390 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002391 goto theend;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002392 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG)
2393 {
2394 s->negated = TRUE;
2395 s1->negated = TRUE;
2396 }
2397
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398 PUSH(frag(s, list1(&s1->out)));
2399 break;
2400
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002401#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002402 case NFA_COMPOSING: /* char with composing char */
2403#if 0
2404 /* TODO */
2405 if (regflags & RF_ICOMBINE)
2406 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002407 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002408 }
2409#endif
2410 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002411#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002412
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002413 case NFA_MOPEN + 0: /* Submatch */
2414 case NFA_MOPEN + 1:
2415 case NFA_MOPEN + 2:
2416 case NFA_MOPEN + 3:
2417 case NFA_MOPEN + 4:
2418 case NFA_MOPEN + 5:
2419 case NFA_MOPEN + 6:
2420 case NFA_MOPEN + 7:
2421 case NFA_MOPEN + 8:
2422 case NFA_MOPEN + 9:
2423 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002424 if (nfa_calc_size == TRUE)
2425 {
2426 nstate += 2;
2427 break;
2428 }
2429
2430 mopen = *p;
2431 switch (*p)
2432 {
2433 case NFA_NOPEN:
2434 mclose = NFA_NCLOSE;
2435 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002436#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002437 case NFA_COMPOSING:
2438 mclose = NFA_END_COMPOSING;
2439 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002440#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002441 default:
2442 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2443 mclose = *p + NSUBEXP;
2444 break;
2445 }
2446
2447 /* Allow "NFA_MOPEN" as a valid postfix representation for
2448 * the empty regexp "". In this case, the NFA will be
2449 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2450 * empty groups of parenthesis, and empty mbyte chars */
2451 if (stackp == stack)
2452 {
2453 s = new_state(mopen, NULL, NULL);
2454 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002455 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002456 s1 = new_state(mclose, NULL, NULL);
2457 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002458 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 patch(list1(&s->out), s1);
2460 PUSH(frag(s, list1(&s1->out)));
2461 break;
2462 }
2463
2464 /* At least one node was emitted before NFA_MOPEN, so
2465 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2466 e = POP();
2467 s = new_state(mopen, e.start, NULL); /* `(' */
2468 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002469 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002470
2471 s1 = new_state(mclose, NULL, NULL); /* `)' */
2472 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002473 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002474 patch(e.out, s1);
2475
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002476#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002477 if (mopen == NFA_COMPOSING)
2478 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002480#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002481
2482 PUSH(frag(s, list1(&s1->out)));
2483 break;
2484
Bram Moolenaar5714b802013-05-28 22:03:20 +02002485 case NFA_BACKREF1:
2486 case NFA_BACKREF2:
2487 case NFA_BACKREF3:
2488 case NFA_BACKREF4:
2489 case NFA_BACKREF5:
2490 case NFA_BACKREF6:
2491 case NFA_BACKREF7:
2492 case NFA_BACKREF8:
2493 case NFA_BACKREF9:
2494 if (nfa_calc_size == TRUE)
2495 {
2496 nstate += 2;
2497 break;
2498 }
2499 s = new_state(*p, NULL, NULL);
2500 if (s == NULL)
2501 goto theend;
2502 s1 = new_state(NFA_SKIP, NULL, NULL);
2503 if (s1 == NULL)
2504 goto theend;
2505 patch(list1(&s->out), s1);
2506 PUSH(frag(s, list1(&s1->out)));
2507 break;
2508
Bram Moolenaar423532e2013-05-29 21:14:42 +02002509 case NFA_LNUM:
2510 case NFA_LNUM_GT:
2511 case NFA_LNUM_LT:
2512 case NFA_VCOL:
2513 case NFA_VCOL_GT:
2514 case NFA_VCOL_LT:
2515 case NFA_COL:
2516 case NFA_COL_GT:
2517 case NFA_COL_LT:
2518 if (nfa_calc_size == TRUE)
2519 {
2520 nstate += 1;
2521 break;
2522 }
2523 e1 = POP();
2524 s = new_state(*p, NULL, NULL);
2525 if (s == NULL)
2526 goto theend;
2527 s->val = e1.start->c;
2528 PUSH(frag(s, list1(&s->out)));
2529 break;
2530
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002531 case NFA_ZSTART:
2532 case NFA_ZEND:
2533 default:
2534 /* Operands */
2535 if (nfa_calc_size == TRUE)
2536 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002537 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538 break;
2539 }
2540 s = new_state(*p, NULL, NULL);
2541 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002542 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 PUSH(frag(s, list1(&s->out)));
2544 break;
2545
2546 } /* switch(*p) */
2547
2548 } /* for(p = postfix; *p; ++p) */
2549
2550 if (nfa_calc_size == TRUE)
2551 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002552 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002553 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002554 }
2555
2556 e = POP();
2557 if (stackp != stack)
2558 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2559
2560 if (istate >= nstate)
2561 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2562
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002563 matchstate = &state_ptr[istate++]; /* the match state */
2564 matchstate->c = NFA_MATCH;
2565 matchstate->out = matchstate->out1 = NULL;
2566
2567 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002568 ret = e.start;
2569
2570theend:
2571 vim_free(stack);
2572 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573
2574#undef POP1
2575#undef PUSH1
2576#undef POP2
2577#undef PUSH2
2578#undef POP
2579#undef PUSH
2580}
2581
2582/****************************************************************
2583 * NFA execution code.
2584 ****************************************************************/
2585
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002586typedef struct
2587{
2588 int in_use; /* number of subexpr with useful info */
2589
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002590 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002591 union
2592 {
2593 struct multipos
2594 {
2595 lpos_T start;
2596 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002597 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002598 struct linepos
2599 {
2600 char_u *start;
2601 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002602 } line[NSUBEXP];
2603 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002604} regsub_T;
2605
Bram Moolenaar963fee22013-05-26 21:47:28 +02002606/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002607typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608{
2609 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002610 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002611 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002612} nfa_thread_T;
2613
Bram Moolenaar963fee22013-05-26 21:47:28 +02002614/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002615typedef struct
2616{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002617 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002618 int n; /* nr of states currently in "t" */
2619 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002620 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002621} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002622
Bram Moolenaar5714b802013-05-28 22:03:20 +02002623#ifdef ENABLE_LOG
2624 static void
2625log_subexpr(sub)
2626 regsub_T *sub;
2627{
2628 int j;
2629
2630 for (j = 0; j < sub->in_use; j++)
2631 if (REG_MULTI)
2632 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2633 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002634 sub->list.multi[j].start.col,
2635 (int)sub->list.multi[j].start.lnum,
2636 sub->list.multi[j].end.col,
2637 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002638 else
2639 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2640 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002641 (char *)sub->list.line[j].start,
2642 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002643 fprintf(log_fd, "\n");
2644}
2645#endif
2646
Bram Moolenaar963fee22013-05-26 21:47:28 +02002647/* Used during execution: whether a match has been found. */
2648static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002649
Bram Moolenaar428e9872013-05-30 17:05:39 +02002650static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002651static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2652static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002653
Bram Moolenaar428e9872013-05-30 17:05:39 +02002654/*
2655 * Return TRUE if "sub1" and "sub2" have the same positions.
2656 */
2657 static int
2658sub_equal(sub1, sub2)
2659 regsub_T *sub1;
2660 regsub_T *sub2;
2661{
2662 int i;
2663 int todo;
2664 linenr_T s1, e1;
2665 linenr_T s2, e2;
2666 char_u *sp1, *ep1;
2667 char_u *sp2, *ep2;
2668
2669 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2670 if (REG_MULTI)
2671 {
2672 for (i = 0; i < todo; ++i)
2673 {
2674 if (i < sub1->in_use)
2675 {
2676 s1 = sub1->list.multi[i].start.lnum;
2677 e1 = sub1->list.multi[i].end.lnum;
2678 }
2679 else
2680 {
2681 s1 = 0;
2682 e1 = 0;
2683 }
2684 if (i < sub2->in_use)
2685 {
2686 s2 = sub2->list.multi[i].start.lnum;
2687 e2 = sub2->list.multi[i].end.lnum;
2688 }
2689 else
2690 {
2691 s2 = 0;
2692 e2 = 0;
2693 }
2694 if (s1 != s2 || e1 != e2)
2695 return FALSE;
2696 if (s1 != 0 && sub1->list.multi[i].start.col
2697 != sub2->list.multi[i].start.col)
2698 return FALSE;
2699 if (e1 != 0 && sub1->list.multi[i].end.col
2700 != sub2->list.multi[i].end.col)
2701 return FALSE;
2702 }
2703 }
2704 else
2705 {
2706 for (i = 0; i < todo; ++i)
2707 {
2708 if (i < sub1->in_use)
2709 {
2710 sp1 = sub1->list.line[i].start;
2711 ep1 = sub1->list.line[i].end;
2712 }
2713 else
2714 {
2715 sp1 = NULL;
2716 ep1 = NULL;
2717 }
2718 if (i < sub2->in_use)
2719 {
2720 sp2 = sub2->list.line[i].start;
2721 ep2 = sub2->list.line[i].end;
2722 }
2723 else
2724 {
2725 sp2 = NULL;
2726 ep2 = NULL;
2727 }
2728 if (sp1 != sp2 || ep1 != ep2)
2729 return FALSE;
2730 }
2731 }
2732
2733 return TRUE;
2734}
2735
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002736 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002737addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002738 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002740 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002741 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002743 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002744 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002745 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002746 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002747 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002748 int i;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002749#ifdef ENABLE_LOG
2750 int did_print = FALSE;
2751#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752
2753 if (l == NULL || state == NULL)
2754 return;
2755
2756 switch (state->c)
2757 {
2758 case NFA_SPLIT:
2759 case NFA_NOT:
2760 case NFA_NOPEN:
2761 case NFA_NCLOSE:
2762 case NFA_MCLOSE:
2763 case NFA_MCLOSE + 1:
2764 case NFA_MCLOSE + 2:
2765 case NFA_MCLOSE + 3:
2766 case NFA_MCLOSE + 4:
2767 case NFA_MCLOSE + 5:
2768 case NFA_MCLOSE + 6:
2769 case NFA_MCLOSE + 7:
2770 case NFA_MCLOSE + 8:
2771 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002772 /* These nodes are not added themselves but their "out" and/or
2773 * "out1" may be added below. */
2774 break;
2775
2776 case NFA_MOPEN:
2777 case NFA_MOPEN + 1:
2778 case NFA_MOPEN + 2:
2779 case NFA_MOPEN + 3:
2780 case NFA_MOPEN + 4:
2781 case NFA_MOPEN + 5:
2782 case NFA_MOPEN + 6:
2783 case NFA_MOPEN + 7:
2784 case NFA_MOPEN + 8:
2785 case NFA_MOPEN + 9:
2786 /* These nodes do not need to be added, but we need to bail out
2787 * when it was tried to be added to this list before. */
2788 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002789 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002790 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002791 break;
2792
2793 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002794 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002795 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002796 /* This state is already in the list, don't add it again,
2797 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002798 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002799 {
2800skip_add:
2801#ifdef ENABLE_LOG
2802 nfa_set_code(state->c);
2803 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
2804 abs(state->id), l->id, state->c, code);
2805#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02002806 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002807 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02002808
2809 /* See if the same state is already in the list with the same
2810 * positions. */
2811 for (i = 0; i < l->n; ++i)
2812 {
2813 thread = &l->t[i];
2814 if (thread->state->id == state->id
2815 && sub_equal(&thread->sub, sub))
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002816 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002817 }
2818 }
2819
2820 /* when there are backreferences the number of states may be (a
2821 * lot) bigger */
2822 if (nfa_has_backref && l->n == l->len)
2823 {
2824 int newlen = l->len * 3 / 2 + 50;
2825
2826 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2827 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002829
2830 /* add the state to the list */
2831 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002832 thread = &l->t[l->n++];
2833 thread->state = state;
2834 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002835 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002837 /* Copy the match start and end positions. */
2838 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002839 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002840 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002841 sizeof(struct multipos) * sub->in_use);
2842 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002843 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002844 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002845 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002846 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002847#ifdef ENABLE_LOG
2848 {
2849 int col;
2850
2851 if (thread->sub.in_use <= 0)
2852 col = -1;
2853 else if (REG_MULTI)
2854 col = thread->sub.list.multi[0].start.col;
2855 else
2856 col = (int)(thread->sub.list.line[0].start - regline);
2857 nfa_set_code(state->c);
2858 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
2859 abs(state->id), l->id, state->c, code, col);
2860 did_print = TRUE;
2861 }
2862#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002863 }
2864
2865#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002866 if (!did_print)
2867 {
2868 int col;
2869
2870 if (sub->in_use <= 0)
2871 col = -1;
2872 else if (REG_MULTI)
2873 col = sub->list.multi[0].start.col;
2874 else
2875 col = (int)(sub->list.line[0].start - regline);
2876 nfa_set_code(state->c);
2877 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
2878 abs(state->id), l->id, state->c, code, col);
2879 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002880#endif
2881 switch (state->c)
2882 {
2883 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002884 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002885 break;
2886
2887 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002888 addstate(l, state->out, sub, off);
2889 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002890 break;
2891
2892#if 0
2893 case NFA_END_NEG_RANGE:
2894 /* Nothing to handle here. nfa_regmatch() will take care of it */
2895 break;
2896
2897 case NFA_NOT:
2898 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2899#ifdef ENABLE_LOG
2900 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2901#endif
2902 break;
2903
2904 case NFA_COMPOSING:
2905 /* nfa_regmatch() will match all the bytes of this composing char. */
2906 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002907#endif
2908
Bram Moolenaar5714b802013-05-28 22:03:20 +02002909 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002910 case NFA_NOPEN:
2911 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002912 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002913 break;
2914
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002915 case NFA_MOPEN + 0:
2916 case NFA_MOPEN + 1:
2917 case NFA_MOPEN + 2:
2918 case NFA_MOPEN + 3:
2919 case NFA_MOPEN + 4:
2920 case NFA_MOPEN + 5:
2921 case NFA_MOPEN + 6:
2922 case NFA_MOPEN + 7:
2923 case NFA_MOPEN + 8:
2924 case NFA_MOPEN + 9:
2925 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 if (state->c == NFA_ZSTART)
2927 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002928 else
2929 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002930
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002931 /* Set the position (with "off") in the subexpression. Save and
2932 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02002933 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 if (REG_MULTI)
2935 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002936 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002937 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002938 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002939 save_in_use = -1;
2940 }
2941 else
2942 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002943 save_in_use = sub->in_use;
2944 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002945 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002946 sub->list.multi[i].start.lnum = -1;
2947 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002948 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002949 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002950 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002951 if (off == -1)
2952 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002953 sub->list.multi[subidx].start.lnum = reglnum + 1;
2954 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002955 }
2956 else
2957 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002958 sub->list.multi[subidx].start.lnum = reglnum;
2959 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002960 (colnr_T)(reginput - regline + off);
2961 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002962 }
2963 else
2964 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002965 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002966 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002967 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002968 save_in_use = -1;
2969 }
2970 else
2971 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002972 save_in_use = sub->in_use;
2973 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002974 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002975 sub->list.line[i].start = NULL;
2976 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002977 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002978 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002979 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002980 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002981 }
2982
Bram Moolenaar5714b802013-05-28 22:03:20 +02002983 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002984
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002985 if (save_in_use == -1)
2986 {
2987 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002988 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002989 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002990 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002991 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002993 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002994 break;
2995
2996 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002997 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002998 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002999 /* Do not overwrite the position set by \ze. If no \ze
3000 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003001 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003002 break;
3003 }
3004 case NFA_MCLOSE + 1:
3005 case NFA_MCLOSE + 2:
3006 case NFA_MCLOSE + 3:
3007 case NFA_MCLOSE + 4:
3008 case NFA_MCLOSE + 5:
3009 case NFA_MCLOSE + 6:
3010 case NFA_MCLOSE + 7:
3011 case NFA_MCLOSE + 8:
3012 case NFA_MCLOSE + 9:
3013 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014 if (state->c == NFA_ZEND)
3015 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003016 else
3017 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003018
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003019 /* We don't fill in gaps here, there must have been an MOPEN that
3020 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003021 save_in_use = sub->in_use;
3022 if (sub->in_use <= subidx)
3023 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003024 if (REG_MULTI)
3025 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003026 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003027 if (off == -1)
3028 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003029 sub->list.multi[subidx].end.lnum = reglnum + 1;
3030 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003031 }
3032 else
3033 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003034 sub->list.multi[subidx].end.lnum = reglnum;
3035 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003036 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003037 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003038 }
3039 else
3040 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003041 save_ptr = sub->list.line[subidx].end;
3042 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003043 }
3044
Bram Moolenaar5714b802013-05-28 22:03:20 +02003045 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003046
3047 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003048 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003049 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003050 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003051 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003052 break;
3053 }
3054}
3055
3056/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003057 * Like addstate(), but the new state(s) are put at position "*ip".
3058 * Used for zero-width matches, next state to use is the added one.
3059 * This makes sure the order of states to be tried does not change, which
3060 * matters for alternatives.
3061 */
3062 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003063addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003064 nfa_list_T *l; /* runtime state list */
3065 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003066 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003067 int *ip;
3068{
3069 int tlen = l->n;
3070 int count;
3071 int i = *ip;
3072
3073 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003074 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003075
3076 /* when "*ip" was at the end of the list, nothing to do */
3077 if (i + 1 == tlen)
3078 return;
3079
3080 /* re-order to put the new state at the current position */
3081 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003082 if (count == 1)
3083 {
3084 /* overwrite the current state */
3085 l->t[i] = l->t[l->n - 1];
3086 }
3087 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003088 {
3089 /* make space for new states, then move them from the
3090 * end to the current position */
3091 mch_memmove(&(l->t[i + count]),
3092 &(l->t[i + 1]),
3093 sizeof(nfa_thread_T) * (l->n - i - 1));
3094 mch_memmove(&(l->t[i]),
3095 &(l->t[l->n - 1]),
3096 sizeof(nfa_thread_T) * count);
3097 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003098 --l->n;
3099 *ip = i - 1;
3100}
3101
3102/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003103 * Check character class "class" against current character c.
3104 */
3105 static int
3106check_char_class(class, c)
3107 int class;
3108 int c;
3109{
3110 switch (class)
3111 {
3112 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003113 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114 return OK;
3115 break;
3116 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003117 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118 return OK;
3119 break;
3120 case NFA_CLASS_BLANK:
3121 if (c == ' ' || c == '\t')
3122 return OK;
3123 break;
3124 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003125 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003126 return OK;
3127 break;
3128 case NFA_CLASS_DIGIT:
3129 if (VIM_ISDIGIT(c))
3130 return OK;
3131 break;
3132 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003133 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003134 return OK;
3135 break;
3136 case NFA_CLASS_LOWER:
3137 if (MB_ISLOWER(c))
3138 return OK;
3139 break;
3140 case NFA_CLASS_PRINT:
3141 if (vim_isprintc(c))
3142 return OK;
3143 break;
3144 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003145 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003146 return OK;
3147 break;
3148 case NFA_CLASS_SPACE:
3149 if ((c >=9 && c <= 13) || (c == ' '))
3150 return OK;
3151 break;
3152 case NFA_CLASS_UPPER:
3153 if (MB_ISUPPER(c))
3154 return OK;
3155 break;
3156 case NFA_CLASS_XDIGIT:
3157 if (vim_isxdigit(c))
3158 return OK;
3159 break;
3160 case NFA_CLASS_TAB:
3161 if (c == '\t')
3162 return OK;
3163 break;
3164 case NFA_CLASS_RETURN:
3165 if (c == '\r')
3166 return OK;
3167 break;
3168 case NFA_CLASS_BACKSPACE:
3169 if (c == '\b')
3170 return OK;
3171 break;
3172 case NFA_CLASS_ESCAPE:
3173 if (c == '\033')
3174 return OK;
3175 break;
3176
3177 default:
3178 /* should not be here :P */
3179 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3180 }
3181 return FAIL;
3182}
3183
Bram Moolenaar5714b802013-05-28 22:03:20 +02003184static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3185
3186/*
3187 * Check for a match with subexpression "subidx".
3188 * return TRUE if it matches.
3189 */
3190 static int
3191match_backref(sub, subidx, bytelen)
3192 regsub_T *sub; /* pointers to subexpressions */
3193 int subidx;
3194 int *bytelen; /* out: length of match in bytes */
3195{
3196 int len;
3197
3198 if (sub->in_use <= subidx)
3199 {
3200retempty:
3201 /* backref was not set, match an empty string */
3202 *bytelen = 0;
3203 return TRUE;
3204 }
3205
3206 if (REG_MULTI)
3207 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003208 if (sub->list.multi[subidx].start.lnum < 0
3209 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003210 goto retempty;
3211 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003212 len = sub->list.multi[subidx].end.col
3213 - sub->list.multi[subidx].start.col;
3214 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003215 reginput, &len) == 0)
3216 {
3217 *bytelen = len;
3218 return TRUE;
3219 }
3220 }
3221 else
3222 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003223 if (sub->list.line[subidx].start == NULL
3224 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003225 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003226 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3227 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003228 {
3229 *bytelen = len;
3230 return TRUE;
3231 }
3232 }
3233 return FALSE;
3234}
3235
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236/*
3237 * Set all NFA nodes' list ID equal to -1.
3238 */
3239 static void
3240nfa_set_neg_listids(start)
3241 nfa_state_T *start;
3242{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003243 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 {
3245 start->lastlist = -1;
3246 nfa_set_neg_listids(start->out);
3247 nfa_set_neg_listids(start->out1);
3248 }
3249}
3250
3251/*
3252 * Set all NFA nodes' list ID equal to 0.
3253 */
3254 static void
3255nfa_set_null_listids(start)
3256 nfa_state_T *start;
3257{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003258 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 {
3260 start->lastlist = 0;
3261 nfa_set_null_listids(start->out);
3262 nfa_set_null_listids(start->out1);
3263 }
3264}
3265
3266/*
3267 * Save list IDs for all NFA states in "list".
3268 */
3269 static void
3270nfa_save_listids(start, list)
3271 nfa_state_T *start;
3272 int *list;
3273{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003274 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 {
3276 list[abs(start->id)] = start->lastlist;
3277 start->lastlist = -1;
3278 nfa_save_listids(start->out, list);
3279 nfa_save_listids(start->out1, list);
3280 }
3281}
3282
3283/*
3284 * Restore list IDs from "list" to all NFA states.
3285 */
3286 static void
3287nfa_restore_listids(start, list)
3288 nfa_state_T *start;
3289 int *list;
3290{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003291 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 {
3293 start->lastlist = list[abs(start->id)];
3294 nfa_restore_listids(start->out, list);
3295 nfa_restore_listids(start->out1, list);
3296 }
3297}
3298
Bram Moolenaar423532e2013-05-29 21:14:42 +02003299 static int
3300nfa_re_num_cmp(val, op, pos)
3301 long_u val;
3302 int op;
3303 long_u pos;
3304{
3305 if (op == 1) return pos > val;
3306 if (op == 2) return pos < val;
3307 return val == pos;
3308}
3309
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003310static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3311
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312/*
3313 * Main matching routine.
3314 *
3315 * Run NFA to determine whether it matches reginput.
3316 *
3317 * Return TRUE if there is a match, FALSE otherwise.
3318 * Note: Caller must ensure that: start != NULL.
3319 */
3320 static int
3321nfa_regmatch(start, submatch, m)
3322 nfa_state_T *start;
3323 regsub_T *submatch;
3324 regsub_T *m;
3325{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003326 int result;
3327 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003329 int go_to_nextline = FALSE;
3330 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003331 nfa_list_T list[3];
3332 nfa_list_T *listtbl[2][2];
3333 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003335 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003336 nfa_list_T *thislist;
3337 nfa_list_T *nextlist;
3338 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003340#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003341 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003342
3343 if (debug == NULL)
3344 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003345 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346 return FALSE;
3347 }
3348#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003349 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003351 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003352 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003353 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3354 list[0].len = nstate + 1;
3355 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3356 list[1].len = nstate + 1;
3357 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3358 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003359 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3360 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003361
3362#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003363 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 if (log_fd != NULL)
3365 {
3366 fprintf(log_fd, "**********************************\n");
3367 nfa_set_code(start->c);
3368 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3369 abs(start->id), code);
3370 fprintf(log_fd, "**********************************\n");
3371 }
3372 else
3373 {
3374 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3375 log_fd = stderr;
3376 }
3377#endif
3378
3379 thislist = &list[0];
3380 thislist->n = 0;
3381 nextlist = &list[1];
3382 nextlist->n = 0;
3383 neglist = &list[2];
3384 neglist->n = 0;
3385#ifdef ENABLE_LOG
3386 fprintf(log_fd, "(---) STARTSTATE\n");
3387#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003388 thislist->id = listid;
3389 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003390
3391 /* There are two cases when the NFA advances: 1. input char matches the
3392 * NFA node and 2. input char does not match the NFA node, but the next
3393 * node is NFA_NOT. The following macro calls addstate() according to
3394 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3395 listtbl[0][0] = NULL;
3396 listtbl[0][1] = neglist;
3397 listtbl[1][0] = nextlist;
3398 listtbl[1][1] = NULL;
3399#define ADD_POS_NEG_STATE(node) \
3400 ll = listtbl[result ? 1 : 0][node->negated]; \
3401 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003402 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003403
3404
3405 /*
3406 * Run for each character.
3407 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003408 for (;;)
3409 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003410 int curc;
3411 int clen;
3412
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413#ifdef FEAT_MBYTE
3414 if (has_mbyte)
3415 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003416 curc = (*mb_ptr2char)(reginput);
3417 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 }
3419 else
3420#endif
3421 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003422 curc = *reginput;
3423 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003425 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003426 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003427 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003428 go_to_nextline = FALSE;
3429 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003430
3431 /* swap lists */
3432 thislist = &list[flag];
3433 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003434 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003435 listtbl[1][0] = nextlist;
3436 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003437 thislist->id = listid;
3438 nextlist->id = listid + 1;
3439 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003440
3441#ifdef ENABLE_LOG
3442 fprintf(log_fd, "------------------------------------------\n");
3443 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003444 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003446 {
3447 int i;
3448
3449 for (i = 0; i < thislist->n; i++)
3450 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3451 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003452 fprintf(log_fd, "\n");
3453#endif
3454
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003455#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 fprintf(debug, "\n-------------------\n");
3457#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003458 /*
3459 * If the state lists are empty we can stop.
3460 */
3461 if (thislist->n == 0 && neglist->n == 0)
3462 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003463
3464 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003465 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 {
3467 if (neglist->n > 0)
3468 {
3469 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003470 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003471 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003472 }
3473 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003474 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003476#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003477 nfa_set_code(t->state->c);
3478 fprintf(debug, "%s, ", code);
3479#endif
3480#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003481 {
3482 int col;
3483
3484 if (t->sub.in_use <= 0)
3485 col = -1;
3486 else if (REG_MULTI)
3487 col = t->sub.list.multi[0].start.col;
3488 else
3489 col = (int)(t->sub.list.line[0].start - regline);
3490 nfa_set_code(t->state->c);
3491 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3492 abs(t->state->id), (int)t->state->c, code, col);
3493 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494#endif
3495
3496 /*
3497 * Handle the possible codes of the current state.
3498 * The most important is NFA_MATCH.
3499 */
3500 switch (t->state->c)
3501 {
3502 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003503 {
3504 int j;
3505
Bram Moolenaar963fee22013-05-26 21:47:28 +02003506 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003507 submatch->in_use = t->sub.in_use;
3508 if (REG_MULTI)
3509 for (j = 0; j < submatch->in_use; j++)
3510 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003511 submatch->list.multi[j].start =
3512 t->sub.list.multi[j].start;
3513 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003514 }
3515 else
3516 for (j = 0; j < submatch->in_use; j++)
3517 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003518 submatch->list.line[j].start =
3519 t->sub.list.line[j].start;
3520 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003521 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003522#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003523 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003525 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003526 * states at this position. When the list of states is going
3527 * to be empty quit without advancing, so that "reginput" is
3528 * correct. */
3529 if (nextlist->n == 0 && neglist->n == 0)
3530 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003531 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003532 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003533
3534 case NFA_END_INVISIBLE:
3535 /* This is only encountered after a NFA_START_INVISIBLE node.
3536 * They surround a zero-width group, used with "\@=" and "\&".
3537 * If we got here, it means that the current "invisible" group
3538 * finished successfully, so return control to the parent
3539 * nfa_regmatch(). Submatches are stored in *m, and used in
3540 * the parent call. */
3541 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003542 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003543 else
3544 {
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003545 /* do not set submatches for \@! */
3546 if (!t->state->negated)
3547 /* TODO: only copy positions in use. */
3548 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003549 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003550 }
3551 break;
3552
3553 case NFA_START_INVISIBLE:
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003554 {
3555 char_u *save_reginput = reginput;
3556 char_u *save_regline = regline;
3557 int save_reglnum = reglnum;
3558 int save_nfa_match = nfa_match;
3559
3560 /* Call nfa_regmatch() to check if the current concat matches
3561 * at this position. The concat ends with the node
3562 * NFA_END_INVISIBLE */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003563 if (listids == NULL)
3564 {
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003565 listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003566 if (listids == NULL)
3567 {
3568 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3569 return 0;
3570 }
3571 }
3572#ifdef ENABLE_LOG
3573 if (log_fd != stderr)
3574 fclose(log_fd);
3575 log_fd = NULL;
3576#endif
3577 /* Have to clear the listid field of the NFA nodes, so that
3578 * nfa_regmatch() and addstate() can run properly after
3579 * recursion. */
3580 nfa_save_listids(start, listids);
3581 nfa_set_null_listids(start);
3582 result = nfa_regmatch(t->state->out, submatch, m);
3583 nfa_set_neg_listids(start);
3584 nfa_restore_listids(start, listids);
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003585
3586 /* restore position in input text */
3587 reginput = save_reginput;
3588 regline = save_regline;
3589 reglnum = save_reglnum;
3590 nfa_match = save_nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003591
3592#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003593 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 if (log_fd != NULL)
3595 {
3596 fprintf(log_fd, "****************************\n");
3597 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3598 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3599 fprintf(log_fd, "****************************\n");
3600 }
3601 else
3602 {
3603 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3604 log_fd = stderr;
3605 }
3606#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003607 /* for \@! it is a match when result is FALSE */
3608 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003609 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003610 int j;
3611
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003612 /* Copy submatch info from the recursive call */
3613 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003614 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003616 t->sub.list.multi[j].start = m->list.multi[j].start;
3617 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003618 }
3619 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003620 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003622 t->sub.list.line[j].start = m->list.line[j].start;
3623 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003624 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003625 if (m->in_use > t->sub.in_use)
3626 t->sub.in_use = m->in_use;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003627
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003628 /* t->state->out1 is the corresponding END_INVISIBLE node;
3629 * Add it to the current list (zero-width match). */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003630 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003631 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003633 break;
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003634 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003635
3636 case NFA_BOL:
3637 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003638 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639 break;
3640
3641 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003642 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003643 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 break;
3645
3646 case NFA_BOW:
3647 {
3648 int bow = TRUE;
3649
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003650 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651 bow = FALSE;
3652#ifdef FEAT_MBYTE
3653 else if (has_mbyte)
3654 {
3655 int this_class;
3656
3657 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003658 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003659 if (this_class <= 1)
3660 bow = FALSE;
3661 else if (reg_prev_class() == this_class)
3662 bow = FALSE;
3663 }
3664#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003665 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003666 || (reginput > regline
3667 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003668 bow = FALSE;
3669 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003670 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 break;
3672 }
3673
3674 case NFA_EOW:
3675 {
3676 int eow = TRUE;
3677
3678 if (reginput == regline)
3679 eow = FALSE;
3680#ifdef FEAT_MBYTE
3681 else if (has_mbyte)
3682 {
3683 int this_class, prev_class;
3684
3685 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003686 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003687 prev_class = reg_prev_class();
3688 if (this_class == prev_class
3689 || prev_class == 0 || prev_class == 1)
3690 eow = FALSE;
3691 }
3692#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003693 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003694 || (reginput[0] != NUL
3695 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696 eow = FALSE;
3697 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003698 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003699 break;
3700 }
3701
Bram Moolenaar4b780632013-05-31 22:14:52 +02003702 case NFA_BOF:
3703 if (reglnum == 0 && reginput == regline
3704 && (!REG_MULTI || reg_firstlnum == 1))
3705 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3706 break;
3707
3708 case NFA_EOF:
3709 if (reglnum == reg_maxline && curc == NUL)
3710 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3711 break;
3712
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003713#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003714 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003715 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003716 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003717 int len = 0;
3718 nfa_state_T *end;
3719 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003720 int cchars[MAX_MCO];
3721 int ccount = 0;
3722 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003723
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003725 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003726 if (utf_iscomposing(sta->c))
3727 {
3728 /* Only match composing character(s), ignore base
3729 * character. Used for ".{composing}" and "{composing}"
3730 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003731 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003732 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003733 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003734 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003735 /* If \Z was present, then ignore composing characters.
3736 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003737 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003738 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003739 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003740 else
3741 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003742 while (sta->c != NFA_END_COMPOSING)
3743 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003744 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003745
3746 /* Check base character matches first, unless ignored. */
3747 else if (len > 0 || mc == sta->c)
3748 {
3749 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003750 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003751 len += mb_char2len(mc);
3752 sta = sta->out;
3753 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003755 /* We don't care about the order of composing characters.
3756 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003757 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003758 {
3759 mc = mb_ptr2char(reginput + len);
3760 cchars[ccount++] = mc;
3761 len += mb_char2len(mc);
3762 if (ccount == MAX_MCO)
3763 break;
3764 }
3765
3766 /* Check that each composing char in the pattern matches a
3767 * composing char in the text. We do not check if all
3768 * composing chars are matched. */
3769 result = OK;
3770 while (sta->c != NFA_END_COMPOSING)
3771 {
3772 for (j = 0; j < ccount; ++j)
3773 if (cchars[j] == sta->c)
3774 break;
3775 if (j == ccount)
3776 {
3777 result = FAIL;
3778 break;
3779 }
3780 sta = sta->out;
3781 }
3782 }
3783 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003784 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003785
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003786 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003787 ADD_POS_NEG_STATE(end);
3788 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003789 }
3790#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003791
3792 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003793 if (curc == NUL && !reg_line_lbr && REG_MULTI
3794 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003796 go_to_nextline = TRUE;
3797 /* Pass -1 for the offset, which means taking the position
3798 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003799 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003801 else if (curc == '\n' && reg_line_lbr)
3802 {
3803 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003804 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003805 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003806 break;
3807
3808 case NFA_CLASS_ALNUM:
3809 case NFA_CLASS_ALPHA:
3810 case NFA_CLASS_BLANK:
3811 case NFA_CLASS_CNTRL:
3812 case NFA_CLASS_DIGIT:
3813 case NFA_CLASS_GRAPH:
3814 case NFA_CLASS_LOWER:
3815 case NFA_CLASS_PRINT:
3816 case NFA_CLASS_PUNCT:
3817 case NFA_CLASS_SPACE:
3818 case NFA_CLASS_UPPER:
3819 case NFA_CLASS_XDIGIT:
3820 case NFA_CLASS_TAB:
3821 case NFA_CLASS_RETURN:
3822 case NFA_CLASS_BACKSPACE:
3823 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003824 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003825 ADD_POS_NEG_STATE(t->state);
3826 break;
3827
3828 case NFA_END_NEG_RANGE:
3829 /* This follows a series of negated nodes, like:
3830 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003831 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003832 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003833 break;
3834
3835 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003836 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003837 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003838 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003839 break;
3840
3841 /*
3842 * Character classes like \a for alpha, \d for digit etc.
3843 */
3844 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003845 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846 ADD_POS_NEG_STATE(t->state);
3847 break;
3848
3849 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003850 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003851 ADD_POS_NEG_STATE(t->state);
3852 break;
3853
3854 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003855 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003856 ADD_POS_NEG_STATE(t->state);
3857 break;
3858
3859 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003860 result = !VIM_ISDIGIT(curc)
3861 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003862 ADD_POS_NEG_STATE(t->state);
3863 break;
3864
3865 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003866 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003867 ADD_POS_NEG_STATE(t->state);
3868 break;
3869
3870 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003871 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003872 ADD_POS_NEG_STATE(t->state);
3873 break;
3874
3875 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003876 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003877 ADD_POS_NEG_STATE(t->state);
3878 break;
3879
3880 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003881 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003882 ADD_POS_NEG_STATE(t->state);
3883 break;
3884
3885 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003886 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003887 ADD_POS_NEG_STATE(t->state);
3888 break;
3889
3890 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003891 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003892 ADD_POS_NEG_STATE(t->state);
3893 break;
3894
3895 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003896 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003897 ADD_POS_NEG_STATE(t->state);
3898 break;
3899
3900 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003901 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003902 ADD_POS_NEG_STATE(t->state);
3903 break;
3904
3905 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003906 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003907 ADD_POS_NEG_STATE(t->state);
3908 break;
3909
3910 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003911 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003912 ADD_POS_NEG_STATE(t->state);
3913 break;
3914
3915 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003916 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917 ADD_POS_NEG_STATE(t->state);
3918 break;
3919
3920 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003921 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922 ADD_POS_NEG_STATE(t->state);
3923 break;
3924
3925 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003926 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003927 ADD_POS_NEG_STATE(t->state);
3928 break;
3929
3930 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003931 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003932 ADD_POS_NEG_STATE(t->state);
3933 break;
3934
3935 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003936 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003937 ADD_POS_NEG_STATE(t->state);
3938 break;
3939
3940 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003941 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003942 ADD_POS_NEG_STATE(t->state);
3943 break;
3944
3945 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003946 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003947 ADD_POS_NEG_STATE(t->state);
3948 break;
3949
3950 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003951 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003952 ADD_POS_NEG_STATE(t->state);
3953 break;
3954
3955 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003956 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003957 ADD_POS_NEG_STATE(t->state);
3958 break;
3959
3960 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003961 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003962 ADD_POS_NEG_STATE(t->state);
3963 break;
3964
3965 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003966 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003967 ADD_POS_NEG_STATE(t->state);
3968 break;
3969
3970 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003971 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003972 ADD_POS_NEG_STATE(t->state);
3973 break;
3974
Bram Moolenaar5714b802013-05-28 22:03:20 +02003975 case NFA_BACKREF1:
3976 case NFA_BACKREF2:
3977 case NFA_BACKREF3:
3978 case NFA_BACKREF4:
3979 case NFA_BACKREF5:
3980 case NFA_BACKREF6:
3981 case NFA_BACKREF7:
3982 case NFA_BACKREF8:
3983 case NFA_BACKREF9:
3984 /* \1 .. \9 */
3985 {
3986 int subidx = t->state->c - NFA_BACKREF1 + 1;
3987 int bytelen;
3988
3989 result = match_backref(&t->sub, subidx, &bytelen);
3990 if (result)
3991 {
3992 if (bytelen == 0)
3993 {
3994 /* empty match always works, add NFA_SKIP with zero to
3995 * be used next */
3996 addstate_here(thislist, t->state->out, &t->sub,
3997 &listidx);
3998 thislist->t[listidx + 1].count = 0;
3999 }
4000 else if (bytelen <= clen)
4001 {
4002 /* match current character, jump ahead to out of
4003 * NFA_SKIP */
4004 addstate(nextlist, t->state->out->out, &t->sub, clen);
4005#ifdef ENABLE_LOG
4006 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4007#endif
4008 }
4009 else
4010 {
4011 /* skip ofer the matched characters, set character
4012 * count in NFA_SKIP */
4013 addstate(nextlist, t->state->out, &t->sub, bytelen);
4014 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4015#ifdef ENABLE_LOG
4016 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4017#endif
4018 }
4019
4020 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004021 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004022 }
4023 case NFA_SKIP:
4024 /* charater of previous matching \1 .. \9 */
4025 if (t->count - clen <= 0)
4026 {
4027 /* end of match, go to what follows */
4028 addstate(nextlist, t->state->out, &t->sub, clen);
4029#ifdef ENABLE_LOG
4030 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4031#endif
4032 }
4033 else
4034 {
4035 /* add state again with decremented count */
4036 addstate(nextlist, t->state, &t->sub, 0);
4037 nextlist->t[nextlist->n - 1].count = t->count - clen;
4038#ifdef ENABLE_LOG
4039 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4040#endif
4041 }
4042 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004043
4044 case NFA_SKIP_CHAR:
4045 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004046 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004047 /* TODO: should not happen? */
4048 break;
4049
Bram Moolenaar423532e2013-05-29 21:14:42 +02004050 case NFA_LNUM:
4051 case NFA_LNUM_GT:
4052 case NFA_LNUM_LT:
4053 result = (REG_MULTI &&
4054 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4055 (long_u)(reglnum + reg_firstlnum)));
4056 if (result)
4057 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4058 break;
4059
4060 case NFA_COL:
4061 case NFA_COL_GT:
4062 case NFA_COL_LT:
4063 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4064 (long_u)(reginput - regline) + 1);
4065 if (result)
4066 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4067 break;
4068
4069 case NFA_VCOL:
4070 case NFA_VCOL_GT:
4071 case NFA_VCOL_LT:
4072 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4073 (long_u)win_linetabsize(
4074 reg_win == NULL ? curwin : reg_win,
4075 regline, (colnr_T)(reginput - regline)) + 1);
4076 if (result)
4077 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4078 break;
4079
4080 case NFA_CURSOR:
4081 result = (reg_win != NULL
4082 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4083 && ((colnr_T)(reginput - regline)
4084 == reg_win->w_cursor.col));
4085 if (result)
4086 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4087 break;
4088
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004090 {
4091 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004092
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004093 /* TODO: put this in #ifdef later */
4094 if (c < -256)
4095 EMSGN("INTERNAL: Negative state char: %ld", c);
4096 if (is_Magic(c))
4097 c = un_Magic(c);
4098 result = (c == curc);
4099
4100 if (!result && ireg_ic)
4101 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004102#ifdef FEAT_MBYTE
4103 /* If there is a composing character which is not being
4104 * ignored there can be no match. Match with composing
4105 * character uses NFA_COMPOSING above. */
4106 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004107 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004108 result = FALSE;
4109#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004110 ADD_POS_NEG_STATE(t->state);
4111 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004112 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 }
4114
4115 } /* for (thislist = thislist; thislist->state; thislist++) */
4116
Bram Moolenaare23febd2013-05-26 18:40:14 +02004117 /* Look for the start of a match in the current position by adding the
4118 * start state to the list of states.
4119 * The first found match is the leftmost one, thus the order of states
4120 * matters!
4121 * Do not add the start state in recursive calls of nfa_regmatch(),
4122 * because recursive calls should only start in the first position.
4123 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004124 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004125 && reglnum == 0 && clen != 0
4126 && (ireg_maxcol == 0
4127 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004128 {
4129#ifdef ENABLE_LOG
4130 fprintf(log_fd, "(---) STARTSTATE\n");
4131#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004132 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004133 }
4134
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135#ifdef ENABLE_LOG
4136 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004137 {
4138 int i;
4139
4140 for (i = 0; i < thislist->n; i++)
4141 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4142 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004143 fprintf(log_fd, "\n");
4144#endif
4145
4146nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004147 /* Advance to the next character, or advance to the next line, or
4148 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004149 if (clen != 0)
4150 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004151 else if (go_to_nextline)
4152 reg_nextline();
4153 else
4154 break;
4155 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004156
4157#ifdef ENABLE_LOG
4158 if (log_fd != stderr)
4159 fclose(log_fd);
4160 log_fd = NULL;
4161#endif
4162
4163theend:
4164 /* Free memory */
4165 vim_free(list[0].t);
4166 vim_free(list[1].t);
4167 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004168 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004169#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004170#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004171 fclose(debug);
4172#endif
4173
Bram Moolenaar963fee22013-05-26 21:47:28 +02004174 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004175}
4176
4177/*
4178 * Try match of "prog" with at regline["col"].
4179 * Returns 0 for failure, number of lines contained in the match otherwise.
4180 */
4181 static long
4182nfa_regtry(start, col)
4183 nfa_state_T *start;
4184 colnr_T col;
4185{
4186 int i;
4187 regsub_T sub, m;
4188#ifdef ENABLE_LOG
4189 FILE *f;
4190#endif
4191
4192 reginput = regline + col;
4193 need_clear_subexpr = TRUE;
4194
4195#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004196 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004197 if (f != NULL)
4198 {
4199 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4200 fprintf(f, " =======================================================\n");
4201#ifdef DEBUG
4202 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4203#endif
4204 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004205 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004206 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004207 fprintf(f, "\n\n");
4208 fclose(f);
4209 }
4210 else
4211 EMSG(_("Could not open temporary log file for writing "));
4212#endif
4213
4214 if (REG_MULTI)
4215 {
4216 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004217 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4218 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004219 }
4220 else
4221 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004222 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4223 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004224 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004225 sub.in_use = 0;
4226 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004227
4228 if (nfa_regmatch(start, &sub, &m) == FALSE)
4229 return 0;
4230
4231 cleanup_subexpr();
4232 if (REG_MULTI)
4233 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004234 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004236 reg_startpos[i] = sub.list.multi[i].start;
4237 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004238 }
4239
4240 if (reg_startpos[0].lnum < 0)
4241 {
4242 reg_startpos[0].lnum = 0;
4243 reg_startpos[0].col = col;
4244 }
4245 if (reg_endpos[0].lnum < 0)
4246 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004247 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248 reg_endpos[0].lnum = reglnum;
4249 reg_endpos[0].col = (int)(reginput - regline);
4250 }
4251 else
4252 /* Use line number of "\ze". */
4253 reglnum = reg_endpos[0].lnum;
4254 }
4255 else
4256 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004257 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004258 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004259 reg_startp[i] = sub.list.line[i].start;
4260 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004261 }
4262
4263 if (reg_startp[0] == NULL)
4264 reg_startp[0] = regline + col;
4265 if (reg_endp[0] == NULL)
4266 reg_endp[0] = reginput;
4267 }
4268
4269 return 1 + reglnum;
4270}
4271
4272/*
4273 * Match a regexp against a string ("line" points to the string) or multiple
4274 * lines ("line" is NULL, use reg_getline()).
4275 *
4276 * Returns 0 for failure, number of lines contained in the match otherwise.
4277 */
4278 static long
4279nfa_regexec_both(line, col)
4280 char_u *line;
4281 colnr_T col; /* column to start looking for match */
4282{
4283 nfa_regprog_T *prog;
4284 long retval = 0L;
4285 int i;
4286
4287 if (REG_MULTI)
4288 {
4289 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4290 line = reg_getline((linenr_T)0); /* relative to the cursor */
4291 reg_startpos = reg_mmatch->startpos;
4292 reg_endpos = reg_mmatch->endpos;
4293 }
4294 else
4295 {
4296 prog = (nfa_regprog_T *)reg_match->regprog;
4297 reg_startp = reg_match->startp;
4298 reg_endp = reg_match->endp;
4299 }
4300
4301 /* Be paranoid... */
4302 if (prog == NULL || line == NULL)
4303 {
4304 EMSG(_(e_null));
4305 goto theend;
4306 }
4307
4308 /* If the start column is past the maximum column: no need to try. */
4309 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4310 goto theend;
4311
4312 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4313 if (prog->regflags & RF_ICASE)
4314 ireg_ic = TRUE;
4315 else if (prog->regflags & RF_NOICASE)
4316 ireg_ic = FALSE;
4317
4318#ifdef FEAT_MBYTE
4319 /* If pattern contains "\Z" overrule value of ireg_icombine */
4320 if (prog->regflags & RF_ICOMBINE)
4321 ireg_icombine = TRUE;
4322#endif
4323
4324 regline = line;
4325 reglnum = 0; /* relative to line */
4326
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004327 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004328 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004329 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004331 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004332 for (i = 0; i < nstate; ++i)
4333 {
4334 prog->state[i].id = i;
4335 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336 }
4337
4338 retval = nfa_regtry(prog->start, col);
4339
4340theend:
4341 return retval;
4342}
4343
4344/*
4345 * Compile a regular expression into internal code for the NFA matcher.
4346 * Returns the program in allocated space. Returns NULL for an error.
4347 */
4348 static regprog_T *
4349nfa_regcomp(expr, re_flags)
4350 char_u *expr;
4351 int re_flags;
4352{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004353 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004354 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004355 int *postfix;
4356
4357 if (expr == NULL)
4358 return NULL;
4359
4360#ifdef DEBUG
4361 nfa_regengine.expr = expr;
4362#endif
4363
4364 init_class_tab();
4365
4366 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4367 return NULL;
4368
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004369 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004370 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004371 postfix = re2post();
4372 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004373 {
4374 /* TODO: only give this error for debugging? */
4375 if (post_ptr >= post_end)
4376 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004377 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004378 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004379
4380 /*
4381 * In order to build the NFA, we parse the input regexp twice:
4382 * 1. first pass to count size (so we can allocate space)
4383 * 2. second to emit code
4384 */
4385#ifdef ENABLE_LOG
4386 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004387 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388
4389 if (f != NULL)
4390 {
4391 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4392 fclose(f);
4393 }
4394 }
4395#endif
4396
4397 /*
4398 * PASS 1
4399 * Count number of NFA states in "nstate". Do not build the NFA.
4400 */
4401 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004402
4403 /* Space for compiled regexp */
4404 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4405 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4406 if (prog == NULL)
4407 goto fail;
4408 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004409 state_ptr = prog->state;
4410
4411 /*
4412 * PASS 2
4413 * Build the NFA
4414 */
4415 prog->start = post2nfa(postfix, post_ptr, FALSE);
4416 if (prog->start == NULL)
4417 goto fail;
4418
4419 prog->regflags = regflags;
4420 prog->engine = &nfa_regengine;
4421 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004422 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004423 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004424 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004425#ifdef ENABLE_LOG
4426 nfa_postfix_dump(expr, OK);
4427 nfa_dump(prog);
4428#endif
4429
4430out:
4431 vim_free(post_start);
4432 post_start = post_ptr = post_end = NULL;
4433 state_ptr = NULL;
4434 return (regprog_T *)prog;
4435
4436fail:
4437 vim_free(prog);
4438 prog = NULL;
4439#ifdef ENABLE_LOG
4440 nfa_postfix_dump(expr, FAIL);
4441#endif
4442#ifdef DEBUG
4443 nfa_regengine.expr = NULL;
4444#endif
4445 goto out;
4446}
4447
4448
4449/*
4450 * Match a regexp against a string.
4451 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4452 * Uses curbuf for line count and 'iskeyword'.
4453 *
4454 * Return TRUE if there is a match, FALSE if not.
4455 */
4456 static int
4457nfa_regexec(rmp, line, col)
4458 regmatch_T *rmp;
4459 char_u *line; /* string to match against */
4460 colnr_T col; /* column to start looking for match */
4461{
4462 reg_match = rmp;
4463 reg_mmatch = NULL;
4464 reg_maxline = 0;
4465 reg_line_lbr = FALSE;
4466 reg_buf = curbuf;
4467 reg_win = NULL;
4468 ireg_ic = rmp->rm_ic;
4469#ifdef FEAT_MBYTE
4470 ireg_icombine = FALSE;
4471#endif
4472 ireg_maxcol = 0;
4473 return (nfa_regexec_both(line, col) != 0);
4474}
4475
4476#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4477 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4478
4479static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4480
4481/*
4482 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4483 */
4484 static int
4485nfa_regexec_nl(rmp, line, col)
4486 regmatch_T *rmp;
4487 char_u *line; /* string to match against */
4488 colnr_T col; /* column to start looking for match */
4489{
4490 reg_match = rmp;
4491 reg_mmatch = NULL;
4492 reg_maxline = 0;
4493 reg_line_lbr = TRUE;
4494 reg_buf = curbuf;
4495 reg_win = NULL;
4496 ireg_ic = rmp->rm_ic;
4497#ifdef FEAT_MBYTE
4498 ireg_icombine = FALSE;
4499#endif
4500 ireg_maxcol = 0;
4501 return (nfa_regexec_both(line, col) != 0);
4502}
4503#endif
4504
4505
4506/*
4507 * Match a regexp against multiple lines.
4508 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4509 * Uses curbuf for line count and 'iskeyword'.
4510 *
4511 * Return zero if there is no match. Return number of lines contained in the
4512 * match otherwise.
4513 *
4514 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4515 *
4516 * ! Also NOTE : match may actually be in another line. e.g.:
4517 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4518 *
4519 * +-------------------------+
4520 * |a |
4521 * |b |
4522 * |c |
4523 * | |
4524 * +-------------------------+
4525 *
4526 * then nfa_regexec_multi() returns 3. while the original
4527 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4528 *
4529 * FIXME if this behavior is not compatible.
4530 */
4531 static long
4532nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4533 regmmatch_T *rmp;
4534 win_T *win; /* window in which to search or NULL */
4535 buf_T *buf; /* buffer in which to search */
4536 linenr_T lnum; /* nr of line to start looking for match */
4537 colnr_T col; /* column to start looking for match */
4538 proftime_T *tm UNUSED; /* timeout limit or NULL */
4539{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004540 reg_match = NULL;
4541 reg_mmatch = rmp;
4542 reg_buf = buf;
4543 reg_win = win;
4544 reg_firstlnum = lnum;
4545 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4546 reg_line_lbr = FALSE;
4547 ireg_ic = rmp->rmm_ic;
4548#ifdef FEAT_MBYTE
4549 ireg_icombine = FALSE;
4550#endif
4551 ireg_maxcol = rmp->rmm_maxcol;
4552
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004553 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554}
4555
4556#ifdef DEBUG
4557# undef ENABLE_LOG
4558#endif