blob: 7eba2c69bae9d84a0f55dacd416a8d7e2a2d3a2e [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
Bram Moolenaar963fee22013-05-26 21:47:28 +0200164/* Number of sub expressions actually being used during execution. 1 if only
165 * the whole match (subexpr 0) is used. */
166static int nfa_nsubexpr;
167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200168static int *post_start; /* holds the postfix form of r.e. */
169static int *post_end;
170static int *post_ptr;
171
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200172static int nstate; /* Number of states in the NFA. Also used when
173 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200174static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200175
176
177static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
178static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
179static int nfa_emit_equi_class __ARGS((int c, int neg));
180static void nfa_inc __ARGS((char_u **p));
181static void nfa_dec __ARGS((char_u **p));
182static int nfa_regatom __ARGS((void));
183static int nfa_regpiece __ARGS((void));
184static int nfa_regconcat __ARGS((void));
185static int nfa_regbranch __ARGS((void));
186static int nfa_reg __ARGS((int paren));
187#ifdef DEBUG
188static void nfa_set_code __ARGS((int c));
189static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200190static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
191static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200192static void nfa_dump __ARGS((nfa_regprog_T *prog));
193#endif
194static int *re2post __ARGS((void));
195static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
196static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
197static int check_char_class __ARGS((int class, int c));
198static void st_error __ARGS((int *postfix, int *end, int *p));
199static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
200static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
201static void nfa_set_null_listids __ARGS((nfa_state_T *start));
202static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
203static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
204static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
205static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
206static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
207static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
208
209/* helper functions used when doing re2post() ... regatom() parsing */
210#define EMIT(c) do { \
211 if (post_ptr >= post_end) \
212 return FAIL; \
213 *post_ptr++ = c; \
214 } while (0)
215
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200216/*
217 * Initialize internal variables before NFA compilation.
218 * Return OK on success, FAIL otherwise.
219 */
220 static int
221nfa_regcomp_start(expr, re_flags)
222 char_u *expr;
223 int re_flags; /* see vim_regcomp() */
224{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200225 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200226 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200227
228 nstate = 0;
229 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200230 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200231 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200232
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200233 /* Some items blow up in size, such as [A-z]. Add more space for that.
234 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200235 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200236
237 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200238 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200239
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200240 post_start = (int *)lalloc(postfix_size, TRUE);
241 if (post_start == NULL)
242 return FAIL;
243 vim_memset(post_start, 0, postfix_size);
244 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200245 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246 nfa_has_zend = FALSE;
247
248 regcomp_start(expr, re_flags);
249
250 return OK;
251}
252
253/*
254 * Search between "start" and "end" and try to recognize a
255 * character class in expanded form. For example [0-9].
256 * On success, return the id the character class to be emitted.
257 * On failure, return 0 (=FAIL)
258 * Start points to the first char of the range, while end should point
259 * to the closing brace.
260 */
261 static int
262nfa_recognize_char_class(start, end, extra_newl)
263 char_u *start;
264 char_u *end;
265 int extra_newl;
266{
267 int i;
268 /* Each of these variables takes up a char in "config[]",
269 * in the order they are here. */
270 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
271 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
272 char_u *p;
273#define NCONFIGS 16
274 int classid[NCONFIGS] = {
275 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
276 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
277 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
278 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
279 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200280 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200281 char_u config[NCONFIGS][9] = {
282 "000000100", /* digit */
283 "100000100", /* non digit */
284 "011000100", /* hex-digit */
285 "111000100", /* non hex-digit */
286 "000001000", /* octal-digit */
287 "100001000", /* [^0-7] */
288 "000110110", /* [0-9A-Za-z_] */
289 "100110110", /* [^0-9A-Za-z_] */
290 "000110010", /* head of word */
291 "100110010", /* not head of word */
292 "000110000", /* alphabetic char a-z */
293 "100110000", /* non alphabetic char */
294 "000100000", /* lowercase letter */
295 "100100000", /* non lowercase */
296 "000010000", /* uppercase */
297 "100010000" /* non uppercase */
298 };
299
300 if (extra_newl == TRUE)
301 newl = TRUE;
302
303 if (*end != ']')
304 return FAIL;
305 p = start;
306 if (*p == '^')
307 {
308 not = TRUE;
309 p ++;
310 }
311
312 while (p < end)
313 {
314 if (p + 2 < end && *(p + 1) == '-')
315 {
316 switch (*p)
317 {
318 case '0':
319 if (*(p + 2) == '9')
320 {
321 o9 = TRUE;
322 break;
323 }
324 else
325 if (*(p + 2) == '7')
326 {
327 o7 = TRUE;
328 break;
329 }
330 case 'a':
331 if (*(p + 2) == 'z')
332 {
333 az = TRUE;
334 break;
335 }
336 else
337 if (*(p + 2) == 'f')
338 {
339 af = TRUE;
340 break;
341 }
342 case 'A':
343 if (*(p + 2) == 'Z')
344 {
345 AZ = TRUE;
346 break;
347 }
348 else
349 if (*(p + 2) == 'F')
350 {
351 AF = TRUE;
352 break;
353 }
354 /* FALLTHROUGH */
355 default:
356 return FAIL;
357 }
358 p += 3;
359 }
360 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
361 {
362 newl = TRUE;
363 p += 2;
364 }
365 else if (*p == '_')
366 {
367 underscore = TRUE;
368 p ++;
369 }
370 else if (*p == '\n')
371 {
372 newl = TRUE;
373 p ++;
374 }
375 else
376 return FAIL;
377 } /* while (p < end) */
378
379 if (p != end)
380 return FAIL;
381
382 /* build the config that represents the ranges we gathered */
383 STRCPY(myconfig, "000000000");
384 if (not == TRUE)
385 myconfig[0] = '1';
386 if (af == TRUE)
387 myconfig[1] = '1';
388 if (AF == TRUE)
389 myconfig[2] = '1';
390 if (az == TRUE)
391 myconfig[3] = '1';
392 if (AZ == TRUE)
393 myconfig[4] = '1';
394 if (o7 == TRUE)
395 myconfig[5] = '1';
396 if (o9 == TRUE)
397 myconfig[6] = '1';
398 if (underscore == TRUE)
399 myconfig[7] = '1';
400 if (newl == TRUE)
401 {
402 myconfig[8] = '1';
403 extra_newl = ADD_NL;
404 }
405 /* try to recognize character classes */
406 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200407 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200408 return classid[i] + extra_newl;
409
410 /* fallthrough => no success so far */
411 return FAIL;
412
413#undef NCONFIGS
414}
415
416/*
417 * Produce the bytes for equivalence class "c".
418 * Currently only handles latin1, latin9 and utf-8.
419 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
420 * equivalent to 'a OR b OR c'
421 *
422 * NOTE! When changing this function, also update reg_equi_class()
423 */
424 static int
425nfa_emit_equi_class(c, neg)
426 int c;
427 int neg;
428{
429 int first = TRUE;
430 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
431#define EMIT2(c) \
432 EMIT(c); \
433 if (neg == TRUE) { \
434 EMIT(NFA_NOT); \
435 } \
436 if (first == FALSE) \
437 EMIT(glue); \
438 else \
439 first = FALSE; \
440
441#ifdef FEAT_MBYTE
442 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
443 || STRCMP(p_enc, "iso-8859-15") == 0)
444#endif
445 {
446 switch (c)
447 {
448 case 'A': case '\300': case '\301': case '\302':
449 case '\303': case '\304': case '\305':
450 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
451 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
452 EMIT2('\305');
453 return OK;
454
455 case 'C': case '\307':
456 EMIT2('C'); EMIT2('\307');
457 return OK;
458
459 case 'E': case '\310': case '\311': case '\312': case '\313':
460 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
461 EMIT2('\312'); EMIT2('\313');
462 return OK;
463
464 case 'I': case '\314': case '\315': case '\316': case '\317':
465 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
466 EMIT2('\316'); EMIT2('\317');
467 return OK;
468
469 case 'N': case '\321':
470 EMIT2('N'); EMIT2('\321');
471 return OK;
472
473 case 'O': case '\322': case '\323': case '\324': case '\325':
474 case '\326':
475 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
476 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
477 return OK;
478
479 case 'U': case '\331': case '\332': case '\333': case '\334':
480 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
481 EMIT2('\333'); EMIT2('\334');
482 return OK;
483
484 case 'Y': case '\335':
485 EMIT2('Y'); EMIT2('\335');
486 return OK;
487
488 case 'a': case '\340': case '\341': case '\342':
489 case '\343': case '\344': case '\345':
490 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
491 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
492 EMIT2('\345');
493 return OK;
494
495 case 'c': case '\347':
496 EMIT2('c'); EMIT2('\347');
497 return OK;
498
499 case 'e': case '\350': case '\351': case '\352': case '\353':
500 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
501 EMIT2('\352'); EMIT2('\353');
502 return OK;
503
504 case 'i': case '\354': case '\355': case '\356': case '\357':
505 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
506 EMIT2('\356'); EMIT2('\357');
507 return OK;
508
509 case 'n': case '\361':
510 EMIT2('n'); EMIT2('\361');
511 return OK;
512
513 case 'o': case '\362': case '\363': case '\364': case '\365':
514 case '\366':
515 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
516 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
517 return OK;
518
519 case 'u': case '\371': case '\372': case '\373': case '\374':
520 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
521 EMIT2('\373'); EMIT2('\374');
522 return OK;
523
524 case 'y': case '\375': case '\377':
525 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
526 return OK;
527
528 default:
529 return FAIL;
530 }
531 }
532
533 EMIT(c);
534 return OK;
535#undef EMIT2
536}
537
538/*
539 * Code to parse regular expression.
540 *
541 * We try to reuse parsing functions in regexp.c to
542 * minimize surprise and keep the syntax consistent.
543 */
544
545/*
546 * Increments the pointer "p" by one (multi-byte) character.
547 */
548 static void
549nfa_inc(p)
550 char_u **p;
551{
552#ifdef FEAT_MBYTE
553 if (has_mbyte)
554 mb_ptr2char_adv(p);
555 else
556#endif
557 *p = *p + 1;
558}
559
560/*
561 * Decrements the pointer "p" by one (multi-byte) character.
562 */
563 static void
564nfa_dec(p)
565 char_u **p;
566{
567#ifdef FEAT_MBYTE
568 char_u *p2, *oldp;
569
570 if (has_mbyte)
571 {
572 oldp = *p;
573 /* Try to find the multibyte char that advances to the current
574 * position. */
575 do
576 {
577 *p = *p - 1;
578 p2 = *p;
579 mb_ptr2char_adv(&p2);
580 } while (p2 != oldp);
581 }
582#else
583 *p = *p - 1;
584#endif
585}
586
587/*
588 * Parse the lowest level.
589 *
590 * An atom can be one of a long list of items. Many atoms match one character
591 * in the text. It is often an ordinary character or a character class.
592 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
593 * is only for syntax highlighting.
594 *
595 * atom ::= ordinary-atom
596 * or \( pattern \)
597 * or \%( pattern \)
598 * or \z( pattern \)
599 */
600 static int
601nfa_regatom()
602{
603 int c;
604 int charclass;
605 int equiclass;
606 int collclass;
607 int got_coll_char;
608 char_u *p;
609 char_u *endp;
610#ifdef FEAT_MBYTE
611 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200612#endif
613 int extra = 0;
614 int first;
615 int emit_range;
616 int negated;
617 int result;
618 int startc = -1;
619 int endc = -1;
620 int oldstartc = -1;
621 int cpo_lit; /* 'cpoptions' contains 'l' flag */
622 int cpo_bsl; /* 'cpoptions' contains '\' flag */
623 int glue; /* ID that will "glue" nodes together */
624
625 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
626 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
627
628 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 switch (c)
630 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200631 case NUL:
632 syntax_error = TRUE;
633 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
634
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 case Magic('^'):
636 EMIT(NFA_BOL);
637 break;
638
639 case Magic('$'):
640 EMIT(NFA_EOL);
641#if defined(FEAT_SYN_HL) || defined(PROTO)
642 had_eol = TRUE;
643#endif
644 break;
645
646 case Magic('<'):
647 EMIT(NFA_BOW);
648 break;
649
650 case Magic('>'):
651 EMIT(NFA_EOW);
652 break;
653
654 case Magic('_'):
655 c = no_Magic(getchr());
656 if (c == '^') /* "\_^" is start-of-line */
657 {
658 EMIT(NFA_BOL);
659 break;
660 }
661 if (c == '$') /* "\_$" is end-of-line */
662 {
663 EMIT(NFA_EOL);
664#if defined(FEAT_SYN_HL) || defined(PROTO)
665 had_eol = TRUE;
666#endif
667 break;
668 }
669
670 extra = ADD_NL;
671
672 /* "\_[" is collection plus newline */
673 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200674 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675
676 /* "\_x" is character class plus newline */
677 /*FALLTHROUGH*/
678
679 /*
680 * Character classes.
681 */
682 case Magic('.'):
683 case Magic('i'):
684 case Magic('I'):
685 case Magic('k'):
686 case Magic('K'):
687 case Magic('f'):
688 case Magic('F'):
689 case Magic('p'):
690 case Magic('P'):
691 case Magic('s'):
692 case Magic('S'):
693 case Magic('d'):
694 case Magic('D'):
695 case Magic('x'):
696 case Magic('X'):
697 case Magic('o'):
698 case Magic('O'):
699 case Magic('w'):
700 case Magic('W'):
701 case Magic('h'):
702 case Magic('H'):
703 case Magic('a'):
704 case Magic('A'):
705 case Magic('l'):
706 case Magic('L'):
707 case Magic('u'):
708 case Magic('U'):
709 p = vim_strchr(classchars, no_Magic(c));
710 if (p == NULL)
711 {
712 return FAIL; /* runtime error */
713 }
714#ifdef FEAT_MBYTE
715 /* When '.' is followed by a composing char ignore the dot, so that
716 * the composing char is matched here. */
717 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
718 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200719 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720 c = getchr();
721 goto nfa_do_multibyte;
722 }
723#endif
724 EMIT(nfa_classcodes[p - classchars]);
725 if (extra == ADD_NL)
726 {
727 EMIT(NFA_NEWL);
728 EMIT(NFA_OR);
729 regflags |= RF_HASNL;
730 }
731 break;
732
733 case Magic('n'):
734 if (reg_string)
735 /* In a string "\n" matches a newline character. */
736 EMIT(NL);
737 else
738 {
739 /* In buffer text "\n" matches the end of a line. */
740 EMIT(NFA_NEWL);
741 regflags |= RF_HASNL;
742 }
743 break;
744
745 case Magic('('):
746 if (nfa_reg(REG_PAREN) == FAIL)
747 return FAIL; /* cascaded error */
748 break;
749
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750 case Magic('|'):
751 case Magic('&'):
752 case Magic(')'):
753 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200754 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200755 return FAIL;
756
757 case Magic('='):
758 case Magic('?'):
759 case Magic('+'):
760 case Magic('@'):
761 case Magic('*'):
762 case Magic('{'):
763 /* these should follow an atom, not form an atom */
764 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200765 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200766 return FAIL;
767
768 case Magic('~'): /* previous substitute pattern */
769 /* Not supported yet */
770 return FAIL;
771
772 case Magic('1'):
773 case Magic('2'):
774 case Magic('3'):
775 case Magic('4'):
776 case Magic('5'):
777 case Magic('6'):
778 case Magic('7'):
779 case Magic('8'):
780 case Magic('9'):
781 /* not supported yet */
782 return FAIL;
783
784 case Magic('z'):
785 c = no_Magic(getchr());
786 switch (c)
787 {
788 case 's':
789 EMIT(NFA_ZSTART);
790 break;
791 case 'e':
792 EMIT(NFA_ZEND);
793 nfa_has_zend = TRUE;
794 /* TODO: Currently \ze does not work properly. */
795 return FAIL;
796 /* break; */
797 case '1':
798 case '2':
799 case '3':
800 case '4':
801 case '5':
802 case '6':
803 case '7':
804 case '8':
805 case '9':
806 case '(':
807 /* \z1...\z9 and \z( not yet supported */
808 return FAIL;
809 default:
810 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200811 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 no_Magic(c));
813 return FAIL;
814 }
815 break;
816
817 case Magic('%'):
818 c = no_Magic(getchr());
819 switch (c)
820 {
821 /* () without a back reference */
822 case '(':
823 if (nfa_reg(REG_NPAREN) == FAIL)
824 return FAIL;
825 EMIT(NFA_NOPEN);
826 break;
827
828 case 'd': /* %d123 decimal */
829 case 'o': /* %o123 octal */
830 case 'x': /* %xab hex 2 */
831 case 'u': /* %uabcd hex 4 */
832 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200833 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200834 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200835
Bram Moolenaar47196582013-05-25 22:04:23 +0200836 switch (c)
837 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200838 case 'd': nr = getdecchrs(); break;
839 case 'o': nr = getoctchrs(); break;
840 case 'x': nr = gethexchrs(2); break;
841 case 'u': nr = gethexchrs(4); break;
842 case 'U': nr = gethexchrs(8); break;
843 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200844 }
845
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200846 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200847 EMSG2_RET_FAIL(
848 _("E678: Invalid character after %s%%[dxouU]"),
849 reg_magic == MAGIC_ALL);
850 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200851 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200852 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200853 break;
854
855 /* Catch \%^ and \%$ regardless of where they appear in the
856 * pattern -- regardless of whether or not it makes sense. */
857 case '^':
858 EMIT(NFA_BOF);
859 /* Not yet supported */
860 return FAIL;
861 break;
862
863 case '$':
864 EMIT(NFA_EOF);
865 /* Not yet supported */
866 return FAIL;
867 break;
868
869 case '#':
870 /* not supported yet */
871 return FAIL;
872 break;
873
874 case 'V':
875 /* not supported yet */
876 return FAIL;
877 break;
878
879 case '[':
880 /* \%[abc] not supported yet */
881 return FAIL;
882
883 default:
884 /* not supported yet */
885 return FAIL;
886 }
887 break;
888
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200889 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200890collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200891 /*
892 * Glue is emitted between several atoms from the [].
893 * It is either NFA_OR, or NFA_CONCAT.
894 *
895 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
896 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
897 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
898 * notation)
899 *
900 */
901
902
903/* Emit negation atoms, if needed.
904 * The CONCAT below merges the NOT with the previous node. */
905#define TRY_NEG() \
906 if (negated == TRUE) \
907 { \
908 EMIT(NFA_NOT); \
909 }
910
911/* Emit glue between important nodes : CONCAT or OR. */
912#define EMIT_GLUE() \
913 if (first == FALSE) \
914 EMIT(glue); \
915 else \
916 first = FALSE;
917
918 p = regparse;
919 endp = skip_anyof(p);
920 if (*endp == ']')
921 {
922 /*
923 * Try to reverse engineer character classes. For example,
924 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
925 * and perform the necessary substitutions in the NFA.
926 */
927 result = nfa_recognize_char_class(regparse, endp,
928 extra == ADD_NL);
929 if (result != FAIL)
930 {
931 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
932 EMIT(result);
933 else /* must be char class + newline */
934 {
935 EMIT(result - ADD_NL);
936 EMIT(NFA_NEWL);
937 EMIT(NFA_OR);
938 }
939 regparse = endp;
940 nfa_inc(&regparse);
941 return OK;
942 }
943 /*
944 * Failed to recognize a character class. Use the simple
945 * version that turns [abc] into 'a' OR 'b' OR 'c'
946 */
947 startc = endc = oldstartc = -1;
948 first = TRUE; /* Emitting first atom in this sequence? */
949 negated = FALSE;
950 glue = NFA_OR;
951 if (*regparse == '^') /* negated range */
952 {
953 negated = TRUE;
954 glue = NFA_CONCAT;
955 nfa_inc(&regparse);
956 }
957 if (*regparse == '-')
958 {
959 startc = '-';
960 EMIT(startc);
961 TRY_NEG();
962 EMIT_GLUE();
963 nfa_inc(&regparse);
964 }
965 /* Emit the OR branches for each character in the [] */
966 emit_range = FALSE;
967 while (regparse < endp)
968 {
969 oldstartc = startc;
970 startc = -1;
971 got_coll_char = FALSE;
972 if (*regparse == '[')
973 {
974 /* Check for [: :], [= =], [. .] */
975 equiclass = collclass = 0;
976 charclass = get_char_class(&regparse);
977 if (charclass == CLASS_NONE)
978 {
979 equiclass = get_equi_class(&regparse);
980 if (equiclass == 0)
981 collclass = get_coll_element(&regparse);
982 }
983
984 /* Character class like [:alpha:] */
985 if (charclass != CLASS_NONE)
986 {
987 switch (charclass)
988 {
989 case CLASS_ALNUM:
990 EMIT(NFA_CLASS_ALNUM);
991 break;
992 case CLASS_ALPHA:
993 EMIT(NFA_CLASS_ALPHA);
994 break;
995 case CLASS_BLANK:
996 EMIT(NFA_CLASS_BLANK);
997 break;
998 case CLASS_CNTRL:
999 EMIT(NFA_CLASS_CNTRL);
1000 break;
1001 case CLASS_DIGIT:
1002 EMIT(NFA_CLASS_DIGIT);
1003 break;
1004 case CLASS_GRAPH:
1005 EMIT(NFA_CLASS_GRAPH);
1006 break;
1007 case CLASS_LOWER:
1008 EMIT(NFA_CLASS_LOWER);
1009 break;
1010 case CLASS_PRINT:
1011 EMIT(NFA_CLASS_PRINT);
1012 break;
1013 case CLASS_PUNCT:
1014 EMIT(NFA_CLASS_PUNCT);
1015 break;
1016 case CLASS_SPACE:
1017 EMIT(NFA_CLASS_SPACE);
1018 break;
1019 case CLASS_UPPER:
1020 EMIT(NFA_CLASS_UPPER);
1021 break;
1022 case CLASS_XDIGIT:
1023 EMIT(NFA_CLASS_XDIGIT);
1024 break;
1025 case CLASS_TAB:
1026 EMIT(NFA_CLASS_TAB);
1027 break;
1028 case CLASS_RETURN:
1029 EMIT(NFA_CLASS_RETURN);
1030 break;
1031 case CLASS_BACKSPACE:
1032 EMIT(NFA_CLASS_BACKSPACE);
1033 break;
1034 case CLASS_ESCAPE:
1035 EMIT(NFA_CLASS_ESCAPE);
1036 break;
1037 }
1038 TRY_NEG();
1039 EMIT_GLUE();
1040 continue;
1041 }
1042 /* Try equivalence class [=a=] and the like */
1043 if (equiclass != 0)
1044 {
1045 result = nfa_emit_equi_class(equiclass, negated);
1046 if (result == FAIL)
1047 {
1048 /* should never happen */
1049 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1050 }
1051 EMIT_GLUE();
1052 continue;
1053 }
1054 /* Try collating class like [. .] */
1055 if (collclass != 0)
1056 {
1057 startc = collclass; /* allow [.a.]-x as a range */
1058 /* Will emit the proper atom at the end of the
1059 * while loop. */
1060 }
1061 }
1062 /* Try a range like 'a-x' or '\t-z' */
1063 if (*regparse == '-')
1064 {
1065 emit_range = TRUE;
1066 startc = oldstartc;
1067 nfa_inc(&regparse);
1068 continue; /* reading the end of the range */
1069 }
1070
1071 /* Now handle simple and escaped characters.
1072 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1073 * accepts "\t", "\e", etc., but only when the 'l' flag in
1074 * 'cpoptions' is not included.
1075 * Posix doesn't recognize backslash at all.
1076 */
1077 if (*regparse == '\\'
1078 && !cpo_bsl
1079 && regparse + 1 <= endp
1080 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1081 || (!cpo_lit
1082 && vim_strchr(REGEXP_ABBR, regparse[1])
1083 != NULL)
1084 )
1085 )
1086 {
1087 nfa_inc(&regparse);
1088
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001089 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001090 startc = reg_string ? NL : NFA_NEWL;
1091 else
1092 if (*regparse == 'd'
1093 || *regparse == 'o'
1094 || *regparse == 'x'
1095 || *regparse == 'u'
1096 || *regparse == 'U'
1097 )
1098 {
1099 /* TODO(RE) This needs more testing */
1100 startc = coll_get_char();
1101 got_coll_char = TRUE;
1102 nfa_dec(&regparse);
1103 }
1104 else
1105 {
1106 /* \r,\t,\e,\b */
1107 startc = backslash_trans(*regparse);
1108 }
1109 }
1110
1111 /* Normal printable char */
1112 if (startc == -1)
1113#ifdef FEAT_MBYTE
1114 startc = (*mb_ptr2char)(regparse);
1115#else
1116 startc = *regparse;
1117#endif
1118
1119 /* Previous char was '-', so this char is end of range. */
1120 if (emit_range)
1121 {
1122 endc = startc; startc = oldstartc;
1123 if (startc > endc)
1124 EMSG_RET_FAIL(_(e_invrange));
1125#ifdef FEAT_MBYTE
1126 if (has_mbyte && ((*mb_char2len)(startc) > 1
1127 || (*mb_char2len)(endc) > 1))
1128 {
1129 if (endc > startc + 256)
1130 EMSG_RET_FAIL(_(e_invrange));
1131 /* Emit the range. "startc" was already emitted, so
1132 * skip it. */
1133 for (c = startc + 1; c <= endc; c++)
1134 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001135 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001136 TRY_NEG();
1137 EMIT_GLUE();
1138 }
1139 emit_range = FALSE;
1140 }
1141 else
1142#endif
1143 {
1144#ifdef EBCDIC
1145 int alpha_only = FALSE;
1146
1147 /* for alphabetical range skip the gaps
1148 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1149 if (isalpha(startc) && isalpha(endc))
1150 alpha_only = TRUE;
1151#endif
1152 /* Emit the range. "startc" was already emitted, so
1153 * skip it. */
1154 for (c = startc + 1; c <= endc; c++)
1155#ifdef EBCDIC
1156 if (!alpha_only || isalpha(startc))
1157#endif
1158 {
1159 EMIT(c);
1160 TRY_NEG();
1161 EMIT_GLUE();
1162 }
1163 emit_range = FALSE;
1164 }
1165 }
1166 else
1167 {
1168 /*
1169 * This char (startc) is not part of a range. Just
1170 * emit it.
1171 *
1172 * Normally, simply emit startc. But if we get char
1173 * code=0 from a collating char, then replace it with
1174 * 0x0a.
1175 *
1176 * This is needed to completely mimic the behaviour of
1177 * the backtracking engine.
1178 */
1179 if (got_coll_char == TRUE && startc == 0)
1180 EMIT(0x0a);
1181 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001182 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001183 TRY_NEG();
1184 EMIT_GLUE();
1185 }
1186
1187 nfa_inc(&regparse);
1188 } /* while (p < endp) */
1189
1190 nfa_dec(&regparse);
1191 if (*regparse == '-') /* if last, '-' is just a char */
1192 {
1193 EMIT('-');
1194 TRY_NEG();
1195 EMIT_GLUE();
1196 }
1197 nfa_inc(&regparse);
1198
1199 if (extra == ADD_NL) /* \_[] also matches \n */
1200 {
1201 EMIT(reg_string ? NL : NFA_NEWL);
1202 TRY_NEG();
1203 EMIT_GLUE();
1204 }
1205
1206 /* skip the trailing ] */
1207 regparse = endp;
1208 nfa_inc(&regparse);
1209 if (negated == TRUE)
1210 {
1211 /* Mark end of negated char range */
1212 EMIT(NFA_END_NEG_RANGE);
1213 EMIT(NFA_CONCAT);
1214 }
1215 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001216 } /* if exists closing ] */
1217
1218 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219 {
1220 syntax_error = TRUE;
1221 EMSG_RET_FAIL(_(e_missingbracket));
1222 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001223 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001224
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 default:
1226 {
1227#ifdef FEAT_MBYTE
1228 int plen;
1229
1230nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001231 /* plen is length of current char with composing chars */
1232 if (enc_utf8 && ((*mb_char2len)(c)
1233 != (plen = (*mb_ptr2len)(old_regparse))
1234 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001235 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001236 int i = 0;
1237
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001238 /* A base character plus composing characters, or just one
1239 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001240 * This requires creating a separate atom as if enclosing
1241 * the characters in (), where NFA_COMPOSING is the ( and
1242 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243 * building the postfix form, not the NFA itself;
1244 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001245 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001246 for (;;)
1247 {
1248 EMIT(c);
1249 if (i > 0)
1250 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001251 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001252 break;
1253 c = utf_ptr2char(old_regparse + i);
1254 }
1255 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256 regparse = old_regparse + plen;
1257 }
1258 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259#endif
1260 {
1261 c = no_Magic(c);
1262 EMIT(c);
1263 }
1264 return OK;
1265 }
1266 }
1267
1268#undef TRY_NEG
1269#undef EMIT_GLUE
1270
1271 return OK;
1272}
1273
1274/*
1275 * Parse something followed by possible [*+=].
1276 *
1277 * A piece is an atom, possibly followed by a multi, an indication of how many
1278 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1279 * characters: "", "a", "aa", etc.
1280 *
1281 * piece ::= atom
1282 * or atom multi
1283 */
1284 static int
1285nfa_regpiece()
1286{
1287 int i;
1288 int op;
1289 int ret;
1290 long minval, maxval;
1291 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1292 char_u *old_regparse, *new_regparse;
1293 int c2;
1294 int *old_post_ptr, *my_post_start;
1295 int old_regnpar;
1296 int quest;
1297
1298 /* Save the current position in the regexp, so that we can use it if
1299 * <atom>{m,n} is next. */
1300 old_regparse = regparse;
1301 /* Save current number of open parenthesis, so we can use it if
1302 * <atom>{m,n} is next */
1303 old_regnpar = regnpar;
1304 /* store current pos in the postfix form, for \{m,n} involving 0s */
1305 my_post_start = post_ptr;
1306
1307 ret = nfa_regatom();
1308 if (ret == FAIL)
1309 return FAIL; /* cascaded error */
1310
1311 op = peekchr();
1312 if (re_multi_type(op) == NOT_MULTI)
1313 return OK;
1314
1315 skipchr();
1316 switch (op)
1317 {
1318 case Magic('*'):
1319 EMIT(NFA_STAR);
1320 break;
1321
1322 case Magic('+'):
1323 /*
1324 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1325 * first and only submatch would be "aaa". But the backtracking
1326 * engine interprets the plus as "try matching one more time", and
1327 * a* matches a second time at the end of the input, the empty
1328 * string.
1329 * The submatch will the empty string.
1330 *
1331 * In order to be consistent with the old engine, we disable
1332 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1333 */
1334 /* EMIT(NFA_PLUS); */
1335 regnpar = old_regnpar;
1336 regparse = old_regparse;
1337 curchr = -1;
1338 if (nfa_regatom() == FAIL)
1339 return FAIL;
1340 EMIT(NFA_STAR);
1341 EMIT(NFA_CONCAT);
1342 skipchr(); /* skip the \+ */
1343 break;
1344
1345 case Magic('@'):
1346 op = no_Magic(getchr());
1347 switch(op)
1348 {
1349 case '=':
1350 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1351 break;
1352 case '!':
1353 case '<':
1354 case '>':
1355 /* Not supported yet */
1356 return FAIL;
1357 default:
1358 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001359 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001360 return FAIL;
1361 }
1362 break;
1363
1364 case Magic('?'):
1365 case Magic('='):
1366 EMIT(NFA_QUEST);
1367 break;
1368
1369 case Magic('{'):
1370 /* a{2,5} will expand to 'aaa?a?a?'
1371 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1372 * version of '?'
1373 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1374 * parenthesis have the same id
1375 */
1376
1377 greedy = TRUE;
1378 c2 = peekchr();
1379 if (c2 == '-' || c2 == Magic('-'))
1380 {
1381 skipchr();
1382 greedy = FALSE;
1383 }
1384 if (!read_limits(&minval, &maxval))
1385 {
1386 syntax_error = TRUE;
1387 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1388 }
1389 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1390 * <atom>* */
1391 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1392 {
1393 EMIT(NFA_STAR);
1394 break;
1395 }
1396
1397 if (maxval > NFA_BRACES_MAXLIMIT)
1398 {
1399 /* This would yield a huge automaton and use too much memory.
1400 * Revert to old engine */
1401 return FAIL;
1402 }
1403
1404 /* Special case: x{0} or x{-0} */
1405 if (maxval == 0)
1406 {
1407 /* Ignore result of previous call to nfa_regatom() */
1408 post_ptr = my_post_start;
1409 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1410 EMIT(NFA_SKIP_CHAR);
1411 return OK;
1412 }
1413
1414 /* Ignore previous call to nfa_regatom() */
1415 post_ptr = my_post_start;
1416 /* Save pos after the repeated atom and the \{} */
1417 new_regparse = regparse;
1418
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001419 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1420 for (i = 0; i < maxval; i++)
1421 {
1422 /* Goto beginning of the repeated atom */
1423 regparse = old_regparse;
1424 curchr = -1;
1425 /* Restore count of parenthesis */
1426 regnpar = old_regnpar;
1427 old_post_ptr = post_ptr;
1428 if (nfa_regatom() == FAIL)
1429 return FAIL;
1430 /* after "minval" times, atoms are optional */
1431 if (i + 1 > minval)
1432 EMIT(quest);
1433 if (old_post_ptr != my_post_start)
1434 EMIT(NFA_CONCAT);
1435 }
1436
1437 /* Go to just after the repeated atom and the \{} */
1438 regparse = new_regparse;
1439 curchr = -1;
1440
1441 break;
1442
1443
1444 default:
1445 break;
1446 } /* end switch */
1447
1448 if (re_multi_type(peekchr()) != NOT_MULTI)
1449 {
1450 /* Can't have a multi follow a multi. */
1451 syntax_error = TRUE;
1452 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1453 }
1454
1455 return OK;
1456}
1457
1458/*
1459 * Parse one or more pieces, concatenated. It matches a match for the
1460 * first piece, followed by a match for the second piece, etc. Example:
1461 * "f[0-9]b", first matches "f", then a digit and then "b".
1462 *
1463 * concat ::= piece
1464 * or piece piece
1465 * or piece piece piece
1466 * etc.
1467 */
1468 static int
1469nfa_regconcat()
1470{
1471 int cont = TRUE;
1472 int first = TRUE;
1473
1474 while (cont)
1475 {
1476 switch (peekchr())
1477 {
1478 case NUL:
1479 case Magic('|'):
1480 case Magic('&'):
1481 case Magic(')'):
1482 cont = FALSE;
1483 break;
1484
1485 case Magic('Z'):
1486#ifdef FEAT_MBYTE
1487 regflags |= RF_ICOMBINE;
1488#endif
1489 skipchr_keepstart();
1490 break;
1491 case Magic('c'):
1492 regflags |= RF_ICASE;
1493 skipchr_keepstart();
1494 break;
1495 case Magic('C'):
1496 regflags |= RF_NOICASE;
1497 skipchr_keepstart();
1498 break;
1499 case Magic('v'):
1500 reg_magic = MAGIC_ALL;
1501 skipchr_keepstart();
1502 curchr = -1;
1503 break;
1504 case Magic('m'):
1505 reg_magic = MAGIC_ON;
1506 skipchr_keepstart();
1507 curchr = -1;
1508 break;
1509 case Magic('M'):
1510 reg_magic = MAGIC_OFF;
1511 skipchr_keepstart();
1512 curchr = -1;
1513 break;
1514 case Magic('V'):
1515 reg_magic = MAGIC_NONE;
1516 skipchr_keepstart();
1517 curchr = -1;
1518 break;
1519
1520 default:
1521 if (nfa_regpiece() == FAIL)
1522 return FAIL;
1523 if (first == FALSE)
1524 EMIT(NFA_CONCAT);
1525 else
1526 first = FALSE;
1527 break;
1528 }
1529 }
1530
1531 return OK;
1532}
1533
1534/*
1535 * Parse a branch, one or more concats, separated by "\&". It matches the
1536 * last concat, but only if all the preceding concats also match at the same
1537 * position. Examples:
1538 * "foobeep\&..." matches "foo" in "foobeep".
1539 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1540 *
1541 * branch ::= concat
1542 * or concat \& concat
1543 * or concat \& concat \& concat
1544 * etc.
1545 */
1546 static int
1547nfa_regbranch()
1548{
1549 int ch;
1550 int *old_post_ptr;
1551
1552 old_post_ptr = post_ptr;
1553
1554 /* First branch, possibly the only one */
1555 if (nfa_regconcat() == FAIL)
1556 return FAIL;
1557
1558 ch = peekchr();
1559 /* Try next concats */
1560 while (ch == Magic('&'))
1561 {
1562 skipchr();
1563 EMIT(NFA_NOPEN);
1564 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1565 old_post_ptr = post_ptr;
1566 if (nfa_regconcat() == FAIL)
1567 return FAIL;
1568 /* if concat is empty, skip a input char. But do emit a node */
1569 if (old_post_ptr == post_ptr)
1570 EMIT(NFA_SKIP_CHAR);
1571 EMIT(NFA_CONCAT);
1572 ch = peekchr();
1573 }
1574
1575 /* Even if a branch is empty, emit one node for it */
1576 if (old_post_ptr == post_ptr)
1577 EMIT(NFA_SKIP_CHAR);
1578
1579 return OK;
1580}
1581
1582/*
1583 * Parse a pattern, one or more branches, separated by "\|". It matches
1584 * anything that matches one of the branches. Example: "foo\|beep" matches
1585 * "foo" and matches "beep". If more than one branch matches, the first one
1586 * is used.
1587 *
1588 * pattern ::= branch
1589 * or branch \| branch
1590 * or branch \| branch \| branch
1591 * etc.
1592 */
1593 static int
1594nfa_reg(paren)
1595 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1596{
1597 int parno = 0;
1598
1599#ifdef FEAT_SYN_HL
1600#endif
1601 if (paren == REG_PAREN)
1602 {
1603 if (regnpar >= NSUBEXP) /* Too many `(' */
1604 {
1605 syntax_error = TRUE;
1606 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1607 }
1608 parno = regnpar++;
1609 }
1610
1611 if (nfa_regbranch() == FAIL)
1612 return FAIL; /* cascaded error */
1613
1614 while (peekchr() == Magic('|'))
1615 {
1616 skipchr();
1617 if (nfa_regbranch() == FAIL)
1618 return FAIL; /* cascaded error */
1619 EMIT(NFA_OR);
1620 }
1621
1622 /* Check for proper termination. */
1623 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1624 {
1625 syntax_error = TRUE;
1626 if (paren == REG_NPAREN)
1627 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1628 else
1629 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1630 }
1631 else if (paren == REG_NOPAREN && peekchr() != NUL)
1632 {
1633 syntax_error = TRUE;
1634 if (peekchr() == Magic(')'))
1635 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1636 else
1637 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1638 }
1639 /*
1640 * Here we set the flag allowing back references to this set of
1641 * parentheses.
1642 */
1643 if (paren == REG_PAREN)
1644 {
1645 had_endbrace[parno] = TRUE; /* have seen the close paren */
1646 EMIT(NFA_MOPEN + parno);
1647 }
1648
1649 return OK;
1650}
1651
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001652#ifdef DEBUG
1653static char_u code[50];
1654
1655 static void
1656nfa_set_code(c)
1657 int c;
1658{
1659 int addnl = FALSE;
1660
1661 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1662 {
1663 addnl = TRUE;
1664 c -= ADD_NL;
1665 }
1666
1667 STRCPY(code, "");
1668 switch (c)
1669 {
1670 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1671 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1672 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1673 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1674 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1675 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1676
1677 case NFA_PREV_ATOM_NO_WIDTH:
1678 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1679 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1680 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1681 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1682 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1683
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1685 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1686
1687 case NFA_MOPEN + 0:
1688 case NFA_MOPEN + 1:
1689 case NFA_MOPEN + 2:
1690 case NFA_MOPEN + 3:
1691 case NFA_MOPEN + 4:
1692 case NFA_MOPEN + 5:
1693 case NFA_MOPEN + 6:
1694 case NFA_MOPEN + 7:
1695 case NFA_MOPEN + 8:
1696 case NFA_MOPEN + 9:
1697 STRCPY(code, "NFA_MOPEN(x)");
1698 code[10] = c - NFA_MOPEN + '0';
1699 break;
1700 case NFA_MCLOSE + 0:
1701 case NFA_MCLOSE + 1:
1702 case NFA_MCLOSE + 2:
1703 case NFA_MCLOSE + 3:
1704 case NFA_MCLOSE + 4:
1705 case NFA_MCLOSE + 5:
1706 case NFA_MCLOSE + 6:
1707 case NFA_MCLOSE + 7:
1708 case NFA_MCLOSE + 8:
1709 case NFA_MCLOSE + 9:
1710 STRCPY(code, "NFA_MCLOSE(x)");
1711 code[11] = c - NFA_MCLOSE + '0';
1712 break;
1713 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1714 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1715 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1716 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1717 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1718 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1719 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1720 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1721 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1722 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1723 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1724 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1725 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1726 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1727 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1728 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1729 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1730 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1731 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1732 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1733 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1734 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1735 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1736 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1737 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1738 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1739 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1740 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1741
1742 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1743 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1744 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1745 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1746 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1747 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1748 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1749 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1750 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1751 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1752 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1753 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1754 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1755 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1756 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1757 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1758 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1759 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1760 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1761 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1762 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1763 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1764 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1765 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1766 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1767 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1768 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1769
1770 default:
1771 STRCPY(code, "CHAR(x)");
1772 code[5] = c;
1773 }
1774
1775 if (addnl == TRUE)
1776 STRCAT(code, " + NEWLINE ");
1777
1778}
1779
1780#ifdef ENABLE_LOG
1781static FILE *log_fd;
1782
1783/*
1784 * Print the postfix notation of the current regexp.
1785 */
1786 static void
1787nfa_postfix_dump(expr, retval)
1788 char_u *expr;
1789 int retval;
1790{
1791 int *p;
1792 FILE *f;
1793
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001794 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001795 if (f != NULL)
1796 {
1797 fprintf(f, "\n-------------------------\n");
1798 if (retval == FAIL)
1799 fprintf(f, ">>> NFA engine failed ... \n");
1800 else if (retval == OK)
1801 fprintf(f, ">>> NFA engine succeeded !\n");
1802 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001803 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001804 {
1805 nfa_set_code(*p);
1806 fprintf(f, "%s, ", code);
1807 }
1808 fprintf(f, "\"\nPostfix notation (int): ");
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 fprintf(f, "%d ", *p);
1811 fprintf(f, "\n\n");
1812 fclose(f);
1813 }
1814}
1815
1816/*
1817 * Print the NFA starting with a root node "state".
1818 */
1819 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001820nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 FILE *debugf;
1822 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001824 garray_T indent;
1825
1826 ga_init2(&indent, 1, 64);
1827 ga_append(&indent, '\0');
1828 nfa_print_state2(debugf, state, &indent);
1829 ga_clear(&indent);
1830}
1831
1832 static void
1833nfa_print_state2(debugf, state, indent)
1834 FILE *debugf;
1835 nfa_state_T *state;
1836 garray_T *indent;
1837{
1838 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001839
1840 if (state == NULL)
1841 return;
1842
1843 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001844
1845 /* Output indent */
1846 p = (char_u *)indent->ga_data;
1847 if (indent->ga_len >= 3)
1848 {
1849 int last = indent->ga_len - 3;
1850 char_u save[2];
1851
1852 STRNCPY(save, &p[last], 2);
1853 STRNCPY(&p[last], "+-", 2);
1854 fprintf(debugf, " %s", p);
1855 STRNCPY(&p[last], save, 2);
1856 }
1857 else
1858 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859
1860 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001861 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1862 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001863 if (state->id < 0)
1864 return;
1865
1866 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001867
1868 /* grow indent for state->out */
1869 indent->ga_len -= 1;
1870 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001871 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001872 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001873 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001874 ga_append(indent, '\0');
1875
1876 nfa_print_state2(debugf, state->out, indent);
1877
1878 /* replace last part of indent for state->out1 */
1879 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001880 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001881 ga_append(indent, '\0');
1882
1883 nfa_print_state2(debugf, state->out1, indent);
1884
1885 /* shrink indent */
1886 indent->ga_len -= 3;
1887 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001888}
1889
1890/*
1891 * Print the NFA state machine.
1892 */
1893 static void
1894nfa_dump(prog)
1895 nfa_regprog_T *prog;
1896{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001897 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898
1899 if (debugf != NULL)
1900 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001901 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 fclose(debugf);
1903 }
1904}
1905#endif /* ENABLE_LOG */
1906#endif /* DEBUG */
1907
1908/*
1909 * Parse r.e. @expr and convert it into postfix form.
1910 * Return the postfix string on success, NULL otherwise.
1911 */
1912 static int *
1913re2post()
1914{
1915 if (nfa_reg(REG_NOPAREN) == FAIL)
1916 return NULL;
1917 EMIT(NFA_MOPEN);
1918 return post_start;
1919}
1920
1921/* NB. Some of the code below is inspired by Russ's. */
1922
1923/*
1924 * Represents an NFA state plus zero or one or two arrows exiting.
1925 * if c == MATCH, no arrows out; matching state.
1926 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1927 * If c < 256, labeled arrow with character c to out.
1928 */
1929
1930static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1931
1932/*
1933 * Allocate and initialize nfa_state_T.
1934 */
1935 static nfa_state_T *
1936new_state(c, out, out1)
1937 int c;
1938 nfa_state_T *out;
1939 nfa_state_T *out1;
1940{
1941 nfa_state_T *s;
1942
1943 if (istate >= nstate)
1944 return NULL;
1945
1946 s = &state_ptr[istate++];
1947
1948 s->c = c;
1949 s->out = out;
1950 s->out1 = out1;
1951
1952 s->id = istate;
1953 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001954 s->visits = 0;
1955 s->negated = FALSE;
1956
1957 return s;
1958}
1959
1960/*
1961 * A partially built NFA without the matching state filled in.
1962 * Frag_T.start points at the start state.
1963 * Frag_T.out is a list of places that need to be set to the
1964 * next state for this fragment.
1965 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001966
1967/* Since the out pointers in the list are always
1968 * uninitialized, we use the pointers themselves
1969 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001970typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001971union Ptrlist
1972{
1973 Ptrlist *next;
1974 nfa_state_T *s;
1975};
1976
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001977struct Frag
1978{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001979 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001980 Ptrlist *out;
1981};
1982typedef struct Frag Frag_T;
1983
1984static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
1985static Ptrlist *list1 __ARGS((nfa_state_T **outp));
1986static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
1987static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
1988static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
1989static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
1990
1991/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02001992 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001993 */
1994 static Frag_T
1995frag(start, out)
1996 nfa_state_T *start;
1997 Ptrlist *out;
1998{
Bram Moolenaar053bb602013-05-20 13:55:21 +02001999 Frag_T n;
2000
2001 n.start = start;
2002 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003 return n;
2004}
2005
2006/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007 * Create singleton list containing just outp.
2008 */
2009 static Ptrlist *
2010list1(outp)
2011 nfa_state_T **outp;
2012{
2013 Ptrlist *l;
2014
2015 l = (Ptrlist *)outp;
2016 l->next = NULL;
2017 return l;
2018}
2019
2020/*
2021 * Patch the list of states at out to point to start.
2022 */
2023 static void
2024patch(l, s)
2025 Ptrlist *l;
2026 nfa_state_T *s;
2027{
2028 Ptrlist *next;
2029
2030 for (; l; l = next)
2031 {
2032 next = l->next;
2033 l->s = s;
2034 }
2035}
2036
2037
2038/*
2039 * Join the two lists l1 and l2, returning the combination.
2040 */
2041 static Ptrlist *
2042append(l1, l2)
2043 Ptrlist *l1;
2044 Ptrlist *l2;
2045{
2046 Ptrlist *oldl1;
2047
2048 oldl1 = l1;
2049 while (l1->next)
2050 l1 = l1->next;
2051 l1->next = l2;
2052 return oldl1;
2053}
2054
2055/*
2056 * Stack used for transforming postfix form into NFA.
2057 */
2058static Frag_T empty;
2059
2060 static void
2061st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002062 int *postfix UNUSED;
2063 int *end UNUSED;
2064 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002066#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 FILE *df;
2068 int *p2;
2069
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002070 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002071 if (df)
2072 {
2073 fprintf(df, "Error popping the stack!\n");
2074#ifdef DEBUG
2075 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2076#endif
2077 fprintf(df, "Postfix form is: ");
2078#ifdef DEBUG
2079 for (p2 = postfix; p2 < end; p2++)
2080 {
2081 nfa_set_code(*p2);
2082 fprintf(df, "%s, ", code);
2083 }
2084 nfa_set_code(*p);
2085 fprintf(df, "\nCurrent position is: ");
2086 for (p2 = postfix; p2 <= p; p2 ++)
2087 {
2088 nfa_set_code(*p2);
2089 fprintf(df, "%s, ", code);
2090 }
2091#else
2092 for (p2 = postfix; p2 < end; p2++)
2093 {
2094 fprintf(df, "%d, ", *p2);
2095 }
2096 fprintf(df, "\nCurrent position is: ");
2097 for (p2 = postfix; p2 <= p; p2 ++)
2098 {
2099 fprintf(df, "%d, ", *p2);
2100 }
2101#endif
2102 fprintf(df, "\n--------------------------\n");
2103 fclose(df);
2104 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002105#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002106 EMSG(_("E874: (NFA) Could not pop the stack !"));
2107}
2108
2109/*
2110 * Push an item onto the stack.
2111 */
2112 static void
2113st_push(s, p, stack_end)
2114 Frag_T s;
2115 Frag_T **p;
2116 Frag_T *stack_end;
2117{
2118 Frag_T *stackp = *p;
2119
2120 if (stackp >= stack_end)
2121 return;
2122 *stackp = s;
2123 *p = *p + 1;
2124}
2125
2126/*
2127 * Pop an item from the stack.
2128 */
2129 static Frag_T
2130st_pop(p, stack)
2131 Frag_T **p;
2132 Frag_T *stack;
2133{
2134 Frag_T *stackp;
2135
2136 *p = *p - 1;
2137 stackp = *p;
2138 if (stackp < stack)
2139 return empty;
2140 return **p;
2141}
2142
2143/*
2144 * Convert a postfix form into its equivalent NFA.
2145 * Return the NFA start state on success, NULL otherwise.
2146 */
2147 static nfa_state_T *
2148post2nfa(postfix, end, nfa_calc_size)
2149 int *postfix;
2150 int *end;
2151 int nfa_calc_size;
2152{
2153 int *p;
2154 int mopen;
2155 int mclose;
2156 Frag_T *stack = NULL;
2157 Frag_T *stackp = NULL;
2158 Frag_T *stack_end = NULL;
2159 Frag_T e1;
2160 Frag_T e2;
2161 Frag_T e;
2162 nfa_state_T *s;
2163 nfa_state_T *s1;
2164 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002165 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166
2167 if (postfix == NULL)
2168 return NULL;
2169
Bram Moolenaar053bb602013-05-20 13:55:21 +02002170#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002171#define POP() st_pop(&stackp, stack); \
2172 if (stackp < stack) \
2173 { \
2174 st_error(postfix, end, p); \
2175 return NULL; \
2176 }
2177
2178 if (nfa_calc_size == FALSE)
2179 {
2180 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002181 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002183 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002184 }
2185
2186 for (p = postfix; p < end; ++p)
2187 {
2188 switch (*p)
2189 {
2190 case NFA_CONCAT:
2191 /* Catenation.
2192 * Pay attention: this operator does not exist
2193 * in the r.e. itself (it is implicit, really).
2194 * It is added when r.e. is translated to postfix
2195 * form in re2post().
2196 *
2197 * No new state added here. */
2198 if (nfa_calc_size == TRUE)
2199 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002200 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 break;
2202 }
2203 e2 = POP();
2204 e1 = POP();
2205 patch(e1.out, e2.start);
2206 PUSH(frag(e1.start, e2.out));
2207 break;
2208
2209 case NFA_NOT:
2210 /* Negation of a character */
2211 if (nfa_calc_size == TRUE)
2212 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002213 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002214 break;
2215 }
2216 e1 = POP();
2217 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002218#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002219 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002221#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002222 PUSH(e1);
2223 break;
2224
2225 case NFA_OR:
2226 /* Alternation */
2227 if (nfa_calc_size == TRUE)
2228 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002229 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002230 break;
2231 }
2232 e2 = POP();
2233 e1 = POP();
2234 s = new_state(NFA_SPLIT, e1.start, e2.start);
2235 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002236 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237 PUSH(frag(s, append(e1.out, e2.out)));
2238 break;
2239
2240 case NFA_STAR:
2241 /* Zero or more */
2242 if (nfa_calc_size == TRUE)
2243 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002244 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002245 break;
2246 }
2247 e = POP();
2248 s = new_state(NFA_SPLIT, e.start, NULL);
2249 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002250 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002251 patch(e.out, s);
2252 PUSH(frag(s, list1(&s->out1)));
2253 break;
2254
2255 case NFA_QUEST:
2256 /* one or zero atoms=> greedy match */
2257 if (nfa_calc_size == TRUE)
2258 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002259 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002260 break;
2261 }
2262 e = POP();
2263 s = new_state(NFA_SPLIT, e.start, NULL);
2264 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002265 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 PUSH(frag(s, append(e.out, list1(&s->out1))));
2267 break;
2268
2269 case NFA_QUEST_NONGREEDY:
2270 /* zero or one atoms => non-greedy match */
2271 if (nfa_calc_size == TRUE)
2272 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002273 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274 break;
2275 }
2276 e = POP();
2277 s = new_state(NFA_SPLIT, NULL, e.start);
2278 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002279 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002280 PUSH(frag(s, append(e.out, list1(&s->out))));
2281 break;
2282
2283 case NFA_PLUS:
2284 /* One or more */
2285 if (nfa_calc_size == TRUE)
2286 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002287 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288 break;
2289 }
2290 e = POP();
2291 s = new_state(NFA_SPLIT, e.start, NULL);
2292 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002293 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002294 patch(e.out, s);
2295 PUSH(frag(e.start, list1(&s->out1)));
2296 break;
2297
2298 case NFA_SKIP_CHAR:
2299 /* Symbol of 0-length, Used in a repetition
2300 * with max/min count of 0 */
2301 if (nfa_calc_size == TRUE)
2302 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002303 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 break;
2305 }
2306 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2307 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002308 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 PUSH(frag(s, list1(&s->out)));
2310 break;
2311
2312 case NFA_PREV_ATOM_NO_WIDTH:
2313 /* The \@= operator: match the preceding atom with 0 width.
2314 * Surrounds the preceding atom with START_INVISIBLE and
2315 * END_INVISIBLE, similarly to MOPEN.
2316 */
2317 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002318 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002319
2320 if (nfa_calc_size == TRUE)
2321 {
2322 nstate += 2;
2323 break;
2324 }
2325 e = POP();
2326 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2327 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002328 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329 patch(e.out, s1);
2330
2331 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2332 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002333 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 PUSH(frag(s, list1(&s1->out)));
2335 break;
2336
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002337#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002338 case NFA_COMPOSING: /* char with composing char */
2339#if 0
2340 /* TODO */
2341 if (regflags & RF_ICOMBINE)
2342 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002343 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002344 }
2345#endif
2346 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002347#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002348
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349 case NFA_MOPEN + 0: /* Submatch */
2350 case NFA_MOPEN + 1:
2351 case NFA_MOPEN + 2:
2352 case NFA_MOPEN + 3:
2353 case NFA_MOPEN + 4:
2354 case NFA_MOPEN + 5:
2355 case NFA_MOPEN + 6:
2356 case NFA_MOPEN + 7:
2357 case NFA_MOPEN + 8:
2358 case NFA_MOPEN + 9:
2359 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002360 if (nfa_calc_size == TRUE)
2361 {
2362 nstate += 2;
2363 break;
2364 }
2365
2366 mopen = *p;
2367 switch (*p)
2368 {
2369 case NFA_NOPEN:
2370 mclose = NFA_NCLOSE;
2371 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002372#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002373 case NFA_COMPOSING:
2374 mclose = NFA_END_COMPOSING;
2375 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002376#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002377 default:
2378 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2379 mclose = *p + NSUBEXP;
2380 break;
2381 }
2382
2383 /* Allow "NFA_MOPEN" as a valid postfix representation for
2384 * the empty regexp "". In this case, the NFA will be
2385 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2386 * empty groups of parenthesis, and empty mbyte chars */
2387 if (stackp == stack)
2388 {
2389 s = new_state(mopen, NULL, NULL);
2390 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002391 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002392 s1 = new_state(mclose, NULL, NULL);
2393 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002394 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002395 patch(list1(&s->out), s1);
2396 PUSH(frag(s, list1(&s1->out)));
2397 break;
2398 }
2399
2400 /* At least one node was emitted before NFA_MOPEN, so
2401 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2402 e = POP();
2403 s = new_state(mopen, e.start, NULL); /* `(' */
2404 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002405 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002406
2407 s1 = new_state(mclose, NULL, NULL); /* `)' */
2408 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002409 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002410 patch(e.out, s1);
2411
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002412#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002413 if (mopen == NFA_COMPOSING)
2414 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002416#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002417
2418 PUSH(frag(s, list1(&s1->out)));
2419 break;
2420
2421 case NFA_ZSTART:
2422 case NFA_ZEND:
2423 default:
2424 /* Operands */
2425 if (nfa_calc_size == TRUE)
2426 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002427 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002428 break;
2429 }
2430 s = new_state(*p, NULL, NULL);
2431 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002432 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433 PUSH(frag(s, list1(&s->out)));
2434 break;
2435
2436 } /* switch(*p) */
2437
2438 } /* for(p = postfix; *p; ++p) */
2439
2440 if (nfa_calc_size == TRUE)
2441 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002442 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002443 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 }
2445
2446 e = POP();
2447 if (stackp != stack)
2448 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2449
2450 if (istate >= nstate)
2451 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2452
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002453 matchstate = &state_ptr[istate++]; /* the match state */
2454 matchstate->c = NFA_MATCH;
2455 matchstate->out = matchstate->out1 = NULL;
2456
2457 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002458 ret = e.start;
2459
2460theend:
2461 vim_free(stack);
2462 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463
2464#undef POP1
2465#undef PUSH1
2466#undef POP2
2467#undef PUSH2
2468#undef POP
2469#undef PUSH
2470}
2471
2472/****************************************************************
2473 * NFA execution code.
2474 ****************************************************************/
2475
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002476typedef struct
2477{
2478 int in_use; /* number of subexpr with useful info */
2479
2480 /* When REG_MULTI is TRUE multilist is used, otherwise linelist. */
2481 union
2482 {
2483 struct multipos
2484 {
2485 lpos_T start;
2486 lpos_T end;
2487 } multilist[NSUBEXP];
2488 struct linepos
2489 {
2490 char_u *start;
2491 char_u *end;
2492 } linelist[NSUBEXP];
2493 };
2494} regsub_T;
2495
Bram Moolenaar963fee22013-05-26 21:47:28 +02002496/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002497typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002498{
2499 nfa_state_T *state;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002500 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002501} nfa_thread_T;
2502
Bram Moolenaar963fee22013-05-26 21:47:28 +02002503/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504typedef struct
2505{
Bram Moolenaar4b417062013-05-25 20:19:50 +02002506 nfa_thread_T *t;
2507 int n;
2508} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002509
Bram Moolenaar963fee22013-05-26 21:47:28 +02002510/* Used during execution: whether a match has been found. */
2511static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002512
Bram Moolenaar963fee22013-05-26 21:47:28 +02002513static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int off, int lid));
Bram Moolenaar963fee22013-05-26 21:47:28 +02002514static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int lid, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515
2516 static void
Bram Moolenaar963fee22013-05-26 21:47:28 +02002517addstate(l, state, m, off, lid)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002518 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 nfa_state_T *state; /* state to update */
2520 regsub_T *m; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002521 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522 int lid;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002523{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002524 int subidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002525 nfa_thread_T *lastthread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002526 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002527 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002528 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002529 int i;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002530
2531 if (l == NULL || state == NULL)
2532 return;
2533
2534 switch (state->c)
2535 {
2536 case NFA_SPLIT:
2537 case NFA_NOT:
2538 case NFA_NOPEN:
2539 case NFA_NCLOSE:
2540 case NFA_MCLOSE:
2541 case NFA_MCLOSE + 1:
2542 case NFA_MCLOSE + 2:
2543 case NFA_MCLOSE + 3:
2544 case NFA_MCLOSE + 4:
2545 case NFA_MCLOSE + 5:
2546 case NFA_MCLOSE + 6:
2547 case NFA_MCLOSE + 7:
2548 case NFA_MCLOSE + 8:
2549 case NFA_MCLOSE + 9:
2550 /* Do not remember these nodes in list "thislist" or "nextlist" */
2551 break;
2552
2553 default:
2554 if (state->lastlist == lid)
2555 {
2556 if (++state->visits > 2)
2557 return;
2558 }
2559 else
2560 {
2561 /* add the state to the list */
2562 state->lastlist = lid;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002563 lastthread = &l->t[l->n++];
2564 lastthread->state = state;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002565 lastthread->sub.in_use = m->in_use;
2566 if (m->in_use > 0)
2567 {
2568 /* Copy the match start and end positions. */
2569 if (REG_MULTI)
2570 mch_memmove(&lastthread->sub.multilist[0],
2571 &m->multilist[0],
2572 sizeof(struct multipos) * m->in_use);
2573 else
2574 mch_memmove(&lastthread->sub.linelist[0],
2575 &m->linelist[0],
2576 sizeof(struct linepos) * m->in_use);
2577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002578 }
2579 }
2580
2581#ifdef ENABLE_LOG
2582 nfa_set_code(state->c);
2583 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n",
2584 abs(state->id), code, state->c);
2585#endif
2586 switch (state->c)
2587 {
2588 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002589 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002590 break;
2591
2592 case NFA_SPLIT:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002593 addstate(l, state->out, m, off, lid);
2594 addstate(l, state->out1, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002595 break;
2596
2597 case NFA_SKIP_CHAR:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002598 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 break;
2600
2601#if 0
2602 case NFA_END_NEG_RANGE:
2603 /* Nothing to handle here. nfa_regmatch() will take care of it */
2604 break;
2605
2606 case NFA_NOT:
2607 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2608#ifdef ENABLE_LOG
2609 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2610#endif
2611 break;
2612
2613 case NFA_COMPOSING:
2614 /* nfa_regmatch() will match all the bytes of this composing char. */
2615 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616#endif
2617
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618 case NFA_NOPEN:
2619 case NFA_NCLOSE:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002620 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002621 break;
2622
2623 /* If this state is reached, then a recursive call of nfa_regmatch()
2624 * succeeded. the next call saves the found submatches in the
2625 * first state after the "invisible" branch. */
2626#if 0
2627 case NFA_END_INVISIBLE:
2628 break;
2629#endif
2630
2631 case NFA_MOPEN + 0:
2632 case NFA_MOPEN + 1:
2633 case NFA_MOPEN + 2:
2634 case NFA_MOPEN + 3:
2635 case NFA_MOPEN + 4:
2636 case NFA_MOPEN + 5:
2637 case NFA_MOPEN + 6:
2638 case NFA_MOPEN + 7:
2639 case NFA_MOPEN + 8:
2640 case NFA_MOPEN + 9:
2641 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002642 if (state->c == NFA_ZSTART)
2643 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002644 else
2645 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002646
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002647 /* Set the position (with "off") in the subexpression. Save and
2648 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649 if (REG_MULTI)
2650 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002651 if (subidx < m->in_use)
2652 {
2653 save_lpos = m->multilist[subidx].start;
2654 save_in_use = -1;
2655 }
2656 else
2657 {
2658 save_in_use = m->in_use;
2659 for (i = m->in_use; i < subidx; ++i)
2660 {
2661 m->multilist[i].start.lnum = -1;
2662 m->multilist[i].end.lnum = -1;
2663 }
2664 m->in_use = subidx + 1;
2665 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002666 if (off == -1)
2667 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002668 m->multilist[subidx].start.lnum = reglnum + 1;
2669 m->multilist[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002670 }
2671 else
2672 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002673 m->multilist[subidx].start.lnum = reglnum;
2674 m->multilist[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002675 (colnr_T)(reginput - regline + off);
2676 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002677 }
2678 else
2679 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002680 if (subidx < m->in_use)
2681 {
2682 save_ptr = m->linelist[subidx].start;
2683 save_in_use = -1;
2684 }
2685 else
2686 {
2687 save_in_use = m->in_use;
2688 for (i = m->in_use; i < subidx; ++i)
2689 {
2690 m->linelist[i].start = NULL;
2691 m->linelist[i].end = NULL;
2692 }
2693 m->in_use = subidx + 1;
2694 }
Bram Moolenaar963fee22013-05-26 21:47:28 +02002695 m->linelist[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696 }
2697
Bram Moolenaar963fee22013-05-26 21:47:28 +02002698 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002700 if (save_in_use == -1)
2701 {
2702 if (REG_MULTI)
2703 m->multilist[subidx].start = save_lpos;
2704 else
2705 m->linelist[subidx].start = save_ptr;
2706 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002707 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002708 m->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002709 break;
2710
2711 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002712 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002713 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002714 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715 break;
2716 }
2717 case NFA_MCLOSE + 1:
2718 case NFA_MCLOSE + 2:
2719 case NFA_MCLOSE + 3:
2720 case NFA_MCLOSE + 4:
2721 case NFA_MCLOSE + 5:
2722 case NFA_MCLOSE + 6:
2723 case NFA_MCLOSE + 7:
2724 case NFA_MCLOSE + 8:
2725 case NFA_MCLOSE + 9:
2726 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002727 if (state->c == NFA_ZEND)
2728 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002729 else
2730 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002732 /* We don't fill in gaps here, there must have been an MOPEN that
2733 * has done that. */
2734 save_in_use = m->in_use;
2735 if (m->in_use <= subidx)
2736 m->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002737 if (REG_MULTI)
2738 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002739 save_lpos = m->multilist[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002740 if (off == -1)
2741 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002742 m->multilist[subidx].end.lnum = reglnum + 1;
2743 m->multilist[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002744 }
2745 else
2746 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002747 m->multilist[subidx].end.lnum = reglnum;
2748 m->multilist[subidx].end.col =
2749 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002750 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002751 }
2752 else
2753 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002754 save_ptr = m->linelist[subidx].end;
2755 m->linelist[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002756 }
2757
Bram Moolenaar963fee22013-05-26 21:47:28 +02002758 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002759
2760 if (REG_MULTI)
Bram Moolenaar963fee22013-05-26 21:47:28 +02002761 m->multilist[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762 else
Bram Moolenaar963fee22013-05-26 21:47:28 +02002763 m->linelist[subidx].end = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002764 m->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765 break;
2766 }
2767}
2768
2769/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002770 * Like addstate(), but the new state(s) are put at position "*ip".
2771 * Used for zero-width matches, next state to use is the added one.
2772 * This makes sure the order of states to be tried does not change, which
2773 * matters for alternatives.
2774 */
2775 static void
Bram Moolenaar963fee22013-05-26 21:47:28 +02002776addstate_here(l, state, m, lid, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002777 nfa_list_T *l; /* runtime state list */
2778 nfa_state_T *state; /* state to update */
2779 regsub_T *m; /* pointers to subexpressions */
2780 int lid;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002781 int *ip;
2782{
2783 int tlen = l->n;
2784 int count;
2785 int i = *ip;
2786
2787 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar963fee22013-05-26 21:47:28 +02002788 addstate(l, state, m, 0, lid);
Bram Moolenaar4b417062013-05-25 20:19:50 +02002789
2790 /* when "*ip" was at the end of the list, nothing to do */
2791 if (i + 1 == tlen)
2792 return;
2793
2794 /* re-order to put the new state at the current position */
2795 count = l->n - tlen;
2796 if (count > 1)
2797 {
2798 /* make space for new states, then move them from the
2799 * end to the current position */
2800 mch_memmove(&(l->t[i + count]),
2801 &(l->t[i + 1]),
2802 sizeof(nfa_thread_T) * (l->n - i - 1));
2803 mch_memmove(&(l->t[i]),
2804 &(l->t[l->n - 1]),
2805 sizeof(nfa_thread_T) * count);
2806 }
2807 else
2808 {
2809 /* overwrite the current state */
2810 l->t[i] = l->t[l->n - 1];
2811 }
2812 --l->n;
2813 *ip = i - 1;
2814}
2815
2816/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002817 * Check character class "class" against current character c.
2818 */
2819 static int
2820check_char_class(class, c)
2821 int class;
2822 int c;
2823{
2824 switch (class)
2825 {
2826 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002827 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 return OK;
2829 break;
2830 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002831 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832 return OK;
2833 break;
2834 case NFA_CLASS_BLANK:
2835 if (c == ' ' || c == '\t')
2836 return OK;
2837 break;
2838 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002839 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840 return OK;
2841 break;
2842 case NFA_CLASS_DIGIT:
2843 if (VIM_ISDIGIT(c))
2844 return OK;
2845 break;
2846 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002847 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002848 return OK;
2849 break;
2850 case NFA_CLASS_LOWER:
2851 if (MB_ISLOWER(c))
2852 return OK;
2853 break;
2854 case NFA_CLASS_PRINT:
2855 if (vim_isprintc(c))
2856 return OK;
2857 break;
2858 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002859 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860 return OK;
2861 break;
2862 case NFA_CLASS_SPACE:
2863 if ((c >=9 && c <= 13) || (c == ' '))
2864 return OK;
2865 break;
2866 case NFA_CLASS_UPPER:
2867 if (MB_ISUPPER(c))
2868 return OK;
2869 break;
2870 case NFA_CLASS_XDIGIT:
2871 if (vim_isxdigit(c))
2872 return OK;
2873 break;
2874 case NFA_CLASS_TAB:
2875 if (c == '\t')
2876 return OK;
2877 break;
2878 case NFA_CLASS_RETURN:
2879 if (c == '\r')
2880 return OK;
2881 break;
2882 case NFA_CLASS_BACKSPACE:
2883 if (c == '\b')
2884 return OK;
2885 break;
2886 case NFA_CLASS_ESCAPE:
2887 if (c == '\033')
2888 return OK;
2889 break;
2890
2891 default:
2892 /* should not be here :P */
2893 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2894 }
2895 return FAIL;
2896}
2897
2898/*
2899 * Set all NFA nodes' list ID equal to -1.
2900 */
2901 static void
2902nfa_set_neg_listids(start)
2903 nfa_state_T *start;
2904{
2905 if (start == NULL)
2906 return;
2907 if (start->lastlist >= 0)
2908 {
2909 start->lastlist = -1;
2910 nfa_set_neg_listids(start->out);
2911 nfa_set_neg_listids(start->out1);
2912 }
2913}
2914
2915/*
2916 * Set all NFA nodes' list ID equal to 0.
2917 */
2918 static void
2919nfa_set_null_listids(start)
2920 nfa_state_T *start;
2921{
2922 if (start == NULL)
2923 return;
2924 if (start->lastlist == -1)
2925 {
2926 start->lastlist = 0;
2927 nfa_set_null_listids(start->out);
2928 nfa_set_null_listids(start->out1);
2929 }
2930}
2931
2932/*
2933 * Save list IDs for all NFA states in "list".
2934 */
2935 static void
2936nfa_save_listids(start, list)
2937 nfa_state_T *start;
2938 int *list;
2939{
2940 if (start == NULL)
2941 return;
2942 if (start->lastlist != -1)
2943 {
2944 list[abs(start->id)] = start->lastlist;
2945 start->lastlist = -1;
2946 nfa_save_listids(start->out, list);
2947 nfa_save_listids(start->out1, list);
2948 }
2949}
2950
2951/*
2952 * Restore list IDs from "list" to all NFA states.
2953 */
2954 static void
2955nfa_restore_listids(start, list)
2956 nfa_state_T *start;
2957 int *list;
2958{
2959 if (start == NULL)
2960 return;
2961 if (start->lastlist == -1)
2962 {
2963 start->lastlist = list[abs(start->id)];
2964 nfa_restore_listids(start->out, list);
2965 nfa_restore_listids(start->out1, list);
2966 }
2967}
2968
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002969static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
2970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002971/*
2972 * Main matching routine.
2973 *
2974 * Run NFA to determine whether it matches reginput.
2975 *
2976 * Return TRUE if there is a match, FALSE otherwise.
2977 * Note: Caller must ensure that: start != NULL.
2978 */
2979 static int
2980nfa_regmatch(start, submatch, m)
2981 nfa_state_T *start;
2982 regsub_T *submatch;
2983 regsub_T *m;
2984{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002985 int result;
2986 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002987 int flag = 0;
2988 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002989 int go_to_nextline = FALSE;
2990 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002991 char_u *old_reginput = NULL;
2992 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002993 nfa_list_T list[3];
2994 nfa_list_T *listtbl[2][2];
2995 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002996 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02002997 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002998 nfa_list_T *thislist;
2999 nfa_list_T *nextlist;
3000 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003001 int *listids = NULL;
3002 int j = 0;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003003#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003004 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003005
3006 if (debug == NULL)
3007 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003008 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003009 return FALSE;
3010 }
3011#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003012 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003013
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003014 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003015 size = (nstate + 1) * sizeof(nfa_thread_T);
3016 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
3017 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
3018 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003019 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3020 goto theend;
3021 vim_memset(list[0].t, 0, size);
3022 vim_memset(list[1].t, 0, size);
3023 vim_memset(list[2].t, 0, size);
3024
3025#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003026 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003027 if (log_fd != NULL)
3028 {
3029 fprintf(log_fd, "**********************************\n");
3030 nfa_set_code(start->c);
3031 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3032 abs(start->id), code);
3033 fprintf(log_fd, "**********************************\n");
3034 }
3035 else
3036 {
3037 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3038 log_fd = stderr;
3039 }
3040#endif
3041
3042 thislist = &list[0];
3043 thislist->n = 0;
3044 nextlist = &list[1];
3045 nextlist->n = 0;
3046 neglist = &list[2];
3047 neglist->n = 0;
3048#ifdef ENABLE_LOG
3049 fprintf(log_fd, "(---) STARTSTATE\n");
3050#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003051 addstate(thislist, start, m, 0, listid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003052
3053 /* There are two cases when the NFA advances: 1. input char matches the
3054 * NFA node and 2. input char does not match the NFA node, but the next
3055 * node is NFA_NOT. The following macro calls addstate() according to
3056 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3057 listtbl[0][0] = NULL;
3058 listtbl[0][1] = neglist;
3059 listtbl[1][0] = nextlist;
3060 listtbl[1][1] = NULL;
3061#define ADD_POS_NEG_STATE(node) \
3062 ll = listtbl[result ? 1 : 0][node->negated]; \
3063 if (ll != NULL) \
Bram Moolenaar963fee22013-05-26 21:47:28 +02003064 addstate(ll, node->out , &t->sub, clen, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003065
3066
3067 /*
3068 * Run for each character.
3069 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003070 for (;;)
3071 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003072 int curc;
3073 int clen;
3074
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003075#ifdef FEAT_MBYTE
3076 if (has_mbyte)
3077 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003078 curc = (*mb_ptr2char)(reginput);
3079 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003080 }
3081 else
3082#endif
3083 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003084 curc = *reginput;
3085 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003086 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003087 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003088 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003089 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003090 go_to_nextline = FALSE;
3091 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003092
3093 /* swap lists */
3094 thislist = &list[flag];
3095 nextlist = &list[flag ^= 1];
3096 nextlist->n = 0; /* `clear' nextlist */
3097 listtbl[1][0] = nextlist;
3098 ++listid;
3099
3100#ifdef ENABLE_LOG
3101 fprintf(log_fd, "------------------------------------------\n");
3102 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003103 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003104 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003105 {
3106 int i;
3107
3108 for (i = 0; i < thislist->n; i++)
3109 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3110 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003111 fprintf(log_fd, "\n");
3112#endif
3113
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003114#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003115 fprintf(debug, "\n-------------------\n");
3116#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003117 /*
3118 * If the state lists are empty we can stop.
3119 */
3120 if (thislist->n == 0 && neglist->n == 0)
3121 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122
3123 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003124 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003125 {
3126 if (neglist->n > 0)
3127 {
3128 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003129 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003130 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003131 }
3132 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003133 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003134
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003135#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003136 nfa_set_code(t->state->c);
3137 fprintf(debug, "%s, ", code);
3138#endif
3139#ifdef ENABLE_LOG
3140 nfa_set_code(t->state->c);
3141 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3142 code, (int)t->state->c);
3143#endif
3144
3145 /*
3146 * Handle the possible codes of the current state.
3147 * The most important is NFA_MATCH.
3148 */
3149 switch (t->state->c)
3150 {
3151 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003152 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003153 submatch->in_use = t->sub.in_use;
3154 if (REG_MULTI)
3155 for (j = 0; j < submatch->in_use; j++)
3156 {
3157 submatch->multilist[j].start = t->sub.multilist[j].start;
3158 submatch->multilist[j].end = t->sub.multilist[j].end;
3159 }
3160 else
3161 for (j = 0; j < submatch->in_use; j++)
3162 {
3163 submatch->linelist[j].start = t->sub.linelist[j].start;
3164 submatch->linelist[j].end = t->sub.linelist[j].end;
3165 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003166#ifdef ENABLE_LOG
3167 for (j = 0; j < 4; j++)
3168 if (REG_MULTI)
3169 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
3170 j,
3171 t->sub.startpos[j].col,
3172 (int)t->sub.startpos[j].lnum,
3173 t->sub.endpos[j].col,
3174 (int)t->sub.endpos[j].lnum);
3175 else
3176 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
3177 j,
3178 (char *)t->sub.start[j],
3179 (char *)t->sub.end[j]);
3180 fprintf(log_fd, "\n");
3181#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003182 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003183 * states at this position. When the list of states is going
3184 * to be empty quit without advancing, so that "reginput" is
3185 * correct. */
3186 if (nextlist->n == 0 && neglist->n == 0)
3187 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003188 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189
3190 case NFA_END_INVISIBLE:
3191 /* This is only encountered after a NFA_START_INVISIBLE node.
3192 * They surround a zero-width group, used with "\@=" and "\&".
3193 * If we got here, it means that the current "invisible" group
3194 * finished successfully, so return control to the parent
3195 * nfa_regmatch(). Submatches are stored in *m, and used in
3196 * the parent call. */
3197 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003198 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003199 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 else
3201 {
3202 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003203 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 }
3205 break;
3206
3207 case NFA_START_INVISIBLE:
3208 /* Save global variables, and call nfa_regmatch() to check if
3209 * the current concat matches at this position. The concat
3210 * ends with the node NFA_END_INVISIBLE */
3211 old_reginput = reginput;
3212 old_regline = regline;
3213 old_reglnum = reglnum;
3214 if (listids == NULL)
3215 {
3216 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3217 if (listids == NULL)
3218 {
3219 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3220 return 0;
3221 }
3222 }
3223#ifdef ENABLE_LOG
3224 if (log_fd != stderr)
3225 fclose(log_fd);
3226 log_fd = NULL;
3227#endif
3228 /* Have to clear the listid field of the NFA nodes, so that
3229 * nfa_regmatch() and addstate() can run properly after
3230 * recursion. */
3231 nfa_save_listids(start, listids);
3232 nfa_set_null_listids(start);
3233 result = nfa_regmatch(t->state->out, submatch, m);
3234 nfa_set_neg_listids(start);
3235 nfa_restore_listids(start, listids);
3236
3237#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003238 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239 if (log_fd != NULL)
3240 {
3241 fprintf(log_fd, "****************************\n");
3242 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3243 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3244 fprintf(log_fd, "****************************\n");
3245 }
3246 else
3247 {
3248 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3249 log_fd = stderr;
3250 }
3251#endif
3252 if (result == TRUE)
3253 {
3254 /* Restore position in input text */
3255 reginput = old_reginput;
3256 regline = old_regline;
3257 reglnum = old_reglnum;
3258 /* Copy submatch info from the recursive call */
3259 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003260 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003262 t->sub.multilist[j].start = m->multilist[j].start;
3263 t->sub.multilist[j].end = m->multilist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003264 }
3265 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003266 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003267 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003268 t->sub.linelist[j].start = m->linelist[j].start;
3269 t->sub.linelist[j].end = m->linelist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003271 t->sub.in_use = m->in_use;
3272
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003274 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003275 listid, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276 }
3277 else
3278 {
3279 /* continue with next input char */
3280 reginput = old_reginput;
3281 }
3282 break;
3283
3284 case NFA_BOL:
3285 if (reginput == regline)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003286 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003287 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288 break;
3289
3290 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003291 if (curc == NUL)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003292 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003293 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 break;
3295
3296 case NFA_BOW:
3297 {
3298 int bow = TRUE;
3299
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003300 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 bow = FALSE;
3302#ifdef FEAT_MBYTE
3303 else if (has_mbyte)
3304 {
3305 int this_class;
3306
3307 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003308 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003309 if (this_class <= 1)
3310 bow = FALSE;
3311 else if (reg_prev_class() == this_class)
3312 bow = FALSE;
3313 }
3314#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003315 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003316 || (reginput > regline
3317 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 bow = FALSE;
3319 if (bow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003320 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003321 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322 break;
3323 }
3324
3325 case NFA_EOW:
3326 {
3327 int eow = TRUE;
3328
3329 if (reginput == regline)
3330 eow = FALSE;
3331#ifdef FEAT_MBYTE
3332 else if (has_mbyte)
3333 {
3334 int this_class, prev_class;
3335
3336 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003337 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003338 prev_class = reg_prev_class();
3339 if (this_class == prev_class
3340 || prev_class == 0 || prev_class == 1)
3341 eow = FALSE;
3342 }
3343#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003344 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003345 || (reginput[0] != NUL
3346 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003347 eow = FALSE;
3348 if (eow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003349 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003350 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003351 break;
3352 }
3353
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003354#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003355 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003356 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003357 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003358 int len = 0;
3359 nfa_state_T *end;
3360 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003361 int cchars[MAX_MCO];
3362 int ccount = 0;
3363 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003364
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003366 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003367 if (utf_iscomposing(sta->c))
3368 {
3369 /* Only match composing character(s), ignore base
3370 * character. Used for ".{composing}" and "{composing}"
3371 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003372 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003373 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003374 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003375 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003376 /* If \Z was present, then ignore composing characters.
3377 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003378 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003379 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003380 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003381 else
3382 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003383 while (sta->c != NFA_END_COMPOSING)
3384 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003386
3387 /* Check base character matches first, unless ignored. */
3388 else if (len > 0 || mc == sta->c)
3389 {
3390 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003391 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003392 len += mb_char2len(mc);
3393 sta = sta->out;
3394 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003395
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003396 /* We don't care about the order of composing characters.
3397 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003398 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003399 {
3400 mc = mb_ptr2char(reginput + len);
3401 cchars[ccount++] = mc;
3402 len += mb_char2len(mc);
3403 if (ccount == MAX_MCO)
3404 break;
3405 }
3406
3407 /* Check that each composing char in the pattern matches a
3408 * composing char in the text. We do not check if all
3409 * composing chars are matched. */
3410 result = OK;
3411 while (sta->c != NFA_END_COMPOSING)
3412 {
3413 for (j = 0; j < ccount; ++j)
3414 if (cchars[j] == sta->c)
3415 break;
3416 if (j == ccount)
3417 {
3418 result = FAIL;
3419 break;
3420 }
3421 sta = sta->out;
3422 }
3423 }
3424 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003425 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003426
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003427 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428 ADD_POS_NEG_STATE(end);
3429 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003430 }
3431#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003432
3433 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003434 if (curc == NUL && !reg_line_lbr && REG_MULTI
3435 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003436 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003437 go_to_nextline = TRUE;
3438 /* Pass -1 for the offset, which means taking the position
3439 * at the start of the next line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003440 addstate(nextlist, t->state->out, &t->sub, -1, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003441 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003442 else if (curc == '\n' && reg_line_lbr)
3443 {
3444 /* match \n as if it is an ordinary character */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003445 addstate(nextlist, t->state->out, &t->sub, 1, listid + 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003446 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003447 break;
3448
3449 case NFA_CLASS_ALNUM:
3450 case NFA_CLASS_ALPHA:
3451 case NFA_CLASS_BLANK:
3452 case NFA_CLASS_CNTRL:
3453 case NFA_CLASS_DIGIT:
3454 case NFA_CLASS_GRAPH:
3455 case NFA_CLASS_LOWER:
3456 case NFA_CLASS_PRINT:
3457 case NFA_CLASS_PUNCT:
3458 case NFA_CLASS_SPACE:
3459 case NFA_CLASS_UPPER:
3460 case NFA_CLASS_XDIGIT:
3461 case NFA_CLASS_TAB:
3462 case NFA_CLASS_RETURN:
3463 case NFA_CLASS_BACKSPACE:
3464 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003465 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 ADD_POS_NEG_STATE(t->state);
3467 break;
3468
3469 case NFA_END_NEG_RANGE:
3470 /* This follows a series of negated nodes, like:
3471 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003472 if (curc > 0)
3473 addstate(nextlist, t->state->out, &t->sub, clen,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003474 listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 break;
3476
3477 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003478 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003479 if (curc > 0)
3480 addstate(nextlist, t->state->out, &t->sub, clen,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003481 listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 break;
3483
3484 /*
3485 * Character classes like \a for alpha, \d for digit etc.
3486 */
3487 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003488 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 ADD_POS_NEG_STATE(t->state);
3490 break;
3491
3492 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003493 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 ADD_POS_NEG_STATE(t->state);
3495 break;
3496
3497 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003498 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499 ADD_POS_NEG_STATE(t->state);
3500 break;
3501
3502 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003503 result = !VIM_ISDIGIT(curc)
3504 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003505 ADD_POS_NEG_STATE(t->state);
3506 break;
3507
3508 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003509 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 ADD_POS_NEG_STATE(t->state);
3511 break;
3512
3513 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003514 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003515 ADD_POS_NEG_STATE(t->state);
3516 break;
3517
3518 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003519 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 ADD_POS_NEG_STATE(t->state);
3521 break;
3522
3523 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003524 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003525 ADD_POS_NEG_STATE(t->state);
3526 break;
3527
3528 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003529 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530 ADD_POS_NEG_STATE(t->state);
3531 break;
3532
3533 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003534 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003535 ADD_POS_NEG_STATE(t->state);
3536 break;
3537
3538 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003539 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540 ADD_POS_NEG_STATE(t->state);
3541 break;
3542
3543 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003544 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003545 ADD_POS_NEG_STATE(t->state);
3546 break;
3547
3548 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003549 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003550 ADD_POS_NEG_STATE(t->state);
3551 break;
3552
3553 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003554 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 ADD_POS_NEG_STATE(t->state);
3556 break;
3557
3558 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003559 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003560 ADD_POS_NEG_STATE(t->state);
3561 break;
3562
3563 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003564 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 ADD_POS_NEG_STATE(t->state);
3566 break;
3567
3568 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003569 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003570 ADD_POS_NEG_STATE(t->state);
3571 break;
3572
3573 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003574 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003575 ADD_POS_NEG_STATE(t->state);
3576 break;
3577
3578 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003579 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 ADD_POS_NEG_STATE(t->state);
3581 break;
3582
3583 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003584 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003585 ADD_POS_NEG_STATE(t->state);
3586 break;
3587
3588 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003589 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003590 ADD_POS_NEG_STATE(t->state);
3591 break;
3592
3593 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003594 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595 ADD_POS_NEG_STATE(t->state);
3596 break;
3597
3598 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003599 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 ADD_POS_NEG_STATE(t->state);
3601 break;
3602
3603 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003604 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 ADD_POS_NEG_STATE(t->state);
3606 break;
3607
3608 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003609 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 ADD_POS_NEG_STATE(t->state);
3611 break;
3612
3613 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003614 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615 ADD_POS_NEG_STATE(t->state);
3616 break;
3617
Bram Moolenaar12e40142013-05-21 15:33:41 +02003618 case NFA_MOPEN + 0:
3619 case NFA_MOPEN + 1:
3620 case NFA_MOPEN + 2:
3621 case NFA_MOPEN + 3:
3622 case NFA_MOPEN + 4:
3623 case NFA_MOPEN + 5:
3624 case NFA_MOPEN + 6:
3625 case NFA_MOPEN + 7:
3626 case NFA_MOPEN + 8:
3627 case NFA_MOPEN + 9:
3628 /* handled below */
3629 break;
3630
3631 case NFA_SKIP_CHAR:
3632 case NFA_ZSTART:
3633 /* TODO: should not happen? */
3634 break;
3635
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003637 {
3638 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003639
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003640 /* TODO: put this in #ifdef later */
3641 if (c < -256)
3642 EMSGN("INTERNAL: Negative state char: %ld", c);
3643 if (is_Magic(c))
3644 c = un_Magic(c);
3645 result = (c == curc);
3646
3647 if (!result && ireg_ic)
3648 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003649#ifdef FEAT_MBYTE
3650 /* If there is a composing character which is not being
3651 * ignored there can be no match. Match with composing
3652 * character uses NFA_COMPOSING above. */
3653 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003654 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003655 result = FALSE;
3656#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003657 ADD_POS_NEG_STATE(t->state);
3658 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003659 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660 }
3661
3662 } /* for (thislist = thislist; thislist->state; thislist++) */
3663
Bram Moolenaare23febd2013-05-26 18:40:14 +02003664 /* Look for the start of a match in the current position by adding the
3665 * start state to the list of states.
3666 * The first found match is the leftmost one, thus the order of states
3667 * matters!
3668 * Do not add the start state in recursive calls of nfa_regmatch(),
3669 * because recursive calls should only start in the first position.
3670 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003671 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaare23febd2013-05-26 18:40:14 +02003672 && reglnum == 0 && clen != 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003673 {
3674#ifdef ENABLE_LOG
3675 fprintf(log_fd, "(---) STARTSTATE\n");
3676#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003677 addstate(nextlist, start, m, clen, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 }
3679
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003680#ifdef ENABLE_LOG
3681 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003682 {
3683 int i;
3684
3685 for (i = 0; i < thislist->n; i++)
3686 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3687 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688 fprintf(log_fd, "\n");
3689#endif
3690
3691nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003692 /* Advance to the next character, or advance to the next line, or
3693 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003694 if (clen != 0)
3695 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003696 else if (go_to_nextline)
3697 reg_nextline();
3698 else
3699 break;
3700 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003701
3702#ifdef ENABLE_LOG
3703 if (log_fd != stderr)
3704 fclose(log_fd);
3705 log_fd = NULL;
3706#endif
3707
3708theend:
3709 /* Free memory */
3710 vim_free(list[0].t);
3711 vim_free(list[1].t);
3712 vim_free(list[2].t);
3713 list[0].t = list[1].t = list[2].t = NULL;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003714 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003715#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003716#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717 fclose(debug);
3718#endif
3719
Bram Moolenaar963fee22013-05-26 21:47:28 +02003720 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003721}
3722
3723/*
3724 * Try match of "prog" with at regline["col"].
3725 * Returns 0 for failure, number of lines contained in the match otherwise.
3726 */
3727 static long
3728nfa_regtry(start, col)
3729 nfa_state_T *start;
3730 colnr_T col;
3731{
3732 int i;
3733 regsub_T sub, m;
3734#ifdef ENABLE_LOG
3735 FILE *f;
3736#endif
3737
3738 reginput = regline + col;
3739 need_clear_subexpr = TRUE;
3740
3741#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003742 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003743 if (f != NULL)
3744 {
3745 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3746 fprintf(f, " =======================================================\n");
3747#ifdef DEBUG
3748 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3749#endif
3750 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3751 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003752 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003753 fprintf(f, "\n\n");
3754 fclose(f);
3755 }
3756 else
3757 EMSG(_("Could not open temporary log file for writing "));
3758#endif
3759
3760 if (REG_MULTI)
3761 {
3762 /* Use 0xff to set lnum to -1 */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003763 vim_memset(sub.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
3764 vim_memset(m.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003765 }
3766 else
3767 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003768 vim_memset(sub.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
3769 vim_memset(m.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003770 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003771 sub.in_use = 0;
3772 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003773
3774 if (nfa_regmatch(start, &sub, &m) == FALSE)
3775 return 0;
3776
3777 cleanup_subexpr();
3778 if (REG_MULTI)
3779 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003780 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003781 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003782 reg_startpos[i] = sub.multilist[i].start;
3783 reg_endpos[i] = sub.multilist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003784 }
3785
3786 if (reg_startpos[0].lnum < 0)
3787 {
3788 reg_startpos[0].lnum = 0;
3789 reg_startpos[0].col = col;
3790 }
3791 if (reg_endpos[0].lnum < 0)
3792 {
3793 reg_endpos[0].lnum = reglnum;
3794 reg_endpos[0].col = (int)(reginput - regline);
3795 }
3796 else
3797 /* Use line number of "\ze". */
3798 reglnum = reg_endpos[0].lnum;
3799 }
3800 else
3801 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003802 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003803 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003804 reg_startp[i] = sub.linelist[i].start;
3805 reg_endp[i] = sub.linelist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003806 }
3807
3808 if (reg_startp[0] == NULL)
3809 reg_startp[0] = regline + col;
3810 if (reg_endp[0] == NULL)
3811 reg_endp[0] = reginput;
3812 }
3813
3814 return 1 + reglnum;
3815}
3816
3817/*
3818 * Match a regexp against a string ("line" points to the string) or multiple
3819 * lines ("line" is NULL, use reg_getline()).
3820 *
3821 * Returns 0 for failure, number of lines contained in the match otherwise.
3822 */
3823 static long
3824nfa_regexec_both(line, col)
3825 char_u *line;
3826 colnr_T col; /* column to start looking for match */
3827{
3828 nfa_regprog_T *prog;
3829 long retval = 0L;
3830 int i;
3831
3832 if (REG_MULTI)
3833 {
3834 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3835 line = reg_getline((linenr_T)0); /* relative to the cursor */
3836 reg_startpos = reg_mmatch->startpos;
3837 reg_endpos = reg_mmatch->endpos;
3838 }
3839 else
3840 {
3841 prog = (nfa_regprog_T *)reg_match->regprog;
3842 reg_startp = reg_match->startp;
3843 reg_endp = reg_match->endp;
3844 }
3845
3846 /* Be paranoid... */
3847 if (prog == NULL || line == NULL)
3848 {
3849 EMSG(_(e_null));
3850 goto theend;
3851 }
3852
3853 /* If the start column is past the maximum column: no need to try. */
3854 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3855 goto theend;
3856
3857 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3858 if (prog->regflags & RF_ICASE)
3859 ireg_ic = TRUE;
3860 else if (prog->regflags & RF_NOICASE)
3861 ireg_ic = FALSE;
3862
3863#ifdef FEAT_MBYTE
3864 /* If pattern contains "\Z" overrule value of ireg_icombine */
3865 if (prog->regflags & RF_ICOMBINE)
3866 ireg_icombine = TRUE;
3867#endif
3868
3869 regline = line;
3870 reglnum = 0; /* relative to line */
3871
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003872 nfa_has_zend = prog->has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003873 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003874
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003875 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003876 for (i = 0; i < nstate; ++i)
3877 {
3878 prog->state[i].id = i;
3879 prog->state[i].lastlist = 0;
3880 prog->state[i].visits = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881 }
3882
3883 retval = nfa_regtry(prog->start, col);
3884
3885theend:
3886 return retval;
3887}
3888
3889/*
3890 * Compile a regular expression into internal code for the NFA matcher.
3891 * Returns the program in allocated space. Returns NULL for an error.
3892 */
3893 static regprog_T *
3894nfa_regcomp(expr, re_flags)
3895 char_u *expr;
3896 int re_flags;
3897{
Bram Moolenaaraae48832013-05-25 21:18:34 +02003898 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003899 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900 int *postfix;
3901
3902 if (expr == NULL)
3903 return NULL;
3904
3905#ifdef DEBUG
3906 nfa_regengine.expr = expr;
3907#endif
3908
3909 init_class_tab();
3910
3911 if (nfa_regcomp_start(expr, re_flags) == FAIL)
3912 return NULL;
3913
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003914 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02003915 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003916 postfix = re2post();
3917 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003918 {
3919 /* TODO: only give this error for debugging? */
3920 if (post_ptr >= post_end)
3921 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003923 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003924
3925 /*
3926 * In order to build the NFA, we parse the input regexp twice:
3927 * 1. first pass to count size (so we can allocate space)
3928 * 2. second to emit code
3929 */
3930#ifdef ENABLE_LOG
3931 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003932 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003933
3934 if (f != NULL)
3935 {
3936 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
3937 fclose(f);
3938 }
3939 }
3940#endif
3941
3942 /*
3943 * PASS 1
3944 * Count number of NFA states in "nstate". Do not build the NFA.
3945 */
3946 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02003947
3948 /* Space for compiled regexp */
3949 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
3950 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
3951 if (prog == NULL)
3952 goto fail;
3953 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003954 state_ptr = prog->state;
3955
3956 /*
3957 * PASS 2
3958 * Build the NFA
3959 */
3960 prog->start = post2nfa(postfix, post_ptr, FALSE);
3961 if (prog->start == NULL)
3962 goto fail;
3963
3964 prog->regflags = regflags;
3965 prog->engine = &nfa_regengine;
3966 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003967 prog->has_zend = nfa_has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003968 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003969#ifdef ENABLE_LOG
3970 nfa_postfix_dump(expr, OK);
3971 nfa_dump(prog);
3972#endif
3973
3974out:
3975 vim_free(post_start);
3976 post_start = post_ptr = post_end = NULL;
3977 state_ptr = NULL;
3978 return (regprog_T *)prog;
3979
3980fail:
3981 vim_free(prog);
3982 prog = NULL;
3983#ifdef ENABLE_LOG
3984 nfa_postfix_dump(expr, FAIL);
3985#endif
3986#ifdef DEBUG
3987 nfa_regengine.expr = NULL;
3988#endif
3989 goto out;
3990}
3991
3992
3993/*
3994 * Match a regexp against a string.
3995 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
3996 * Uses curbuf for line count and 'iskeyword'.
3997 *
3998 * Return TRUE if there is a match, FALSE if not.
3999 */
4000 static int
4001nfa_regexec(rmp, line, col)
4002 regmatch_T *rmp;
4003 char_u *line; /* string to match against */
4004 colnr_T col; /* column to start looking for match */
4005{
4006 reg_match = rmp;
4007 reg_mmatch = NULL;
4008 reg_maxline = 0;
4009 reg_line_lbr = FALSE;
4010 reg_buf = curbuf;
4011 reg_win = NULL;
4012 ireg_ic = rmp->rm_ic;
4013#ifdef FEAT_MBYTE
4014 ireg_icombine = FALSE;
4015#endif
4016 ireg_maxcol = 0;
4017 return (nfa_regexec_both(line, col) != 0);
4018}
4019
4020#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4021 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4022
4023static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4024
4025/*
4026 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4027 */
4028 static int
4029nfa_regexec_nl(rmp, line, col)
4030 regmatch_T *rmp;
4031 char_u *line; /* string to match against */
4032 colnr_T col; /* column to start looking for match */
4033{
4034 reg_match = rmp;
4035 reg_mmatch = NULL;
4036 reg_maxline = 0;
4037 reg_line_lbr = TRUE;
4038 reg_buf = curbuf;
4039 reg_win = NULL;
4040 ireg_ic = rmp->rm_ic;
4041#ifdef FEAT_MBYTE
4042 ireg_icombine = FALSE;
4043#endif
4044 ireg_maxcol = 0;
4045 return (nfa_regexec_both(line, col) != 0);
4046}
4047#endif
4048
4049
4050/*
4051 * Match a regexp against multiple lines.
4052 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4053 * Uses curbuf for line count and 'iskeyword'.
4054 *
4055 * Return zero if there is no match. Return number of lines contained in the
4056 * match otherwise.
4057 *
4058 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4059 *
4060 * ! Also NOTE : match may actually be in another line. e.g.:
4061 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4062 *
4063 * +-------------------------+
4064 * |a |
4065 * |b |
4066 * |c |
4067 * | |
4068 * +-------------------------+
4069 *
4070 * then nfa_regexec_multi() returns 3. while the original
4071 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4072 *
4073 * FIXME if this behavior is not compatible.
4074 */
4075 static long
4076nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4077 regmmatch_T *rmp;
4078 win_T *win; /* window in which to search or NULL */
4079 buf_T *buf; /* buffer in which to search */
4080 linenr_T lnum; /* nr of line to start looking for match */
4081 colnr_T col; /* column to start looking for match */
4082 proftime_T *tm UNUSED; /* timeout limit or NULL */
4083{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004084 reg_match = NULL;
4085 reg_mmatch = rmp;
4086 reg_buf = buf;
4087 reg_win = win;
4088 reg_firstlnum = lnum;
4089 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4090 reg_line_lbr = FALSE;
4091 ireg_ic = rmp->rmm_ic;
4092#ifdef FEAT_MBYTE
4093 ireg_icombine = FALSE;
4094#endif
4095 ireg_maxcol = rmp->rmm_maxcol;
4096
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004097 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004098}
4099
4100#ifdef DEBUG
4101# undef ENABLE_LOG
4102#endif