blob: 753733d2b6df7b6a1bdc447232fd2398499e7b4c [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 }
1092 /* Try a range like 'a-x' or '\t-z' */
1093 if (*regparse == '-')
1094 {
1095 emit_range = TRUE;
1096 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001097 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001098 continue; /* reading the end of the range */
1099 }
1100
1101 /* Now handle simple and escaped characters.
1102 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1103 * accepts "\t", "\e", etc., but only when the 'l' flag in
1104 * 'cpoptions' is not included.
1105 * Posix doesn't recognize backslash at all.
1106 */
1107 if (*regparse == '\\'
1108 && !cpo_bsl
1109 && regparse + 1 <= endp
1110 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1111 || (!cpo_lit
1112 && vim_strchr(REGEXP_ABBR, regparse[1])
1113 != NULL)
1114 )
1115 )
1116 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001117 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001118
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001119 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001120 startc = reg_string ? NL : NFA_NEWL;
1121 else
1122 if (*regparse == 'd'
1123 || *regparse == 'o'
1124 || *regparse == 'x'
1125 || *regparse == 'u'
1126 || *regparse == 'U'
1127 )
1128 {
1129 /* TODO(RE) This needs more testing */
1130 startc = coll_get_char();
1131 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001132 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001133 }
1134 else
1135 {
1136 /* \r,\t,\e,\b */
1137 startc = backslash_trans(*regparse);
1138 }
1139 }
1140
1141 /* Normal printable char */
1142 if (startc == -1)
1143#ifdef FEAT_MBYTE
1144 startc = (*mb_ptr2char)(regparse);
1145#else
1146 startc = *regparse;
1147#endif
1148
1149 /* Previous char was '-', so this char is end of range. */
1150 if (emit_range)
1151 {
1152 endc = startc; startc = oldstartc;
1153 if (startc > endc)
1154 EMSG_RET_FAIL(_(e_invrange));
1155#ifdef FEAT_MBYTE
1156 if (has_mbyte && ((*mb_char2len)(startc) > 1
1157 || (*mb_char2len)(endc) > 1))
1158 {
1159 if (endc > startc + 256)
1160 EMSG_RET_FAIL(_(e_invrange));
1161 /* Emit the range. "startc" was already emitted, so
1162 * skip it. */
1163 for (c = startc + 1; c <= endc; c++)
1164 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001165 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001166 TRY_NEG();
1167 EMIT_GLUE();
1168 }
1169 emit_range = FALSE;
1170 }
1171 else
1172#endif
1173 {
1174#ifdef EBCDIC
1175 int alpha_only = FALSE;
1176
1177 /* for alphabetical range skip the gaps
1178 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1179 if (isalpha(startc) && isalpha(endc))
1180 alpha_only = TRUE;
1181#endif
1182 /* Emit the range. "startc" was already emitted, so
1183 * skip it. */
1184 for (c = startc + 1; c <= endc; c++)
1185#ifdef EBCDIC
1186 if (!alpha_only || isalpha(startc))
1187#endif
1188 {
1189 EMIT(c);
1190 TRY_NEG();
1191 EMIT_GLUE();
1192 }
1193 emit_range = FALSE;
1194 }
1195 }
1196 else
1197 {
1198 /*
1199 * This char (startc) is not part of a range. Just
1200 * emit it.
1201 *
1202 * Normally, simply emit startc. But if we get char
1203 * code=0 from a collating char, then replace it with
1204 * 0x0a.
1205 *
1206 * This is needed to completely mimic the behaviour of
1207 * the backtracking engine.
1208 */
1209 if (got_coll_char == TRUE && startc == 0)
1210 EMIT(0x0a);
1211 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001212 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 TRY_NEG();
1214 EMIT_GLUE();
1215 }
1216
Bram Moolenaar51a29832013-05-28 22:30:35 +02001217 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001218 } /* while (p < endp) */
1219
Bram Moolenaar51a29832013-05-28 22:30:35 +02001220 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 if (*regparse == '-') /* if last, '-' is just a char */
1222 {
1223 EMIT('-');
1224 TRY_NEG();
1225 EMIT_GLUE();
1226 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001227 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001229 /* skip the trailing ] */
1230 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001231 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001232 if (negated == TRUE)
1233 {
1234 /* Mark end of negated char range */
1235 EMIT(NFA_END_NEG_RANGE);
1236 EMIT(NFA_CONCAT);
1237 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001238
1239 /* \_[] also matches \n but it's not negated */
1240 if (extra == ADD_NL)
1241 {
1242 EMIT(reg_string ? NL : NFA_NEWL);
1243 EMIT(NFA_OR);
1244 }
1245
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001247 } /* if exists closing ] */
1248
1249 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001250 {
1251 syntax_error = TRUE;
1252 EMSG_RET_FAIL(_(e_missingbracket));
1253 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001254 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256 default:
1257 {
1258#ifdef FEAT_MBYTE
1259 int plen;
1260
1261nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001262 /* plen is length of current char with composing chars */
1263 if (enc_utf8 && ((*mb_char2len)(c)
1264 != (plen = (*mb_ptr2len)(old_regparse))
1265 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001266 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001267 int i = 0;
1268
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001269 /* A base character plus composing characters, or just one
1270 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001271 * This requires creating a separate atom as if enclosing
1272 * the characters in (), where NFA_COMPOSING is the ( and
1273 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001274 * building the postfix form, not the NFA itself;
1275 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001276 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001277 for (;;)
1278 {
1279 EMIT(c);
1280 if (i > 0)
1281 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001282 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001283 break;
1284 c = utf_ptr2char(old_regparse + i);
1285 }
1286 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001287 regparse = old_regparse + plen;
1288 }
1289 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001290#endif
1291 {
1292 c = no_Magic(c);
1293 EMIT(c);
1294 }
1295 return OK;
1296 }
1297 }
1298
1299#undef TRY_NEG
1300#undef EMIT_GLUE
1301
1302 return OK;
1303}
1304
1305/*
1306 * Parse something followed by possible [*+=].
1307 *
1308 * A piece is an atom, possibly followed by a multi, an indication of how many
1309 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1310 * characters: "", "a", "aa", etc.
1311 *
1312 * piece ::= atom
1313 * or atom multi
1314 */
1315 static int
1316nfa_regpiece()
1317{
1318 int i;
1319 int op;
1320 int ret;
1321 long minval, maxval;
1322 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1323 char_u *old_regparse, *new_regparse;
1324 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001325 int old_post_pos;
1326 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001327 int old_regnpar;
1328 int quest;
1329
1330 /* Save the current position in the regexp, so that we can use it if
1331 * <atom>{m,n} is next. */
1332 old_regparse = regparse;
1333 /* Save current number of open parenthesis, so we can use it if
1334 * <atom>{m,n} is next */
1335 old_regnpar = regnpar;
1336 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001337 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001338
1339 ret = nfa_regatom();
1340 if (ret == FAIL)
1341 return FAIL; /* cascaded error */
1342
1343 op = peekchr();
1344 if (re_multi_type(op) == NOT_MULTI)
1345 return OK;
1346
1347 skipchr();
1348 switch (op)
1349 {
1350 case Magic('*'):
1351 EMIT(NFA_STAR);
1352 break;
1353
1354 case Magic('+'):
1355 /*
1356 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1357 * first and only submatch would be "aaa". But the backtracking
1358 * engine interprets the plus as "try matching one more time", and
1359 * a* matches a second time at the end of the input, the empty
1360 * string.
1361 * The submatch will the empty string.
1362 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001363 * In order to be consistent with the old engine, we replace
1364 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001365 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 regnpar = old_regnpar;
1367 regparse = old_regparse;
1368 curchr = -1;
1369 if (nfa_regatom() == FAIL)
1370 return FAIL;
1371 EMIT(NFA_STAR);
1372 EMIT(NFA_CONCAT);
1373 skipchr(); /* skip the \+ */
1374 break;
1375
1376 case Magic('@'):
1377 op = no_Magic(getchr());
1378 switch(op)
1379 {
1380 case '=':
1381 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1382 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001383 case '!':
1384 EMIT(NFA_PREV_ATOM_NO_WIDTH_NEG);
1385 break;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001386 case '0':
1387 case '1':
1388 case '2':
1389 case '3':
1390 case '4':
1391 case '5':
1392 case '6':
1393 case '7':
1394 case '8':
1395 case '9':
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001396 case '<':
1397 case '>':
1398 /* Not supported yet */
1399 return FAIL;
1400 default:
1401 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001402 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 return FAIL;
1404 }
1405 break;
1406
1407 case Magic('?'):
1408 case Magic('='):
1409 EMIT(NFA_QUEST);
1410 break;
1411
1412 case Magic('{'):
1413 /* a{2,5} will expand to 'aaa?a?a?'
1414 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1415 * version of '?'
1416 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1417 * parenthesis have the same id
1418 */
1419
1420 greedy = TRUE;
1421 c2 = peekchr();
1422 if (c2 == '-' || c2 == Magic('-'))
1423 {
1424 skipchr();
1425 greedy = FALSE;
1426 }
1427 if (!read_limits(&minval, &maxval))
1428 {
1429 syntax_error = TRUE;
1430 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1431 }
1432 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1433 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001434 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001435 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001436 if (greedy)
1437 /* \{}, \{0,} */
1438 EMIT(NFA_STAR);
1439 else
1440 /* \{-}, \{-0,} */
1441 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 break;
1443 }
1444
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445 /* Special case: x{0} or x{-0} */
1446 if (maxval == 0)
1447 {
1448 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001449 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1451 EMIT(NFA_SKIP_CHAR);
1452 return OK;
1453 }
1454
1455 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001456 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001457 /* Save pos after the repeated atom and the \{} */
1458 new_regparse = regparse;
1459
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001460 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1461 for (i = 0; i < maxval; i++)
1462 {
1463 /* Goto beginning of the repeated atom */
1464 regparse = old_regparse;
1465 curchr = -1;
1466 /* Restore count of parenthesis */
1467 regnpar = old_regnpar;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001468 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001469 if (nfa_regatom() == FAIL)
1470 return FAIL;
1471 /* after "minval" times, atoms are optional */
1472 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001473 {
1474 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001475 {
1476 if (greedy)
1477 EMIT(NFA_STAR);
1478 else
1479 EMIT(NFA_STAR_NONGREEDY);
1480 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001481 else
1482 EMIT(quest);
1483 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001484 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001486 if (i + 1 > minval && maxval == MAX_LIMIT)
1487 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001488 }
1489
1490 /* Go to just after the repeated atom and the \{} */
1491 regparse = new_regparse;
1492 curchr = -1;
1493
1494 break;
1495
1496
1497 default:
1498 break;
1499 } /* end switch */
1500
1501 if (re_multi_type(peekchr()) != NOT_MULTI)
1502 {
1503 /* Can't have a multi follow a multi. */
1504 syntax_error = TRUE;
1505 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1506 }
1507
1508 return OK;
1509}
1510
1511/*
1512 * Parse one or more pieces, concatenated. It matches a match for the
1513 * first piece, followed by a match for the second piece, etc. Example:
1514 * "f[0-9]b", first matches "f", then a digit and then "b".
1515 *
1516 * concat ::= piece
1517 * or piece piece
1518 * or piece piece piece
1519 * etc.
1520 */
1521 static int
1522nfa_regconcat()
1523{
1524 int cont = TRUE;
1525 int first = TRUE;
1526
1527 while (cont)
1528 {
1529 switch (peekchr())
1530 {
1531 case NUL:
1532 case Magic('|'):
1533 case Magic('&'):
1534 case Magic(')'):
1535 cont = FALSE;
1536 break;
1537
1538 case Magic('Z'):
1539#ifdef FEAT_MBYTE
1540 regflags |= RF_ICOMBINE;
1541#endif
1542 skipchr_keepstart();
1543 break;
1544 case Magic('c'):
1545 regflags |= RF_ICASE;
1546 skipchr_keepstart();
1547 break;
1548 case Magic('C'):
1549 regflags |= RF_NOICASE;
1550 skipchr_keepstart();
1551 break;
1552 case Magic('v'):
1553 reg_magic = MAGIC_ALL;
1554 skipchr_keepstart();
1555 curchr = -1;
1556 break;
1557 case Magic('m'):
1558 reg_magic = MAGIC_ON;
1559 skipchr_keepstart();
1560 curchr = -1;
1561 break;
1562 case Magic('M'):
1563 reg_magic = MAGIC_OFF;
1564 skipchr_keepstart();
1565 curchr = -1;
1566 break;
1567 case Magic('V'):
1568 reg_magic = MAGIC_NONE;
1569 skipchr_keepstart();
1570 curchr = -1;
1571 break;
1572
1573 default:
1574 if (nfa_regpiece() == FAIL)
1575 return FAIL;
1576 if (first == FALSE)
1577 EMIT(NFA_CONCAT);
1578 else
1579 first = FALSE;
1580 break;
1581 }
1582 }
1583
1584 return OK;
1585}
1586
1587/*
1588 * Parse a branch, one or more concats, separated by "\&". It matches the
1589 * last concat, but only if all the preceding concats also match at the same
1590 * position. Examples:
1591 * "foobeep\&..." matches "foo" in "foobeep".
1592 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1593 *
1594 * branch ::= concat
1595 * or concat \& concat
1596 * or concat \& concat \& concat
1597 * etc.
1598 */
1599 static int
1600nfa_regbranch()
1601{
1602 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001603 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604
Bram Moolenaar16299b52013-05-30 18:45:23 +02001605 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001606
1607 /* First branch, possibly the only one */
1608 if (nfa_regconcat() == FAIL)
1609 return FAIL;
1610
1611 ch = peekchr();
1612 /* Try next concats */
1613 while (ch == Magic('&'))
1614 {
1615 skipchr();
1616 EMIT(NFA_NOPEN);
1617 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001618 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 if (nfa_regconcat() == FAIL)
1620 return FAIL;
1621 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001622 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001623 EMIT(NFA_SKIP_CHAR);
1624 EMIT(NFA_CONCAT);
1625 ch = peekchr();
1626 }
1627
1628 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001629 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001630 EMIT(NFA_SKIP_CHAR);
1631
1632 return OK;
1633}
1634
1635/*
1636 * Parse a pattern, one or more branches, separated by "\|". It matches
1637 * anything that matches one of the branches. Example: "foo\|beep" matches
1638 * "foo" and matches "beep". If more than one branch matches, the first one
1639 * is used.
1640 *
1641 * pattern ::= branch
1642 * or branch \| branch
1643 * or branch \| branch \| branch
1644 * etc.
1645 */
1646 static int
1647nfa_reg(paren)
1648 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1649{
1650 int parno = 0;
1651
1652#ifdef FEAT_SYN_HL
1653#endif
1654 if (paren == REG_PAREN)
1655 {
1656 if (regnpar >= NSUBEXP) /* Too many `(' */
1657 {
1658 syntax_error = TRUE;
1659 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1660 }
1661 parno = regnpar++;
1662 }
1663
1664 if (nfa_regbranch() == FAIL)
1665 return FAIL; /* cascaded error */
1666
1667 while (peekchr() == Magic('|'))
1668 {
1669 skipchr();
1670 if (nfa_regbranch() == FAIL)
1671 return FAIL; /* cascaded error */
1672 EMIT(NFA_OR);
1673 }
1674
1675 /* Check for proper termination. */
1676 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1677 {
1678 syntax_error = TRUE;
1679 if (paren == REG_NPAREN)
1680 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1681 else
1682 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1683 }
1684 else if (paren == REG_NOPAREN && peekchr() != NUL)
1685 {
1686 syntax_error = TRUE;
1687 if (peekchr() == Magic(')'))
1688 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1689 else
1690 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1691 }
1692 /*
1693 * Here we set the flag allowing back references to this set of
1694 * parentheses.
1695 */
1696 if (paren == REG_PAREN)
1697 {
1698 had_endbrace[parno] = TRUE; /* have seen the close paren */
1699 EMIT(NFA_MOPEN + parno);
1700 }
1701
1702 return OK;
1703}
1704
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001705#ifdef DEBUG
1706static char_u code[50];
1707
1708 static void
1709nfa_set_code(c)
1710 int c;
1711{
1712 int addnl = FALSE;
1713
1714 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1715 {
1716 addnl = TRUE;
1717 c -= ADD_NL;
1718 }
1719
1720 STRCPY(code, "");
1721 switch (c)
1722 {
1723 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1724 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1725 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1726 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1727 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1728 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1729
Bram Moolenaar5714b802013-05-28 22:03:20 +02001730 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1731 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1732 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1733 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1734 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1735 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1736 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1737 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1738 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1739 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1740
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001741 case NFA_PREV_ATOM_NO_WIDTH:
1742 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001743 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1744 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001745 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1746 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1748 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1749
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001750 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1751 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1752
1753 case NFA_MOPEN + 0:
1754 case NFA_MOPEN + 1:
1755 case NFA_MOPEN + 2:
1756 case NFA_MOPEN + 3:
1757 case NFA_MOPEN + 4:
1758 case NFA_MOPEN + 5:
1759 case NFA_MOPEN + 6:
1760 case NFA_MOPEN + 7:
1761 case NFA_MOPEN + 8:
1762 case NFA_MOPEN + 9:
1763 STRCPY(code, "NFA_MOPEN(x)");
1764 code[10] = c - NFA_MOPEN + '0';
1765 break;
1766 case NFA_MCLOSE + 0:
1767 case NFA_MCLOSE + 1:
1768 case NFA_MCLOSE + 2:
1769 case NFA_MCLOSE + 3:
1770 case NFA_MCLOSE + 4:
1771 case NFA_MCLOSE + 5:
1772 case NFA_MCLOSE + 6:
1773 case NFA_MCLOSE + 7:
1774 case NFA_MCLOSE + 8:
1775 case NFA_MCLOSE + 9:
1776 STRCPY(code, "NFA_MCLOSE(x)");
1777 code[11] = c - NFA_MCLOSE + '0';
1778 break;
1779 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1780 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1781 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1782 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001783 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1784 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001785 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001786 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1787 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1788 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1790 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1791 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001792 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1793 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1794 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1795 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1796 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1797 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1798 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1799 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1800 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1801 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1802 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1803 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1804 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1805 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1806 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1807 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1808 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1809
1810 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1811 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1812 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1813 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1814 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1815 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1816 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1817 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1818 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1819 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1820 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1821 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1822 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1823 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1824 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1825 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1826 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1827 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1828 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1829 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1830 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1831 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1832 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1833 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1834 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1835 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1836 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1837
1838 default:
1839 STRCPY(code, "CHAR(x)");
1840 code[5] = c;
1841 }
1842
1843 if (addnl == TRUE)
1844 STRCAT(code, " + NEWLINE ");
1845
1846}
1847
1848#ifdef ENABLE_LOG
1849static FILE *log_fd;
1850
1851/*
1852 * Print the postfix notation of the current regexp.
1853 */
1854 static void
1855nfa_postfix_dump(expr, retval)
1856 char_u *expr;
1857 int retval;
1858{
1859 int *p;
1860 FILE *f;
1861
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001862 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001863 if (f != NULL)
1864 {
1865 fprintf(f, "\n-------------------------\n");
1866 if (retval == FAIL)
1867 fprintf(f, ">>> NFA engine failed ... \n");
1868 else if (retval == OK)
1869 fprintf(f, ">>> NFA engine succeeded !\n");
1870 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001871 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872 {
1873 nfa_set_code(*p);
1874 fprintf(f, "%s, ", code);
1875 }
1876 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001877 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001878 fprintf(f, "%d ", *p);
1879 fprintf(f, "\n\n");
1880 fclose(f);
1881 }
1882}
1883
1884/*
1885 * Print the NFA starting with a root node "state".
1886 */
1887 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001888nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889 FILE *debugf;
1890 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001891{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001892 garray_T indent;
1893
1894 ga_init2(&indent, 1, 64);
1895 ga_append(&indent, '\0');
1896 nfa_print_state2(debugf, state, &indent);
1897 ga_clear(&indent);
1898}
1899
1900 static void
1901nfa_print_state2(debugf, state, indent)
1902 FILE *debugf;
1903 nfa_state_T *state;
1904 garray_T *indent;
1905{
1906 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907
1908 if (state == NULL)
1909 return;
1910
1911 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001912
1913 /* Output indent */
1914 p = (char_u *)indent->ga_data;
1915 if (indent->ga_len >= 3)
1916 {
1917 int last = indent->ga_len - 3;
1918 char_u save[2];
1919
1920 STRNCPY(save, &p[last], 2);
1921 STRNCPY(&p[last], "+-", 2);
1922 fprintf(debugf, " %s", p);
1923 STRNCPY(&p[last], save, 2);
1924 }
1925 else
1926 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927
1928 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001929 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1930 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 if (state->id < 0)
1932 return;
1933
1934 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001935
1936 /* grow indent for state->out */
1937 indent->ga_len -= 1;
1938 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001939 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001940 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001941 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001942 ga_append(indent, '\0');
1943
1944 nfa_print_state2(debugf, state->out, indent);
1945
1946 /* replace last part of indent for state->out1 */
1947 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001948 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001949 ga_append(indent, '\0');
1950
1951 nfa_print_state2(debugf, state->out1, indent);
1952
1953 /* shrink indent */
1954 indent->ga_len -= 3;
1955 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001956}
1957
1958/*
1959 * Print the NFA state machine.
1960 */
1961 static void
1962nfa_dump(prog)
1963 nfa_regprog_T *prog;
1964{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001965 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966
1967 if (debugf != NULL)
1968 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001969 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001970 fclose(debugf);
1971 }
1972}
1973#endif /* ENABLE_LOG */
1974#endif /* DEBUG */
1975
1976/*
1977 * Parse r.e. @expr and convert it into postfix form.
1978 * Return the postfix string on success, NULL otherwise.
1979 */
1980 static int *
1981re2post()
1982{
1983 if (nfa_reg(REG_NOPAREN) == FAIL)
1984 return NULL;
1985 EMIT(NFA_MOPEN);
1986 return post_start;
1987}
1988
1989/* NB. Some of the code below is inspired by Russ's. */
1990
1991/*
1992 * Represents an NFA state plus zero or one or two arrows exiting.
1993 * if c == MATCH, no arrows out; matching state.
1994 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1995 * If c < 256, labeled arrow with character c to out.
1996 */
1997
1998static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1999
2000/*
2001 * Allocate and initialize nfa_state_T.
2002 */
2003 static nfa_state_T *
2004new_state(c, out, out1)
2005 int c;
2006 nfa_state_T *out;
2007 nfa_state_T *out1;
2008{
2009 nfa_state_T *s;
2010
2011 if (istate >= nstate)
2012 return NULL;
2013
2014 s = &state_ptr[istate++];
2015
2016 s->c = c;
2017 s->out = out;
2018 s->out1 = out1;
2019
2020 s->id = istate;
2021 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002022 s->negated = FALSE;
2023
2024 return s;
2025}
2026
2027/*
2028 * A partially built NFA without the matching state filled in.
2029 * Frag_T.start points at the start state.
2030 * Frag_T.out is a list of places that need to be set to the
2031 * next state for this fragment.
2032 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002033
2034/* Since the out pointers in the list are always
2035 * uninitialized, we use the pointers themselves
2036 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002038union Ptrlist
2039{
2040 Ptrlist *next;
2041 nfa_state_T *s;
2042};
2043
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044struct Frag
2045{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002046 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002047 Ptrlist *out;
2048};
2049typedef struct Frag Frag_T;
2050
2051static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2052static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2053static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2054static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2055static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2056static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2057
2058/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002059 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002060 */
2061 static Frag_T
2062frag(start, out)
2063 nfa_state_T *start;
2064 Ptrlist *out;
2065{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002066 Frag_T n;
2067
2068 n.start = start;
2069 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 return n;
2071}
2072
2073/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002074 * Create singleton list containing just outp.
2075 */
2076 static Ptrlist *
2077list1(outp)
2078 nfa_state_T **outp;
2079{
2080 Ptrlist *l;
2081
2082 l = (Ptrlist *)outp;
2083 l->next = NULL;
2084 return l;
2085}
2086
2087/*
2088 * Patch the list of states at out to point to start.
2089 */
2090 static void
2091patch(l, s)
2092 Ptrlist *l;
2093 nfa_state_T *s;
2094{
2095 Ptrlist *next;
2096
2097 for (; l; l = next)
2098 {
2099 next = l->next;
2100 l->s = s;
2101 }
2102}
2103
2104
2105/*
2106 * Join the two lists l1 and l2, returning the combination.
2107 */
2108 static Ptrlist *
2109append(l1, l2)
2110 Ptrlist *l1;
2111 Ptrlist *l2;
2112{
2113 Ptrlist *oldl1;
2114
2115 oldl1 = l1;
2116 while (l1->next)
2117 l1 = l1->next;
2118 l1->next = l2;
2119 return oldl1;
2120}
2121
2122/*
2123 * Stack used for transforming postfix form into NFA.
2124 */
2125static Frag_T empty;
2126
2127 static void
2128st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002129 int *postfix UNUSED;
2130 int *end UNUSED;
2131 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002132{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002133#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002134 FILE *df;
2135 int *p2;
2136
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002137 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002138 if (df)
2139 {
2140 fprintf(df, "Error popping the stack!\n");
2141#ifdef DEBUG
2142 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2143#endif
2144 fprintf(df, "Postfix form is: ");
2145#ifdef DEBUG
2146 for (p2 = postfix; p2 < end; p2++)
2147 {
2148 nfa_set_code(*p2);
2149 fprintf(df, "%s, ", code);
2150 }
2151 nfa_set_code(*p);
2152 fprintf(df, "\nCurrent position is: ");
2153 for (p2 = postfix; p2 <= p; p2 ++)
2154 {
2155 nfa_set_code(*p2);
2156 fprintf(df, "%s, ", code);
2157 }
2158#else
2159 for (p2 = postfix; p2 < end; p2++)
2160 {
2161 fprintf(df, "%d, ", *p2);
2162 }
2163 fprintf(df, "\nCurrent position is: ");
2164 for (p2 = postfix; p2 <= p; p2 ++)
2165 {
2166 fprintf(df, "%d, ", *p2);
2167 }
2168#endif
2169 fprintf(df, "\n--------------------------\n");
2170 fclose(df);
2171 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002172#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173 EMSG(_("E874: (NFA) Could not pop the stack !"));
2174}
2175
2176/*
2177 * Push an item onto the stack.
2178 */
2179 static void
2180st_push(s, p, stack_end)
2181 Frag_T s;
2182 Frag_T **p;
2183 Frag_T *stack_end;
2184{
2185 Frag_T *stackp = *p;
2186
2187 if (stackp >= stack_end)
2188 return;
2189 *stackp = s;
2190 *p = *p + 1;
2191}
2192
2193/*
2194 * Pop an item from the stack.
2195 */
2196 static Frag_T
2197st_pop(p, stack)
2198 Frag_T **p;
2199 Frag_T *stack;
2200{
2201 Frag_T *stackp;
2202
2203 *p = *p - 1;
2204 stackp = *p;
2205 if (stackp < stack)
2206 return empty;
2207 return **p;
2208}
2209
2210/*
2211 * Convert a postfix form into its equivalent NFA.
2212 * Return the NFA start state on success, NULL otherwise.
2213 */
2214 static nfa_state_T *
2215post2nfa(postfix, end, nfa_calc_size)
2216 int *postfix;
2217 int *end;
2218 int nfa_calc_size;
2219{
2220 int *p;
2221 int mopen;
2222 int mclose;
2223 Frag_T *stack = NULL;
2224 Frag_T *stackp = NULL;
2225 Frag_T *stack_end = NULL;
2226 Frag_T e1;
2227 Frag_T e2;
2228 Frag_T e;
2229 nfa_state_T *s;
2230 nfa_state_T *s1;
2231 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002232 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233
2234 if (postfix == NULL)
2235 return NULL;
2236
Bram Moolenaar053bb602013-05-20 13:55:21 +02002237#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002238#define POP() st_pop(&stackp, stack); \
2239 if (stackp < stack) \
2240 { \
2241 st_error(postfix, end, p); \
2242 return NULL; \
2243 }
2244
2245 if (nfa_calc_size == FALSE)
2246 {
2247 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002248 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002249 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002250 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002251 }
2252
2253 for (p = postfix; p < end; ++p)
2254 {
2255 switch (*p)
2256 {
2257 case NFA_CONCAT:
2258 /* Catenation.
2259 * Pay attention: this operator does not exist
2260 * in the r.e. itself (it is implicit, really).
2261 * It is added when r.e. is translated to postfix
2262 * form in re2post().
2263 *
2264 * No new state added here. */
2265 if (nfa_calc_size == TRUE)
2266 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002267 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002268 break;
2269 }
2270 e2 = POP();
2271 e1 = POP();
2272 patch(e1.out, e2.start);
2273 PUSH(frag(e1.start, e2.out));
2274 break;
2275
2276 case NFA_NOT:
2277 /* Negation of a character */
2278 if (nfa_calc_size == TRUE)
2279 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002280 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002281 break;
2282 }
2283 e1 = POP();
2284 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002285#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002286 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002288#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 PUSH(e1);
2290 break;
2291
2292 case NFA_OR:
2293 /* Alternation */
2294 if (nfa_calc_size == TRUE)
2295 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002296 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002297 break;
2298 }
2299 e2 = POP();
2300 e1 = POP();
2301 s = new_state(NFA_SPLIT, e1.start, e2.start);
2302 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002303 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 PUSH(frag(s, append(e1.out, e2.out)));
2305 break;
2306
2307 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002308 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 if (nfa_calc_size == TRUE)
2310 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002311 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002312 break;
2313 }
2314 e = POP();
2315 s = new_state(NFA_SPLIT, e.start, NULL);
2316 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002317 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002318 patch(e.out, s);
2319 PUSH(frag(s, list1(&s->out1)));
2320 break;
2321
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002322 case NFA_STAR_NONGREEDY:
2323 /* Zero or more, prefer zero */
2324 if (nfa_calc_size == TRUE)
2325 {
2326 nstate++;
2327 break;
2328 }
2329 e = POP();
2330 s = new_state(NFA_SPLIT, NULL, e.start);
2331 if (s == NULL)
2332 goto theend;
2333 patch(e.out, s);
2334 PUSH(frag(s, list1(&s->out)));
2335 break;
2336
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002337 case NFA_QUEST:
2338 /* one or zero atoms=> greedy match */
2339 if (nfa_calc_size == TRUE)
2340 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002341 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002342 break;
2343 }
2344 e = POP();
2345 s = new_state(NFA_SPLIT, e.start, NULL);
2346 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002347 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002348 PUSH(frag(s, append(e.out, list1(&s->out1))));
2349 break;
2350
2351 case NFA_QUEST_NONGREEDY:
2352 /* zero or one atoms => non-greedy match */
2353 if (nfa_calc_size == TRUE)
2354 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002355 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002356 break;
2357 }
2358 e = POP();
2359 s = new_state(NFA_SPLIT, NULL, e.start);
2360 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002361 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362 PUSH(frag(s, append(e.out, list1(&s->out))));
2363 break;
2364
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365 case NFA_SKIP_CHAR:
2366 /* Symbol of 0-length, Used in a repetition
2367 * with max/min count of 0 */
2368 if (nfa_calc_size == TRUE)
2369 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002370 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 break;
2372 }
2373 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2374 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002375 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002376 PUSH(frag(s, list1(&s->out)));
2377 break;
2378
2379 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002380 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002381 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002382 * The \@! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002384 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002385
2386 if (nfa_calc_size == TRUE)
2387 {
2388 nstate += 2;
2389 break;
2390 }
2391 e = POP();
2392 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2393 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002394 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002395 patch(e.out, s1);
2396
2397 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2398 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002399 goto theend;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002400 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG)
2401 {
2402 s->negated = TRUE;
2403 s1->negated = TRUE;
2404 }
2405
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002406 PUSH(frag(s, list1(&s1->out)));
2407 break;
2408
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002409#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002410 case NFA_COMPOSING: /* char with composing char */
2411#if 0
2412 /* TODO */
2413 if (regflags & RF_ICOMBINE)
2414 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002415 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002416 }
2417#endif
2418 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002419#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421 case NFA_MOPEN + 0: /* Submatch */
2422 case NFA_MOPEN + 1:
2423 case NFA_MOPEN + 2:
2424 case NFA_MOPEN + 3:
2425 case NFA_MOPEN + 4:
2426 case NFA_MOPEN + 5:
2427 case NFA_MOPEN + 6:
2428 case NFA_MOPEN + 7:
2429 case NFA_MOPEN + 8:
2430 case NFA_MOPEN + 9:
2431 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432 if (nfa_calc_size == TRUE)
2433 {
2434 nstate += 2;
2435 break;
2436 }
2437
2438 mopen = *p;
2439 switch (*p)
2440 {
2441 case NFA_NOPEN:
2442 mclose = NFA_NCLOSE;
2443 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002444#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 case NFA_COMPOSING:
2446 mclose = NFA_END_COMPOSING;
2447 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002448#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002449 default:
2450 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2451 mclose = *p + NSUBEXP;
2452 break;
2453 }
2454
2455 /* Allow "NFA_MOPEN" as a valid postfix representation for
2456 * the empty regexp "". In this case, the NFA will be
2457 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2458 * empty groups of parenthesis, and empty mbyte chars */
2459 if (stackp == stack)
2460 {
2461 s = new_state(mopen, NULL, NULL);
2462 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002463 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002464 s1 = new_state(mclose, NULL, NULL);
2465 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002466 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002467 patch(list1(&s->out), s1);
2468 PUSH(frag(s, list1(&s1->out)));
2469 break;
2470 }
2471
2472 /* At least one node was emitted before NFA_MOPEN, so
2473 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2474 e = POP();
2475 s = new_state(mopen, e.start, NULL); /* `(' */
2476 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002477 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002478
2479 s1 = new_state(mclose, NULL, NULL); /* `)' */
2480 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002481 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002482 patch(e.out, s1);
2483
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002484#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002485 if (mopen == NFA_COMPOSING)
2486 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002487 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002488#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002489
2490 PUSH(frag(s, list1(&s1->out)));
2491 break;
2492
Bram Moolenaar5714b802013-05-28 22:03:20 +02002493 case NFA_BACKREF1:
2494 case NFA_BACKREF2:
2495 case NFA_BACKREF3:
2496 case NFA_BACKREF4:
2497 case NFA_BACKREF5:
2498 case NFA_BACKREF6:
2499 case NFA_BACKREF7:
2500 case NFA_BACKREF8:
2501 case NFA_BACKREF9:
2502 if (nfa_calc_size == TRUE)
2503 {
2504 nstate += 2;
2505 break;
2506 }
2507 s = new_state(*p, NULL, NULL);
2508 if (s == NULL)
2509 goto theend;
2510 s1 = new_state(NFA_SKIP, NULL, NULL);
2511 if (s1 == NULL)
2512 goto theend;
2513 patch(list1(&s->out), s1);
2514 PUSH(frag(s, list1(&s1->out)));
2515 break;
2516
Bram Moolenaar423532e2013-05-29 21:14:42 +02002517 case NFA_LNUM:
2518 case NFA_LNUM_GT:
2519 case NFA_LNUM_LT:
2520 case NFA_VCOL:
2521 case NFA_VCOL_GT:
2522 case NFA_VCOL_LT:
2523 case NFA_COL:
2524 case NFA_COL_GT:
2525 case NFA_COL_LT:
2526 if (nfa_calc_size == TRUE)
2527 {
2528 nstate += 1;
2529 break;
2530 }
2531 e1 = POP();
2532 s = new_state(*p, NULL, NULL);
2533 if (s == NULL)
2534 goto theend;
2535 s->val = e1.start->c;
2536 PUSH(frag(s, list1(&s->out)));
2537 break;
2538
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539 case NFA_ZSTART:
2540 case NFA_ZEND:
2541 default:
2542 /* Operands */
2543 if (nfa_calc_size == TRUE)
2544 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002545 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002546 break;
2547 }
2548 s = new_state(*p, NULL, NULL);
2549 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002550 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002551 PUSH(frag(s, list1(&s->out)));
2552 break;
2553
2554 } /* switch(*p) */
2555
2556 } /* for(p = postfix; *p; ++p) */
2557
2558 if (nfa_calc_size == TRUE)
2559 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002560 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002561 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562 }
2563
2564 e = POP();
2565 if (stackp != stack)
2566 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2567
2568 if (istate >= nstate)
2569 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2570
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002571 matchstate = &state_ptr[istate++]; /* the match state */
2572 matchstate->c = NFA_MATCH;
2573 matchstate->out = matchstate->out1 = NULL;
2574
2575 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002576 ret = e.start;
2577
2578theend:
2579 vim_free(stack);
2580 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002581
2582#undef POP1
2583#undef PUSH1
2584#undef POP2
2585#undef PUSH2
2586#undef POP
2587#undef PUSH
2588}
2589
2590/****************************************************************
2591 * NFA execution code.
2592 ****************************************************************/
2593
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002594typedef struct
2595{
2596 int in_use; /* number of subexpr with useful info */
2597
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002598 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002599 union
2600 {
2601 struct multipos
2602 {
2603 lpos_T start;
2604 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002605 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002606 struct linepos
2607 {
2608 char_u *start;
2609 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002610 } line[NSUBEXP];
2611 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002612} regsub_T;
2613
Bram Moolenaar963fee22013-05-26 21:47:28 +02002614/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002615typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616{
2617 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002618 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002619 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002620} nfa_thread_T;
2621
Bram Moolenaar963fee22013-05-26 21:47:28 +02002622/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002623typedef struct
2624{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002625 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002626 int n; /* nr of states currently in "t" */
2627 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002628 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002629} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002630
Bram Moolenaar5714b802013-05-28 22:03:20 +02002631#ifdef ENABLE_LOG
2632 static void
2633log_subexpr(sub)
2634 regsub_T *sub;
2635{
2636 int j;
2637
2638 for (j = 0; j < sub->in_use; j++)
2639 if (REG_MULTI)
2640 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2641 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002642 sub->list.multi[j].start.col,
2643 (int)sub->list.multi[j].start.lnum,
2644 sub->list.multi[j].end.col,
2645 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002646 else
2647 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2648 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002649 (char *)sub->list.line[j].start,
2650 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002651 fprintf(log_fd, "\n");
2652}
2653#endif
2654
Bram Moolenaar963fee22013-05-26 21:47:28 +02002655/* Used during execution: whether a match has been found. */
2656static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002657
Bram Moolenaar428e9872013-05-30 17:05:39 +02002658static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002659static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2660static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002661
Bram Moolenaar428e9872013-05-30 17:05:39 +02002662/*
2663 * Return TRUE if "sub1" and "sub2" have the same positions.
2664 */
2665 static int
2666sub_equal(sub1, sub2)
2667 regsub_T *sub1;
2668 regsub_T *sub2;
2669{
2670 int i;
2671 int todo;
2672 linenr_T s1, e1;
2673 linenr_T s2, e2;
2674 char_u *sp1, *ep1;
2675 char_u *sp2, *ep2;
2676
2677 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2678 if (REG_MULTI)
2679 {
2680 for (i = 0; i < todo; ++i)
2681 {
2682 if (i < sub1->in_use)
2683 {
2684 s1 = sub1->list.multi[i].start.lnum;
2685 e1 = sub1->list.multi[i].end.lnum;
2686 }
2687 else
2688 {
2689 s1 = 0;
2690 e1 = 0;
2691 }
2692 if (i < sub2->in_use)
2693 {
2694 s2 = sub2->list.multi[i].start.lnum;
2695 e2 = sub2->list.multi[i].end.lnum;
2696 }
2697 else
2698 {
2699 s2 = 0;
2700 e2 = 0;
2701 }
2702 if (s1 != s2 || e1 != e2)
2703 return FALSE;
2704 if (s1 != 0 && sub1->list.multi[i].start.col
2705 != sub2->list.multi[i].start.col)
2706 return FALSE;
2707 if (e1 != 0 && sub1->list.multi[i].end.col
2708 != sub2->list.multi[i].end.col)
2709 return FALSE;
2710 }
2711 }
2712 else
2713 {
2714 for (i = 0; i < todo; ++i)
2715 {
2716 if (i < sub1->in_use)
2717 {
2718 sp1 = sub1->list.line[i].start;
2719 ep1 = sub1->list.line[i].end;
2720 }
2721 else
2722 {
2723 sp1 = NULL;
2724 ep1 = NULL;
2725 }
2726 if (i < sub2->in_use)
2727 {
2728 sp2 = sub2->list.line[i].start;
2729 ep2 = sub2->list.line[i].end;
2730 }
2731 else
2732 {
2733 sp2 = NULL;
2734 ep2 = NULL;
2735 }
2736 if (sp1 != sp2 || ep1 != ep2)
2737 return FALSE;
2738 }
2739 }
2740
2741 return TRUE;
2742}
2743
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002745addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002746 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002747 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002748 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002749 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002750{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002751 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002752 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002753 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002754 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002755 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002756 int i;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002757#ifdef ENABLE_LOG
2758 int did_print = FALSE;
2759#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002760
2761 if (l == NULL || state == NULL)
2762 return;
2763
2764 switch (state->c)
2765 {
2766 case NFA_SPLIT:
2767 case NFA_NOT:
2768 case NFA_NOPEN:
2769 case NFA_NCLOSE:
2770 case NFA_MCLOSE:
2771 case NFA_MCLOSE + 1:
2772 case NFA_MCLOSE + 2:
2773 case NFA_MCLOSE + 3:
2774 case NFA_MCLOSE + 4:
2775 case NFA_MCLOSE + 5:
2776 case NFA_MCLOSE + 6:
2777 case NFA_MCLOSE + 7:
2778 case NFA_MCLOSE + 8:
2779 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002780 /* These nodes are not added themselves but their "out" and/or
2781 * "out1" may be added below. */
2782 break;
2783
2784 case NFA_MOPEN:
2785 case NFA_MOPEN + 1:
2786 case NFA_MOPEN + 2:
2787 case NFA_MOPEN + 3:
2788 case NFA_MOPEN + 4:
2789 case NFA_MOPEN + 5:
2790 case NFA_MOPEN + 6:
2791 case NFA_MOPEN + 7:
2792 case NFA_MOPEN + 8:
2793 case NFA_MOPEN + 9:
2794 /* These nodes do not need to be added, but we need to bail out
2795 * when it was tried to be added to this list before. */
2796 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002797 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002798 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002799 break;
2800
2801 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002802 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002803 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002804 /* This state is already in the list, don't add it again,
2805 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002806 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002807 {
2808skip_add:
2809#ifdef ENABLE_LOG
2810 nfa_set_code(state->c);
2811 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
2812 abs(state->id), l->id, state->c, code);
2813#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02002814 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002815 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02002816
2817 /* See if the same state is already in the list with the same
2818 * positions. */
2819 for (i = 0; i < l->n; ++i)
2820 {
2821 thread = &l->t[i];
2822 if (thread->state->id == state->id
2823 && sub_equal(&thread->sub, sub))
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002824 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002825 }
2826 }
2827
2828 /* when there are backreferences the number of states may be (a
2829 * lot) bigger */
2830 if (nfa_has_backref && l->n == l->len)
2831 {
2832 int newlen = l->len * 3 / 2 + 50;
2833
2834 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2835 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002837
2838 /* add the state to the list */
2839 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002840 thread = &l->t[l->n++];
2841 thread->state = state;
2842 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002843 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002844 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002845 /* Copy the match start and end positions. */
2846 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002847 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002848 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002849 sizeof(struct multipos) * sub->in_use);
2850 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002851 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002852 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002853 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002855#ifdef ENABLE_LOG
2856 {
2857 int col;
2858
2859 if (thread->sub.in_use <= 0)
2860 col = -1;
2861 else if (REG_MULTI)
2862 col = thread->sub.list.multi[0].start.col;
2863 else
2864 col = (int)(thread->sub.list.line[0].start - regline);
2865 nfa_set_code(state->c);
2866 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
2867 abs(state->id), l->id, state->c, code, col);
2868 did_print = TRUE;
2869 }
2870#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002871 }
2872
2873#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002874 if (!did_print)
2875 {
2876 int col;
2877
2878 if (sub->in_use <= 0)
2879 col = -1;
2880 else if (REG_MULTI)
2881 col = sub->list.multi[0].start.col;
2882 else
2883 col = (int)(sub->list.line[0].start - regline);
2884 nfa_set_code(state->c);
2885 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
2886 abs(state->id), l->id, state->c, code, col);
2887 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888#endif
2889 switch (state->c)
2890 {
2891 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002892 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 break;
2894
2895 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002896 addstate(l, state->out, sub, off);
2897 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002898 break;
2899
2900#if 0
2901 case NFA_END_NEG_RANGE:
2902 /* Nothing to handle here. nfa_regmatch() will take care of it */
2903 break;
2904
2905 case NFA_NOT:
2906 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2907#ifdef ENABLE_LOG
2908 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2909#endif
2910 break;
2911
2912 case NFA_COMPOSING:
2913 /* nfa_regmatch() will match all the bytes of this composing char. */
2914 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002915#endif
2916
Bram Moolenaar5714b802013-05-28 22:03:20 +02002917 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918 case NFA_NOPEN:
2919 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002920 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921 break;
2922
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002923 case NFA_MOPEN + 0:
2924 case NFA_MOPEN + 1:
2925 case NFA_MOPEN + 2:
2926 case NFA_MOPEN + 3:
2927 case NFA_MOPEN + 4:
2928 case NFA_MOPEN + 5:
2929 case NFA_MOPEN + 6:
2930 case NFA_MOPEN + 7:
2931 case NFA_MOPEN + 8:
2932 case NFA_MOPEN + 9:
2933 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 if (state->c == NFA_ZSTART)
2935 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002936 else
2937 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002939 /* Set the position (with "off") in the subexpression. Save and
2940 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02002941 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002942 if (REG_MULTI)
2943 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002944 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002945 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002946 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002947 save_in_use = -1;
2948 }
2949 else
2950 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002951 save_in_use = sub->in_use;
2952 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002953 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002954 sub->list.multi[i].start.lnum = -1;
2955 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002956 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002957 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002958 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002959 if (off == -1)
2960 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002961 sub->list.multi[subidx].start.lnum = reglnum + 1;
2962 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002963 }
2964 else
2965 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002966 sub->list.multi[subidx].start.lnum = reglnum;
2967 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002968 (colnr_T)(reginput - regline + off);
2969 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002970 }
2971 else
2972 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002973 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002974 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002975 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002976 save_in_use = -1;
2977 }
2978 else
2979 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002980 save_in_use = sub->in_use;
2981 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002982 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002983 sub->list.line[i].start = NULL;
2984 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002985 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002986 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002987 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002988 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002989 }
2990
Bram Moolenaar5714b802013-05-28 22:03:20 +02002991 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002993 if (save_in_use == -1)
2994 {
2995 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002996 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002997 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002998 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002999 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003001 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003002 break;
3003
3004 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003005 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003006 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003007 /* Do not overwrite the position set by \ze. If no \ze
3008 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003009 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003010 break;
3011 }
3012 case NFA_MCLOSE + 1:
3013 case NFA_MCLOSE + 2:
3014 case NFA_MCLOSE + 3:
3015 case NFA_MCLOSE + 4:
3016 case NFA_MCLOSE + 5:
3017 case NFA_MCLOSE + 6:
3018 case NFA_MCLOSE + 7:
3019 case NFA_MCLOSE + 8:
3020 case NFA_MCLOSE + 9:
3021 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003022 if (state->c == NFA_ZEND)
3023 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003024 else
3025 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003026
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003027 /* We don't fill in gaps here, there must have been an MOPEN that
3028 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003029 save_in_use = sub->in_use;
3030 if (sub->in_use <= subidx)
3031 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003032 if (REG_MULTI)
3033 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003034 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003035 if (off == -1)
3036 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003037 sub->list.multi[subidx].end.lnum = reglnum + 1;
3038 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003039 }
3040 else
3041 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003042 sub->list.multi[subidx].end.lnum = reglnum;
3043 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003044 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003045 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003046 }
3047 else
3048 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003049 save_ptr = sub->list.line[subidx].end;
3050 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003051 }
3052
Bram Moolenaar5714b802013-05-28 22:03:20 +02003053 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003054
3055 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003056 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003057 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003058 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003059 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003060 break;
3061 }
3062}
3063
3064/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003065 * Like addstate(), but the new state(s) are put at position "*ip".
3066 * Used for zero-width matches, next state to use is the added one.
3067 * This makes sure the order of states to be tried does not change, which
3068 * matters for alternatives.
3069 */
3070 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003071addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003072 nfa_list_T *l; /* runtime state list */
3073 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003074 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003075 int *ip;
3076{
3077 int tlen = l->n;
3078 int count;
3079 int i = *ip;
3080
3081 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003082 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003083
3084 /* when "*ip" was at the end of the list, nothing to do */
3085 if (i + 1 == tlen)
3086 return;
3087
3088 /* re-order to put the new state at the current position */
3089 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003090 if (count == 1)
3091 {
3092 /* overwrite the current state */
3093 l->t[i] = l->t[l->n - 1];
3094 }
3095 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003096 {
3097 /* make space for new states, then move them from the
3098 * end to the current position */
3099 mch_memmove(&(l->t[i + count]),
3100 &(l->t[i + 1]),
3101 sizeof(nfa_thread_T) * (l->n - i - 1));
3102 mch_memmove(&(l->t[i]),
3103 &(l->t[l->n - 1]),
3104 sizeof(nfa_thread_T) * count);
3105 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003106 --l->n;
3107 *ip = i - 1;
3108}
3109
3110/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003111 * Check character class "class" against current character c.
3112 */
3113 static int
3114check_char_class(class, c)
3115 int class;
3116 int c;
3117{
3118 switch (class)
3119 {
3120 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003121 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122 return OK;
3123 break;
3124 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003125 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003126 return OK;
3127 break;
3128 case NFA_CLASS_BLANK:
3129 if (c == ' ' || c == '\t')
3130 return OK;
3131 break;
3132 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003133 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003134 return OK;
3135 break;
3136 case NFA_CLASS_DIGIT:
3137 if (VIM_ISDIGIT(c))
3138 return OK;
3139 break;
3140 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003141 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003142 return OK;
3143 break;
3144 case NFA_CLASS_LOWER:
3145 if (MB_ISLOWER(c))
3146 return OK;
3147 break;
3148 case NFA_CLASS_PRINT:
3149 if (vim_isprintc(c))
3150 return OK;
3151 break;
3152 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003153 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003154 return OK;
3155 break;
3156 case NFA_CLASS_SPACE:
3157 if ((c >=9 && c <= 13) || (c == ' '))
3158 return OK;
3159 break;
3160 case NFA_CLASS_UPPER:
3161 if (MB_ISUPPER(c))
3162 return OK;
3163 break;
3164 case NFA_CLASS_XDIGIT:
3165 if (vim_isxdigit(c))
3166 return OK;
3167 break;
3168 case NFA_CLASS_TAB:
3169 if (c == '\t')
3170 return OK;
3171 break;
3172 case NFA_CLASS_RETURN:
3173 if (c == '\r')
3174 return OK;
3175 break;
3176 case NFA_CLASS_BACKSPACE:
3177 if (c == '\b')
3178 return OK;
3179 break;
3180 case NFA_CLASS_ESCAPE:
3181 if (c == '\033')
3182 return OK;
3183 break;
3184
3185 default:
3186 /* should not be here :P */
3187 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3188 }
3189 return FAIL;
3190}
3191
Bram Moolenaar5714b802013-05-28 22:03:20 +02003192static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3193
3194/*
3195 * Check for a match with subexpression "subidx".
3196 * return TRUE if it matches.
3197 */
3198 static int
3199match_backref(sub, subidx, bytelen)
3200 regsub_T *sub; /* pointers to subexpressions */
3201 int subidx;
3202 int *bytelen; /* out: length of match in bytes */
3203{
3204 int len;
3205
3206 if (sub->in_use <= subidx)
3207 {
3208retempty:
3209 /* backref was not set, match an empty string */
3210 *bytelen = 0;
3211 return TRUE;
3212 }
3213
3214 if (REG_MULTI)
3215 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003216 if (sub->list.multi[subidx].start.lnum < 0
3217 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003218 goto retempty;
3219 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003220 len = sub->list.multi[subidx].end.col
3221 - sub->list.multi[subidx].start.col;
3222 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003223 reginput, &len) == 0)
3224 {
3225 *bytelen = len;
3226 return TRUE;
3227 }
3228 }
3229 else
3230 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003231 if (sub->list.line[subidx].start == NULL
3232 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003233 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003234 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3235 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003236 {
3237 *bytelen = len;
3238 return TRUE;
3239 }
3240 }
3241 return FALSE;
3242}
3243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244/*
3245 * Set all NFA nodes' list ID equal to -1.
3246 */
3247 static void
3248nfa_set_neg_listids(start)
3249 nfa_state_T *start;
3250{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003251 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003252 {
3253 start->lastlist = -1;
3254 nfa_set_neg_listids(start->out);
3255 nfa_set_neg_listids(start->out1);
3256 }
3257}
3258
3259/*
3260 * Set all NFA nodes' list ID equal to 0.
3261 */
3262 static void
3263nfa_set_null_listids(start)
3264 nfa_state_T *start;
3265{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003266 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003267 {
3268 start->lastlist = 0;
3269 nfa_set_null_listids(start->out);
3270 nfa_set_null_listids(start->out1);
3271 }
3272}
3273
3274/*
3275 * Save list IDs for all NFA states in "list".
3276 */
3277 static void
3278nfa_save_listids(start, list)
3279 nfa_state_T *start;
3280 int *list;
3281{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003282 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 {
3284 list[abs(start->id)] = start->lastlist;
3285 start->lastlist = -1;
3286 nfa_save_listids(start->out, list);
3287 nfa_save_listids(start->out1, list);
3288 }
3289}
3290
3291/*
3292 * Restore list IDs from "list" to all NFA states.
3293 */
3294 static void
3295nfa_restore_listids(start, list)
3296 nfa_state_T *start;
3297 int *list;
3298{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003299 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 {
3301 start->lastlist = list[abs(start->id)];
3302 nfa_restore_listids(start->out, list);
3303 nfa_restore_listids(start->out1, list);
3304 }
3305}
3306
Bram Moolenaar423532e2013-05-29 21:14:42 +02003307 static int
3308nfa_re_num_cmp(val, op, pos)
3309 long_u val;
3310 int op;
3311 long_u pos;
3312{
3313 if (op == 1) return pos > val;
3314 if (op == 2) return pos < val;
3315 return val == pos;
3316}
3317
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003318static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3319
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320/*
3321 * Main matching routine.
3322 *
3323 * Run NFA to determine whether it matches reginput.
3324 *
3325 * Return TRUE if there is a match, FALSE otherwise.
3326 * Note: Caller must ensure that: start != NULL.
3327 */
3328 static int
3329nfa_regmatch(start, submatch, m)
3330 nfa_state_T *start;
3331 regsub_T *submatch;
3332 regsub_T *m;
3333{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 int result;
3335 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003336 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003337 int go_to_nextline = FALSE;
3338 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003339 nfa_list_T list[3];
3340 nfa_list_T *listtbl[2][2];
3341 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003342 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003343 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003344 nfa_list_T *thislist;
3345 nfa_list_T *nextlist;
3346 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003347 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003348#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003349 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350
3351 if (debug == NULL)
3352 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003353 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 return FALSE;
3355 }
3356#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003357 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003359 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003360 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003361 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3362 list[0].len = nstate + 1;
3363 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3364 list[1].len = nstate + 1;
3365 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3366 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3368 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369
3370#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003371 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372 if (log_fd != NULL)
3373 {
3374 fprintf(log_fd, "**********************************\n");
3375 nfa_set_code(start->c);
3376 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3377 abs(start->id), code);
3378 fprintf(log_fd, "**********************************\n");
3379 }
3380 else
3381 {
3382 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3383 log_fd = stderr;
3384 }
3385#endif
3386
3387 thislist = &list[0];
3388 thislist->n = 0;
3389 nextlist = &list[1];
3390 nextlist->n = 0;
3391 neglist = &list[2];
3392 neglist->n = 0;
3393#ifdef ENABLE_LOG
3394 fprintf(log_fd, "(---) STARTSTATE\n");
3395#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003396 thislist->id = listid;
3397 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003398
3399 /* There are two cases when the NFA advances: 1. input char matches the
3400 * NFA node and 2. input char does not match the NFA node, but the next
3401 * node is NFA_NOT. The following macro calls addstate() according to
3402 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3403 listtbl[0][0] = NULL;
3404 listtbl[0][1] = neglist;
3405 listtbl[1][0] = nextlist;
3406 listtbl[1][1] = NULL;
3407#define ADD_POS_NEG_STATE(node) \
3408 ll = listtbl[result ? 1 : 0][node->negated]; \
3409 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003410 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411
3412
3413 /*
3414 * Run for each character.
3415 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003416 for (;;)
3417 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003418 int curc;
3419 int clen;
3420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003421#ifdef FEAT_MBYTE
3422 if (has_mbyte)
3423 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003424 curc = (*mb_ptr2char)(reginput);
3425 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003426 }
3427 else
3428#endif
3429 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003430 curc = *reginput;
3431 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003432 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003433 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003434 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003435 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003436 go_to_nextline = FALSE;
3437 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003438
3439 /* swap lists */
3440 thislist = &list[flag];
3441 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003442 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003443 listtbl[1][0] = nextlist;
3444 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003445 thislist->id = listid;
3446 nextlist->id = listid + 1;
3447 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448
3449#ifdef ENABLE_LOG
3450 fprintf(log_fd, "------------------------------------------\n");
3451 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003452 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003453 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003454 {
3455 int i;
3456
3457 for (i = 0; i < thislist->n; i++)
3458 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3459 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460 fprintf(log_fd, "\n");
3461#endif
3462
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003463#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464 fprintf(debug, "\n-------------------\n");
3465#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003466 /*
3467 * If the state lists are empty we can stop.
3468 */
3469 if (thislist->n == 0 && neglist->n == 0)
3470 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471
3472 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003473 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 {
3475 if (neglist->n > 0)
3476 {
3477 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003478 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003479 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003480 }
3481 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003482 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003483
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003484#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 nfa_set_code(t->state->c);
3486 fprintf(debug, "%s, ", code);
3487#endif
3488#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003489 {
3490 int col;
3491
3492 if (t->sub.in_use <= 0)
3493 col = -1;
3494 else if (REG_MULTI)
3495 col = t->sub.list.multi[0].start.col;
3496 else
3497 col = (int)(t->sub.list.line[0].start - regline);
3498 nfa_set_code(t->state->c);
3499 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3500 abs(t->state->id), (int)t->state->c, code, col);
3501 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502#endif
3503
3504 /*
3505 * Handle the possible codes of the current state.
3506 * The most important is NFA_MATCH.
3507 */
3508 switch (t->state->c)
3509 {
3510 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003511 {
3512 int j;
3513
Bram Moolenaar963fee22013-05-26 21:47:28 +02003514 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003515 submatch->in_use = t->sub.in_use;
3516 if (REG_MULTI)
3517 for (j = 0; j < submatch->in_use; j++)
3518 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003519 submatch->list.multi[j].start =
3520 t->sub.list.multi[j].start;
3521 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003522 }
3523 else
3524 for (j = 0; j < submatch->in_use; j++)
3525 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003526 submatch->list.line[j].start =
3527 t->sub.list.line[j].start;
3528 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003529 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003531 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003532#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003533 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003534 * states at this position. When the list of states is going
3535 * to be empty quit without advancing, so that "reginput" is
3536 * correct. */
3537 if (nextlist->n == 0 && neglist->n == 0)
3538 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003539 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003540 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003541
3542 case NFA_END_INVISIBLE:
3543 /* This is only encountered after a NFA_START_INVISIBLE node.
3544 * They surround a zero-width group, used with "\@=" and "\&".
3545 * If we got here, it means that the current "invisible" group
3546 * finished successfully, so return control to the parent
3547 * nfa_regmatch(). Submatches are stored in *m, and used in
3548 * the parent call. */
3549 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003550 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003551 else
3552 {
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003553 /* do not set submatches for \@! */
3554 if (!t->state->negated)
3555 /* TODO: only copy positions in use. */
3556 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003557 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003558 }
3559 break;
3560
3561 case NFA_START_INVISIBLE:
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003562 {
3563 char_u *save_reginput = reginput;
3564 char_u *save_regline = regline;
3565 int save_reglnum = reglnum;
3566 int save_nfa_match = nfa_match;
3567
3568 /* Call nfa_regmatch() to check if the current concat matches
3569 * at this position. The concat ends with the node
3570 * NFA_END_INVISIBLE */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 if (listids == NULL)
3572 {
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003573 listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003574 if (listids == NULL)
3575 {
3576 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3577 return 0;
3578 }
3579 }
3580#ifdef ENABLE_LOG
3581 if (log_fd != stderr)
3582 fclose(log_fd);
3583 log_fd = NULL;
3584#endif
3585 /* Have to clear the listid field of the NFA nodes, so that
3586 * nfa_regmatch() and addstate() can run properly after
3587 * recursion. */
3588 nfa_save_listids(start, listids);
3589 nfa_set_null_listids(start);
3590 result = nfa_regmatch(t->state->out, submatch, m);
3591 nfa_set_neg_listids(start);
3592 nfa_restore_listids(start, listids);
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003593
3594 /* restore position in input text */
3595 reginput = save_reginput;
3596 regline = save_regline;
3597 reglnum = save_reglnum;
3598 nfa_match = save_nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599
3600#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003601 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 if (log_fd != NULL)
3603 {
3604 fprintf(log_fd, "****************************\n");
3605 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3606 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3607 fprintf(log_fd, "****************************\n");
3608 }
3609 else
3610 {
3611 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3612 log_fd = stderr;
3613 }
3614#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003615 /* for \@! it is a match when result is FALSE */
3616 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003618 int j;
3619
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003620 /* Copy submatch info from the recursive call */
3621 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003622 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003624 t->sub.list.multi[j].start = m->list.multi[j].start;
3625 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003626 }
3627 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003628 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003629 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003630 t->sub.list.line[j].start = m->list.line[j].start;
3631 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003633 if (m->in_use > t->sub.in_use)
3634 t->sub.in_use = m->in_use;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003635
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003636 /* t->state->out1 is the corresponding END_INVISIBLE node;
3637 * Add it to the current list (zero-width match). */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003638 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003639 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003641 break;
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003642 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643
3644 case NFA_BOL:
3645 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003646 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003647 break;
3648
3649 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003650 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003651 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003652 break;
3653
3654 case NFA_BOW:
3655 {
3656 int bow = TRUE;
3657
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003658 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003659 bow = FALSE;
3660#ifdef FEAT_MBYTE
3661 else if (has_mbyte)
3662 {
3663 int this_class;
3664
3665 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003666 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003667 if (this_class <= 1)
3668 bow = FALSE;
3669 else if (reg_prev_class() == this_class)
3670 bow = FALSE;
3671 }
3672#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003673 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003674 || (reginput > regline
3675 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003676 bow = FALSE;
3677 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003678 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679 break;
3680 }
3681
3682 case NFA_EOW:
3683 {
3684 int eow = TRUE;
3685
3686 if (reginput == regline)
3687 eow = FALSE;
3688#ifdef FEAT_MBYTE
3689 else if (has_mbyte)
3690 {
3691 int this_class, prev_class;
3692
3693 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003694 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003695 prev_class = reg_prev_class();
3696 if (this_class == prev_class
3697 || prev_class == 0 || prev_class == 1)
3698 eow = FALSE;
3699 }
3700#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003701 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003702 || (reginput[0] != NUL
3703 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003704 eow = FALSE;
3705 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003706 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003707 break;
3708 }
3709
Bram Moolenaar4b780632013-05-31 22:14:52 +02003710 case NFA_BOF:
3711 if (reglnum == 0 && reginput == regline
3712 && (!REG_MULTI || reg_firstlnum == 1))
3713 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3714 break;
3715
3716 case NFA_EOF:
3717 if (reglnum == reg_maxline && curc == NUL)
3718 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3719 break;
3720
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003721#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003723 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003724 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003725 int len = 0;
3726 nfa_state_T *end;
3727 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003728 int cchars[MAX_MCO];
3729 int ccount = 0;
3730 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003731
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003732 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003733 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003734 if (utf_iscomposing(sta->c))
3735 {
3736 /* Only match composing character(s), ignore base
3737 * character. Used for ".{composing}" and "{composing}"
3738 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003739 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003740 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003741 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003742 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003743 /* If \Z was present, then ignore composing characters.
3744 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003745 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003746 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003747 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003748 else
3749 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003750 while (sta->c != NFA_END_COMPOSING)
3751 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003753
3754 /* Check base character matches first, unless ignored. */
3755 else if (len > 0 || mc == sta->c)
3756 {
3757 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003758 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003759 len += mb_char2len(mc);
3760 sta = sta->out;
3761 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003762
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003763 /* We don't care about the order of composing characters.
3764 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003765 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003766 {
3767 mc = mb_ptr2char(reginput + len);
3768 cchars[ccount++] = mc;
3769 len += mb_char2len(mc);
3770 if (ccount == MAX_MCO)
3771 break;
3772 }
3773
3774 /* Check that each composing char in the pattern matches a
3775 * composing char in the text. We do not check if all
3776 * composing chars are matched. */
3777 result = OK;
3778 while (sta->c != NFA_END_COMPOSING)
3779 {
3780 for (j = 0; j < ccount; ++j)
3781 if (cchars[j] == sta->c)
3782 break;
3783 if (j == ccount)
3784 {
3785 result = FAIL;
3786 break;
3787 }
3788 sta = sta->out;
3789 }
3790 }
3791 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003792 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003793
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003794 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795 ADD_POS_NEG_STATE(end);
3796 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003797 }
3798#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003799
3800 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003801 if (curc == NUL && !reg_line_lbr && REG_MULTI
3802 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003803 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003804 go_to_nextline = TRUE;
3805 /* Pass -1 for the offset, which means taking the position
3806 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003807 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003808 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003809 else if (curc == '\n' && reg_line_lbr)
3810 {
3811 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003812 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003813 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003814 break;
3815
3816 case NFA_CLASS_ALNUM:
3817 case NFA_CLASS_ALPHA:
3818 case NFA_CLASS_BLANK:
3819 case NFA_CLASS_CNTRL:
3820 case NFA_CLASS_DIGIT:
3821 case NFA_CLASS_GRAPH:
3822 case NFA_CLASS_LOWER:
3823 case NFA_CLASS_PRINT:
3824 case NFA_CLASS_PUNCT:
3825 case NFA_CLASS_SPACE:
3826 case NFA_CLASS_UPPER:
3827 case NFA_CLASS_XDIGIT:
3828 case NFA_CLASS_TAB:
3829 case NFA_CLASS_RETURN:
3830 case NFA_CLASS_BACKSPACE:
3831 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003832 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003833 ADD_POS_NEG_STATE(t->state);
3834 break;
3835
3836 case NFA_END_NEG_RANGE:
3837 /* This follows a series of negated nodes, like:
3838 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003839 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003840 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003841 break;
3842
3843 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003844 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003845 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003846 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003847 break;
3848
3849 /*
3850 * Character classes like \a for alpha, \d for digit etc.
3851 */
3852 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003853 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 ADD_POS_NEG_STATE(t->state);
3855 break;
3856
3857 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003858 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003859 ADD_POS_NEG_STATE(t->state);
3860 break;
3861
3862 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003863 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003864 ADD_POS_NEG_STATE(t->state);
3865 break;
3866
3867 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003868 result = !VIM_ISDIGIT(curc)
3869 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003870 ADD_POS_NEG_STATE(t->state);
3871 break;
3872
3873 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003874 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875 ADD_POS_NEG_STATE(t->state);
3876 break;
3877
3878 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003879 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003880 ADD_POS_NEG_STATE(t->state);
3881 break;
3882
3883 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003884 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003885 ADD_POS_NEG_STATE(t->state);
3886 break;
3887
3888 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003889 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003890 ADD_POS_NEG_STATE(t->state);
3891 break;
3892
3893 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003894 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003895 ADD_POS_NEG_STATE(t->state);
3896 break;
3897
3898 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003899 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900 ADD_POS_NEG_STATE(t->state);
3901 break;
3902
3903 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003904 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003905 ADD_POS_NEG_STATE(t->state);
3906 break;
3907
3908 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003909 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910 ADD_POS_NEG_STATE(t->state);
3911 break;
3912
3913 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003914 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003915 ADD_POS_NEG_STATE(t->state);
3916 break;
3917
3918 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003919 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003920 ADD_POS_NEG_STATE(t->state);
3921 break;
3922
3923 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003924 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003925 ADD_POS_NEG_STATE(t->state);
3926 break;
3927
3928 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003929 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003930 ADD_POS_NEG_STATE(t->state);
3931 break;
3932
3933 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003934 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003935 ADD_POS_NEG_STATE(t->state);
3936 break;
3937
3938 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003939 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003940 ADD_POS_NEG_STATE(t->state);
3941 break;
3942
3943 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003944 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003945 ADD_POS_NEG_STATE(t->state);
3946 break;
3947
3948 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003949 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003950 ADD_POS_NEG_STATE(t->state);
3951 break;
3952
3953 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003954 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003955 ADD_POS_NEG_STATE(t->state);
3956 break;
3957
3958 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003959 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003960 ADD_POS_NEG_STATE(t->state);
3961 break;
3962
3963 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003964 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965 ADD_POS_NEG_STATE(t->state);
3966 break;
3967
3968 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003969 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003970 ADD_POS_NEG_STATE(t->state);
3971 break;
3972
3973 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003974 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003975 ADD_POS_NEG_STATE(t->state);
3976 break;
3977
3978 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003979 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003980 ADD_POS_NEG_STATE(t->state);
3981 break;
3982
Bram Moolenaar5714b802013-05-28 22:03:20 +02003983 case NFA_BACKREF1:
3984 case NFA_BACKREF2:
3985 case NFA_BACKREF3:
3986 case NFA_BACKREF4:
3987 case NFA_BACKREF5:
3988 case NFA_BACKREF6:
3989 case NFA_BACKREF7:
3990 case NFA_BACKREF8:
3991 case NFA_BACKREF9:
3992 /* \1 .. \9 */
3993 {
3994 int subidx = t->state->c - NFA_BACKREF1 + 1;
3995 int bytelen;
3996
3997 result = match_backref(&t->sub, subidx, &bytelen);
3998 if (result)
3999 {
4000 if (bytelen == 0)
4001 {
4002 /* empty match always works, add NFA_SKIP with zero to
4003 * be used next */
4004 addstate_here(thislist, t->state->out, &t->sub,
4005 &listidx);
4006 thislist->t[listidx + 1].count = 0;
4007 }
4008 else if (bytelen <= clen)
4009 {
4010 /* match current character, jump ahead to out of
4011 * NFA_SKIP */
4012 addstate(nextlist, t->state->out->out, &t->sub, clen);
4013#ifdef ENABLE_LOG
4014 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4015#endif
4016 }
4017 else
4018 {
4019 /* skip ofer the matched characters, set character
4020 * count in NFA_SKIP */
4021 addstate(nextlist, t->state->out, &t->sub, bytelen);
4022 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4023#ifdef ENABLE_LOG
4024 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4025#endif
4026 }
4027
4028 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004029 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004030 }
4031 case NFA_SKIP:
4032 /* charater of previous matching \1 .. \9 */
4033 if (t->count - clen <= 0)
4034 {
4035 /* end of match, go to what follows */
4036 addstate(nextlist, t->state->out, &t->sub, clen);
4037#ifdef ENABLE_LOG
4038 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4039#endif
4040 }
4041 else
4042 {
4043 /* add state again with decremented count */
4044 addstate(nextlist, t->state, &t->sub, 0);
4045 nextlist->t[nextlist->n - 1].count = t->count - clen;
4046#ifdef ENABLE_LOG
4047 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4048#endif
4049 }
4050 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004051
4052 case NFA_SKIP_CHAR:
4053 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004054 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004055 /* TODO: should not happen? */
4056 break;
4057
Bram Moolenaar423532e2013-05-29 21:14:42 +02004058 case NFA_LNUM:
4059 case NFA_LNUM_GT:
4060 case NFA_LNUM_LT:
4061 result = (REG_MULTI &&
4062 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4063 (long_u)(reglnum + reg_firstlnum)));
4064 if (result)
4065 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4066 break;
4067
4068 case NFA_COL:
4069 case NFA_COL_GT:
4070 case NFA_COL_LT:
4071 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4072 (long_u)(reginput - regline) + 1);
4073 if (result)
4074 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4075 break;
4076
4077 case NFA_VCOL:
4078 case NFA_VCOL_GT:
4079 case NFA_VCOL_LT:
4080 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4081 (long_u)win_linetabsize(
4082 reg_win == NULL ? curwin : reg_win,
4083 regline, (colnr_T)(reginput - regline)) + 1);
4084 if (result)
4085 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4086 break;
4087
4088 case NFA_CURSOR:
4089 result = (reg_win != NULL
4090 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4091 && ((colnr_T)(reginput - regline)
4092 == reg_win->w_cursor.col));
4093 if (result)
4094 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4095 break;
4096
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004097 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004098 {
4099 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004100
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004101 /* TODO: put this in #ifdef later */
4102 if (c < -256)
4103 EMSGN("INTERNAL: Negative state char: %ld", c);
4104 if (is_Magic(c))
4105 c = un_Magic(c);
4106 result = (c == curc);
4107
4108 if (!result && ireg_ic)
4109 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004110#ifdef FEAT_MBYTE
4111 /* If there is a composing character which is not being
4112 * ignored there can be no match. Match with composing
4113 * character uses NFA_COMPOSING above. */
4114 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004115 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004116 result = FALSE;
4117#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004118 ADD_POS_NEG_STATE(t->state);
4119 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004120 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004121 }
4122
4123 } /* for (thislist = thislist; thislist->state; thislist++) */
4124
Bram Moolenaare23febd2013-05-26 18:40:14 +02004125 /* Look for the start of a match in the current position by adding the
4126 * start state to the list of states.
4127 * The first found match is the leftmost one, thus the order of states
4128 * matters!
4129 * Do not add the start state in recursive calls of nfa_regmatch(),
4130 * because recursive calls should only start in the first position.
4131 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004132 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004133 && reglnum == 0 && clen != 0
4134 && (ireg_maxcol == 0
4135 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004136 {
4137#ifdef ENABLE_LOG
4138 fprintf(log_fd, "(---) STARTSTATE\n");
4139#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004140 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141 }
4142
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004143#ifdef ENABLE_LOG
4144 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004145 {
4146 int i;
4147
4148 for (i = 0; i < thislist->n; i++)
4149 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4150 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004151 fprintf(log_fd, "\n");
4152#endif
4153
4154nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004155 /* Advance to the next character, or advance to the next line, or
4156 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004157 if (clen != 0)
4158 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004159 else if (go_to_nextline)
4160 reg_nextline();
4161 else
4162 break;
4163 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004164
4165#ifdef ENABLE_LOG
4166 if (log_fd != stderr)
4167 fclose(log_fd);
4168 log_fd = NULL;
4169#endif
4170
4171theend:
4172 /* Free memory */
4173 vim_free(list[0].t);
4174 vim_free(list[1].t);
4175 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004176 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004177#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004178#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004179 fclose(debug);
4180#endif
4181
Bram Moolenaar963fee22013-05-26 21:47:28 +02004182 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004183}
4184
4185/*
4186 * Try match of "prog" with at regline["col"].
4187 * Returns 0 for failure, number of lines contained in the match otherwise.
4188 */
4189 static long
4190nfa_regtry(start, col)
4191 nfa_state_T *start;
4192 colnr_T col;
4193{
4194 int i;
4195 regsub_T sub, m;
4196#ifdef ENABLE_LOG
4197 FILE *f;
4198#endif
4199
4200 reginput = regline + col;
4201 need_clear_subexpr = TRUE;
4202
4203#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004204 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004205 if (f != NULL)
4206 {
4207 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4208 fprintf(f, " =======================================================\n");
4209#ifdef DEBUG
4210 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4211#endif
4212 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004213 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004214 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004215 fprintf(f, "\n\n");
4216 fclose(f);
4217 }
4218 else
4219 EMSG(_("Could not open temporary log file for writing "));
4220#endif
4221
4222 if (REG_MULTI)
4223 {
4224 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004225 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4226 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004227 }
4228 else
4229 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004230 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4231 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004232 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004233 sub.in_use = 0;
4234 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235
4236 if (nfa_regmatch(start, &sub, &m) == FALSE)
4237 return 0;
4238
4239 cleanup_subexpr();
4240 if (REG_MULTI)
4241 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004242 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004243 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004244 reg_startpos[i] = sub.list.multi[i].start;
4245 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004246 }
4247
4248 if (reg_startpos[0].lnum < 0)
4249 {
4250 reg_startpos[0].lnum = 0;
4251 reg_startpos[0].col = col;
4252 }
4253 if (reg_endpos[0].lnum < 0)
4254 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004255 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 reg_endpos[0].lnum = reglnum;
4257 reg_endpos[0].col = (int)(reginput - regline);
4258 }
4259 else
4260 /* Use line number of "\ze". */
4261 reglnum = reg_endpos[0].lnum;
4262 }
4263 else
4264 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004265 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004266 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004267 reg_startp[i] = sub.list.line[i].start;
4268 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269 }
4270
4271 if (reg_startp[0] == NULL)
4272 reg_startp[0] = regline + col;
4273 if (reg_endp[0] == NULL)
4274 reg_endp[0] = reginput;
4275 }
4276
4277 return 1 + reglnum;
4278}
4279
4280/*
4281 * Match a regexp against a string ("line" points to the string) or multiple
4282 * lines ("line" is NULL, use reg_getline()).
4283 *
4284 * Returns 0 for failure, number of lines contained in the match otherwise.
4285 */
4286 static long
4287nfa_regexec_both(line, col)
4288 char_u *line;
4289 colnr_T col; /* column to start looking for match */
4290{
4291 nfa_regprog_T *prog;
4292 long retval = 0L;
4293 int i;
4294
4295 if (REG_MULTI)
4296 {
4297 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4298 line = reg_getline((linenr_T)0); /* relative to the cursor */
4299 reg_startpos = reg_mmatch->startpos;
4300 reg_endpos = reg_mmatch->endpos;
4301 }
4302 else
4303 {
4304 prog = (nfa_regprog_T *)reg_match->regprog;
4305 reg_startp = reg_match->startp;
4306 reg_endp = reg_match->endp;
4307 }
4308
4309 /* Be paranoid... */
4310 if (prog == NULL || line == NULL)
4311 {
4312 EMSG(_(e_null));
4313 goto theend;
4314 }
4315
4316 /* If the start column is past the maximum column: no need to try. */
4317 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4318 goto theend;
4319
4320 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4321 if (prog->regflags & RF_ICASE)
4322 ireg_ic = TRUE;
4323 else if (prog->regflags & RF_NOICASE)
4324 ireg_ic = FALSE;
4325
4326#ifdef FEAT_MBYTE
4327 /* If pattern contains "\Z" overrule value of ireg_icombine */
4328 if (prog->regflags & RF_ICOMBINE)
4329 ireg_icombine = TRUE;
4330#endif
4331
4332 regline = line;
4333 reglnum = 0; /* relative to line */
4334
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004335 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004336 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004337 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004338
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004339 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004340 for (i = 0; i < nstate; ++i)
4341 {
4342 prog->state[i].id = i;
4343 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004344 }
4345
4346 retval = nfa_regtry(prog->start, col);
4347
4348theend:
4349 return retval;
4350}
4351
4352/*
4353 * Compile a regular expression into internal code for the NFA matcher.
4354 * Returns the program in allocated space. Returns NULL for an error.
4355 */
4356 static regprog_T *
4357nfa_regcomp(expr, re_flags)
4358 char_u *expr;
4359 int re_flags;
4360{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004361 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004362 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004363 int *postfix;
4364
4365 if (expr == NULL)
4366 return NULL;
4367
4368#ifdef DEBUG
4369 nfa_regengine.expr = expr;
4370#endif
4371
4372 init_class_tab();
4373
4374 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4375 return NULL;
4376
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004377 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004378 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004379 postfix = re2post();
4380 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004381 {
4382 /* TODO: only give this error for debugging? */
4383 if (post_ptr >= post_end)
4384 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004385 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004386 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004387
4388 /*
4389 * In order to build the NFA, we parse the input regexp twice:
4390 * 1. first pass to count size (so we can allocate space)
4391 * 2. second to emit code
4392 */
4393#ifdef ENABLE_LOG
4394 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004395 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004396
4397 if (f != NULL)
4398 {
4399 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4400 fclose(f);
4401 }
4402 }
4403#endif
4404
4405 /*
4406 * PASS 1
4407 * Count number of NFA states in "nstate". Do not build the NFA.
4408 */
4409 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004410
4411 /* Space for compiled regexp */
4412 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4413 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4414 if (prog == NULL)
4415 goto fail;
4416 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004417 state_ptr = prog->state;
4418
4419 /*
4420 * PASS 2
4421 * Build the NFA
4422 */
4423 prog->start = post2nfa(postfix, post_ptr, FALSE);
4424 if (prog->start == NULL)
4425 goto fail;
4426
4427 prog->regflags = regflags;
4428 prog->engine = &nfa_regengine;
4429 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004430 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004431 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004432 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004433#ifdef ENABLE_LOG
4434 nfa_postfix_dump(expr, OK);
4435 nfa_dump(prog);
4436#endif
4437
4438out:
4439 vim_free(post_start);
4440 post_start = post_ptr = post_end = NULL;
4441 state_ptr = NULL;
4442 return (regprog_T *)prog;
4443
4444fail:
4445 vim_free(prog);
4446 prog = NULL;
4447#ifdef ENABLE_LOG
4448 nfa_postfix_dump(expr, FAIL);
4449#endif
4450#ifdef DEBUG
4451 nfa_regengine.expr = NULL;
4452#endif
4453 goto out;
4454}
4455
4456
4457/*
4458 * Match a regexp against a string.
4459 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4460 * Uses curbuf for line count and 'iskeyword'.
4461 *
4462 * Return TRUE if there is a match, FALSE if not.
4463 */
4464 static int
4465nfa_regexec(rmp, line, col)
4466 regmatch_T *rmp;
4467 char_u *line; /* string to match against */
4468 colnr_T col; /* column to start looking for match */
4469{
4470 reg_match = rmp;
4471 reg_mmatch = NULL;
4472 reg_maxline = 0;
4473 reg_line_lbr = FALSE;
4474 reg_buf = curbuf;
4475 reg_win = NULL;
4476 ireg_ic = rmp->rm_ic;
4477#ifdef FEAT_MBYTE
4478 ireg_icombine = FALSE;
4479#endif
4480 ireg_maxcol = 0;
4481 return (nfa_regexec_both(line, col) != 0);
4482}
4483
4484#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4485 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4486
4487static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4488
4489/*
4490 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4491 */
4492 static int
4493nfa_regexec_nl(rmp, line, col)
4494 regmatch_T *rmp;
4495 char_u *line; /* string to match against */
4496 colnr_T col; /* column to start looking for match */
4497{
4498 reg_match = rmp;
4499 reg_mmatch = NULL;
4500 reg_maxline = 0;
4501 reg_line_lbr = TRUE;
4502 reg_buf = curbuf;
4503 reg_win = NULL;
4504 ireg_ic = rmp->rm_ic;
4505#ifdef FEAT_MBYTE
4506 ireg_icombine = FALSE;
4507#endif
4508 ireg_maxcol = 0;
4509 return (nfa_regexec_both(line, col) != 0);
4510}
4511#endif
4512
4513
4514/*
4515 * Match a regexp against multiple lines.
4516 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4517 * Uses curbuf for line count and 'iskeyword'.
4518 *
4519 * Return zero if there is no match. Return number of lines contained in the
4520 * match otherwise.
4521 *
4522 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4523 *
4524 * ! Also NOTE : match may actually be in another line. e.g.:
4525 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4526 *
4527 * +-------------------------+
4528 * |a |
4529 * |b |
4530 * |c |
4531 * | |
4532 * +-------------------------+
4533 *
4534 * then nfa_regexec_multi() returns 3. while the original
4535 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4536 *
4537 * FIXME if this behavior is not compatible.
4538 */
4539 static long
4540nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4541 regmmatch_T *rmp;
4542 win_T *win; /* window in which to search or NULL */
4543 buf_T *buf; /* buffer in which to search */
4544 linenr_T lnum; /* nr of line to start looking for match */
4545 colnr_T col; /* column to start looking for match */
4546 proftime_T *tm UNUSED; /* timeout limit or NULL */
4547{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 reg_match = NULL;
4549 reg_mmatch = rmp;
4550 reg_buf = buf;
4551 reg_win = win;
4552 reg_firstlnum = lnum;
4553 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4554 reg_line_lbr = FALSE;
4555 ireg_ic = rmp->rmm_ic;
4556#ifdef FEAT_MBYTE
4557 ireg_icombine = FALSE;
4558#endif
4559 ireg_maxcol = rmp->rmm_maxcol;
4560
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004561 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004562}
4563
4564#ifdef DEBUG
4565# undef ENABLE_LOG
4566#endif