blob: 2304ce40fc21c92516df7d164dc9e4891a60a99b [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
32/* Upper limit allowed for {m,n} repetitions handled by NFA */
33#define NFA_BRACES_MAXLIMIT 10
34/* For allocating space for the postfix representation */
35#define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020036
37enum
38{
39 NFA_SPLIT = -1024,
40 NFA_MATCH,
41 NFA_SKIP_CHAR, /* matches a 0-length char */
42 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
43
44 NFA_CONCAT,
45 NFA_OR,
46 NFA_STAR,
47 NFA_PLUS,
48 NFA_QUEST,
49 NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */
50 NFA_NOT, /* used for [^ab] negated char ranges */
51
52 NFA_BOL, /* ^ Begin line */
53 NFA_EOL, /* $ End line */
54 NFA_BOW, /* \< Begin word */
55 NFA_EOW, /* \> End word */
56 NFA_BOF, /* \%^ Begin file */
57 NFA_EOF, /* \%$ End file */
58 NFA_NEWL,
59 NFA_ZSTART, /* Used for \zs */
60 NFA_ZEND, /* Used for \ze */
61 NFA_NOPEN, /* Start of subexpression marked with \%( */
62 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
63 NFA_START_INVISIBLE,
64 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020065 NFA_COMPOSING, /* Next nodes in NFA are part of the
66 composing multibyte char */
67 NFA_END_COMPOSING, /* End of a composing char in the NFA */
68
69 /* The following are used only in the postfix form, not in the NFA */
70 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
71 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
72 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
73 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
74 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
75
Bram Moolenaar5714b802013-05-28 22:03:20 +020076 NFA_BACKREF1, /* \1 */
77 NFA_BACKREF2, /* \2 */
78 NFA_BACKREF3, /* \3 */
79 NFA_BACKREF4, /* \4 */
80 NFA_BACKREF5, /* \5 */
81 NFA_BACKREF6, /* \6 */
82 NFA_BACKREF7, /* \7 */
83 NFA_BACKREF8, /* \8 */
84 NFA_BACKREF9, /* \9 */
85 NFA_SKIP, /* Skip characters */
86
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020087 NFA_MOPEN,
88 NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
89
90 /* NFA_FIRST_NL */
91 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
92 NFA_ANYOF, /* Match any character in this string. */
93 NFA_ANYBUT, /* Match any character not in this string. */
94 NFA_IDENT, /* Match identifier char */
95 NFA_SIDENT, /* Match identifier char but no digit */
96 NFA_KWORD, /* Match keyword char */
97 NFA_SKWORD, /* Match word char but no digit */
98 NFA_FNAME, /* Match file name char */
99 NFA_SFNAME, /* Match file name char but no digit */
100 NFA_PRINT, /* Match printable char */
101 NFA_SPRINT, /* Match printable char but no digit */
102 NFA_WHITE, /* Match whitespace char */
103 NFA_NWHITE, /* Match non-whitespace char */
104 NFA_DIGIT, /* Match digit char */
105 NFA_NDIGIT, /* Match non-digit char */
106 NFA_HEX, /* Match hex char */
107 NFA_NHEX, /* Match non-hex char */
108 NFA_OCTAL, /* Match octal char */
109 NFA_NOCTAL, /* Match non-octal char */
110 NFA_WORD, /* Match word char */
111 NFA_NWORD, /* Match non-word char */
112 NFA_HEAD, /* Match head char */
113 NFA_NHEAD, /* Match non-head char */
114 NFA_ALPHA, /* Match alpha char */
115 NFA_NALPHA, /* Match non-alpha char */
116 NFA_LOWER, /* Match lowercase char */
117 NFA_NLOWER, /* Match non-lowercase char */
118 NFA_UPPER, /* Match uppercase char */
119 NFA_NUPPER, /* Match non-uppercase char */
120 NFA_FIRST_NL = NFA_ANY + ADD_NL,
121 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
122
123 /* Character classes [:alnum:] etc */
124 NFA_CLASS_ALNUM,
125 NFA_CLASS_ALPHA,
126 NFA_CLASS_BLANK,
127 NFA_CLASS_CNTRL,
128 NFA_CLASS_DIGIT,
129 NFA_CLASS_GRAPH,
130 NFA_CLASS_LOWER,
131 NFA_CLASS_PRINT,
132 NFA_CLASS_PUNCT,
133 NFA_CLASS_SPACE,
134 NFA_CLASS_UPPER,
135 NFA_CLASS_XDIGIT,
136 NFA_CLASS_TAB,
137 NFA_CLASS_RETURN,
138 NFA_CLASS_BACKSPACE,
139 NFA_CLASS_ESCAPE
140};
141
142/* Keep in sync with classchars. */
143static int nfa_classcodes[] = {
144 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
145 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
146 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
147 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
148 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
149 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
150 NFA_UPPER, NFA_NUPPER
151};
152
153static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
154
155/*
156 * NFA errors can be of 3 types:
157 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
158 * silently and revert the to backtracking engine.
159 * syntax_error = FALSE;
160 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
161 * The NFA engine displays an error message, and nothing else happens.
162 * syntax_error = TRUE
163 * *** Unsupported features, when the input regexp uses an operator that is not
164 * implemented in the NFA. The NFA engine fails silently, and reverts to the
165 * old backtracking engine.
166 * syntax_error = FALSE
167 * "The NFA fails" means that "compiling the regexp with the NFA fails":
168 * nfa_regcomp() returns FAIL.
169 */
170static int syntax_error = FALSE;
171
172/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200173static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200174
Bram Moolenaar963fee22013-05-26 21:47:28 +0200175/* Number of sub expressions actually being used during execution. 1 if only
176 * the whole match (subexpr 0) is used. */
177static int nfa_nsubexpr;
178
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200179static int *post_start; /* holds the postfix form of r.e. */
180static int *post_end;
181static int *post_ptr;
182
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200183static int nstate; /* Number of states in the NFA. Also used when
184 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200185static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200186
187
188static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
189static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
190static int nfa_emit_equi_class __ARGS((int c, int neg));
191static void nfa_inc __ARGS((char_u **p));
192static void nfa_dec __ARGS((char_u **p));
193static int nfa_regatom __ARGS((void));
194static int nfa_regpiece __ARGS((void));
195static int nfa_regconcat __ARGS((void));
196static int nfa_regbranch __ARGS((void));
197static int nfa_reg __ARGS((int paren));
198#ifdef DEBUG
199static void nfa_set_code __ARGS((int c));
200static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200201static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
202static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200203static void nfa_dump __ARGS((nfa_regprog_T *prog));
204#endif
205static int *re2post __ARGS((void));
206static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
207static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
208static int check_char_class __ARGS((int class, int c));
209static void st_error __ARGS((int *postfix, int *end, int *p));
210static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
211static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
212static void nfa_set_null_listids __ARGS((nfa_state_T *start));
213static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
214static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
215static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
216static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
217static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
218static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
219
220/* helper functions used when doing re2post() ... regatom() parsing */
221#define EMIT(c) do { \
222 if (post_ptr >= post_end) \
223 return FAIL; \
224 *post_ptr++ = c; \
225 } while (0)
226
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200227/*
228 * Initialize internal variables before NFA compilation.
229 * Return OK on success, FAIL otherwise.
230 */
231 static int
232nfa_regcomp_start(expr, re_flags)
233 char_u *expr;
234 int re_flags; /* see vim_regcomp() */
235{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200236 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200237 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200238
239 nstate = 0;
240 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200241 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200242 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200243
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200244 /* Some items blow up in size, such as [A-z]. Add more space for that.
245 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200246 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200247
248 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200250
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200251 post_start = (int *)lalloc(postfix_size, TRUE);
252 if (post_start == NULL)
253 return FAIL;
254 vim_memset(post_start, 0, postfix_size);
255 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200256 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257 nfa_has_zend = FALSE;
258
259 regcomp_start(expr, re_flags);
260
261 return OK;
262}
263
264/*
265 * Search between "start" and "end" and try to recognize a
266 * character class in expanded form. For example [0-9].
267 * On success, return the id the character class to be emitted.
268 * On failure, return 0 (=FAIL)
269 * Start points to the first char of the range, while end should point
270 * to the closing brace.
271 */
272 static int
273nfa_recognize_char_class(start, end, extra_newl)
274 char_u *start;
275 char_u *end;
276 int extra_newl;
277{
278 int i;
279 /* Each of these variables takes up a char in "config[]",
280 * in the order they are here. */
281 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
282 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
283 char_u *p;
284#define NCONFIGS 16
285 int classid[NCONFIGS] = {
286 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
287 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
288 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
289 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
290 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200291 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292 char_u config[NCONFIGS][9] = {
293 "000000100", /* digit */
294 "100000100", /* non digit */
295 "011000100", /* hex-digit */
296 "111000100", /* non hex-digit */
297 "000001000", /* octal-digit */
298 "100001000", /* [^0-7] */
299 "000110110", /* [0-9A-Za-z_] */
300 "100110110", /* [^0-9A-Za-z_] */
301 "000110010", /* head of word */
302 "100110010", /* not head of word */
303 "000110000", /* alphabetic char a-z */
304 "100110000", /* non alphabetic char */
305 "000100000", /* lowercase letter */
306 "100100000", /* non lowercase */
307 "000010000", /* uppercase */
308 "100010000" /* non uppercase */
309 };
310
311 if (extra_newl == TRUE)
312 newl = TRUE;
313
314 if (*end != ']')
315 return FAIL;
316 p = start;
317 if (*p == '^')
318 {
319 not = TRUE;
320 p ++;
321 }
322
323 while (p < end)
324 {
325 if (p + 2 < end && *(p + 1) == '-')
326 {
327 switch (*p)
328 {
329 case '0':
330 if (*(p + 2) == '9')
331 {
332 o9 = TRUE;
333 break;
334 }
335 else
336 if (*(p + 2) == '7')
337 {
338 o7 = TRUE;
339 break;
340 }
341 case 'a':
342 if (*(p + 2) == 'z')
343 {
344 az = TRUE;
345 break;
346 }
347 else
348 if (*(p + 2) == 'f')
349 {
350 af = TRUE;
351 break;
352 }
353 case 'A':
354 if (*(p + 2) == 'Z')
355 {
356 AZ = TRUE;
357 break;
358 }
359 else
360 if (*(p + 2) == 'F')
361 {
362 AF = TRUE;
363 break;
364 }
365 /* FALLTHROUGH */
366 default:
367 return FAIL;
368 }
369 p += 3;
370 }
371 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
372 {
373 newl = TRUE;
374 p += 2;
375 }
376 else if (*p == '_')
377 {
378 underscore = TRUE;
379 p ++;
380 }
381 else if (*p == '\n')
382 {
383 newl = TRUE;
384 p ++;
385 }
386 else
387 return FAIL;
388 } /* while (p < end) */
389
390 if (p != end)
391 return FAIL;
392
393 /* build the config that represents the ranges we gathered */
394 STRCPY(myconfig, "000000000");
395 if (not == TRUE)
396 myconfig[0] = '1';
397 if (af == TRUE)
398 myconfig[1] = '1';
399 if (AF == TRUE)
400 myconfig[2] = '1';
401 if (az == TRUE)
402 myconfig[3] = '1';
403 if (AZ == TRUE)
404 myconfig[4] = '1';
405 if (o7 == TRUE)
406 myconfig[5] = '1';
407 if (o9 == TRUE)
408 myconfig[6] = '1';
409 if (underscore == TRUE)
410 myconfig[7] = '1';
411 if (newl == TRUE)
412 {
413 myconfig[8] = '1';
414 extra_newl = ADD_NL;
415 }
416 /* try to recognize character classes */
417 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200418 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200419 return classid[i] + extra_newl;
420
421 /* fallthrough => no success so far */
422 return FAIL;
423
424#undef NCONFIGS
425}
426
427/*
428 * Produce the bytes for equivalence class "c".
429 * Currently only handles latin1, latin9 and utf-8.
430 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
431 * equivalent to 'a OR b OR c'
432 *
433 * NOTE! When changing this function, also update reg_equi_class()
434 */
435 static int
436nfa_emit_equi_class(c, neg)
437 int c;
438 int neg;
439{
440 int first = TRUE;
441 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
442#define EMIT2(c) \
443 EMIT(c); \
444 if (neg == TRUE) { \
445 EMIT(NFA_NOT); \
446 } \
447 if (first == FALSE) \
448 EMIT(glue); \
449 else \
450 first = FALSE; \
451
452#ifdef FEAT_MBYTE
453 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
454 || STRCMP(p_enc, "iso-8859-15") == 0)
455#endif
456 {
457 switch (c)
458 {
459 case 'A': case '\300': case '\301': case '\302':
460 case '\303': case '\304': case '\305':
461 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
462 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
463 EMIT2('\305');
464 return OK;
465
466 case 'C': case '\307':
467 EMIT2('C'); EMIT2('\307');
468 return OK;
469
470 case 'E': case '\310': case '\311': case '\312': case '\313':
471 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
472 EMIT2('\312'); EMIT2('\313');
473 return OK;
474
475 case 'I': case '\314': case '\315': case '\316': case '\317':
476 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
477 EMIT2('\316'); EMIT2('\317');
478 return OK;
479
480 case 'N': case '\321':
481 EMIT2('N'); EMIT2('\321');
482 return OK;
483
484 case 'O': case '\322': case '\323': case '\324': case '\325':
485 case '\326':
486 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
487 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
488 return OK;
489
490 case 'U': case '\331': case '\332': case '\333': case '\334':
491 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
492 EMIT2('\333'); EMIT2('\334');
493 return OK;
494
495 case 'Y': case '\335':
496 EMIT2('Y'); EMIT2('\335');
497 return OK;
498
499 case 'a': case '\340': case '\341': case '\342':
500 case '\343': case '\344': case '\345':
501 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
502 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
503 EMIT2('\345');
504 return OK;
505
506 case 'c': case '\347':
507 EMIT2('c'); EMIT2('\347');
508 return OK;
509
510 case 'e': case '\350': case '\351': case '\352': case '\353':
511 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
512 EMIT2('\352'); EMIT2('\353');
513 return OK;
514
515 case 'i': case '\354': case '\355': case '\356': case '\357':
516 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
517 EMIT2('\356'); EMIT2('\357');
518 return OK;
519
520 case 'n': case '\361':
521 EMIT2('n'); EMIT2('\361');
522 return OK;
523
524 case 'o': case '\362': case '\363': case '\364': case '\365':
525 case '\366':
526 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
527 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
528 return OK;
529
530 case 'u': case '\371': case '\372': case '\373': case '\374':
531 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
532 EMIT2('\373'); EMIT2('\374');
533 return OK;
534
535 case 'y': case '\375': case '\377':
536 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
537 return OK;
538
539 default:
540 return FAIL;
541 }
542 }
543
544 EMIT(c);
545 return OK;
546#undef EMIT2
547}
548
549/*
550 * Code to parse regular expression.
551 *
552 * We try to reuse parsing functions in regexp.c to
553 * minimize surprise and keep the syntax consistent.
554 */
555
556/*
557 * Increments the pointer "p" by one (multi-byte) character.
558 */
559 static void
560nfa_inc(p)
561 char_u **p;
562{
563#ifdef FEAT_MBYTE
564 if (has_mbyte)
565 mb_ptr2char_adv(p);
566 else
567#endif
568 *p = *p + 1;
569}
570
571/*
572 * Decrements the pointer "p" by one (multi-byte) character.
573 */
574 static void
575nfa_dec(p)
576 char_u **p;
577{
578#ifdef FEAT_MBYTE
579 char_u *p2, *oldp;
580
581 if (has_mbyte)
582 {
583 oldp = *p;
584 /* Try to find the multibyte char that advances to the current
585 * position. */
586 do
587 {
588 *p = *p - 1;
589 p2 = *p;
590 mb_ptr2char_adv(&p2);
591 } while (p2 != oldp);
592 }
593#else
594 *p = *p - 1;
595#endif
596}
597
598/*
599 * Parse the lowest level.
600 *
601 * An atom can be one of a long list of items. Many atoms match one character
602 * in the text. It is often an ordinary character or a character class.
603 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
604 * is only for syntax highlighting.
605 *
606 * atom ::= ordinary-atom
607 * or \( pattern \)
608 * or \%( pattern \)
609 * or \z( pattern \)
610 */
611 static int
612nfa_regatom()
613{
614 int c;
615 int charclass;
616 int equiclass;
617 int collclass;
618 int got_coll_char;
619 char_u *p;
620 char_u *endp;
621#ifdef FEAT_MBYTE
622 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200623#endif
624 int extra = 0;
625 int first;
626 int emit_range;
627 int negated;
628 int result;
629 int startc = -1;
630 int endc = -1;
631 int oldstartc = -1;
632 int cpo_lit; /* 'cpoptions' contains 'l' flag */
633 int cpo_bsl; /* 'cpoptions' contains '\' flag */
634 int glue; /* ID that will "glue" nodes together */
635
636 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
637 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
638
639 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200640 switch (c)
641 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200642 case NUL:
643 syntax_error = TRUE;
644 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
645
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200646 case Magic('^'):
647 EMIT(NFA_BOL);
648 break;
649
650 case Magic('$'):
651 EMIT(NFA_EOL);
652#if defined(FEAT_SYN_HL) || defined(PROTO)
653 had_eol = TRUE;
654#endif
655 break;
656
657 case Magic('<'):
658 EMIT(NFA_BOW);
659 break;
660
661 case Magic('>'):
662 EMIT(NFA_EOW);
663 break;
664
665 case Magic('_'):
666 c = no_Magic(getchr());
667 if (c == '^') /* "\_^" is start-of-line */
668 {
669 EMIT(NFA_BOL);
670 break;
671 }
672 if (c == '$') /* "\_$" is end-of-line */
673 {
674 EMIT(NFA_EOL);
675#if defined(FEAT_SYN_HL) || defined(PROTO)
676 had_eol = TRUE;
677#endif
678 break;
679 }
680
681 extra = ADD_NL;
682
683 /* "\_[" is collection plus newline */
684 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200685 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200686
687 /* "\_x" is character class plus newline */
688 /*FALLTHROUGH*/
689
690 /*
691 * Character classes.
692 */
693 case Magic('.'):
694 case Magic('i'):
695 case Magic('I'):
696 case Magic('k'):
697 case Magic('K'):
698 case Magic('f'):
699 case Magic('F'):
700 case Magic('p'):
701 case Magic('P'):
702 case Magic('s'):
703 case Magic('S'):
704 case Magic('d'):
705 case Magic('D'):
706 case Magic('x'):
707 case Magic('X'):
708 case Magic('o'):
709 case Magic('O'):
710 case Magic('w'):
711 case Magic('W'):
712 case Magic('h'):
713 case Magic('H'):
714 case Magic('a'):
715 case Magic('A'):
716 case Magic('l'):
717 case Magic('L'):
718 case Magic('u'):
719 case Magic('U'):
720 p = vim_strchr(classchars, no_Magic(c));
721 if (p == NULL)
722 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200723 EMSGN("INTERNAL: Unknown character class char: %ld", c);
724 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200725 }
726#ifdef FEAT_MBYTE
727 /* When '.' is followed by a composing char ignore the dot, so that
728 * the composing char is matched here. */
729 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
730 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200731 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732 c = getchr();
733 goto nfa_do_multibyte;
734 }
735#endif
736 EMIT(nfa_classcodes[p - classchars]);
737 if (extra == ADD_NL)
738 {
739 EMIT(NFA_NEWL);
740 EMIT(NFA_OR);
741 regflags |= RF_HASNL;
742 }
743 break;
744
745 case Magic('n'):
746 if (reg_string)
747 /* In a string "\n" matches a newline character. */
748 EMIT(NL);
749 else
750 {
751 /* In buffer text "\n" matches the end of a line. */
752 EMIT(NFA_NEWL);
753 regflags |= RF_HASNL;
754 }
755 break;
756
757 case Magic('('):
758 if (nfa_reg(REG_PAREN) == FAIL)
759 return FAIL; /* cascaded error */
760 break;
761
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200762 case Magic('|'):
763 case Magic('&'):
764 case Magic(')'):
765 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200766 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200767 return FAIL;
768
769 case Magic('='):
770 case Magic('?'):
771 case Magic('+'):
772 case Magic('@'):
773 case Magic('*'):
774 case Magic('{'):
775 /* these should follow an atom, not form an atom */
776 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200777 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200778 return FAIL;
779
780 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200781 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200782 return FAIL;
783
Bram Moolenaar5714b802013-05-28 22:03:20 +0200784 case Magic('1'): EMIT(NFA_BACKREF1); break;
785 case Magic('2'): EMIT(NFA_BACKREF2); break;
786 case Magic('3'): EMIT(NFA_BACKREF3); break;
787 case Magic('4'): EMIT(NFA_BACKREF4); break;
788 case Magic('5'): EMIT(NFA_BACKREF5); break;
789 case Magic('6'): EMIT(NFA_BACKREF6); break;
790 case Magic('7'): EMIT(NFA_BACKREF7); break;
791 case Magic('8'): EMIT(NFA_BACKREF8); break;
792 case Magic('9'): EMIT(NFA_BACKREF9); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200793
794 case Magic('z'):
795 c = no_Magic(getchr());
796 switch (c)
797 {
798 case 's':
799 EMIT(NFA_ZSTART);
800 break;
801 case 'e':
802 EMIT(NFA_ZEND);
803 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200804 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200805 case '1':
806 case '2':
807 case '3':
808 case '4':
809 case '5':
810 case '6':
811 case '7':
812 case '8':
813 case '9':
814 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200815 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 return FAIL;
817 default:
818 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200819 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200820 no_Magic(c));
821 return FAIL;
822 }
823 break;
824
825 case Magic('%'):
826 c = no_Magic(getchr());
827 switch (c)
828 {
829 /* () without a back reference */
830 case '(':
831 if (nfa_reg(REG_NPAREN) == FAIL)
832 return FAIL;
833 EMIT(NFA_NOPEN);
834 break;
835
836 case 'd': /* %d123 decimal */
837 case 'o': /* %o123 octal */
838 case 'x': /* %xab hex 2 */
839 case 'u': /* %uabcd hex 4 */
840 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200841 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200842 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200843
Bram Moolenaar47196582013-05-25 22:04:23 +0200844 switch (c)
845 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200846 case 'd': nr = getdecchrs(); break;
847 case 'o': nr = getoctchrs(); break;
848 case 'x': nr = gethexchrs(2); break;
849 case 'u': nr = gethexchrs(4); break;
850 case 'U': nr = gethexchrs(8); break;
851 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200852 }
853
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200854 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200855 EMSG2_RET_FAIL(
856 _("E678: Invalid character after %s%%[dxouU]"),
857 reg_magic == MAGIC_ALL);
858 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200859 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200860 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200861 break;
862
863 /* Catch \%^ and \%$ regardless of where they appear in the
864 * pattern -- regardless of whether or not it makes sense. */
865 case '^':
866 EMIT(NFA_BOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200867 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 return FAIL;
869 break;
870
871 case '$':
872 EMIT(NFA_EOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200873 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200874 return FAIL;
875 break;
876
877 case '#':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200878 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200879 return FAIL;
880 break;
881
882 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200883 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 return FAIL;
885 break;
886
887 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200888 /* TODO: \%[abc] not supported yet */
889 return FAIL;
890
891 case '0':
892 case '1':
893 case '2':
894 case '3':
895 case '4':
896 case '5':
897 case '6':
898 case '7':
899 case '8':
900 case '9':
901 case '<':
902 case '>':
903 case '\'':
904 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200905 return FAIL;
906
907 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +0200908 syntax_error = TRUE;
909 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
910 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200911 return FAIL;
912 }
913 break;
914
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200915 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200916collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200917 /*
918 * Glue is emitted between several atoms from the [].
919 * It is either NFA_OR, or NFA_CONCAT.
920 *
921 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
922 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
923 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
924 * notation)
925 *
926 */
927
928
929/* Emit negation atoms, if needed.
930 * The CONCAT below merges the NOT with the previous node. */
931#define TRY_NEG() \
932 if (negated == TRUE) \
933 { \
934 EMIT(NFA_NOT); \
935 }
936
937/* Emit glue between important nodes : CONCAT or OR. */
938#define EMIT_GLUE() \
939 if (first == FALSE) \
940 EMIT(glue); \
941 else \
942 first = FALSE;
943
944 p = regparse;
945 endp = skip_anyof(p);
946 if (*endp == ']')
947 {
948 /*
949 * Try to reverse engineer character classes. For example,
950 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
951 * and perform the necessary substitutions in the NFA.
952 */
953 result = nfa_recognize_char_class(regparse, endp,
954 extra == ADD_NL);
955 if (result != FAIL)
956 {
957 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
958 EMIT(result);
959 else /* must be char class + newline */
960 {
961 EMIT(result - ADD_NL);
962 EMIT(NFA_NEWL);
963 EMIT(NFA_OR);
964 }
965 regparse = endp;
966 nfa_inc(&regparse);
967 return OK;
968 }
969 /*
970 * Failed to recognize a character class. Use the simple
971 * version that turns [abc] into 'a' OR 'b' OR 'c'
972 */
973 startc = endc = oldstartc = -1;
974 first = TRUE; /* Emitting first atom in this sequence? */
975 negated = FALSE;
976 glue = NFA_OR;
977 if (*regparse == '^') /* negated range */
978 {
979 negated = TRUE;
980 glue = NFA_CONCAT;
981 nfa_inc(&regparse);
982 }
983 if (*regparse == '-')
984 {
985 startc = '-';
986 EMIT(startc);
987 TRY_NEG();
988 EMIT_GLUE();
989 nfa_inc(&regparse);
990 }
991 /* Emit the OR branches for each character in the [] */
992 emit_range = FALSE;
993 while (regparse < endp)
994 {
995 oldstartc = startc;
996 startc = -1;
997 got_coll_char = FALSE;
998 if (*regparse == '[')
999 {
1000 /* Check for [: :], [= =], [. .] */
1001 equiclass = collclass = 0;
1002 charclass = get_char_class(&regparse);
1003 if (charclass == CLASS_NONE)
1004 {
1005 equiclass = get_equi_class(&regparse);
1006 if (equiclass == 0)
1007 collclass = get_coll_element(&regparse);
1008 }
1009
1010 /* Character class like [:alpha:] */
1011 if (charclass != CLASS_NONE)
1012 {
1013 switch (charclass)
1014 {
1015 case CLASS_ALNUM:
1016 EMIT(NFA_CLASS_ALNUM);
1017 break;
1018 case CLASS_ALPHA:
1019 EMIT(NFA_CLASS_ALPHA);
1020 break;
1021 case CLASS_BLANK:
1022 EMIT(NFA_CLASS_BLANK);
1023 break;
1024 case CLASS_CNTRL:
1025 EMIT(NFA_CLASS_CNTRL);
1026 break;
1027 case CLASS_DIGIT:
1028 EMIT(NFA_CLASS_DIGIT);
1029 break;
1030 case CLASS_GRAPH:
1031 EMIT(NFA_CLASS_GRAPH);
1032 break;
1033 case CLASS_LOWER:
1034 EMIT(NFA_CLASS_LOWER);
1035 break;
1036 case CLASS_PRINT:
1037 EMIT(NFA_CLASS_PRINT);
1038 break;
1039 case CLASS_PUNCT:
1040 EMIT(NFA_CLASS_PUNCT);
1041 break;
1042 case CLASS_SPACE:
1043 EMIT(NFA_CLASS_SPACE);
1044 break;
1045 case CLASS_UPPER:
1046 EMIT(NFA_CLASS_UPPER);
1047 break;
1048 case CLASS_XDIGIT:
1049 EMIT(NFA_CLASS_XDIGIT);
1050 break;
1051 case CLASS_TAB:
1052 EMIT(NFA_CLASS_TAB);
1053 break;
1054 case CLASS_RETURN:
1055 EMIT(NFA_CLASS_RETURN);
1056 break;
1057 case CLASS_BACKSPACE:
1058 EMIT(NFA_CLASS_BACKSPACE);
1059 break;
1060 case CLASS_ESCAPE:
1061 EMIT(NFA_CLASS_ESCAPE);
1062 break;
1063 }
1064 TRY_NEG();
1065 EMIT_GLUE();
1066 continue;
1067 }
1068 /* Try equivalence class [=a=] and the like */
1069 if (equiclass != 0)
1070 {
1071 result = nfa_emit_equi_class(equiclass, negated);
1072 if (result == FAIL)
1073 {
1074 /* should never happen */
1075 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1076 }
1077 EMIT_GLUE();
1078 continue;
1079 }
1080 /* Try collating class like [. .] */
1081 if (collclass != 0)
1082 {
1083 startc = collclass; /* allow [.a.]-x as a range */
1084 /* Will emit the proper atom at the end of the
1085 * while loop. */
1086 }
1087 }
1088 /* Try a range like 'a-x' or '\t-z' */
1089 if (*regparse == '-')
1090 {
1091 emit_range = TRUE;
1092 startc = oldstartc;
1093 nfa_inc(&regparse);
1094 continue; /* reading the end of the range */
1095 }
1096
1097 /* Now handle simple and escaped characters.
1098 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1099 * accepts "\t", "\e", etc., but only when the 'l' flag in
1100 * 'cpoptions' is not included.
1101 * Posix doesn't recognize backslash at all.
1102 */
1103 if (*regparse == '\\'
1104 && !cpo_bsl
1105 && regparse + 1 <= endp
1106 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1107 || (!cpo_lit
1108 && vim_strchr(REGEXP_ABBR, regparse[1])
1109 != NULL)
1110 )
1111 )
1112 {
1113 nfa_inc(&regparse);
1114
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001115 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001116 startc = reg_string ? NL : NFA_NEWL;
1117 else
1118 if (*regparse == 'd'
1119 || *regparse == 'o'
1120 || *regparse == 'x'
1121 || *regparse == 'u'
1122 || *regparse == 'U'
1123 )
1124 {
1125 /* TODO(RE) This needs more testing */
1126 startc = coll_get_char();
1127 got_coll_char = TRUE;
1128 nfa_dec(&regparse);
1129 }
1130 else
1131 {
1132 /* \r,\t,\e,\b */
1133 startc = backslash_trans(*regparse);
1134 }
1135 }
1136
1137 /* Normal printable char */
1138 if (startc == -1)
1139#ifdef FEAT_MBYTE
1140 startc = (*mb_ptr2char)(regparse);
1141#else
1142 startc = *regparse;
1143#endif
1144
1145 /* Previous char was '-', so this char is end of range. */
1146 if (emit_range)
1147 {
1148 endc = startc; startc = oldstartc;
1149 if (startc > endc)
1150 EMSG_RET_FAIL(_(e_invrange));
1151#ifdef FEAT_MBYTE
1152 if (has_mbyte && ((*mb_char2len)(startc) > 1
1153 || (*mb_char2len)(endc) > 1))
1154 {
1155 if (endc > startc + 256)
1156 EMSG_RET_FAIL(_(e_invrange));
1157 /* Emit the range. "startc" was already emitted, so
1158 * skip it. */
1159 for (c = startc + 1; c <= endc; c++)
1160 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001161 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001162 TRY_NEG();
1163 EMIT_GLUE();
1164 }
1165 emit_range = FALSE;
1166 }
1167 else
1168#endif
1169 {
1170#ifdef EBCDIC
1171 int alpha_only = FALSE;
1172
1173 /* for alphabetical range skip the gaps
1174 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1175 if (isalpha(startc) && isalpha(endc))
1176 alpha_only = TRUE;
1177#endif
1178 /* Emit the range. "startc" was already emitted, so
1179 * skip it. */
1180 for (c = startc + 1; c <= endc; c++)
1181#ifdef EBCDIC
1182 if (!alpha_only || isalpha(startc))
1183#endif
1184 {
1185 EMIT(c);
1186 TRY_NEG();
1187 EMIT_GLUE();
1188 }
1189 emit_range = FALSE;
1190 }
1191 }
1192 else
1193 {
1194 /*
1195 * This char (startc) is not part of a range. Just
1196 * emit it.
1197 *
1198 * Normally, simply emit startc. But if we get char
1199 * code=0 from a collating char, then replace it with
1200 * 0x0a.
1201 *
1202 * This is needed to completely mimic the behaviour of
1203 * the backtracking engine.
1204 */
1205 if (got_coll_char == TRUE && startc == 0)
1206 EMIT(0x0a);
1207 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001208 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001209 TRY_NEG();
1210 EMIT_GLUE();
1211 }
1212
1213 nfa_inc(&regparse);
1214 } /* while (p < endp) */
1215
1216 nfa_dec(&regparse);
1217 if (*regparse == '-') /* if last, '-' is just a char */
1218 {
1219 EMIT('-');
1220 TRY_NEG();
1221 EMIT_GLUE();
1222 }
1223 nfa_inc(&regparse);
1224
1225 if (extra == ADD_NL) /* \_[] also matches \n */
1226 {
1227 EMIT(reg_string ? NL : NFA_NEWL);
1228 TRY_NEG();
1229 EMIT_GLUE();
1230 }
1231
1232 /* skip the trailing ] */
1233 regparse = endp;
1234 nfa_inc(&regparse);
1235 if (negated == TRUE)
1236 {
1237 /* Mark end of negated char range */
1238 EMIT(NFA_END_NEG_RANGE);
1239 EMIT(NFA_CONCAT);
1240 }
1241 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001242 } /* if exists closing ] */
1243
1244 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245 {
1246 syntax_error = TRUE;
1247 EMSG_RET_FAIL(_(e_missingbracket));
1248 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001249 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001250
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 default:
1252 {
1253#ifdef FEAT_MBYTE
1254 int plen;
1255
1256nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001257 /* plen is length of current char with composing chars */
1258 if (enc_utf8 && ((*mb_char2len)(c)
1259 != (plen = (*mb_ptr2len)(old_regparse))
1260 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001261 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001262 int i = 0;
1263
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001264 /* A base character plus composing characters, or just one
1265 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001266 * This requires creating a separate atom as if enclosing
1267 * the characters in (), where NFA_COMPOSING is the ( and
1268 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269 * building the postfix form, not the NFA itself;
1270 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001271 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001272 for (;;)
1273 {
1274 EMIT(c);
1275 if (i > 0)
1276 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001277 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001278 break;
1279 c = utf_ptr2char(old_regparse + i);
1280 }
1281 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 regparse = old_regparse + plen;
1283 }
1284 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001285#endif
1286 {
1287 c = no_Magic(c);
1288 EMIT(c);
1289 }
1290 return OK;
1291 }
1292 }
1293
1294#undef TRY_NEG
1295#undef EMIT_GLUE
1296
1297 return OK;
1298}
1299
1300/*
1301 * Parse something followed by possible [*+=].
1302 *
1303 * A piece is an atom, possibly followed by a multi, an indication of how many
1304 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1305 * characters: "", "a", "aa", etc.
1306 *
1307 * piece ::= atom
1308 * or atom multi
1309 */
1310 static int
1311nfa_regpiece()
1312{
1313 int i;
1314 int op;
1315 int ret;
1316 long minval, maxval;
1317 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1318 char_u *old_regparse, *new_regparse;
1319 int c2;
1320 int *old_post_ptr, *my_post_start;
1321 int old_regnpar;
1322 int quest;
1323
1324 /* Save the current position in the regexp, so that we can use it if
1325 * <atom>{m,n} is next. */
1326 old_regparse = regparse;
1327 /* Save current number of open parenthesis, so we can use it if
1328 * <atom>{m,n} is next */
1329 old_regnpar = regnpar;
1330 /* store current pos in the postfix form, for \{m,n} involving 0s */
1331 my_post_start = post_ptr;
1332
1333 ret = nfa_regatom();
1334 if (ret == FAIL)
1335 return FAIL; /* cascaded error */
1336
1337 op = peekchr();
1338 if (re_multi_type(op) == NOT_MULTI)
1339 return OK;
1340
1341 skipchr();
1342 switch (op)
1343 {
1344 case Magic('*'):
1345 EMIT(NFA_STAR);
1346 break;
1347
1348 case Magic('+'):
1349 /*
1350 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1351 * first and only submatch would be "aaa". But the backtracking
1352 * engine interprets the plus as "try matching one more time", and
1353 * a* matches a second time at the end of the input, the empty
1354 * string.
1355 * The submatch will the empty string.
1356 *
1357 * In order to be consistent with the old engine, we disable
1358 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1359 */
1360 /* EMIT(NFA_PLUS); */
1361 regnpar = old_regnpar;
1362 regparse = old_regparse;
1363 curchr = -1;
1364 if (nfa_regatom() == FAIL)
1365 return FAIL;
1366 EMIT(NFA_STAR);
1367 EMIT(NFA_CONCAT);
1368 skipchr(); /* skip the \+ */
1369 break;
1370
1371 case Magic('@'):
1372 op = no_Magic(getchr());
1373 switch(op)
1374 {
1375 case '=':
1376 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1377 break;
1378 case '!':
1379 case '<':
1380 case '>':
1381 /* Not supported yet */
1382 return FAIL;
1383 default:
1384 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001385 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386 return FAIL;
1387 }
1388 break;
1389
1390 case Magic('?'):
1391 case Magic('='):
1392 EMIT(NFA_QUEST);
1393 break;
1394
1395 case Magic('{'):
1396 /* a{2,5} will expand to 'aaa?a?a?'
1397 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1398 * version of '?'
1399 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1400 * parenthesis have the same id
1401 */
1402
1403 greedy = TRUE;
1404 c2 = peekchr();
1405 if (c2 == '-' || c2 == Magic('-'))
1406 {
1407 skipchr();
1408 greedy = FALSE;
1409 }
1410 if (!read_limits(&minval, &maxval))
1411 {
1412 syntax_error = TRUE;
1413 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1414 }
1415 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1416 * <atom>* */
1417 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1418 {
1419 EMIT(NFA_STAR);
1420 break;
1421 }
1422
1423 if (maxval > NFA_BRACES_MAXLIMIT)
1424 {
1425 /* This would yield a huge automaton and use too much memory.
1426 * Revert to old engine */
1427 return FAIL;
1428 }
1429
1430 /* Special case: x{0} or x{-0} */
1431 if (maxval == 0)
1432 {
1433 /* Ignore result of previous call to nfa_regatom() */
1434 post_ptr = my_post_start;
1435 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1436 EMIT(NFA_SKIP_CHAR);
1437 return OK;
1438 }
1439
1440 /* Ignore previous call to nfa_regatom() */
1441 post_ptr = my_post_start;
1442 /* Save pos after the repeated atom and the \{} */
1443 new_regparse = regparse;
1444
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1446 for (i = 0; i < maxval; i++)
1447 {
1448 /* Goto beginning of the repeated atom */
1449 regparse = old_regparse;
1450 curchr = -1;
1451 /* Restore count of parenthesis */
1452 regnpar = old_regnpar;
1453 old_post_ptr = post_ptr;
1454 if (nfa_regatom() == FAIL)
1455 return FAIL;
1456 /* after "minval" times, atoms are optional */
1457 if (i + 1 > minval)
1458 EMIT(quest);
1459 if (old_post_ptr != my_post_start)
1460 EMIT(NFA_CONCAT);
1461 }
1462
1463 /* Go to just after the repeated atom and the \{} */
1464 regparse = new_regparse;
1465 curchr = -1;
1466
1467 break;
1468
1469
1470 default:
1471 break;
1472 } /* end switch */
1473
1474 if (re_multi_type(peekchr()) != NOT_MULTI)
1475 {
1476 /* Can't have a multi follow a multi. */
1477 syntax_error = TRUE;
1478 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1479 }
1480
1481 return OK;
1482}
1483
1484/*
1485 * Parse one or more pieces, concatenated. It matches a match for the
1486 * first piece, followed by a match for the second piece, etc. Example:
1487 * "f[0-9]b", first matches "f", then a digit and then "b".
1488 *
1489 * concat ::= piece
1490 * or piece piece
1491 * or piece piece piece
1492 * etc.
1493 */
1494 static int
1495nfa_regconcat()
1496{
1497 int cont = TRUE;
1498 int first = TRUE;
1499
1500 while (cont)
1501 {
1502 switch (peekchr())
1503 {
1504 case NUL:
1505 case Magic('|'):
1506 case Magic('&'):
1507 case Magic(')'):
1508 cont = FALSE;
1509 break;
1510
1511 case Magic('Z'):
1512#ifdef FEAT_MBYTE
1513 regflags |= RF_ICOMBINE;
1514#endif
1515 skipchr_keepstart();
1516 break;
1517 case Magic('c'):
1518 regflags |= RF_ICASE;
1519 skipchr_keepstart();
1520 break;
1521 case Magic('C'):
1522 regflags |= RF_NOICASE;
1523 skipchr_keepstart();
1524 break;
1525 case Magic('v'):
1526 reg_magic = MAGIC_ALL;
1527 skipchr_keepstart();
1528 curchr = -1;
1529 break;
1530 case Magic('m'):
1531 reg_magic = MAGIC_ON;
1532 skipchr_keepstart();
1533 curchr = -1;
1534 break;
1535 case Magic('M'):
1536 reg_magic = MAGIC_OFF;
1537 skipchr_keepstart();
1538 curchr = -1;
1539 break;
1540 case Magic('V'):
1541 reg_magic = MAGIC_NONE;
1542 skipchr_keepstart();
1543 curchr = -1;
1544 break;
1545
1546 default:
1547 if (nfa_regpiece() == FAIL)
1548 return FAIL;
1549 if (first == FALSE)
1550 EMIT(NFA_CONCAT);
1551 else
1552 first = FALSE;
1553 break;
1554 }
1555 }
1556
1557 return OK;
1558}
1559
1560/*
1561 * Parse a branch, one or more concats, separated by "\&". It matches the
1562 * last concat, but only if all the preceding concats also match at the same
1563 * position. Examples:
1564 * "foobeep\&..." matches "foo" in "foobeep".
1565 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1566 *
1567 * branch ::= concat
1568 * or concat \& concat
1569 * or concat \& concat \& concat
1570 * etc.
1571 */
1572 static int
1573nfa_regbranch()
1574{
1575 int ch;
1576 int *old_post_ptr;
1577
1578 old_post_ptr = post_ptr;
1579
1580 /* First branch, possibly the only one */
1581 if (nfa_regconcat() == FAIL)
1582 return FAIL;
1583
1584 ch = peekchr();
1585 /* Try next concats */
1586 while (ch == Magic('&'))
1587 {
1588 skipchr();
1589 EMIT(NFA_NOPEN);
1590 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1591 old_post_ptr = post_ptr;
1592 if (nfa_regconcat() == FAIL)
1593 return FAIL;
1594 /* if concat is empty, skip a input char. But do emit a node */
1595 if (old_post_ptr == post_ptr)
1596 EMIT(NFA_SKIP_CHAR);
1597 EMIT(NFA_CONCAT);
1598 ch = peekchr();
1599 }
1600
1601 /* Even if a branch is empty, emit one node for it */
1602 if (old_post_ptr == post_ptr)
1603 EMIT(NFA_SKIP_CHAR);
1604
1605 return OK;
1606}
1607
1608/*
1609 * Parse a pattern, one or more branches, separated by "\|". It matches
1610 * anything that matches one of the branches. Example: "foo\|beep" matches
1611 * "foo" and matches "beep". If more than one branch matches, the first one
1612 * is used.
1613 *
1614 * pattern ::= branch
1615 * or branch \| branch
1616 * or branch \| branch \| branch
1617 * etc.
1618 */
1619 static int
1620nfa_reg(paren)
1621 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1622{
1623 int parno = 0;
1624
1625#ifdef FEAT_SYN_HL
1626#endif
1627 if (paren == REG_PAREN)
1628 {
1629 if (regnpar >= NSUBEXP) /* Too many `(' */
1630 {
1631 syntax_error = TRUE;
1632 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1633 }
1634 parno = regnpar++;
1635 }
1636
1637 if (nfa_regbranch() == FAIL)
1638 return FAIL; /* cascaded error */
1639
1640 while (peekchr() == Magic('|'))
1641 {
1642 skipchr();
1643 if (nfa_regbranch() == FAIL)
1644 return FAIL; /* cascaded error */
1645 EMIT(NFA_OR);
1646 }
1647
1648 /* Check for proper termination. */
1649 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1650 {
1651 syntax_error = TRUE;
1652 if (paren == REG_NPAREN)
1653 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1654 else
1655 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1656 }
1657 else if (paren == REG_NOPAREN && peekchr() != NUL)
1658 {
1659 syntax_error = TRUE;
1660 if (peekchr() == Magic(')'))
1661 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1662 else
1663 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1664 }
1665 /*
1666 * Here we set the flag allowing back references to this set of
1667 * parentheses.
1668 */
1669 if (paren == REG_PAREN)
1670 {
1671 had_endbrace[parno] = TRUE; /* have seen the close paren */
1672 EMIT(NFA_MOPEN + parno);
1673 }
1674
1675 return OK;
1676}
1677
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678#ifdef DEBUG
1679static char_u code[50];
1680
1681 static void
1682nfa_set_code(c)
1683 int c;
1684{
1685 int addnl = FALSE;
1686
1687 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1688 {
1689 addnl = TRUE;
1690 c -= ADD_NL;
1691 }
1692
1693 STRCPY(code, "");
1694 switch (c)
1695 {
1696 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1697 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1698 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1699 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1700 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1701 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1702
Bram Moolenaar5714b802013-05-28 22:03:20 +02001703 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1704 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1705 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1706 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1707 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1708 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1709 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1710 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1711 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1712 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1713
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001714 case NFA_PREV_ATOM_NO_WIDTH:
1715 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1716 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1717 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1718 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1719 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1720
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001721 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1722 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1723
1724 case NFA_MOPEN + 0:
1725 case NFA_MOPEN + 1:
1726 case NFA_MOPEN + 2:
1727 case NFA_MOPEN + 3:
1728 case NFA_MOPEN + 4:
1729 case NFA_MOPEN + 5:
1730 case NFA_MOPEN + 6:
1731 case NFA_MOPEN + 7:
1732 case NFA_MOPEN + 8:
1733 case NFA_MOPEN + 9:
1734 STRCPY(code, "NFA_MOPEN(x)");
1735 code[10] = c - NFA_MOPEN + '0';
1736 break;
1737 case NFA_MCLOSE + 0:
1738 case NFA_MCLOSE + 1:
1739 case NFA_MCLOSE + 2:
1740 case NFA_MCLOSE + 3:
1741 case NFA_MCLOSE + 4:
1742 case NFA_MCLOSE + 5:
1743 case NFA_MCLOSE + 6:
1744 case NFA_MCLOSE + 7:
1745 case NFA_MCLOSE + 8:
1746 case NFA_MCLOSE + 9:
1747 STRCPY(code, "NFA_MCLOSE(x)");
1748 code[11] = c - NFA_MCLOSE + '0';
1749 break;
1750 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1751 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1752 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1753 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1754 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1755 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1756 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1757 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1758 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1759 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1760 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1761 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1762 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1763 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1764 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1765 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1766 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1767 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1768 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1769 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1770 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1771 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1772 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1773 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1774 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1775 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1776 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1777 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1778
1779 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1780 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1781 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1782 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1783 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1784 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1785 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1786 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1787 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1788 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1789 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1790 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1791 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1792 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1793 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1794 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1795 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1796 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1797 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1798 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1799 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1800 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1801 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1802 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1803 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1804 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1805 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1806
1807 default:
1808 STRCPY(code, "CHAR(x)");
1809 code[5] = c;
1810 }
1811
1812 if (addnl == TRUE)
1813 STRCAT(code, " + NEWLINE ");
1814
1815}
1816
1817#ifdef ENABLE_LOG
1818static FILE *log_fd;
1819
1820/*
1821 * Print the postfix notation of the current regexp.
1822 */
1823 static void
1824nfa_postfix_dump(expr, retval)
1825 char_u *expr;
1826 int retval;
1827{
1828 int *p;
1829 FILE *f;
1830
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001831 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832 if (f != NULL)
1833 {
1834 fprintf(f, "\n-------------------------\n");
1835 if (retval == FAIL)
1836 fprintf(f, ">>> NFA engine failed ... \n");
1837 else if (retval == OK)
1838 fprintf(f, ">>> NFA engine succeeded !\n");
1839 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001840 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001841 {
1842 nfa_set_code(*p);
1843 fprintf(f, "%s, ", code);
1844 }
1845 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001846 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847 fprintf(f, "%d ", *p);
1848 fprintf(f, "\n\n");
1849 fclose(f);
1850 }
1851}
1852
1853/*
1854 * Print the NFA starting with a root node "state".
1855 */
1856 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001857nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858 FILE *debugf;
1859 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001860{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001861 garray_T indent;
1862
1863 ga_init2(&indent, 1, 64);
1864 ga_append(&indent, '\0');
1865 nfa_print_state2(debugf, state, &indent);
1866 ga_clear(&indent);
1867}
1868
1869 static void
1870nfa_print_state2(debugf, state, indent)
1871 FILE *debugf;
1872 nfa_state_T *state;
1873 garray_T *indent;
1874{
1875 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001876
1877 if (state == NULL)
1878 return;
1879
1880 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001881
1882 /* Output indent */
1883 p = (char_u *)indent->ga_data;
1884 if (indent->ga_len >= 3)
1885 {
1886 int last = indent->ga_len - 3;
1887 char_u save[2];
1888
1889 STRNCPY(save, &p[last], 2);
1890 STRNCPY(&p[last], "+-", 2);
1891 fprintf(debugf, " %s", p);
1892 STRNCPY(&p[last], save, 2);
1893 }
1894 else
1895 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896
1897 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001898 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1899 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 if (state->id < 0)
1901 return;
1902
1903 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001904
1905 /* grow indent for state->out */
1906 indent->ga_len -= 1;
1907 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001908 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001909 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001910 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001911 ga_append(indent, '\0');
1912
1913 nfa_print_state2(debugf, state->out, indent);
1914
1915 /* replace last part of indent for state->out1 */
1916 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001917 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001918 ga_append(indent, '\0');
1919
1920 nfa_print_state2(debugf, state->out1, indent);
1921
1922 /* shrink indent */
1923 indent->ga_len -= 3;
1924 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925}
1926
1927/*
1928 * Print the NFA state machine.
1929 */
1930 static void
1931nfa_dump(prog)
1932 nfa_regprog_T *prog;
1933{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001934 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935
1936 if (debugf != NULL)
1937 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001938 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 fclose(debugf);
1940 }
1941}
1942#endif /* ENABLE_LOG */
1943#endif /* DEBUG */
1944
1945/*
1946 * Parse r.e. @expr and convert it into postfix form.
1947 * Return the postfix string on success, NULL otherwise.
1948 */
1949 static int *
1950re2post()
1951{
1952 if (nfa_reg(REG_NOPAREN) == FAIL)
1953 return NULL;
1954 EMIT(NFA_MOPEN);
1955 return post_start;
1956}
1957
1958/* NB. Some of the code below is inspired by Russ's. */
1959
1960/*
1961 * Represents an NFA state plus zero or one or two arrows exiting.
1962 * if c == MATCH, no arrows out; matching state.
1963 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1964 * If c < 256, labeled arrow with character c to out.
1965 */
1966
1967static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1968
1969/*
1970 * Allocate and initialize nfa_state_T.
1971 */
1972 static nfa_state_T *
1973new_state(c, out, out1)
1974 int c;
1975 nfa_state_T *out;
1976 nfa_state_T *out1;
1977{
1978 nfa_state_T *s;
1979
1980 if (istate >= nstate)
1981 return NULL;
1982
1983 s = &state_ptr[istate++];
1984
1985 s->c = c;
1986 s->out = out;
1987 s->out1 = out1;
1988
1989 s->id = istate;
1990 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001991 s->negated = FALSE;
1992
1993 return s;
1994}
1995
1996/*
1997 * A partially built NFA without the matching state filled in.
1998 * Frag_T.start points at the start state.
1999 * Frag_T.out is a list of places that need to be set to the
2000 * next state for this fragment.
2001 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002002
2003/* Since the out pointers in the list are always
2004 * uninitialized, we use the pointers themselves
2005 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002006typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002007union Ptrlist
2008{
2009 Ptrlist *next;
2010 nfa_state_T *s;
2011};
2012
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002013struct Frag
2014{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002015 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002016 Ptrlist *out;
2017};
2018typedef struct Frag Frag_T;
2019
2020static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2021static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2022static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2023static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2024static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2025static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2026
2027/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002028 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029 */
2030 static Frag_T
2031frag(start, out)
2032 nfa_state_T *start;
2033 Ptrlist *out;
2034{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002035 Frag_T n;
2036
2037 n.start = start;
2038 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002039 return n;
2040}
2041
2042/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 * Create singleton list containing just outp.
2044 */
2045 static Ptrlist *
2046list1(outp)
2047 nfa_state_T **outp;
2048{
2049 Ptrlist *l;
2050
2051 l = (Ptrlist *)outp;
2052 l->next = NULL;
2053 return l;
2054}
2055
2056/*
2057 * Patch the list of states at out to point to start.
2058 */
2059 static void
2060patch(l, s)
2061 Ptrlist *l;
2062 nfa_state_T *s;
2063{
2064 Ptrlist *next;
2065
2066 for (; l; l = next)
2067 {
2068 next = l->next;
2069 l->s = s;
2070 }
2071}
2072
2073
2074/*
2075 * Join the two lists l1 and l2, returning the combination.
2076 */
2077 static Ptrlist *
2078append(l1, l2)
2079 Ptrlist *l1;
2080 Ptrlist *l2;
2081{
2082 Ptrlist *oldl1;
2083
2084 oldl1 = l1;
2085 while (l1->next)
2086 l1 = l1->next;
2087 l1->next = l2;
2088 return oldl1;
2089}
2090
2091/*
2092 * Stack used for transforming postfix form into NFA.
2093 */
2094static Frag_T empty;
2095
2096 static void
2097st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002098 int *postfix UNUSED;
2099 int *end UNUSED;
2100 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002101{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002102#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002103 FILE *df;
2104 int *p2;
2105
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002106 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002107 if (df)
2108 {
2109 fprintf(df, "Error popping the stack!\n");
2110#ifdef DEBUG
2111 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2112#endif
2113 fprintf(df, "Postfix form is: ");
2114#ifdef DEBUG
2115 for (p2 = postfix; p2 < end; p2++)
2116 {
2117 nfa_set_code(*p2);
2118 fprintf(df, "%s, ", code);
2119 }
2120 nfa_set_code(*p);
2121 fprintf(df, "\nCurrent position is: ");
2122 for (p2 = postfix; p2 <= p; p2 ++)
2123 {
2124 nfa_set_code(*p2);
2125 fprintf(df, "%s, ", code);
2126 }
2127#else
2128 for (p2 = postfix; p2 < end; p2++)
2129 {
2130 fprintf(df, "%d, ", *p2);
2131 }
2132 fprintf(df, "\nCurrent position is: ");
2133 for (p2 = postfix; p2 <= p; p2 ++)
2134 {
2135 fprintf(df, "%d, ", *p2);
2136 }
2137#endif
2138 fprintf(df, "\n--------------------------\n");
2139 fclose(df);
2140 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002141#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002142 EMSG(_("E874: (NFA) Could not pop the stack !"));
2143}
2144
2145/*
2146 * Push an item onto the stack.
2147 */
2148 static void
2149st_push(s, p, stack_end)
2150 Frag_T s;
2151 Frag_T **p;
2152 Frag_T *stack_end;
2153{
2154 Frag_T *stackp = *p;
2155
2156 if (stackp >= stack_end)
2157 return;
2158 *stackp = s;
2159 *p = *p + 1;
2160}
2161
2162/*
2163 * Pop an item from the stack.
2164 */
2165 static Frag_T
2166st_pop(p, stack)
2167 Frag_T **p;
2168 Frag_T *stack;
2169{
2170 Frag_T *stackp;
2171
2172 *p = *p - 1;
2173 stackp = *p;
2174 if (stackp < stack)
2175 return empty;
2176 return **p;
2177}
2178
2179/*
2180 * Convert a postfix form into its equivalent NFA.
2181 * Return the NFA start state on success, NULL otherwise.
2182 */
2183 static nfa_state_T *
2184post2nfa(postfix, end, nfa_calc_size)
2185 int *postfix;
2186 int *end;
2187 int nfa_calc_size;
2188{
2189 int *p;
2190 int mopen;
2191 int mclose;
2192 Frag_T *stack = NULL;
2193 Frag_T *stackp = NULL;
2194 Frag_T *stack_end = NULL;
2195 Frag_T e1;
2196 Frag_T e2;
2197 Frag_T e;
2198 nfa_state_T *s;
2199 nfa_state_T *s1;
2200 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002201 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002202
2203 if (postfix == NULL)
2204 return NULL;
2205
Bram Moolenaar053bb602013-05-20 13:55:21 +02002206#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207#define POP() st_pop(&stackp, stack); \
2208 if (stackp < stack) \
2209 { \
2210 st_error(postfix, end, p); \
2211 return NULL; \
2212 }
2213
2214 if (nfa_calc_size == FALSE)
2215 {
2216 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002217 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002219 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 }
2221
2222 for (p = postfix; p < end; ++p)
2223 {
2224 switch (*p)
2225 {
2226 case NFA_CONCAT:
2227 /* Catenation.
2228 * Pay attention: this operator does not exist
2229 * in the r.e. itself (it is implicit, really).
2230 * It is added when r.e. is translated to postfix
2231 * form in re2post().
2232 *
2233 * No new state added here. */
2234 if (nfa_calc_size == TRUE)
2235 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002236 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237 break;
2238 }
2239 e2 = POP();
2240 e1 = POP();
2241 patch(e1.out, e2.start);
2242 PUSH(frag(e1.start, e2.out));
2243 break;
2244
2245 case NFA_NOT:
2246 /* Negation of a character */
2247 if (nfa_calc_size == TRUE)
2248 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002249 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002250 break;
2251 }
2252 e1 = POP();
2253 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002254#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002255 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002256 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002257#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002258 PUSH(e1);
2259 break;
2260
2261 case NFA_OR:
2262 /* Alternation */
2263 if (nfa_calc_size == TRUE)
2264 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002265 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 break;
2267 }
2268 e2 = POP();
2269 e1 = POP();
2270 s = new_state(NFA_SPLIT, e1.start, e2.start);
2271 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002272 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002273 PUSH(frag(s, append(e1.out, e2.out)));
2274 break;
2275
2276 case NFA_STAR:
2277 /* Zero or more */
2278 if (nfa_calc_size == TRUE)
2279 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002280 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002281 break;
2282 }
2283 e = POP();
2284 s = new_state(NFA_SPLIT, e.start, NULL);
2285 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002286 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287 patch(e.out, s);
2288 PUSH(frag(s, list1(&s->out1)));
2289 break;
2290
2291 case NFA_QUEST:
2292 /* one or zero atoms=> greedy match */
2293 if (nfa_calc_size == TRUE)
2294 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002295 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002296 break;
2297 }
2298 e = POP();
2299 s = new_state(NFA_SPLIT, e.start, NULL);
2300 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002301 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302 PUSH(frag(s, append(e.out, list1(&s->out1))));
2303 break;
2304
2305 case NFA_QUEST_NONGREEDY:
2306 /* zero or one atoms => non-greedy match */
2307 if (nfa_calc_size == TRUE)
2308 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002309 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002310 break;
2311 }
2312 e = POP();
2313 s = new_state(NFA_SPLIT, NULL, e.start);
2314 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002315 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002316 PUSH(frag(s, append(e.out, list1(&s->out))));
2317 break;
2318
2319 case NFA_PLUS:
2320 /* One or more */
2321 if (nfa_calc_size == TRUE)
2322 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002323 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324 break;
2325 }
2326 e = POP();
2327 s = new_state(NFA_SPLIT, e.start, NULL);
2328 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002329 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330 patch(e.out, s);
2331 PUSH(frag(e.start, list1(&s->out1)));
2332 break;
2333
2334 case NFA_SKIP_CHAR:
2335 /* Symbol of 0-length, Used in a repetition
2336 * with max/min count of 0 */
2337 if (nfa_calc_size == TRUE)
2338 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002339 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340 break;
2341 }
2342 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2343 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002344 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002345 PUSH(frag(s, list1(&s->out)));
2346 break;
2347
2348 case NFA_PREV_ATOM_NO_WIDTH:
2349 /* The \@= operator: match the preceding atom with 0 width.
2350 * Surrounds the preceding atom with START_INVISIBLE and
2351 * END_INVISIBLE, similarly to MOPEN.
2352 */
2353 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002354 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355
2356 if (nfa_calc_size == TRUE)
2357 {
2358 nstate += 2;
2359 break;
2360 }
2361 e = POP();
2362 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2363 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002364 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365 patch(e.out, s1);
2366
2367 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2368 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002369 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370 PUSH(frag(s, list1(&s1->out)));
2371 break;
2372
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002373#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002374 case NFA_COMPOSING: /* char with composing char */
2375#if 0
2376 /* TODO */
2377 if (regflags & RF_ICOMBINE)
2378 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002379 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002380 }
2381#endif
2382 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002383#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002384
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002385 case NFA_MOPEN + 0: /* Submatch */
2386 case NFA_MOPEN + 1:
2387 case NFA_MOPEN + 2:
2388 case NFA_MOPEN + 3:
2389 case NFA_MOPEN + 4:
2390 case NFA_MOPEN + 5:
2391 case NFA_MOPEN + 6:
2392 case NFA_MOPEN + 7:
2393 case NFA_MOPEN + 8:
2394 case NFA_MOPEN + 9:
2395 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396 if (nfa_calc_size == TRUE)
2397 {
2398 nstate += 2;
2399 break;
2400 }
2401
2402 mopen = *p;
2403 switch (*p)
2404 {
2405 case NFA_NOPEN:
2406 mclose = NFA_NCLOSE;
2407 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002408#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002409 case NFA_COMPOSING:
2410 mclose = NFA_END_COMPOSING;
2411 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002412#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002413 default:
2414 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2415 mclose = *p + NSUBEXP;
2416 break;
2417 }
2418
2419 /* Allow "NFA_MOPEN" as a valid postfix representation for
2420 * the empty regexp "". In this case, the NFA will be
2421 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2422 * empty groups of parenthesis, and empty mbyte chars */
2423 if (stackp == stack)
2424 {
2425 s = new_state(mopen, NULL, NULL);
2426 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002427 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002428 s1 = new_state(mclose, NULL, NULL);
2429 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002430 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 patch(list1(&s->out), s1);
2432 PUSH(frag(s, list1(&s1->out)));
2433 break;
2434 }
2435
2436 /* At least one node was emitted before NFA_MOPEN, so
2437 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2438 e = POP();
2439 s = new_state(mopen, e.start, NULL); /* `(' */
2440 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002441 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442
2443 s1 = new_state(mclose, NULL, NULL); /* `)' */
2444 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002445 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002446 patch(e.out, s1);
2447
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002448#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002449 if (mopen == NFA_COMPOSING)
2450 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002451 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002452#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002453
2454 PUSH(frag(s, list1(&s1->out)));
2455 break;
2456
Bram Moolenaar5714b802013-05-28 22:03:20 +02002457 case NFA_BACKREF1:
2458 case NFA_BACKREF2:
2459 case NFA_BACKREF3:
2460 case NFA_BACKREF4:
2461 case NFA_BACKREF5:
2462 case NFA_BACKREF6:
2463 case NFA_BACKREF7:
2464 case NFA_BACKREF8:
2465 case NFA_BACKREF9:
2466 if (nfa_calc_size == TRUE)
2467 {
2468 nstate += 2;
2469 break;
2470 }
2471 s = new_state(*p, NULL, NULL);
2472 if (s == NULL)
2473 goto theend;
2474 s1 = new_state(NFA_SKIP, NULL, NULL);
2475 if (s1 == NULL)
2476 goto theend;
2477 patch(list1(&s->out), s1);
2478 PUSH(frag(s, list1(&s1->out)));
2479 break;
2480
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002481 case NFA_ZSTART:
2482 case NFA_ZEND:
2483 default:
2484 /* Operands */
2485 if (nfa_calc_size == TRUE)
2486 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002487 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002488 break;
2489 }
2490 s = new_state(*p, NULL, NULL);
2491 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002492 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002493 PUSH(frag(s, list1(&s->out)));
2494 break;
2495
2496 } /* switch(*p) */
2497
2498 } /* for(p = postfix; *p; ++p) */
2499
2500 if (nfa_calc_size == TRUE)
2501 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002502 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002503 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504 }
2505
2506 e = POP();
2507 if (stackp != stack)
2508 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2509
2510 if (istate >= nstate)
2511 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2512
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002513 matchstate = &state_ptr[istate++]; /* the match state */
2514 matchstate->c = NFA_MATCH;
2515 matchstate->out = matchstate->out1 = NULL;
2516
2517 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002518 ret = e.start;
2519
2520theend:
2521 vim_free(stack);
2522 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002523
2524#undef POP1
2525#undef PUSH1
2526#undef POP2
2527#undef PUSH2
2528#undef POP
2529#undef PUSH
2530}
2531
2532/****************************************************************
2533 * NFA execution code.
2534 ****************************************************************/
2535
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002536typedef struct
2537{
2538 int in_use; /* number of subexpr with useful info */
2539
2540 /* When REG_MULTI is TRUE multilist is used, otherwise linelist. */
2541 union
2542 {
2543 struct multipos
2544 {
2545 lpos_T start;
2546 lpos_T end;
2547 } multilist[NSUBEXP];
2548 struct linepos
2549 {
2550 char_u *start;
2551 char_u *end;
2552 } linelist[NSUBEXP];
2553 };
2554} regsub_T;
2555
Bram Moolenaar963fee22013-05-26 21:47:28 +02002556/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002557typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558{
2559 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002560 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002561 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002562} nfa_thread_T;
2563
Bram Moolenaar963fee22013-05-26 21:47:28 +02002564/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002565typedef struct
2566{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002567 nfa_thread_T *t; /* allocated array of states */
2568 int n; /* nr of states in "t" */
2569 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002570} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002571
Bram Moolenaar5714b802013-05-28 22:03:20 +02002572#ifdef ENABLE_LOG
2573 static void
2574log_subexpr(sub)
2575 regsub_T *sub;
2576{
2577 int j;
2578
2579 for (j = 0; j < sub->in_use; j++)
2580 if (REG_MULTI)
2581 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2582 j,
2583 sub->multilist[j].start.col,
2584 (int)sub->multilist[j].start.lnum,
2585 sub->multilist[j].end.col,
2586 (int)sub->multilist[j].end.lnum);
2587 else
2588 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2589 j,
2590 (char *)sub->linelist[j].start,
2591 (char *)sub->linelist[j].end);
2592 fprintf(log_fd, "\n");
2593}
2594#endif
2595
Bram Moolenaar963fee22013-05-26 21:47:28 +02002596/* Used during execution: whether a match has been found. */
2597static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002598
Bram Moolenaar5714b802013-05-28 22:03:20 +02002599static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2600static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002601
2602 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002603addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002604 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002605 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002606 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002607 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002609 int subidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002610 nfa_thread_T *lastthread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002611 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002612 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002613 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002614 int i;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002615
2616 if (l == NULL || state == NULL)
2617 return;
2618
2619 switch (state->c)
2620 {
2621 case NFA_SPLIT:
2622 case NFA_NOT:
2623 case NFA_NOPEN:
2624 case NFA_NCLOSE:
2625 case NFA_MCLOSE:
2626 case NFA_MCLOSE + 1:
2627 case NFA_MCLOSE + 2:
2628 case NFA_MCLOSE + 3:
2629 case NFA_MCLOSE + 4:
2630 case NFA_MCLOSE + 5:
2631 case NFA_MCLOSE + 6:
2632 case NFA_MCLOSE + 7:
2633 case NFA_MCLOSE + 8:
2634 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002635 /* These nodes are not added themselves but their "out" and/or
2636 * "out1" may be added below. */
2637 break;
2638
2639 case NFA_MOPEN:
2640 case NFA_MOPEN + 1:
2641 case NFA_MOPEN + 2:
2642 case NFA_MOPEN + 3:
2643 case NFA_MOPEN + 4:
2644 case NFA_MOPEN + 5:
2645 case NFA_MOPEN + 6:
2646 case NFA_MOPEN + 7:
2647 case NFA_MOPEN + 8:
2648 case NFA_MOPEN + 9:
2649 /* These nodes do not need to be added, but we need to bail out
2650 * when it was tried to be added to this list before. */
2651 if (state->lastlist == l->id)
2652 return;
2653 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654 break;
2655
2656 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002657 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002659 /* This state is already in the list, don't add it again,
2660 * unless it is an MOPEN that is used for a backreference. */
2661 return;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002662 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002663
2664 /* add the state to the list */
2665 state->lastlist = l->id;
2666 lastthread = &l->t[l->n++];
2667 lastthread->state = state;
2668 lastthread->sub.in_use = sub->in_use;
2669 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002671 /* Copy the match start and end positions. */
2672 if (REG_MULTI)
2673 mch_memmove(&lastthread->sub.multilist[0],
2674 &sub->multilist[0],
2675 sizeof(struct multipos) * sub->in_use);
2676 else
2677 mch_memmove(&lastthread->sub.linelist[0],
2678 &sub->linelist[0],
2679 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002680 }
2681 }
2682
2683#ifdef ENABLE_LOG
2684 nfa_set_code(state->c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002685 fprintf(log_fd, "> Adding state %d to list. Character %d: %s\n",
2686 abs(state->id), state->c, code);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002687#endif
2688 switch (state->c)
2689 {
2690 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002691 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002692 break;
2693
2694 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002695 addstate(l, state->out, sub, off);
2696 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697 break;
2698
2699#if 0
2700 case NFA_END_NEG_RANGE:
2701 /* Nothing to handle here. nfa_regmatch() will take care of it */
2702 break;
2703
2704 case NFA_NOT:
2705 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2706#ifdef ENABLE_LOG
2707 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2708#endif
2709 break;
2710
2711 case NFA_COMPOSING:
2712 /* nfa_regmatch() will match all the bytes of this composing char. */
2713 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714#endif
2715
Bram Moolenaar5714b802013-05-28 22:03:20 +02002716 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002717 case NFA_NOPEN:
2718 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002719 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002720 break;
2721
2722 /* If this state is reached, then a recursive call of nfa_regmatch()
2723 * succeeded. the next call saves the found submatches in the
2724 * first state after the "invisible" branch. */
2725#if 0
2726 case NFA_END_INVISIBLE:
2727 break;
2728#endif
2729
2730 case NFA_MOPEN + 0:
2731 case NFA_MOPEN + 1:
2732 case NFA_MOPEN + 2:
2733 case NFA_MOPEN + 3:
2734 case NFA_MOPEN + 4:
2735 case NFA_MOPEN + 5:
2736 case NFA_MOPEN + 6:
2737 case NFA_MOPEN + 7:
2738 case NFA_MOPEN + 8:
2739 case NFA_MOPEN + 9:
2740 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002741 if (state->c == NFA_ZSTART)
2742 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002743 else
2744 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002745
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002746 /* Set the position (with "off") in the subexpression. Save and
2747 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748 if (REG_MULTI)
2749 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002750 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002751 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002752 save_lpos = sub->multilist[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002753 save_in_use = -1;
2754 }
2755 else
2756 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002757 save_in_use = sub->in_use;
2758 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002759 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002760 sub->multilist[i].start.lnum = -1;
2761 sub->multilist[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002762 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002763 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002764 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002765 if (off == -1)
2766 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002767 sub->multilist[subidx].start.lnum = reglnum + 1;
2768 sub->multilist[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002769 }
2770 else
2771 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002772 sub->multilist[subidx].start.lnum = reglnum;
2773 sub->multilist[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002774 (colnr_T)(reginput - regline + off);
2775 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776 }
2777 else
2778 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002779 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002780 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002781 save_ptr = sub->linelist[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002782 save_in_use = -1;
2783 }
2784 else
2785 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002786 save_in_use = sub->in_use;
2787 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002788 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002789 sub->linelist[i].start = NULL;
2790 sub->linelist[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002791 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002792 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002793 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002794 sub->linelist[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002795 }
2796
Bram Moolenaar5714b802013-05-28 22:03:20 +02002797 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002799 if (save_in_use == -1)
2800 {
2801 if (REG_MULTI)
Bram Moolenaar5714b802013-05-28 22:03:20 +02002802 sub->multilist[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002803 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002804 sub->linelist[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002805 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002806 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002807 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002808 break;
2809
2810 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002811 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002812 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002813 /* Do not overwrite the position set by \ze. If no \ze
2814 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002815 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002816 break;
2817 }
2818 case NFA_MCLOSE + 1:
2819 case NFA_MCLOSE + 2:
2820 case NFA_MCLOSE + 3:
2821 case NFA_MCLOSE + 4:
2822 case NFA_MCLOSE + 5:
2823 case NFA_MCLOSE + 6:
2824 case NFA_MCLOSE + 7:
2825 case NFA_MCLOSE + 8:
2826 case NFA_MCLOSE + 9:
2827 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 if (state->c == NFA_ZEND)
2829 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002830 else
2831 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002833 /* We don't fill in gaps here, there must have been an MOPEN that
2834 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002835 save_in_use = sub->in_use;
2836 if (sub->in_use <= subidx)
2837 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002838 if (REG_MULTI)
2839 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002840 save_lpos = sub->multilist[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002841 if (off == -1)
2842 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002843 sub->multilist[subidx].end.lnum = reglnum + 1;
2844 sub->multilist[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002845 }
2846 else
2847 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002848 sub->multilist[subidx].end.lnum = reglnum;
2849 sub->multilist[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02002850 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002851 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002852 }
2853 else
2854 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002855 save_ptr = sub->linelist[subidx].end;
2856 sub->linelist[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002857 }
2858
Bram Moolenaar5714b802013-05-28 22:03:20 +02002859 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860
2861 if (REG_MULTI)
Bram Moolenaar5714b802013-05-28 22:03:20 +02002862 sub->multilist[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002863 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002864 sub->linelist[subidx].end = save_ptr;
2865 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002866 break;
2867 }
2868}
2869
2870/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002871 * Like addstate(), but the new state(s) are put at position "*ip".
2872 * Used for zero-width matches, next state to use is the added one.
2873 * This makes sure the order of states to be tried does not change, which
2874 * matters for alternatives.
2875 */
2876 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002877addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002878 nfa_list_T *l; /* runtime state list */
2879 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002880 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002881 int *ip;
2882{
2883 int tlen = l->n;
2884 int count;
2885 int i = *ip;
2886
2887 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002888 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02002889
2890 /* when "*ip" was at the end of the list, nothing to do */
2891 if (i + 1 == tlen)
2892 return;
2893
2894 /* re-order to put the new state at the current position */
2895 count = l->n - tlen;
2896 if (count > 1)
2897 {
2898 /* make space for new states, then move them from the
2899 * end to the current position */
2900 mch_memmove(&(l->t[i + count]),
2901 &(l->t[i + 1]),
2902 sizeof(nfa_thread_T) * (l->n - i - 1));
2903 mch_memmove(&(l->t[i]),
2904 &(l->t[l->n - 1]),
2905 sizeof(nfa_thread_T) * count);
2906 }
2907 else
2908 {
2909 /* overwrite the current state */
2910 l->t[i] = l->t[l->n - 1];
2911 }
2912 --l->n;
2913 *ip = i - 1;
2914}
2915
2916/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917 * Check character class "class" against current character c.
2918 */
2919 static int
2920check_char_class(class, c)
2921 int class;
2922 int c;
2923{
2924 switch (class)
2925 {
2926 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002927 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002928 return OK;
2929 break;
2930 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002931 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002932 return OK;
2933 break;
2934 case NFA_CLASS_BLANK:
2935 if (c == ' ' || c == '\t')
2936 return OK;
2937 break;
2938 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002939 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 return OK;
2941 break;
2942 case NFA_CLASS_DIGIT:
2943 if (VIM_ISDIGIT(c))
2944 return OK;
2945 break;
2946 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002947 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002948 return OK;
2949 break;
2950 case NFA_CLASS_LOWER:
2951 if (MB_ISLOWER(c))
2952 return OK;
2953 break;
2954 case NFA_CLASS_PRINT:
2955 if (vim_isprintc(c))
2956 return OK;
2957 break;
2958 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002959 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002960 return OK;
2961 break;
2962 case NFA_CLASS_SPACE:
2963 if ((c >=9 && c <= 13) || (c == ' '))
2964 return OK;
2965 break;
2966 case NFA_CLASS_UPPER:
2967 if (MB_ISUPPER(c))
2968 return OK;
2969 break;
2970 case NFA_CLASS_XDIGIT:
2971 if (vim_isxdigit(c))
2972 return OK;
2973 break;
2974 case NFA_CLASS_TAB:
2975 if (c == '\t')
2976 return OK;
2977 break;
2978 case NFA_CLASS_RETURN:
2979 if (c == '\r')
2980 return OK;
2981 break;
2982 case NFA_CLASS_BACKSPACE:
2983 if (c == '\b')
2984 return OK;
2985 break;
2986 case NFA_CLASS_ESCAPE:
2987 if (c == '\033')
2988 return OK;
2989 break;
2990
2991 default:
2992 /* should not be here :P */
2993 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2994 }
2995 return FAIL;
2996}
2997
Bram Moolenaar5714b802013-05-28 22:03:20 +02002998static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
2999
3000/*
3001 * Check for a match with subexpression "subidx".
3002 * return TRUE if it matches.
3003 */
3004 static int
3005match_backref(sub, subidx, bytelen)
3006 regsub_T *sub; /* pointers to subexpressions */
3007 int subidx;
3008 int *bytelen; /* out: length of match in bytes */
3009{
3010 int len;
3011
3012 if (sub->in_use <= subidx)
3013 {
3014retempty:
3015 /* backref was not set, match an empty string */
3016 *bytelen = 0;
3017 return TRUE;
3018 }
3019
3020 if (REG_MULTI)
3021 {
3022 if (sub->multilist[subidx].start.lnum < 0
3023 || sub->multilist[subidx].end.lnum < 0)
3024 goto retempty;
3025 /* TODO: line breaks */
3026 len = sub->multilist[subidx].end.col
3027 - sub->multilist[subidx].start.col;
3028 if (cstrncmp(regline + sub->multilist[subidx].start.col,
3029 reginput, &len) == 0)
3030 {
3031 *bytelen = len;
3032 return TRUE;
3033 }
3034 }
3035 else
3036 {
3037 if (sub->linelist[subidx].start == NULL
3038 || sub->linelist[subidx].end == NULL)
3039 goto retempty;
3040 len = (int)(sub->linelist[subidx].end - sub->linelist[subidx].start);
3041 if (cstrncmp(sub->linelist[subidx].start, reginput, &len) == 0)
3042 {
3043 *bytelen = len;
3044 return TRUE;
3045 }
3046 }
3047 return FALSE;
3048}
3049
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003050/*
3051 * Set all NFA nodes' list ID equal to -1.
3052 */
3053 static void
3054nfa_set_neg_listids(start)
3055 nfa_state_T *start;
3056{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003057 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003058 {
3059 start->lastlist = -1;
3060 nfa_set_neg_listids(start->out);
3061 nfa_set_neg_listids(start->out1);
3062 }
3063}
3064
3065/*
3066 * Set all NFA nodes' list ID equal to 0.
3067 */
3068 static void
3069nfa_set_null_listids(start)
3070 nfa_state_T *start;
3071{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003072 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003073 {
3074 start->lastlist = 0;
3075 nfa_set_null_listids(start->out);
3076 nfa_set_null_listids(start->out1);
3077 }
3078}
3079
3080/*
3081 * Save list IDs for all NFA states in "list".
3082 */
3083 static void
3084nfa_save_listids(start, list)
3085 nfa_state_T *start;
3086 int *list;
3087{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003088 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003089 {
3090 list[abs(start->id)] = start->lastlist;
3091 start->lastlist = -1;
3092 nfa_save_listids(start->out, list);
3093 nfa_save_listids(start->out1, list);
3094 }
3095}
3096
3097/*
3098 * Restore list IDs from "list" to all NFA states.
3099 */
3100 static void
3101nfa_restore_listids(start, list)
3102 nfa_state_T *start;
3103 int *list;
3104{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003105 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003106 {
3107 start->lastlist = list[abs(start->id)];
3108 nfa_restore_listids(start->out, list);
3109 nfa_restore_listids(start->out1, list);
3110 }
3111}
3112
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003113static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3114
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003115/*
3116 * Main matching routine.
3117 *
3118 * Run NFA to determine whether it matches reginput.
3119 *
3120 * Return TRUE if there is a match, FALSE otherwise.
3121 * Note: Caller must ensure that: start != NULL.
3122 */
3123 static int
3124nfa_regmatch(start, submatch, m)
3125 nfa_state_T *start;
3126 regsub_T *submatch;
3127 regsub_T *m;
3128{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003129 int result;
3130 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003131 int flag = 0;
3132 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003133 int go_to_nextline = FALSE;
3134 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003135 char_u *old_reginput = NULL;
3136 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003137 nfa_list_T list[3];
3138 nfa_list_T *listtbl[2][2];
3139 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003140 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003141 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003142 nfa_list_T *thislist;
3143 nfa_list_T *nextlist;
3144 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003145 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003146#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003147 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003148
3149 if (debug == NULL)
3150 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003151 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003152 return FALSE;
3153 }
3154#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003155 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003156
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003157 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003158 size = (nstate + 1) * sizeof(nfa_thread_T);
3159 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
3160 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
3161 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003162 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3163 goto theend;
3164 vim_memset(list[0].t, 0, size);
3165 vim_memset(list[1].t, 0, size);
3166 vim_memset(list[2].t, 0, size);
3167
3168#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003169 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003170 if (log_fd != NULL)
3171 {
3172 fprintf(log_fd, "**********************************\n");
3173 nfa_set_code(start->c);
3174 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3175 abs(start->id), code);
3176 fprintf(log_fd, "**********************************\n");
3177 }
3178 else
3179 {
3180 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3181 log_fd = stderr;
3182 }
3183#endif
3184
3185 thislist = &list[0];
3186 thislist->n = 0;
3187 nextlist = &list[1];
3188 nextlist->n = 0;
3189 neglist = &list[2];
3190 neglist->n = 0;
3191#ifdef ENABLE_LOG
3192 fprintf(log_fd, "(---) STARTSTATE\n");
3193#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003194 thislist->id = listid;
3195 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003196
3197 /* There are two cases when the NFA advances: 1. input char matches the
3198 * NFA node and 2. input char does not match the NFA node, but the next
3199 * node is NFA_NOT. The following macro calls addstate() according to
3200 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3201 listtbl[0][0] = NULL;
3202 listtbl[0][1] = neglist;
3203 listtbl[1][0] = nextlist;
3204 listtbl[1][1] = NULL;
3205#define ADD_POS_NEG_STATE(node) \
3206 ll = listtbl[result ? 1 : 0][node->negated]; \
3207 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003208 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209
3210
3211 /*
3212 * Run for each character.
3213 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003214 for (;;)
3215 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003216 int curc;
3217 int clen;
3218
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219#ifdef FEAT_MBYTE
3220 if (has_mbyte)
3221 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003222 curc = (*mb_ptr2char)(reginput);
3223 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003224 }
3225 else
3226#endif
3227 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003228 curc = *reginput;
3229 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003230 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003231 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003232 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003233 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003234 go_to_nextline = FALSE;
3235 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236
3237 /* swap lists */
3238 thislist = &list[flag];
3239 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003240 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003241 listtbl[1][0] = nextlist;
3242 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003243 thislist->id = listid;
3244 nextlist->id = listid + 1;
3245 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003246
3247#ifdef ENABLE_LOG
3248 fprintf(log_fd, "------------------------------------------\n");
3249 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003250 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003252 {
3253 int i;
3254
3255 for (i = 0; i < thislist->n; i++)
3256 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3257 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003258 fprintf(log_fd, "\n");
3259#endif
3260
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003261#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003262 fprintf(debug, "\n-------------------\n");
3263#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003264 /*
3265 * If the state lists are empty we can stop.
3266 */
3267 if (thislist->n == 0 && neglist->n == 0)
3268 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269
3270 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003271 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003272 {
3273 if (neglist->n > 0)
3274 {
3275 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003276 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003277 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 }
3279 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003280 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003282#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 nfa_set_code(t->state->c);
3284 fprintf(debug, "%s, ", code);
3285#endif
3286#ifdef ENABLE_LOG
3287 nfa_set_code(t->state->c);
3288 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3289 code, (int)t->state->c);
3290#endif
3291
3292 /*
3293 * Handle the possible codes of the current state.
3294 * The most important is NFA_MATCH.
3295 */
3296 switch (t->state->c)
3297 {
3298 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003299 {
3300 int j;
3301
Bram Moolenaar963fee22013-05-26 21:47:28 +02003302 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003303 submatch->in_use = t->sub.in_use;
3304 if (REG_MULTI)
3305 for (j = 0; j < submatch->in_use; j++)
3306 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003307 submatch->multilist[j].start =
3308 t->sub.multilist[j].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003309 submatch->multilist[j].end = t->sub.multilist[j].end;
3310 }
3311 else
3312 for (j = 0; j < submatch->in_use; j++)
3313 {
3314 submatch->linelist[j].start = t->sub.linelist[j].start;
3315 submatch->linelist[j].end = t->sub.linelist[j].end;
3316 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003317#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003318 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003320 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003321 * states at this position. When the list of states is going
3322 * to be empty quit without advancing, so that "reginput" is
3323 * correct. */
3324 if (nextlist->n == 0 && neglist->n == 0)
3325 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003326 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003327 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328
3329 case NFA_END_INVISIBLE:
3330 /* This is only encountered after a NFA_START_INVISIBLE node.
3331 * They surround a zero-width group, used with "\@=" and "\&".
3332 * If we got here, it means that the current "invisible" group
3333 * finished successfully, so return control to the parent
3334 * nfa_regmatch(). Submatches are stored in *m, and used in
3335 * the parent call. */
3336 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003337 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003338 else
3339 {
3340 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003341 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003342 }
3343 break;
3344
3345 case NFA_START_INVISIBLE:
3346 /* Save global variables, and call nfa_regmatch() to check if
3347 * the current concat matches at this position. The concat
3348 * ends with the node NFA_END_INVISIBLE */
3349 old_reginput = reginput;
3350 old_regline = regline;
3351 old_reglnum = reglnum;
3352 if (listids == NULL)
3353 {
3354 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3355 if (listids == NULL)
3356 {
3357 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3358 return 0;
3359 }
3360 }
3361#ifdef ENABLE_LOG
3362 if (log_fd != stderr)
3363 fclose(log_fd);
3364 log_fd = NULL;
3365#endif
3366 /* Have to clear the listid field of the NFA nodes, so that
3367 * nfa_regmatch() and addstate() can run properly after
3368 * recursion. */
3369 nfa_save_listids(start, listids);
3370 nfa_set_null_listids(start);
3371 result = nfa_regmatch(t->state->out, submatch, m);
3372 nfa_set_neg_listids(start);
3373 nfa_restore_listids(start, listids);
3374
3375#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003376 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003377 if (log_fd != NULL)
3378 {
3379 fprintf(log_fd, "****************************\n");
3380 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3381 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3382 fprintf(log_fd, "****************************\n");
3383 }
3384 else
3385 {
3386 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3387 log_fd = stderr;
3388 }
3389#endif
3390 if (result == TRUE)
3391 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003392 int j;
3393
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003394 /* Restore position in input text */
3395 reginput = old_reginput;
3396 regline = old_regline;
3397 reglnum = old_reglnum;
3398 /* Copy submatch info from the recursive call */
3399 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003400 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003401 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003402 t->sub.multilist[j].start = m->multilist[j].start;
3403 t->sub.multilist[j].end = m->multilist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404 }
3405 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003406 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003407 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003408 t->sub.linelist[j].start = m->linelist[j].start;
3409 t->sub.linelist[j].end = m->linelist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003410 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003411 t->sub.in_use = m->in_use;
3412
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003414 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003415 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003416 }
3417 else
3418 {
3419 /* continue with next input char */
3420 reginput = old_reginput;
3421 }
3422 break;
3423
3424 case NFA_BOL:
3425 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003426 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003427 break;
3428
3429 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003430 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003431 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003432 break;
3433
3434 case NFA_BOW:
3435 {
3436 int bow = TRUE;
3437
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003438 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439 bow = FALSE;
3440#ifdef FEAT_MBYTE
3441 else if (has_mbyte)
3442 {
3443 int this_class;
3444
3445 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003446 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003447 if (this_class <= 1)
3448 bow = FALSE;
3449 else if (reg_prev_class() == this_class)
3450 bow = FALSE;
3451 }
3452#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003453 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003454 || (reginput > regline
3455 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 bow = FALSE;
3457 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003458 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 break;
3460 }
3461
3462 case NFA_EOW:
3463 {
3464 int eow = TRUE;
3465
3466 if (reginput == regline)
3467 eow = FALSE;
3468#ifdef FEAT_MBYTE
3469 else if (has_mbyte)
3470 {
3471 int this_class, prev_class;
3472
3473 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003474 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 prev_class = reg_prev_class();
3476 if (this_class == prev_class
3477 || prev_class == 0 || prev_class == 1)
3478 eow = FALSE;
3479 }
3480#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003481 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003482 || (reginput[0] != NUL
3483 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484 eow = FALSE;
3485 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003486 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 break;
3488 }
3489
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003490#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003491 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003492 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003493 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003494 int len = 0;
3495 nfa_state_T *end;
3496 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003497 int cchars[MAX_MCO];
3498 int ccount = 0;
3499 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003500
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003501 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003502 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003503 if (utf_iscomposing(sta->c))
3504 {
3505 /* Only match composing character(s), ignore base
3506 * character. Used for ".{composing}" and "{composing}"
3507 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003508 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003509 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003510 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003511 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003512 /* If \Z was present, then ignore composing characters.
3513 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003514 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003515 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003516 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003517 else
3518 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003519 while (sta->c != NFA_END_COMPOSING)
3520 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003521 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003522
3523 /* Check base character matches first, unless ignored. */
3524 else if (len > 0 || mc == sta->c)
3525 {
3526 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003527 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003528 len += mb_char2len(mc);
3529 sta = sta->out;
3530 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003531
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003532 /* We don't care about the order of composing characters.
3533 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003534 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003535 {
3536 mc = mb_ptr2char(reginput + len);
3537 cchars[ccount++] = mc;
3538 len += mb_char2len(mc);
3539 if (ccount == MAX_MCO)
3540 break;
3541 }
3542
3543 /* Check that each composing char in the pattern matches a
3544 * composing char in the text. We do not check if all
3545 * composing chars are matched. */
3546 result = OK;
3547 while (sta->c != NFA_END_COMPOSING)
3548 {
3549 for (j = 0; j < ccount; ++j)
3550 if (cchars[j] == sta->c)
3551 break;
3552 if (j == ccount)
3553 {
3554 result = FAIL;
3555 break;
3556 }
3557 sta = sta->out;
3558 }
3559 }
3560 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003561 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003562
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003563 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003564 ADD_POS_NEG_STATE(end);
3565 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003566 }
3567#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003568
3569 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003570 if (curc == NUL && !reg_line_lbr && REG_MULTI
3571 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003572 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003573 go_to_nextline = TRUE;
3574 /* Pass -1 for the offset, which means taking the position
3575 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003576 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003577 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003578 else if (curc == '\n' && reg_line_lbr)
3579 {
3580 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003581 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003582 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003583 break;
3584
3585 case NFA_CLASS_ALNUM:
3586 case NFA_CLASS_ALPHA:
3587 case NFA_CLASS_BLANK:
3588 case NFA_CLASS_CNTRL:
3589 case NFA_CLASS_DIGIT:
3590 case NFA_CLASS_GRAPH:
3591 case NFA_CLASS_LOWER:
3592 case NFA_CLASS_PRINT:
3593 case NFA_CLASS_PUNCT:
3594 case NFA_CLASS_SPACE:
3595 case NFA_CLASS_UPPER:
3596 case NFA_CLASS_XDIGIT:
3597 case NFA_CLASS_TAB:
3598 case NFA_CLASS_RETURN:
3599 case NFA_CLASS_BACKSPACE:
3600 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003601 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 ADD_POS_NEG_STATE(t->state);
3603 break;
3604
3605 case NFA_END_NEG_RANGE:
3606 /* This follows a series of negated nodes, like:
3607 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003608 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003609 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 break;
3611
3612 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003613 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003614 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003615 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003616 break;
3617
3618 /*
3619 * Character classes like \a for alpha, \d for digit etc.
3620 */
3621 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003622 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 ADD_POS_NEG_STATE(t->state);
3624 break;
3625
3626 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003627 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003628 ADD_POS_NEG_STATE(t->state);
3629 break;
3630
3631 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003632 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003633 ADD_POS_NEG_STATE(t->state);
3634 break;
3635
3636 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003637 result = !VIM_ISDIGIT(curc)
3638 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639 ADD_POS_NEG_STATE(t->state);
3640 break;
3641
3642 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003643 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 ADD_POS_NEG_STATE(t->state);
3645 break;
3646
3647 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003648 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003649 ADD_POS_NEG_STATE(t->state);
3650 break;
3651
3652 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003653 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003654 ADD_POS_NEG_STATE(t->state);
3655 break;
3656
3657 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003658 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003659 ADD_POS_NEG_STATE(t->state);
3660 break;
3661
3662 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003663 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003664 ADD_POS_NEG_STATE(t->state);
3665 break;
3666
3667 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003668 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669 ADD_POS_NEG_STATE(t->state);
3670 break;
3671
3672 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003673 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003674 ADD_POS_NEG_STATE(t->state);
3675 break;
3676
3677 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003678 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679 ADD_POS_NEG_STATE(t->state);
3680 break;
3681
3682 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003683 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003684 ADD_POS_NEG_STATE(t->state);
3685 break;
3686
3687 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003688 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003689 ADD_POS_NEG_STATE(t->state);
3690 break;
3691
3692 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003693 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694 ADD_POS_NEG_STATE(t->state);
3695 break;
3696
3697 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003698 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003699 ADD_POS_NEG_STATE(t->state);
3700 break;
3701
3702 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003703 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003704 ADD_POS_NEG_STATE(t->state);
3705 break;
3706
3707 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003708 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709 ADD_POS_NEG_STATE(t->state);
3710 break;
3711
3712 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003713 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003714 ADD_POS_NEG_STATE(t->state);
3715 break;
3716
3717 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003718 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003719 ADD_POS_NEG_STATE(t->state);
3720 break;
3721
3722 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003723 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 ADD_POS_NEG_STATE(t->state);
3725 break;
3726
3727 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003728 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 ADD_POS_NEG_STATE(t->state);
3730 break;
3731
3732 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003733 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003734 ADD_POS_NEG_STATE(t->state);
3735 break;
3736
3737 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003738 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 ADD_POS_NEG_STATE(t->state);
3740 break;
3741
3742 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003743 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003744 ADD_POS_NEG_STATE(t->state);
3745 break;
3746
3747 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003748 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749 ADD_POS_NEG_STATE(t->state);
3750 break;
3751
Bram Moolenaar5714b802013-05-28 22:03:20 +02003752 case NFA_BACKREF1:
3753 case NFA_BACKREF2:
3754 case NFA_BACKREF3:
3755 case NFA_BACKREF4:
3756 case NFA_BACKREF5:
3757 case NFA_BACKREF6:
3758 case NFA_BACKREF7:
3759 case NFA_BACKREF8:
3760 case NFA_BACKREF9:
3761 /* \1 .. \9 */
3762 {
3763 int subidx = t->state->c - NFA_BACKREF1 + 1;
3764 int bytelen;
3765
3766 result = match_backref(&t->sub, subidx, &bytelen);
3767 if (result)
3768 {
3769 if (bytelen == 0)
3770 {
3771 /* empty match always works, add NFA_SKIP with zero to
3772 * be used next */
3773 addstate_here(thislist, t->state->out, &t->sub,
3774 &listidx);
3775 thislist->t[listidx + 1].count = 0;
3776 }
3777 else if (bytelen <= clen)
3778 {
3779 /* match current character, jump ahead to out of
3780 * NFA_SKIP */
3781 addstate(nextlist, t->state->out->out, &t->sub, clen);
3782#ifdef ENABLE_LOG
3783 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3784#endif
3785 }
3786 else
3787 {
3788 /* skip ofer the matched characters, set character
3789 * count in NFA_SKIP */
3790 addstate(nextlist, t->state->out, &t->sub, bytelen);
3791 nextlist->t[nextlist->n - 1].count = bytelen - clen;
3792#ifdef ENABLE_LOG
3793 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3794#endif
3795 }
3796
3797 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02003798 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003799 }
3800 case NFA_SKIP:
3801 /* charater of previous matching \1 .. \9 */
3802 if (t->count - clen <= 0)
3803 {
3804 /* end of match, go to what follows */
3805 addstate(nextlist, t->state->out, &t->sub, clen);
3806#ifdef ENABLE_LOG
3807 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3808#endif
3809 }
3810 else
3811 {
3812 /* add state again with decremented count */
3813 addstate(nextlist, t->state, &t->sub, 0);
3814 nextlist->t[nextlist->n - 1].count = t->count - clen;
3815#ifdef ENABLE_LOG
3816 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3817#endif
3818 }
3819 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003820
3821 case NFA_SKIP_CHAR:
3822 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003823 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02003824 /* TODO: should not happen? */
3825 break;
3826
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003827 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003828 {
3829 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003830
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003831 /* TODO: put this in #ifdef later */
3832 if (c < -256)
3833 EMSGN("INTERNAL: Negative state char: %ld", c);
3834 if (is_Magic(c))
3835 c = un_Magic(c);
3836 result = (c == curc);
3837
3838 if (!result && ireg_ic)
3839 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003840#ifdef FEAT_MBYTE
3841 /* If there is a composing character which is not being
3842 * ignored there can be no match. Match with composing
3843 * character uses NFA_COMPOSING above. */
3844 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003845 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003846 result = FALSE;
3847#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003848 ADD_POS_NEG_STATE(t->state);
3849 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003850 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003851 }
3852
3853 } /* for (thislist = thislist; thislist->state; thislist++) */
3854
Bram Moolenaare23febd2013-05-26 18:40:14 +02003855 /* Look for the start of a match in the current position by adding the
3856 * start state to the list of states.
3857 * The first found match is the leftmost one, thus the order of states
3858 * matters!
3859 * Do not add the start state in recursive calls of nfa_regmatch(),
3860 * because recursive calls should only start in the first position.
3861 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003862 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaare23febd2013-05-26 18:40:14 +02003863 && reglnum == 0 && clen != 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003864 {
3865#ifdef ENABLE_LOG
3866 fprintf(log_fd, "(---) STARTSTATE\n");
3867#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003868 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003869 }
3870
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003871#ifdef ENABLE_LOG
3872 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003873 {
3874 int i;
3875
3876 for (i = 0; i < thislist->n; i++)
3877 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3878 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003879 fprintf(log_fd, "\n");
3880#endif
3881
3882nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003883 /* Advance to the next character, or advance to the next line, or
3884 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003885 if (clen != 0)
3886 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003887 else if (go_to_nextline)
3888 reg_nextline();
3889 else
3890 break;
3891 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003892
3893#ifdef ENABLE_LOG
3894 if (log_fd != stderr)
3895 fclose(log_fd);
3896 log_fd = NULL;
3897#endif
3898
3899theend:
3900 /* Free memory */
3901 vim_free(list[0].t);
3902 vim_free(list[1].t);
3903 vim_free(list[2].t);
3904 list[0].t = list[1].t = list[2].t = NULL;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003905 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003906#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003907#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003908 fclose(debug);
3909#endif
3910
Bram Moolenaar963fee22013-05-26 21:47:28 +02003911 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003912}
3913
3914/*
3915 * Try match of "prog" with at regline["col"].
3916 * Returns 0 for failure, number of lines contained in the match otherwise.
3917 */
3918 static long
3919nfa_regtry(start, col)
3920 nfa_state_T *start;
3921 colnr_T col;
3922{
3923 int i;
3924 regsub_T sub, m;
3925#ifdef ENABLE_LOG
3926 FILE *f;
3927#endif
3928
3929 reginput = regline + col;
3930 need_clear_subexpr = TRUE;
3931
3932#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003933 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003934 if (f != NULL)
3935 {
3936 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3937 fprintf(f, " =======================================================\n");
3938#ifdef DEBUG
3939 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3940#endif
3941 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3942 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003943 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003944 fprintf(f, "\n\n");
3945 fclose(f);
3946 }
3947 else
3948 EMSG(_("Could not open temporary log file for writing "));
3949#endif
3950
3951 if (REG_MULTI)
3952 {
3953 /* Use 0xff to set lnum to -1 */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003954 vim_memset(sub.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
3955 vim_memset(m.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003956 }
3957 else
3958 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003959 vim_memset(sub.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
3960 vim_memset(m.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003961 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003962 sub.in_use = 0;
3963 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003964
3965 if (nfa_regmatch(start, &sub, &m) == FALSE)
3966 return 0;
3967
3968 cleanup_subexpr();
3969 if (REG_MULTI)
3970 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003971 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003972 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003973 reg_startpos[i] = sub.multilist[i].start;
3974 reg_endpos[i] = sub.multilist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003975 }
3976
3977 if (reg_startpos[0].lnum < 0)
3978 {
3979 reg_startpos[0].lnum = 0;
3980 reg_startpos[0].col = col;
3981 }
3982 if (reg_endpos[0].lnum < 0)
3983 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003984 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003985 reg_endpos[0].lnum = reglnum;
3986 reg_endpos[0].col = (int)(reginput - regline);
3987 }
3988 else
3989 /* Use line number of "\ze". */
3990 reglnum = reg_endpos[0].lnum;
3991 }
3992 else
3993 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003994 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003995 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003996 reg_startp[i] = sub.linelist[i].start;
3997 reg_endp[i] = sub.linelist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003998 }
3999
4000 if (reg_startp[0] == NULL)
4001 reg_startp[0] = regline + col;
4002 if (reg_endp[0] == NULL)
4003 reg_endp[0] = reginput;
4004 }
4005
4006 return 1 + reglnum;
4007}
4008
4009/*
4010 * Match a regexp against a string ("line" points to the string) or multiple
4011 * lines ("line" is NULL, use reg_getline()).
4012 *
4013 * Returns 0 for failure, number of lines contained in the match otherwise.
4014 */
4015 static long
4016nfa_regexec_both(line, col)
4017 char_u *line;
4018 colnr_T col; /* column to start looking for match */
4019{
4020 nfa_regprog_T *prog;
4021 long retval = 0L;
4022 int i;
4023
4024 if (REG_MULTI)
4025 {
4026 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4027 line = reg_getline((linenr_T)0); /* relative to the cursor */
4028 reg_startpos = reg_mmatch->startpos;
4029 reg_endpos = reg_mmatch->endpos;
4030 }
4031 else
4032 {
4033 prog = (nfa_regprog_T *)reg_match->regprog;
4034 reg_startp = reg_match->startp;
4035 reg_endp = reg_match->endp;
4036 }
4037
4038 /* Be paranoid... */
4039 if (prog == NULL || line == NULL)
4040 {
4041 EMSG(_(e_null));
4042 goto theend;
4043 }
4044
4045 /* If the start column is past the maximum column: no need to try. */
4046 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4047 goto theend;
4048
4049 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4050 if (prog->regflags & RF_ICASE)
4051 ireg_ic = TRUE;
4052 else if (prog->regflags & RF_NOICASE)
4053 ireg_ic = FALSE;
4054
4055#ifdef FEAT_MBYTE
4056 /* If pattern contains "\Z" overrule value of ireg_icombine */
4057 if (prog->regflags & RF_ICOMBINE)
4058 ireg_icombine = TRUE;
4059#endif
4060
4061 regline = line;
4062 reglnum = 0; /* relative to line */
4063
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004064 nfa_has_zend = prog->has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004065 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004066
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004067 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004068 for (i = 0; i < nstate; ++i)
4069 {
4070 prog->state[i].id = i;
4071 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004072 }
4073
4074 retval = nfa_regtry(prog->start, col);
4075
4076theend:
4077 return retval;
4078}
4079
4080/*
4081 * Compile a regular expression into internal code for the NFA matcher.
4082 * Returns the program in allocated space. Returns NULL for an error.
4083 */
4084 static regprog_T *
4085nfa_regcomp(expr, re_flags)
4086 char_u *expr;
4087 int re_flags;
4088{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004089 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004090 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004091 int *postfix;
4092
4093 if (expr == NULL)
4094 return NULL;
4095
4096#ifdef DEBUG
4097 nfa_regengine.expr = expr;
4098#endif
4099
4100 init_class_tab();
4101
4102 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4103 return NULL;
4104
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004105 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004106 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004107 postfix = re2post();
4108 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004109 {
4110 /* TODO: only give this error for debugging? */
4111 if (post_ptr >= post_end)
4112 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004114 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004115
4116 /*
4117 * In order to build the NFA, we parse the input regexp twice:
4118 * 1. first pass to count size (so we can allocate space)
4119 * 2. second to emit code
4120 */
4121#ifdef ENABLE_LOG
4122 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004123 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004124
4125 if (f != NULL)
4126 {
4127 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4128 fclose(f);
4129 }
4130 }
4131#endif
4132
4133 /*
4134 * PASS 1
4135 * Count number of NFA states in "nstate". Do not build the NFA.
4136 */
4137 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004138
4139 /* Space for compiled regexp */
4140 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4141 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4142 if (prog == NULL)
4143 goto fail;
4144 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004145 state_ptr = prog->state;
4146
4147 /*
4148 * PASS 2
4149 * Build the NFA
4150 */
4151 prog->start = post2nfa(postfix, post_ptr, FALSE);
4152 if (prog->start == NULL)
4153 goto fail;
4154
4155 prog->regflags = regflags;
4156 prog->engine = &nfa_regengine;
4157 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004158 prog->has_zend = nfa_has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004159 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004160#ifdef ENABLE_LOG
4161 nfa_postfix_dump(expr, OK);
4162 nfa_dump(prog);
4163#endif
4164
4165out:
4166 vim_free(post_start);
4167 post_start = post_ptr = post_end = NULL;
4168 state_ptr = NULL;
4169 return (regprog_T *)prog;
4170
4171fail:
4172 vim_free(prog);
4173 prog = NULL;
4174#ifdef ENABLE_LOG
4175 nfa_postfix_dump(expr, FAIL);
4176#endif
4177#ifdef DEBUG
4178 nfa_regengine.expr = NULL;
4179#endif
4180 goto out;
4181}
4182
4183
4184/*
4185 * Match a regexp against a string.
4186 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4187 * Uses curbuf for line count and 'iskeyword'.
4188 *
4189 * Return TRUE if there is a match, FALSE if not.
4190 */
4191 static int
4192nfa_regexec(rmp, line, col)
4193 regmatch_T *rmp;
4194 char_u *line; /* string to match against */
4195 colnr_T col; /* column to start looking for match */
4196{
4197 reg_match = rmp;
4198 reg_mmatch = NULL;
4199 reg_maxline = 0;
4200 reg_line_lbr = FALSE;
4201 reg_buf = curbuf;
4202 reg_win = NULL;
4203 ireg_ic = rmp->rm_ic;
4204#ifdef FEAT_MBYTE
4205 ireg_icombine = FALSE;
4206#endif
4207 ireg_maxcol = 0;
4208 return (nfa_regexec_both(line, col) != 0);
4209}
4210
4211#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4212 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4213
4214static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4215
4216/*
4217 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4218 */
4219 static int
4220nfa_regexec_nl(rmp, line, col)
4221 regmatch_T *rmp;
4222 char_u *line; /* string to match against */
4223 colnr_T col; /* column to start looking for match */
4224{
4225 reg_match = rmp;
4226 reg_mmatch = NULL;
4227 reg_maxline = 0;
4228 reg_line_lbr = TRUE;
4229 reg_buf = curbuf;
4230 reg_win = NULL;
4231 ireg_ic = rmp->rm_ic;
4232#ifdef FEAT_MBYTE
4233 ireg_icombine = FALSE;
4234#endif
4235 ireg_maxcol = 0;
4236 return (nfa_regexec_both(line, col) != 0);
4237}
4238#endif
4239
4240
4241/*
4242 * Match a regexp against multiple lines.
4243 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4244 * Uses curbuf for line count and 'iskeyword'.
4245 *
4246 * Return zero if there is no match. Return number of lines contained in the
4247 * match otherwise.
4248 *
4249 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4250 *
4251 * ! Also NOTE : match may actually be in another line. e.g.:
4252 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4253 *
4254 * +-------------------------+
4255 * |a |
4256 * |b |
4257 * |c |
4258 * | |
4259 * +-------------------------+
4260 *
4261 * then nfa_regexec_multi() returns 3. while the original
4262 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4263 *
4264 * FIXME if this behavior is not compatible.
4265 */
4266 static long
4267nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4268 regmmatch_T *rmp;
4269 win_T *win; /* window in which to search or NULL */
4270 buf_T *buf; /* buffer in which to search */
4271 linenr_T lnum; /* nr of line to start looking for match */
4272 colnr_T col; /* column to start looking for match */
4273 proftime_T *tm UNUSED; /* timeout limit or NULL */
4274{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004275 reg_match = NULL;
4276 reg_mmatch = rmp;
4277 reg_buf = buf;
4278 reg_win = win;
4279 reg_firstlnum = lnum;
4280 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4281 reg_line_lbr = FALSE;
4282 ireg_ic = rmp->rmm_ic;
4283#ifdef FEAT_MBYTE
4284 ireg_icombine = FALSE;
4285#endif
4286 ireg_maxcol = rmp->rmm_maxcol;
4287
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004288 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004289}
4290
4291#ifdef DEBUG
4292# undef ENABLE_LOG
4293#endif