blob: 9339cd3d42c00573859a09a8dcea0dac6eadc573 [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));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200191static int nfa_regatom __ARGS((void));
192static int nfa_regpiece __ARGS((void));
193static int nfa_regconcat __ARGS((void));
194static int nfa_regbranch __ARGS((void));
195static int nfa_reg __ARGS((int paren));
196#ifdef DEBUG
197static void nfa_set_code __ARGS((int c));
198static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200199static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
200static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200201static void nfa_dump __ARGS((nfa_regprog_T *prog));
202#endif
203static int *re2post __ARGS((void));
204static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
205static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
206static int check_char_class __ARGS((int class, int c));
207static void st_error __ARGS((int *postfix, int *end, int *p));
208static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
209static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
210static void nfa_set_null_listids __ARGS((nfa_state_T *start));
211static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
212static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
213static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
214static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
215static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
216static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
217
218/* helper functions used when doing re2post() ... regatom() parsing */
219#define EMIT(c) do { \
220 if (post_ptr >= post_end) \
221 return FAIL; \
222 *post_ptr++ = c; \
223 } while (0)
224
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200225/*
226 * Initialize internal variables before NFA compilation.
227 * Return OK on success, FAIL otherwise.
228 */
229 static int
230nfa_regcomp_start(expr, re_flags)
231 char_u *expr;
232 int re_flags; /* see vim_regcomp() */
233{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200234 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200235 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236
237 nstate = 0;
238 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200239 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200240 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200241
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200242 /* Some items blow up in size, such as [A-z]. Add more space for that.
243 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200244 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200245
246 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200248
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249 post_start = (int *)lalloc(postfix_size, TRUE);
250 if (post_start == NULL)
251 return FAIL;
252 vim_memset(post_start, 0, postfix_size);
253 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200254 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200255 nfa_has_zend = FALSE;
256
257 regcomp_start(expr, re_flags);
258
259 return OK;
260}
261
262/*
263 * Search between "start" and "end" and try to recognize a
264 * character class in expanded form. For example [0-9].
265 * On success, return the id the character class to be emitted.
266 * On failure, return 0 (=FAIL)
267 * Start points to the first char of the range, while end should point
268 * to the closing brace.
269 */
270 static int
271nfa_recognize_char_class(start, end, extra_newl)
272 char_u *start;
273 char_u *end;
274 int extra_newl;
275{
276 int i;
277 /* Each of these variables takes up a char in "config[]",
278 * in the order they are here. */
279 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
280 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
281 char_u *p;
282#define NCONFIGS 16
283 int classid[NCONFIGS] = {
284 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
285 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
286 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
287 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
288 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200289 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290 char_u config[NCONFIGS][9] = {
291 "000000100", /* digit */
292 "100000100", /* non digit */
293 "011000100", /* hex-digit */
294 "111000100", /* non hex-digit */
295 "000001000", /* octal-digit */
296 "100001000", /* [^0-7] */
297 "000110110", /* [0-9A-Za-z_] */
298 "100110110", /* [^0-9A-Za-z_] */
299 "000110010", /* head of word */
300 "100110010", /* not head of word */
301 "000110000", /* alphabetic char a-z */
302 "100110000", /* non alphabetic char */
303 "000100000", /* lowercase letter */
304 "100100000", /* non lowercase */
305 "000010000", /* uppercase */
306 "100010000" /* non uppercase */
307 };
308
309 if (extra_newl == TRUE)
310 newl = TRUE;
311
312 if (*end != ']')
313 return FAIL;
314 p = start;
315 if (*p == '^')
316 {
317 not = TRUE;
318 p ++;
319 }
320
321 while (p < end)
322 {
323 if (p + 2 < end && *(p + 1) == '-')
324 {
325 switch (*p)
326 {
327 case '0':
328 if (*(p + 2) == '9')
329 {
330 o9 = TRUE;
331 break;
332 }
333 else
334 if (*(p + 2) == '7')
335 {
336 o7 = TRUE;
337 break;
338 }
339 case 'a':
340 if (*(p + 2) == 'z')
341 {
342 az = TRUE;
343 break;
344 }
345 else
346 if (*(p + 2) == 'f')
347 {
348 af = TRUE;
349 break;
350 }
351 case 'A':
352 if (*(p + 2) == 'Z')
353 {
354 AZ = TRUE;
355 break;
356 }
357 else
358 if (*(p + 2) == 'F')
359 {
360 AF = TRUE;
361 break;
362 }
363 /* FALLTHROUGH */
364 default:
365 return FAIL;
366 }
367 p += 3;
368 }
369 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
370 {
371 newl = TRUE;
372 p += 2;
373 }
374 else if (*p == '_')
375 {
376 underscore = TRUE;
377 p ++;
378 }
379 else if (*p == '\n')
380 {
381 newl = TRUE;
382 p ++;
383 }
384 else
385 return FAIL;
386 } /* while (p < end) */
387
388 if (p != end)
389 return FAIL;
390
391 /* build the config that represents the ranges we gathered */
392 STRCPY(myconfig, "000000000");
393 if (not == TRUE)
394 myconfig[0] = '1';
395 if (af == TRUE)
396 myconfig[1] = '1';
397 if (AF == TRUE)
398 myconfig[2] = '1';
399 if (az == TRUE)
400 myconfig[3] = '1';
401 if (AZ == TRUE)
402 myconfig[4] = '1';
403 if (o7 == TRUE)
404 myconfig[5] = '1';
405 if (o9 == TRUE)
406 myconfig[6] = '1';
407 if (underscore == TRUE)
408 myconfig[7] = '1';
409 if (newl == TRUE)
410 {
411 myconfig[8] = '1';
412 extra_newl = ADD_NL;
413 }
414 /* try to recognize character classes */
415 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200416 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200417 return classid[i] + extra_newl;
418
419 /* fallthrough => no success so far */
420 return FAIL;
421
422#undef NCONFIGS
423}
424
425/*
426 * Produce the bytes for equivalence class "c".
427 * Currently only handles latin1, latin9 and utf-8.
428 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
429 * equivalent to 'a OR b OR c'
430 *
431 * NOTE! When changing this function, also update reg_equi_class()
432 */
433 static int
434nfa_emit_equi_class(c, neg)
435 int c;
436 int neg;
437{
438 int first = TRUE;
439 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
440#define EMIT2(c) \
441 EMIT(c); \
442 if (neg == TRUE) { \
443 EMIT(NFA_NOT); \
444 } \
445 if (first == FALSE) \
446 EMIT(glue); \
447 else \
448 first = FALSE; \
449
450#ifdef FEAT_MBYTE
451 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
452 || STRCMP(p_enc, "iso-8859-15") == 0)
453#endif
454 {
455 switch (c)
456 {
457 case 'A': case '\300': case '\301': case '\302':
458 case '\303': case '\304': case '\305':
459 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
460 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
461 EMIT2('\305');
462 return OK;
463
464 case 'C': case '\307':
465 EMIT2('C'); EMIT2('\307');
466 return OK;
467
468 case 'E': case '\310': case '\311': case '\312': case '\313':
469 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
470 EMIT2('\312'); EMIT2('\313');
471 return OK;
472
473 case 'I': case '\314': case '\315': case '\316': case '\317':
474 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
475 EMIT2('\316'); EMIT2('\317');
476 return OK;
477
478 case 'N': case '\321':
479 EMIT2('N'); EMIT2('\321');
480 return OK;
481
482 case 'O': case '\322': case '\323': case '\324': case '\325':
483 case '\326':
484 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
485 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
486 return OK;
487
488 case 'U': case '\331': case '\332': case '\333': case '\334':
489 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
490 EMIT2('\333'); EMIT2('\334');
491 return OK;
492
493 case 'Y': case '\335':
494 EMIT2('Y'); EMIT2('\335');
495 return OK;
496
497 case 'a': case '\340': case '\341': case '\342':
498 case '\343': case '\344': case '\345':
499 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
500 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
501 EMIT2('\345');
502 return OK;
503
504 case 'c': case '\347':
505 EMIT2('c'); EMIT2('\347');
506 return OK;
507
508 case 'e': case '\350': case '\351': case '\352': case '\353':
509 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
510 EMIT2('\352'); EMIT2('\353');
511 return OK;
512
513 case 'i': case '\354': case '\355': case '\356': case '\357':
514 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
515 EMIT2('\356'); EMIT2('\357');
516 return OK;
517
518 case 'n': case '\361':
519 EMIT2('n'); EMIT2('\361');
520 return OK;
521
522 case 'o': case '\362': case '\363': case '\364': case '\365':
523 case '\366':
524 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
525 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
526 return OK;
527
528 case 'u': case '\371': case '\372': case '\373': case '\374':
529 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
530 EMIT2('\373'); EMIT2('\374');
531 return OK;
532
533 case 'y': case '\375': case '\377':
534 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
535 return OK;
536
537 default:
538 return FAIL;
539 }
540 }
541
542 EMIT(c);
543 return OK;
544#undef EMIT2
545}
546
547/*
548 * Code to parse regular expression.
549 *
550 * We try to reuse parsing functions in regexp.c to
551 * minimize surprise and keep the syntax consistent.
552 */
553
554/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200555 * Parse the lowest level.
556 *
557 * An atom can be one of a long list of items. Many atoms match one character
558 * in the text. It is often an ordinary character or a character class.
559 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
560 * is only for syntax highlighting.
561 *
562 * atom ::= ordinary-atom
563 * or \( pattern \)
564 * or \%( pattern \)
565 * or \z( pattern \)
566 */
567 static int
568nfa_regatom()
569{
570 int c;
571 int charclass;
572 int equiclass;
573 int collclass;
574 int got_coll_char;
575 char_u *p;
576 char_u *endp;
577#ifdef FEAT_MBYTE
578 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200579#endif
580 int extra = 0;
581 int first;
582 int emit_range;
583 int negated;
584 int result;
585 int startc = -1;
586 int endc = -1;
587 int oldstartc = -1;
588 int cpo_lit; /* 'cpoptions' contains 'l' flag */
589 int cpo_bsl; /* 'cpoptions' contains '\' flag */
590 int glue; /* ID that will "glue" nodes together */
591
592 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
593 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
594
595 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200596 switch (c)
597 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200598 case NUL:
599 syntax_error = TRUE;
600 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
601
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200602 case Magic('^'):
603 EMIT(NFA_BOL);
604 break;
605
606 case Magic('$'):
607 EMIT(NFA_EOL);
608#if defined(FEAT_SYN_HL) || defined(PROTO)
609 had_eol = TRUE;
610#endif
611 break;
612
613 case Magic('<'):
614 EMIT(NFA_BOW);
615 break;
616
617 case Magic('>'):
618 EMIT(NFA_EOW);
619 break;
620
621 case Magic('_'):
622 c = no_Magic(getchr());
623 if (c == '^') /* "\_^" is start-of-line */
624 {
625 EMIT(NFA_BOL);
626 break;
627 }
628 if (c == '$') /* "\_$" is end-of-line */
629 {
630 EMIT(NFA_EOL);
631#if defined(FEAT_SYN_HL) || defined(PROTO)
632 had_eol = TRUE;
633#endif
634 break;
635 }
636
637 extra = ADD_NL;
638
639 /* "\_[" is collection plus newline */
640 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200641 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200642
643 /* "\_x" is character class plus newline */
644 /*FALLTHROUGH*/
645
646 /*
647 * Character classes.
648 */
649 case Magic('.'):
650 case Magic('i'):
651 case Magic('I'):
652 case Magic('k'):
653 case Magic('K'):
654 case Magic('f'):
655 case Magic('F'):
656 case Magic('p'):
657 case Magic('P'):
658 case Magic('s'):
659 case Magic('S'):
660 case Magic('d'):
661 case Magic('D'):
662 case Magic('x'):
663 case Magic('X'):
664 case Magic('o'):
665 case Magic('O'):
666 case Magic('w'):
667 case Magic('W'):
668 case Magic('h'):
669 case Magic('H'):
670 case Magic('a'):
671 case Magic('A'):
672 case Magic('l'):
673 case Magic('L'):
674 case Magic('u'):
675 case Magic('U'):
676 p = vim_strchr(classchars, no_Magic(c));
677 if (p == NULL)
678 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200679 EMSGN("INTERNAL: Unknown character class char: %ld", c);
680 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200681 }
682#ifdef FEAT_MBYTE
683 /* When '.' is followed by a composing char ignore the dot, so that
684 * the composing char is matched here. */
685 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
686 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200687 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200688 c = getchr();
689 goto nfa_do_multibyte;
690 }
691#endif
692 EMIT(nfa_classcodes[p - classchars]);
693 if (extra == ADD_NL)
694 {
695 EMIT(NFA_NEWL);
696 EMIT(NFA_OR);
697 regflags |= RF_HASNL;
698 }
699 break;
700
701 case Magic('n'):
702 if (reg_string)
703 /* In a string "\n" matches a newline character. */
704 EMIT(NL);
705 else
706 {
707 /* In buffer text "\n" matches the end of a line. */
708 EMIT(NFA_NEWL);
709 regflags |= RF_HASNL;
710 }
711 break;
712
713 case Magic('('):
714 if (nfa_reg(REG_PAREN) == FAIL)
715 return FAIL; /* cascaded error */
716 break;
717
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200718 case Magic('|'):
719 case Magic('&'):
720 case Magic(')'):
721 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200722 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200723 return FAIL;
724
725 case Magic('='):
726 case Magic('?'):
727 case Magic('+'):
728 case Magic('@'):
729 case Magic('*'):
730 case Magic('{'):
731 /* these should follow an atom, not form an atom */
732 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200733 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200734 return FAIL;
735
736 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200737 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200738 return FAIL;
739
Bram Moolenaar5714b802013-05-28 22:03:20 +0200740 case Magic('1'): EMIT(NFA_BACKREF1); break;
741 case Magic('2'): EMIT(NFA_BACKREF2); break;
742 case Magic('3'): EMIT(NFA_BACKREF3); break;
743 case Magic('4'): EMIT(NFA_BACKREF4); break;
744 case Magic('5'): EMIT(NFA_BACKREF5); break;
745 case Magic('6'): EMIT(NFA_BACKREF6); break;
746 case Magic('7'): EMIT(NFA_BACKREF7); break;
747 case Magic('8'): EMIT(NFA_BACKREF8); break;
748 case Magic('9'): EMIT(NFA_BACKREF9); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749
750 case Magic('z'):
751 c = no_Magic(getchr());
752 switch (c)
753 {
754 case 's':
755 EMIT(NFA_ZSTART);
756 break;
757 case 'e':
758 EMIT(NFA_ZEND);
759 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200760 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200761 case '1':
762 case '2':
763 case '3':
764 case '4':
765 case '5':
766 case '6':
767 case '7':
768 case '8':
769 case '9':
770 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200771 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200772 return FAIL;
773 default:
774 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200775 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 no_Magic(c));
777 return FAIL;
778 }
779 break;
780
781 case Magic('%'):
782 c = no_Magic(getchr());
783 switch (c)
784 {
785 /* () without a back reference */
786 case '(':
787 if (nfa_reg(REG_NPAREN) == FAIL)
788 return FAIL;
789 EMIT(NFA_NOPEN);
790 break;
791
792 case 'd': /* %d123 decimal */
793 case 'o': /* %o123 octal */
794 case 'x': /* %xab hex 2 */
795 case 'u': /* %uabcd hex 4 */
796 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200797 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200798 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200799
Bram Moolenaar47196582013-05-25 22:04:23 +0200800 switch (c)
801 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200802 case 'd': nr = getdecchrs(); break;
803 case 'o': nr = getoctchrs(); break;
804 case 'x': nr = gethexchrs(2); break;
805 case 'u': nr = gethexchrs(4); break;
806 case 'U': nr = gethexchrs(8); break;
807 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200808 }
809
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200810 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200811 EMSG2_RET_FAIL(
812 _("E678: Invalid character after %s%%[dxouU]"),
813 reg_magic == MAGIC_ALL);
814 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200815 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200816 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 break;
818
819 /* Catch \%^ and \%$ regardless of where they appear in the
820 * pattern -- regardless of whether or not it makes sense. */
821 case '^':
822 EMIT(NFA_BOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200823 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200824 return FAIL;
825 break;
826
827 case '$':
828 EMIT(NFA_EOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200829 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200830 return FAIL;
831 break;
832
833 case '#':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200834 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200835 return FAIL;
836 break;
837
838 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200839 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200840 return FAIL;
841 break;
842
843 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200844 /* TODO: \%[abc] not supported yet */
845 return FAIL;
846
847 case '0':
848 case '1':
849 case '2':
850 case '3':
851 case '4':
852 case '5':
853 case '6':
854 case '7':
855 case '8':
856 case '9':
857 case '<':
858 case '>':
859 case '\'':
860 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200861 return FAIL;
862
863 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +0200864 syntax_error = TRUE;
865 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
866 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867 return FAIL;
868 }
869 break;
870
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200871 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200872collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200873 /*
874 * Glue is emitted between several atoms from the [].
875 * It is either NFA_OR, or NFA_CONCAT.
876 *
877 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
878 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
879 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
880 * notation)
881 *
882 */
883
884
885/* Emit negation atoms, if needed.
886 * The CONCAT below merges the NOT with the previous node. */
887#define TRY_NEG() \
888 if (negated == TRUE) \
889 { \
890 EMIT(NFA_NOT); \
891 }
892
893/* Emit glue between important nodes : CONCAT or OR. */
894#define EMIT_GLUE() \
895 if (first == FALSE) \
896 EMIT(glue); \
897 else \
898 first = FALSE;
899
900 p = regparse;
901 endp = skip_anyof(p);
902 if (*endp == ']')
903 {
904 /*
905 * Try to reverse engineer character classes. For example,
906 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
907 * and perform the necessary substitutions in the NFA.
908 */
909 result = nfa_recognize_char_class(regparse, endp,
910 extra == ADD_NL);
911 if (result != FAIL)
912 {
913 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
914 EMIT(result);
915 else /* must be char class + newline */
916 {
917 EMIT(result - ADD_NL);
918 EMIT(NFA_NEWL);
919 EMIT(NFA_OR);
920 }
921 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200922 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200923 return OK;
924 }
925 /*
926 * Failed to recognize a character class. Use the simple
927 * version that turns [abc] into 'a' OR 'b' OR 'c'
928 */
929 startc = endc = oldstartc = -1;
930 first = TRUE; /* Emitting first atom in this sequence? */
931 negated = FALSE;
932 glue = NFA_OR;
933 if (*regparse == '^') /* negated range */
934 {
935 negated = TRUE;
936 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200937 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200938 }
939 if (*regparse == '-')
940 {
941 startc = '-';
942 EMIT(startc);
943 TRY_NEG();
944 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +0200945 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200946 }
947 /* Emit the OR branches for each character in the [] */
948 emit_range = FALSE;
949 while (regparse < endp)
950 {
951 oldstartc = startc;
952 startc = -1;
953 got_coll_char = FALSE;
954 if (*regparse == '[')
955 {
956 /* Check for [: :], [= =], [. .] */
957 equiclass = collclass = 0;
958 charclass = get_char_class(&regparse);
959 if (charclass == CLASS_NONE)
960 {
961 equiclass = get_equi_class(&regparse);
962 if (equiclass == 0)
963 collclass = get_coll_element(&regparse);
964 }
965
966 /* Character class like [:alpha:] */
967 if (charclass != CLASS_NONE)
968 {
969 switch (charclass)
970 {
971 case CLASS_ALNUM:
972 EMIT(NFA_CLASS_ALNUM);
973 break;
974 case CLASS_ALPHA:
975 EMIT(NFA_CLASS_ALPHA);
976 break;
977 case CLASS_BLANK:
978 EMIT(NFA_CLASS_BLANK);
979 break;
980 case CLASS_CNTRL:
981 EMIT(NFA_CLASS_CNTRL);
982 break;
983 case CLASS_DIGIT:
984 EMIT(NFA_CLASS_DIGIT);
985 break;
986 case CLASS_GRAPH:
987 EMIT(NFA_CLASS_GRAPH);
988 break;
989 case CLASS_LOWER:
990 EMIT(NFA_CLASS_LOWER);
991 break;
992 case CLASS_PRINT:
993 EMIT(NFA_CLASS_PRINT);
994 break;
995 case CLASS_PUNCT:
996 EMIT(NFA_CLASS_PUNCT);
997 break;
998 case CLASS_SPACE:
999 EMIT(NFA_CLASS_SPACE);
1000 break;
1001 case CLASS_UPPER:
1002 EMIT(NFA_CLASS_UPPER);
1003 break;
1004 case CLASS_XDIGIT:
1005 EMIT(NFA_CLASS_XDIGIT);
1006 break;
1007 case CLASS_TAB:
1008 EMIT(NFA_CLASS_TAB);
1009 break;
1010 case CLASS_RETURN:
1011 EMIT(NFA_CLASS_RETURN);
1012 break;
1013 case CLASS_BACKSPACE:
1014 EMIT(NFA_CLASS_BACKSPACE);
1015 break;
1016 case CLASS_ESCAPE:
1017 EMIT(NFA_CLASS_ESCAPE);
1018 break;
1019 }
1020 TRY_NEG();
1021 EMIT_GLUE();
1022 continue;
1023 }
1024 /* Try equivalence class [=a=] and the like */
1025 if (equiclass != 0)
1026 {
1027 result = nfa_emit_equi_class(equiclass, negated);
1028 if (result == FAIL)
1029 {
1030 /* should never happen */
1031 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1032 }
1033 EMIT_GLUE();
1034 continue;
1035 }
1036 /* Try collating class like [. .] */
1037 if (collclass != 0)
1038 {
1039 startc = collclass; /* allow [.a.]-x as a range */
1040 /* Will emit the proper atom at the end of the
1041 * while loop. */
1042 }
1043 }
1044 /* Try a range like 'a-x' or '\t-z' */
1045 if (*regparse == '-')
1046 {
1047 emit_range = TRUE;
1048 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001049 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001050 continue; /* reading the end of the range */
1051 }
1052
1053 /* Now handle simple and escaped characters.
1054 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1055 * accepts "\t", "\e", etc., but only when the 'l' flag in
1056 * 'cpoptions' is not included.
1057 * Posix doesn't recognize backslash at all.
1058 */
1059 if (*regparse == '\\'
1060 && !cpo_bsl
1061 && regparse + 1 <= endp
1062 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1063 || (!cpo_lit
1064 && vim_strchr(REGEXP_ABBR, regparse[1])
1065 != NULL)
1066 )
1067 )
1068 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001069 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001070
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001071 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001072 startc = reg_string ? NL : NFA_NEWL;
1073 else
1074 if (*regparse == 'd'
1075 || *regparse == 'o'
1076 || *regparse == 'x'
1077 || *regparse == 'u'
1078 || *regparse == 'U'
1079 )
1080 {
1081 /* TODO(RE) This needs more testing */
1082 startc = coll_get_char();
1083 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001084 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 }
1086 else
1087 {
1088 /* \r,\t,\e,\b */
1089 startc = backslash_trans(*regparse);
1090 }
1091 }
1092
1093 /* Normal printable char */
1094 if (startc == -1)
1095#ifdef FEAT_MBYTE
1096 startc = (*mb_ptr2char)(regparse);
1097#else
1098 startc = *regparse;
1099#endif
1100
1101 /* Previous char was '-', so this char is end of range. */
1102 if (emit_range)
1103 {
1104 endc = startc; startc = oldstartc;
1105 if (startc > endc)
1106 EMSG_RET_FAIL(_(e_invrange));
1107#ifdef FEAT_MBYTE
1108 if (has_mbyte && ((*mb_char2len)(startc) > 1
1109 || (*mb_char2len)(endc) > 1))
1110 {
1111 if (endc > startc + 256)
1112 EMSG_RET_FAIL(_(e_invrange));
1113 /* Emit the range. "startc" was already emitted, so
1114 * skip it. */
1115 for (c = startc + 1; c <= endc; c++)
1116 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001117 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001118 TRY_NEG();
1119 EMIT_GLUE();
1120 }
1121 emit_range = FALSE;
1122 }
1123 else
1124#endif
1125 {
1126#ifdef EBCDIC
1127 int alpha_only = FALSE;
1128
1129 /* for alphabetical range skip the gaps
1130 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1131 if (isalpha(startc) && isalpha(endc))
1132 alpha_only = TRUE;
1133#endif
1134 /* Emit the range. "startc" was already emitted, so
1135 * skip it. */
1136 for (c = startc + 1; c <= endc; c++)
1137#ifdef EBCDIC
1138 if (!alpha_only || isalpha(startc))
1139#endif
1140 {
1141 EMIT(c);
1142 TRY_NEG();
1143 EMIT_GLUE();
1144 }
1145 emit_range = FALSE;
1146 }
1147 }
1148 else
1149 {
1150 /*
1151 * This char (startc) is not part of a range. Just
1152 * emit it.
1153 *
1154 * Normally, simply emit startc. But if we get char
1155 * code=0 from a collating char, then replace it with
1156 * 0x0a.
1157 *
1158 * This is needed to completely mimic the behaviour of
1159 * the backtracking engine.
1160 */
1161 if (got_coll_char == TRUE && startc == 0)
1162 EMIT(0x0a);
1163 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001164 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001165 TRY_NEG();
1166 EMIT_GLUE();
1167 }
1168
Bram Moolenaar51a29832013-05-28 22:30:35 +02001169 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001170 } /* while (p < endp) */
1171
Bram Moolenaar51a29832013-05-28 22:30:35 +02001172 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001173 if (*regparse == '-') /* if last, '-' is just a char */
1174 {
1175 EMIT('-');
1176 TRY_NEG();
1177 EMIT_GLUE();
1178 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001179 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001180
1181 if (extra == ADD_NL) /* \_[] also matches \n */
1182 {
1183 EMIT(reg_string ? NL : NFA_NEWL);
1184 TRY_NEG();
1185 EMIT_GLUE();
1186 }
1187
1188 /* skip the trailing ] */
1189 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001190 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001191 if (negated == TRUE)
1192 {
1193 /* Mark end of negated char range */
1194 EMIT(NFA_END_NEG_RANGE);
1195 EMIT(NFA_CONCAT);
1196 }
1197 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001198 } /* if exists closing ] */
1199
1200 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 {
1202 syntax_error = TRUE;
1203 EMSG_RET_FAIL(_(e_missingbracket));
1204 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001205 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001207 default:
1208 {
1209#ifdef FEAT_MBYTE
1210 int plen;
1211
1212nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001213 /* plen is length of current char with composing chars */
1214 if (enc_utf8 && ((*mb_char2len)(c)
1215 != (plen = (*mb_ptr2len)(old_regparse))
1216 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001217 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001218 int i = 0;
1219
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001220 /* A base character plus composing characters, or just one
1221 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001222 * This requires creating a separate atom as if enclosing
1223 * the characters in (), where NFA_COMPOSING is the ( and
1224 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 * building the postfix form, not the NFA itself;
1226 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001227 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001228 for (;;)
1229 {
1230 EMIT(c);
1231 if (i > 0)
1232 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001233 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001234 break;
1235 c = utf_ptr2char(old_regparse + i);
1236 }
1237 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 regparse = old_regparse + plen;
1239 }
1240 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241#endif
1242 {
1243 c = no_Magic(c);
1244 EMIT(c);
1245 }
1246 return OK;
1247 }
1248 }
1249
1250#undef TRY_NEG
1251#undef EMIT_GLUE
1252
1253 return OK;
1254}
1255
1256/*
1257 * Parse something followed by possible [*+=].
1258 *
1259 * A piece is an atom, possibly followed by a multi, an indication of how many
1260 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1261 * characters: "", "a", "aa", etc.
1262 *
1263 * piece ::= atom
1264 * or atom multi
1265 */
1266 static int
1267nfa_regpiece()
1268{
1269 int i;
1270 int op;
1271 int ret;
1272 long minval, maxval;
1273 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1274 char_u *old_regparse, *new_regparse;
1275 int c2;
1276 int *old_post_ptr, *my_post_start;
1277 int old_regnpar;
1278 int quest;
1279
1280 /* Save the current position in the regexp, so that we can use it if
1281 * <atom>{m,n} is next. */
1282 old_regparse = regparse;
1283 /* Save current number of open parenthesis, so we can use it if
1284 * <atom>{m,n} is next */
1285 old_regnpar = regnpar;
1286 /* store current pos in the postfix form, for \{m,n} involving 0s */
1287 my_post_start = post_ptr;
1288
1289 ret = nfa_regatom();
1290 if (ret == FAIL)
1291 return FAIL; /* cascaded error */
1292
1293 op = peekchr();
1294 if (re_multi_type(op) == NOT_MULTI)
1295 return OK;
1296
1297 skipchr();
1298 switch (op)
1299 {
1300 case Magic('*'):
1301 EMIT(NFA_STAR);
1302 break;
1303
1304 case Magic('+'):
1305 /*
1306 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1307 * first and only submatch would be "aaa". But the backtracking
1308 * engine interprets the plus as "try matching one more time", and
1309 * a* matches a second time at the end of the input, the empty
1310 * string.
1311 * The submatch will the empty string.
1312 *
1313 * In order to be consistent with the old engine, we disable
1314 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1315 */
1316 /* EMIT(NFA_PLUS); */
1317 regnpar = old_regnpar;
1318 regparse = old_regparse;
1319 curchr = -1;
1320 if (nfa_regatom() == FAIL)
1321 return FAIL;
1322 EMIT(NFA_STAR);
1323 EMIT(NFA_CONCAT);
1324 skipchr(); /* skip the \+ */
1325 break;
1326
1327 case Magic('@'):
1328 op = no_Magic(getchr());
1329 switch(op)
1330 {
1331 case '=':
1332 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1333 break;
1334 case '!':
1335 case '<':
1336 case '>':
1337 /* Not supported yet */
1338 return FAIL;
1339 default:
1340 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001341 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342 return FAIL;
1343 }
1344 break;
1345
1346 case Magic('?'):
1347 case Magic('='):
1348 EMIT(NFA_QUEST);
1349 break;
1350
1351 case Magic('{'):
1352 /* a{2,5} will expand to 'aaa?a?a?'
1353 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1354 * version of '?'
1355 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1356 * parenthesis have the same id
1357 */
1358
1359 greedy = TRUE;
1360 c2 = peekchr();
1361 if (c2 == '-' || c2 == Magic('-'))
1362 {
1363 skipchr();
1364 greedy = FALSE;
1365 }
1366 if (!read_limits(&minval, &maxval))
1367 {
1368 syntax_error = TRUE;
1369 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1370 }
1371 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1372 * <atom>* */
1373 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1374 {
1375 EMIT(NFA_STAR);
1376 break;
1377 }
1378
1379 if (maxval > NFA_BRACES_MAXLIMIT)
1380 {
1381 /* This would yield a huge automaton and use too much memory.
1382 * Revert to old engine */
1383 return FAIL;
1384 }
1385
1386 /* Special case: x{0} or x{-0} */
1387 if (maxval == 0)
1388 {
1389 /* Ignore result of previous call to nfa_regatom() */
1390 post_ptr = my_post_start;
1391 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1392 EMIT(NFA_SKIP_CHAR);
1393 return OK;
1394 }
1395
1396 /* Ignore previous call to nfa_regatom() */
1397 post_ptr = my_post_start;
1398 /* Save pos after the repeated atom and the \{} */
1399 new_regparse = regparse;
1400
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001401 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1402 for (i = 0; i < maxval; i++)
1403 {
1404 /* Goto beginning of the repeated atom */
1405 regparse = old_regparse;
1406 curchr = -1;
1407 /* Restore count of parenthesis */
1408 regnpar = old_regnpar;
1409 old_post_ptr = post_ptr;
1410 if (nfa_regatom() == FAIL)
1411 return FAIL;
1412 /* after "minval" times, atoms are optional */
1413 if (i + 1 > minval)
1414 EMIT(quest);
1415 if (old_post_ptr != my_post_start)
1416 EMIT(NFA_CONCAT);
1417 }
1418
1419 /* Go to just after the repeated atom and the \{} */
1420 regparse = new_regparse;
1421 curchr = -1;
1422
1423 break;
1424
1425
1426 default:
1427 break;
1428 } /* end switch */
1429
1430 if (re_multi_type(peekchr()) != NOT_MULTI)
1431 {
1432 /* Can't have a multi follow a multi. */
1433 syntax_error = TRUE;
1434 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1435 }
1436
1437 return OK;
1438}
1439
1440/*
1441 * Parse one or more pieces, concatenated. It matches a match for the
1442 * first piece, followed by a match for the second piece, etc. Example:
1443 * "f[0-9]b", first matches "f", then a digit and then "b".
1444 *
1445 * concat ::= piece
1446 * or piece piece
1447 * or piece piece piece
1448 * etc.
1449 */
1450 static int
1451nfa_regconcat()
1452{
1453 int cont = TRUE;
1454 int first = TRUE;
1455
1456 while (cont)
1457 {
1458 switch (peekchr())
1459 {
1460 case NUL:
1461 case Magic('|'):
1462 case Magic('&'):
1463 case Magic(')'):
1464 cont = FALSE;
1465 break;
1466
1467 case Magic('Z'):
1468#ifdef FEAT_MBYTE
1469 regflags |= RF_ICOMBINE;
1470#endif
1471 skipchr_keepstart();
1472 break;
1473 case Magic('c'):
1474 regflags |= RF_ICASE;
1475 skipchr_keepstart();
1476 break;
1477 case Magic('C'):
1478 regflags |= RF_NOICASE;
1479 skipchr_keepstart();
1480 break;
1481 case Magic('v'):
1482 reg_magic = MAGIC_ALL;
1483 skipchr_keepstart();
1484 curchr = -1;
1485 break;
1486 case Magic('m'):
1487 reg_magic = MAGIC_ON;
1488 skipchr_keepstart();
1489 curchr = -1;
1490 break;
1491 case Magic('M'):
1492 reg_magic = MAGIC_OFF;
1493 skipchr_keepstart();
1494 curchr = -1;
1495 break;
1496 case Magic('V'):
1497 reg_magic = MAGIC_NONE;
1498 skipchr_keepstart();
1499 curchr = -1;
1500 break;
1501
1502 default:
1503 if (nfa_regpiece() == FAIL)
1504 return FAIL;
1505 if (first == FALSE)
1506 EMIT(NFA_CONCAT);
1507 else
1508 first = FALSE;
1509 break;
1510 }
1511 }
1512
1513 return OK;
1514}
1515
1516/*
1517 * Parse a branch, one or more concats, separated by "\&". It matches the
1518 * last concat, but only if all the preceding concats also match at the same
1519 * position. Examples:
1520 * "foobeep\&..." matches "foo" in "foobeep".
1521 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1522 *
1523 * branch ::= concat
1524 * or concat \& concat
1525 * or concat \& concat \& concat
1526 * etc.
1527 */
1528 static int
1529nfa_regbranch()
1530{
1531 int ch;
1532 int *old_post_ptr;
1533
1534 old_post_ptr = post_ptr;
1535
1536 /* First branch, possibly the only one */
1537 if (nfa_regconcat() == FAIL)
1538 return FAIL;
1539
1540 ch = peekchr();
1541 /* Try next concats */
1542 while (ch == Magic('&'))
1543 {
1544 skipchr();
1545 EMIT(NFA_NOPEN);
1546 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1547 old_post_ptr = post_ptr;
1548 if (nfa_regconcat() == FAIL)
1549 return FAIL;
1550 /* if concat is empty, skip a input char. But do emit a node */
1551 if (old_post_ptr == post_ptr)
1552 EMIT(NFA_SKIP_CHAR);
1553 EMIT(NFA_CONCAT);
1554 ch = peekchr();
1555 }
1556
1557 /* Even if a branch is empty, emit one node for it */
1558 if (old_post_ptr == post_ptr)
1559 EMIT(NFA_SKIP_CHAR);
1560
1561 return OK;
1562}
1563
1564/*
1565 * Parse a pattern, one or more branches, separated by "\|". It matches
1566 * anything that matches one of the branches. Example: "foo\|beep" matches
1567 * "foo" and matches "beep". If more than one branch matches, the first one
1568 * is used.
1569 *
1570 * pattern ::= branch
1571 * or branch \| branch
1572 * or branch \| branch \| branch
1573 * etc.
1574 */
1575 static int
1576nfa_reg(paren)
1577 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1578{
1579 int parno = 0;
1580
1581#ifdef FEAT_SYN_HL
1582#endif
1583 if (paren == REG_PAREN)
1584 {
1585 if (regnpar >= NSUBEXP) /* Too many `(' */
1586 {
1587 syntax_error = TRUE;
1588 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1589 }
1590 parno = regnpar++;
1591 }
1592
1593 if (nfa_regbranch() == FAIL)
1594 return FAIL; /* cascaded error */
1595
1596 while (peekchr() == Magic('|'))
1597 {
1598 skipchr();
1599 if (nfa_regbranch() == FAIL)
1600 return FAIL; /* cascaded error */
1601 EMIT(NFA_OR);
1602 }
1603
1604 /* Check for proper termination. */
1605 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1606 {
1607 syntax_error = TRUE;
1608 if (paren == REG_NPAREN)
1609 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1610 else
1611 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1612 }
1613 else if (paren == REG_NOPAREN && peekchr() != NUL)
1614 {
1615 syntax_error = TRUE;
1616 if (peekchr() == Magic(')'))
1617 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1618 else
1619 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1620 }
1621 /*
1622 * Here we set the flag allowing back references to this set of
1623 * parentheses.
1624 */
1625 if (paren == REG_PAREN)
1626 {
1627 had_endbrace[parno] = TRUE; /* have seen the close paren */
1628 EMIT(NFA_MOPEN + parno);
1629 }
1630
1631 return OK;
1632}
1633
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001634#ifdef DEBUG
1635static char_u code[50];
1636
1637 static void
1638nfa_set_code(c)
1639 int c;
1640{
1641 int addnl = FALSE;
1642
1643 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1644 {
1645 addnl = TRUE;
1646 c -= ADD_NL;
1647 }
1648
1649 STRCPY(code, "");
1650 switch (c)
1651 {
1652 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1653 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1654 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1655 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1656 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1657 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1658
Bram Moolenaar5714b802013-05-28 22:03:20 +02001659 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1660 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1661 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1662 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1663 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1664 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1665 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1666 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1667 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1668 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1669
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001670 case NFA_PREV_ATOM_NO_WIDTH:
1671 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1672 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1673 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1674 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1675 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1676
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1678 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1679
1680 case NFA_MOPEN + 0:
1681 case NFA_MOPEN + 1:
1682 case NFA_MOPEN + 2:
1683 case NFA_MOPEN + 3:
1684 case NFA_MOPEN + 4:
1685 case NFA_MOPEN + 5:
1686 case NFA_MOPEN + 6:
1687 case NFA_MOPEN + 7:
1688 case NFA_MOPEN + 8:
1689 case NFA_MOPEN + 9:
1690 STRCPY(code, "NFA_MOPEN(x)");
1691 code[10] = c - NFA_MOPEN + '0';
1692 break;
1693 case NFA_MCLOSE + 0:
1694 case NFA_MCLOSE + 1:
1695 case NFA_MCLOSE + 2:
1696 case NFA_MCLOSE + 3:
1697 case NFA_MCLOSE + 4:
1698 case NFA_MCLOSE + 5:
1699 case NFA_MCLOSE + 6:
1700 case NFA_MCLOSE + 7:
1701 case NFA_MCLOSE + 8:
1702 case NFA_MCLOSE + 9:
1703 STRCPY(code, "NFA_MCLOSE(x)");
1704 code[11] = c - NFA_MCLOSE + '0';
1705 break;
1706 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1707 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1708 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1709 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1710 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1711 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1712 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1713 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1714 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1715 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1716 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1717 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1718 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1719 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1720 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1721 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1722 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1723 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1724 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1725 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1726 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1727 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1728 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1729 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1730 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1731 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1732 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1733 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1734
1735 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1736 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1737 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1738 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1739 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1740 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1741 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1742 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1743 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1744 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1745 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1746 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1747 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1748 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1749 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1750 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1751 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1752 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1753 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1754 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1755 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1756 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1757 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1758 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1759 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1760 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1761 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1762
1763 default:
1764 STRCPY(code, "CHAR(x)");
1765 code[5] = c;
1766 }
1767
1768 if (addnl == TRUE)
1769 STRCAT(code, " + NEWLINE ");
1770
1771}
1772
1773#ifdef ENABLE_LOG
1774static FILE *log_fd;
1775
1776/*
1777 * Print the postfix notation of the current regexp.
1778 */
1779 static void
1780nfa_postfix_dump(expr, retval)
1781 char_u *expr;
1782 int retval;
1783{
1784 int *p;
1785 FILE *f;
1786
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001787 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 if (f != NULL)
1789 {
1790 fprintf(f, "\n-------------------------\n");
1791 if (retval == FAIL)
1792 fprintf(f, ">>> NFA engine failed ... \n");
1793 else if (retval == OK)
1794 fprintf(f, ">>> NFA engine succeeded !\n");
1795 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001796 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 {
1798 nfa_set_code(*p);
1799 fprintf(f, "%s, ", code);
1800 }
1801 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001802 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001803 fprintf(f, "%d ", *p);
1804 fprintf(f, "\n\n");
1805 fclose(f);
1806 }
1807}
1808
1809/*
1810 * Print the NFA starting with a root node "state".
1811 */
1812 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001813nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001814 FILE *debugf;
1815 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001817 garray_T indent;
1818
1819 ga_init2(&indent, 1, 64);
1820 ga_append(&indent, '\0');
1821 nfa_print_state2(debugf, state, &indent);
1822 ga_clear(&indent);
1823}
1824
1825 static void
1826nfa_print_state2(debugf, state, indent)
1827 FILE *debugf;
1828 nfa_state_T *state;
1829 garray_T *indent;
1830{
1831 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832
1833 if (state == NULL)
1834 return;
1835
1836 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001837
1838 /* Output indent */
1839 p = (char_u *)indent->ga_data;
1840 if (indent->ga_len >= 3)
1841 {
1842 int last = indent->ga_len - 3;
1843 char_u save[2];
1844
1845 STRNCPY(save, &p[last], 2);
1846 STRNCPY(&p[last], "+-", 2);
1847 fprintf(debugf, " %s", p);
1848 STRNCPY(&p[last], save, 2);
1849 }
1850 else
1851 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001852
1853 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001854 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1855 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856 if (state->id < 0)
1857 return;
1858
1859 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001860
1861 /* grow indent for state->out */
1862 indent->ga_len -= 1;
1863 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001864 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001865 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001866 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001867 ga_append(indent, '\0');
1868
1869 nfa_print_state2(debugf, state->out, indent);
1870
1871 /* replace last part of indent for state->out1 */
1872 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001873 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001874 ga_append(indent, '\0');
1875
1876 nfa_print_state2(debugf, state->out1, indent);
1877
1878 /* shrink indent */
1879 indent->ga_len -= 3;
1880 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881}
1882
1883/*
1884 * Print the NFA state machine.
1885 */
1886 static void
1887nfa_dump(prog)
1888 nfa_regprog_T *prog;
1889{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001890 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001891
1892 if (debugf != NULL)
1893 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001894 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 fclose(debugf);
1896 }
1897}
1898#endif /* ENABLE_LOG */
1899#endif /* DEBUG */
1900
1901/*
1902 * Parse r.e. @expr and convert it into postfix form.
1903 * Return the postfix string on success, NULL otherwise.
1904 */
1905 static int *
1906re2post()
1907{
1908 if (nfa_reg(REG_NOPAREN) == FAIL)
1909 return NULL;
1910 EMIT(NFA_MOPEN);
1911 return post_start;
1912}
1913
1914/* NB. Some of the code below is inspired by Russ's. */
1915
1916/*
1917 * Represents an NFA state plus zero or one or two arrows exiting.
1918 * if c == MATCH, no arrows out; matching state.
1919 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1920 * If c < 256, labeled arrow with character c to out.
1921 */
1922
1923static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1924
1925/*
1926 * Allocate and initialize nfa_state_T.
1927 */
1928 static nfa_state_T *
1929new_state(c, out, out1)
1930 int c;
1931 nfa_state_T *out;
1932 nfa_state_T *out1;
1933{
1934 nfa_state_T *s;
1935
1936 if (istate >= nstate)
1937 return NULL;
1938
1939 s = &state_ptr[istate++];
1940
1941 s->c = c;
1942 s->out = out;
1943 s->out1 = out1;
1944
1945 s->id = istate;
1946 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001947 s->negated = FALSE;
1948
1949 return s;
1950}
1951
1952/*
1953 * A partially built NFA without the matching state filled in.
1954 * Frag_T.start points at the start state.
1955 * Frag_T.out is a list of places that need to be set to the
1956 * next state for this fragment.
1957 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001958
1959/* Since the out pointers in the list are always
1960 * uninitialized, we use the pointers themselves
1961 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001963union Ptrlist
1964{
1965 Ptrlist *next;
1966 nfa_state_T *s;
1967};
1968
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001969struct Frag
1970{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001971 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001972 Ptrlist *out;
1973};
1974typedef struct Frag Frag_T;
1975
1976static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
1977static Ptrlist *list1 __ARGS((nfa_state_T **outp));
1978static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
1979static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
1980static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
1981static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
1982
1983/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02001984 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001985 */
1986 static Frag_T
1987frag(start, out)
1988 nfa_state_T *start;
1989 Ptrlist *out;
1990{
Bram Moolenaar053bb602013-05-20 13:55:21 +02001991 Frag_T n;
1992
1993 n.start = start;
1994 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001995 return n;
1996}
1997
1998/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999 * Create singleton list containing just outp.
2000 */
2001 static Ptrlist *
2002list1(outp)
2003 nfa_state_T **outp;
2004{
2005 Ptrlist *l;
2006
2007 l = (Ptrlist *)outp;
2008 l->next = NULL;
2009 return l;
2010}
2011
2012/*
2013 * Patch the list of states at out to point to start.
2014 */
2015 static void
2016patch(l, s)
2017 Ptrlist *l;
2018 nfa_state_T *s;
2019{
2020 Ptrlist *next;
2021
2022 for (; l; l = next)
2023 {
2024 next = l->next;
2025 l->s = s;
2026 }
2027}
2028
2029
2030/*
2031 * Join the two lists l1 and l2, returning the combination.
2032 */
2033 static Ptrlist *
2034append(l1, l2)
2035 Ptrlist *l1;
2036 Ptrlist *l2;
2037{
2038 Ptrlist *oldl1;
2039
2040 oldl1 = l1;
2041 while (l1->next)
2042 l1 = l1->next;
2043 l1->next = l2;
2044 return oldl1;
2045}
2046
2047/*
2048 * Stack used for transforming postfix form into NFA.
2049 */
2050static Frag_T empty;
2051
2052 static void
2053st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002054 int *postfix UNUSED;
2055 int *end UNUSED;
2056 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002057{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002058#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059 FILE *df;
2060 int *p2;
2061
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002062 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063 if (df)
2064 {
2065 fprintf(df, "Error popping the stack!\n");
2066#ifdef DEBUG
2067 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2068#endif
2069 fprintf(df, "Postfix form is: ");
2070#ifdef DEBUG
2071 for (p2 = postfix; p2 < end; p2++)
2072 {
2073 nfa_set_code(*p2);
2074 fprintf(df, "%s, ", code);
2075 }
2076 nfa_set_code(*p);
2077 fprintf(df, "\nCurrent position is: ");
2078 for (p2 = postfix; p2 <= p; p2 ++)
2079 {
2080 nfa_set_code(*p2);
2081 fprintf(df, "%s, ", code);
2082 }
2083#else
2084 for (p2 = postfix; p2 < end; p2++)
2085 {
2086 fprintf(df, "%d, ", *p2);
2087 }
2088 fprintf(df, "\nCurrent position is: ");
2089 for (p2 = postfix; p2 <= p; p2 ++)
2090 {
2091 fprintf(df, "%d, ", *p2);
2092 }
2093#endif
2094 fprintf(df, "\n--------------------------\n");
2095 fclose(df);
2096 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002097#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002098 EMSG(_("E874: (NFA) Could not pop the stack !"));
2099}
2100
2101/*
2102 * Push an item onto the stack.
2103 */
2104 static void
2105st_push(s, p, stack_end)
2106 Frag_T s;
2107 Frag_T **p;
2108 Frag_T *stack_end;
2109{
2110 Frag_T *stackp = *p;
2111
2112 if (stackp >= stack_end)
2113 return;
2114 *stackp = s;
2115 *p = *p + 1;
2116}
2117
2118/*
2119 * Pop an item from the stack.
2120 */
2121 static Frag_T
2122st_pop(p, stack)
2123 Frag_T **p;
2124 Frag_T *stack;
2125{
2126 Frag_T *stackp;
2127
2128 *p = *p - 1;
2129 stackp = *p;
2130 if (stackp < stack)
2131 return empty;
2132 return **p;
2133}
2134
2135/*
2136 * Convert a postfix form into its equivalent NFA.
2137 * Return the NFA start state on success, NULL otherwise.
2138 */
2139 static nfa_state_T *
2140post2nfa(postfix, end, nfa_calc_size)
2141 int *postfix;
2142 int *end;
2143 int nfa_calc_size;
2144{
2145 int *p;
2146 int mopen;
2147 int mclose;
2148 Frag_T *stack = NULL;
2149 Frag_T *stackp = NULL;
2150 Frag_T *stack_end = NULL;
2151 Frag_T e1;
2152 Frag_T e2;
2153 Frag_T e;
2154 nfa_state_T *s;
2155 nfa_state_T *s1;
2156 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002157 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002158
2159 if (postfix == NULL)
2160 return NULL;
2161
Bram Moolenaar053bb602013-05-20 13:55:21 +02002162#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163#define POP() st_pop(&stackp, stack); \
2164 if (stackp < stack) \
2165 { \
2166 st_error(postfix, end, p); \
2167 return NULL; \
2168 }
2169
2170 if (nfa_calc_size == FALSE)
2171 {
2172 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002173 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002174 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002175 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002176 }
2177
2178 for (p = postfix; p < end; ++p)
2179 {
2180 switch (*p)
2181 {
2182 case NFA_CONCAT:
2183 /* Catenation.
2184 * Pay attention: this operator does not exist
2185 * in the r.e. itself (it is implicit, really).
2186 * It is added when r.e. is translated to postfix
2187 * form in re2post().
2188 *
2189 * No new state added here. */
2190 if (nfa_calc_size == TRUE)
2191 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002192 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002193 break;
2194 }
2195 e2 = POP();
2196 e1 = POP();
2197 patch(e1.out, e2.start);
2198 PUSH(frag(e1.start, e2.out));
2199 break;
2200
2201 case NFA_NOT:
2202 /* Negation of a character */
2203 if (nfa_calc_size == TRUE)
2204 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002205 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002206 break;
2207 }
2208 e1 = POP();
2209 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002210#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002211 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002212 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002213#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002214 PUSH(e1);
2215 break;
2216
2217 case NFA_OR:
2218 /* Alternation */
2219 if (nfa_calc_size == TRUE)
2220 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002221 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002222 break;
2223 }
2224 e2 = POP();
2225 e1 = POP();
2226 s = new_state(NFA_SPLIT, e1.start, e2.start);
2227 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002228 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229 PUSH(frag(s, append(e1.out, e2.out)));
2230 break;
2231
2232 case NFA_STAR:
2233 /* Zero or more */
2234 if (nfa_calc_size == TRUE)
2235 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002236 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237 break;
2238 }
2239 e = POP();
2240 s = new_state(NFA_SPLIT, e.start, NULL);
2241 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002242 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 patch(e.out, s);
2244 PUSH(frag(s, list1(&s->out1)));
2245 break;
2246
2247 case NFA_QUEST:
2248 /* one or zero atoms=> greedy match */
2249 if (nfa_calc_size == TRUE)
2250 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002251 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002252 break;
2253 }
2254 e = POP();
2255 s = new_state(NFA_SPLIT, e.start, NULL);
2256 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002257 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002258 PUSH(frag(s, append(e.out, list1(&s->out1))));
2259 break;
2260
2261 case NFA_QUEST_NONGREEDY:
2262 /* zero or one atoms => non-greedy match */
2263 if (nfa_calc_size == TRUE)
2264 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002265 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266 break;
2267 }
2268 e = POP();
2269 s = new_state(NFA_SPLIT, NULL, e.start);
2270 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002271 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002272 PUSH(frag(s, append(e.out, list1(&s->out))));
2273 break;
2274
2275 case NFA_PLUS:
2276 /* One or more */
2277 if (nfa_calc_size == TRUE)
2278 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002279 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002280 break;
2281 }
2282 e = POP();
2283 s = new_state(NFA_SPLIT, e.start, NULL);
2284 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002285 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002286 patch(e.out, s);
2287 PUSH(frag(e.start, list1(&s->out1)));
2288 break;
2289
2290 case NFA_SKIP_CHAR:
2291 /* Symbol of 0-length, Used in a repetition
2292 * with max/min count of 0 */
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 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2299 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002300 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002301 PUSH(frag(s, list1(&s->out)));
2302 break;
2303
2304 case NFA_PREV_ATOM_NO_WIDTH:
2305 /* The \@= operator: match the preceding atom with 0 width.
2306 * Surrounds the preceding atom with START_INVISIBLE and
2307 * END_INVISIBLE, similarly to MOPEN.
2308 */
2309 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002310 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002311
2312 if (nfa_calc_size == TRUE)
2313 {
2314 nstate += 2;
2315 break;
2316 }
2317 e = POP();
2318 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2319 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002320 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002321 patch(e.out, s1);
2322
2323 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2324 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002325 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002326 PUSH(frag(s, list1(&s1->out)));
2327 break;
2328
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002329#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002330 case NFA_COMPOSING: /* char with composing char */
2331#if 0
2332 /* TODO */
2333 if (regflags & RF_ICOMBINE)
2334 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002335 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002336 }
2337#endif
2338 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002339#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002341 case NFA_MOPEN + 0: /* Submatch */
2342 case NFA_MOPEN + 1:
2343 case NFA_MOPEN + 2:
2344 case NFA_MOPEN + 3:
2345 case NFA_MOPEN + 4:
2346 case NFA_MOPEN + 5:
2347 case NFA_MOPEN + 6:
2348 case NFA_MOPEN + 7:
2349 case NFA_MOPEN + 8:
2350 case NFA_MOPEN + 9:
2351 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352 if (nfa_calc_size == TRUE)
2353 {
2354 nstate += 2;
2355 break;
2356 }
2357
2358 mopen = *p;
2359 switch (*p)
2360 {
2361 case NFA_NOPEN:
2362 mclose = NFA_NCLOSE;
2363 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002364#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365 case NFA_COMPOSING:
2366 mclose = NFA_END_COMPOSING;
2367 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002368#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369 default:
2370 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2371 mclose = *p + NSUBEXP;
2372 break;
2373 }
2374
2375 /* Allow "NFA_MOPEN" as a valid postfix representation for
2376 * the empty regexp "". In this case, the NFA will be
2377 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2378 * empty groups of parenthesis, and empty mbyte chars */
2379 if (stackp == stack)
2380 {
2381 s = new_state(mopen, NULL, NULL);
2382 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002383 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384 s1 = new_state(mclose, NULL, NULL);
2385 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002386 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387 patch(list1(&s->out), s1);
2388 PUSH(frag(s, list1(&s1->out)));
2389 break;
2390 }
2391
2392 /* At least one node was emitted before NFA_MOPEN, so
2393 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2394 e = POP();
2395 s = new_state(mopen, e.start, NULL); /* `(' */
2396 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002397 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398
2399 s1 = new_state(mclose, NULL, NULL); /* `)' */
2400 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002401 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002402 patch(e.out, s1);
2403
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002404#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002405 if (mopen == NFA_COMPOSING)
2406 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002407 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002408#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002409
2410 PUSH(frag(s, list1(&s1->out)));
2411 break;
2412
Bram Moolenaar5714b802013-05-28 22:03:20 +02002413 case NFA_BACKREF1:
2414 case NFA_BACKREF2:
2415 case NFA_BACKREF3:
2416 case NFA_BACKREF4:
2417 case NFA_BACKREF5:
2418 case NFA_BACKREF6:
2419 case NFA_BACKREF7:
2420 case NFA_BACKREF8:
2421 case NFA_BACKREF9:
2422 if (nfa_calc_size == TRUE)
2423 {
2424 nstate += 2;
2425 break;
2426 }
2427 s = new_state(*p, NULL, NULL);
2428 if (s == NULL)
2429 goto theend;
2430 s1 = new_state(NFA_SKIP, NULL, NULL);
2431 if (s1 == NULL)
2432 goto theend;
2433 patch(list1(&s->out), s1);
2434 PUSH(frag(s, list1(&s1->out)));
2435 break;
2436
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002437 case NFA_ZSTART:
2438 case NFA_ZEND:
2439 default:
2440 /* Operands */
2441 if (nfa_calc_size == TRUE)
2442 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002443 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 break;
2445 }
2446 s = new_state(*p, NULL, NULL);
2447 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002448 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002449 PUSH(frag(s, list1(&s->out)));
2450 break;
2451
2452 } /* switch(*p) */
2453
2454 } /* for(p = postfix; *p; ++p) */
2455
2456 if (nfa_calc_size == TRUE)
2457 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002458 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002459 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002460 }
2461
2462 e = POP();
2463 if (stackp != stack)
2464 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2465
2466 if (istate >= nstate)
2467 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2468
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 matchstate = &state_ptr[istate++]; /* the match state */
2470 matchstate->c = NFA_MATCH;
2471 matchstate->out = matchstate->out1 = NULL;
2472
2473 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002474 ret = e.start;
2475
2476theend:
2477 vim_free(stack);
2478 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479
2480#undef POP1
2481#undef PUSH1
2482#undef POP2
2483#undef PUSH2
2484#undef POP
2485#undef PUSH
2486}
2487
2488/****************************************************************
2489 * NFA execution code.
2490 ****************************************************************/
2491
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002492typedef struct
2493{
2494 int in_use; /* number of subexpr with useful info */
2495
2496 /* When REG_MULTI is TRUE multilist is used, otherwise linelist. */
2497 union
2498 {
2499 struct multipos
2500 {
2501 lpos_T start;
2502 lpos_T end;
2503 } multilist[NSUBEXP];
2504 struct linepos
2505 {
2506 char_u *start;
2507 char_u *end;
2508 } linelist[NSUBEXP];
2509 };
2510} regsub_T;
2511
Bram Moolenaar963fee22013-05-26 21:47:28 +02002512/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002513typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002514{
2515 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002516 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002517 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002518} nfa_thread_T;
2519
Bram Moolenaar963fee22013-05-26 21:47:28 +02002520/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521typedef struct
2522{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002523 nfa_thread_T *t; /* allocated array of states */
2524 int n; /* nr of states in "t" */
2525 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002526} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002527
Bram Moolenaar5714b802013-05-28 22:03:20 +02002528#ifdef ENABLE_LOG
2529 static void
2530log_subexpr(sub)
2531 regsub_T *sub;
2532{
2533 int j;
2534
2535 for (j = 0; j < sub->in_use; j++)
2536 if (REG_MULTI)
2537 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2538 j,
2539 sub->multilist[j].start.col,
2540 (int)sub->multilist[j].start.lnum,
2541 sub->multilist[j].end.col,
2542 (int)sub->multilist[j].end.lnum);
2543 else
2544 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2545 j,
2546 (char *)sub->linelist[j].start,
2547 (char *)sub->linelist[j].end);
2548 fprintf(log_fd, "\n");
2549}
2550#endif
2551
Bram Moolenaar963fee22013-05-26 21:47:28 +02002552/* Used during execution: whether a match has been found. */
2553static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002554
Bram Moolenaar5714b802013-05-28 22:03:20 +02002555static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2556static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002557
2558 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002559addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002560 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002561 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002562 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002563 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002564{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002565 int subidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002566 nfa_thread_T *lastthread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002567 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002568 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002569 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002570 int i;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002571
2572 if (l == NULL || state == NULL)
2573 return;
2574
2575 switch (state->c)
2576 {
2577 case NFA_SPLIT:
2578 case NFA_NOT:
2579 case NFA_NOPEN:
2580 case NFA_NCLOSE:
2581 case NFA_MCLOSE:
2582 case NFA_MCLOSE + 1:
2583 case NFA_MCLOSE + 2:
2584 case NFA_MCLOSE + 3:
2585 case NFA_MCLOSE + 4:
2586 case NFA_MCLOSE + 5:
2587 case NFA_MCLOSE + 6:
2588 case NFA_MCLOSE + 7:
2589 case NFA_MCLOSE + 8:
2590 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002591 /* These nodes are not added themselves but their "out" and/or
2592 * "out1" may be added below. */
2593 break;
2594
2595 case NFA_MOPEN:
2596 case NFA_MOPEN + 1:
2597 case NFA_MOPEN + 2:
2598 case NFA_MOPEN + 3:
2599 case NFA_MOPEN + 4:
2600 case NFA_MOPEN + 5:
2601 case NFA_MOPEN + 6:
2602 case NFA_MOPEN + 7:
2603 case NFA_MOPEN + 8:
2604 case NFA_MOPEN + 9:
2605 /* These nodes do not need to be added, but we need to bail out
2606 * when it was tried to be added to this list before. */
2607 if (state->lastlist == l->id)
2608 return;
2609 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002610 break;
2611
2612 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002613 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002615 /* This state is already in the list, don't add it again,
2616 * unless it is an MOPEN that is used for a backreference. */
2617 return;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002619
2620 /* add the state to the list */
2621 state->lastlist = l->id;
2622 lastthread = &l->t[l->n++];
2623 lastthread->state = state;
2624 lastthread->sub.in_use = sub->in_use;
2625 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002626 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002627 /* Copy the match start and end positions. */
2628 if (REG_MULTI)
2629 mch_memmove(&lastthread->sub.multilist[0],
2630 &sub->multilist[0],
2631 sizeof(struct multipos) * sub->in_use);
2632 else
2633 mch_memmove(&lastthread->sub.linelist[0],
2634 &sub->linelist[0],
2635 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002636 }
2637 }
2638
2639#ifdef ENABLE_LOG
2640 nfa_set_code(state->c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002641 fprintf(log_fd, "> Adding state %d to list. Character %d: %s\n",
2642 abs(state->id), state->c, code);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643#endif
2644 switch (state->c)
2645 {
2646 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002647 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 break;
2649
2650 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002651 addstate(l, state->out, sub, off);
2652 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002653 break;
2654
2655#if 0
2656 case NFA_END_NEG_RANGE:
2657 /* Nothing to handle here. nfa_regmatch() will take care of it */
2658 break;
2659
2660 case NFA_NOT:
2661 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2662#ifdef ENABLE_LOG
2663 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2664#endif
2665 break;
2666
2667 case NFA_COMPOSING:
2668 /* nfa_regmatch() will match all the bytes of this composing char. */
2669 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670#endif
2671
Bram Moolenaar5714b802013-05-28 22:03:20 +02002672 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002673 case NFA_NOPEN:
2674 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002675 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002676 break;
2677
2678 /* If this state is reached, then a recursive call of nfa_regmatch()
2679 * succeeded. the next call saves the found submatches in the
2680 * first state after the "invisible" branch. */
2681#if 0
2682 case NFA_END_INVISIBLE:
2683 break;
2684#endif
2685
2686 case NFA_MOPEN + 0:
2687 case NFA_MOPEN + 1:
2688 case NFA_MOPEN + 2:
2689 case NFA_MOPEN + 3:
2690 case NFA_MOPEN + 4:
2691 case NFA_MOPEN + 5:
2692 case NFA_MOPEN + 6:
2693 case NFA_MOPEN + 7:
2694 case NFA_MOPEN + 8:
2695 case NFA_MOPEN + 9:
2696 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697 if (state->c == NFA_ZSTART)
2698 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002699 else
2700 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002701
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002702 /* Set the position (with "off") in the subexpression. Save and
2703 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002704 if (REG_MULTI)
2705 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002706 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002707 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002708 save_lpos = sub->multilist[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002709 save_in_use = -1;
2710 }
2711 else
2712 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002713 save_in_use = sub->in_use;
2714 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002715 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002716 sub->multilist[i].start.lnum = -1;
2717 sub->multilist[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002718 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002719 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002720 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002721 if (off == -1)
2722 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002723 sub->multilist[subidx].start.lnum = reglnum + 1;
2724 sub->multilist[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002725 }
2726 else
2727 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002728 sub->multilist[subidx].start.lnum = reglnum;
2729 sub->multilist[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002730 (colnr_T)(reginput - regline + off);
2731 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732 }
2733 else
2734 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002735 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002736 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002737 save_ptr = sub->linelist[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002738 save_in_use = -1;
2739 }
2740 else
2741 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002742 save_in_use = sub->in_use;
2743 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002744 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002745 sub->linelist[i].start = NULL;
2746 sub->linelist[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002747 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002748 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002749 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002750 sub->linelist[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002751 }
2752
Bram Moolenaar5714b802013-05-28 22:03:20 +02002753 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002754
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002755 if (save_in_use == -1)
2756 {
2757 if (REG_MULTI)
Bram Moolenaar5714b802013-05-28 22:03:20 +02002758 sub->multilist[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002759 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002760 sub->linelist[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002761 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002763 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002764 break;
2765
2766 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002767 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002768 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002769 /* Do not overwrite the position set by \ze. If no \ze
2770 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002771 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002772 break;
2773 }
2774 case NFA_MCLOSE + 1:
2775 case NFA_MCLOSE + 2:
2776 case NFA_MCLOSE + 3:
2777 case NFA_MCLOSE + 4:
2778 case NFA_MCLOSE + 5:
2779 case NFA_MCLOSE + 6:
2780 case NFA_MCLOSE + 7:
2781 case NFA_MCLOSE + 8:
2782 case NFA_MCLOSE + 9:
2783 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002784 if (state->c == NFA_ZEND)
2785 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002786 else
2787 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002788
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002789 /* We don't fill in gaps here, there must have been an MOPEN that
2790 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002791 save_in_use = sub->in_use;
2792 if (sub->in_use <= subidx)
2793 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002794 if (REG_MULTI)
2795 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002796 save_lpos = sub->multilist[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002797 if (off == -1)
2798 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002799 sub->multilist[subidx].end.lnum = reglnum + 1;
2800 sub->multilist[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002801 }
2802 else
2803 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002804 sub->multilist[subidx].end.lnum = reglnum;
2805 sub->multilist[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02002806 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002807 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002808 }
2809 else
2810 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002811 save_ptr = sub->linelist[subidx].end;
2812 sub->linelist[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002813 }
2814
Bram Moolenaar5714b802013-05-28 22:03:20 +02002815 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002816
2817 if (REG_MULTI)
Bram Moolenaar5714b802013-05-28 22:03:20 +02002818 sub->multilist[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002819 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002820 sub->linelist[subidx].end = save_ptr;
2821 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 break;
2823 }
2824}
2825
2826/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002827 * Like addstate(), but the new state(s) are put at position "*ip".
2828 * Used for zero-width matches, next state to use is the added one.
2829 * This makes sure the order of states to be tried does not change, which
2830 * matters for alternatives.
2831 */
2832 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002833addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002834 nfa_list_T *l; /* runtime state list */
2835 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002836 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002837 int *ip;
2838{
2839 int tlen = l->n;
2840 int count;
2841 int i = *ip;
2842
2843 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002844 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02002845
2846 /* when "*ip" was at the end of the list, nothing to do */
2847 if (i + 1 == tlen)
2848 return;
2849
2850 /* re-order to put the new state at the current position */
2851 count = l->n - tlen;
2852 if (count > 1)
2853 {
2854 /* make space for new states, then move them from the
2855 * end to the current position */
2856 mch_memmove(&(l->t[i + count]),
2857 &(l->t[i + 1]),
2858 sizeof(nfa_thread_T) * (l->n - i - 1));
2859 mch_memmove(&(l->t[i]),
2860 &(l->t[l->n - 1]),
2861 sizeof(nfa_thread_T) * count);
2862 }
2863 else
2864 {
2865 /* overwrite the current state */
2866 l->t[i] = l->t[l->n - 1];
2867 }
2868 --l->n;
2869 *ip = i - 1;
2870}
2871
2872/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873 * Check character class "class" against current character c.
2874 */
2875 static int
2876check_char_class(class, c)
2877 int class;
2878 int c;
2879{
2880 switch (class)
2881 {
2882 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002883 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884 return OK;
2885 break;
2886 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002887 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888 return OK;
2889 break;
2890 case NFA_CLASS_BLANK:
2891 if (c == ' ' || c == '\t')
2892 return OK;
2893 break;
2894 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002895 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 return OK;
2897 break;
2898 case NFA_CLASS_DIGIT:
2899 if (VIM_ISDIGIT(c))
2900 return OK;
2901 break;
2902 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002903 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904 return OK;
2905 break;
2906 case NFA_CLASS_LOWER:
2907 if (MB_ISLOWER(c))
2908 return OK;
2909 break;
2910 case NFA_CLASS_PRINT:
2911 if (vim_isprintc(c))
2912 return OK;
2913 break;
2914 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002915 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002916 return OK;
2917 break;
2918 case NFA_CLASS_SPACE:
2919 if ((c >=9 && c <= 13) || (c == ' '))
2920 return OK;
2921 break;
2922 case NFA_CLASS_UPPER:
2923 if (MB_ISUPPER(c))
2924 return OK;
2925 break;
2926 case NFA_CLASS_XDIGIT:
2927 if (vim_isxdigit(c))
2928 return OK;
2929 break;
2930 case NFA_CLASS_TAB:
2931 if (c == '\t')
2932 return OK;
2933 break;
2934 case NFA_CLASS_RETURN:
2935 if (c == '\r')
2936 return OK;
2937 break;
2938 case NFA_CLASS_BACKSPACE:
2939 if (c == '\b')
2940 return OK;
2941 break;
2942 case NFA_CLASS_ESCAPE:
2943 if (c == '\033')
2944 return OK;
2945 break;
2946
2947 default:
2948 /* should not be here :P */
2949 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2950 }
2951 return FAIL;
2952}
2953
Bram Moolenaar5714b802013-05-28 22:03:20 +02002954static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
2955
2956/*
2957 * Check for a match with subexpression "subidx".
2958 * return TRUE if it matches.
2959 */
2960 static int
2961match_backref(sub, subidx, bytelen)
2962 regsub_T *sub; /* pointers to subexpressions */
2963 int subidx;
2964 int *bytelen; /* out: length of match in bytes */
2965{
2966 int len;
2967
2968 if (sub->in_use <= subidx)
2969 {
2970retempty:
2971 /* backref was not set, match an empty string */
2972 *bytelen = 0;
2973 return TRUE;
2974 }
2975
2976 if (REG_MULTI)
2977 {
2978 if (sub->multilist[subidx].start.lnum < 0
2979 || sub->multilist[subidx].end.lnum < 0)
2980 goto retempty;
2981 /* TODO: line breaks */
2982 len = sub->multilist[subidx].end.col
2983 - sub->multilist[subidx].start.col;
2984 if (cstrncmp(regline + sub->multilist[subidx].start.col,
2985 reginput, &len) == 0)
2986 {
2987 *bytelen = len;
2988 return TRUE;
2989 }
2990 }
2991 else
2992 {
2993 if (sub->linelist[subidx].start == NULL
2994 || sub->linelist[subidx].end == NULL)
2995 goto retempty;
2996 len = (int)(sub->linelist[subidx].end - sub->linelist[subidx].start);
2997 if (cstrncmp(sub->linelist[subidx].start, reginput, &len) == 0)
2998 {
2999 *bytelen = len;
3000 return TRUE;
3001 }
3002 }
3003 return FALSE;
3004}
3005
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003006/*
3007 * Set all NFA nodes' list ID equal to -1.
3008 */
3009 static void
3010nfa_set_neg_listids(start)
3011 nfa_state_T *start;
3012{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003013 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014 {
3015 start->lastlist = -1;
3016 nfa_set_neg_listids(start->out);
3017 nfa_set_neg_listids(start->out1);
3018 }
3019}
3020
3021/*
3022 * Set all NFA nodes' list ID equal to 0.
3023 */
3024 static void
3025nfa_set_null_listids(start)
3026 nfa_state_T *start;
3027{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003028 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003029 {
3030 start->lastlist = 0;
3031 nfa_set_null_listids(start->out);
3032 nfa_set_null_listids(start->out1);
3033 }
3034}
3035
3036/*
3037 * Save list IDs for all NFA states in "list".
3038 */
3039 static void
3040nfa_save_listids(start, list)
3041 nfa_state_T *start;
3042 int *list;
3043{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003044 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045 {
3046 list[abs(start->id)] = start->lastlist;
3047 start->lastlist = -1;
3048 nfa_save_listids(start->out, list);
3049 nfa_save_listids(start->out1, list);
3050 }
3051}
3052
3053/*
3054 * Restore list IDs from "list" to all NFA states.
3055 */
3056 static void
3057nfa_restore_listids(start, list)
3058 nfa_state_T *start;
3059 int *list;
3060{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003061 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003062 {
3063 start->lastlist = list[abs(start->id)];
3064 nfa_restore_listids(start->out, list);
3065 nfa_restore_listids(start->out1, list);
3066 }
3067}
3068
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003069static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3070
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003071/*
3072 * Main matching routine.
3073 *
3074 * Run NFA to determine whether it matches reginput.
3075 *
3076 * Return TRUE if there is a match, FALSE otherwise.
3077 * Note: Caller must ensure that: start != NULL.
3078 */
3079 static int
3080nfa_regmatch(start, submatch, m)
3081 nfa_state_T *start;
3082 regsub_T *submatch;
3083 regsub_T *m;
3084{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003085 int result;
3086 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003087 int flag = 0;
3088 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003089 int go_to_nextline = FALSE;
3090 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003091 char_u *old_reginput = NULL;
3092 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003093 nfa_list_T list[3];
3094 nfa_list_T *listtbl[2][2];
3095 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003096 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003097 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003098 nfa_list_T *thislist;
3099 nfa_list_T *nextlist;
3100 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003101 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003102#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003103 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003104
3105 if (debug == NULL)
3106 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003107 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003108 return FALSE;
3109 }
3110#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003111 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003112
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003113 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003114 size = (nstate + 1) * sizeof(nfa_thread_T);
3115 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
3116 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
3117 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3119 goto theend;
3120 vim_memset(list[0].t, 0, size);
3121 vim_memset(list[1].t, 0, size);
3122 vim_memset(list[2].t, 0, size);
3123
3124#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003125 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003126 if (log_fd != NULL)
3127 {
3128 fprintf(log_fd, "**********************************\n");
3129 nfa_set_code(start->c);
3130 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3131 abs(start->id), code);
3132 fprintf(log_fd, "**********************************\n");
3133 }
3134 else
3135 {
3136 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3137 log_fd = stderr;
3138 }
3139#endif
3140
3141 thislist = &list[0];
3142 thislist->n = 0;
3143 nextlist = &list[1];
3144 nextlist->n = 0;
3145 neglist = &list[2];
3146 neglist->n = 0;
3147#ifdef ENABLE_LOG
3148 fprintf(log_fd, "(---) STARTSTATE\n");
3149#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003150 thislist->id = listid;
3151 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003152
3153 /* There are two cases when the NFA advances: 1. input char matches the
3154 * NFA node and 2. input char does not match the NFA node, but the next
3155 * node is NFA_NOT. The following macro calls addstate() according to
3156 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3157 listtbl[0][0] = NULL;
3158 listtbl[0][1] = neglist;
3159 listtbl[1][0] = nextlist;
3160 listtbl[1][1] = NULL;
3161#define ADD_POS_NEG_STATE(node) \
3162 ll = listtbl[result ? 1 : 0][node->negated]; \
3163 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003164 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003165
3166
3167 /*
3168 * Run for each character.
3169 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003170 for (;;)
3171 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003172 int curc;
3173 int clen;
3174
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003175#ifdef FEAT_MBYTE
3176 if (has_mbyte)
3177 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003178 curc = (*mb_ptr2char)(reginput);
3179 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180 }
3181 else
3182#endif
3183 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003184 curc = *reginput;
3185 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003186 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003187 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003188 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003189 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003190 go_to_nextline = FALSE;
3191 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003192
3193 /* swap lists */
3194 thislist = &list[flag];
3195 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003196 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003197 listtbl[1][0] = nextlist;
3198 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003199 thislist->id = listid;
3200 nextlist->id = listid + 1;
3201 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202
3203#ifdef ENABLE_LOG
3204 fprintf(log_fd, "------------------------------------------\n");
3205 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003206 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003208 {
3209 int i;
3210
3211 for (i = 0; i < thislist->n; i++)
3212 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3213 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003214 fprintf(log_fd, "\n");
3215#endif
3216
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003217#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 fprintf(debug, "\n-------------------\n");
3219#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003220 /*
3221 * If the state lists are empty we can stop.
3222 */
3223 if (thislist->n == 0 && neglist->n == 0)
3224 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003225
3226 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003227 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003228 {
3229 if (neglist->n > 0)
3230 {
3231 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003232 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003233 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 }
3235 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003236 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003238#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239 nfa_set_code(t->state->c);
3240 fprintf(debug, "%s, ", code);
3241#endif
3242#ifdef ENABLE_LOG
3243 nfa_set_code(t->state->c);
3244 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3245 code, (int)t->state->c);
3246#endif
3247
3248 /*
3249 * Handle the possible codes of the current state.
3250 * The most important is NFA_MATCH.
3251 */
3252 switch (t->state->c)
3253 {
3254 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003255 {
3256 int j;
3257
Bram Moolenaar963fee22013-05-26 21:47:28 +02003258 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003259 submatch->in_use = t->sub.in_use;
3260 if (REG_MULTI)
3261 for (j = 0; j < submatch->in_use; j++)
3262 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003263 submatch->multilist[j].start =
3264 t->sub.multilist[j].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003265 submatch->multilist[j].end = t->sub.multilist[j].end;
3266 }
3267 else
3268 for (j = 0; j < submatch->in_use; j++)
3269 {
3270 submatch->linelist[j].start = t->sub.linelist[j].start;
3271 submatch->linelist[j].end = t->sub.linelist[j].end;
3272 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003274 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003276 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003277 * states at this position. When the list of states is going
3278 * to be empty quit without advancing, so that "reginput" is
3279 * correct. */
3280 if (nextlist->n == 0 && neglist->n == 0)
3281 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003282 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003283 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284
3285 case NFA_END_INVISIBLE:
3286 /* This is only encountered after a NFA_START_INVISIBLE node.
3287 * They surround a zero-width group, used with "\@=" and "\&".
3288 * If we got here, it means that the current "invisible" group
3289 * finished successfully, so return control to the parent
3290 * nfa_regmatch(). Submatches are stored in *m, and used in
3291 * the parent call. */
3292 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003293 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 else
3295 {
3296 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003297 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 }
3299 break;
3300
3301 case NFA_START_INVISIBLE:
3302 /* Save global variables, and call nfa_regmatch() to check if
3303 * the current concat matches at this position. The concat
3304 * ends with the node NFA_END_INVISIBLE */
3305 old_reginput = reginput;
3306 old_regline = regline;
3307 old_reglnum = reglnum;
3308 if (listids == NULL)
3309 {
3310 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3311 if (listids == NULL)
3312 {
3313 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3314 return 0;
3315 }
3316 }
3317#ifdef ENABLE_LOG
3318 if (log_fd != stderr)
3319 fclose(log_fd);
3320 log_fd = NULL;
3321#endif
3322 /* Have to clear the listid field of the NFA nodes, so that
3323 * nfa_regmatch() and addstate() can run properly after
3324 * recursion. */
3325 nfa_save_listids(start, listids);
3326 nfa_set_null_listids(start);
3327 result = nfa_regmatch(t->state->out, submatch, m);
3328 nfa_set_neg_listids(start);
3329 nfa_restore_listids(start, listids);
3330
3331#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003332 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 if (log_fd != NULL)
3334 {
3335 fprintf(log_fd, "****************************\n");
3336 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3337 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3338 fprintf(log_fd, "****************************\n");
3339 }
3340 else
3341 {
3342 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3343 log_fd = stderr;
3344 }
3345#endif
3346 if (result == TRUE)
3347 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003348 int j;
3349
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350 /* Restore position in input text */
3351 reginput = old_reginput;
3352 regline = old_regline;
3353 reglnum = old_reglnum;
3354 /* Copy submatch info from the recursive call */
3355 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003356 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003358 t->sub.multilist[j].start = m->multilist[j].start;
3359 t->sub.multilist[j].end = m->multilist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 }
3361 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003362 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003364 t->sub.linelist[j].start = m->linelist[j].start;
3365 t->sub.linelist[j].end = m->linelist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003366 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003367 t->sub.in_use = m->in_use;
3368
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003370 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003371 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372 }
3373 else
3374 {
3375 /* continue with next input char */
3376 reginput = old_reginput;
3377 }
3378 break;
3379
3380 case NFA_BOL:
3381 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003382 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003383 break;
3384
3385 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003386 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003387 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003388 break;
3389
3390 case NFA_BOW:
3391 {
3392 int bow = TRUE;
3393
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003394 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003395 bow = FALSE;
3396#ifdef FEAT_MBYTE
3397 else if (has_mbyte)
3398 {
3399 int this_class;
3400
3401 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003402 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003403 if (this_class <= 1)
3404 bow = FALSE;
3405 else if (reg_prev_class() == this_class)
3406 bow = FALSE;
3407 }
3408#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003409 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003410 || (reginput > regline
3411 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 bow = FALSE;
3413 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003414 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003415 break;
3416 }
3417
3418 case NFA_EOW:
3419 {
3420 int eow = TRUE;
3421
3422 if (reginput == regline)
3423 eow = FALSE;
3424#ifdef FEAT_MBYTE
3425 else if (has_mbyte)
3426 {
3427 int this_class, prev_class;
3428
3429 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003430 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003431 prev_class = reg_prev_class();
3432 if (this_class == prev_class
3433 || prev_class == 0 || prev_class == 1)
3434 eow = FALSE;
3435 }
3436#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003437 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003438 || (reginput[0] != NUL
3439 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003440 eow = FALSE;
3441 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003442 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003443 break;
3444 }
3445
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003446#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003447 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003448 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003449 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003450 int len = 0;
3451 nfa_state_T *end;
3452 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003453 int cchars[MAX_MCO];
3454 int ccount = 0;
3455 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003456
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003457 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003458 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003459 if (utf_iscomposing(sta->c))
3460 {
3461 /* Only match composing character(s), ignore base
3462 * character. Used for ".{composing}" and "{composing}"
3463 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003464 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003465 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003466 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003467 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003468 /* If \Z was present, then ignore composing characters.
3469 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003470 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003471 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003472 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003473 else
3474 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003475 while (sta->c != NFA_END_COMPOSING)
3476 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003477 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003478
3479 /* Check base character matches first, unless ignored. */
3480 else if (len > 0 || mc == sta->c)
3481 {
3482 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003483 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003484 len += mb_char2len(mc);
3485 sta = sta->out;
3486 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003488 /* We don't care about the order of composing characters.
3489 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003490 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003491 {
3492 mc = mb_ptr2char(reginput + len);
3493 cchars[ccount++] = mc;
3494 len += mb_char2len(mc);
3495 if (ccount == MAX_MCO)
3496 break;
3497 }
3498
3499 /* Check that each composing char in the pattern matches a
3500 * composing char in the text. We do not check if all
3501 * composing chars are matched. */
3502 result = OK;
3503 while (sta->c != NFA_END_COMPOSING)
3504 {
3505 for (j = 0; j < ccount; ++j)
3506 if (cchars[j] == sta->c)
3507 break;
3508 if (j == ccount)
3509 {
3510 result = FAIL;
3511 break;
3512 }
3513 sta = sta->out;
3514 }
3515 }
3516 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003517 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003518
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003519 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 ADD_POS_NEG_STATE(end);
3521 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003522 }
3523#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524
3525 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003526 if (curc == NUL && !reg_line_lbr && REG_MULTI
3527 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003528 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003529 go_to_nextline = TRUE;
3530 /* Pass -1 for the offset, which means taking the position
3531 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003532 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003533 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003534 else if (curc == '\n' && reg_line_lbr)
3535 {
3536 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003537 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003538 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003539 break;
3540
3541 case NFA_CLASS_ALNUM:
3542 case NFA_CLASS_ALPHA:
3543 case NFA_CLASS_BLANK:
3544 case NFA_CLASS_CNTRL:
3545 case NFA_CLASS_DIGIT:
3546 case NFA_CLASS_GRAPH:
3547 case NFA_CLASS_LOWER:
3548 case NFA_CLASS_PRINT:
3549 case NFA_CLASS_PUNCT:
3550 case NFA_CLASS_SPACE:
3551 case NFA_CLASS_UPPER:
3552 case NFA_CLASS_XDIGIT:
3553 case NFA_CLASS_TAB:
3554 case NFA_CLASS_RETURN:
3555 case NFA_CLASS_BACKSPACE:
3556 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003557 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003558 ADD_POS_NEG_STATE(t->state);
3559 break;
3560
3561 case NFA_END_NEG_RANGE:
3562 /* This follows a series of negated nodes, like:
3563 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003564 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003565 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003566 break;
3567
3568 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003569 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003570 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003571 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003572 break;
3573
3574 /*
3575 * Character classes like \a for alpha, \d for digit etc.
3576 */
3577 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003578 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003579 ADD_POS_NEG_STATE(t->state);
3580 break;
3581
3582 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003583 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003584 ADD_POS_NEG_STATE(t->state);
3585 break;
3586
3587 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003588 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003589 ADD_POS_NEG_STATE(t->state);
3590 break;
3591
3592 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003593 result = !VIM_ISDIGIT(curc)
3594 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595 ADD_POS_NEG_STATE(t->state);
3596 break;
3597
3598 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003599 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 ADD_POS_NEG_STATE(t->state);
3601 break;
3602
3603 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003604 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 ADD_POS_NEG_STATE(t->state);
3606 break;
3607
3608 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003609 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 ADD_POS_NEG_STATE(t->state);
3611 break;
3612
3613 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003614 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615 ADD_POS_NEG_STATE(t->state);
3616 break;
3617
3618 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003619 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003620 ADD_POS_NEG_STATE(t->state);
3621 break;
3622
3623 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003624 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003625 ADD_POS_NEG_STATE(t->state);
3626 break;
3627
3628 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003629 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 ADD_POS_NEG_STATE(t->state);
3631 break;
3632
3633 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003634 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003635 ADD_POS_NEG_STATE(t->state);
3636 break;
3637
3638 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003639 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640 ADD_POS_NEG_STATE(t->state);
3641 break;
3642
3643 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003644 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003645 ADD_POS_NEG_STATE(t->state);
3646 break;
3647
3648 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003649 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003650 ADD_POS_NEG_STATE(t->state);
3651 break;
3652
3653 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003654 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655 ADD_POS_NEG_STATE(t->state);
3656 break;
3657
3658 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003659 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660 ADD_POS_NEG_STATE(t->state);
3661 break;
3662
3663 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003664 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003665 ADD_POS_NEG_STATE(t->state);
3666 break;
3667
3668 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003669 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003670 ADD_POS_NEG_STATE(t->state);
3671 break;
3672
3673 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003674 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003675 ADD_POS_NEG_STATE(t->state);
3676 break;
3677
3678 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003679 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003680 ADD_POS_NEG_STATE(t->state);
3681 break;
3682
3683 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003684 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685 ADD_POS_NEG_STATE(t->state);
3686 break;
3687
3688 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003689 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003690 ADD_POS_NEG_STATE(t->state);
3691 break;
3692
3693 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003694 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003695 ADD_POS_NEG_STATE(t->state);
3696 break;
3697
3698 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003699 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 ADD_POS_NEG_STATE(t->state);
3701 break;
3702
3703 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003704 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 ADD_POS_NEG_STATE(t->state);
3706 break;
3707
Bram Moolenaar5714b802013-05-28 22:03:20 +02003708 case NFA_BACKREF1:
3709 case NFA_BACKREF2:
3710 case NFA_BACKREF3:
3711 case NFA_BACKREF4:
3712 case NFA_BACKREF5:
3713 case NFA_BACKREF6:
3714 case NFA_BACKREF7:
3715 case NFA_BACKREF8:
3716 case NFA_BACKREF9:
3717 /* \1 .. \9 */
3718 {
3719 int subidx = t->state->c - NFA_BACKREF1 + 1;
3720 int bytelen;
3721
3722 result = match_backref(&t->sub, subidx, &bytelen);
3723 if (result)
3724 {
3725 if (bytelen == 0)
3726 {
3727 /* empty match always works, add NFA_SKIP with zero to
3728 * be used next */
3729 addstate_here(thislist, t->state->out, &t->sub,
3730 &listidx);
3731 thislist->t[listidx + 1].count = 0;
3732 }
3733 else if (bytelen <= clen)
3734 {
3735 /* match current character, jump ahead to out of
3736 * NFA_SKIP */
3737 addstate(nextlist, t->state->out->out, &t->sub, clen);
3738#ifdef ENABLE_LOG
3739 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3740#endif
3741 }
3742 else
3743 {
3744 /* skip ofer the matched characters, set character
3745 * count in NFA_SKIP */
3746 addstate(nextlist, t->state->out, &t->sub, bytelen);
3747 nextlist->t[nextlist->n - 1].count = bytelen - clen;
3748#ifdef ENABLE_LOG
3749 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3750#endif
3751 }
3752
3753 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02003754 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003755 }
3756 case NFA_SKIP:
3757 /* charater of previous matching \1 .. \9 */
3758 if (t->count - clen <= 0)
3759 {
3760 /* end of match, go to what follows */
3761 addstate(nextlist, t->state->out, &t->sub, clen);
3762#ifdef ENABLE_LOG
3763 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3764#endif
3765 }
3766 else
3767 {
3768 /* add state again with decremented count */
3769 addstate(nextlist, t->state, &t->sub, 0);
3770 nextlist->t[nextlist->n - 1].count = t->count - clen;
3771#ifdef ENABLE_LOG
3772 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3773#endif
3774 }
3775 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003776
3777 case NFA_SKIP_CHAR:
3778 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003779 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02003780 /* TODO: should not happen? */
3781 break;
3782
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003783 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003784 {
3785 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003786
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003787 /* TODO: put this in #ifdef later */
3788 if (c < -256)
3789 EMSGN("INTERNAL: Negative state char: %ld", c);
3790 if (is_Magic(c))
3791 c = un_Magic(c);
3792 result = (c == curc);
3793
3794 if (!result && ireg_ic)
3795 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003796#ifdef FEAT_MBYTE
3797 /* If there is a composing character which is not being
3798 * ignored there can be no match. Match with composing
3799 * character uses NFA_COMPOSING above. */
3800 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003801 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003802 result = FALSE;
3803#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003804 ADD_POS_NEG_STATE(t->state);
3805 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003806 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807 }
3808
3809 } /* for (thislist = thislist; thislist->state; thislist++) */
3810
Bram Moolenaare23febd2013-05-26 18:40:14 +02003811 /* Look for the start of a match in the current position by adding the
3812 * start state to the list of states.
3813 * The first found match is the leftmost one, thus the order of states
3814 * matters!
3815 * Do not add the start state in recursive calls of nfa_regmatch(),
3816 * because recursive calls should only start in the first position.
3817 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003818 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaare23febd2013-05-26 18:40:14 +02003819 && reglnum == 0 && clen != 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003820 {
3821#ifdef ENABLE_LOG
3822 fprintf(log_fd, "(---) STARTSTATE\n");
3823#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003824 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003825 }
3826
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003827#ifdef ENABLE_LOG
3828 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003829 {
3830 int i;
3831
3832 for (i = 0; i < thislist->n; i++)
3833 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3834 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003835 fprintf(log_fd, "\n");
3836#endif
3837
3838nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003839 /* Advance to the next character, or advance to the next line, or
3840 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003841 if (clen != 0)
3842 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003843 else if (go_to_nextline)
3844 reg_nextline();
3845 else
3846 break;
3847 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003848
3849#ifdef ENABLE_LOG
3850 if (log_fd != stderr)
3851 fclose(log_fd);
3852 log_fd = NULL;
3853#endif
3854
3855theend:
3856 /* Free memory */
3857 vim_free(list[0].t);
3858 vim_free(list[1].t);
3859 vim_free(list[2].t);
3860 list[0].t = list[1].t = list[2].t = NULL;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003861 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003862#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003863#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003864 fclose(debug);
3865#endif
3866
Bram Moolenaar963fee22013-05-26 21:47:28 +02003867 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003868}
3869
3870/*
3871 * Try match of "prog" with at regline["col"].
3872 * Returns 0 for failure, number of lines contained in the match otherwise.
3873 */
3874 static long
3875nfa_regtry(start, col)
3876 nfa_state_T *start;
3877 colnr_T col;
3878{
3879 int i;
3880 regsub_T sub, m;
3881#ifdef ENABLE_LOG
3882 FILE *f;
3883#endif
3884
3885 reginput = regline + col;
3886 need_clear_subexpr = TRUE;
3887
3888#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003889 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003890 if (f != NULL)
3891 {
3892 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3893 fprintf(f, " =======================================================\n");
3894#ifdef DEBUG
3895 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3896#endif
3897 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3898 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003899 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900 fprintf(f, "\n\n");
3901 fclose(f);
3902 }
3903 else
3904 EMSG(_("Could not open temporary log file for writing "));
3905#endif
3906
3907 if (REG_MULTI)
3908 {
3909 /* Use 0xff to set lnum to -1 */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003910 vim_memset(sub.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
3911 vim_memset(m.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003912 }
3913 else
3914 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003915 vim_memset(sub.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
3916 vim_memset(m.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003918 sub.in_use = 0;
3919 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003920
3921 if (nfa_regmatch(start, &sub, &m) == FALSE)
3922 return 0;
3923
3924 cleanup_subexpr();
3925 if (REG_MULTI)
3926 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003927 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003928 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003929 reg_startpos[i] = sub.multilist[i].start;
3930 reg_endpos[i] = sub.multilist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003931 }
3932
3933 if (reg_startpos[0].lnum < 0)
3934 {
3935 reg_startpos[0].lnum = 0;
3936 reg_startpos[0].col = col;
3937 }
3938 if (reg_endpos[0].lnum < 0)
3939 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003940 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003941 reg_endpos[0].lnum = reglnum;
3942 reg_endpos[0].col = (int)(reginput - regline);
3943 }
3944 else
3945 /* Use line number of "\ze". */
3946 reglnum = reg_endpos[0].lnum;
3947 }
3948 else
3949 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003950 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003951 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003952 reg_startp[i] = sub.linelist[i].start;
3953 reg_endp[i] = sub.linelist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003954 }
3955
3956 if (reg_startp[0] == NULL)
3957 reg_startp[0] = regline + col;
3958 if (reg_endp[0] == NULL)
3959 reg_endp[0] = reginput;
3960 }
3961
3962 return 1 + reglnum;
3963}
3964
3965/*
3966 * Match a regexp against a string ("line" points to the string) or multiple
3967 * lines ("line" is NULL, use reg_getline()).
3968 *
3969 * Returns 0 for failure, number of lines contained in the match otherwise.
3970 */
3971 static long
3972nfa_regexec_both(line, col)
3973 char_u *line;
3974 colnr_T col; /* column to start looking for match */
3975{
3976 nfa_regprog_T *prog;
3977 long retval = 0L;
3978 int i;
3979
3980 if (REG_MULTI)
3981 {
3982 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3983 line = reg_getline((linenr_T)0); /* relative to the cursor */
3984 reg_startpos = reg_mmatch->startpos;
3985 reg_endpos = reg_mmatch->endpos;
3986 }
3987 else
3988 {
3989 prog = (nfa_regprog_T *)reg_match->regprog;
3990 reg_startp = reg_match->startp;
3991 reg_endp = reg_match->endp;
3992 }
3993
3994 /* Be paranoid... */
3995 if (prog == NULL || line == NULL)
3996 {
3997 EMSG(_(e_null));
3998 goto theend;
3999 }
4000
4001 /* If the start column is past the maximum column: no need to try. */
4002 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4003 goto theend;
4004
4005 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4006 if (prog->regflags & RF_ICASE)
4007 ireg_ic = TRUE;
4008 else if (prog->regflags & RF_NOICASE)
4009 ireg_ic = FALSE;
4010
4011#ifdef FEAT_MBYTE
4012 /* If pattern contains "\Z" overrule value of ireg_icombine */
4013 if (prog->regflags & RF_ICOMBINE)
4014 ireg_icombine = TRUE;
4015#endif
4016
4017 regline = line;
4018 reglnum = 0; /* relative to line */
4019
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004020 nfa_has_zend = prog->has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004021 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004022
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004023 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004024 for (i = 0; i < nstate; ++i)
4025 {
4026 prog->state[i].id = i;
4027 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004028 }
4029
4030 retval = nfa_regtry(prog->start, col);
4031
4032theend:
4033 return retval;
4034}
4035
4036/*
4037 * Compile a regular expression into internal code for the NFA matcher.
4038 * Returns the program in allocated space. Returns NULL for an error.
4039 */
4040 static regprog_T *
4041nfa_regcomp(expr, re_flags)
4042 char_u *expr;
4043 int re_flags;
4044{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004045 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004046 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004047 int *postfix;
4048
4049 if (expr == NULL)
4050 return NULL;
4051
4052#ifdef DEBUG
4053 nfa_regengine.expr = expr;
4054#endif
4055
4056 init_class_tab();
4057
4058 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4059 return NULL;
4060
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004061 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004062 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004063 postfix = re2post();
4064 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004065 {
4066 /* TODO: only give this error for debugging? */
4067 if (post_ptr >= post_end)
4068 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004069 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004070 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004071
4072 /*
4073 * In order to build the NFA, we parse the input regexp twice:
4074 * 1. first pass to count size (so we can allocate space)
4075 * 2. second to emit code
4076 */
4077#ifdef ENABLE_LOG
4078 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004079 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004080
4081 if (f != NULL)
4082 {
4083 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4084 fclose(f);
4085 }
4086 }
4087#endif
4088
4089 /*
4090 * PASS 1
4091 * Count number of NFA states in "nstate". Do not build the NFA.
4092 */
4093 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004094
4095 /* Space for compiled regexp */
4096 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4097 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4098 if (prog == NULL)
4099 goto fail;
4100 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004101 state_ptr = prog->state;
4102
4103 /*
4104 * PASS 2
4105 * Build the NFA
4106 */
4107 prog->start = post2nfa(postfix, post_ptr, FALSE);
4108 if (prog->start == NULL)
4109 goto fail;
4110
4111 prog->regflags = regflags;
4112 prog->engine = &nfa_regengine;
4113 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004114 prog->has_zend = nfa_has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004115 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004116#ifdef ENABLE_LOG
4117 nfa_postfix_dump(expr, OK);
4118 nfa_dump(prog);
4119#endif
4120
4121out:
4122 vim_free(post_start);
4123 post_start = post_ptr = post_end = NULL;
4124 state_ptr = NULL;
4125 return (regprog_T *)prog;
4126
4127fail:
4128 vim_free(prog);
4129 prog = NULL;
4130#ifdef ENABLE_LOG
4131 nfa_postfix_dump(expr, FAIL);
4132#endif
4133#ifdef DEBUG
4134 nfa_regengine.expr = NULL;
4135#endif
4136 goto out;
4137}
4138
4139
4140/*
4141 * Match a regexp against a string.
4142 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4143 * Uses curbuf for line count and 'iskeyword'.
4144 *
4145 * Return TRUE if there is a match, FALSE if not.
4146 */
4147 static int
4148nfa_regexec(rmp, line, col)
4149 regmatch_T *rmp;
4150 char_u *line; /* string to match against */
4151 colnr_T col; /* column to start looking for match */
4152{
4153 reg_match = rmp;
4154 reg_mmatch = NULL;
4155 reg_maxline = 0;
4156 reg_line_lbr = FALSE;
4157 reg_buf = curbuf;
4158 reg_win = NULL;
4159 ireg_ic = rmp->rm_ic;
4160#ifdef FEAT_MBYTE
4161 ireg_icombine = FALSE;
4162#endif
4163 ireg_maxcol = 0;
4164 return (nfa_regexec_both(line, col) != 0);
4165}
4166
4167#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4168 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4169
4170static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4171
4172/*
4173 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4174 */
4175 static int
4176nfa_regexec_nl(rmp, line, col)
4177 regmatch_T *rmp;
4178 char_u *line; /* string to match against */
4179 colnr_T col; /* column to start looking for match */
4180{
4181 reg_match = rmp;
4182 reg_mmatch = NULL;
4183 reg_maxline = 0;
4184 reg_line_lbr = TRUE;
4185 reg_buf = curbuf;
4186 reg_win = NULL;
4187 ireg_ic = rmp->rm_ic;
4188#ifdef FEAT_MBYTE
4189 ireg_icombine = FALSE;
4190#endif
4191 ireg_maxcol = 0;
4192 return (nfa_regexec_both(line, col) != 0);
4193}
4194#endif
4195
4196
4197/*
4198 * Match a regexp against multiple lines.
4199 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4200 * Uses curbuf for line count and 'iskeyword'.
4201 *
4202 * Return zero if there is no match. Return number of lines contained in the
4203 * match otherwise.
4204 *
4205 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4206 *
4207 * ! Also NOTE : match may actually be in another line. e.g.:
4208 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4209 *
4210 * +-------------------------+
4211 * |a |
4212 * |b |
4213 * |c |
4214 * | |
4215 * +-------------------------+
4216 *
4217 * then nfa_regexec_multi() returns 3. while the original
4218 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4219 *
4220 * FIXME if this behavior is not compatible.
4221 */
4222 static long
4223nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4224 regmmatch_T *rmp;
4225 win_T *win; /* window in which to search or NULL */
4226 buf_T *buf; /* buffer in which to search */
4227 linenr_T lnum; /* nr of line to start looking for match */
4228 colnr_T col; /* column to start looking for match */
4229 proftime_T *tm UNUSED; /* timeout limit or NULL */
4230{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004231 reg_match = NULL;
4232 reg_mmatch = rmp;
4233 reg_buf = buf;
4234 reg_win = win;
4235 reg_firstlnum = lnum;
4236 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4237 reg_line_lbr = FALSE;
4238 ireg_ic = rmp->rmm_ic;
4239#ifdef FEAT_MBYTE
4240 ireg_icombine = FALSE;
4241#endif
4242 ireg_maxcol = rmp->rmm_maxcol;
4243
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004244 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004245}
4246
4247#ifdef DEBUG
4248# undef ENABLE_LOG
4249#endif