blob: 93100087e9042c498e027d460f4cf15489a98d9f [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
76 NFA_MOPEN,
77 NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
78
79 /* NFA_FIRST_NL */
80 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
81 NFA_ANYOF, /* Match any character in this string. */
82 NFA_ANYBUT, /* Match any character not in this string. */
83 NFA_IDENT, /* Match identifier char */
84 NFA_SIDENT, /* Match identifier char but no digit */
85 NFA_KWORD, /* Match keyword char */
86 NFA_SKWORD, /* Match word char but no digit */
87 NFA_FNAME, /* Match file name char */
88 NFA_SFNAME, /* Match file name char but no digit */
89 NFA_PRINT, /* Match printable char */
90 NFA_SPRINT, /* Match printable char but no digit */
91 NFA_WHITE, /* Match whitespace char */
92 NFA_NWHITE, /* Match non-whitespace char */
93 NFA_DIGIT, /* Match digit char */
94 NFA_NDIGIT, /* Match non-digit char */
95 NFA_HEX, /* Match hex char */
96 NFA_NHEX, /* Match non-hex char */
97 NFA_OCTAL, /* Match octal char */
98 NFA_NOCTAL, /* Match non-octal char */
99 NFA_WORD, /* Match word char */
100 NFA_NWORD, /* Match non-word char */
101 NFA_HEAD, /* Match head char */
102 NFA_NHEAD, /* Match non-head char */
103 NFA_ALPHA, /* Match alpha char */
104 NFA_NALPHA, /* Match non-alpha char */
105 NFA_LOWER, /* Match lowercase char */
106 NFA_NLOWER, /* Match non-lowercase char */
107 NFA_UPPER, /* Match uppercase char */
108 NFA_NUPPER, /* Match non-uppercase char */
109 NFA_FIRST_NL = NFA_ANY + ADD_NL,
110 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
111
112 /* Character classes [:alnum:] etc */
113 NFA_CLASS_ALNUM,
114 NFA_CLASS_ALPHA,
115 NFA_CLASS_BLANK,
116 NFA_CLASS_CNTRL,
117 NFA_CLASS_DIGIT,
118 NFA_CLASS_GRAPH,
119 NFA_CLASS_LOWER,
120 NFA_CLASS_PRINT,
121 NFA_CLASS_PUNCT,
122 NFA_CLASS_SPACE,
123 NFA_CLASS_UPPER,
124 NFA_CLASS_XDIGIT,
125 NFA_CLASS_TAB,
126 NFA_CLASS_RETURN,
127 NFA_CLASS_BACKSPACE,
128 NFA_CLASS_ESCAPE
129};
130
131/* Keep in sync with classchars. */
132static int nfa_classcodes[] = {
133 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
134 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
135 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
136 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
137 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
138 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
139 NFA_UPPER, NFA_NUPPER
140};
141
142static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
143
144/*
145 * NFA errors can be of 3 types:
146 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
147 * silently and revert the to backtracking engine.
148 * syntax_error = FALSE;
149 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
150 * The NFA engine displays an error message, and nothing else happens.
151 * syntax_error = TRUE
152 * *** Unsupported features, when the input regexp uses an operator that is not
153 * implemented in the NFA. The NFA engine fails silently, and reverts to the
154 * old backtracking engine.
155 * syntax_error = FALSE
156 * "The NFA fails" means that "compiling the regexp with the NFA fails":
157 * nfa_regcomp() returns FAIL.
158 */
159static int syntax_error = FALSE;
160
161/* NFA regexp \ze operator encountered. */
162static int nfa_has_zend = FALSE;
163
164static int *post_start; /* holds the postfix form of r.e. */
165static int *post_end;
166static int *post_ptr;
167
168static int nstate; /* Number of states in the NFA. */
169static int istate; /* Index in the state vector, used in new_state() */
170static int nstate_max; /* Upper bound of estimated number of states. */
171
172
173static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
174static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
175static int nfa_emit_equi_class __ARGS((int c, int neg));
176static void nfa_inc __ARGS((char_u **p));
177static void nfa_dec __ARGS((char_u **p));
178static int nfa_regatom __ARGS((void));
179static int nfa_regpiece __ARGS((void));
180static int nfa_regconcat __ARGS((void));
181static int nfa_regbranch __ARGS((void));
182static int nfa_reg __ARGS((int paren));
183#ifdef DEBUG
184static void nfa_set_code __ARGS((int c));
185static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200186static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
187static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200188static void nfa_dump __ARGS((nfa_regprog_T *prog));
189#endif
190static int *re2post __ARGS((void));
191static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
192static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
193static int check_char_class __ARGS((int class, int c));
194static void st_error __ARGS((int *postfix, int *end, int *p));
195static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
196static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
197static void nfa_set_null_listids __ARGS((nfa_state_T *start));
198static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
199static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
200static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
201static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
202static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
203static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
204
205/* helper functions used when doing re2post() ... regatom() parsing */
206#define EMIT(c) do { \
207 if (post_ptr >= post_end) \
208 return FAIL; \
209 *post_ptr++ = c; \
210 } while (0)
211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200212/*
213 * Initialize internal variables before NFA compilation.
214 * Return OK on success, FAIL otherwise.
215 */
216 static int
217nfa_regcomp_start(expr, re_flags)
218 char_u *expr;
219 int re_flags; /* see vim_regcomp() */
220{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200221 size_t postfix_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200222
223 nstate = 0;
224 istate = 0;
225 /* A reasonable estimation for size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200226 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200227
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200228 /* Some items blow up in size, such as [A-z]. Add more space for that.
229 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200230 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200231
232 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200234
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200235 post_start = (int *)lalloc(postfix_size, TRUE);
236 if (post_start == NULL)
237 return FAIL;
238 vim_memset(post_start, 0, postfix_size);
239 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200240 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200241 nfa_has_zend = FALSE;
242
243 regcomp_start(expr, re_flags);
244
245 return OK;
246}
247
248/*
249 * Search between "start" and "end" and try to recognize a
250 * character class in expanded form. For example [0-9].
251 * On success, return the id the character class to be emitted.
252 * On failure, return 0 (=FAIL)
253 * Start points to the first char of the range, while end should point
254 * to the closing brace.
255 */
256 static int
257nfa_recognize_char_class(start, end, extra_newl)
258 char_u *start;
259 char_u *end;
260 int extra_newl;
261{
262 int i;
263 /* Each of these variables takes up a char in "config[]",
264 * in the order they are here. */
265 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
266 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
267 char_u *p;
268#define NCONFIGS 16
269 int classid[NCONFIGS] = {
270 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
271 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
272 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
273 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
274 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200275 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276 char_u config[NCONFIGS][9] = {
277 "000000100", /* digit */
278 "100000100", /* non digit */
279 "011000100", /* hex-digit */
280 "111000100", /* non hex-digit */
281 "000001000", /* octal-digit */
282 "100001000", /* [^0-7] */
283 "000110110", /* [0-9A-Za-z_] */
284 "100110110", /* [^0-9A-Za-z_] */
285 "000110010", /* head of word */
286 "100110010", /* not head of word */
287 "000110000", /* alphabetic char a-z */
288 "100110000", /* non alphabetic char */
289 "000100000", /* lowercase letter */
290 "100100000", /* non lowercase */
291 "000010000", /* uppercase */
292 "100010000" /* non uppercase */
293 };
294
295 if (extra_newl == TRUE)
296 newl = TRUE;
297
298 if (*end != ']')
299 return FAIL;
300 p = start;
301 if (*p == '^')
302 {
303 not = TRUE;
304 p ++;
305 }
306
307 while (p < end)
308 {
309 if (p + 2 < end && *(p + 1) == '-')
310 {
311 switch (*p)
312 {
313 case '0':
314 if (*(p + 2) == '9')
315 {
316 o9 = TRUE;
317 break;
318 }
319 else
320 if (*(p + 2) == '7')
321 {
322 o7 = TRUE;
323 break;
324 }
325 case 'a':
326 if (*(p + 2) == 'z')
327 {
328 az = TRUE;
329 break;
330 }
331 else
332 if (*(p + 2) == 'f')
333 {
334 af = TRUE;
335 break;
336 }
337 case 'A':
338 if (*(p + 2) == 'Z')
339 {
340 AZ = TRUE;
341 break;
342 }
343 else
344 if (*(p + 2) == 'F')
345 {
346 AF = TRUE;
347 break;
348 }
349 /* FALLTHROUGH */
350 default:
351 return FAIL;
352 }
353 p += 3;
354 }
355 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
356 {
357 newl = TRUE;
358 p += 2;
359 }
360 else if (*p == '_')
361 {
362 underscore = TRUE;
363 p ++;
364 }
365 else if (*p == '\n')
366 {
367 newl = TRUE;
368 p ++;
369 }
370 else
371 return FAIL;
372 } /* while (p < end) */
373
374 if (p != end)
375 return FAIL;
376
377 /* build the config that represents the ranges we gathered */
378 STRCPY(myconfig, "000000000");
379 if (not == TRUE)
380 myconfig[0] = '1';
381 if (af == TRUE)
382 myconfig[1] = '1';
383 if (AF == TRUE)
384 myconfig[2] = '1';
385 if (az == TRUE)
386 myconfig[3] = '1';
387 if (AZ == TRUE)
388 myconfig[4] = '1';
389 if (o7 == TRUE)
390 myconfig[5] = '1';
391 if (o9 == TRUE)
392 myconfig[6] = '1';
393 if (underscore == TRUE)
394 myconfig[7] = '1';
395 if (newl == TRUE)
396 {
397 myconfig[8] = '1';
398 extra_newl = ADD_NL;
399 }
400 /* try to recognize character classes */
401 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200402 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200403 return classid[i] + extra_newl;
404
405 /* fallthrough => no success so far */
406 return FAIL;
407
408#undef NCONFIGS
409}
410
411/*
412 * Produce the bytes for equivalence class "c".
413 * Currently only handles latin1, latin9 and utf-8.
414 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
415 * equivalent to 'a OR b OR c'
416 *
417 * NOTE! When changing this function, also update reg_equi_class()
418 */
419 static int
420nfa_emit_equi_class(c, neg)
421 int c;
422 int neg;
423{
424 int first = TRUE;
425 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
426#define EMIT2(c) \
427 EMIT(c); \
428 if (neg == TRUE) { \
429 EMIT(NFA_NOT); \
430 } \
431 if (first == FALSE) \
432 EMIT(glue); \
433 else \
434 first = FALSE; \
435
436#ifdef FEAT_MBYTE
437 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
438 || STRCMP(p_enc, "iso-8859-15") == 0)
439#endif
440 {
441 switch (c)
442 {
443 case 'A': case '\300': case '\301': case '\302':
444 case '\303': case '\304': case '\305':
445 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
446 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
447 EMIT2('\305');
448 return OK;
449
450 case 'C': case '\307':
451 EMIT2('C'); EMIT2('\307');
452 return OK;
453
454 case 'E': case '\310': case '\311': case '\312': case '\313':
455 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
456 EMIT2('\312'); EMIT2('\313');
457 return OK;
458
459 case 'I': case '\314': case '\315': case '\316': case '\317':
460 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
461 EMIT2('\316'); EMIT2('\317');
462 return OK;
463
464 case 'N': case '\321':
465 EMIT2('N'); EMIT2('\321');
466 return OK;
467
468 case 'O': case '\322': case '\323': case '\324': case '\325':
469 case '\326':
470 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
471 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
472 return OK;
473
474 case 'U': case '\331': case '\332': case '\333': case '\334':
475 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
476 EMIT2('\333'); EMIT2('\334');
477 return OK;
478
479 case 'Y': case '\335':
480 EMIT2('Y'); EMIT2('\335');
481 return OK;
482
483 case 'a': case '\340': case '\341': case '\342':
484 case '\343': case '\344': case '\345':
485 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
486 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
487 EMIT2('\345');
488 return OK;
489
490 case 'c': case '\347':
491 EMIT2('c'); EMIT2('\347');
492 return OK;
493
494 case 'e': case '\350': case '\351': case '\352': case '\353':
495 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
496 EMIT2('\352'); EMIT2('\353');
497 return OK;
498
499 case 'i': case '\354': case '\355': case '\356': case '\357':
500 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
501 EMIT2('\356'); EMIT2('\357');
502 return OK;
503
504 case 'n': case '\361':
505 EMIT2('n'); EMIT2('\361');
506 return OK;
507
508 case 'o': case '\362': case '\363': case '\364': case '\365':
509 case '\366':
510 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
511 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
512 return OK;
513
514 case 'u': case '\371': case '\372': case '\373': case '\374':
515 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
516 EMIT2('\373'); EMIT2('\374');
517 return OK;
518
519 case 'y': case '\375': case '\377':
520 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
521 return OK;
522
523 default:
524 return FAIL;
525 }
526 }
527
528 EMIT(c);
529 return OK;
530#undef EMIT2
531}
532
533/*
534 * Code to parse regular expression.
535 *
536 * We try to reuse parsing functions in regexp.c to
537 * minimize surprise and keep the syntax consistent.
538 */
539
540/*
541 * Increments the pointer "p" by one (multi-byte) character.
542 */
543 static void
544nfa_inc(p)
545 char_u **p;
546{
547#ifdef FEAT_MBYTE
548 if (has_mbyte)
549 mb_ptr2char_adv(p);
550 else
551#endif
552 *p = *p + 1;
553}
554
555/*
556 * Decrements the pointer "p" by one (multi-byte) character.
557 */
558 static void
559nfa_dec(p)
560 char_u **p;
561{
562#ifdef FEAT_MBYTE
563 char_u *p2, *oldp;
564
565 if (has_mbyte)
566 {
567 oldp = *p;
568 /* Try to find the multibyte char that advances to the current
569 * position. */
570 do
571 {
572 *p = *p - 1;
573 p2 = *p;
574 mb_ptr2char_adv(&p2);
575 } while (p2 != oldp);
576 }
577#else
578 *p = *p - 1;
579#endif
580}
581
582/*
583 * Parse the lowest level.
584 *
585 * An atom can be one of a long list of items. Many atoms match one character
586 * in the text. It is often an ordinary character or a character class.
587 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
588 * is only for syntax highlighting.
589 *
590 * atom ::= ordinary-atom
591 * or \( pattern \)
592 * or \%( pattern \)
593 * or \z( pattern \)
594 */
595 static int
596nfa_regatom()
597{
598 int c;
599 int charclass;
600 int equiclass;
601 int collclass;
602 int got_coll_char;
603 char_u *p;
604 char_u *endp;
605#ifdef FEAT_MBYTE
606 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607#endif
608 int extra = 0;
609 int first;
610 int emit_range;
611 int negated;
612 int result;
613 int startc = -1;
614 int endc = -1;
615 int oldstartc = -1;
616 int cpo_lit; /* 'cpoptions' contains 'l' flag */
617 int cpo_bsl; /* 'cpoptions' contains '\' flag */
618 int glue; /* ID that will "glue" nodes together */
619
620 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
621 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
622
623 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200624 switch (c)
625 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200626 case NUL:
627 syntax_error = TRUE;
628 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
629
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200630 case Magic('^'):
631 EMIT(NFA_BOL);
632 break;
633
634 case Magic('$'):
635 EMIT(NFA_EOL);
636#if defined(FEAT_SYN_HL) || defined(PROTO)
637 had_eol = TRUE;
638#endif
639 break;
640
641 case Magic('<'):
642 EMIT(NFA_BOW);
643 break;
644
645 case Magic('>'):
646 EMIT(NFA_EOW);
647 break;
648
649 case Magic('_'):
650 c = no_Magic(getchr());
651 if (c == '^') /* "\_^" is start-of-line */
652 {
653 EMIT(NFA_BOL);
654 break;
655 }
656 if (c == '$') /* "\_$" is end-of-line */
657 {
658 EMIT(NFA_EOL);
659#if defined(FEAT_SYN_HL) || defined(PROTO)
660 had_eol = TRUE;
661#endif
662 break;
663 }
664
665 extra = ADD_NL;
666
667 /* "\_[" is collection plus newline */
668 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200669 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200670
671 /* "\_x" is character class plus newline */
672 /*FALLTHROUGH*/
673
674 /*
675 * Character classes.
676 */
677 case Magic('.'):
678 case Magic('i'):
679 case Magic('I'):
680 case Magic('k'):
681 case Magic('K'):
682 case Magic('f'):
683 case Magic('F'):
684 case Magic('p'):
685 case Magic('P'):
686 case Magic('s'):
687 case Magic('S'):
688 case Magic('d'):
689 case Magic('D'):
690 case Magic('x'):
691 case Magic('X'):
692 case Magic('o'):
693 case Magic('O'):
694 case Magic('w'):
695 case Magic('W'):
696 case Magic('h'):
697 case Magic('H'):
698 case Magic('a'):
699 case Magic('A'):
700 case Magic('l'):
701 case Magic('L'):
702 case Magic('u'):
703 case Magic('U'):
704 p = vim_strchr(classchars, no_Magic(c));
705 if (p == NULL)
706 {
707 return FAIL; /* runtime error */
708 }
709#ifdef FEAT_MBYTE
710 /* When '.' is followed by a composing char ignore the dot, so that
711 * the composing char is matched here. */
712 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
713 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200714 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200715 c = getchr();
716 goto nfa_do_multibyte;
717 }
718#endif
719 EMIT(nfa_classcodes[p - classchars]);
720 if (extra == ADD_NL)
721 {
722 EMIT(NFA_NEWL);
723 EMIT(NFA_OR);
724 regflags |= RF_HASNL;
725 }
726 break;
727
728 case Magic('n'):
729 if (reg_string)
730 /* In a string "\n" matches a newline character. */
731 EMIT(NL);
732 else
733 {
734 /* In buffer text "\n" matches the end of a line. */
735 EMIT(NFA_NEWL);
736 regflags |= RF_HASNL;
737 }
738 break;
739
740 case Magic('('):
741 if (nfa_reg(REG_PAREN) == FAIL)
742 return FAIL; /* cascaded error */
743 break;
744
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200745 case Magic('|'):
746 case Magic('&'):
747 case Magic(')'):
748 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200749 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750 return FAIL;
751
752 case Magic('='):
753 case Magic('?'):
754 case Magic('+'):
755 case Magic('@'):
756 case Magic('*'):
757 case Magic('{'):
758 /* these should follow an atom, not form an atom */
759 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200760 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200761 return FAIL;
762
763 case Magic('~'): /* previous substitute pattern */
764 /* Not supported yet */
765 return FAIL;
766
767 case Magic('1'):
768 case Magic('2'):
769 case Magic('3'):
770 case Magic('4'):
771 case Magic('5'):
772 case Magic('6'):
773 case Magic('7'):
774 case Magic('8'):
775 case Magic('9'):
776 /* not supported yet */
777 return FAIL;
778
779 case Magic('z'):
780 c = no_Magic(getchr());
781 switch (c)
782 {
783 case 's':
784 EMIT(NFA_ZSTART);
785 break;
786 case 'e':
787 EMIT(NFA_ZEND);
788 nfa_has_zend = TRUE;
789 /* TODO: Currently \ze does not work properly. */
790 return FAIL;
791 /* break; */
792 case '1':
793 case '2':
794 case '3':
795 case '4':
796 case '5':
797 case '6':
798 case '7':
799 case '8':
800 case '9':
801 case '(':
802 /* \z1...\z9 and \z( not yet supported */
803 return FAIL;
804 default:
805 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200806 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200807 no_Magic(c));
808 return FAIL;
809 }
810 break;
811
812 case Magic('%'):
813 c = no_Magic(getchr());
814 switch (c)
815 {
816 /* () without a back reference */
817 case '(':
818 if (nfa_reg(REG_NPAREN) == FAIL)
819 return FAIL;
820 EMIT(NFA_NOPEN);
821 break;
822
823 case 'd': /* %d123 decimal */
824 case 'o': /* %o123 octal */
825 case 'x': /* %xab hex 2 */
826 case 'u': /* %uabcd hex 4 */
827 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200828 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200829 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200830
Bram Moolenaar47196582013-05-25 22:04:23 +0200831 switch (c)
832 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200833 case 'd': nr = getdecchrs(); break;
834 case 'o': nr = getoctchrs(); break;
835 case 'x': nr = gethexchrs(2); break;
836 case 'u': nr = gethexchrs(4); break;
837 case 'U': nr = gethexchrs(8); break;
838 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200839 }
840
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200841 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200842 EMSG2_RET_FAIL(
843 _("E678: Invalid character after %s%%[dxouU]"),
844 reg_magic == MAGIC_ALL);
845 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200846 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200847 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200848 break;
849
850 /* Catch \%^ and \%$ regardless of where they appear in the
851 * pattern -- regardless of whether or not it makes sense. */
852 case '^':
853 EMIT(NFA_BOF);
854 /* Not yet supported */
855 return FAIL;
856 break;
857
858 case '$':
859 EMIT(NFA_EOF);
860 /* Not yet supported */
861 return FAIL;
862 break;
863
864 case '#':
865 /* not supported yet */
866 return FAIL;
867 break;
868
869 case 'V':
870 /* not supported yet */
871 return FAIL;
872 break;
873
874 case '[':
875 /* \%[abc] not supported yet */
876 return FAIL;
877
878 default:
879 /* not supported yet */
880 return FAIL;
881 }
882 break;
883
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200885collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200886 /*
887 * Glue is emitted between several atoms from the [].
888 * It is either NFA_OR, or NFA_CONCAT.
889 *
890 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
891 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
892 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
893 * notation)
894 *
895 */
896
897
898/* Emit negation atoms, if needed.
899 * The CONCAT below merges the NOT with the previous node. */
900#define TRY_NEG() \
901 if (negated == TRUE) \
902 { \
903 EMIT(NFA_NOT); \
904 }
905
906/* Emit glue between important nodes : CONCAT or OR. */
907#define EMIT_GLUE() \
908 if (first == FALSE) \
909 EMIT(glue); \
910 else \
911 first = FALSE;
912
913 p = regparse;
914 endp = skip_anyof(p);
915 if (*endp == ']')
916 {
917 /*
918 * Try to reverse engineer character classes. For example,
919 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
920 * and perform the necessary substitutions in the NFA.
921 */
922 result = nfa_recognize_char_class(regparse, endp,
923 extra == ADD_NL);
924 if (result != FAIL)
925 {
926 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
927 EMIT(result);
928 else /* must be char class + newline */
929 {
930 EMIT(result - ADD_NL);
931 EMIT(NFA_NEWL);
932 EMIT(NFA_OR);
933 }
934 regparse = endp;
935 nfa_inc(&regparse);
936 return OK;
937 }
938 /*
939 * Failed to recognize a character class. Use the simple
940 * version that turns [abc] into 'a' OR 'b' OR 'c'
941 */
942 startc = endc = oldstartc = -1;
943 first = TRUE; /* Emitting first atom in this sequence? */
944 negated = FALSE;
945 glue = NFA_OR;
946 if (*regparse == '^') /* negated range */
947 {
948 negated = TRUE;
949 glue = NFA_CONCAT;
950 nfa_inc(&regparse);
951 }
952 if (*regparse == '-')
953 {
954 startc = '-';
955 EMIT(startc);
956 TRY_NEG();
957 EMIT_GLUE();
958 nfa_inc(&regparse);
959 }
960 /* Emit the OR branches for each character in the [] */
961 emit_range = FALSE;
962 while (regparse < endp)
963 {
964 oldstartc = startc;
965 startc = -1;
966 got_coll_char = FALSE;
967 if (*regparse == '[')
968 {
969 /* Check for [: :], [= =], [. .] */
970 equiclass = collclass = 0;
971 charclass = get_char_class(&regparse);
972 if (charclass == CLASS_NONE)
973 {
974 equiclass = get_equi_class(&regparse);
975 if (equiclass == 0)
976 collclass = get_coll_element(&regparse);
977 }
978
979 /* Character class like [:alpha:] */
980 if (charclass != CLASS_NONE)
981 {
982 switch (charclass)
983 {
984 case CLASS_ALNUM:
985 EMIT(NFA_CLASS_ALNUM);
986 break;
987 case CLASS_ALPHA:
988 EMIT(NFA_CLASS_ALPHA);
989 break;
990 case CLASS_BLANK:
991 EMIT(NFA_CLASS_BLANK);
992 break;
993 case CLASS_CNTRL:
994 EMIT(NFA_CLASS_CNTRL);
995 break;
996 case CLASS_DIGIT:
997 EMIT(NFA_CLASS_DIGIT);
998 break;
999 case CLASS_GRAPH:
1000 EMIT(NFA_CLASS_GRAPH);
1001 break;
1002 case CLASS_LOWER:
1003 EMIT(NFA_CLASS_LOWER);
1004 break;
1005 case CLASS_PRINT:
1006 EMIT(NFA_CLASS_PRINT);
1007 break;
1008 case CLASS_PUNCT:
1009 EMIT(NFA_CLASS_PUNCT);
1010 break;
1011 case CLASS_SPACE:
1012 EMIT(NFA_CLASS_SPACE);
1013 break;
1014 case CLASS_UPPER:
1015 EMIT(NFA_CLASS_UPPER);
1016 break;
1017 case CLASS_XDIGIT:
1018 EMIT(NFA_CLASS_XDIGIT);
1019 break;
1020 case CLASS_TAB:
1021 EMIT(NFA_CLASS_TAB);
1022 break;
1023 case CLASS_RETURN:
1024 EMIT(NFA_CLASS_RETURN);
1025 break;
1026 case CLASS_BACKSPACE:
1027 EMIT(NFA_CLASS_BACKSPACE);
1028 break;
1029 case CLASS_ESCAPE:
1030 EMIT(NFA_CLASS_ESCAPE);
1031 break;
1032 }
1033 TRY_NEG();
1034 EMIT_GLUE();
1035 continue;
1036 }
1037 /* Try equivalence class [=a=] and the like */
1038 if (equiclass != 0)
1039 {
1040 result = nfa_emit_equi_class(equiclass, negated);
1041 if (result == FAIL)
1042 {
1043 /* should never happen */
1044 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1045 }
1046 EMIT_GLUE();
1047 continue;
1048 }
1049 /* Try collating class like [. .] */
1050 if (collclass != 0)
1051 {
1052 startc = collclass; /* allow [.a.]-x as a range */
1053 /* Will emit the proper atom at the end of the
1054 * while loop. */
1055 }
1056 }
1057 /* Try a range like 'a-x' or '\t-z' */
1058 if (*regparse == '-')
1059 {
1060 emit_range = TRUE;
1061 startc = oldstartc;
1062 nfa_inc(&regparse);
1063 continue; /* reading the end of the range */
1064 }
1065
1066 /* Now handle simple and escaped characters.
1067 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1068 * accepts "\t", "\e", etc., but only when the 'l' flag in
1069 * 'cpoptions' is not included.
1070 * Posix doesn't recognize backslash at all.
1071 */
1072 if (*regparse == '\\'
1073 && !cpo_bsl
1074 && regparse + 1 <= endp
1075 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1076 || (!cpo_lit
1077 && vim_strchr(REGEXP_ABBR, regparse[1])
1078 != NULL)
1079 )
1080 )
1081 {
1082 nfa_inc(&regparse);
1083
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001084 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 startc = reg_string ? NL : NFA_NEWL;
1086 else
1087 if (*regparse == 'd'
1088 || *regparse == 'o'
1089 || *regparse == 'x'
1090 || *regparse == 'u'
1091 || *regparse == 'U'
1092 )
1093 {
1094 /* TODO(RE) This needs more testing */
1095 startc = coll_get_char();
1096 got_coll_char = TRUE;
1097 nfa_dec(&regparse);
1098 }
1099 else
1100 {
1101 /* \r,\t,\e,\b */
1102 startc = backslash_trans(*regparse);
1103 }
1104 }
1105
1106 /* Normal printable char */
1107 if (startc == -1)
1108#ifdef FEAT_MBYTE
1109 startc = (*mb_ptr2char)(regparse);
1110#else
1111 startc = *regparse;
1112#endif
1113
1114 /* Previous char was '-', so this char is end of range. */
1115 if (emit_range)
1116 {
1117 endc = startc; startc = oldstartc;
1118 if (startc > endc)
1119 EMSG_RET_FAIL(_(e_invrange));
1120#ifdef FEAT_MBYTE
1121 if (has_mbyte && ((*mb_char2len)(startc) > 1
1122 || (*mb_char2len)(endc) > 1))
1123 {
1124 if (endc > startc + 256)
1125 EMSG_RET_FAIL(_(e_invrange));
1126 /* Emit the range. "startc" was already emitted, so
1127 * skip it. */
1128 for (c = startc + 1; c <= endc; c++)
1129 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001130 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001131 TRY_NEG();
1132 EMIT_GLUE();
1133 }
1134 emit_range = FALSE;
1135 }
1136 else
1137#endif
1138 {
1139#ifdef EBCDIC
1140 int alpha_only = FALSE;
1141
1142 /* for alphabetical range skip the gaps
1143 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1144 if (isalpha(startc) && isalpha(endc))
1145 alpha_only = TRUE;
1146#endif
1147 /* Emit the range. "startc" was already emitted, so
1148 * skip it. */
1149 for (c = startc + 1; c <= endc; c++)
1150#ifdef EBCDIC
1151 if (!alpha_only || isalpha(startc))
1152#endif
1153 {
1154 EMIT(c);
1155 TRY_NEG();
1156 EMIT_GLUE();
1157 }
1158 emit_range = FALSE;
1159 }
1160 }
1161 else
1162 {
1163 /*
1164 * This char (startc) is not part of a range. Just
1165 * emit it.
1166 *
1167 * Normally, simply emit startc. But if we get char
1168 * code=0 from a collating char, then replace it with
1169 * 0x0a.
1170 *
1171 * This is needed to completely mimic the behaviour of
1172 * the backtracking engine.
1173 */
1174 if (got_coll_char == TRUE && startc == 0)
1175 EMIT(0x0a);
1176 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001177 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001178 TRY_NEG();
1179 EMIT_GLUE();
1180 }
1181
1182 nfa_inc(&regparse);
1183 } /* while (p < endp) */
1184
1185 nfa_dec(&regparse);
1186 if (*regparse == '-') /* if last, '-' is just a char */
1187 {
1188 EMIT('-');
1189 TRY_NEG();
1190 EMIT_GLUE();
1191 }
1192 nfa_inc(&regparse);
1193
1194 if (extra == ADD_NL) /* \_[] also matches \n */
1195 {
1196 EMIT(reg_string ? NL : NFA_NEWL);
1197 TRY_NEG();
1198 EMIT_GLUE();
1199 }
1200
1201 /* skip the trailing ] */
1202 regparse = endp;
1203 nfa_inc(&regparse);
1204 if (negated == TRUE)
1205 {
1206 /* Mark end of negated char range */
1207 EMIT(NFA_END_NEG_RANGE);
1208 EMIT(NFA_CONCAT);
1209 }
1210 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001211 } /* if exists closing ] */
1212
1213 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 {
1215 syntax_error = TRUE;
1216 EMSG_RET_FAIL(_(e_missingbracket));
1217 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001218 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001220 default:
1221 {
1222#ifdef FEAT_MBYTE
1223 int plen;
1224
1225nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001226 /* plen is length of current char with composing chars */
1227 if (enc_utf8 && ((*mb_char2len)(c)
1228 != (plen = (*mb_ptr2len)(old_regparse))
1229 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001230 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001231 int i = 0;
1232
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001233 /* A base character plus composing characters, or just one
1234 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001235 * This requires creating a separate atom as if enclosing
1236 * the characters in (), where NFA_COMPOSING is the ( and
1237 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 * building the postfix form, not the NFA itself;
1239 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001240 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001241 for (;;)
1242 {
1243 EMIT(c);
1244 if (i > 0)
1245 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001246 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001247 break;
1248 c = utf_ptr2char(old_regparse + i);
1249 }
1250 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 regparse = old_regparse + plen;
1252 }
1253 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254#endif
1255 {
1256 c = no_Magic(c);
1257 EMIT(c);
1258 }
1259 return OK;
1260 }
1261 }
1262
1263#undef TRY_NEG
1264#undef EMIT_GLUE
1265
1266 return OK;
1267}
1268
1269/*
1270 * Parse something followed by possible [*+=].
1271 *
1272 * A piece is an atom, possibly followed by a multi, an indication of how many
1273 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1274 * characters: "", "a", "aa", etc.
1275 *
1276 * piece ::= atom
1277 * or atom multi
1278 */
1279 static int
1280nfa_regpiece()
1281{
1282 int i;
1283 int op;
1284 int ret;
1285 long minval, maxval;
1286 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1287 char_u *old_regparse, *new_regparse;
1288 int c2;
1289 int *old_post_ptr, *my_post_start;
1290 int old_regnpar;
1291 int quest;
1292
1293 /* Save the current position in the regexp, so that we can use it if
1294 * <atom>{m,n} is next. */
1295 old_regparse = regparse;
1296 /* Save current number of open parenthesis, so we can use it if
1297 * <atom>{m,n} is next */
1298 old_regnpar = regnpar;
1299 /* store current pos in the postfix form, for \{m,n} involving 0s */
1300 my_post_start = post_ptr;
1301
1302 ret = nfa_regatom();
1303 if (ret == FAIL)
1304 return FAIL; /* cascaded error */
1305
1306 op = peekchr();
1307 if (re_multi_type(op) == NOT_MULTI)
1308 return OK;
1309
1310 skipchr();
1311 switch (op)
1312 {
1313 case Magic('*'):
1314 EMIT(NFA_STAR);
1315 break;
1316
1317 case Magic('+'):
1318 /*
1319 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1320 * first and only submatch would be "aaa". But the backtracking
1321 * engine interprets the plus as "try matching one more time", and
1322 * a* matches a second time at the end of the input, the empty
1323 * string.
1324 * The submatch will the empty string.
1325 *
1326 * In order to be consistent with the old engine, we disable
1327 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1328 */
1329 /* EMIT(NFA_PLUS); */
1330 regnpar = old_regnpar;
1331 regparse = old_regparse;
1332 curchr = -1;
1333 if (nfa_regatom() == FAIL)
1334 return FAIL;
1335 EMIT(NFA_STAR);
1336 EMIT(NFA_CONCAT);
1337 skipchr(); /* skip the \+ */
1338 break;
1339
1340 case Magic('@'):
1341 op = no_Magic(getchr());
1342 switch(op)
1343 {
1344 case '=':
1345 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1346 break;
1347 case '!':
1348 case '<':
1349 case '>':
1350 /* Not supported yet */
1351 return FAIL;
1352 default:
1353 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001354 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355 return FAIL;
1356 }
1357 break;
1358
1359 case Magic('?'):
1360 case Magic('='):
1361 EMIT(NFA_QUEST);
1362 break;
1363
1364 case Magic('{'):
1365 /* a{2,5} will expand to 'aaa?a?a?'
1366 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1367 * version of '?'
1368 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1369 * parenthesis have the same id
1370 */
1371
1372 greedy = TRUE;
1373 c2 = peekchr();
1374 if (c2 == '-' || c2 == Magic('-'))
1375 {
1376 skipchr();
1377 greedy = FALSE;
1378 }
1379 if (!read_limits(&minval, &maxval))
1380 {
1381 syntax_error = TRUE;
1382 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1383 }
1384 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1385 * <atom>* */
1386 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1387 {
1388 EMIT(NFA_STAR);
1389 break;
1390 }
1391
1392 if (maxval > NFA_BRACES_MAXLIMIT)
1393 {
1394 /* This would yield a huge automaton and use too much memory.
1395 * Revert to old engine */
1396 return FAIL;
1397 }
1398
1399 /* Special case: x{0} or x{-0} */
1400 if (maxval == 0)
1401 {
1402 /* Ignore result of previous call to nfa_regatom() */
1403 post_ptr = my_post_start;
1404 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1405 EMIT(NFA_SKIP_CHAR);
1406 return OK;
1407 }
1408
1409 /* Ignore previous call to nfa_regatom() */
1410 post_ptr = my_post_start;
1411 /* Save pos after the repeated atom and the \{} */
1412 new_regparse = regparse;
1413
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001414 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1415 for (i = 0; i < maxval; i++)
1416 {
1417 /* Goto beginning of the repeated atom */
1418 regparse = old_regparse;
1419 curchr = -1;
1420 /* Restore count of parenthesis */
1421 regnpar = old_regnpar;
1422 old_post_ptr = post_ptr;
1423 if (nfa_regatom() == FAIL)
1424 return FAIL;
1425 /* after "minval" times, atoms are optional */
1426 if (i + 1 > minval)
1427 EMIT(quest);
1428 if (old_post_ptr != my_post_start)
1429 EMIT(NFA_CONCAT);
1430 }
1431
1432 /* Go to just after the repeated atom and the \{} */
1433 regparse = new_regparse;
1434 curchr = -1;
1435
1436 break;
1437
1438
1439 default:
1440 break;
1441 } /* end switch */
1442
1443 if (re_multi_type(peekchr()) != NOT_MULTI)
1444 {
1445 /* Can't have a multi follow a multi. */
1446 syntax_error = TRUE;
1447 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1448 }
1449
1450 return OK;
1451}
1452
1453/*
1454 * Parse one or more pieces, concatenated. It matches a match for the
1455 * first piece, followed by a match for the second piece, etc. Example:
1456 * "f[0-9]b", first matches "f", then a digit and then "b".
1457 *
1458 * concat ::= piece
1459 * or piece piece
1460 * or piece piece piece
1461 * etc.
1462 */
1463 static int
1464nfa_regconcat()
1465{
1466 int cont = TRUE;
1467 int first = TRUE;
1468
1469 while (cont)
1470 {
1471 switch (peekchr())
1472 {
1473 case NUL:
1474 case Magic('|'):
1475 case Magic('&'):
1476 case Magic(')'):
1477 cont = FALSE;
1478 break;
1479
1480 case Magic('Z'):
1481#ifdef FEAT_MBYTE
1482 regflags |= RF_ICOMBINE;
1483#endif
1484 skipchr_keepstart();
1485 break;
1486 case Magic('c'):
1487 regflags |= RF_ICASE;
1488 skipchr_keepstart();
1489 break;
1490 case Magic('C'):
1491 regflags |= RF_NOICASE;
1492 skipchr_keepstart();
1493 break;
1494 case Magic('v'):
1495 reg_magic = MAGIC_ALL;
1496 skipchr_keepstart();
1497 curchr = -1;
1498 break;
1499 case Magic('m'):
1500 reg_magic = MAGIC_ON;
1501 skipchr_keepstart();
1502 curchr = -1;
1503 break;
1504 case Magic('M'):
1505 reg_magic = MAGIC_OFF;
1506 skipchr_keepstart();
1507 curchr = -1;
1508 break;
1509 case Magic('V'):
1510 reg_magic = MAGIC_NONE;
1511 skipchr_keepstart();
1512 curchr = -1;
1513 break;
1514
1515 default:
1516 if (nfa_regpiece() == FAIL)
1517 return FAIL;
1518 if (first == FALSE)
1519 EMIT(NFA_CONCAT);
1520 else
1521 first = FALSE;
1522 break;
1523 }
1524 }
1525
1526 return OK;
1527}
1528
1529/*
1530 * Parse a branch, one or more concats, separated by "\&". It matches the
1531 * last concat, but only if all the preceding concats also match at the same
1532 * position. Examples:
1533 * "foobeep\&..." matches "foo" in "foobeep".
1534 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1535 *
1536 * branch ::= concat
1537 * or concat \& concat
1538 * or concat \& concat \& concat
1539 * etc.
1540 */
1541 static int
1542nfa_regbranch()
1543{
1544 int ch;
1545 int *old_post_ptr;
1546
1547 old_post_ptr = post_ptr;
1548
1549 /* First branch, possibly the only one */
1550 if (nfa_regconcat() == FAIL)
1551 return FAIL;
1552
1553 ch = peekchr();
1554 /* Try next concats */
1555 while (ch == Magic('&'))
1556 {
1557 skipchr();
1558 EMIT(NFA_NOPEN);
1559 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1560 old_post_ptr = post_ptr;
1561 if (nfa_regconcat() == FAIL)
1562 return FAIL;
1563 /* if concat is empty, skip a input char. But do emit a node */
1564 if (old_post_ptr == post_ptr)
1565 EMIT(NFA_SKIP_CHAR);
1566 EMIT(NFA_CONCAT);
1567 ch = peekchr();
1568 }
1569
1570 /* Even if a branch is empty, emit one node for it */
1571 if (old_post_ptr == post_ptr)
1572 EMIT(NFA_SKIP_CHAR);
1573
1574 return OK;
1575}
1576
1577/*
1578 * Parse a pattern, one or more branches, separated by "\|". It matches
1579 * anything that matches one of the branches. Example: "foo\|beep" matches
1580 * "foo" and matches "beep". If more than one branch matches, the first one
1581 * is used.
1582 *
1583 * pattern ::= branch
1584 * or branch \| branch
1585 * or branch \| branch \| branch
1586 * etc.
1587 */
1588 static int
1589nfa_reg(paren)
1590 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1591{
1592 int parno = 0;
1593
1594#ifdef FEAT_SYN_HL
1595#endif
1596 if (paren == REG_PAREN)
1597 {
1598 if (regnpar >= NSUBEXP) /* Too many `(' */
1599 {
1600 syntax_error = TRUE;
1601 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1602 }
1603 parno = regnpar++;
1604 }
1605
1606 if (nfa_regbranch() == FAIL)
1607 return FAIL; /* cascaded error */
1608
1609 while (peekchr() == Magic('|'))
1610 {
1611 skipchr();
1612 if (nfa_regbranch() == FAIL)
1613 return FAIL; /* cascaded error */
1614 EMIT(NFA_OR);
1615 }
1616
1617 /* Check for proper termination. */
1618 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1619 {
1620 syntax_error = TRUE;
1621 if (paren == REG_NPAREN)
1622 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1623 else
1624 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1625 }
1626 else if (paren == REG_NOPAREN && peekchr() != NUL)
1627 {
1628 syntax_error = TRUE;
1629 if (peekchr() == Magic(')'))
1630 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1631 else
1632 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1633 }
1634 /*
1635 * Here we set the flag allowing back references to this set of
1636 * parentheses.
1637 */
1638 if (paren == REG_PAREN)
1639 {
1640 had_endbrace[parno] = TRUE; /* have seen the close paren */
1641 EMIT(NFA_MOPEN + parno);
1642 }
1643
1644 return OK;
1645}
1646
1647typedef struct
1648{
1649 char_u *start[NSUBEXP];
1650 char_u *end[NSUBEXP];
1651 lpos_T startpos[NSUBEXP];
1652 lpos_T endpos[NSUBEXP];
1653} regsub_T;
1654
1655static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
1656
1657#ifdef DEBUG
1658static char_u code[50];
1659
1660 static void
1661nfa_set_code(c)
1662 int c;
1663{
1664 int addnl = FALSE;
1665
1666 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1667 {
1668 addnl = TRUE;
1669 c -= ADD_NL;
1670 }
1671
1672 STRCPY(code, "");
1673 switch (c)
1674 {
1675 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1676 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1677 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1678 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1679 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1680 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1681
1682 case NFA_PREV_ATOM_NO_WIDTH:
1683 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1684 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1685 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1686 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1687 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1688
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1690 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1691
1692 case NFA_MOPEN + 0:
1693 case NFA_MOPEN + 1:
1694 case NFA_MOPEN + 2:
1695 case NFA_MOPEN + 3:
1696 case NFA_MOPEN + 4:
1697 case NFA_MOPEN + 5:
1698 case NFA_MOPEN + 6:
1699 case NFA_MOPEN + 7:
1700 case NFA_MOPEN + 8:
1701 case NFA_MOPEN + 9:
1702 STRCPY(code, "NFA_MOPEN(x)");
1703 code[10] = c - NFA_MOPEN + '0';
1704 break;
1705 case NFA_MCLOSE + 0:
1706 case NFA_MCLOSE + 1:
1707 case NFA_MCLOSE + 2:
1708 case NFA_MCLOSE + 3:
1709 case NFA_MCLOSE + 4:
1710 case NFA_MCLOSE + 5:
1711 case NFA_MCLOSE + 6:
1712 case NFA_MCLOSE + 7:
1713 case NFA_MCLOSE + 8:
1714 case NFA_MCLOSE + 9:
1715 STRCPY(code, "NFA_MCLOSE(x)");
1716 code[11] = c - NFA_MCLOSE + '0';
1717 break;
1718 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1719 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1720 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1721 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1722 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1723 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1724 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1725 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1726 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1727 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1728 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1729 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1730 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1731 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1732 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1733 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1734 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1735 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1736 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1737 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1738 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1739 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1740 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1741 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1742 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1743 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1744 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1745 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1746
1747 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1748 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1749 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1750 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1751 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1752 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1753 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1754 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1755 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1756 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1757 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1758 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1759 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1760 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1761 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1762 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1763 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1764 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1765 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1766 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1767 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1768 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1769 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1770 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1771 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1772 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1773 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1774
1775 default:
1776 STRCPY(code, "CHAR(x)");
1777 code[5] = c;
1778 }
1779
1780 if (addnl == TRUE)
1781 STRCAT(code, " + NEWLINE ");
1782
1783}
1784
1785#ifdef ENABLE_LOG
1786static FILE *log_fd;
1787
1788/*
1789 * Print the postfix notation of the current regexp.
1790 */
1791 static void
1792nfa_postfix_dump(expr, retval)
1793 char_u *expr;
1794 int retval;
1795{
1796 int *p;
1797 FILE *f;
1798
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001799 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800 if (f != NULL)
1801 {
1802 fprintf(f, "\n-------------------------\n");
1803 if (retval == FAIL)
1804 fprintf(f, ">>> NFA engine failed ... \n");
1805 else if (retval == OK)
1806 fprintf(f, ">>> NFA engine succeeded !\n");
1807 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001808 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809 {
1810 nfa_set_code(*p);
1811 fprintf(f, "%s, ", code);
1812 }
1813 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001814 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001815 fprintf(f, "%d ", *p);
1816 fprintf(f, "\n\n");
1817 fclose(f);
1818 }
1819}
1820
1821/*
1822 * Print the NFA starting with a root node "state".
1823 */
1824 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001825nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001826 FILE *debugf;
1827 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001828{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001829 garray_T indent;
1830
1831 ga_init2(&indent, 1, 64);
1832 ga_append(&indent, '\0');
1833 nfa_print_state2(debugf, state, &indent);
1834 ga_clear(&indent);
1835}
1836
1837 static void
1838nfa_print_state2(debugf, state, indent)
1839 FILE *debugf;
1840 nfa_state_T *state;
1841 garray_T *indent;
1842{
1843 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844
1845 if (state == NULL)
1846 return;
1847
1848 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001849
1850 /* Output indent */
1851 p = (char_u *)indent->ga_data;
1852 if (indent->ga_len >= 3)
1853 {
1854 int last = indent->ga_len - 3;
1855 char_u save[2];
1856
1857 STRNCPY(save, &p[last], 2);
1858 STRNCPY(&p[last], "+-", 2);
1859 fprintf(debugf, " %s", p);
1860 STRNCPY(&p[last], save, 2);
1861 }
1862 else
1863 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001864
1865 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001866 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1867 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 if (state->id < 0)
1869 return;
1870
1871 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001872
1873 /* grow indent for state->out */
1874 indent->ga_len -= 1;
1875 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001876 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001877 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001878 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001879 ga_append(indent, '\0');
1880
1881 nfa_print_state2(debugf, state->out, indent);
1882
1883 /* replace last part of indent for state->out1 */
1884 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001885 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001886 ga_append(indent, '\0');
1887
1888 nfa_print_state2(debugf, state->out1, indent);
1889
1890 /* shrink indent */
1891 indent->ga_len -= 3;
1892 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001893}
1894
1895/*
1896 * Print the NFA state machine.
1897 */
1898 static void
1899nfa_dump(prog)
1900 nfa_regprog_T *prog;
1901{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001902 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903
1904 if (debugf != NULL)
1905 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001906 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 fclose(debugf);
1908 }
1909}
1910#endif /* ENABLE_LOG */
1911#endif /* DEBUG */
1912
1913/*
1914 * Parse r.e. @expr and convert it into postfix form.
1915 * Return the postfix string on success, NULL otherwise.
1916 */
1917 static int *
1918re2post()
1919{
1920 if (nfa_reg(REG_NOPAREN) == FAIL)
1921 return NULL;
1922 EMIT(NFA_MOPEN);
1923 return post_start;
1924}
1925
1926/* NB. Some of the code below is inspired by Russ's. */
1927
1928/*
1929 * Represents an NFA state plus zero or one or two arrows exiting.
1930 * if c == MATCH, no arrows out; matching state.
1931 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1932 * If c < 256, labeled arrow with character c to out.
1933 */
1934
1935static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1936
1937/*
1938 * Allocate and initialize nfa_state_T.
1939 */
1940 static nfa_state_T *
1941new_state(c, out, out1)
1942 int c;
1943 nfa_state_T *out;
1944 nfa_state_T *out1;
1945{
1946 nfa_state_T *s;
1947
1948 if (istate >= nstate)
1949 return NULL;
1950
1951 s = &state_ptr[istate++];
1952
1953 s->c = c;
1954 s->out = out;
1955 s->out1 = out1;
1956
1957 s->id = istate;
1958 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001959 s->visits = 0;
1960 s->negated = FALSE;
1961
1962 return s;
1963}
1964
1965/*
1966 * A partially built NFA without the matching state filled in.
1967 * Frag_T.start points at the start state.
1968 * Frag_T.out is a list of places that need to be set to the
1969 * next state for this fragment.
1970 */
1971typedef union Ptrlist Ptrlist;
1972struct Frag
1973{
1974 nfa_state_T *start;
1975 Ptrlist *out;
1976};
1977typedef struct Frag Frag_T;
1978
1979static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
1980static Ptrlist *list1 __ARGS((nfa_state_T **outp));
1981static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
1982static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
1983static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
1984static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
1985
1986/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02001987 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001988 */
1989 static Frag_T
1990frag(start, out)
1991 nfa_state_T *start;
1992 Ptrlist *out;
1993{
Bram Moolenaar053bb602013-05-20 13:55:21 +02001994 Frag_T n;
1995
1996 n.start = start;
1997 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001998 return n;
1999}
2000
2001/*
2002 * Since the out pointers in the list are always
2003 * uninitialized, we use the pointers themselves
2004 * as storage for the Ptrlists.
2005 */
2006union Ptrlist
2007{
2008 Ptrlist *next;
2009 nfa_state_T *s;
2010};
2011
2012/*
2013 * Create singleton list containing just outp.
2014 */
2015 static Ptrlist *
2016list1(outp)
2017 nfa_state_T **outp;
2018{
2019 Ptrlist *l;
2020
2021 l = (Ptrlist *)outp;
2022 l->next = NULL;
2023 return l;
2024}
2025
2026/*
2027 * Patch the list of states at out to point to start.
2028 */
2029 static void
2030patch(l, s)
2031 Ptrlist *l;
2032 nfa_state_T *s;
2033{
2034 Ptrlist *next;
2035
2036 for (; l; l = next)
2037 {
2038 next = l->next;
2039 l->s = s;
2040 }
2041}
2042
2043
2044/*
2045 * Join the two lists l1 and l2, returning the combination.
2046 */
2047 static Ptrlist *
2048append(l1, l2)
2049 Ptrlist *l1;
2050 Ptrlist *l2;
2051{
2052 Ptrlist *oldl1;
2053
2054 oldl1 = l1;
2055 while (l1->next)
2056 l1 = l1->next;
2057 l1->next = l2;
2058 return oldl1;
2059}
2060
2061/*
2062 * Stack used for transforming postfix form into NFA.
2063 */
2064static Frag_T empty;
2065
2066 static void
2067st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002068 int *postfix UNUSED;
2069 int *end UNUSED;
2070 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002071{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002072#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073 FILE *df;
2074 int *p2;
2075
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002076 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 if (df)
2078 {
2079 fprintf(df, "Error popping the stack!\n");
2080#ifdef DEBUG
2081 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2082#endif
2083 fprintf(df, "Postfix form is: ");
2084#ifdef DEBUG
2085 for (p2 = postfix; p2 < end; p2++)
2086 {
2087 nfa_set_code(*p2);
2088 fprintf(df, "%s, ", code);
2089 }
2090 nfa_set_code(*p);
2091 fprintf(df, "\nCurrent position is: ");
2092 for (p2 = postfix; p2 <= p; p2 ++)
2093 {
2094 nfa_set_code(*p2);
2095 fprintf(df, "%s, ", code);
2096 }
2097#else
2098 for (p2 = postfix; p2 < end; p2++)
2099 {
2100 fprintf(df, "%d, ", *p2);
2101 }
2102 fprintf(df, "\nCurrent position is: ");
2103 for (p2 = postfix; p2 <= p; p2 ++)
2104 {
2105 fprintf(df, "%d, ", *p2);
2106 }
2107#endif
2108 fprintf(df, "\n--------------------------\n");
2109 fclose(df);
2110 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002111#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 EMSG(_("E874: (NFA) Could not pop the stack !"));
2113}
2114
2115/*
2116 * Push an item onto the stack.
2117 */
2118 static void
2119st_push(s, p, stack_end)
2120 Frag_T s;
2121 Frag_T **p;
2122 Frag_T *stack_end;
2123{
2124 Frag_T *stackp = *p;
2125
2126 if (stackp >= stack_end)
2127 return;
2128 *stackp = s;
2129 *p = *p + 1;
2130}
2131
2132/*
2133 * Pop an item from the stack.
2134 */
2135 static Frag_T
2136st_pop(p, stack)
2137 Frag_T **p;
2138 Frag_T *stack;
2139{
2140 Frag_T *stackp;
2141
2142 *p = *p - 1;
2143 stackp = *p;
2144 if (stackp < stack)
2145 return empty;
2146 return **p;
2147}
2148
2149/*
2150 * Convert a postfix form into its equivalent NFA.
2151 * Return the NFA start state on success, NULL otherwise.
2152 */
2153 static nfa_state_T *
2154post2nfa(postfix, end, nfa_calc_size)
2155 int *postfix;
2156 int *end;
2157 int nfa_calc_size;
2158{
2159 int *p;
2160 int mopen;
2161 int mclose;
2162 Frag_T *stack = NULL;
2163 Frag_T *stackp = NULL;
2164 Frag_T *stack_end = NULL;
2165 Frag_T e1;
2166 Frag_T e2;
2167 Frag_T e;
2168 nfa_state_T *s;
2169 nfa_state_T *s1;
2170 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002171 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002172
2173 if (postfix == NULL)
2174 return NULL;
2175
Bram Moolenaar053bb602013-05-20 13:55:21 +02002176#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002177#define POP() st_pop(&stackp, stack); \
2178 if (stackp < stack) \
2179 { \
2180 st_error(postfix, end, p); \
2181 return NULL; \
2182 }
2183
2184 if (nfa_calc_size == FALSE)
2185 {
2186 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002187 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002188 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002189 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002190 }
2191
2192 for (p = postfix; p < end; ++p)
2193 {
2194 switch (*p)
2195 {
2196 case NFA_CONCAT:
2197 /* Catenation.
2198 * Pay attention: this operator does not exist
2199 * in the r.e. itself (it is implicit, really).
2200 * It is added when r.e. is translated to postfix
2201 * form in re2post().
2202 *
2203 * No new state added here. */
2204 if (nfa_calc_size == TRUE)
2205 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002206 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207 break;
2208 }
2209 e2 = POP();
2210 e1 = POP();
2211 patch(e1.out, e2.start);
2212 PUSH(frag(e1.start, e2.out));
2213 break;
2214
2215 case NFA_NOT:
2216 /* Negation of a character */
2217 if (nfa_calc_size == TRUE)
2218 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002219 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 break;
2221 }
2222 e1 = POP();
2223 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002224#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002225 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002226 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002227#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228 PUSH(e1);
2229 break;
2230
2231 case NFA_OR:
2232 /* Alternation */
2233 if (nfa_calc_size == TRUE)
2234 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002235 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236 break;
2237 }
2238 e2 = POP();
2239 e1 = POP();
2240 s = new_state(NFA_SPLIT, e1.start, e2.start);
2241 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002242 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 PUSH(frag(s, append(e1.out, e2.out)));
2244 break;
2245
2246 case NFA_STAR:
2247 /* Zero or more */
2248 if (nfa_calc_size == TRUE)
2249 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002250 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002251 break;
2252 }
2253 e = POP();
2254 s = new_state(NFA_SPLIT, e.start, NULL);
2255 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002256 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002257 patch(e.out, s);
2258 PUSH(frag(s, list1(&s->out1)));
2259 break;
2260
2261 case NFA_QUEST:
2262 /* one or zero atoms=> greedy match */
2263 if (nfa_calc_size == TRUE)
2264 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002265 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 break;
2267 }
2268 e = POP();
2269 s = new_state(NFA_SPLIT, e.start, NULL);
2270 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002271 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002272 PUSH(frag(s, append(e.out, list1(&s->out1))));
2273 break;
2274
2275 case NFA_QUEST_NONGREEDY:
2276 /* zero or one atoms => non-greedy match */
2277 if (nfa_calc_size == TRUE)
2278 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002279 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002280 break;
2281 }
2282 e = POP();
2283 s = new_state(NFA_SPLIT, NULL, e.start);
2284 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002285 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002286 PUSH(frag(s, append(e.out, list1(&s->out))));
2287 break;
2288
2289 case NFA_PLUS:
2290 /* One or more */
2291 if (nfa_calc_size == TRUE)
2292 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002293 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002294 break;
2295 }
2296 e = POP();
2297 s = new_state(NFA_SPLIT, e.start, NULL);
2298 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002299 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300 patch(e.out, s);
2301 PUSH(frag(e.start, list1(&s->out1)));
2302 break;
2303
2304 case NFA_SKIP_CHAR:
2305 /* Symbol of 0-length, Used in a repetition
2306 * with max/min count of 0 */
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 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2313 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002314 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002315 PUSH(frag(s, list1(&s->out)));
2316 break;
2317
2318 case NFA_PREV_ATOM_NO_WIDTH:
2319 /* The \@= operator: match the preceding atom with 0 width.
2320 * Surrounds the preceding atom with START_INVISIBLE and
2321 * END_INVISIBLE, similarly to MOPEN.
2322 */
2323 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002324 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325
2326 if (nfa_calc_size == TRUE)
2327 {
2328 nstate += 2;
2329 break;
2330 }
2331 e = POP();
2332 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2333 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002334 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002335 patch(e.out, s1);
2336
2337 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2338 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002339 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340 PUSH(frag(s, list1(&s1->out)));
2341 break;
2342
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002343#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002344 case NFA_COMPOSING: /* char with composing char */
2345#if 0
2346 /* TODO */
2347 if (regflags & RF_ICOMBINE)
2348 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002349 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002350 }
2351#endif
2352 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002353#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002354
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 case NFA_MOPEN + 0: /* Submatch */
2356 case NFA_MOPEN + 1:
2357 case NFA_MOPEN + 2:
2358 case NFA_MOPEN + 3:
2359 case NFA_MOPEN + 4:
2360 case NFA_MOPEN + 5:
2361 case NFA_MOPEN + 6:
2362 case NFA_MOPEN + 7:
2363 case NFA_MOPEN + 8:
2364 case NFA_MOPEN + 9:
2365 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002366 if (nfa_calc_size == TRUE)
2367 {
2368 nstate += 2;
2369 break;
2370 }
2371
2372 mopen = *p;
2373 switch (*p)
2374 {
2375 case NFA_NOPEN:
2376 mclose = NFA_NCLOSE;
2377 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002378#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379 case NFA_COMPOSING:
2380 mclose = NFA_END_COMPOSING;
2381 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002382#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383 default:
2384 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2385 mclose = *p + NSUBEXP;
2386 break;
2387 }
2388
2389 /* Allow "NFA_MOPEN" as a valid postfix representation for
2390 * the empty regexp "". In this case, the NFA will be
2391 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2392 * empty groups of parenthesis, and empty mbyte chars */
2393 if (stackp == stack)
2394 {
2395 s = new_state(mopen, NULL, NULL);
2396 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002397 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398 s1 = new_state(mclose, NULL, NULL);
2399 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002400 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002401 patch(list1(&s->out), s1);
2402 PUSH(frag(s, list1(&s1->out)));
2403 break;
2404 }
2405
2406 /* At least one node was emitted before NFA_MOPEN, so
2407 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2408 e = POP();
2409 s = new_state(mopen, e.start, NULL); /* `(' */
2410 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002411 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412
2413 s1 = new_state(mclose, NULL, NULL); /* `)' */
2414 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002415 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002416 patch(e.out, s1);
2417
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002418#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002419 if (mopen == NFA_COMPOSING)
2420 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002422#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002423
2424 PUSH(frag(s, list1(&s1->out)));
2425 break;
2426
2427 case NFA_ZSTART:
2428 case NFA_ZEND:
2429 default:
2430 /* Operands */
2431 if (nfa_calc_size == TRUE)
2432 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002433 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002434 break;
2435 }
2436 s = new_state(*p, NULL, NULL);
2437 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002438 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 PUSH(frag(s, list1(&s->out)));
2440 break;
2441
2442 } /* switch(*p) */
2443
2444 } /* for(p = postfix; *p; ++p) */
2445
2446 if (nfa_calc_size == TRUE)
2447 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002448 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002449 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450 }
2451
2452 e = POP();
2453 if (stackp != stack)
2454 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2455
2456 if (istate >= nstate)
2457 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2458
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 matchstate = &state_ptr[istate++]; /* the match state */
2460 matchstate->c = NFA_MATCH;
2461 matchstate->out = matchstate->out1 = NULL;
2462
2463 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002464 ret = e.start;
2465
2466theend:
2467 vim_free(stack);
2468 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469
2470#undef POP1
2471#undef PUSH1
2472#undef POP2
2473#undef PUSH2
2474#undef POP
2475#undef PUSH
2476}
2477
2478/****************************************************************
2479 * NFA execution code.
2480 ****************************************************************/
2481
Bram Moolenaar4b417062013-05-25 20:19:50 +02002482/* nfa_thread_T contains runtime information of a NFA state */
2483typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002484{
2485 nfa_state_T *state;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002486 regsub_T sub; /* Submatch info. TODO: expensive! */
2487} nfa_thread_T;
2488
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002489
2490typedef struct
2491{
Bram Moolenaar4b417062013-05-25 20:19:50 +02002492 nfa_thread_T *t;
2493 int n;
2494} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002495
Bram Moolenaar4b417062013-05-25 20:19:50 +02002496static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int off, int lid, int *match));
2497
2498static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int lid, int *match, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499
2500 static void
2501addstate(l, state, m, off, lid, match)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002502 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503 nfa_state_T *state; /* state to update */
2504 regsub_T *m; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002505 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 int lid;
2507 int *match; /* found match? */
2508{
2509 regsub_T save;
2510 int subidx = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002511 nfa_thread_T *lastthread;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002512
2513 if (l == NULL || state == NULL)
2514 return;
2515
2516 switch (state->c)
2517 {
2518 case NFA_SPLIT:
2519 case NFA_NOT:
2520 case NFA_NOPEN:
2521 case NFA_NCLOSE:
2522 case NFA_MCLOSE:
2523 case NFA_MCLOSE + 1:
2524 case NFA_MCLOSE + 2:
2525 case NFA_MCLOSE + 3:
2526 case NFA_MCLOSE + 4:
2527 case NFA_MCLOSE + 5:
2528 case NFA_MCLOSE + 6:
2529 case NFA_MCLOSE + 7:
2530 case NFA_MCLOSE + 8:
2531 case NFA_MCLOSE + 9:
2532 /* Do not remember these nodes in list "thislist" or "nextlist" */
2533 break;
2534
2535 default:
2536 if (state->lastlist == lid)
2537 {
2538 if (++state->visits > 2)
2539 return;
2540 }
2541 else
2542 {
2543 /* add the state to the list */
2544 state->lastlist = lid;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002545 lastthread = &l->t[l->n++];
2546 lastthread->state = state;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002547 lastthread->sub = *m; /* TODO: expensive! */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 }
2549 }
2550
2551#ifdef ENABLE_LOG
2552 nfa_set_code(state->c);
2553 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n",
2554 abs(state->id), code, state->c);
2555#endif
2556 switch (state->c)
2557 {
2558 case NFA_MATCH:
2559 *match = TRUE;
2560 break;
2561
2562 case NFA_SPLIT:
2563 addstate(l, state->out, m, off, lid, match);
2564 addstate(l, state->out1, m, off, lid, match);
2565 break;
2566
2567 case NFA_SKIP_CHAR:
2568 addstate(l, state->out, m, off, lid, match);
2569 break;
2570
2571#if 0
2572 case NFA_END_NEG_RANGE:
2573 /* Nothing to handle here. nfa_regmatch() will take care of it */
2574 break;
2575
2576 case NFA_NOT:
2577 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2578#ifdef ENABLE_LOG
2579 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2580#endif
2581 break;
2582
2583 case NFA_COMPOSING:
2584 /* nfa_regmatch() will match all the bytes of this composing char. */
2585 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002586#endif
2587
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002588 case NFA_NOPEN:
2589 case NFA_NCLOSE:
2590 addstate(l, state->out, m, off, lid, match);
2591 break;
2592
2593 /* If this state is reached, then a recursive call of nfa_regmatch()
2594 * succeeded. the next call saves the found submatches in the
2595 * first state after the "invisible" branch. */
2596#if 0
2597 case NFA_END_INVISIBLE:
2598 break;
2599#endif
2600
2601 case NFA_MOPEN + 0:
2602 case NFA_MOPEN + 1:
2603 case NFA_MOPEN + 2:
2604 case NFA_MOPEN + 3:
2605 case NFA_MOPEN + 4:
2606 case NFA_MOPEN + 5:
2607 case NFA_MOPEN + 6:
2608 case NFA_MOPEN + 7:
2609 case NFA_MOPEN + 8:
2610 case NFA_MOPEN + 9:
2611 case NFA_ZSTART:
2612 subidx = state->c - NFA_MOPEN;
2613 if (state->c == NFA_ZSTART)
2614 subidx = 0;
2615
2616 if (REG_MULTI)
2617 {
2618 save.startpos[subidx] = m->startpos[subidx];
2619 save.endpos[subidx] = m->endpos[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02002620 if (off == -1)
2621 {
2622 m->startpos[subidx].lnum = reglnum + 1;
2623 m->startpos[subidx].col = 0;
2624 }
2625 else
2626 {
2627 m->startpos[subidx].lnum = reglnum;
2628 m->startpos[subidx].col =
2629 (colnr_T)(reginput - regline + off);
2630 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002631 }
2632 else
2633 {
2634 save.start[subidx] = m->start[subidx];
2635 save.end[subidx] = m->end[subidx];
2636 m->start[subidx] = reginput + off;
2637 }
2638
2639 addstate(l, state->out, m, off, lid, match);
2640
2641 if (REG_MULTI)
2642 {
2643 m->startpos[subidx] = save.startpos[subidx];
2644 m->endpos[subidx] = save.endpos[subidx];
2645 }
2646 else
2647 {
2648 m->start[subidx] = save.start[subidx];
2649 m->end[subidx] = save.end[subidx];
2650 }
2651 break;
2652
2653 case NFA_MCLOSE + 0:
2654 if (nfa_has_zend == TRUE)
2655 {
2656 addstate(l, state->out, m, off, lid, match);
2657 break;
2658 }
2659 case NFA_MCLOSE + 1:
2660 case NFA_MCLOSE + 2:
2661 case NFA_MCLOSE + 3:
2662 case NFA_MCLOSE + 4:
2663 case NFA_MCLOSE + 5:
2664 case NFA_MCLOSE + 6:
2665 case NFA_MCLOSE + 7:
2666 case NFA_MCLOSE + 8:
2667 case NFA_MCLOSE + 9:
2668 case NFA_ZEND:
2669 subidx = state->c - NFA_MCLOSE;
2670 if (state->c == NFA_ZEND)
2671 subidx = 0;
2672
2673 if (REG_MULTI)
2674 {
2675 save.startpos[subidx] = m->startpos[subidx];
2676 save.endpos[subidx] = m->endpos[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02002677 if (off == -1)
2678 {
2679 m->endpos[subidx].lnum = reglnum + 1;
2680 m->endpos[subidx].col = 0;
2681 }
2682 else
2683 {
2684 m->endpos[subidx].lnum = reglnum;
2685 m->endpos[subidx].col = (colnr_T)(reginput - regline + off);
2686 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002687 }
2688 else
2689 {
2690 save.start[subidx] = m->start[subidx];
2691 save.end[subidx] = m->end[subidx];
2692 m->end[subidx] = reginput + off;
2693 }
2694
2695 addstate(l, state->out, m, off, lid, match);
2696
2697 if (REG_MULTI)
2698 {
2699 m->startpos[subidx] = save.startpos[subidx];
2700 m->endpos[subidx] = save.endpos[subidx];
2701 }
2702 else
2703 {
2704 m->start[subidx] = save.start[subidx];
2705 m->end[subidx] = save.end[subidx];
2706 }
2707 break;
2708 }
2709}
2710
2711/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002712 * Like addstate(), but the new state(s) are put at position "*ip".
2713 * Used for zero-width matches, next state to use is the added one.
2714 * This makes sure the order of states to be tried does not change, which
2715 * matters for alternatives.
2716 */
2717 static void
2718addstate_here(l, state, m, lid, matchp, ip)
2719 nfa_list_T *l; /* runtime state list */
2720 nfa_state_T *state; /* state to update */
2721 regsub_T *m; /* pointers to subexpressions */
2722 int lid;
2723 int *matchp; /* found match? */
2724 int *ip;
2725{
2726 int tlen = l->n;
2727 int count;
2728 int i = *ip;
2729
2730 /* first add the state(s) at the end, so that we know how many there are */
2731 addstate(l, state, m, 0, lid, matchp);
2732
2733 /* when "*ip" was at the end of the list, nothing to do */
2734 if (i + 1 == tlen)
2735 return;
2736
2737 /* re-order to put the new state at the current position */
2738 count = l->n - tlen;
2739 if (count > 1)
2740 {
2741 /* make space for new states, then move them from the
2742 * end to the current position */
2743 mch_memmove(&(l->t[i + count]),
2744 &(l->t[i + 1]),
2745 sizeof(nfa_thread_T) * (l->n - i - 1));
2746 mch_memmove(&(l->t[i]),
2747 &(l->t[l->n - 1]),
2748 sizeof(nfa_thread_T) * count);
2749 }
2750 else
2751 {
2752 /* overwrite the current state */
2753 l->t[i] = l->t[l->n - 1];
2754 }
2755 --l->n;
2756 *ip = i - 1;
2757}
2758
2759/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002760 * Check character class "class" against current character c.
2761 */
2762 static int
2763check_char_class(class, c)
2764 int class;
2765 int c;
2766{
2767 switch (class)
2768 {
2769 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002770 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002771 return OK;
2772 break;
2773 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002774 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002775 return OK;
2776 break;
2777 case NFA_CLASS_BLANK:
2778 if (c == ' ' || c == '\t')
2779 return OK;
2780 break;
2781 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002782 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002783 return OK;
2784 break;
2785 case NFA_CLASS_DIGIT:
2786 if (VIM_ISDIGIT(c))
2787 return OK;
2788 break;
2789 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002790 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002791 return OK;
2792 break;
2793 case NFA_CLASS_LOWER:
2794 if (MB_ISLOWER(c))
2795 return OK;
2796 break;
2797 case NFA_CLASS_PRINT:
2798 if (vim_isprintc(c))
2799 return OK;
2800 break;
2801 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002802 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002803 return OK;
2804 break;
2805 case NFA_CLASS_SPACE:
2806 if ((c >=9 && c <= 13) || (c == ' '))
2807 return OK;
2808 break;
2809 case NFA_CLASS_UPPER:
2810 if (MB_ISUPPER(c))
2811 return OK;
2812 break;
2813 case NFA_CLASS_XDIGIT:
2814 if (vim_isxdigit(c))
2815 return OK;
2816 break;
2817 case NFA_CLASS_TAB:
2818 if (c == '\t')
2819 return OK;
2820 break;
2821 case NFA_CLASS_RETURN:
2822 if (c == '\r')
2823 return OK;
2824 break;
2825 case NFA_CLASS_BACKSPACE:
2826 if (c == '\b')
2827 return OK;
2828 break;
2829 case NFA_CLASS_ESCAPE:
2830 if (c == '\033')
2831 return OK;
2832 break;
2833
2834 default:
2835 /* should not be here :P */
2836 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2837 }
2838 return FAIL;
2839}
2840
2841/*
2842 * Set all NFA nodes' list ID equal to -1.
2843 */
2844 static void
2845nfa_set_neg_listids(start)
2846 nfa_state_T *start;
2847{
2848 if (start == NULL)
2849 return;
2850 if (start->lastlist >= 0)
2851 {
2852 start->lastlist = -1;
2853 nfa_set_neg_listids(start->out);
2854 nfa_set_neg_listids(start->out1);
2855 }
2856}
2857
2858/*
2859 * Set all NFA nodes' list ID equal to 0.
2860 */
2861 static void
2862nfa_set_null_listids(start)
2863 nfa_state_T *start;
2864{
2865 if (start == NULL)
2866 return;
2867 if (start->lastlist == -1)
2868 {
2869 start->lastlist = 0;
2870 nfa_set_null_listids(start->out);
2871 nfa_set_null_listids(start->out1);
2872 }
2873}
2874
2875/*
2876 * Save list IDs for all NFA states in "list".
2877 */
2878 static void
2879nfa_save_listids(start, list)
2880 nfa_state_T *start;
2881 int *list;
2882{
2883 if (start == NULL)
2884 return;
2885 if (start->lastlist != -1)
2886 {
2887 list[abs(start->id)] = start->lastlist;
2888 start->lastlist = -1;
2889 nfa_save_listids(start->out, list);
2890 nfa_save_listids(start->out1, list);
2891 }
2892}
2893
2894/*
2895 * Restore list IDs from "list" to all NFA states.
2896 */
2897 static void
2898nfa_restore_listids(start, list)
2899 nfa_state_T *start;
2900 int *list;
2901{
2902 if (start == NULL)
2903 return;
2904 if (start->lastlist == -1)
2905 {
2906 start->lastlist = list[abs(start->id)];
2907 nfa_restore_listids(start->out, list);
2908 nfa_restore_listids(start->out1, list);
2909 }
2910}
2911
2912/*
2913 * Main matching routine.
2914 *
2915 * Run NFA to determine whether it matches reginput.
2916 *
2917 * Return TRUE if there is a match, FALSE otherwise.
2918 * Note: Caller must ensure that: start != NULL.
2919 */
2920 static int
2921nfa_regmatch(start, submatch, m)
2922 nfa_state_T *start;
2923 regsub_T *submatch;
2924 regsub_T *m;
2925{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 int result;
2927 int size = 0;
2928 int match = FALSE;
2929 int flag = 0;
2930 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002931 int go_to_nextline = FALSE;
2932 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002933 char_u *old_reginput = NULL;
2934 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002935 nfa_list_T list[3];
2936 nfa_list_T *listtbl[2][2];
2937 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02002939 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002940 nfa_list_T *thislist;
2941 nfa_list_T *nextlist;
2942 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002943 int *listids = NULL;
2944 int j = 0;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02002945#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002946 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002947
2948 if (debug == NULL)
2949 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002950 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002951 return FALSE;
2952 }
2953#endif
2954
2955 /* Allocate memory for the lists of nodes */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002956 size = (nstate + 1) * sizeof(nfa_thread_T);
2957 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
2958 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
2959 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002960 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
2961 goto theend;
2962 vim_memset(list[0].t, 0, size);
2963 vim_memset(list[1].t, 0, size);
2964 vim_memset(list[2].t, 0, size);
2965
2966#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002967 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002968 if (log_fd != NULL)
2969 {
2970 fprintf(log_fd, "**********************************\n");
2971 nfa_set_code(start->c);
2972 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
2973 abs(start->id), code);
2974 fprintf(log_fd, "**********************************\n");
2975 }
2976 else
2977 {
2978 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
2979 log_fd = stderr;
2980 }
2981#endif
2982
2983 thislist = &list[0];
2984 thislist->n = 0;
2985 nextlist = &list[1];
2986 nextlist->n = 0;
2987 neglist = &list[2];
2988 neglist->n = 0;
2989#ifdef ENABLE_LOG
2990 fprintf(log_fd, "(---) STARTSTATE\n");
2991#endif
2992 addstate(thislist, start, m, 0, listid, &match);
2993
2994 /* There are two cases when the NFA advances: 1. input char matches the
2995 * NFA node and 2. input char does not match the NFA node, but the next
2996 * node is NFA_NOT. The following macro calls addstate() according to
2997 * these rules. It is used A LOT, so use the "listtbl" table for speed */
2998 listtbl[0][0] = NULL;
2999 listtbl[0][1] = neglist;
3000 listtbl[1][0] = nextlist;
3001 listtbl[1][1] = NULL;
3002#define ADD_POS_NEG_STATE(node) \
3003 ll = listtbl[result ? 1 : 0][node->negated]; \
3004 if (ll != NULL) \
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003005 addstate(ll, node->out , &t->sub, clen, listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003006
3007
3008 /*
3009 * Run for each character.
3010 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003011 for (;;)
3012 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003013 int curc;
3014 int clen;
3015
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003016#ifdef FEAT_MBYTE
3017 if (has_mbyte)
3018 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003019 curc = (*mb_ptr2char)(reginput);
3020 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003021 }
3022 else
3023#endif
3024 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003025 curc = *reginput;
3026 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003027 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003028 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003029 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003030 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003031 go_to_nextline = FALSE;
3032 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003033
3034 /* swap lists */
3035 thislist = &list[flag];
3036 nextlist = &list[flag ^= 1];
3037 nextlist->n = 0; /* `clear' nextlist */
3038 listtbl[1][0] = nextlist;
3039 ++listid;
3040
3041#ifdef ENABLE_LOG
3042 fprintf(log_fd, "------------------------------------------\n");
3043 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003044 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003046 {
3047 int i;
3048
3049 for (i = 0; i < thislist->n; i++)
3050 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3051 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003052 fprintf(log_fd, "\n");
3053#endif
3054
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003055#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003056 fprintf(debug, "\n-------------------\n");
3057#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003058 /*
3059 * If the state lists are empty we can stop.
3060 */
3061 if (thislist->n == 0 && neglist->n == 0)
3062 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003063
3064 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003065 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003066 {
3067 if (neglist->n > 0)
3068 {
3069 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003070 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003071 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003072 }
3073 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003074 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003075
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003076#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003077 nfa_set_code(t->state->c);
3078 fprintf(debug, "%s, ", code);
3079#endif
3080#ifdef ENABLE_LOG
3081 nfa_set_code(t->state->c);
3082 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3083 code, (int)t->state->c);
3084#endif
3085
3086 /*
3087 * Handle the possible codes of the current state.
3088 * The most important is NFA_MATCH.
3089 */
3090 switch (t->state->c)
3091 {
3092 case NFA_MATCH:
3093 match = TRUE;
3094 *submatch = t->sub;
3095#ifdef ENABLE_LOG
3096 for (j = 0; j < 4; j++)
3097 if (REG_MULTI)
3098 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
3099 j,
3100 t->sub.startpos[j].col,
3101 (int)t->sub.startpos[j].lnum,
3102 t->sub.endpos[j].col,
3103 (int)t->sub.endpos[j].lnum);
3104 else
3105 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
3106 j,
3107 (char *)t->sub.start[j],
3108 (char *)t->sub.end[j]);
3109 fprintf(log_fd, "\n");
3110#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003111 /* Found the left-most longest match, do not look at any other
3112 * states at this position. */
3113 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114
3115 case NFA_END_INVISIBLE:
3116 /* This is only encountered after a NFA_START_INVISIBLE node.
3117 * They surround a zero-width group, used with "\@=" and "\&".
3118 * If we got here, it means that the current "invisible" group
3119 * finished successfully, so return control to the parent
3120 * nfa_regmatch(). Submatches are stored in *m, and used in
3121 * the parent call. */
3122 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003123 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003124 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003125 else
3126 {
3127 *m = t->sub;
3128 match = TRUE;
3129 }
3130 break;
3131
3132 case NFA_START_INVISIBLE:
3133 /* Save global variables, and call nfa_regmatch() to check if
3134 * the current concat matches at this position. The concat
3135 * ends with the node NFA_END_INVISIBLE */
3136 old_reginput = reginput;
3137 old_regline = regline;
3138 old_reglnum = reglnum;
3139 if (listids == NULL)
3140 {
3141 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3142 if (listids == NULL)
3143 {
3144 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3145 return 0;
3146 }
3147 }
3148#ifdef ENABLE_LOG
3149 if (log_fd != stderr)
3150 fclose(log_fd);
3151 log_fd = NULL;
3152#endif
3153 /* Have to clear the listid field of the NFA nodes, so that
3154 * nfa_regmatch() and addstate() can run properly after
3155 * recursion. */
3156 nfa_save_listids(start, listids);
3157 nfa_set_null_listids(start);
3158 result = nfa_regmatch(t->state->out, submatch, m);
3159 nfa_set_neg_listids(start);
3160 nfa_restore_listids(start, listids);
3161
3162#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003163 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003164 if (log_fd != NULL)
3165 {
3166 fprintf(log_fd, "****************************\n");
3167 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3168 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3169 fprintf(log_fd, "****************************\n");
3170 }
3171 else
3172 {
3173 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3174 log_fd = stderr;
3175 }
3176#endif
3177 if (result == TRUE)
3178 {
3179 /* Restore position in input text */
3180 reginput = old_reginput;
3181 regline = old_regline;
3182 reglnum = old_reglnum;
3183 /* Copy submatch info from the recursive call */
3184 if (REG_MULTI)
3185 for (j = 1; j < NSUBEXP; j++)
3186 {
3187 t->sub.startpos[j] = m->startpos[j];
3188 t->sub.endpos[j] = m->endpos[j];
3189 }
3190 else
3191 for (j = 1; j < NSUBEXP; j++)
3192 {
3193 t->sub.start[j] = m->start[j];
3194 t->sub.end[j] = m->end[j];
3195 }
3196 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003197 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003198 listid, &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003199 }
3200 else
3201 {
3202 /* continue with next input char */
3203 reginput = old_reginput;
3204 }
3205 break;
3206
3207 case NFA_BOL:
3208 if (reginput == regline)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003209 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003210 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 break;
3212
3213 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003214 if (curc == NUL)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003215 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003216 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217 break;
3218
3219 case NFA_BOW:
3220 {
3221 int bow = TRUE;
3222
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003223 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003224 bow = FALSE;
3225#ifdef FEAT_MBYTE
3226 else if (has_mbyte)
3227 {
3228 int this_class;
3229
3230 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003231 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232 if (this_class <= 1)
3233 bow = FALSE;
3234 else if (reg_prev_class() == this_class)
3235 bow = FALSE;
3236 }
3237#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003238 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003239 || (reginput > regline
3240 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003241 bow = FALSE;
3242 if (bow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003243 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003244 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245 break;
3246 }
3247
3248 case NFA_EOW:
3249 {
3250 int eow = TRUE;
3251
3252 if (reginput == regline)
3253 eow = FALSE;
3254#ifdef FEAT_MBYTE
3255 else if (has_mbyte)
3256 {
3257 int this_class, prev_class;
3258
3259 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003260 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 prev_class = reg_prev_class();
3262 if (this_class == prev_class
3263 || prev_class == 0 || prev_class == 1)
3264 eow = FALSE;
3265 }
3266#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003267 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003268 || (reginput[0] != NUL
3269 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 eow = FALSE;
3271 if (eow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003272 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003273 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003274 break;
3275 }
3276
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003277#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003279 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003280 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003281 int len = 0;
3282 nfa_state_T *end;
3283 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003284 int cchars[MAX_MCO];
3285 int ccount = 0;
3286 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003287
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003289 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003290 if (utf_iscomposing(sta->c))
3291 {
3292 /* Only match composing character(s), ignore base
3293 * character. Used for ".{composing}" and "{composing}"
3294 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003295 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003296 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003297 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003299 /* If \Z was present, then ignore composing characters.
3300 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003301 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003302 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003303 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003304 else
3305 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003306 while (sta->c != NFA_END_COMPOSING)
3307 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003309
3310 /* Check base character matches first, unless ignored. */
3311 else if (len > 0 || mc == sta->c)
3312 {
3313 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003314 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003315 len += mb_char2len(mc);
3316 sta = sta->out;
3317 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003319 /* We don't care about the order of composing characters.
3320 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003321 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003322 {
3323 mc = mb_ptr2char(reginput + len);
3324 cchars[ccount++] = mc;
3325 len += mb_char2len(mc);
3326 if (ccount == MAX_MCO)
3327 break;
3328 }
3329
3330 /* Check that each composing char in the pattern matches a
3331 * composing char in the text. We do not check if all
3332 * composing chars are matched. */
3333 result = OK;
3334 while (sta->c != NFA_END_COMPOSING)
3335 {
3336 for (j = 0; j < ccount; ++j)
3337 if (cchars[j] == sta->c)
3338 break;
3339 if (j == ccount)
3340 {
3341 result = FAIL;
3342 break;
3343 }
3344 sta = sta->out;
3345 }
3346 }
3347 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003348 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003349
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003350 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003351 ADD_POS_NEG_STATE(end);
3352 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003353 }
3354#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003355
3356 case NFA_NEWL:
3357 if (!reg_line_lbr && REG_MULTI
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003358 && curc == NUL && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003359 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003360 go_to_nextline = TRUE;
3361 /* Pass -1 for the offset, which means taking the position
3362 * at the start of the next line. */
3363 addstate(nextlist, t->state->out, &t->sub, -1,
3364 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365 }
3366 break;
3367
3368 case NFA_CLASS_ALNUM:
3369 case NFA_CLASS_ALPHA:
3370 case NFA_CLASS_BLANK:
3371 case NFA_CLASS_CNTRL:
3372 case NFA_CLASS_DIGIT:
3373 case NFA_CLASS_GRAPH:
3374 case NFA_CLASS_LOWER:
3375 case NFA_CLASS_PRINT:
3376 case NFA_CLASS_PUNCT:
3377 case NFA_CLASS_SPACE:
3378 case NFA_CLASS_UPPER:
3379 case NFA_CLASS_XDIGIT:
3380 case NFA_CLASS_TAB:
3381 case NFA_CLASS_RETURN:
3382 case NFA_CLASS_BACKSPACE:
3383 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003384 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385 ADD_POS_NEG_STATE(t->state);
3386 break;
3387
3388 case NFA_END_NEG_RANGE:
3389 /* This follows a series of negated nodes, like:
3390 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003391 if (curc > 0)
3392 addstate(nextlist, t->state->out, &t->sub, clen,
3393 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003394 break;
3395
3396 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003397 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003398 if (curc > 0)
3399 addstate(nextlist, t->state->out, &t->sub, clen,
3400 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003401 break;
3402
3403 /*
3404 * Character classes like \a for alpha, \d for digit etc.
3405 */
3406 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003407 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003408 ADD_POS_NEG_STATE(t->state);
3409 break;
3410
3411 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003412 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 ADD_POS_NEG_STATE(t->state);
3414 break;
3415
3416 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003417 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 ADD_POS_NEG_STATE(t->state);
3419 break;
3420
3421 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003422 result = !VIM_ISDIGIT(curc)
3423 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 ADD_POS_NEG_STATE(t->state);
3425 break;
3426
3427 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003428 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429 ADD_POS_NEG_STATE(t->state);
3430 break;
3431
3432 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003433 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003434 ADD_POS_NEG_STATE(t->state);
3435 break;
3436
3437 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003438 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439 ADD_POS_NEG_STATE(t->state);
3440 break;
3441
3442 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003443 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 ADD_POS_NEG_STATE(t->state);
3445 break;
3446
3447 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003448 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449 ADD_POS_NEG_STATE(t->state);
3450 break;
3451
3452 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003453 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003454 ADD_POS_NEG_STATE(t->state);
3455 break;
3456
3457 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003458 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 ADD_POS_NEG_STATE(t->state);
3460 break;
3461
3462 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003463 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464 ADD_POS_NEG_STATE(t->state);
3465 break;
3466
3467 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003468 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 ADD_POS_NEG_STATE(t->state);
3470 break;
3471
3472 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003473 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 ADD_POS_NEG_STATE(t->state);
3475 break;
3476
3477 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003478 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 ADD_POS_NEG_STATE(t->state);
3480 break;
3481
3482 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003483 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484 ADD_POS_NEG_STATE(t->state);
3485 break;
3486
3487 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003488 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 ADD_POS_NEG_STATE(t->state);
3490 break;
3491
3492 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003493 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 ADD_POS_NEG_STATE(t->state);
3495 break;
3496
3497 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003498 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499 ADD_POS_NEG_STATE(t->state);
3500 break;
3501
3502 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003503 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 ADD_POS_NEG_STATE(t->state);
3505 break;
3506
3507 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003508 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509 ADD_POS_NEG_STATE(t->state);
3510 break;
3511
3512 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003513 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 ADD_POS_NEG_STATE(t->state);
3515 break;
3516
3517 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003518 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519 ADD_POS_NEG_STATE(t->state);
3520 break;
3521
3522 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003523 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524 ADD_POS_NEG_STATE(t->state);
3525 break;
3526
3527 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003528 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529 ADD_POS_NEG_STATE(t->state);
3530 break;
3531
3532 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003533 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003534 ADD_POS_NEG_STATE(t->state);
3535 break;
3536
Bram Moolenaar12e40142013-05-21 15:33:41 +02003537 case NFA_MOPEN + 0:
3538 case NFA_MOPEN + 1:
3539 case NFA_MOPEN + 2:
3540 case NFA_MOPEN + 3:
3541 case NFA_MOPEN + 4:
3542 case NFA_MOPEN + 5:
3543 case NFA_MOPEN + 6:
3544 case NFA_MOPEN + 7:
3545 case NFA_MOPEN + 8:
3546 case NFA_MOPEN + 9:
3547 /* handled below */
3548 break;
3549
3550 case NFA_SKIP_CHAR:
3551 case NFA_ZSTART:
3552 /* TODO: should not happen? */
3553 break;
3554
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 default: /* regular character */
Bram Moolenaar12e40142013-05-21 15:33:41 +02003556 /* TODO: put this in #ifdef later */
3557 if (t->state->c < -256)
3558 EMSGN("INTERNAL: Negative state char: %ld", t->state->c);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003559 result = (no_Magic(t->state->c) == curc);
Bram Moolenaar12e40142013-05-21 15:33:41 +02003560
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003561 if (!result)
3562 result = ireg_ic == TRUE
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003563 && MB_TOLOWER(t->state->c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003564#ifdef FEAT_MBYTE
3565 /* If there is a composing character which is not being
3566 * ignored there can be no match. Match with composing
3567 * character uses NFA_COMPOSING above. */
3568 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003569 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003570 result = FALSE;
3571#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003572 ADD_POS_NEG_STATE(t->state);
3573 break;
3574 }
3575
3576 } /* for (thislist = thislist; thislist->state; thislist++) */
3577
3578 /* The first found match is the leftmost one, but there may be a
3579 * longer one. Keep running the NFA, but don't start from the
3580 * beginning. Also, do not add the start state in recursive calls of
3581 * nfa_regmatch(), because recursive calls should only start in the
3582 * first position. */
3583 if (match == FALSE && start->c == NFA_MOPEN + 0)
3584 {
3585#ifdef ENABLE_LOG
3586 fprintf(log_fd, "(---) STARTSTATE\n");
3587#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003588 addstate(nextlist, start, m, clen, listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003589 }
3590
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003591#ifdef ENABLE_LOG
3592 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003593 {
3594 int i;
3595
3596 for (i = 0; i < thislist->n; i++)
3597 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3598 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 fprintf(log_fd, "\n");
3600#endif
3601
3602nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003603 /* Advance to the next character, or advance to the next line, or
3604 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003605 if (clen != 0)
3606 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003607 else if (go_to_nextline)
3608 reg_nextline();
3609 else
3610 break;
3611 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003612
3613#ifdef ENABLE_LOG
3614 if (log_fd != stderr)
3615 fclose(log_fd);
3616 log_fd = NULL;
3617#endif
3618
3619theend:
3620 /* Free memory */
3621 vim_free(list[0].t);
3622 vim_free(list[1].t);
3623 vim_free(list[2].t);
3624 list[0].t = list[1].t = list[2].t = NULL;
3625 if (listids != NULL)
3626 vim_free(listids);
3627#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003628#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003629 fclose(debug);
3630#endif
3631
3632 return match;
3633}
3634
3635/*
3636 * Try match of "prog" with at regline["col"].
3637 * Returns 0 for failure, number of lines contained in the match otherwise.
3638 */
3639 static long
3640nfa_regtry(start, col)
3641 nfa_state_T *start;
3642 colnr_T col;
3643{
3644 int i;
3645 regsub_T sub, m;
3646#ifdef ENABLE_LOG
3647 FILE *f;
3648#endif
3649
3650 reginput = regline + col;
3651 need_clear_subexpr = TRUE;
3652
3653#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003654 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655 if (f != NULL)
3656 {
3657 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3658 fprintf(f, " =======================================================\n");
3659#ifdef DEBUG
3660 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3661#endif
3662 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3663 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003664 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003665 fprintf(f, "\n\n");
3666 fclose(f);
3667 }
3668 else
3669 EMSG(_("Could not open temporary log file for writing "));
3670#endif
3671
3672 if (REG_MULTI)
3673 {
3674 /* Use 0xff to set lnum to -1 */
3675 vim_memset(sub.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3676 vim_memset(sub.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3677 vim_memset(m.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3678 vim_memset(m.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3679 }
3680 else
3681 {
3682 vim_memset(sub.start, 0, sizeof(char_u *) * NSUBEXP);
3683 vim_memset(sub.end, 0, sizeof(char_u *) * NSUBEXP);
3684 vim_memset(m.start, 0, sizeof(char_u *) * NSUBEXP);
3685 vim_memset(m.end, 0, sizeof(char_u *) * NSUBEXP);
3686 }
3687
3688 if (nfa_regmatch(start, &sub, &m) == FALSE)
3689 return 0;
3690
3691 cleanup_subexpr();
3692 if (REG_MULTI)
3693 {
3694 for (i = 0; i < NSUBEXP; i++)
3695 {
3696 reg_startpos[i] = sub.startpos[i];
3697 reg_endpos[i] = sub.endpos[i];
3698 }
3699
3700 if (reg_startpos[0].lnum < 0)
3701 {
3702 reg_startpos[0].lnum = 0;
3703 reg_startpos[0].col = col;
3704 }
3705 if (reg_endpos[0].lnum < 0)
3706 {
3707 reg_endpos[0].lnum = reglnum;
3708 reg_endpos[0].col = (int)(reginput - regline);
3709 }
3710 else
3711 /* Use line number of "\ze". */
3712 reglnum = reg_endpos[0].lnum;
3713 }
3714 else
3715 {
3716 for (i = 0; i < NSUBEXP; i++)
3717 {
3718 reg_startp[i] = sub.start[i];
3719 reg_endp[i] = sub.end[i];
3720 }
3721
3722 if (reg_startp[0] == NULL)
3723 reg_startp[0] = regline + col;
3724 if (reg_endp[0] == NULL)
3725 reg_endp[0] = reginput;
3726 }
3727
3728 return 1 + reglnum;
3729}
3730
3731/*
3732 * Match a regexp against a string ("line" points to the string) or multiple
3733 * lines ("line" is NULL, use reg_getline()).
3734 *
3735 * Returns 0 for failure, number of lines contained in the match otherwise.
3736 */
3737 static long
3738nfa_regexec_both(line, col)
3739 char_u *line;
3740 colnr_T col; /* column to start looking for match */
3741{
3742 nfa_regprog_T *prog;
3743 long retval = 0L;
3744 int i;
3745
3746 if (REG_MULTI)
3747 {
3748 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3749 line = reg_getline((linenr_T)0); /* relative to the cursor */
3750 reg_startpos = reg_mmatch->startpos;
3751 reg_endpos = reg_mmatch->endpos;
3752 }
3753 else
3754 {
3755 prog = (nfa_regprog_T *)reg_match->regprog;
3756 reg_startp = reg_match->startp;
3757 reg_endp = reg_match->endp;
3758 }
3759
3760 /* Be paranoid... */
3761 if (prog == NULL || line == NULL)
3762 {
3763 EMSG(_(e_null));
3764 goto theend;
3765 }
3766
3767 /* If the start column is past the maximum column: no need to try. */
3768 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3769 goto theend;
3770
3771 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3772 if (prog->regflags & RF_ICASE)
3773 ireg_ic = TRUE;
3774 else if (prog->regflags & RF_NOICASE)
3775 ireg_ic = FALSE;
3776
3777#ifdef FEAT_MBYTE
3778 /* If pattern contains "\Z" overrule value of ireg_icombine */
3779 if (prog->regflags & RF_ICOMBINE)
3780 ireg_icombine = TRUE;
3781#endif
3782
3783 regline = line;
3784 reglnum = 0; /* relative to line */
3785
3786 nstate = prog->nstate;
3787
3788 for (i = 0; i < nstate; ++i)
3789 {
3790 prog->state[i].id = i;
3791 prog->state[i].lastlist = 0;
3792 prog->state[i].visits = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003793 }
3794
3795 retval = nfa_regtry(prog->start, col);
3796
3797theend:
3798 return retval;
3799}
3800
3801/*
3802 * Compile a regular expression into internal code for the NFA matcher.
3803 * Returns the program in allocated space. Returns NULL for an error.
3804 */
3805 static regprog_T *
3806nfa_regcomp(expr, re_flags)
3807 char_u *expr;
3808 int re_flags;
3809{
Bram Moolenaaraae48832013-05-25 21:18:34 +02003810 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003811 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003812 int *postfix;
3813
3814 if (expr == NULL)
3815 return NULL;
3816
3817#ifdef DEBUG
3818 nfa_regengine.expr = expr;
3819#endif
3820
3821 init_class_tab();
3822
3823 if (nfa_regcomp_start(expr, re_flags) == FAIL)
3824 return NULL;
3825
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003826 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02003827 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003828 postfix = re2post();
3829 if (postfix == NULL)
3830 goto fail; /* Cascaded (syntax?) error */
3831
3832 /*
3833 * In order to build the NFA, we parse the input regexp twice:
3834 * 1. first pass to count size (so we can allocate space)
3835 * 2. second to emit code
3836 */
3837#ifdef ENABLE_LOG
3838 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003839 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003840
3841 if (f != NULL)
3842 {
3843 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
3844 fclose(f);
3845 }
3846 }
3847#endif
3848
3849 /*
3850 * PASS 1
3851 * Count number of NFA states in "nstate". Do not build the NFA.
3852 */
3853 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02003854
3855 /* Space for compiled regexp */
3856 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
3857 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
3858 if (prog == NULL)
3859 goto fail;
3860 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003861 state_ptr = prog->state;
3862
3863 /*
3864 * PASS 2
3865 * Build the NFA
3866 */
3867 prog->start = post2nfa(postfix, post_ptr, FALSE);
3868 if (prog->start == NULL)
3869 goto fail;
3870
3871 prog->regflags = regflags;
3872 prog->engine = &nfa_regengine;
3873 prog->nstate = nstate;
3874#ifdef ENABLE_LOG
3875 nfa_postfix_dump(expr, OK);
3876 nfa_dump(prog);
3877#endif
3878
3879out:
3880 vim_free(post_start);
3881 post_start = post_ptr = post_end = NULL;
3882 state_ptr = NULL;
3883 return (regprog_T *)prog;
3884
3885fail:
3886 vim_free(prog);
3887 prog = NULL;
3888#ifdef ENABLE_LOG
3889 nfa_postfix_dump(expr, FAIL);
3890#endif
3891#ifdef DEBUG
3892 nfa_regengine.expr = NULL;
3893#endif
3894 goto out;
3895}
3896
3897
3898/*
3899 * Match a regexp against a string.
3900 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
3901 * Uses curbuf for line count and 'iskeyword'.
3902 *
3903 * Return TRUE if there is a match, FALSE if not.
3904 */
3905 static int
3906nfa_regexec(rmp, line, col)
3907 regmatch_T *rmp;
3908 char_u *line; /* string to match against */
3909 colnr_T col; /* column to start looking for match */
3910{
3911 reg_match = rmp;
3912 reg_mmatch = NULL;
3913 reg_maxline = 0;
3914 reg_line_lbr = FALSE;
3915 reg_buf = curbuf;
3916 reg_win = NULL;
3917 ireg_ic = rmp->rm_ic;
3918#ifdef FEAT_MBYTE
3919 ireg_icombine = FALSE;
3920#endif
3921 ireg_maxcol = 0;
3922 return (nfa_regexec_both(line, col) != 0);
3923}
3924
3925#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3926 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
3927
3928static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3929
3930/*
3931 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
3932 */
3933 static int
3934nfa_regexec_nl(rmp, line, col)
3935 regmatch_T *rmp;
3936 char_u *line; /* string to match against */
3937 colnr_T col; /* column to start looking for match */
3938{
3939 reg_match = rmp;
3940 reg_mmatch = NULL;
3941 reg_maxline = 0;
3942 reg_line_lbr = TRUE;
3943 reg_buf = curbuf;
3944 reg_win = NULL;
3945 ireg_ic = rmp->rm_ic;
3946#ifdef FEAT_MBYTE
3947 ireg_icombine = FALSE;
3948#endif
3949 ireg_maxcol = 0;
3950 return (nfa_regexec_both(line, col) != 0);
3951}
3952#endif
3953
3954
3955/*
3956 * Match a regexp against multiple lines.
3957 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3958 * Uses curbuf for line count and 'iskeyword'.
3959 *
3960 * Return zero if there is no match. Return number of lines contained in the
3961 * match otherwise.
3962 *
3963 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
3964 *
3965 * ! Also NOTE : match may actually be in another line. e.g.:
3966 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
3967 *
3968 * +-------------------------+
3969 * |a |
3970 * |b |
3971 * |c |
3972 * | |
3973 * +-------------------------+
3974 *
3975 * then nfa_regexec_multi() returns 3. while the original
3976 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
3977 *
3978 * FIXME if this behavior is not compatible.
3979 */
3980 static long
3981nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
3982 regmmatch_T *rmp;
3983 win_T *win; /* window in which to search or NULL */
3984 buf_T *buf; /* buffer in which to search */
3985 linenr_T lnum; /* nr of line to start looking for match */
3986 colnr_T col; /* column to start looking for match */
3987 proftime_T *tm UNUSED; /* timeout limit or NULL */
3988{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003989 reg_match = NULL;
3990 reg_mmatch = rmp;
3991 reg_buf = buf;
3992 reg_win = win;
3993 reg_firstlnum = lnum;
3994 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3995 reg_line_lbr = FALSE;
3996 ireg_ic = rmp->rmm_ic;
3997#ifdef FEAT_MBYTE
3998 ireg_icombine = FALSE;
3999#endif
4000 ireg_maxcol = rmp->rmm_maxcol;
4001
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004002 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004003}
4004
4005#ifdef DEBUG
4006# undef ENABLE_LOG
4007#endif