blob: b6fba2715ba7b93be07a2110032e107463a2b0da [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
32/* Upper limit allowed for {m,n} repetitions handled by NFA */
33#define NFA_BRACES_MAXLIMIT 10
34/* For allocating space for the postfix representation */
35#define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020036
37enum
38{
39 NFA_SPLIT = -1024,
40 NFA_MATCH,
41 NFA_SKIP_CHAR, /* matches a 0-length char */
42 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
43
44 NFA_CONCAT,
45 NFA_OR,
46 NFA_STAR,
47 NFA_PLUS,
48 NFA_QUEST,
49 NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */
50 NFA_NOT, /* used for [^ab] negated char ranges */
51
52 NFA_BOL, /* ^ Begin line */
53 NFA_EOL, /* $ End line */
54 NFA_BOW, /* \< Begin word */
55 NFA_EOW, /* \> End word */
56 NFA_BOF, /* \%^ Begin file */
57 NFA_EOF, /* \%$ End file */
58 NFA_NEWL,
59 NFA_ZSTART, /* Used for \zs */
60 NFA_ZEND, /* Used for \ze */
61 NFA_NOPEN, /* Start of subexpression marked with \%( */
62 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
63 NFA_START_INVISIBLE,
64 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020065 NFA_COMPOSING, /* Next nodes in NFA are part of the
66 composing multibyte char */
67 NFA_END_COMPOSING, /* End of a composing char in the NFA */
68
69 /* The following are used only in the postfix form, not in the NFA */
70 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
71 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
72 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
73 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
74 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
75
Bram Moolenaar5714b802013-05-28 22:03:20 +020076 NFA_BACKREF1, /* \1 */
77 NFA_BACKREF2, /* \2 */
78 NFA_BACKREF3, /* \3 */
79 NFA_BACKREF4, /* \4 */
80 NFA_BACKREF5, /* \5 */
81 NFA_BACKREF6, /* \6 */
82 NFA_BACKREF7, /* \7 */
83 NFA_BACKREF8, /* \8 */
84 NFA_BACKREF9, /* \9 */
85 NFA_SKIP, /* Skip characters */
86
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020087 NFA_MOPEN,
88 NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
89
90 /* NFA_FIRST_NL */
91 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
92 NFA_ANYOF, /* Match any character in this string. */
93 NFA_ANYBUT, /* Match any character not in this string. */
94 NFA_IDENT, /* Match identifier char */
95 NFA_SIDENT, /* Match identifier char but no digit */
96 NFA_KWORD, /* Match keyword char */
97 NFA_SKWORD, /* Match word char but no digit */
98 NFA_FNAME, /* Match file name char */
99 NFA_SFNAME, /* Match file name char but no digit */
100 NFA_PRINT, /* Match printable char */
101 NFA_SPRINT, /* Match printable char but no digit */
102 NFA_WHITE, /* Match whitespace char */
103 NFA_NWHITE, /* Match non-whitespace char */
104 NFA_DIGIT, /* Match digit char */
105 NFA_NDIGIT, /* Match non-digit char */
106 NFA_HEX, /* Match hex char */
107 NFA_NHEX, /* Match non-hex char */
108 NFA_OCTAL, /* Match octal char */
109 NFA_NOCTAL, /* Match non-octal char */
110 NFA_WORD, /* Match word char */
111 NFA_NWORD, /* Match non-word char */
112 NFA_HEAD, /* Match head char */
113 NFA_NHEAD, /* Match non-head char */
114 NFA_ALPHA, /* Match alpha char */
115 NFA_NALPHA, /* Match non-alpha char */
116 NFA_LOWER, /* Match lowercase char */
117 NFA_NLOWER, /* Match non-lowercase char */
118 NFA_UPPER, /* Match uppercase char */
119 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200120
121 NFA_CURSOR, /* Match cursor pos */
122 NFA_LNUM, /* Match line number */
123 NFA_LNUM_GT, /* Match > line number */
124 NFA_LNUM_LT, /* Match < line number */
125 NFA_COL, /* Match cursor column */
126 NFA_COL_GT, /* Match > cursor column */
127 NFA_COL_LT, /* Match < cursor column */
128 NFA_VCOL, /* Match cursor virtual column */
129 NFA_VCOL_GT, /* Match > cursor virtual column */
130 NFA_VCOL_LT, /* Match < cursor virtual column */
131
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200132 NFA_FIRST_NL = NFA_ANY + ADD_NL,
133 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
134
135 /* Character classes [:alnum:] etc */
136 NFA_CLASS_ALNUM,
137 NFA_CLASS_ALPHA,
138 NFA_CLASS_BLANK,
139 NFA_CLASS_CNTRL,
140 NFA_CLASS_DIGIT,
141 NFA_CLASS_GRAPH,
142 NFA_CLASS_LOWER,
143 NFA_CLASS_PRINT,
144 NFA_CLASS_PUNCT,
145 NFA_CLASS_SPACE,
146 NFA_CLASS_UPPER,
147 NFA_CLASS_XDIGIT,
148 NFA_CLASS_TAB,
149 NFA_CLASS_RETURN,
150 NFA_CLASS_BACKSPACE,
151 NFA_CLASS_ESCAPE
152};
153
154/* Keep in sync with classchars. */
155static int nfa_classcodes[] = {
156 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
157 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
158 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
159 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
160 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
161 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
162 NFA_UPPER, NFA_NUPPER
163};
164
165static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
166
167/*
168 * NFA errors can be of 3 types:
169 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
170 * silently and revert the to backtracking engine.
171 * syntax_error = FALSE;
172 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
173 * The NFA engine displays an error message, and nothing else happens.
174 * syntax_error = TRUE
175 * *** Unsupported features, when the input regexp uses an operator that is not
176 * implemented in the NFA. The NFA engine fails silently, and reverts to the
177 * old backtracking engine.
178 * syntax_error = FALSE
179 * "The NFA fails" means that "compiling the regexp with the NFA fails":
180 * nfa_regcomp() returns FAIL.
181 */
182static int syntax_error = FALSE;
183
184/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200185static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200186
Bram Moolenaar428e9872013-05-30 17:05:39 +0200187/* NFA regexp \1 .. \9 encountered. */
188static int nfa_has_backref;
189
Bram Moolenaar963fee22013-05-26 21:47:28 +0200190/* Number of sub expressions actually being used during execution. 1 if only
191 * the whole match (subexpr 0) is used. */
192static int nfa_nsubexpr;
193
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200194static int *post_start; /* holds the postfix form of r.e. */
195static int *post_end;
196static int *post_ptr;
197
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200198static int nstate; /* Number of states in the NFA. Also used when
199 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200200static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200201
202
203static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
204static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
205static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200206static int nfa_regatom __ARGS((void));
207static int nfa_regpiece __ARGS((void));
208static int nfa_regconcat __ARGS((void));
209static int nfa_regbranch __ARGS((void));
210static int nfa_reg __ARGS((int paren));
211#ifdef DEBUG
212static void nfa_set_code __ARGS((int c));
213static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200214static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
215static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200216static void nfa_dump __ARGS((nfa_regprog_T *prog));
217#endif
218static int *re2post __ARGS((void));
219static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
220static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
221static int check_char_class __ARGS((int class, int c));
222static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200223static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
224static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200225static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
226static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200227static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200228static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
229static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
230static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
231static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
232static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
233
234/* helper functions used when doing re2post() ... regatom() parsing */
235#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200236 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237 return FAIL; \
238 *post_ptr++ = c; \
239 } while (0)
240
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200241/*
242 * Initialize internal variables before NFA compilation.
243 * Return OK on success, FAIL otherwise.
244 */
245 static int
246nfa_regcomp_start(expr, re_flags)
247 char_u *expr;
248 int re_flags; /* see vim_regcomp() */
249{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200250 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200251 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
253 nstate = 0;
254 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200255 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200256 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200258 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200259 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200260 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200261
262 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200263 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265 post_start = (int *)lalloc(postfix_size, TRUE);
266 if (post_start == NULL)
267 return FAIL;
268 vim_memset(post_start, 0, postfix_size);
269 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200270 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200272 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273
274 regcomp_start(expr, re_flags);
275
276 return OK;
277}
278
279/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200280 * Allocate more space for post_start. Called when
281 * running above the estimated number of states.
282 */
283 static int
284realloc_post_list()
285{
286 int nstate_max = post_end - post_start;
287 int new_max = nstate_max + 1000;
288 int *new_start;
289 int *old_start;
290
291 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
292 if (new_start == NULL)
293 return FAIL;
294 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
295 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
296 old_start = post_start;
297 post_start = new_start;
298 post_ptr = new_start + (post_ptr - old_start);
299 post_end = post_start + new_max;
300 vim_free(old_start);
301 return OK;
302}
303
304/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305 * Search between "start" and "end" and try to recognize a
306 * character class in expanded form. For example [0-9].
307 * On success, return the id the character class to be emitted.
308 * On failure, return 0 (=FAIL)
309 * Start points to the first char of the range, while end should point
310 * to the closing brace.
311 */
312 static int
313nfa_recognize_char_class(start, end, extra_newl)
314 char_u *start;
315 char_u *end;
316 int extra_newl;
317{
318 int i;
319 /* Each of these variables takes up a char in "config[]",
320 * in the order they are here. */
321 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
322 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
323 char_u *p;
324#define NCONFIGS 16
325 int classid[NCONFIGS] = {
326 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
327 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
328 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
329 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
330 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200331 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200332 char_u config[NCONFIGS][9] = {
333 "000000100", /* digit */
334 "100000100", /* non digit */
335 "011000100", /* hex-digit */
336 "111000100", /* non hex-digit */
337 "000001000", /* octal-digit */
338 "100001000", /* [^0-7] */
339 "000110110", /* [0-9A-Za-z_] */
340 "100110110", /* [^0-9A-Za-z_] */
341 "000110010", /* head of word */
342 "100110010", /* not head of word */
343 "000110000", /* alphabetic char a-z */
344 "100110000", /* non alphabetic char */
345 "000100000", /* lowercase letter */
346 "100100000", /* non lowercase */
347 "000010000", /* uppercase */
348 "100010000" /* non uppercase */
349 };
350
351 if (extra_newl == TRUE)
352 newl = TRUE;
353
354 if (*end != ']')
355 return FAIL;
356 p = start;
357 if (*p == '^')
358 {
359 not = TRUE;
360 p ++;
361 }
362
363 while (p < end)
364 {
365 if (p + 2 < end && *(p + 1) == '-')
366 {
367 switch (*p)
368 {
369 case '0':
370 if (*(p + 2) == '9')
371 {
372 o9 = TRUE;
373 break;
374 }
375 else
376 if (*(p + 2) == '7')
377 {
378 o7 = TRUE;
379 break;
380 }
381 case 'a':
382 if (*(p + 2) == 'z')
383 {
384 az = TRUE;
385 break;
386 }
387 else
388 if (*(p + 2) == 'f')
389 {
390 af = TRUE;
391 break;
392 }
393 case 'A':
394 if (*(p + 2) == 'Z')
395 {
396 AZ = TRUE;
397 break;
398 }
399 else
400 if (*(p + 2) == 'F')
401 {
402 AF = TRUE;
403 break;
404 }
405 /* FALLTHROUGH */
406 default:
407 return FAIL;
408 }
409 p += 3;
410 }
411 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
412 {
413 newl = TRUE;
414 p += 2;
415 }
416 else if (*p == '_')
417 {
418 underscore = TRUE;
419 p ++;
420 }
421 else if (*p == '\n')
422 {
423 newl = TRUE;
424 p ++;
425 }
426 else
427 return FAIL;
428 } /* while (p < end) */
429
430 if (p != end)
431 return FAIL;
432
433 /* build the config that represents the ranges we gathered */
434 STRCPY(myconfig, "000000000");
435 if (not == TRUE)
436 myconfig[0] = '1';
437 if (af == TRUE)
438 myconfig[1] = '1';
439 if (AF == TRUE)
440 myconfig[2] = '1';
441 if (az == TRUE)
442 myconfig[3] = '1';
443 if (AZ == TRUE)
444 myconfig[4] = '1';
445 if (o7 == TRUE)
446 myconfig[5] = '1';
447 if (o9 == TRUE)
448 myconfig[6] = '1';
449 if (underscore == TRUE)
450 myconfig[7] = '1';
451 if (newl == TRUE)
452 {
453 myconfig[8] = '1';
454 extra_newl = ADD_NL;
455 }
456 /* try to recognize character classes */
457 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200458 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200459 return classid[i] + extra_newl;
460
461 /* fallthrough => no success so far */
462 return FAIL;
463
464#undef NCONFIGS
465}
466
467/*
468 * Produce the bytes for equivalence class "c".
469 * Currently only handles latin1, latin9 and utf-8.
470 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
471 * equivalent to 'a OR b OR c'
472 *
473 * NOTE! When changing this function, also update reg_equi_class()
474 */
475 static int
476nfa_emit_equi_class(c, neg)
477 int c;
478 int neg;
479{
480 int first = TRUE;
481 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
482#define EMIT2(c) \
483 EMIT(c); \
484 if (neg == TRUE) { \
485 EMIT(NFA_NOT); \
486 } \
487 if (first == FALSE) \
488 EMIT(glue); \
489 else \
490 first = FALSE; \
491
492#ifdef FEAT_MBYTE
493 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
494 || STRCMP(p_enc, "iso-8859-15") == 0)
495#endif
496 {
497 switch (c)
498 {
499 case 'A': case '\300': case '\301': case '\302':
500 case '\303': case '\304': case '\305':
501 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
502 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
503 EMIT2('\305');
504 return OK;
505
506 case 'C': case '\307':
507 EMIT2('C'); EMIT2('\307');
508 return OK;
509
510 case 'E': case '\310': case '\311': case '\312': case '\313':
511 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
512 EMIT2('\312'); EMIT2('\313');
513 return OK;
514
515 case 'I': case '\314': case '\315': case '\316': case '\317':
516 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
517 EMIT2('\316'); EMIT2('\317');
518 return OK;
519
520 case 'N': case '\321':
521 EMIT2('N'); EMIT2('\321');
522 return OK;
523
524 case 'O': case '\322': case '\323': case '\324': case '\325':
525 case '\326':
526 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
527 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
528 return OK;
529
530 case 'U': case '\331': case '\332': case '\333': case '\334':
531 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
532 EMIT2('\333'); EMIT2('\334');
533 return OK;
534
535 case 'Y': case '\335':
536 EMIT2('Y'); EMIT2('\335');
537 return OK;
538
539 case 'a': case '\340': case '\341': case '\342':
540 case '\343': case '\344': case '\345':
541 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
542 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
543 EMIT2('\345');
544 return OK;
545
546 case 'c': case '\347':
547 EMIT2('c'); EMIT2('\347');
548 return OK;
549
550 case 'e': case '\350': case '\351': case '\352': case '\353':
551 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
552 EMIT2('\352'); EMIT2('\353');
553 return OK;
554
555 case 'i': case '\354': case '\355': case '\356': case '\357':
556 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
557 EMIT2('\356'); EMIT2('\357');
558 return OK;
559
560 case 'n': case '\361':
561 EMIT2('n'); EMIT2('\361');
562 return OK;
563
564 case 'o': case '\362': case '\363': case '\364': case '\365':
565 case '\366':
566 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
567 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
568 return OK;
569
570 case 'u': case '\371': case '\372': case '\373': case '\374':
571 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
572 EMIT2('\373'); EMIT2('\374');
573 return OK;
574
575 case 'y': case '\375': case '\377':
576 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
577 return OK;
578
579 default:
580 return FAIL;
581 }
582 }
583
584 EMIT(c);
585 return OK;
586#undef EMIT2
587}
588
589/*
590 * Code to parse regular expression.
591 *
592 * We try to reuse parsing functions in regexp.c to
593 * minimize surprise and keep the syntax consistent.
594 */
595
596/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 * Parse the lowest level.
598 *
599 * An atom can be one of a long list of items. Many atoms match one character
600 * in the text. It is often an ordinary character or a character class.
601 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
602 * is only for syntax highlighting.
603 *
604 * atom ::= ordinary-atom
605 * or \( pattern \)
606 * or \%( pattern \)
607 * or \z( pattern \)
608 */
609 static int
610nfa_regatom()
611{
612 int c;
613 int charclass;
614 int equiclass;
615 int collclass;
616 int got_coll_char;
617 char_u *p;
618 char_u *endp;
619#ifdef FEAT_MBYTE
620 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200621#endif
622 int extra = 0;
623 int first;
624 int emit_range;
625 int negated;
626 int result;
627 int startc = -1;
628 int endc = -1;
629 int oldstartc = -1;
630 int cpo_lit; /* 'cpoptions' contains 'l' flag */
631 int cpo_bsl; /* 'cpoptions' contains '\' flag */
632 int glue; /* ID that will "glue" nodes together */
633
634 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
635 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
636
637 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 switch (c)
639 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200640 case NUL:
641 syntax_error = TRUE;
642 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
643
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 case Magic('^'):
645 EMIT(NFA_BOL);
646 break;
647
648 case Magic('$'):
649 EMIT(NFA_EOL);
650#if defined(FEAT_SYN_HL) || defined(PROTO)
651 had_eol = TRUE;
652#endif
653 break;
654
655 case Magic('<'):
656 EMIT(NFA_BOW);
657 break;
658
659 case Magic('>'):
660 EMIT(NFA_EOW);
661 break;
662
663 case Magic('_'):
664 c = no_Magic(getchr());
665 if (c == '^') /* "\_^" is start-of-line */
666 {
667 EMIT(NFA_BOL);
668 break;
669 }
670 if (c == '$') /* "\_$" is end-of-line */
671 {
672 EMIT(NFA_EOL);
673#if defined(FEAT_SYN_HL) || defined(PROTO)
674 had_eol = TRUE;
675#endif
676 break;
677 }
678
679 extra = ADD_NL;
680
681 /* "\_[" is collection plus newline */
682 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200683 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200684
685 /* "\_x" is character class plus newline */
686 /*FALLTHROUGH*/
687
688 /*
689 * Character classes.
690 */
691 case Magic('.'):
692 case Magic('i'):
693 case Magic('I'):
694 case Magic('k'):
695 case Magic('K'):
696 case Magic('f'):
697 case Magic('F'):
698 case Magic('p'):
699 case Magic('P'):
700 case Magic('s'):
701 case Magic('S'):
702 case Magic('d'):
703 case Magic('D'):
704 case Magic('x'):
705 case Magic('X'):
706 case Magic('o'):
707 case Magic('O'):
708 case Magic('w'):
709 case Magic('W'):
710 case Magic('h'):
711 case Magic('H'):
712 case Magic('a'):
713 case Magic('A'):
714 case Magic('l'):
715 case Magic('L'):
716 case Magic('u'):
717 case Magic('U'):
718 p = vim_strchr(classchars, no_Magic(c));
719 if (p == NULL)
720 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200721 EMSGN("INTERNAL: Unknown character class char: %ld", c);
722 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200723 }
724#ifdef FEAT_MBYTE
725 /* When '.' is followed by a composing char ignore the dot, so that
726 * the composing char is matched here. */
727 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
728 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200729 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730 c = getchr();
731 goto nfa_do_multibyte;
732 }
733#endif
734 EMIT(nfa_classcodes[p - classchars]);
735 if (extra == ADD_NL)
736 {
737 EMIT(NFA_NEWL);
738 EMIT(NFA_OR);
739 regflags |= RF_HASNL;
740 }
741 break;
742
743 case Magic('n'):
744 if (reg_string)
745 /* In a string "\n" matches a newline character. */
746 EMIT(NL);
747 else
748 {
749 /* In buffer text "\n" matches the end of a line. */
750 EMIT(NFA_NEWL);
751 regflags |= RF_HASNL;
752 }
753 break;
754
755 case Magic('('):
756 if (nfa_reg(REG_PAREN) == FAIL)
757 return FAIL; /* cascaded error */
758 break;
759
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200760 case Magic('|'):
761 case Magic('&'):
762 case Magic(')'):
763 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200764 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200765 return FAIL;
766
767 case Magic('='):
768 case Magic('?'):
769 case Magic('+'):
770 case Magic('@'):
771 case Magic('*'):
772 case Magic('{'):
773 /* these should follow an atom, not form an atom */
774 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200775 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 return FAIL;
777
778 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200779 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200780 return FAIL;
781
Bram Moolenaar428e9872013-05-30 17:05:39 +0200782 case Magic('1'):
783 case Magic('2'):
784 case Magic('3'):
785 case Magic('4'):
786 case Magic('5'):
787 case Magic('6'):
788 case Magic('7'):
789 case Magic('8'):
790 case Magic('9'):
791 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
792 nfa_has_backref = TRUE;
793 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200794
795 case Magic('z'):
796 c = no_Magic(getchr());
797 switch (c)
798 {
799 case 's':
800 EMIT(NFA_ZSTART);
801 break;
802 case 'e':
803 EMIT(NFA_ZEND);
804 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200805 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200806 case '1':
807 case '2':
808 case '3':
809 case '4':
810 case '5':
811 case '6':
812 case '7':
813 case '8':
814 case '9':
815 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200816 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 return FAIL;
818 default:
819 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200820 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 no_Magic(c));
822 return FAIL;
823 }
824 break;
825
826 case Magic('%'):
827 c = no_Magic(getchr());
828 switch (c)
829 {
830 /* () without a back reference */
831 case '(':
832 if (nfa_reg(REG_NPAREN) == FAIL)
833 return FAIL;
834 EMIT(NFA_NOPEN);
835 break;
836
837 case 'd': /* %d123 decimal */
838 case 'o': /* %o123 octal */
839 case 'x': /* %xab hex 2 */
840 case 'u': /* %uabcd hex 4 */
841 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200842 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200843 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200844
Bram Moolenaar47196582013-05-25 22:04:23 +0200845 switch (c)
846 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200847 case 'd': nr = getdecchrs(); break;
848 case 'o': nr = getoctchrs(); break;
849 case 'x': nr = gethexchrs(2); break;
850 case 'u': nr = gethexchrs(4); break;
851 case 'U': nr = gethexchrs(8); break;
852 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200853 }
854
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200855 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200856 EMSG2_RET_FAIL(
857 _("E678: Invalid character after %s%%[dxouU]"),
858 reg_magic == MAGIC_ALL);
859 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200860 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200861 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200862 break;
863
864 /* Catch \%^ and \%$ regardless of where they appear in the
865 * pattern -- regardless of whether or not it makes sense. */
866 case '^':
867 EMIT(NFA_BOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200868 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200869 return FAIL;
870 break;
871
872 case '$':
873 EMIT(NFA_EOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200874 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 return FAIL;
876 break;
877
878 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200879 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 break;
881
882 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200883 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 return FAIL;
885 break;
886
887 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200888 /* TODO: \%[abc] not supported yet */
889 return FAIL;
890
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200891 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200892 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200893 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200894 int cmp = c;
895
896 if (c == '<' || c == '>')
897 c = getchr();
898 while (VIM_ISDIGIT(c))
899 {
900 n = n * 10 + (c - '0');
901 c = getchr();
902 }
903 if (c == 'l' || c == 'c' || c == 'v')
904 {
905 EMIT(n);
906 if (c == 'l')
907 EMIT(cmp == '<' ? NFA_LNUM_LT :
908 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
909 else if (c == 'c')
910 EMIT(cmp == '<' ? NFA_COL_LT :
911 cmp == '>' ? NFA_COL_GT : NFA_COL);
912 else
913 EMIT(cmp == '<' ? NFA_VCOL_LT :
914 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
915 break;
916 }
917 else if (c == '\'')
918 /* TODO: \%'m not supported yet */
919 return FAIL;
920 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200921 syntax_error = TRUE;
922 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
923 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200924 return FAIL;
925 }
926 break;
927
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200928 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200929collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200930 /*
931 * Glue is emitted between several atoms from the [].
932 * It is either NFA_OR, or NFA_CONCAT.
933 *
934 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
935 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
936 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
937 * notation)
938 *
939 */
940
941
942/* Emit negation atoms, if needed.
943 * The CONCAT below merges the NOT with the previous node. */
944#define TRY_NEG() \
945 if (negated == TRUE) \
946 { \
947 EMIT(NFA_NOT); \
948 }
949
950/* Emit glue between important nodes : CONCAT or OR. */
951#define EMIT_GLUE() \
952 if (first == FALSE) \
953 EMIT(glue); \
954 else \
955 first = FALSE;
956
957 p = regparse;
958 endp = skip_anyof(p);
959 if (*endp == ']')
960 {
961 /*
962 * Try to reverse engineer character classes. For example,
963 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
964 * and perform the necessary substitutions in the NFA.
965 */
966 result = nfa_recognize_char_class(regparse, endp,
967 extra == ADD_NL);
968 if (result != FAIL)
969 {
970 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
971 EMIT(result);
972 else /* must be char class + newline */
973 {
974 EMIT(result - ADD_NL);
975 EMIT(NFA_NEWL);
976 EMIT(NFA_OR);
977 }
978 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200979 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200980 return OK;
981 }
982 /*
983 * Failed to recognize a character class. Use the simple
984 * version that turns [abc] into 'a' OR 'b' OR 'c'
985 */
986 startc = endc = oldstartc = -1;
987 first = TRUE; /* Emitting first atom in this sequence? */
988 negated = FALSE;
989 glue = NFA_OR;
990 if (*regparse == '^') /* negated range */
991 {
992 negated = TRUE;
993 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200994 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200995 }
996 if (*regparse == '-')
997 {
998 startc = '-';
999 EMIT(startc);
1000 TRY_NEG();
1001 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001002 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001003 }
1004 /* Emit the OR branches for each character in the [] */
1005 emit_range = FALSE;
1006 while (regparse < endp)
1007 {
1008 oldstartc = startc;
1009 startc = -1;
1010 got_coll_char = FALSE;
1011 if (*regparse == '[')
1012 {
1013 /* Check for [: :], [= =], [. .] */
1014 equiclass = collclass = 0;
1015 charclass = get_char_class(&regparse);
1016 if (charclass == CLASS_NONE)
1017 {
1018 equiclass = get_equi_class(&regparse);
1019 if (equiclass == 0)
1020 collclass = get_coll_element(&regparse);
1021 }
1022
1023 /* Character class like [:alpha:] */
1024 if (charclass != CLASS_NONE)
1025 {
1026 switch (charclass)
1027 {
1028 case CLASS_ALNUM:
1029 EMIT(NFA_CLASS_ALNUM);
1030 break;
1031 case CLASS_ALPHA:
1032 EMIT(NFA_CLASS_ALPHA);
1033 break;
1034 case CLASS_BLANK:
1035 EMIT(NFA_CLASS_BLANK);
1036 break;
1037 case CLASS_CNTRL:
1038 EMIT(NFA_CLASS_CNTRL);
1039 break;
1040 case CLASS_DIGIT:
1041 EMIT(NFA_CLASS_DIGIT);
1042 break;
1043 case CLASS_GRAPH:
1044 EMIT(NFA_CLASS_GRAPH);
1045 break;
1046 case CLASS_LOWER:
1047 EMIT(NFA_CLASS_LOWER);
1048 break;
1049 case CLASS_PRINT:
1050 EMIT(NFA_CLASS_PRINT);
1051 break;
1052 case CLASS_PUNCT:
1053 EMIT(NFA_CLASS_PUNCT);
1054 break;
1055 case CLASS_SPACE:
1056 EMIT(NFA_CLASS_SPACE);
1057 break;
1058 case CLASS_UPPER:
1059 EMIT(NFA_CLASS_UPPER);
1060 break;
1061 case CLASS_XDIGIT:
1062 EMIT(NFA_CLASS_XDIGIT);
1063 break;
1064 case CLASS_TAB:
1065 EMIT(NFA_CLASS_TAB);
1066 break;
1067 case CLASS_RETURN:
1068 EMIT(NFA_CLASS_RETURN);
1069 break;
1070 case CLASS_BACKSPACE:
1071 EMIT(NFA_CLASS_BACKSPACE);
1072 break;
1073 case CLASS_ESCAPE:
1074 EMIT(NFA_CLASS_ESCAPE);
1075 break;
1076 }
1077 TRY_NEG();
1078 EMIT_GLUE();
1079 continue;
1080 }
1081 /* Try equivalence class [=a=] and the like */
1082 if (equiclass != 0)
1083 {
1084 result = nfa_emit_equi_class(equiclass, negated);
1085 if (result == FAIL)
1086 {
1087 /* should never happen */
1088 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1089 }
1090 EMIT_GLUE();
1091 continue;
1092 }
1093 /* Try collating class like [. .] */
1094 if (collclass != 0)
1095 {
1096 startc = collclass; /* allow [.a.]-x as a range */
1097 /* Will emit the proper atom at the end of the
1098 * while loop. */
1099 }
1100 }
1101 /* Try a range like 'a-x' or '\t-z' */
1102 if (*regparse == '-')
1103 {
1104 emit_range = TRUE;
1105 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001106 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001107 continue; /* reading the end of the range */
1108 }
1109
1110 /* Now handle simple and escaped characters.
1111 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1112 * accepts "\t", "\e", etc., but only when the 'l' flag in
1113 * 'cpoptions' is not included.
1114 * Posix doesn't recognize backslash at all.
1115 */
1116 if (*regparse == '\\'
1117 && !cpo_bsl
1118 && regparse + 1 <= endp
1119 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1120 || (!cpo_lit
1121 && vim_strchr(REGEXP_ABBR, regparse[1])
1122 != NULL)
1123 )
1124 )
1125 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001126 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001127
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001128 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001129 startc = reg_string ? NL : NFA_NEWL;
1130 else
1131 if (*regparse == 'd'
1132 || *regparse == 'o'
1133 || *regparse == 'x'
1134 || *regparse == 'u'
1135 || *regparse == 'U'
1136 )
1137 {
1138 /* TODO(RE) This needs more testing */
1139 startc = coll_get_char();
1140 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001141 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001142 }
1143 else
1144 {
1145 /* \r,\t,\e,\b */
1146 startc = backslash_trans(*regparse);
1147 }
1148 }
1149
1150 /* Normal printable char */
1151 if (startc == -1)
1152#ifdef FEAT_MBYTE
1153 startc = (*mb_ptr2char)(regparse);
1154#else
1155 startc = *regparse;
1156#endif
1157
1158 /* Previous char was '-', so this char is end of range. */
1159 if (emit_range)
1160 {
1161 endc = startc; startc = oldstartc;
1162 if (startc > endc)
1163 EMSG_RET_FAIL(_(e_invrange));
1164#ifdef FEAT_MBYTE
1165 if (has_mbyte && ((*mb_char2len)(startc) > 1
1166 || (*mb_char2len)(endc) > 1))
1167 {
1168 if (endc > startc + 256)
1169 EMSG_RET_FAIL(_(e_invrange));
1170 /* Emit the range. "startc" was already emitted, so
1171 * skip it. */
1172 for (c = startc + 1; c <= endc; c++)
1173 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001174 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001175 TRY_NEG();
1176 EMIT_GLUE();
1177 }
1178 emit_range = FALSE;
1179 }
1180 else
1181#endif
1182 {
1183#ifdef EBCDIC
1184 int alpha_only = FALSE;
1185
1186 /* for alphabetical range skip the gaps
1187 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1188 if (isalpha(startc) && isalpha(endc))
1189 alpha_only = TRUE;
1190#endif
1191 /* Emit the range. "startc" was already emitted, so
1192 * skip it. */
1193 for (c = startc + 1; c <= endc; c++)
1194#ifdef EBCDIC
1195 if (!alpha_only || isalpha(startc))
1196#endif
1197 {
1198 EMIT(c);
1199 TRY_NEG();
1200 EMIT_GLUE();
1201 }
1202 emit_range = FALSE;
1203 }
1204 }
1205 else
1206 {
1207 /*
1208 * This char (startc) is not part of a range. Just
1209 * emit it.
1210 *
1211 * Normally, simply emit startc. But if we get char
1212 * code=0 from a collating char, then replace it with
1213 * 0x0a.
1214 *
1215 * This is needed to completely mimic the behaviour of
1216 * the backtracking engine.
1217 */
1218 if (got_coll_char == TRUE && startc == 0)
1219 EMIT(0x0a);
1220 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001221 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001222 TRY_NEG();
1223 EMIT_GLUE();
1224 }
1225
Bram Moolenaar51a29832013-05-28 22:30:35 +02001226 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 } /* while (p < endp) */
1228
Bram Moolenaar51a29832013-05-28 22:30:35 +02001229 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001230 if (*regparse == '-') /* if last, '-' is just a char */
1231 {
1232 EMIT('-');
1233 TRY_NEG();
1234 EMIT_GLUE();
1235 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001236 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001237
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 /* skip the trailing ] */
1239 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001240 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 if (negated == TRUE)
1242 {
1243 /* Mark end of negated char range */
1244 EMIT(NFA_END_NEG_RANGE);
1245 EMIT(NFA_CONCAT);
1246 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001247
1248 /* \_[] also matches \n but it's not negated */
1249 if (extra == ADD_NL)
1250 {
1251 EMIT(reg_string ? NL : NFA_NEWL);
1252 EMIT(NFA_OR);
1253 }
1254
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001256 } /* if exists closing ] */
1257
1258 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259 {
1260 syntax_error = TRUE;
1261 EMSG_RET_FAIL(_(e_missingbracket));
1262 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001263 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001265 default:
1266 {
1267#ifdef FEAT_MBYTE
1268 int plen;
1269
1270nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001271 /* plen is length of current char with composing chars */
1272 if (enc_utf8 && ((*mb_char2len)(c)
1273 != (plen = (*mb_ptr2len)(old_regparse))
1274 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001275 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001276 int i = 0;
1277
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001278 /* A base character plus composing characters, or just one
1279 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001280 * This requires creating a separate atom as if enclosing
1281 * the characters in (), where NFA_COMPOSING is the ( and
1282 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001283 * building the postfix form, not the NFA itself;
1284 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001285 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001286 for (;;)
1287 {
1288 EMIT(c);
1289 if (i > 0)
1290 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001291 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001292 break;
1293 c = utf_ptr2char(old_regparse + i);
1294 }
1295 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001296 regparse = old_regparse + plen;
1297 }
1298 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001299#endif
1300 {
1301 c = no_Magic(c);
1302 EMIT(c);
1303 }
1304 return OK;
1305 }
1306 }
1307
1308#undef TRY_NEG
1309#undef EMIT_GLUE
1310
1311 return OK;
1312}
1313
1314/*
1315 * Parse something followed by possible [*+=].
1316 *
1317 * A piece is an atom, possibly followed by a multi, an indication of how many
1318 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1319 * characters: "", "a", "aa", etc.
1320 *
1321 * piece ::= atom
1322 * or atom multi
1323 */
1324 static int
1325nfa_regpiece()
1326{
1327 int i;
1328 int op;
1329 int ret;
1330 long minval, maxval;
1331 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1332 char_u *old_regparse, *new_regparse;
1333 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001334 int old_post_pos;
1335 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 int old_regnpar;
1337 int quest;
1338
1339 /* Save the current position in the regexp, so that we can use it if
1340 * <atom>{m,n} is next. */
1341 old_regparse = regparse;
1342 /* Save current number of open parenthesis, so we can use it if
1343 * <atom>{m,n} is next */
1344 old_regnpar = regnpar;
1345 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001346 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001347
1348 ret = nfa_regatom();
1349 if (ret == FAIL)
1350 return FAIL; /* cascaded error */
1351
1352 op = peekchr();
1353 if (re_multi_type(op) == NOT_MULTI)
1354 return OK;
1355
1356 skipchr();
1357 switch (op)
1358 {
1359 case Magic('*'):
1360 EMIT(NFA_STAR);
1361 break;
1362
1363 case Magic('+'):
1364 /*
1365 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1366 * first and only submatch would be "aaa". But the backtracking
1367 * engine interprets the plus as "try matching one more time", and
1368 * a* matches a second time at the end of the input, the empty
1369 * string.
1370 * The submatch will the empty string.
1371 *
1372 * In order to be consistent with the old engine, we disable
1373 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1374 */
1375 /* EMIT(NFA_PLUS); */
1376 regnpar = old_regnpar;
1377 regparse = old_regparse;
1378 curchr = -1;
1379 if (nfa_regatom() == FAIL)
1380 return FAIL;
1381 EMIT(NFA_STAR);
1382 EMIT(NFA_CONCAT);
1383 skipchr(); /* skip the \+ */
1384 break;
1385
1386 case Magic('@'):
1387 op = no_Magic(getchr());
1388 switch(op)
1389 {
1390 case '=':
1391 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1392 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001393 case '!':
1394 EMIT(NFA_PREV_ATOM_NO_WIDTH_NEG);
1395 break;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001396 case '0':
1397 case '1':
1398 case '2':
1399 case '3':
1400 case '4':
1401 case '5':
1402 case '6':
1403 case '7':
1404 case '8':
1405 case '9':
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 case '<':
1407 case '>':
1408 /* Not supported yet */
1409 return FAIL;
1410 default:
1411 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001412 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 return FAIL;
1414 }
1415 break;
1416
1417 case Magic('?'):
1418 case Magic('='):
1419 EMIT(NFA_QUEST);
1420 break;
1421
1422 case Magic('{'):
1423 /* a{2,5} will expand to 'aaa?a?a?'
1424 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1425 * version of '?'
1426 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1427 * parenthesis have the same id
1428 */
1429
1430 greedy = TRUE;
1431 c2 = peekchr();
1432 if (c2 == '-' || c2 == Magic('-'))
1433 {
1434 skipchr();
1435 greedy = FALSE;
1436 }
1437 if (!read_limits(&minval, &maxval))
1438 {
1439 syntax_error = TRUE;
1440 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1441 }
1442 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1443 * <atom>* */
1444 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1445 {
1446 EMIT(NFA_STAR);
1447 break;
1448 }
1449
1450 if (maxval > NFA_BRACES_MAXLIMIT)
1451 {
1452 /* This would yield a huge automaton and use too much memory.
1453 * Revert to old engine */
1454 return FAIL;
1455 }
1456
1457 /* Special case: x{0} or x{-0} */
1458 if (maxval == 0)
1459 {
1460 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001461 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1463 EMIT(NFA_SKIP_CHAR);
1464 return OK;
1465 }
1466
1467 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001468 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001469 /* Save pos after the repeated atom and the \{} */
1470 new_regparse = regparse;
1471
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001472 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1473 for (i = 0; i < maxval; i++)
1474 {
1475 /* Goto beginning of the repeated atom */
1476 regparse = old_regparse;
1477 curchr = -1;
1478 /* Restore count of parenthesis */
1479 regnpar = old_regnpar;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001480 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001481 if (nfa_regatom() == FAIL)
1482 return FAIL;
1483 /* after "minval" times, atoms are optional */
1484 if (i + 1 > minval)
1485 EMIT(quest);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001486 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 EMIT(NFA_CONCAT);
1488 }
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;
1783 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1784 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1785 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1786 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1787 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1788 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1789 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1790 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1791 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1792 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1793 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1794 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1795 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1796 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1797 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1798 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1799 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1800 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1801 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1802 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1803 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1804 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1805 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1806 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1807
1808 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1809 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1810 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1811 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1812 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1813 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1814 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1815 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1816 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1817 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1818 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1819 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1820 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1821 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1822 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1823 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1824 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1825 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1826 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1827 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1828 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1829 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1830 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1831 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1832 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1833 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1834 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1835
1836 default:
1837 STRCPY(code, "CHAR(x)");
1838 code[5] = c;
1839 }
1840
1841 if (addnl == TRUE)
1842 STRCAT(code, " + NEWLINE ");
1843
1844}
1845
1846#ifdef ENABLE_LOG
1847static FILE *log_fd;
1848
1849/*
1850 * Print the postfix notation of the current regexp.
1851 */
1852 static void
1853nfa_postfix_dump(expr, retval)
1854 char_u *expr;
1855 int retval;
1856{
1857 int *p;
1858 FILE *f;
1859
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001860 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001861 if (f != NULL)
1862 {
1863 fprintf(f, "\n-------------------------\n");
1864 if (retval == FAIL)
1865 fprintf(f, ">>> NFA engine failed ... \n");
1866 else if (retval == OK)
1867 fprintf(f, ">>> NFA engine succeeded !\n");
1868 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001869 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870 {
1871 nfa_set_code(*p);
1872 fprintf(f, "%s, ", code);
1873 }
1874 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001875 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001876 fprintf(f, "%d ", *p);
1877 fprintf(f, "\n\n");
1878 fclose(f);
1879 }
1880}
1881
1882/*
1883 * Print the NFA starting with a root node "state".
1884 */
1885 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001886nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887 FILE *debugf;
1888 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001890 garray_T indent;
1891
1892 ga_init2(&indent, 1, 64);
1893 ga_append(&indent, '\0');
1894 nfa_print_state2(debugf, state, &indent);
1895 ga_clear(&indent);
1896}
1897
1898 static void
1899nfa_print_state2(debugf, state, indent)
1900 FILE *debugf;
1901 nfa_state_T *state;
1902 garray_T *indent;
1903{
1904 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905
1906 if (state == NULL)
1907 return;
1908
1909 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001910
1911 /* Output indent */
1912 p = (char_u *)indent->ga_data;
1913 if (indent->ga_len >= 3)
1914 {
1915 int last = indent->ga_len - 3;
1916 char_u save[2];
1917
1918 STRNCPY(save, &p[last], 2);
1919 STRNCPY(&p[last], "+-", 2);
1920 fprintf(debugf, " %s", p);
1921 STRNCPY(&p[last], save, 2);
1922 }
1923 else
1924 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925
1926 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001927 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1928 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929 if (state->id < 0)
1930 return;
1931
1932 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001933
1934 /* grow indent for state->out */
1935 indent->ga_len -= 1;
1936 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001937 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001938 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001939 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001940 ga_append(indent, '\0');
1941
1942 nfa_print_state2(debugf, state->out, indent);
1943
1944 /* replace last part of indent for state->out1 */
1945 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001946 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001947 ga_append(indent, '\0');
1948
1949 nfa_print_state2(debugf, state->out1, indent);
1950
1951 /* shrink indent */
1952 indent->ga_len -= 3;
1953 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001954}
1955
1956/*
1957 * Print the NFA state machine.
1958 */
1959 static void
1960nfa_dump(prog)
1961 nfa_regprog_T *prog;
1962{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001963 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964
1965 if (debugf != NULL)
1966 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001967 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968 fclose(debugf);
1969 }
1970}
1971#endif /* ENABLE_LOG */
1972#endif /* DEBUG */
1973
1974/*
1975 * Parse r.e. @expr and convert it into postfix form.
1976 * Return the postfix string on success, NULL otherwise.
1977 */
1978 static int *
1979re2post()
1980{
1981 if (nfa_reg(REG_NOPAREN) == FAIL)
1982 return NULL;
1983 EMIT(NFA_MOPEN);
1984 return post_start;
1985}
1986
1987/* NB. Some of the code below is inspired by Russ's. */
1988
1989/*
1990 * Represents an NFA state plus zero or one or two arrows exiting.
1991 * if c == MATCH, no arrows out; matching state.
1992 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1993 * If c < 256, labeled arrow with character c to out.
1994 */
1995
1996static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1997
1998/*
1999 * Allocate and initialize nfa_state_T.
2000 */
2001 static nfa_state_T *
2002new_state(c, out, out1)
2003 int c;
2004 nfa_state_T *out;
2005 nfa_state_T *out1;
2006{
2007 nfa_state_T *s;
2008
2009 if (istate >= nstate)
2010 return NULL;
2011
2012 s = &state_ptr[istate++];
2013
2014 s->c = c;
2015 s->out = out;
2016 s->out1 = out1;
2017
2018 s->id = istate;
2019 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002020 s->negated = FALSE;
2021
2022 return s;
2023}
2024
2025/*
2026 * A partially built NFA without the matching state filled in.
2027 * Frag_T.start points at the start state.
2028 * Frag_T.out is a list of places that need to be set to the
2029 * next state for this fragment.
2030 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002031
2032/* Since the out pointers in the list are always
2033 * uninitialized, we use the pointers themselves
2034 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002036union Ptrlist
2037{
2038 Ptrlist *next;
2039 nfa_state_T *s;
2040};
2041
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042struct Frag
2043{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002044 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002045 Ptrlist *out;
2046};
2047typedef struct Frag Frag_T;
2048
2049static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2050static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2051static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2052static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2053static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2054static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2055
2056/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002057 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 */
2059 static Frag_T
2060frag(start, out)
2061 nfa_state_T *start;
2062 Ptrlist *out;
2063{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002064 Frag_T n;
2065
2066 n.start = start;
2067 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 return n;
2069}
2070
2071/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 * Create singleton list containing just outp.
2073 */
2074 static Ptrlist *
2075list1(outp)
2076 nfa_state_T **outp;
2077{
2078 Ptrlist *l;
2079
2080 l = (Ptrlist *)outp;
2081 l->next = NULL;
2082 return l;
2083}
2084
2085/*
2086 * Patch the list of states at out to point to start.
2087 */
2088 static void
2089patch(l, s)
2090 Ptrlist *l;
2091 nfa_state_T *s;
2092{
2093 Ptrlist *next;
2094
2095 for (; l; l = next)
2096 {
2097 next = l->next;
2098 l->s = s;
2099 }
2100}
2101
2102
2103/*
2104 * Join the two lists l1 and l2, returning the combination.
2105 */
2106 static Ptrlist *
2107append(l1, l2)
2108 Ptrlist *l1;
2109 Ptrlist *l2;
2110{
2111 Ptrlist *oldl1;
2112
2113 oldl1 = l1;
2114 while (l1->next)
2115 l1 = l1->next;
2116 l1->next = l2;
2117 return oldl1;
2118}
2119
2120/*
2121 * Stack used for transforming postfix form into NFA.
2122 */
2123static Frag_T empty;
2124
2125 static void
2126st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002127 int *postfix UNUSED;
2128 int *end UNUSED;
2129 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002130{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002131#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002132 FILE *df;
2133 int *p2;
2134
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002135 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002136 if (df)
2137 {
2138 fprintf(df, "Error popping the stack!\n");
2139#ifdef DEBUG
2140 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2141#endif
2142 fprintf(df, "Postfix form is: ");
2143#ifdef DEBUG
2144 for (p2 = postfix; p2 < end; p2++)
2145 {
2146 nfa_set_code(*p2);
2147 fprintf(df, "%s, ", code);
2148 }
2149 nfa_set_code(*p);
2150 fprintf(df, "\nCurrent position is: ");
2151 for (p2 = postfix; p2 <= p; p2 ++)
2152 {
2153 nfa_set_code(*p2);
2154 fprintf(df, "%s, ", code);
2155 }
2156#else
2157 for (p2 = postfix; p2 < end; p2++)
2158 {
2159 fprintf(df, "%d, ", *p2);
2160 }
2161 fprintf(df, "\nCurrent position is: ");
2162 for (p2 = postfix; p2 <= p; p2 ++)
2163 {
2164 fprintf(df, "%d, ", *p2);
2165 }
2166#endif
2167 fprintf(df, "\n--------------------------\n");
2168 fclose(df);
2169 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002170#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002171 EMSG(_("E874: (NFA) Could not pop the stack !"));
2172}
2173
2174/*
2175 * Push an item onto the stack.
2176 */
2177 static void
2178st_push(s, p, stack_end)
2179 Frag_T s;
2180 Frag_T **p;
2181 Frag_T *stack_end;
2182{
2183 Frag_T *stackp = *p;
2184
2185 if (stackp >= stack_end)
2186 return;
2187 *stackp = s;
2188 *p = *p + 1;
2189}
2190
2191/*
2192 * Pop an item from the stack.
2193 */
2194 static Frag_T
2195st_pop(p, stack)
2196 Frag_T **p;
2197 Frag_T *stack;
2198{
2199 Frag_T *stackp;
2200
2201 *p = *p - 1;
2202 stackp = *p;
2203 if (stackp < stack)
2204 return empty;
2205 return **p;
2206}
2207
2208/*
2209 * Convert a postfix form into its equivalent NFA.
2210 * Return the NFA start state on success, NULL otherwise.
2211 */
2212 static nfa_state_T *
2213post2nfa(postfix, end, nfa_calc_size)
2214 int *postfix;
2215 int *end;
2216 int nfa_calc_size;
2217{
2218 int *p;
2219 int mopen;
2220 int mclose;
2221 Frag_T *stack = NULL;
2222 Frag_T *stackp = NULL;
2223 Frag_T *stack_end = NULL;
2224 Frag_T e1;
2225 Frag_T e2;
2226 Frag_T e;
2227 nfa_state_T *s;
2228 nfa_state_T *s1;
2229 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002230 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002231
2232 if (postfix == NULL)
2233 return NULL;
2234
Bram Moolenaar053bb602013-05-20 13:55:21 +02002235#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236#define POP() st_pop(&stackp, stack); \
2237 if (stackp < stack) \
2238 { \
2239 st_error(postfix, end, p); \
2240 return NULL; \
2241 }
2242
2243 if (nfa_calc_size == FALSE)
2244 {
2245 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002246 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002247 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002248 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002249 }
2250
2251 for (p = postfix; p < end; ++p)
2252 {
2253 switch (*p)
2254 {
2255 case NFA_CONCAT:
2256 /* Catenation.
2257 * Pay attention: this operator does not exist
2258 * in the r.e. itself (it is implicit, really).
2259 * It is added when r.e. is translated to postfix
2260 * form in re2post().
2261 *
2262 * No new state added here. */
2263 if (nfa_calc_size == TRUE)
2264 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002265 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 break;
2267 }
2268 e2 = POP();
2269 e1 = POP();
2270 patch(e1.out, e2.start);
2271 PUSH(frag(e1.start, e2.out));
2272 break;
2273
2274 case NFA_NOT:
2275 /* Negation of a character */
2276 if (nfa_calc_size == TRUE)
2277 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002278 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279 break;
2280 }
2281 e1 = POP();
2282 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002283#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002284 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002286#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287 PUSH(e1);
2288 break;
2289
2290 case NFA_OR:
2291 /* Alternation */
2292 if (nfa_calc_size == TRUE)
2293 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002294 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 break;
2296 }
2297 e2 = POP();
2298 e1 = POP();
2299 s = new_state(NFA_SPLIT, e1.start, e2.start);
2300 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002301 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302 PUSH(frag(s, append(e1.out, e2.out)));
2303 break;
2304
2305 case NFA_STAR:
2306 /* Zero or more */
2307 if (nfa_calc_size == TRUE)
2308 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002309 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002310 break;
2311 }
2312 e = POP();
2313 s = new_state(NFA_SPLIT, e.start, NULL);
2314 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002315 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002316 patch(e.out, s);
2317 PUSH(frag(s, list1(&s->out1)));
2318 break;
2319
2320 case NFA_QUEST:
2321 /* one or zero atoms=> greedy match */
2322 if (nfa_calc_size == TRUE)
2323 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002324 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325 break;
2326 }
2327 e = POP();
2328 s = new_state(NFA_SPLIT, e.start, NULL);
2329 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002330 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331 PUSH(frag(s, append(e.out, list1(&s->out1))));
2332 break;
2333
2334 case NFA_QUEST_NONGREEDY:
2335 /* zero or one atoms => non-greedy match */
2336 if (nfa_calc_size == TRUE)
2337 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002338 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 break;
2340 }
2341 e = POP();
2342 s = new_state(NFA_SPLIT, NULL, e.start);
2343 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002344 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002345 PUSH(frag(s, append(e.out, list1(&s->out))));
2346 break;
2347
2348 case NFA_PLUS:
2349 /* One or more */
2350 if (nfa_calc_size == TRUE)
2351 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002352 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002353 break;
2354 }
2355 e = POP();
2356 s = new_state(NFA_SPLIT, e.start, NULL);
2357 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002358 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002359 patch(e.out, s);
2360 PUSH(frag(e.start, list1(&s->out1)));
2361 break;
2362
2363 case NFA_SKIP_CHAR:
2364 /* Symbol of 0-length, Used in a repetition
2365 * with max/min count of 0 */
2366 if (nfa_calc_size == TRUE)
2367 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002368 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369 break;
2370 }
2371 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2372 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002373 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002374 PUSH(frag(s, list1(&s->out)));
2375 break;
2376
2377 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002378 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002379 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002380 * The \@! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002382 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383
2384 if (nfa_calc_size == TRUE)
2385 {
2386 nstate += 2;
2387 break;
2388 }
2389 e = POP();
2390 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2391 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002392 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002393 patch(e.out, s1);
2394
2395 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2396 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002397 goto theend;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002398 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG)
2399 {
2400 s->negated = TRUE;
2401 s1->negated = TRUE;
2402 }
2403
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002404 PUSH(frag(s, list1(&s1->out)));
2405 break;
2406
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002407#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002408 case NFA_COMPOSING: /* char with composing char */
2409#if 0
2410 /* TODO */
2411 if (regflags & RF_ICOMBINE)
2412 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002413 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002414 }
2415#endif
2416 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002417#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002418
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002419 case NFA_MOPEN + 0: /* Submatch */
2420 case NFA_MOPEN + 1:
2421 case NFA_MOPEN + 2:
2422 case NFA_MOPEN + 3:
2423 case NFA_MOPEN + 4:
2424 case NFA_MOPEN + 5:
2425 case NFA_MOPEN + 6:
2426 case NFA_MOPEN + 7:
2427 case NFA_MOPEN + 8:
2428 case NFA_MOPEN + 9:
2429 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 if (nfa_calc_size == TRUE)
2431 {
2432 nstate += 2;
2433 break;
2434 }
2435
2436 mopen = *p;
2437 switch (*p)
2438 {
2439 case NFA_NOPEN:
2440 mclose = NFA_NCLOSE;
2441 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002442#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 case NFA_COMPOSING:
2444 mclose = NFA_END_COMPOSING;
2445 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002446#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447 default:
2448 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2449 mclose = *p + NSUBEXP;
2450 break;
2451 }
2452
2453 /* Allow "NFA_MOPEN" as a valid postfix representation for
2454 * the empty regexp "". In this case, the NFA will be
2455 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2456 * empty groups of parenthesis, and empty mbyte chars */
2457 if (stackp == stack)
2458 {
2459 s = new_state(mopen, NULL, NULL);
2460 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002461 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462 s1 = new_state(mclose, NULL, NULL);
2463 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002464 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465 patch(list1(&s->out), s1);
2466 PUSH(frag(s, list1(&s1->out)));
2467 break;
2468 }
2469
2470 /* At least one node was emitted before NFA_MOPEN, so
2471 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2472 e = POP();
2473 s = new_state(mopen, e.start, NULL); /* `(' */
2474 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002475 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002476
2477 s1 = new_state(mclose, NULL, NULL); /* `)' */
2478 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002479 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002480 patch(e.out, s1);
2481
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002482#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002483 if (mopen == NFA_COMPOSING)
2484 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002485 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002486#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002487
2488 PUSH(frag(s, list1(&s1->out)));
2489 break;
2490
Bram Moolenaar5714b802013-05-28 22:03:20 +02002491 case NFA_BACKREF1:
2492 case NFA_BACKREF2:
2493 case NFA_BACKREF3:
2494 case NFA_BACKREF4:
2495 case NFA_BACKREF5:
2496 case NFA_BACKREF6:
2497 case NFA_BACKREF7:
2498 case NFA_BACKREF8:
2499 case NFA_BACKREF9:
2500 if (nfa_calc_size == TRUE)
2501 {
2502 nstate += 2;
2503 break;
2504 }
2505 s = new_state(*p, NULL, NULL);
2506 if (s == NULL)
2507 goto theend;
2508 s1 = new_state(NFA_SKIP, NULL, NULL);
2509 if (s1 == NULL)
2510 goto theend;
2511 patch(list1(&s->out), s1);
2512 PUSH(frag(s, list1(&s1->out)));
2513 break;
2514
Bram Moolenaar423532e2013-05-29 21:14:42 +02002515 case NFA_LNUM:
2516 case NFA_LNUM_GT:
2517 case NFA_LNUM_LT:
2518 case NFA_VCOL:
2519 case NFA_VCOL_GT:
2520 case NFA_VCOL_LT:
2521 case NFA_COL:
2522 case NFA_COL_GT:
2523 case NFA_COL_LT:
2524 if (nfa_calc_size == TRUE)
2525 {
2526 nstate += 1;
2527 break;
2528 }
2529 e1 = POP();
2530 s = new_state(*p, NULL, NULL);
2531 if (s == NULL)
2532 goto theend;
2533 s->val = e1.start->c;
2534 PUSH(frag(s, list1(&s->out)));
2535 break;
2536
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002537 case NFA_ZSTART:
2538 case NFA_ZEND:
2539 default:
2540 /* Operands */
2541 if (nfa_calc_size == TRUE)
2542 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002543 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544 break;
2545 }
2546 s = new_state(*p, NULL, NULL);
2547 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002548 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002549 PUSH(frag(s, list1(&s->out)));
2550 break;
2551
2552 } /* switch(*p) */
2553
2554 } /* for(p = postfix; *p; ++p) */
2555
2556 if (nfa_calc_size == TRUE)
2557 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002558 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002559 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560 }
2561
2562 e = POP();
2563 if (stackp != stack)
2564 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2565
2566 if (istate >= nstate)
2567 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2568
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002569 matchstate = &state_ptr[istate++]; /* the match state */
2570 matchstate->c = NFA_MATCH;
2571 matchstate->out = matchstate->out1 = NULL;
2572
2573 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002574 ret = e.start;
2575
2576theend:
2577 vim_free(stack);
2578 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002579
2580#undef POP1
2581#undef PUSH1
2582#undef POP2
2583#undef PUSH2
2584#undef POP
2585#undef PUSH
2586}
2587
2588/****************************************************************
2589 * NFA execution code.
2590 ****************************************************************/
2591
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002592typedef struct
2593{
2594 int in_use; /* number of subexpr with useful info */
2595
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002596 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002597 union
2598 {
2599 struct multipos
2600 {
2601 lpos_T start;
2602 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002603 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002604 struct linepos
2605 {
2606 char_u *start;
2607 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002608 } line[NSUBEXP];
2609 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002610} regsub_T;
2611
Bram Moolenaar963fee22013-05-26 21:47:28 +02002612/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002613typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614{
2615 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002616 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002617 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002618} nfa_thread_T;
2619
Bram Moolenaar963fee22013-05-26 21:47:28 +02002620/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002621typedef struct
2622{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002623 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002624 int n; /* nr of states currently in "t" */
2625 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002626 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002627} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002628
Bram Moolenaar5714b802013-05-28 22:03:20 +02002629#ifdef ENABLE_LOG
2630 static void
2631log_subexpr(sub)
2632 regsub_T *sub;
2633{
2634 int j;
2635
2636 for (j = 0; j < sub->in_use; j++)
2637 if (REG_MULTI)
2638 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2639 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002640 sub->list.multi[j].start.col,
2641 (int)sub->list.multi[j].start.lnum,
2642 sub->list.multi[j].end.col,
2643 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002644 else
2645 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2646 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002647 (char *)sub->list.line[j].start,
2648 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002649 fprintf(log_fd, "\n");
2650}
2651#endif
2652
Bram Moolenaar963fee22013-05-26 21:47:28 +02002653/* Used during execution: whether a match has been found. */
2654static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002655
Bram Moolenaar428e9872013-05-30 17:05:39 +02002656static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002657static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2658static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659
Bram Moolenaar428e9872013-05-30 17:05:39 +02002660/*
2661 * Return TRUE if "sub1" and "sub2" have the same positions.
2662 */
2663 static int
2664sub_equal(sub1, sub2)
2665 regsub_T *sub1;
2666 regsub_T *sub2;
2667{
2668 int i;
2669 int todo;
2670 linenr_T s1, e1;
2671 linenr_T s2, e2;
2672 char_u *sp1, *ep1;
2673 char_u *sp2, *ep2;
2674
2675 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2676 if (REG_MULTI)
2677 {
2678 for (i = 0; i < todo; ++i)
2679 {
2680 if (i < sub1->in_use)
2681 {
2682 s1 = sub1->list.multi[i].start.lnum;
2683 e1 = sub1->list.multi[i].end.lnum;
2684 }
2685 else
2686 {
2687 s1 = 0;
2688 e1 = 0;
2689 }
2690 if (i < sub2->in_use)
2691 {
2692 s2 = sub2->list.multi[i].start.lnum;
2693 e2 = sub2->list.multi[i].end.lnum;
2694 }
2695 else
2696 {
2697 s2 = 0;
2698 e2 = 0;
2699 }
2700 if (s1 != s2 || e1 != e2)
2701 return FALSE;
2702 if (s1 != 0 && sub1->list.multi[i].start.col
2703 != sub2->list.multi[i].start.col)
2704 return FALSE;
2705 if (e1 != 0 && sub1->list.multi[i].end.col
2706 != sub2->list.multi[i].end.col)
2707 return FALSE;
2708 }
2709 }
2710 else
2711 {
2712 for (i = 0; i < todo; ++i)
2713 {
2714 if (i < sub1->in_use)
2715 {
2716 sp1 = sub1->list.line[i].start;
2717 ep1 = sub1->list.line[i].end;
2718 }
2719 else
2720 {
2721 sp1 = NULL;
2722 ep1 = NULL;
2723 }
2724 if (i < sub2->in_use)
2725 {
2726 sp2 = sub2->list.line[i].start;
2727 ep2 = sub2->list.line[i].end;
2728 }
2729 else
2730 {
2731 sp2 = NULL;
2732 ep2 = NULL;
2733 }
2734 if (sp1 != sp2 || ep1 != ep2)
2735 return FALSE;
2736 }
2737 }
2738
2739 return TRUE;
2740}
2741
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002743addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002744 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002745 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002746 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002747 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002749 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002750 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002751 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002752 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002753 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002754 int i;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002755#ifdef ENABLE_LOG
2756 int did_print = FALSE;
2757#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002758
2759 if (l == NULL || state == NULL)
2760 return;
2761
2762 switch (state->c)
2763 {
2764 case NFA_SPLIT:
2765 case NFA_NOT:
2766 case NFA_NOPEN:
2767 case NFA_NCLOSE:
2768 case NFA_MCLOSE:
2769 case NFA_MCLOSE + 1:
2770 case NFA_MCLOSE + 2:
2771 case NFA_MCLOSE + 3:
2772 case NFA_MCLOSE + 4:
2773 case NFA_MCLOSE + 5:
2774 case NFA_MCLOSE + 6:
2775 case NFA_MCLOSE + 7:
2776 case NFA_MCLOSE + 8:
2777 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002778 /* These nodes are not added themselves but their "out" and/or
2779 * "out1" may be added below. */
2780 break;
2781
2782 case NFA_MOPEN:
2783 case NFA_MOPEN + 1:
2784 case NFA_MOPEN + 2:
2785 case NFA_MOPEN + 3:
2786 case NFA_MOPEN + 4:
2787 case NFA_MOPEN + 5:
2788 case NFA_MOPEN + 6:
2789 case NFA_MOPEN + 7:
2790 case NFA_MOPEN + 8:
2791 case NFA_MOPEN + 9:
2792 /* These nodes do not need to be added, but we need to bail out
2793 * when it was tried to be added to this list before. */
2794 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002795 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002796 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002797 break;
2798
2799 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002800 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002801 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002802 /* This state is already in the list, don't add it again,
2803 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002804 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002805 {
2806skip_add:
2807#ifdef ENABLE_LOG
2808 nfa_set_code(state->c);
2809 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
2810 abs(state->id), l->id, state->c, code);
2811#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02002812 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002813 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02002814
2815 /* See if the same state is already in the list with the same
2816 * positions. */
2817 for (i = 0; i < l->n; ++i)
2818 {
2819 thread = &l->t[i];
2820 if (thread->state->id == state->id
2821 && sub_equal(&thread->sub, sub))
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002822 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002823 }
2824 }
2825
2826 /* when there are backreferences the number of states may be (a
2827 * lot) bigger */
2828 if (nfa_has_backref && l->n == l->len)
2829 {
2830 int newlen = l->len * 3 / 2 + 50;
2831
2832 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2833 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002835
2836 /* add the state to the list */
2837 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002838 thread = &l->t[l->n++];
2839 thread->state = state;
2840 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002841 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002843 /* Copy the match start and end positions. */
2844 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002845 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002846 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002847 sizeof(struct multipos) * sub->in_use);
2848 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002849 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002850 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002851 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002852 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002853#ifdef ENABLE_LOG
2854 {
2855 int col;
2856
2857 if (thread->sub.in_use <= 0)
2858 col = -1;
2859 else if (REG_MULTI)
2860 col = thread->sub.list.multi[0].start.col;
2861 else
2862 col = (int)(thread->sub.list.line[0].start - regline);
2863 nfa_set_code(state->c);
2864 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
2865 abs(state->id), l->id, state->c, code, col);
2866 did_print = TRUE;
2867 }
2868#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869 }
2870
2871#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002872 if (!did_print)
2873 {
2874 int col;
2875
2876 if (sub->in_use <= 0)
2877 col = -1;
2878 else if (REG_MULTI)
2879 col = sub->list.multi[0].start.col;
2880 else
2881 col = (int)(sub->list.line[0].start - regline);
2882 nfa_set_code(state->c);
2883 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
2884 abs(state->id), l->id, state->c, code, col);
2885 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886#endif
2887 switch (state->c)
2888 {
2889 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002890 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891 break;
2892
2893 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002894 addstate(l, state->out, sub, off);
2895 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 break;
2897
2898#if 0
2899 case NFA_END_NEG_RANGE:
2900 /* Nothing to handle here. nfa_regmatch() will take care of it */
2901 break;
2902
2903 case NFA_NOT:
2904 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2905#ifdef ENABLE_LOG
2906 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2907#endif
2908 break;
2909
2910 case NFA_COMPOSING:
2911 /* nfa_regmatch() will match all the bytes of this composing char. */
2912 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002913#endif
2914
Bram Moolenaar5714b802013-05-28 22:03:20 +02002915 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002916 case NFA_NOPEN:
2917 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002918 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002919 break;
2920
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921 case NFA_MOPEN + 0:
2922 case NFA_MOPEN + 1:
2923 case NFA_MOPEN + 2:
2924 case NFA_MOPEN + 3:
2925 case NFA_MOPEN + 4:
2926 case NFA_MOPEN + 5:
2927 case NFA_MOPEN + 6:
2928 case NFA_MOPEN + 7:
2929 case NFA_MOPEN + 8:
2930 case NFA_MOPEN + 9:
2931 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002932 if (state->c == NFA_ZSTART)
2933 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002934 else
2935 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002936
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002937 /* Set the position (with "off") in the subexpression. Save and
2938 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02002939 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 if (REG_MULTI)
2941 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002942 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002943 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002944 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002945 save_in_use = -1;
2946 }
2947 else
2948 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002949 save_in_use = sub->in_use;
2950 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002951 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002952 sub->list.multi[i].start.lnum = -1;
2953 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002954 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002955 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002956 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002957 if (off == -1)
2958 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002959 sub->list.multi[subidx].start.lnum = reglnum + 1;
2960 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002961 }
2962 else
2963 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002964 sub->list.multi[subidx].start.lnum = reglnum;
2965 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002966 (colnr_T)(reginput - regline + off);
2967 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002968 }
2969 else
2970 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002971 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002972 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002973 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002974 save_in_use = -1;
2975 }
2976 else
2977 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002978 save_in_use = sub->in_use;
2979 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002980 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002981 sub->list.line[i].start = NULL;
2982 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002983 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002984 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002985 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002986 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002987 }
2988
Bram Moolenaar5714b802013-05-28 22:03:20 +02002989 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002990
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002991 if (save_in_use == -1)
2992 {
2993 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002994 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002995 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002996 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002997 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002998 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002999 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000 break;
3001
3002 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003003 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003004 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003005 /* Do not overwrite the position set by \ze. If no \ze
3006 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003007 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003008 break;
3009 }
3010 case NFA_MCLOSE + 1:
3011 case NFA_MCLOSE + 2:
3012 case NFA_MCLOSE + 3:
3013 case NFA_MCLOSE + 4:
3014 case NFA_MCLOSE + 5:
3015 case NFA_MCLOSE + 6:
3016 case NFA_MCLOSE + 7:
3017 case NFA_MCLOSE + 8:
3018 case NFA_MCLOSE + 9:
3019 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003020 if (state->c == NFA_ZEND)
3021 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003022 else
3023 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003024
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003025 /* We don't fill in gaps here, there must have been an MOPEN that
3026 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003027 save_in_use = sub->in_use;
3028 if (sub->in_use <= subidx)
3029 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003030 if (REG_MULTI)
3031 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003032 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003033 if (off == -1)
3034 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003035 sub->list.multi[subidx].end.lnum = reglnum + 1;
3036 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003037 }
3038 else
3039 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003040 sub->list.multi[subidx].end.lnum = reglnum;
3041 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003042 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003043 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003044 }
3045 else
3046 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003047 save_ptr = sub->list.line[subidx].end;
3048 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003049 }
3050
Bram Moolenaar5714b802013-05-28 22:03:20 +02003051 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003052
3053 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003054 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003055 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003056 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003057 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003058 break;
3059 }
3060}
3061
3062/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003063 * Like addstate(), but the new state(s) are put at position "*ip".
3064 * Used for zero-width matches, next state to use is the added one.
3065 * This makes sure the order of states to be tried does not change, which
3066 * matters for alternatives.
3067 */
3068 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003069addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003070 nfa_list_T *l; /* runtime state list */
3071 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003072 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003073 int *ip;
3074{
3075 int tlen = l->n;
3076 int count;
3077 int i = *ip;
3078
3079 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003080 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003081
3082 /* when "*ip" was at the end of the list, nothing to do */
3083 if (i + 1 == tlen)
3084 return;
3085
3086 /* re-order to put the new state at the current position */
3087 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003088 if (count == 1)
3089 {
3090 /* overwrite the current state */
3091 l->t[i] = l->t[l->n - 1];
3092 }
3093 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003094 {
3095 /* make space for new states, then move them from the
3096 * end to the current position */
3097 mch_memmove(&(l->t[i + count]),
3098 &(l->t[i + 1]),
3099 sizeof(nfa_thread_T) * (l->n - i - 1));
3100 mch_memmove(&(l->t[i]),
3101 &(l->t[l->n - 1]),
3102 sizeof(nfa_thread_T) * count);
3103 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003104 --l->n;
3105 *ip = i - 1;
3106}
3107
3108/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003109 * Check character class "class" against current character c.
3110 */
3111 static int
3112check_char_class(class, c)
3113 int class;
3114 int c;
3115{
3116 switch (class)
3117 {
3118 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003119 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003120 return OK;
3121 break;
3122 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003123 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003124 return OK;
3125 break;
3126 case NFA_CLASS_BLANK:
3127 if (c == ' ' || c == '\t')
3128 return OK;
3129 break;
3130 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003131 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003132 return OK;
3133 break;
3134 case NFA_CLASS_DIGIT:
3135 if (VIM_ISDIGIT(c))
3136 return OK;
3137 break;
3138 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003139 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003140 return OK;
3141 break;
3142 case NFA_CLASS_LOWER:
3143 if (MB_ISLOWER(c))
3144 return OK;
3145 break;
3146 case NFA_CLASS_PRINT:
3147 if (vim_isprintc(c))
3148 return OK;
3149 break;
3150 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003151 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003152 return OK;
3153 break;
3154 case NFA_CLASS_SPACE:
3155 if ((c >=9 && c <= 13) || (c == ' '))
3156 return OK;
3157 break;
3158 case NFA_CLASS_UPPER:
3159 if (MB_ISUPPER(c))
3160 return OK;
3161 break;
3162 case NFA_CLASS_XDIGIT:
3163 if (vim_isxdigit(c))
3164 return OK;
3165 break;
3166 case NFA_CLASS_TAB:
3167 if (c == '\t')
3168 return OK;
3169 break;
3170 case NFA_CLASS_RETURN:
3171 if (c == '\r')
3172 return OK;
3173 break;
3174 case NFA_CLASS_BACKSPACE:
3175 if (c == '\b')
3176 return OK;
3177 break;
3178 case NFA_CLASS_ESCAPE:
3179 if (c == '\033')
3180 return OK;
3181 break;
3182
3183 default:
3184 /* should not be here :P */
3185 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3186 }
3187 return FAIL;
3188}
3189
Bram Moolenaar5714b802013-05-28 22:03:20 +02003190static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3191
3192/*
3193 * Check for a match with subexpression "subidx".
3194 * return TRUE if it matches.
3195 */
3196 static int
3197match_backref(sub, subidx, bytelen)
3198 regsub_T *sub; /* pointers to subexpressions */
3199 int subidx;
3200 int *bytelen; /* out: length of match in bytes */
3201{
3202 int len;
3203
3204 if (sub->in_use <= subidx)
3205 {
3206retempty:
3207 /* backref was not set, match an empty string */
3208 *bytelen = 0;
3209 return TRUE;
3210 }
3211
3212 if (REG_MULTI)
3213 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003214 if (sub->list.multi[subidx].start.lnum < 0
3215 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003216 goto retempty;
3217 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003218 len = sub->list.multi[subidx].end.col
3219 - sub->list.multi[subidx].start.col;
3220 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003221 reginput, &len) == 0)
3222 {
3223 *bytelen = len;
3224 return TRUE;
3225 }
3226 }
3227 else
3228 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003229 if (sub->list.line[subidx].start == NULL
3230 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003231 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003232 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3233 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003234 {
3235 *bytelen = len;
3236 return TRUE;
3237 }
3238 }
3239 return FALSE;
3240}
3241
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003242/*
3243 * Set all NFA nodes' list ID equal to -1.
3244 */
3245 static void
3246nfa_set_neg_listids(start)
3247 nfa_state_T *start;
3248{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003249 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003250 {
3251 start->lastlist = -1;
3252 nfa_set_neg_listids(start->out);
3253 nfa_set_neg_listids(start->out1);
3254 }
3255}
3256
3257/*
3258 * Set all NFA nodes' list ID equal to 0.
3259 */
3260 static void
3261nfa_set_null_listids(start)
3262 nfa_state_T *start;
3263{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003264 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003265 {
3266 start->lastlist = 0;
3267 nfa_set_null_listids(start->out);
3268 nfa_set_null_listids(start->out1);
3269 }
3270}
3271
3272/*
3273 * Save list IDs for all NFA states in "list".
3274 */
3275 static void
3276nfa_save_listids(start, list)
3277 nfa_state_T *start;
3278 int *list;
3279{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003280 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 {
3282 list[abs(start->id)] = start->lastlist;
3283 start->lastlist = -1;
3284 nfa_save_listids(start->out, list);
3285 nfa_save_listids(start->out1, list);
3286 }
3287}
3288
3289/*
3290 * Restore list IDs from "list" to all NFA states.
3291 */
3292 static void
3293nfa_restore_listids(start, list)
3294 nfa_state_T *start;
3295 int *list;
3296{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003297 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 {
3299 start->lastlist = list[abs(start->id)];
3300 nfa_restore_listids(start->out, list);
3301 nfa_restore_listids(start->out1, list);
3302 }
3303}
3304
Bram Moolenaar423532e2013-05-29 21:14:42 +02003305 static int
3306nfa_re_num_cmp(val, op, pos)
3307 long_u val;
3308 int op;
3309 long_u pos;
3310{
3311 if (op == 1) return pos > val;
3312 if (op == 2) return pos < val;
3313 return val == pos;
3314}
3315
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003316static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3317
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318/*
3319 * Main matching routine.
3320 *
3321 * Run NFA to determine whether it matches reginput.
3322 *
3323 * Return TRUE if there is a match, FALSE otherwise.
3324 * Note: Caller must ensure that: start != NULL.
3325 */
3326 static int
3327nfa_regmatch(start, submatch, m)
3328 nfa_state_T *start;
3329 regsub_T *submatch;
3330 regsub_T *m;
3331{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 int result;
3333 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 int flag = 0;
3335 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003336 int go_to_nextline = FALSE;
3337 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003338 char_u *old_reginput = NULL;
3339 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003340 nfa_list_T list[3];
3341 nfa_list_T *listtbl[2][2];
3342 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003343 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003344 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003345 nfa_list_T *thislist;
3346 nfa_list_T *nextlist;
3347 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003349#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003350 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003351
3352 if (debug == NULL)
3353 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003354 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003355 return FALSE;
3356 }
3357#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003358 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003359
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003360 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003361 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003362 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3363 list[0].len = nstate + 1;
3364 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3365 list[1].len = nstate + 1;
3366 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3367 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3369 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370
3371#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003372 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003373 if (log_fd != NULL)
3374 {
3375 fprintf(log_fd, "**********************************\n");
3376 nfa_set_code(start->c);
3377 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3378 abs(start->id), code);
3379 fprintf(log_fd, "**********************************\n");
3380 }
3381 else
3382 {
3383 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3384 log_fd = stderr;
3385 }
3386#endif
3387
3388 thislist = &list[0];
3389 thislist->n = 0;
3390 nextlist = &list[1];
3391 nextlist->n = 0;
3392 neglist = &list[2];
3393 neglist->n = 0;
3394#ifdef ENABLE_LOG
3395 fprintf(log_fd, "(---) STARTSTATE\n");
3396#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003397 thislist->id = listid;
3398 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003399
3400 /* There are two cases when the NFA advances: 1. input char matches the
3401 * NFA node and 2. input char does not match the NFA node, but the next
3402 * node is NFA_NOT. The following macro calls addstate() according to
3403 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3404 listtbl[0][0] = NULL;
3405 listtbl[0][1] = neglist;
3406 listtbl[1][0] = nextlist;
3407 listtbl[1][1] = NULL;
3408#define ADD_POS_NEG_STATE(node) \
3409 ll = listtbl[result ? 1 : 0][node->negated]; \
3410 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003411 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412
3413
3414 /*
3415 * Run for each character.
3416 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003417 for (;;)
3418 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003419 int curc;
3420 int clen;
3421
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422#ifdef FEAT_MBYTE
3423 if (has_mbyte)
3424 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003425 curc = (*mb_ptr2char)(reginput);
3426 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003427 }
3428 else
3429#endif
3430 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003431 curc = *reginput;
3432 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003433 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003434 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003435 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003436 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003437 go_to_nextline = FALSE;
3438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439
3440 /* swap lists */
3441 thislist = &list[flag];
3442 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003443 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 listtbl[1][0] = nextlist;
3445 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003446 thislist->id = listid;
3447 nextlist->id = listid + 1;
3448 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449
3450#ifdef ENABLE_LOG
3451 fprintf(log_fd, "------------------------------------------\n");
3452 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003453 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003454 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003455 {
3456 int i;
3457
3458 for (i = 0; i < thislist->n; i++)
3459 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3460 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461 fprintf(log_fd, "\n");
3462#endif
3463
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003464#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 fprintf(debug, "\n-------------------\n");
3466#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003467 /*
3468 * If the state lists are empty we can stop.
3469 */
3470 if (thislist->n == 0 && neglist->n == 0)
3471 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003472
3473 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003474 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 {
3476 if (neglist->n > 0)
3477 {
3478 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003479 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003480 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003481 }
3482 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003483 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003485#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003486 nfa_set_code(t->state->c);
3487 fprintf(debug, "%s, ", code);
3488#endif
3489#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003490 {
3491 int col;
3492
3493 if (t->sub.in_use <= 0)
3494 col = -1;
3495 else if (REG_MULTI)
3496 col = t->sub.list.multi[0].start.col;
3497 else
3498 col = (int)(t->sub.list.line[0].start - regline);
3499 nfa_set_code(t->state->c);
3500 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3501 abs(t->state->id), (int)t->state->c, code, col);
3502 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503#endif
3504
3505 /*
3506 * Handle the possible codes of the current state.
3507 * The most important is NFA_MATCH.
3508 */
3509 switch (t->state->c)
3510 {
3511 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003512 {
3513 int j;
3514
Bram Moolenaar963fee22013-05-26 21:47:28 +02003515 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003516 submatch->in_use = t->sub.in_use;
3517 if (REG_MULTI)
3518 for (j = 0; j < submatch->in_use; j++)
3519 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003520 submatch->list.multi[j].start =
3521 t->sub.list.multi[j].start;
3522 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003523 }
3524 else
3525 for (j = 0; j < submatch->in_use; j++)
3526 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003527 submatch->list.line[j].start =
3528 t->sub.list.line[j].start;
3529 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003530 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003531#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003532 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003533#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003534 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003535 * states at this position. When the list of states is going
3536 * to be empty quit without advancing, so that "reginput" is
3537 * correct. */
3538 if (nextlist->n == 0 && neglist->n == 0)
3539 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003540 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003541 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542
3543 case NFA_END_INVISIBLE:
3544 /* This is only encountered after a NFA_START_INVISIBLE node.
3545 * They surround a zero-width group, used with "\@=" and "\&".
3546 * If we got here, it means that the current "invisible" group
3547 * finished successfully, so return control to the parent
3548 * nfa_regmatch(). Submatches are stored in *m, and used in
3549 * the parent call. */
3550 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003551 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003552 else
3553 {
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003554 /* do not set submatches for \@! */
3555 if (!t->state->negated)
3556 /* TODO: only copy positions in use. */
3557 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003558 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003559 }
3560 break;
3561
3562 case NFA_START_INVISIBLE:
3563 /* Save global variables, and call nfa_regmatch() to check if
3564 * the current concat matches at this position. The concat
3565 * ends with the node NFA_END_INVISIBLE */
3566 old_reginput = reginput;
3567 old_regline = regline;
3568 old_reglnum = reglnum;
3569 if (listids == NULL)
3570 {
3571 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3572 if (listids == NULL)
3573 {
3574 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3575 return 0;
3576 }
3577 }
3578#ifdef ENABLE_LOG
3579 if (log_fd != stderr)
3580 fclose(log_fd);
3581 log_fd = NULL;
3582#endif
3583 /* Have to clear the listid field of the NFA nodes, so that
3584 * nfa_regmatch() and addstate() can run properly after
3585 * recursion. */
3586 nfa_save_listids(start, listids);
3587 nfa_set_null_listids(start);
3588 result = nfa_regmatch(t->state->out, submatch, m);
3589 nfa_set_neg_listids(start);
3590 nfa_restore_listids(start, listids);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003591 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592
3593#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003594 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595 if (log_fd != NULL)
3596 {
3597 fprintf(log_fd, "****************************\n");
3598 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3599 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3600 fprintf(log_fd, "****************************\n");
3601 }
3602 else
3603 {
3604 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3605 log_fd = stderr;
3606 }
3607#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003608 /* for \@! it is a match when result is FALSE */
3609 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003611 int j;
3612
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613 /* Restore position in input text */
3614 reginput = old_reginput;
3615 regline = old_regline;
3616 reglnum = old_reglnum;
3617 /* Copy submatch info from the recursive call */
3618 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003619 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003620 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003621 t->sub.list.multi[j].start = m->list.multi[j].start;
3622 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 }
3624 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003625 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003626 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003627 t->sub.list.line[j].start = m->list.line[j].start;
3628 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003629 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003630 if (m->in_use > t->sub.in_use)
3631 t->sub.in_use = m->in_use;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003632
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003633 /* t->state->out1 is the corresponding END_INVISIBLE node;
3634 * Add it to the current list (zero-width match). */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003635 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003636 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637 }
3638 else
3639 {
3640 /* continue with next input char */
3641 reginput = old_reginput;
3642 }
3643 break;
3644
3645 case NFA_BOL:
3646 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003647 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003648 break;
3649
3650 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003651 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003652 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003653 break;
3654
3655 case NFA_BOW:
3656 {
3657 int bow = TRUE;
3658
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003659 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660 bow = FALSE;
3661#ifdef FEAT_MBYTE
3662 else if (has_mbyte)
3663 {
3664 int this_class;
3665
3666 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003667 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003668 if (this_class <= 1)
3669 bow = FALSE;
3670 else if (reg_prev_class() == this_class)
3671 bow = FALSE;
3672 }
3673#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003674 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003675 || (reginput > regline
3676 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003677 bow = FALSE;
3678 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003679 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003680 break;
3681 }
3682
3683 case NFA_EOW:
3684 {
3685 int eow = TRUE;
3686
3687 if (reginput == regline)
3688 eow = FALSE;
3689#ifdef FEAT_MBYTE
3690 else if (has_mbyte)
3691 {
3692 int this_class, prev_class;
3693
3694 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003695 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696 prev_class = reg_prev_class();
3697 if (this_class == prev_class
3698 || prev_class == 0 || prev_class == 1)
3699 eow = FALSE;
3700 }
3701#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003702 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003703 || (reginput[0] != NUL
3704 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 eow = FALSE;
3706 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003707 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003708 break;
3709 }
3710
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003711#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003713 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003714 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003715 int len = 0;
3716 nfa_state_T *end;
3717 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003718 int cchars[MAX_MCO];
3719 int ccount = 0;
3720 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003721
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003723 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003724 if (utf_iscomposing(sta->c))
3725 {
3726 /* Only match composing character(s), ignore base
3727 * character. Used for ".{composing}" and "{composing}"
3728 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003729 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003730 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003731 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003732 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003733 /* If \Z was present, then ignore composing characters.
3734 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003735 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003736 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003737 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003738 else
3739 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003740 while (sta->c != NFA_END_COMPOSING)
3741 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003742 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003743
3744 /* Check base character matches first, unless ignored. */
3745 else if (len > 0 || mc == sta->c)
3746 {
3747 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003748 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003749 len += mb_char2len(mc);
3750 sta = sta->out;
3751 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003753 /* We don't care about the order of composing characters.
3754 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003755 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003756 {
3757 mc = mb_ptr2char(reginput + len);
3758 cchars[ccount++] = mc;
3759 len += mb_char2len(mc);
3760 if (ccount == MAX_MCO)
3761 break;
3762 }
3763
3764 /* Check that each composing char in the pattern matches a
3765 * composing char in the text. We do not check if all
3766 * composing chars are matched. */
3767 result = OK;
3768 while (sta->c != NFA_END_COMPOSING)
3769 {
3770 for (j = 0; j < ccount; ++j)
3771 if (cchars[j] == sta->c)
3772 break;
3773 if (j == ccount)
3774 {
3775 result = FAIL;
3776 break;
3777 }
3778 sta = sta->out;
3779 }
3780 }
3781 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003782 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003783
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003784 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003785 ADD_POS_NEG_STATE(end);
3786 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003787 }
3788#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003789
3790 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003791 if (curc == NUL && !reg_line_lbr && REG_MULTI
3792 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003793 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003794 go_to_nextline = TRUE;
3795 /* Pass -1 for the offset, which means taking the position
3796 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003797 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003798 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003799 else if (curc == '\n' && reg_line_lbr)
3800 {
3801 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003802 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003803 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003804 break;
3805
3806 case NFA_CLASS_ALNUM:
3807 case NFA_CLASS_ALPHA:
3808 case NFA_CLASS_BLANK:
3809 case NFA_CLASS_CNTRL:
3810 case NFA_CLASS_DIGIT:
3811 case NFA_CLASS_GRAPH:
3812 case NFA_CLASS_LOWER:
3813 case NFA_CLASS_PRINT:
3814 case NFA_CLASS_PUNCT:
3815 case NFA_CLASS_SPACE:
3816 case NFA_CLASS_UPPER:
3817 case NFA_CLASS_XDIGIT:
3818 case NFA_CLASS_TAB:
3819 case NFA_CLASS_RETURN:
3820 case NFA_CLASS_BACKSPACE:
3821 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003822 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003823 ADD_POS_NEG_STATE(t->state);
3824 break;
3825
3826 case NFA_END_NEG_RANGE:
3827 /* This follows a series of negated nodes, like:
3828 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003829 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003830 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831 break;
3832
3833 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003834 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003835 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003836 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003837 break;
3838
3839 /*
3840 * Character classes like \a for alpha, \d for digit etc.
3841 */
3842 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003843 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003844 ADD_POS_NEG_STATE(t->state);
3845 break;
3846
3847 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003848 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849 ADD_POS_NEG_STATE(t->state);
3850 break;
3851
3852 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003853 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 ADD_POS_NEG_STATE(t->state);
3855 break;
3856
3857 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003858 result = !VIM_ISDIGIT(curc)
3859 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003860 ADD_POS_NEG_STATE(t->state);
3861 break;
3862
3863 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003864 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003865 ADD_POS_NEG_STATE(t->state);
3866 break;
3867
3868 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003869 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003870 ADD_POS_NEG_STATE(t->state);
3871 break;
3872
3873 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003874 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875 ADD_POS_NEG_STATE(t->state);
3876 break;
3877
3878 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003879 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003880 ADD_POS_NEG_STATE(t->state);
3881 break;
3882
3883 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003884 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003885 ADD_POS_NEG_STATE(t->state);
3886 break;
3887
3888 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003889 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003890 ADD_POS_NEG_STATE(t->state);
3891 break;
3892
3893 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003894 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003895 ADD_POS_NEG_STATE(t->state);
3896 break;
3897
3898 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003899 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900 ADD_POS_NEG_STATE(t->state);
3901 break;
3902
3903 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003904 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003905 ADD_POS_NEG_STATE(t->state);
3906 break;
3907
3908 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003909 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910 ADD_POS_NEG_STATE(t->state);
3911 break;
3912
3913 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003914 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003915 ADD_POS_NEG_STATE(t->state);
3916 break;
3917
3918 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003919 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003920 ADD_POS_NEG_STATE(t->state);
3921 break;
3922
3923 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003924 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003925 ADD_POS_NEG_STATE(t->state);
3926 break;
3927
3928 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003929 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003930 ADD_POS_NEG_STATE(t->state);
3931 break;
3932
3933 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003934 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003935 ADD_POS_NEG_STATE(t->state);
3936 break;
3937
3938 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003939 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003940 ADD_POS_NEG_STATE(t->state);
3941 break;
3942
3943 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003944 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003945 ADD_POS_NEG_STATE(t->state);
3946 break;
3947
3948 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003949 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003950 ADD_POS_NEG_STATE(t->state);
3951 break;
3952
3953 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003954 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003955 ADD_POS_NEG_STATE(t->state);
3956 break;
3957
3958 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003959 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003960 ADD_POS_NEG_STATE(t->state);
3961 break;
3962
3963 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003964 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965 ADD_POS_NEG_STATE(t->state);
3966 break;
3967
3968 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003969 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003970 ADD_POS_NEG_STATE(t->state);
3971 break;
3972
Bram Moolenaar5714b802013-05-28 22:03:20 +02003973 case NFA_BACKREF1:
3974 case NFA_BACKREF2:
3975 case NFA_BACKREF3:
3976 case NFA_BACKREF4:
3977 case NFA_BACKREF5:
3978 case NFA_BACKREF6:
3979 case NFA_BACKREF7:
3980 case NFA_BACKREF8:
3981 case NFA_BACKREF9:
3982 /* \1 .. \9 */
3983 {
3984 int subidx = t->state->c - NFA_BACKREF1 + 1;
3985 int bytelen;
3986
3987 result = match_backref(&t->sub, subidx, &bytelen);
3988 if (result)
3989 {
3990 if (bytelen == 0)
3991 {
3992 /* empty match always works, add NFA_SKIP with zero to
3993 * be used next */
3994 addstate_here(thislist, t->state->out, &t->sub,
3995 &listidx);
3996 thislist->t[listidx + 1].count = 0;
3997 }
3998 else if (bytelen <= clen)
3999 {
4000 /* match current character, jump ahead to out of
4001 * NFA_SKIP */
4002 addstate(nextlist, t->state->out->out, &t->sub, clen);
4003#ifdef ENABLE_LOG
4004 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4005#endif
4006 }
4007 else
4008 {
4009 /* skip ofer the matched characters, set character
4010 * count in NFA_SKIP */
4011 addstate(nextlist, t->state->out, &t->sub, bytelen);
4012 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4013#ifdef ENABLE_LOG
4014 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4015#endif
4016 }
4017
4018 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004019 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004020 }
4021 case NFA_SKIP:
4022 /* charater of previous matching \1 .. \9 */
4023 if (t->count - clen <= 0)
4024 {
4025 /* end of match, go to what follows */
4026 addstate(nextlist, t->state->out, &t->sub, clen);
4027#ifdef ENABLE_LOG
4028 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4029#endif
4030 }
4031 else
4032 {
4033 /* add state again with decremented count */
4034 addstate(nextlist, t->state, &t->sub, 0);
4035 nextlist->t[nextlist->n - 1].count = t->count - clen;
4036#ifdef ENABLE_LOG
4037 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4038#endif
4039 }
4040 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004041
4042 case NFA_SKIP_CHAR:
4043 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004044 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004045 /* TODO: should not happen? */
4046 break;
4047
Bram Moolenaar423532e2013-05-29 21:14:42 +02004048 case NFA_LNUM:
4049 case NFA_LNUM_GT:
4050 case NFA_LNUM_LT:
4051 result = (REG_MULTI &&
4052 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4053 (long_u)(reglnum + reg_firstlnum)));
4054 if (result)
4055 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4056 break;
4057
4058 case NFA_COL:
4059 case NFA_COL_GT:
4060 case NFA_COL_LT:
4061 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4062 (long_u)(reginput - regline) + 1);
4063 if (result)
4064 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4065 break;
4066
4067 case NFA_VCOL:
4068 case NFA_VCOL_GT:
4069 case NFA_VCOL_LT:
4070 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4071 (long_u)win_linetabsize(
4072 reg_win == NULL ? curwin : reg_win,
4073 regline, (colnr_T)(reginput - regline)) + 1);
4074 if (result)
4075 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4076 break;
4077
4078 case NFA_CURSOR:
4079 result = (reg_win != NULL
4080 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4081 && ((colnr_T)(reginput - regline)
4082 == reg_win->w_cursor.col));
4083 if (result)
4084 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4085 break;
4086
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004087 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004088 {
4089 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004090
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004091 /* TODO: put this in #ifdef later */
4092 if (c < -256)
4093 EMSGN("INTERNAL: Negative state char: %ld", c);
4094 if (is_Magic(c))
4095 c = un_Magic(c);
4096 result = (c == curc);
4097
4098 if (!result && ireg_ic)
4099 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004100#ifdef FEAT_MBYTE
4101 /* If there is a composing character which is not being
4102 * ignored there can be no match. Match with composing
4103 * character uses NFA_COMPOSING above. */
4104 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004105 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004106 result = FALSE;
4107#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004108 ADD_POS_NEG_STATE(t->state);
4109 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004110 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004111 }
4112
4113 } /* for (thislist = thislist; thislist->state; thislist++) */
4114
Bram Moolenaare23febd2013-05-26 18:40:14 +02004115 /* Look for the start of a match in the current position by adding the
4116 * start state to the list of states.
4117 * The first found match is the leftmost one, thus the order of states
4118 * matters!
4119 * Do not add the start state in recursive calls of nfa_regmatch(),
4120 * because recursive calls should only start in the first position.
4121 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004122 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004123 && reglnum == 0 && clen != 0
4124 && (ireg_maxcol == 0
4125 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004126 {
4127#ifdef ENABLE_LOG
4128 fprintf(log_fd, "(---) STARTSTATE\n");
4129#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004130 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131 }
4132
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004133#ifdef ENABLE_LOG
4134 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004135 {
4136 int i;
4137
4138 for (i = 0; i < thislist->n; i++)
4139 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4140 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141 fprintf(log_fd, "\n");
4142#endif
4143
4144nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004145 /* Advance to the next character, or advance to the next line, or
4146 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004147 if (clen != 0)
4148 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004149 else if (go_to_nextline)
4150 reg_nextline();
4151 else
4152 break;
4153 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004154
4155#ifdef ENABLE_LOG
4156 if (log_fd != stderr)
4157 fclose(log_fd);
4158 log_fd = NULL;
4159#endif
4160
4161theend:
4162 /* Free memory */
4163 vim_free(list[0].t);
4164 vim_free(list[1].t);
4165 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004166 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004167#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004168#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004169 fclose(debug);
4170#endif
4171
Bram Moolenaar963fee22013-05-26 21:47:28 +02004172 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004173}
4174
4175/*
4176 * Try match of "prog" with at regline["col"].
4177 * Returns 0 for failure, number of lines contained in the match otherwise.
4178 */
4179 static long
4180nfa_regtry(start, col)
4181 nfa_state_T *start;
4182 colnr_T col;
4183{
4184 int i;
4185 regsub_T sub, m;
4186#ifdef ENABLE_LOG
4187 FILE *f;
4188#endif
4189
4190 reginput = regline + col;
4191 need_clear_subexpr = TRUE;
4192
4193#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004194 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004195 if (f != NULL)
4196 {
4197 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4198 fprintf(f, " =======================================================\n");
4199#ifdef DEBUG
4200 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4201#endif
4202 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004203 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004204 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004205 fprintf(f, "\n\n");
4206 fclose(f);
4207 }
4208 else
4209 EMSG(_("Could not open temporary log file for writing "));
4210#endif
4211
4212 if (REG_MULTI)
4213 {
4214 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004215 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4216 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004217 }
4218 else
4219 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004220 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4221 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004222 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004223 sub.in_use = 0;
4224 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225
4226 if (nfa_regmatch(start, &sub, &m) == FALSE)
4227 return 0;
4228
4229 cleanup_subexpr();
4230 if (REG_MULTI)
4231 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004232 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004233 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004234 reg_startpos[i] = sub.list.multi[i].start;
4235 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004236 }
4237
4238 if (reg_startpos[0].lnum < 0)
4239 {
4240 reg_startpos[0].lnum = 0;
4241 reg_startpos[0].col = col;
4242 }
4243 if (reg_endpos[0].lnum < 0)
4244 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004245 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004246 reg_endpos[0].lnum = reglnum;
4247 reg_endpos[0].col = (int)(reginput - regline);
4248 }
4249 else
4250 /* Use line number of "\ze". */
4251 reglnum = reg_endpos[0].lnum;
4252 }
4253 else
4254 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004255 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004257 reg_startp[i] = sub.list.line[i].start;
4258 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004259 }
4260
4261 if (reg_startp[0] == NULL)
4262 reg_startp[0] = regline + col;
4263 if (reg_endp[0] == NULL)
4264 reg_endp[0] = reginput;
4265 }
4266
4267 return 1 + reglnum;
4268}
4269
4270/*
4271 * Match a regexp against a string ("line" points to the string) or multiple
4272 * lines ("line" is NULL, use reg_getline()).
4273 *
4274 * Returns 0 for failure, number of lines contained in the match otherwise.
4275 */
4276 static long
4277nfa_regexec_both(line, col)
4278 char_u *line;
4279 colnr_T col; /* column to start looking for match */
4280{
4281 nfa_regprog_T *prog;
4282 long retval = 0L;
4283 int i;
4284
4285 if (REG_MULTI)
4286 {
4287 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4288 line = reg_getline((linenr_T)0); /* relative to the cursor */
4289 reg_startpos = reg_mmatch->startpos;
4290 reg_endpos = reg_mmatch->endpos;
4291 }
4292 else
4293 {
4294 prog = (nfa_regprog_T *)reg_match->regprog;
4295 reg_startp = reg_match->startp;
4296 reg_endp = reg_match->endp;
4297 }
4298
4299 /* Be paranoid... */
4300 if (prog == NULL || line == NULL)
4301 {
4302 EMSG(_(e_null));
4303 goto theend;
4304 }
4305
4306 /* If the start column is past the maximum column: no need to try. */
4307 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4308 goto theend;
4309
4310 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4311 if (prog->regflags & RF_ICASE)
4312 ireg_ic = TRUE;
4313 else if (prog->regflags & RF_NOICASE)
4314 ireg_ic = FALSE;
4315
4316#ifdef FEAT_MBYTE
4317 /* If pattern contains "\Z" overrule value of ireg_icombine */
4318 if (prog->regflags & RF_ICOMBINE)
4319 ireg_icombine = TRUE;
4320#endif
4321
4322 regline = line;
4323 reglnum = 0; /* relative to line */
4324
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004325 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004326 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004327 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004328
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004329 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330 for (i = 0; i < nstate; ++i)
4331 {
4332 prog->state[i].id = i;
4333 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004334 }
4335
4336 retval = nfa_regtry(prog->start, col);
4337
4338theend:
4339 return retval;
4340}
4341
4342/*
4343 * Compile a regular expression into internal code for the NFA matcher.
4344 * Returns the program in allocated space. Returns NULL for an error.
4345 */
4346 static regprog_T *
4347nfa_regcomp(expr, re_flags)
4348 char_u *expr;
4349 int re_flags;
4350{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004351 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004352 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004353 int *postfix;
4354
4355 if (expr == NULL)
4356 return NULL;
4357
4358#ifdef DEBUG
4359 nfa_regengine.expr = expr;
4360#endif
4361
4362 init_class_tab();
4363
4364 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4365 return NULL;
4366
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004368 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004369 postfix = re2post();
4370 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004371 {
4372 /* TODO: only give this error for debugging? */
4373 if (post_ptr >= post_end)
4374 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004375 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004376 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004377
4378 /*
4379 * In order to build the NFA, we parse the input regexp twice:
4380 * 1. first pass to count size (so we can allocate space)
4381 * 2. second to emit code
4382 */
4383#ifdef ENABLE_LOG
4384 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004385 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004386
4387 if (f != NULL)
4388 {
4389 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4390 fclose(f);
4391 }
4392 }
4393#endif
4394
4395 /*
4396 * PASS 1
4397 * Count number of NFA states in "nstate". Do not build the NFA.
4398 */
4399 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004400
4401 /* Space for compiled regexp */
4402 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4403 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4404 if (prog == NULL)
4405 goto fail;
4406 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004407 state_ptr = prog->state;
4408
4409 /*
4410 * PASS 2
4411 * Build the NFA
4412 */
4413 prog->start = post2nfa(postfix, post_ptr, FALSE);
4414 if (prog->start == NULL)
4415 goto fail;
4416
4417 prog->regflags = regflags;
4418 prog->engine = &nfa_regengine;
4419 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004420 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004421 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004422 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004423#ifdef ENABLE_LOG
4424 nfa_postfix_dump(expr, OK);
4425 nfa_dump(prog);
4426#endif
4427
4428out:
4429 vim_free(post_start);
4430 post_start = post_ptr = post_end = NULL;
4431 state_ptr = NULL;
4432 return (regprog_T *)prog;
4433
4434fail:
4435 vim_free(prog);
4436 prog = NULL;
4437#ifdef ENABLE_LOG
4438 nfa_postfix_dump(expr, FAIL);
4439#endif
4440#ifdef DEBUG
4441 nfa_regengine.expr = NULL;
4442#endif
4443 goto out;
4444}
4445
4446
4447/*
4448 * Match a regexp against a string.
4449 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4450 * Uses curbuf for line count and 'iskeyword'.
4451 *
4452 * Return TRUE if there is a match, FALSE if not.
4453 */
4454 static int
4455nfa_regexec(rmp, line, col)
4456 regmatch_T *rmp;
4457 char_u *line; /* string to match against */
4458 colnr_T col; /* column to start looking for match */
4459{
4460 reg_match = rmp;
4461 reg_mmatch = NULL;
4462 reg_maxline = 0;
4463 reg_line_lbr = FALSE;
4464 reg_buf = curbuf;
4465 reg_win = NULL;
4466 ireg_ic = rmp->rm_ic;
4467#ifdef FEAT_MBYTE
4468 ireg_icombine = FALSE;
4469#endif
4470 ireg_maxcol = 0;
4471 return (nfa_regexec_both(line, col) != 0);
4472}
4473
4474#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4475 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4476
4477static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4478
4479/*
4480 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4481 */
4482 static int
4483nfa_regexec_nl(rmp, line, col)
4484 regmatch_T *rmp;
4485 char_u *line; /* string to match against */
4486 colnr_T col; /* column to start looking for match */
4487{
4488 reg_match = rmp;
4489 reg_mmatch = NULL;
4490 reg_maxline = 0;
4491 reg_line_lbr = TRUE;
4492 reg_buf = curbuf;
4493 reg_win = NULL;
4494 ireg_ic = rmp->rm_ic;
4495#ifdef FEAT_MBYTE
4496 ireg_icombine = FALSE;
4497#endif
4498 ireg_maxcol = 0;
4499 return (nfa_regexec_both(line, col) != 0);
4500}
4501#endif
4502
4503
4504/*
4505 * Match a regexp against multiple lines.
4506 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4507 * Uses curbuf for line count and 'iskeyword'.
4508 *
4509 * Return zero if there is no match. Return number of lines contained in the
4510 * match otherwise.
4511 *
4512 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4513 *
4514 * ! Also NOTE : match may actually be in another line. e.g.:
4515 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4516 *
4517 * +-------------------------+
4518 * |a |
4519 * |b |
4520 * |c |
4521 * | |
4522 * +-------------------------+
4523 *
4524 * then nfa_regexec_multi() returns 3. while the original
4525 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4526 *
4527 * FIXME if this behavior is not compatible.
4528 */
4529 static long
4530nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4531 regmmatch_T *rmp;
4532 win_T *win; /* window in which to search or NULL */
4533 buf_T *buf; /* buffer in which to search */
4534 linenr_T lnum; /* nr of line to start looking for match */
4535 colnr_T col; /* column to start looking for match */
4536 proftime_T *tm UNUSED; /* timeout limit or NULL */
4537{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004538 reg_match = NULL;
4539 reg_mmatch = rmp;
4540 reg_buf = buf;
4541 reg_win = win;
4542 reg_firstlnum = lnum;
4543 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4544 reg_line_lbr = FALSE;
4545 ireg_ic = rmp->rmm_ic;
4546#ifdef FEAT_MBYTE
4547 ireg_icombine = FALSE;
4548#endif
4549 ireg_maxcol = rmp->rmm_maxcol;
4550
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004551 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004552}
4553
4554#ifdef DEBUG
4555# undef ENABLE_LOG
4556#endif