blob: df5be1100ff0cd92eaa155ee672ff896cd62612c [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
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200168static int nstate; /* Number of states in the NFA. Also used when
169 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200170static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200171
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 Moolenaar61db8b52013-05-26 17:45:49 +0200222 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200223
224 nstate = 0;
225 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200226 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200227 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200228
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200229 /* Some items blow up in size, such as [A-z]. Add more space for that.
230 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200231 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200232
233 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200234 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200235
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236 post_start = (int *)lalloc(postfix_size, TRUE);
237 if (post_start == NULL)
238 return FAIL;
239 vim_memset(post_start, 0, postfix_size);
240 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200241 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200242 nfa_has_zend = FALSE;
243
244 regcomp_start(expr, re_flags);
245
246 return OK;
247}
248
249/*
250 * Search between "start" and "end" and try to recognize a
251 * character class in expanded form. For example [0-9].
252 * On success, return the id the character class to be emitted.
253 * On failure, return 0 (=FAIL)
254 * Start points to the first char of the range, while end should point
255 * to the closing brace.
256 */
257 static int
258nfa_recognize_char_class(start, end, extra_newl)
259 char_u *start;
260 char_u *end;
261 int extra_newl;
262{
263 int i;
264 /* Each of these variables takes up a char in "config[]",
265 * in the order they are here. */
266 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
267 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
268 char_u *p;
269#define NCONFIGS 16
270 int classid[NCONFIGS] = {
271 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
272 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
273 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
274 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
275 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200276 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200277 char_u config[NCONFIGS][9] = {
278 "000000100", /* digit */
279 "100000100", /* non digit */
280 "011000100", /* hex-digit */
281 "111000100", /* non hex-digit */
282 "000001000", /* octal-digit */
283 "100001000", /* [^0-7] */
284 "000110110", /* [0-9A-Za-z_] */
285 "100110110", /* [^0-9A-Za-z_] */
286 "000110010", /* head of word */
287 "100110010", /* not head of word */
288 "000110000", /* alphabetic char a-z */
289 "100110000", /* non alphabetic char */
290 "000100000", /* lowercase letter */
291 "100100000", /* non lowercase */
292 "000010000", /* uppercase */
293 "100010000" /* non uppercase */
294 };
295
296 if (extra_newl == TRUE)
297 newl = TRUE;
298
299 if (*end != ']')
300 return FAIL;
301 p = start;
302 if (*p == '^')
303 {
304 not = TRUE;
305 p ++;
306 }
307
308 while (p < end)
309 {
310 if (p + 2 < end && *(p + 1) == '-')
311 {
312 switch (*p)
313 {
314 case '0':
315 if (*(p + 2) == '9')
316 {
317 o9 = TRUE;
318 break;
319 }
320 else
321 if (*(p + 2) == '7')
322 {
323 o7 = TRUE;
324 break;
325 }
326 case 'a':
327 if (*(p + 2) == 'z')
328 {
329 az = TRUE;
330 break;
331 }
332 else
333 if (*(p + 2) == 'f')
334 {
335 af = TRUE;
336 break;
337 }
338 case 'A':
339 if (*(p + 2) == 'Z')
340 {
341 AZ = TRUE;
342 break;
343 }
344 else
345 if (*(p + 2) == 'F')
346 {
347 AF = TRUE;
348 break;
349 }
350 /* FALLTHROUGH */
351 default:
352 return FAIL;
353 }
354 p += 3;
355 }
356 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
357 {
358 newl = TRUE;
359 p += 2;
360 }
361 else if (*p == '_')
362 {
363 underscore = TRUE;
364 p ++;
365 }
366 else if (*p == '\n')
367 {
368 newl = TRUE;
369 p ++;
370 }
371 else
372 return FAIL;
373 } /* while (p < end) */
374
375 if (p != end)
376 return FAIL;
377
378 /* build the config that represents the ranges we gathered */
379 STRCPY(myconfig, "000000000");
380 if (not == TRUE)
381 myconfig[0] = '1';
382 if (af == TRUE)
383 myconfig[1] = '1';
384 if (AF == TRUE)
385 myconfig[2] = '1';
386 if (az == TRUE)
387 myconfig[3] = '1';
388 if (AZ == TRUE)
389 myconfig[4] = '1';
390 if (o7 == TRUE)
391 myconfig[5] = '1';
392 if (o9 == TRUE)
393 myconfig[6] = '1';
394 if (underscore == TRUE)
395 myconfig[7] = '1';
396 if (newl == TRUE)
397 {
398 myconfig[8] = '1';
399 extra_newl = ADD_NL;
400 }
401 /* try to recognize character classes */
402 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200403 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200404 return classid[i] + extra_newl;
405
406 /* fallthrough => no success so far */
407 return FAIL;
408
409#undef NCONFIGS
410}
411
412/*
413 * Produce the bytes for equivalence class "c".
414 * Currently only handles latin1, latin9 and utf-8.
415 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
416 * equivalent to 'a OR b OR c'
417 *
418 * NOTE! When changing this function, also update reg_equi_class()
419 */
420 static int
421nfa_emit_equi_class(c, neg)
422 int c;
423 int neg;
424{
425 int first = TRUE;
426 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
427#define EMIT2(c) \
428 EMIT(c); \
429 if (neg == TRUE) { \
430 EMIT(NFA_NOT); \
431 } \
432 if (first == FALSE) \
433 EMIT(glue); \
434 else \
435 first = FALSE; \
436
437#ifdef FEAT_MBYTE
438 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
439 || STRCMP(p_enc, "iso-8859-15") == 0)
440#endif
441 {
442 switch (c)
443 {
444 case 'A': case '\300': case '\301': case '\302':
445 case '\303': case '\304': case '\305':
446 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
447 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
448 EMIT2('\305');
449 return OK;
450
451 case 'C': case '\307':
452 EMIT2('C'); EMIT2('\307');
453 return OK;
454
455 case 'E': case '\310': case '\311': case '\312': case '\313':
456 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
457 EMIT2('\312'); EMIT2('\313');
458 return OK;
459
460 case 'I': case '\314': case '\315': case '\316': case '\317':
461 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
462 EMIT2('\316'); EMIT2('\317');
463 return OK;
464
465 case 'N': case '\321':
466 EMIT2('N'); EMIT2('\321');
467 return OK;
468
469 case 'O': case '\322': case '\323': case '\324': case '\325':
470 case '\326':
471 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
472 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
473 return OK;
474
475 case 'U': case '\331': case '\332': case '\333': case '\334':
476 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
477 EMIT2('\333'); EMIT2('\334');
478 return OK;
479
480 case 'Y': case '\335':
481 EMIT2('Y'); EMIT2('\335');
482 return OK;
483
484 case 'a': case '\340': case '\341': case '\342':
485 case '\343': case '\344': case '\345':
486 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
487 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
488 EMIT2('\345');
489 return OK;
490
491 case 'c': case '\347':
492 EMIT2('c'); EMIT2('\347');
493 return OK;
494
495 case 'e': case '\350': case '\351': case '\352': case '\353':
496 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
497 EMIT2('\352'); EMIT2('\353');
498 return OK;
499
500 case 'i': case '\354': case '\355': case '\356': case '\357':
501 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
502 EMIT2('\356'); EMIT2('\357');
503 return OK;
504
505 case 'n': case '\361':
506 EMIT2('n'); EMIT2('\361');
507 return OK;
508
509 case 'o': case '\362': case '\363': case '\364': case '\365':
510 case '\366':
511 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
512 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
513 return OK;
514
515 case 'u': case '\371': case '\372': case '\373': case '\374':
516 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
517 EMIT2('\373'); EMIT2('\374');
518 return OK;
519
520 case 'y': case '\375': case '\377':
521 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
522 return OK;
523
524 default:
525 return FAIL;
526 }
527 }
528
529 EMIT(c);
530 return OK;
531#undef EMIT2
532}
533
534/*
535 * Code to parse regular expression.
536 *
537 * We try to reuse parsing functions in regexp.c to
538 * minimize surprise and keep the syntax consistent.
539 */
540
541/*
542 * Increments the pointer "p" by one (multi-byte) character.
543 */
544 static void
545nfa_inc(p)
546 char_u **p;
547{
548#ifdef FEAT_MBYTE
549 if (has_mbyte)
550 mb_ptr2char_adv(p);
551 else
552#endif
553 *p = *p + 1;
554}
555
556/*
557 * Decrements the pointer "p" by one (multi-byte) character.
558 */
559 static void
560nfa_dec(p)
561 char_u **p;
562{
563#ifdef FEAT_MBYTE
564 char_u *p2, *oldp;
565
566 if (has_mbyte)
567 {
568 oldp = *p;
569 /* Try to find the multibyte char that advances to the current
570 * position. */
571 do
572 {
573 *p = *p - 1;
574 p2 = *p;
575 mb_ptr2char_adv(&p2);
576 } while (p2 != oldp);
577 }
578#else
579 *p = *p - 1;
580#endif
581}
582
583/*
584 * Parse the lowest level.
585 *
586 * An atom can be one of a long list of items. Many atoms match one character
587 * in the text. It is often an ordinary character or a character class.
588 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
589 * is only for syntax highlighting.
590 *
591 * atom ::= ordinary-atom
592 * or \( pattern \)
593 * or \%( pattern \)
594 * or \z( pattern \)
595 */
596 static int
597nfa_regatom()
598{
599 int c;
600 int charclass;
601 int equiclass;
602 int collclass;
603 int got_coll_char;
604 char_u *p;
605 char_u *endp;
606#ifdef FEAT_MBYTE
607 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200608#endif
609 int extra = 0;
610 int first;
611 int emit_range;
612 int negated;
613 int result;
614 int startc = -1;
615 int endc = -1;
616 int oldstartc = -1;
617 int cpo_lit; /* 'cpoptions' contains 'l' flag */
618 int cpo_bsl; /* 'cpoptions' contains '\' flag */
619 int glue; /* ID that will "glue" nodes together */
620
621 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
622 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
623
624 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200625 switch (c)
626 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200627 case NUL:
628 syntax_error = TRUE;
629 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
630
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200631 case Magic('^'):
632 EMIT(NFA_BOL);
633 break;
634
635 case Magic('$'):
636 EMIT(NFA_EOL);
637#if defined(FEAT_SYN_HL) || defined(PROTO)
638 had_eol = TRUE;
639#endif
640 break;
641
642 case Magic('<'):
643 EMIT(NFA_BOW);
644 break;
645
646 case Magic('>'):
647 EMIT(NFA_EOW);
648 break;
649
650 case Magic('_'):
651 c = no_Magic(getchr());
652 if (c == '^') /* "\_^" is start-of-line */
653 {
654 EMIT(NFA_BOL);
655 break;
656 }
657 if (c == '$') /* "\_$" is end-of-line */
658 {
659 EMIT(NFA_EOL);
660#if defined(FEAT_SYN_HL) || defined(PROTO)
661 had_eol = TRUE;
662#endif
663 break;
664 }
665
666 extra = ADD_NL;
667
668 /* "\_[" is collection plus newline */
669 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200670 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200671
672 /* "\_x" is character class plus newline */
673 /*FALLTHROUGH*/
674
675 /*
676 * Character classes.
677 */
678 case Magic('.'):
679 case Magic('i'):
680 case Magic('I'):
681 case Magic('k'):
682 case Magic('K'):
683 case Magic('f'):
684 case Magic('F'):
685 case Magic('p'):
686 case Magic('P'):
687 case Magic('s'):
688 case Magic('S'):
689 case Magic('d'):
690 case Magic('D'):
691 case Magic('x'):
692 case Magic('X'):
693 case Magic('o'):
694 case Magic('O'):
695 case Magic('w'):
696 case Magic('W'):
697 case Magic('h'):
698 case Magic('H'):
699 case Magic('a'):
700 case Magic('A'):
701 case Magic('l'):
702 case Magic('L'):
703 case Magic('u'):
704 case Magic('U'):
705 p = vim_strchr(classchars, no_Magic(c));
706 if (p == NULL)
707 {
708 return FAIL; /* runtime error */
709 }
710#ifdef FEAT_MBYTE
711 /* When '.' is followed by a composing char ignore the dot, so that
712 * the composing char is matched here. */
713 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
714 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200715 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200716 c = getchr();
717 goto nfa_do_multibyte;
718 }
719#endif
720 EMIT(nfa_classcodes[p - classchars]);
721 if (extra == ADD_NL)
722 {
723 EMIT(NFA_NEWL);
724 EMIT(NFA_OR);
725 regflags |= RF_HASNL;
726 }
727 break;
728
729 case Magic('n'):
730 if (reg_string)
731 /* In a string "\n" matches a newline character. */
732 EMIT(NL);
733 else
734 {
735 /* In buffer text "\n" matches the end of a line. */
736 EMIT(NFA_NEWL);
737 regflags |= RF_HASNL;
738 }
739 break;
740
741 case Magic('('):
742 if (nfa_reg(REG_PAREN) == FAIL)
743 return FAIL; /* cascaded error */
744 break;
745
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200746 case Magic('|'):
747 case Magic('&'):
748 case Magic(')'):
749 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200750 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200751 return FAIL;
752
753 case Magic('='):
754 case Magic('?'):
755 case Magic('+'):
756 case Magic('@'):
757 case Magic('*'):
758 case Magic('{'):
759 /* these should follow an atom, not form an atom */
760 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200761 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200762 return FAIL;
763
764 case Magic('~'): /* previous substitute pattern */
765 /* Not supported yet */
766 return FAIL;
767
768 case Magic('1'):
769 case Magic('2'):
770 case Magic('3'):
771 case Magic('4'):
772 case Magic('5'):
773 case Magic('6'):
774 case Magic('7'):
775 case Magic('8'):
776 case Magic('9'):
777 /* not supported yet */
778 return FAIL;
779
780 case Magic('z'):
781 c = no_Magic(getchr());
782 switch (c)
783 {
784 case 's':
785 EMIT(NFA_ZSTART);
786 break;
787 case 'e':
788 EMIT(NFA_ZEND);
789 nfa_has_zend = TRUE;
790 /* TODO: Currently \ze does not work properly. */
791 return FAIL;
792 /* break; */
793 case '1':
794 case '2':
795 case '3':
796 case '4':
797 case '5':
798 case '6':
799 case '7':
800 case '8':
801 case '9':
802 case '(':
803 /* \z1...\z9 and \z( not yet supported */
804 return FAIL;
805 default:
806 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200807 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 no_Magic(c));
809 return FAIL;
810 }
811 break;
812
813 case Magic('%'):
814 c = no_Magic(getchr());
815 switch (c)
816 {
817 /* () without a back reference */
818 case '(':
819 if (nfa_reg(REG_NPAREN) == FAIL)
820 return FAIL;
821 EMIT(NFA_NOPEN);
822 break;
823
824 case 'd': /* %d123 decimal */
825 case 'o': /* %o123 octal */
826 case 'x': /* %xab hex 2 */
827 case 'u': /* %uabcd hex 4 */
828 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200829 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200830 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200831
Bram Moolenaar47196582013-05-25 22:04:23 +0200832 switch (c)
833 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200834 case 'd': nr = getdecchrs(); break;
835 case 'o': nr = getoctchrs(); break;
836 case 'x': nr = gethexchrs(2); break;
837 case 'u': nr = gethexchrs(4); break;
838 case 'U': nr = gethexchrs(8); break;
839 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200840 }
841
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200842 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200843 EMSG2_RET_FAIL(
844 _("E678: Invalid character after %s%%[dxouU]"),
845 reg_magic == MAGIC_ALL);
846 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200847 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200848 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200849 break;
850
851 /* Catch \%^ and \%$ regardless of where they appear in the
852 * pattern -- regardless of whether or not it makes sense. */
853 case '^':
854 EMIT(NFA_BOF);
855 /* Not yet supported */
856 return FAIL;
857 break;
858
859 case '$':
860 EMIT(NFA_EOF);
861 /* Not yet supported */
862 return FAIL;
863 break;
864
865 case '#':
866 /* not supported yet */
867 return FAIL;
868 break;
869
870 case 'V':
871 /* not supported yet */
872 return FAIL;
873 break;
874
875 case '[':
876 /* \%[abc] not supported yet */
877 return FAIL;
878
879 default:
880 /* not supported yet */
881 return FAIL;
882 }
883 break;
884
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200885 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200886collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200887 /*
888 * Glue is emitted between several atoms from the [].
889 * It is either NFA_OR, or NFA_CONCAT.
890 *
891 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
892 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
893 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
894 * notation)
895 *
896 */
897
898
899/* Emit negation atoms, if needed.
900 * The CONCAT below merges the NOT with the previous node. */
901#define TRY_NEG() \
902 if (negated == TRUE) \
903 { \
904 EMIT(NFA_NOT); \
905 }
906
907/* Emit glue between important nodes : CONCAT or OR. */
908#define EMIT_GLUE() \
909 if (first == FALSE) \
910 EMIT(glue); \
911 else \
912 first = FALSE;
913
914 p = regparse;
915 endp = skip_anyof(p);
916 if (*endp == ']')
917 {
918 /*
919 * Try to reverse engineer character classes. For example,
920 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
921 * and perform the necessary substitutions in the NFA.
922 */
923 result = nfa_recognize_char_class(regparse, endp,
924 extra == ADD_NL);
925 if (result != FAIL)
926 {
927 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
928 EMIT(result);
929 else /* must be char class + newline */
930 {
931 EMIT(result - ADD_NL);
932 EMIT(NFA_NEWL);
933 EMIT(NFA_OR);
934 }
935 regparse = endp;
936 nfa_inc(&regparse);
937 return OK;
938 }
939 /*
940 * Failed to recognize a character class. Use the simple
941 * version that turns [abc] into 'a' OR 'b' OR 'c'
942 */
943 startc = endc = oldstartc = -1;
944 first = TRUE; /* Emitting first atom in this sequence? */
945 negated = FALSE;
946 glue = NFA_OR;
947 if (*regparse == '^') /* negated range */
948 {
949 negated = TRUE;
950 glue = NFA_CONCAT;
951 nfa_inc(&regparse);
952 }
953 if (*regparse == '-')
954 {
955 startc = '-';
956 EMIT(startc);
957 TRY_NEG();
958 EMIT_GLUE();
959 nfa_inc(&regparse);
960 }
961 /* Emit the OR branches for each character in the [] */
962 emit_range = FALSE;
963 while (regparse < endp)
964 {
965 oldstartc = startc;
966 startc = -1;
967 got_coll_char = FALSE;
968 if (*regparse == '[')
969 {
970 /* Check for [: :], [= =], [. .] */
971 equiclass = collclass = 0;
972 charclass = get_char_class(&regparse);
973 if (charclass == CLASS_NONE)
974 {
975 equiclass = get_equi_class(&regparse);
976 if (equiclass == 0)
977 collclass = get_coll_element(&regparse);
978 }
979
980 /* Character class like [:alpha:] */
981 if (charclass != CLASS_NONE)
982 {
983 switch (charclass)
984 {
985 case CLASS_ALNUM:
986 EMIT(NFA_CLASS_ALNUM);
987 break;
988 case CLASS_ALPHA:
989 EMIT(NFA_CLASS_ALPHA);
990 break;
991 case CLASS_BLANK:
992 EMIT(NFA_CLASS_BLANK);
993 break;
994 case CLASS_CNTRL:
995 EMIT(NFA_CLASS_CNTRL);
996 break;
997 case CLASS_DIGIT:
998 EMIT(NFA_CLASS_DIGIT);
999 break;
1000 case CLASS_GRAPH:
1001 EMIT(NFA_CLASS_GRAPH);
1002 break;
1003 case CLASS_LOWER:
1004 EMIT(NFA_CLASS_LOWER);
1005 break;
1006 case CLASS_PRINT:
1007 EMIT(NFA_CLASS_PRINT);
1008 break;
1009 case CLASS_PUNCT:
1010 EMIT(NFA_CLASS_PUNCT);
1011 break;
1012 case CLASS_SPACE:
1013 EMIT(NFA_CLASS_SPACE);
1014 break;
1015 case CLASS_UPPER:
1016 EMIT(NFA_CLASS_UPPER);
1017 break;
1018 case CLASS_XDIGIT:
1019 EMIT(NFA_CLASS_XDIGIT);
1020 break;
1021 case CLASS_TAB:
1022 EMIT(NFA_CLASS_TAB);
1023 break;
1024 case CLASS_RETURN:
1025 EMIT(NFA_CLASS_RETURN);
1026 break;
1027 case CLASS_BACKSPACE:
1028 EMIT(NFA_CLASS_BACKSPACE);
1029 break;
1030 case CLASS_ESCAPE:
1031 EMIT(NFA_CLASS_ESCAPE);
1032 break;
1033 }
1034 TRY_NEG();
1035 EMIT_GLUE();
1036 continue;
1037 }
1038 /* Try equivalence class [=a=] and the like */
1039 if (equiclass != 0)
1040 {
1041 result = nfa_emit_equi_class(equiclass, negated);
1042 if (result == FAIL)
1043 {
1044 /* should never happen */
1045 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1046 }
1047 EMIT_GLUE();
1048 continue;
1049 }
1050 /* Try collating class like [. .] */
1051 if (collclass != 0)
1052 {
1053 startc = collclass; /* allow [.a.]-x as a range */
1054 /* Will emit the proper atom at the end of the
1055 * while loop. */
1056 }
1057 }
1058 /* Try a range like 'a-x' or '\t-z' */
1059 if (*regparse == '-')
1060 {
1061 emit_range = TRUE;
1062 startc = oldstartc;
1063 nfa_inc(&regparse);
1064 continue; /* reading the end of the range */
1065 }
1066
1067 /* Now handle simple and escaped characters.
1068 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1069 * accepts "\t", "\e", etc., but only when the 'l' flag in
1070 * 'cpoptions' is not included.
1071 * Posix doesn't recognize backslash at all.
1072 */
1073 if (*regparse == '\\'
1074 && !cpo_bsl
1075 && regparse + 1 <= endp
1076 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1077 || (!cpo_lit
1078 && vim_strchr(REGEXP_ABBR, regparse[1])
1079 != NULL)
1080 )
1081 )
1082 {
1083 nfa_inc(&regparse);
1084
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001085 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 startc = reg_string ? NL : NFA_NEWL;
1087 else
1088 if (*regparse == 'd'
1089 || *regparse == 'o'
1090 || *regparse == 'x'
1091 || *regparse == 'u'
1092 || *regparse == 'U'
1093 )
1094 {
1095 /* TODO(RE) This needs more testing */
1096 startc = coll_get_char();
1097 got_coll_char = TRUE;
1098 nfa_dec(&regparse);
1099 }
1100 else
1101 {
1102 /* \r,\t,\e,\b */
1103 startc = backslash_trans(*regparse);
1104 }
1105 }
1106
1107 /* Normal printable char */
1108 if (startc == -1)
1109#ifdef FEAT_MBYTE
1110 startc = (*mb_ptr2char)(regparse);
1111#else
1112 startc = *regparse;
1113#endif
1114
1115 /* Previous char was '-', so this char is end of range. */
1116 if (emit_range)
1117 {
1118 endc = startc; startc = oldstartc;
1119 if (startc > endc)
1120 EMSG_RET_FAIL(_(e_invrange));
1121#ifdef FEAT_MBYTE
1122 if (has_mbyte && ((*mb_char2len)(startc) > 1
1123 || (*mb_char2len)(endc) > 1))
1124 {
1125 if (endc > startc + 256)
1126 EMSG_RET_FAIL(_(e_invrange));
1127 /* Emit the range. "startc" was already emitted, so
1128 * skip it. */
1129 for (c = startc + 1; c <= endc; c++)
1130 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001131 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 TRY_NEG();
1133 EMIT_GLUE();
1134 }
1135 emit_range = FALSE;
1136 }
1137 else
1138#endif
1139 {
1140#ifdef EBCDIC
1141 int alpha_only = FALSE;
1142
1143 /* for alphabetical range skip the gaps
1144 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1145 if (isalpha(startc) && isalpha(endc))
1146 alpha_only = TRUE;
1147#endif
1148 /* Emit the range. "startc" was already emitted, so
1149 * skip it. */
1150 for (c = startc + 1; c <= endc; c++)
1151#ifdef EBCDIC
1152 if (!alpha_only || isalpha(startc))
1153#endif
1154 {
1155 EMIT(c);
1156 TRY_NEG();
1157 EMIT_GLUE();
1158 }
1159 emit_range = FALSE;
1160 }
1161 }
1162 else
1163 {
1164 /*
1165 * This char (startc) is not part of a range. Just
1166 * emit it.
1167 *
1168 * Normally, simply emit startc. But if we get char
1169 * code=0 from a collating char, then replace it with
1170 * 0x0a.
1171 *
1172 * This is needed to completely mimic the behaviour of
1173 * the backtracking engine.
1174 */
1175 if (got_coll_char == TRUE && startc == 0)
1176 EMIT(0x0a);
1177 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001178 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001179 TRY_NEG();
1180 EMIT_GLUE();
1181 }
1182
1183 nfa_inc(&regparse);
1184 } /* while (p < endp) */
1185
1186 nfa_dec(&regparse);
1187 if (*regparse == '-') /* if last, '-' is just a char */
1188 {
1189 EMIT('-');
1190 TRY_NEG();
1191 EMIT_GLUE();
1192 }
1193 nfa_inc(&regparse);
1194
1195 if (extra == ADD_NL) /* \_[] also matches \n */
1196 {
1197 EMIT(reg_string ? NL : NFA_NEWL);
1198 TRY_NEG();
1199 EMIT_GLUE();
1200 }
1201
1202 /* skip the trailing ] */
1203 regparse = endp;
1204 nfa_inc(&regparse);
1205 if (negated == TRUE)
1206 {
1207 /* Mark end of negated char range */
1208 EMIT(NFA_END_NEG_RANGE);
1209 EMIT(NFA_CONCAT);
1210 }
1211 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001212 } /* if exists closing ] */
1213
1214 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001215 {
1216 syntax_error = TRUE;
1217 EMSG_RET_FAIL(_(e_missingbracket));
1218 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001219 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001220
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 default:
1222 {
1223#ifdef FEAT_MBYTE
1224 int plen;
1225
1226nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001227 /* plen is length of current char with composing chars */
1228 if (enc_utf8 && ((*mb_char2len)(c)
1229 != (plen = (*mb_ptr2len)(old_regparse))
1230 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001231 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001232 int i = 0;
1233
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001234 /* A base character plus composing characters, or just one
1235 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001236 * This requires creating a separate atom as if enclosing
1237 * the characters in (), where NFA_COMPOSING is the ( and
1238 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001239 * building the postfix form, not the NFA itself;
1240 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001241 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001242 for (;;)
1243 {
1244 EMIT(c);
1245 if (i > 0)
1246 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001247 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001248 break;
1249 c = utf_ptr2char(old_regparse + i);
1250 }
1251 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001252 regparse = old_regparse + plen;
1253 }
1254 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255#endif
1256 {
1257 c = no_Magic(c);
1258 EMIT(c);
1259 }
1260 return OK;
1261 }
1262 }
1263
1264#undef TRY_NEG
1265#undef EMIT_GLUE
1266
1267 return OK;
1268}
1269
1270/*
1271 * Parse something followed by possible [*+=].
1272 *
1273 * A piece is an atom, possibly followed by a multi, an indication of how many
1274 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1275 * characters: "", "a", "aa", etc.
1276 *
1277 * piece ::= atom
1278 * or atom multi
1279 */
1280 static int
1281nfa_regpiece()
1282{
1283 int i;
1284 int op;
1285 int ret;
1286 long minval, maxval;
1287 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1288 char_u *old_regparse, *new_regparse;
1289 int c2;
1290 int *old_post_ptr, *my_post_start;
1291 int old_regnpar;
1292 int quest;
1293
1294 /* Save the current position in the regexp, so that we can use it if
1295 * <atom>{m,n} is next. */
1296 old_regparse = regparse;
1297 /* Save current number of open parenthesis, so we can use it if
1298 * <atom>{m,n} is next */
1299 old_regnpar = regnpar;
1300 /* store current pos in the postfix form, for \{m,n} involving 0s */
1301 my_post_start = post_ptr;
1302
1303 ret = nfa_regatom();
1304 if (ret == FAIL)
1305 return FAIL; /* cascaded error */
1306
1307 op = peekchr();
1308 if (re_multi_type(op) == NOT_MULTI)
1309 return OK;
1310
1311 skipchr();
1312 switch (op)
1313 {
1314 case Magic('*'):
1315 EMIT(NFA_STAR);
1316 break;
1317
1318 case Magic('+'):
1319 /*
1320 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1321 * first and only submatch would be "aaa". But the backtracking
1322 * engine interprets the plus as "try matching one more time", and
1323 * a* matches a second time at the end of the input, the empty
1324 * string.
1325 * The submatch will the empty string.
1326 *
1327 * In order to be consistent with the old engine, we disable
1328 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1329 */
1330 /* EMIT(NFA_PLUS); */
1331 regnpar = old_regnpar;
1332 regparse = old_regparse;
1333 curchr = -1;
1334 if (nfa_regatom() == FAIL)
1335 return FAIL;
1336 EMIT(NFA_STAR);
1337 EMIT(NFA_CONCAT);
1338 skipchr(); /* skip the \+ */
1339 break;
1340
1341 case Magic('@'):
1342 op = no_Magic(getchr());
1343 switch(op)
1344 {
1345 case '=':
1346 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1347 break;
1348 case '!':
1349 case '<':
1350 case '>':
1351 /* Not supported yet */
1352 return FAIL;
1353 default:
1354 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001355 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001356 return FAIL;
1357 }
1358 break;
1359
1360 case Magic('?'):
1361 case Magic('='):
1362 EMIT(NFA_QUEST);
1363 break;
1364
1365 case Magic('{'):
1366 /* a{2,5} will expand to 'aaa?a?a?'
1367 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1368 * version of '?'
1369 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1370 * parenthesis have the same id
1371 */
1372
1373 greedy = TRUE;
1374 c2 = peekchr();
1375 if (c2 == '-' || c2 == Magic('-'))
1376 {
1377 skipchr();
1378 greedy = FALSE;
1379 }
1380 if (!read_limits(&minval, &maxval))
1381 {
1382 syntax_error = TRUE;
1383 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1384 }
1385 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1386 * <atom>* */
1387 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1388 {
1389 EMIT(NFA_STAR);
1390 break;
1391 }
1392
1393 if (maxval > NFA_BRACES_MAXLIMIT)
1394 {
1395 /* This would yield a huge automaton and use too much memory.
1396 * Revert to old engine */
1397 return FAIL;
1398 }
1399
1400 /* Special case: x{0} or x{-0} */
1401 if (maxval == 0)
1402 {
1403 /* Ignore result of previous call to nfa_regatom() */
1404 post_ptr = my_post_start;
1405 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1406 EMIT(NFA_SKIP_CHAR);
1407 return OK;
1408 }
1409
1410 /* Ignore previous call to nfa_regatom() */
1411 post_ptr = my_post_start;
1412 /* Save pos after the repeated atom and the \{} */
1413 new_regparse = regparse;
1414
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001415 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1416 for (i = 0; i < maxval; i++)
1417 {
1418 /* Goto beginning of the repeated atom */
1419 regparse = old_regparse;
1420 curchr = -1;
1421 /* Restore count of parenthesis */
1422 regnpar = old_regnpar;
1423 old_post_ptr = post_ptr;
1424 if (nfa_regatom() == FAIL)
1425 return FAIL;
1426 /* after "minval" times, atoms are optional */
1427 if (i + 1 > minval)
1428 EMIT(quest);
1429 if (old_post_ptr != my_post_start)
1430 EMIT(NFA_CONCAT);
1431 }
1432
1433 /* Go to just after the repeated atom and the \{} */
1434 regparse = new_regparse;
1435 curchr = -1;
1436
1437 break;
1438
1439
1440 default:
1441 break;
1442 } /* end switch */
1443
1444 if (re_multi_type(peekchr()) != NOT_MULTI)
1445 {
1446 /* Can't have a multi follow a multi. */
1447 syntax_error = TRUE;
1448 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1449 }
1450
1451 return OK;
1452}
1453
1454/*
1455 * Parse one or more pieces, concatenated. It matches a match for the
1456 * first piece, followed by a match for the second piece, etc. Example:
1457 * "f[0-9]b", first matches "f", then a digit and then "b".
1458 *
1459 * concat ::= piece
1460 * or piece piece
1461 * or piece piece piece
1462 * etc.
1463 */
1464 static int
1465nfa_regconcat()
1466{
1467 int cont = TRUE;
1468 int first = TRUE;
1469
1470 while (cont)
1471 {
1472 switch (peekchr())
1473 {
1474 case NUL:
1475 case Magic('|'):
1476 case Magic('&'):
1477 case Magic(')'):
1478 cont = FALSE;
1479 break;
1480
1481 case Magic('Z'):
1482#ifdef FEAT_MBYTE
1483 regflags |= RF_ICOMBINE;
1484#endif
1485 skipchr_keepstart();
1486 break;
1487 case Magic('c'):
1488 regflags |= RF_ICASE;
1489 skipchr_keepstart();
1490 break;
1491 case Magic('C'):
1492 regflags |= RF_NOICASE;
1493 skipchr_keepstart();
1494 break;
1495 case Magic('v'):
1496 reg_magic = MAGIC_ALL;
1497 skipchr_keepstart();
1498 curchr = -1;
1499 break;
1500 case Magic('m'):
1501 reg_magic = MAGIC_ON;
1502 skipchr_keepstart();
1503 curchr = -1;
1504 break;
1505 case Magic('M'):
1506 reg_magic = MAGIC_OFF;
1507 skipchr_keepstart();
1508 curchr = -1;
1509 break;
1510 case Magic('V'):
1511 reg_magic = MAGIC_NONE;
1512 skipchr_keepstart();
1513 curchr = -1;
1514 break;
1515
1516 default:
1517 if (nfa_regpiece() == FAIL)
1518 return FAIL;
1519 if (first == FALSE)
1520 EMIT(NFA_CONCAT);
1521 else
1522 first = FALSE;
1523 break;
1524 }
1525 }
1526
1527 return OK;
1528}
1529
1530/*
1531 * Parse a branch, one or more concats, separated by "\&". It matches the
1532 * last concat, but only if all the preceding concats also match at the same
1533 * position. Examples:
1534 * "foobeep\&..." matches "foo" in "foobeep".
1535 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1536 *
1537 * branch ::= concat
1538 * or concat \& concat
1539 * or concat \& concat \& concat
1540 * etc.
1541 */
1542 static int
1543nfa_regbranch()
1544{
1545 int ch;
1546 int *old_post_ptr;
1547
1548 old_post_ptr = post_ptr;
1549
1550 /* First branch, possibly the only one */
1551 if (nfa_regconcat() == FAIL)
1552 return FAIL;
1553
1554 ch = peekchr();
1555 /* Try next concats */
1556 while (ch == Magic('&'))
1557 {
1558 skipchr();
1559 EMIT(NFA_NOPEN);
1560 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1561 old_post_ptr = post_ptr;
1562 if (nfa_regconcat() == FAIL)
1563 return FAIL;
1564 /* if concat is empty, skip a input char. But do emit a node */
1565 if (old_post_ptr == post_ptr)
1566 EMIT(NFA_SKIP_CHAR);
1567 EMIT(NFA_CONCAT);
1568 ch = peekchr();
1569 }
1570
1571 /* Even if a branch is empty, emit one node for it */
1572 if (old_post_ptr == post_ptr)
1573 EMIT(NFA_SKIP_CHAR);
1574
1575 return OK;
1576}
1577
1578/*
1579 * Parse a pattern, one or more branches, separated by "\|". It matches
1580 * anything that matches one of the branches. Example: "foo\|beep" matches
1581 * "foo" and matches "beep". If more than one branch matches, the first one
1582 * is used.
1583 *
1584 * pattern ::= branch
1585 * or branch \| branch
1586 * or branch \| branch \| branch
1587 * etc.
1588 */
1589 static int
1590nfa_reg(paren)
1591 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1592{
1593 int parno = 0;
1594
1595#ifdef FEAT_SYN_HL
1596#endif
1597 if (paren == REG_PAREN)
1598 {
1599 if (regnpar >= NSUBEXP) /* Too many `(' */
1600 {
1601 syntax_error = TRUE;
1602 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1603 }
1604 parno = regnpar++;
1605 }
1606
1607 if (nfa_regbranch() == FAIL)
1608 return FAIL; /* cascaded error */
1609
1610 while (peekchr() == Magic('|'))
1611 {
1612 skipchr();
1613 if (nfa_regbranch() == FAIL)
1614 return FAIL; /* cascaded error */
1615 EMIT(NFA_OR);
1616 }
1617
1618 /* Check for proper termination. */
1619 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1620 {
1621 syntax_error = TRUE;
1622 if (paren == REG_NPAREN)
1623 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1624 else
1625 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1626 }
1627 else if (paren == REG_NOPAREN && peekchr() != NUL)
1628 {
1629 syntax_error = TRUE;
1630 if (peekchr() == Magic(')'))
1631 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1632 else
1633 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1634 }
1635 /*
1636 * Here we set the flag allowing back references to this set of
1637 * parentheses.
1638 */
1639 if (paren == REG_PAREN)
1640 {
1641 had_endbrace[parno] = TRUE; /* have seen the close paren */
1642 EMIT(NFA_MOPEN + parno);
1643 }
1644
1645 return OK;
1646}
1647
1648typedef struct
1649{
1650 char_u *start[NSUBEXP];
1651 char_u *end[NSUBEXP];
1652 lpos_T startpos[NSUBEXP];
1653 lpos_T endpos[NSUBEXP];
1654} regsub_T;
1655
1656static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
1657
1658#ifdef DEBUG
1659static char_u code[50];
1660
1661 static void
1662nfa_set_code(c)
1663 int c;
1664{
1665 int addnl = FALSE;
1666
1667 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1668 {
1669 addnl = TRUE;
1670 c -= ADD_NL;
1671 }
1672
1673 STRCPY(code, "");
1674 switch (c)
1675 {
1676 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1677 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1678 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1679 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1680 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1681 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1682
1683 case NFA_PREV_ATOM_NO_WIDTH:
1684 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1685 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1686 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1687 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1688 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1689
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001690 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1691 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1692
1693 case NFA_MOPEN + 0:
1694 case NFA_MOPEN + 1:
1695 case NFA_MOPEN + 2:
1696 case NFA_MOPEN + 3:
1697 case NFA_MOPEN + 4:
1698 case NFA_MOPEN + 5:
1699 case NFA_MOPEN + 6:
1700 case NFA_MOPEN + 7:
1701 case NFA_MOPEN + 8:
1702 case NFA_MOPEN + 9:
1703 STRCPY(code, "NFA_MOPEN(x)");
1704 code[10] = c - NFA_MOPEN + '0';
1705 break;
1706 case NFA_MCLOSE + 0:
1707 case NFA_MCLOSE + 1:
1708 case NFA_MCLOSE + 2:
1709 case NFA_MCLOSE + 3:
1710 case NFA_MCLOSE + 4:
1711 case NFA_MCLOSE + 5:
1712 case NFA_MCLOSE + 6:
1713 case NFA_MCLOSE + 7:
1714 case NFA_MCLOSE + 8:
1715 case NFA_MCLOSE + 9:
1716 STRCPY(code, "NFA_MCLOSE(x)");
1717 code[11] = c - NFA_MCLOSE + '0';
1718 break;
1719 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1720 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1721 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1722 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1723 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1724 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1725 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1726 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1727 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1728 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1729 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1730 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1731 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1732 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1733 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1734 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1735 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1736 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1737 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1738 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1739 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1740 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1741 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1742 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1743 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1744 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1745 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1746 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1747
1748 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1749 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1750 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1751 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1752 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1753 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1754 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1755 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1756 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1757 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1758 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1759 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1760 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1761 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1762 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1763 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1764 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1765 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1766 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1767 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1768 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1769 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1770 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1771 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1772 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1773 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1774 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1775
1776 default:
1777 STRCPY(code, "CHAR(x)");
1778 code[5] = c;
1779 }
1780
1781 if (addnl == TRUE)
1782 STRCAT(code, " + NEWLINE ");
1783
1784}
1785
1786#ifdef ENABLE_LOG
1787static FILE *log_fd;
1788
1789/*
1790 * Print the postfix notation of the current regexp.
1791 */
1792 static void
1793nfa_postfix_dump(expr, retval)
1794 char_u *expr;
1795 int retval;
1796{
1797 int *p;
1798 FILE *f;
1799
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001800 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 if (f != NULL)
1802 {
1803 fprintf(f, "\n-------------------------\n");
1804 if (retval == FAIL)
1805 fprintf(f, ">>> NFA engine failed ... \n");
1806 else if (retval == OK)
1807 fprintf(f, ">>> NFA engine succeeded !\n");
1808 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001809 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001810 {
1811 nfa_set_code(*p);
1812 fprintf(f, "%s, ", code);
1813 }
1814 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001815 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 fprintf(f, "%d ", *p);
1817 fprintf(f, "\n\n");
1818 fclose(f);
1819 }
1820}
1821
1822/*
1823 * Print the NFA starting with a root node "state".
1824 */
1825 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001826nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001827 FILE *debugf;
1828 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001830 garray_T indent;
1831
1832 ga_init2(&indent, 1, 64);
1833 ga_append(&indent, '\0');
1834 nfa_print_state2(debugf, state, &indent);
1835 ga_clear(&indent);
1836}
1837
1838 static void
1839nfa_print_state2(debugf, state, indent)
1840 FILE *debugf;
1841 nfa_state_T *state;
1842 garray_T *indent;
1843{
1844 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001845
1846 if (state == NULL)
1847 return;
1848
1849 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001850
1851 /* Output indent */
1852 p = (char_u *)indent->ga_data;
1853 if (indent->ga_len >= 3)
1854 {
1855 int last = indent->ga_len - 3;
1856 char_u save[2];
1857
1858 STRNCPY(save, &p[last], 2);
1859 STRNCPY(&p[last], "+-", 2);
1860 fprintf(debugf, " %s", p);
1861 STRNCPY(&p[last], save, 2);
1862 }
1863 else
1864 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001865
1866 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001867 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1868 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 if (state->id < 0)
1870 return;
1871
1872 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001873
1874 /* grow indent for state->out */
1875 indent->ga_len -= 1;
1876 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001877 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001878 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001879 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001880 ga_append(indent, '\0');
1881
1882 nfa_print_state2(debugf, state->out, indent);
1883
1884 /* replace last part of indent for state->out1 */
1885 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001886 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001887 ga_append(indent, '\0');
1888
1889 nfa_print_state2(debugf, state->out1, indent);
1890
1891 /* shrink indent */
1892 indent->ga_len -= 3;
1893 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001894}
1895
1896/*
1897 * Print the NFA state machine.
1898 */
1899 static void
1900nfa_dump(prog)
1901 nfa_regprog_T *prog;
1902{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001903 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904
1905 if (debugf != NULL)
1906 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001907 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 fclose(debugf);
1909 }
1910}
1911#endif /* ENABLE_LOG */
1912#endif /* DEBUG */
1913
1914/*
1915 * Parse r.e. @expr and convert it into postfix form.
1916 * Return the postfix string on success, NULL otherwise.
1917 */
1918 static int *
1919re2post()
1920{
1921 if (nfa_reg(REG_NOPAREN) == FAIL)
1922 return NULL;
1923 EMIT(NFA_MOPEN);
1924 return post_start;
1925}
1926
1927/* NB. Some of the code below is inspired by Russ's. */
1928
1929/*
1930 * Represents an NFA state plus zero or one or two arrows exiting.
1931 * if c == MATCH, no arrows out; matching state.
1932 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1933 * If c < 256, labeled arrow with character c to out.
1934 */
1935
1936static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1937
1938/*
1939 * Allocate and initialize nfa_state_T.
1940 */
1941 static nfa_state_T *
1942new_state(c, out, out1)
1943 int c;
1944 nfa_state_T *out;
1945 nfa_state_T *out1;
1946{
1947 nfa_state_T *s;
1948
1949 if (istate >= nstate)
1950 return NULL;
1951
1952 s = &state_ptr[istate++];
1953
1954 s->c = c;
1955 s->out = out;
1956 s->out1 = out1;
1957
1958 s->id = istate;
1959 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 s->visits = 0;
1961 s->negated = FALSE;
1962
1963 return s;
1964}
1965
1966/*
1967 * A partially built NFA without the matching state filled in.
1968 * Frag_T.start points at the start state.
1969 * Frag_T.out is a list of places that need to be set to the
1970 * next state for this fragment.
1971 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001972
1973/* Since the out pointers in the list are always
1974 * uninitialized, we use the pointers themselves
1975 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001976typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001977union Ptrlist
1978{
1979 Ptrlist *next;
1980 nfa_state_T *s;
1981};
1982
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983struct Frag
1984{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001985 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001986 Ptrlist *out;
1987};
1988typedef struct Frag Frag_T;
1989
1990static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
1991static Ptrlist *list1 __ARGS((nfa_state_T **outp));
1992static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
1993static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
1994static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
1995static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
1996
1997/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02001998 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999 */
2000 static Frag_T
2001frag(start, out)
2002 nfa_state_T *start;
2003 Ptrlist *out;
2004{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002005 Frag_T n;
2006
2007 n.start = start;
2008 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002009 return n;
2010}
2011
2012/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002013 * 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:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002654 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002655 {
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
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003112 * states at this position. When the list of states is going
3113 * to be empty quit without advancing, so that "reginput" is
3114 * correct. */
3115 if (nextlist->n == 0 && neglist->n == 0)
3116 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003117 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118
3119 case NFA_END_INVISIBLE:
3120 /* This is only encountered after a NFA_START_INVISIBLE node.
3121 * They surround a zero-width group, used with "\@=" and "\&".
3122 * If we got here, it means that the current "invisible" group
3123 * finished successfully, so return control to the parent
3124 * nfa_regmatch(). Submatches are stored in *m, and used in
3125 * the parent call. */
3126 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003127 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003128 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003129 else
3130 {
3131 *m = t->sub;
3132 match = TRUE;
3133 }
3134 break;
3135
3136 case NFA_START_INVISIBLE:
3137 /* Save global variables, and call nfa_regmatch() to check if
3138 * the current concat matches at this position. The concat
3139 * ends with the node NFA_END_INVISIBLE */
3140 old_reginput = reginput;
3141 old_regline = regline;
3142 old_reglnum = reglnum;
3143 if (listids == NULL)
3144 {
3145 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3146 if (listids == NULL)
3147 {
3148 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3149 return 0;
3150 }
3151 }
3152#ifdef ENABLE_LOG
3153 if (log_fd != stderr)
3154 fclose(log_fd);
3155 log_fd = NULL;
3156#endif
3157 /* Have to clear the listid field of the NFA nodes, so that
3158 * nfa_regmatch() and addstate() can run properly after
3159 * recursion. */
3160 nfa_save_listids(start, listids);
3161 nfa_set_null_listids(start);
3162 result = nfa_regmatch(t->state->out, submatch, m);
3163 nfa_set_neg_listids(start);
3164 nfa_restore_listids(start, listids);
3165
3166#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003167 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003168 if (log_fd != NULL)
3169 {
3170 fprintf(log_fd, "****************************\n");
3171 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3172 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3173 fprintf(log_fd, "****************************\n");
3174 }
3175 else
3176 {
3177 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3178 log_fd = stderr;
3179 }
3180#endif
3181 if (result == TRUE)
3182 {
3183 /* Restore position in input text */
3184 reginput = old_reginput;
3185 regline = old_regline;
3186 reglnum = old_reglnum;
3187 /* Copy submatch info from the recursive call */
3188 if (REG_MULTI)
3189 for (j = 1; j < NSUBEXP; j++)
3190 {
3191 t->sub.startpos[j] = m->startpos[j];
3192 t->sub.endpos[j] = m->endpos[j];
3193 }
3194 else
3195 for (j = 1; j < NSUBEXP; j++)
3196 {
3197 t->sub.start[j] = m->start[j];
3198 t->sub.end[j] = m->end[j];
3199 }
3200 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003201 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003202 listid, &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203 }
3204 else
3205 {
3206 /* continue with next input char */
3207 reginput = old_reginput;
3208 }
3209 break;
3210
3211 case NFA_BOL:
3212 if (reginput == regline)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003213 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003214 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003215 break;
3216
3217 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003218 if (curc == NUL)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003219 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003220 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003221 break;
3222
3223 case NFA_BOW:
3224 {
3225 int bow = TRUE;
3226
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003227 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003228 bow = FALSE;
3229#ifdef FEAT_MBYTE
3230 else if (has_mbyte)
3231 {
3232 int this_class;
3233
3234 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003235 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236 if (this_class <= 1)
3237 bow = FALSE;
3238 else if (reg_prev_class() == this_class)
3239 bow = FALSE;
3240 }
3241#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003242 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003243 || (reginput > regline
3244 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245 bow = FALSE;
3246 if (bow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003247 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003248 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 break;
3250 }
3251
3252 case NFA_EOW:
3253 {
3254 int eow = TRUE;
3255
3256 if (reginput == regline)
3257 eow = FALSE;
3258#ifdef FEAT_MBYTE
3259 else if (has_mbyte)
3260 {
3261 int this_class, prev_class;
3262
3263 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003264 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003265 prev_class = reg_prev_class();
3266 if (this_class == prev_class
3267 || prev_class == 0 || prev_class == 1)
3268 eow = FALSE;
3269 }
3270#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003271 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003272 || (reginput[0] != NUL
3273 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003274 eow = FALSE;
3275 if (eow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003276 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003277 &match, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 break;
3279 }
3280
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003281#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003282 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003283 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003284 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003285 int len = 0;
3286 nfa_state_T *end;
3287 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003288 int cchars[MAX_MCO];
3289 int ccount = 0;
3290 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003291
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003293 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003294 if (utf_iscomposing(sta->c))
3295 {
3296 /* Only match composing character(s), ignore base
3297 * character. Used for ".{composing}" and "{composing}"
3298 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003299 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003300 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003301 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003303 /* If \Z was present, then ignore composing characters.
3304 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003305 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003306 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003307 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003308 else
3309 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003310 while (sta->c != NFA_END_COMPOSING)
3311 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003313
3314 /* Check base character matches first, unless ignored. */
3315 else if (len > 0 || mc == sta->c)
3316 {
3317 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003318 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003319 len += mb_char2len(mc);
3320 sta = sta->out;
3321 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003323 /* We don't care about the order of composing characters.
3324 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003325 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003326 {
3327 mc = mb_ptr2char(reginput + len);
3328 cchars[ccount++] = mc;
3329 len += mb_char2len(mc);
3330 if (ccount == MAX_MCO)
3331 break;
3332 }
3333
3334 /* Check that each composing char in the pattern matches a
3335 * composing char in the text. We do not check if all
3336 * composing chars are matched. */
3337 result = OK;
3338 while (sta->c != NFA_END_COMPOSING)
3339 {
3340 for (j = 0; j < ccount; ++j)
3341 if (cchars[j] == sta->c)
3342 break;
3343 if (j == ccount)
3344 {
3345 result = FAIL;
3346 break;
3347 }
3348 sta = sta->out;
3349 }
3350 }
3351 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003352 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003353
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003354 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003355 ADD_POS_NEG_STATE(end);
3356 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003357 }
3358#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003359
3360 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003361 if (curc == NUL && !reg_line_lbr && REG_MULTI
3362 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003364 go_to_nextline = TRUE;
3365 /* Pass -1 for the offset, which means taking the position
3366 * at the start of the next line. */
3367 addstate(nextlist, t->state->out, &t->sub, -1,
3368 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003370 else if (curc == '\n' && reg_line_lbr)
3371 {
3372 /* match \n as if it is an ordinary character */
3373 addstate(nextlist, t->state->out, &t->sub, 1,
3374 listid + 1, &match);
3375 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376 break;
3377
3378 case NFA_CLASS_ALNUM:
3379 case NFA_CLASS_ALPHA:
3380 case NFA_CLASS_BLANK:
3381 case NFA_CLASS_CNTRL:
3382 case NFA_CLASS_DIGIT:
3383 case NFA_CLASS_GRAPH:
3384 case NFA_CLASS_LOWER:
3385 case NFA_CLASS_PRINT:
3386 case NFA_CLASS_PUNCT:
3387 case NFA_CLASS_SPACE:
3388 case NFA_CLASS_UPPER:
3389 case NFA_CLASS_XDIGIT:
3390 case NFA_CLASS_TAB:
3391 case NFA_CLASS_RETURN:
3392 case NFA_CLASS_BACKSPACE:
3393 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003394 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003395 ADD_POS_NEG_STATE(t->state);
3396 break;
3397
3398 case NFA_END_NEG_RANGE:
3399 /* This follows a series of negated nodes, like:
3400 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003401 if (curc > 0)
3402 addstate(nextlist, t->state->out, &t->sub, clen,
3403 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404 break;
3405
3406 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003407 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003408 if (curc > 0)
3409 addstate(nextlist, t->state->out, &t->sub, clen,
3410 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411 break;
3412
3413 /*
3414 * Character classes like \a for alpha, \d for digit etc.
3415 */
3416 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003417 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 ADD_POS_NEG_STATE(t->state);
3419 break;
3420
3421 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003422 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003423 ADD_POS_NEG_STATE(t->state);
3424 break;
3425
3426 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003427 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428 ADD_POS_NEG_STATE(t->state);
3429 break;
3430
3431 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003432 result = !VIM_ISDIGIT(curc)
3433 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003434 ADD_POS_NEG_STATE(t->state);
3435 break;
3436
3437 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003438 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439 ADD_POS_NEG_STATE(t->state);
3440 break;
3441
3442 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003443 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 ADD_POS_NEG_STATE(t->state);
3445 break;
3446
3447 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003448 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449 ADD_POS_NEG_STATE(t->state);
3450 break;
3451
3452 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003453 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003454 ADD_POS_NEG_STATE(t->state);
3455 break;
3456
3457 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003458 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 ADD_POS_NEG_STATE(t->state);
3460 break;
3461
3462 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003463 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464 ADD_POS_NEG_STATE(t->state);
3465 break;
3466
3467 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003468 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 ADD_POS_NEG_STATE(t->state);
3470 break;
3471
3472 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003473 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 ADD_POS_NEG_STATE(t->state);
3475 break;
3476
3477 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003478 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 ADD_POS_NEG_STATE(t->state);
3480 break;
3481
3482 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003483 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484 ADD_POS_NEG_STATE(t->state);
3485 break;
3486
3487 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003488 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 ADD_POS_NEG_STATE(t->state);
3490 break;
3491
3492 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003493 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 ADD_POS_NEG_STATE(t->state);
3495 break;
3496
3497 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003498 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499 ADD_POS_NEG_STATE(t->state);
3500 break;
3501
3502 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003503 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 ADD_POS_NEG_STATE(t->state);
3505 break;
3506
3507 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003508 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509 ADD_POS_NEG_STATE(t->state);
3510 break;
3511
3512 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003513 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 ADD_POS_NEG_STATE(t->state);
3515 break;
3516
3517 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003518 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519 ADD_POS_NEG_STATE(t->state);
3520 break;
3521
3522 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003523 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524 ADD_POS_NEG_STATE(t->state);
3525 break;
3526
3527 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003528 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529 ADD_POS_NEG_STATE(t->state);
3530 break;
3531
3532 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003533 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003534 ADD_POS_NEG_STATE(t->state);
3535 break;
3536
3537 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003538 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003539 ADD_POS_NEG_STATE(t->state);
3540 break;
3541
3542 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003543 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003544 ADD_POS_NEG_STATE(t->state);
3545 break;
3546
Bram Moolenaar12e40142013-05-21 15:33:41 +02003547 case NFA_MOPEN + 0:
3548 case NFA_MOPEN + 1:
3549 case NFA_MOPEN + 2:
3550 case NFA_MOPEN + 3:
3551 case NFA_MOPEN + 4:
3552 case NFA_MOPEN + 5:
3553 case NFA_MOPEN + 6:
3554 case NFA_MOPEN + 7:
3555 case NFA_MOPEN + 8:
3556 case NFA_MOPEN + 9:
3557 /* handled below */
3558 break;
3559
3560 case NFA_SKIP_CHAR:
3561 case NFA_ZSTART:
3562 /* TODO: should not happen? */
3563 break;
3564
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 default: /* regular character */
Bram Moolenaar12e40142013-05-21 15:33:41 +02003566 /* TODO: put this in #ifdef later */
3567 if (t->state->c < -256)
3568 EMSGN("INTERNAL: Negative state char: %ld", t->state->c);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003569 result = (no_Magic(t->state->c) == curc);
Bram Moolenaar12e40142013-05-21 15:33:41 +02003570
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 if (!result)
3572 result = ireg_ic == TRUE
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003573 && MB_TOLOWER(t->state->c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003574#ifdef FEAT_MBYTE
3575 /* If there is a composing character which is not being
3576 * ignored there can be no match. Match with composing
3577 * character uses NFA_COMPOSING above. */
3578 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003579 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003580 result = FALSE;
3581#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003582 ADD_POS_NEG_STATE(t->state);
3583 break;
3584 }
3585
3586 } /* for (thislist = thislist; thislist->state; thislist++) */
3587
3588 /* The first found match is the leftmost one, but there may be a
3589 * longer one. Keep running the NFA, but don't start from the
3590 * beginning. Also, do not add the start state in recursive calls of
3591 * nfa_regmatch(), because recursive calls should only start in the
3592 * first position. */
3593 if (match == FALSE && start->c == NFA_MOPEN + 0)
3594 {
3595#ifdef ENABLE_LOG
3596 fprintf(log_fd, "(---) STARTSTATE\n");
3597#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003598 addstate(nextlist, start, m, clen, listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 }
3600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003601#ifdef ENABLE_LOG
3602 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003603 {
3604 int i;
3605
3606 for (i = 0; i < thislist->n; i++)
3607 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3608 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003609 fprintf(log_fd, "\n");
3610#endif
3611
3612nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003613 /* Advance to the next character, or advance to the next line, or
3614 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003615 if (clen != 0)
3616 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003617 else if (go_to_nextline)
3618 reg_nextline();
3619 else
3620 break;
3621 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003622
3623#ifdef ENABLE_LOG
3624 if (log_fd != stderr)
3625 fclose(log_fd);
3626 log_fd = NULL;
3627#endif
3628
3629theend:
3630 /* Free memory */
3631 vim_free(list[0].t);
3632 vim_free(list[1].t);
3633 vim_free(list[2].t);
3634 list[0].t = list[1].t = list[2].t = NULL;
3635 if (listids != NULL)
3636 vim_free(listids);
3637#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003638#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639 fclose(debug);
3640#endif
3641
3642 return match;
3643}
3644
3645/*
3646 * Try match of "prog" with at regline["col"].
3647 * Returns 0 for failure, number of lines contained in the match otherwise.
3648 */
3649 static long
3650nfa_regtry(start, col)
3651 nfa_state_T *start;
3652 colnr_T col;
3653{
3654 int i;
3655 regsub_T sub, m;
3656#ifdef ENABLE_LOG
3657 FILE *f;
3658#endif
3659
3660 reginput = regline + col;
3661 need_clear_subexpr = TRUE;
3662
3663#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003664 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003665 if (f != NULL)
3666 {
3667 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3668 fprintf(f, " =======================================================\n");
3669#ifdef DEBUG
3670 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3671#endif
3672 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3673 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003674 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003675 fprintf(f, "\n\n");
3676 fclose(f);
3677 }
3678 else
3679 EMSG(_("Could not open temporary log file for writing "));
3680#endif
3681
3682 if (REG_MULTI)
3683 {
3684 /* Use 0xff to set lnum to -1 */
3685 vim_memset(sub.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3686 vim_memset(sub.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3687 vim_memset(m.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3688 vim_memset(m.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3689 }
3690 else
3691 {
3692 vim_memset(sub.start, 0, sizeof(char_u *) * NSUBEXP);
3693 vim_memset(sub.end, 0, sizeof(char_u *) * NSUBEXP);
3694 vim_memset(m.start, 0, sizeof(char_u *) * NSUBEXP);
3695 vim_memset(m.end, 0, sizeof(char_u *) * NSUBEXP);
3696 }
3697
3698 if (nfa_regmatch(start, &sub, &m) == FALSE)
3699 return 0;
3700
3701 cleanup_subexpr();
3702 if (REG_MULTI)
3703 {
3704 for (i = 0; i < NSUBEXP; i++)
3705 {
3706 reg_startpos[i] = sub.startpos[i];
3707 reg_endpos[i] = sub.endpos[i];
3708 }
3709
3710 if (reg_startpos[0].lnum < 0)
3711 {
3712 reg_startpos[0].lnum = 0;
3713 reg_startpos[0].col = col;
3714 }
3715 if (reg_endpos[0].lnum < 0)
3716 {
3717 reg_endpos[0].lnum = reglnum;
3718 reg_endpos[0].col = (int)(reginput - regline);
3719 }
3720 else
3721 /* Use line number of "\ze". */
3722 reglnum = reg_endpos[0].lnum;
3723 }
3724 else
3725 {
3726 for (i = 0; i < NSUBEXP; i++)
3727 {
3728 reg_startp[i] = sub.start[i];
3729 reg_endp[i] = sub.end[i];
3730 }
3731
3732 if (reg_startp[0] == NULL)
3733 reg_startp[0] = regline + col;
3734 if (reg_endp[0] == NULL)
3735 reg_endp[0] = reginput;
3736 }
3737
3738 return 1 + reglnum;
3739}
3740
3741/*
3742 * Match a regexp against a string ("line" points to the string) or multiple
3743 * lines ("line" is NULL, use reg_getline()).
3744 *
3745 * Returns 0 for failure, number of lines contained in the match otherwise.
3746 */
3747 static long
3748nfa_regexec_both(line, col)
3749 char_u *line;
3750 colnr_T col; /* column to start looking for match */
3751{
3752 nfa_regprog_T *prog;
3753 long retval = 0L;
3754 int i;
3755
3756 if (REG_MULTI)
3757 {
3758 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3759 line = reg_getline((linenr_T)0); /* relative to the cursor */
3760 reg_startpos = reg_mmatch->startpos;
3761 reg_endpos = reg_mmatch->endpos;
3762 }
3763 else
3764 {
3765 prog = (nfa_regprog_T *)reg_match->regprog;
3766 reg_startp = reg_match->startp;
3767 reg_endp = reg_match->endp;
3768 }
3769
3770 /* Be paranoid... */
3771 if (prog == NULL || line == NULL)
3772 {
3773 EMSG(_(e_null));
3774 goto theend;
3775 }
3776
3777 /* If the start column is past the maximum column: no need to try. */
3778 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3779 goto theend;
3780
3781 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3782 if (prog->regflags & RF_ICASE)
3783 ireg_ic = TRUE;
3784 else if (prog->regflags & RF_NOICASE)
3785 ireg_ic = FALSE;
3786
3787#ifdef FEAT_MBYTE
3788 /* If pattern contains "\Z" overrule value of ireg_icombine */
3789 if (prog->regflags & RF_ICOMBINE)
3790 ireg_icombine = TRUE;
3791#endif
3792
3793 regline = line;
3794 reglnum = 0; /* relative to line */
3795
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003796 nfa_has_zend = prog->has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003797
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003798 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003799 for (i = 0; i < nstate; ++i)
3800 {
3801 prog->state[i].id = i;
3802 prog->state[i].lastlist = 0;
3803 prog->state[i].visits = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003804 }
3805
3806 retval = nfa_regtry(prog->start, col);
3807
3808theend:
3809 return retval;
3810}
3811
3812/*
3813 * Compile a regular expression into internal code for the NFA matcher.
3814 * Returns the program in allocated space. Returns NULL for an error.
3815 */
3816 static regprog_T *
3817nfa_regcomp(expr, re_flags)
3818 char_u *expr;
3819 int re_flags;
3820{
Bram Moolenaaraae48832013-05-25 21:18:34 +02003821 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003822 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003823 int *postfix;
3824
3825 if (expr == NULL)
3826 return NULL;
3827
3828#ifdef DEBUG
3829 nfa_regengine.expr = expr;
3830#endif
3831
3832 init_class_tab();
3833
3834 if (nfa_regcomp_start(expr, re_flags) == FAIL)
3835 return NULL;
3836
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003837 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02003838 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003839 postfix = re2post();
3840 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003841 {
3842 /* TODO: only give this error for debugging? */
3843 if (post_ptr >= post_end)
3844 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003845 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003846 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003847
3848 /*
3849 * In order to build the NFA, we parse the input regexp twice:
3850 * 1. first pass to count size (so we can allocate space)
3851 * 2. second to emit code
3852 */
3853#ifdef ENABLE_LOG
3854 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003855 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003856
3857 if (f != NULL)
3858 {
3859 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
3860 fclose(f);
3861 }
3862 }
3863#endif
3864
3865 /*
3866 * PASS 1
3867 * Count number of NFA states in "nstate". Do not build the NFA.
3868 */
3869 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02003870
3871 /* Space for compiled regexp */
3872 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
3873 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
3874 if (prog == NULL)
3875 goto fail;
3876 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003877 state_ptr = prog->state;
3878
3879 /*
3880 * PASS 2
3881 * Build the NFA
3882 */
3883 prog->start = post2nfa(postfix, post_ptr, FALSE);
3884 if (prog->start == NULL)
3885 goto fail;
3886
3887 prog->regflags = regflags;
3888 prog->engine = &nfa_regengine;
3889 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003890 prog->has_zend = nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003891#ifdef ENABLE_LOG
3892 nfa_postfix_dump(expr, OK);
3893 nfa_dump(prog);
3894#endif
3895
3896out:
3897 vim_free(post_start);
3898 post_start = post_ptr = post_end = NULL;
3899 state_ptr = NULL;
3900 return (regprog_T *)prog;
3901
3902fail:
3903 vim_free(prog);
3904 prog = NULL;
3905#ifdef ENABLE_LOG
3906 nfa_postfix_dump(expr, FAIL);
3907#endif
3908#ifdef DEBUG
3909 nfa_regengine.expr = NULL;
3910#endif
3911 goto out;
3912}
3913
3914
3915/*
3916 * Match a regexp against a string.
3917 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
3918 * Uses curbuf for line count and 'iskeyword'.
3919 *
3920 * Return TRUE if there is a match, FALSE if not.
3921 */
3922 static int
3923nfa_regexec(rmp, line, col)
3924 regmatch_T *rmp;
3925 char_u *line; /* string to match against */
3926 colnr_T col; /* column to start looking for match */
3927{
3928 reg_match = rmp;
3929 reg_mmatch = NULL;
3930 reg_maxline = 0;
3931 reg_line_lbr = FALSE;
3932 reg_buf = curbuf;
3933 reg_win = NULL;
3934 ireg_ic = rmp->rm_ic;
3935#ifdef FEAT_MBYTE
3936 ireg_icombine = FALSE;
3937#endif
3938 ireg_maxcol = 0;
3939 return (nfa_regexec_both(line, col) != 0);
3940}
3941
3942#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3943 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
3944
3945static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3946
3947/*
3948 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
3949 */
3950 static int
3951nfa_regexec_nl(rmp, line, col)
3952 regmatch_T *rmp;
3953 char_u *line; /* string to match against */
3954 colnr_T col; /* column to start looking for match */
3955{
3956 reg_match = rmp;
3957 reg_mmatch = NULL;
3958 reg_maxline = 0;
3959 reg_line_lbr = TRUE;
3960 reg_buf = curbuf;
3961 reg_win = NULL;
3962 ireg_ic = rmp->rm_ic;
3963#ifdef FEAT_MBYTE
3964 ireg_icombine = FALSE;
3965#endif
3966 ireg_maxcol = 0;
3967 return (nfa_regexec_both(line, col) != 0);
3968}
3969#endif
3970
3971
3972/*
3973 * Match a regexp against multiple lines.
3974 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3975 * Uses curbuf for line count and 'iskeyword'.
3976 *
3977 * Return zero if there is no match. Return number of lines contained in the
3978 * match otherwise.
3979 *
3980 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
3981 *
3982 * ! Also NOTE : match may actually be in another line. e.g.:
3983 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
3984 *
3985 * +-------------------------+
3986 * |a |
3987 * |b |
3988 * |c |
3989 * | |
3990 * +-------------------------+
3991 *
3992 * then nfa_regexec_multi() returns 3. while the original
3993 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
3994 *
3995 * FIXME if this behavior is not compatible.
3996 */
3997 static long
3998nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
3999 regmmatch_T *rmp;
4000 win_T *win; /* window in which to search or NULL */
4001 buf_T *buf; /* buffer in which to search */
4002 linenr_T lnum; /* nr of line to start looking for match */
4003 colnr_T col; /* column to start looking for match */
4004 proftime_T *tm UNUSED; /* timeout limit or NULL */
4005{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004006 reg_match = NULL;
4007 reg_mmatch = rmp;
4008 reg_buf = buf;
4009 reg_win = win;
4010 reg_firstlnum = lnum;
4011 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4012 reg_line_lbr = FALSE;
4013 ireg_ic = rmp->rmm_ic;
4014#ifdef FEAT_MBYTE
4015 ireg_icombine = FALSE;
4016#endif
4017 ireg_maxcol = rmp->rmm_maxcol;
4018
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004019 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004020}
4021
4022#ifdef DEBUG
4023# undef ENABLE_LOG
4024#endif