blob: aa6c56e6aa1dfb72e515c5a88cc82d299954bf20 [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 */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200120
121 NFA_CURSOR, /* Match cursor pos */
122 NFA_LNUM, /* Match line number */
123 NFA_LNUM_GT, /* Match > line number */
124 NFA_LNUM_LT, /* Match < line number */
125 NFA_COL, /* Match cursor column */
126 NFA_COL_GT, /* Match > cursor column */
127 NFA_COL_LT, /* Match < cursor column */
128 NFA_VCOL, /* Match cursor virtual column */
129 NFA_VCOL_GT, /* Match > cursor virtual column */
130 NFA_VCOL_LT, /* Match < cursor virtual column */
131
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200132 NFA_FIRST_NL = NFA_ANY + ADD_NL,
133 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
134
135 /* Character classes [:alnum:] etc */
136 NFA_CLASS_ALNUM,
137 NFA_CLASS_ALPHA,
138 NFA_CLASS_BLANK,
139 NFA_CLASS_CNTRL,
140 NFA_CLASS_DIGIT,
141 NFA_CLASS_GRAPH,
142 NFA_CLASS_LOWER,
143 NFA_CLASS_PRINT,
144 NFA_CLASS_PUNCT,
145 NFA_CLASS_SPACE,
146 NFA_CLASS_UPPER,
147 NFA_CLASS_XDIGIT,
148 NFA_CLASS_TAB,
149 NFA_CLASS_RETURN,
150 NFA_CLASS_BACKSPACE,
151 NFA_CLASS_ESCAPE
152};
153
154/* Keep in sync with classchars. */
155static int nfa_classcodes[] = {
156 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
157 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
158 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
159 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
160 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
161 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
162 NFA_UPPER, NFA_NUPPER
163};
164
165static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
166
167/*
168 * NFA errors can be of 3 types:
169 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
170 * silently and revert the to backtracking engine.
171 * syntax_error = FALSE;
172 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
173 * The NFA engine displays an error message, and nothing else happens.
174 * syntax_error = TRUE
175 * *** Unsupported features, when the input regexp uses an operator that is not
176 * implemented in the NFA. The NFA engine fails silently, and reverts to the
177 * old backtracking engine.
178 * syntax_error = FALSE
179 * "The NFA fails" means that "compiling the regexp with the NFA fails":
180 * nfa_regcomp() returns FAIL.
181 */
182static int syntax_error = FALSE;
183
184/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200185static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200186
Bram Moolenaar963fee22013-05-26 21:47:28 +0200187/* Number of sub expressions actually being used during execution. 1 if only
188 * the whole match (subexpr 0) is used. */
189static int nfa_nsubexpr;
190
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200191static int *post_start; /* holds the postfix form of r.e. */
192static int *post_end;
193static int *post_ptr;
194
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200195static int nstate; /* Number of states in the NFA. Also used when
196 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200197static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200198
199
200static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
201static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
202static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200203static int nfa_regatom __ARGS((void));
204static int nfa_regpiece __ARGS((void));
205static int nfa_regconcat __ARGS((void));
206static int nfa_regbranch __ARGS((void));
207static int nfa_reg __ARGS((int paren));
208#ifdef DEBUG
209static void nfa_set_code __ARGS((int c));
210static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200211static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
212static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213static void nfa_dump __ARGS((nfa_regprog_T *prog));
214#endif
215static int *re2post __ARGS((void));
216static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
217static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
218static int check_char_class __ARGS((int class, int c));
219static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200220static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
221static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200222static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
223static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200224static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200225static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
226static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
227static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
228static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
229static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
230
231/* helper functions used when doing re2post() ... regatom() parsing */
232#define EMIT(c) do { \
233 if (post_ptr >= post_end) \
234 return FAIL; \
235 *post_ptr++ = c; \
236 } while (0)
237
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200238/*
239 * Initialize internal variables before NFA compilation.
240 * Return OK on success, FAIL otherwise.
241 */
242 static int
243nfa_regcomp_start(expr, re_flags)
244 char_u *expr;
245 int re_flags; /* see vim_regcomp() */
246{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200247 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200248 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
250 nstate = 0;
251 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200252 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200253 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200254
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200255 /* Some items blow up in size, such as [A-z]. Add more space for that.
256 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200257 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200258
259 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200261
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262 post_start = (int *)lalloc(postfix_size, TRUE);
263 if (post_start == NULL)
264 return FAIL;
265 vim_memset(post_start, 0, postfix_size);
266 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200267 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268 nfa_has_zend = FALSE;
269
270 regcomp_start(expr, re_flags);
271
272 return OK;
273}
274
275/*
276 * Search between "start" and "end" and try to recognize a
277 * character class in expanded form. For example [0-9].
278 * On success, return the id the character class to be emitted.
279 * On failure, return 0 (=FAIL)
280 * Start points to the first char of the range, while end should point
281 * to the closing brace.
282 */
283 static int
284nfa_recognize_char_class(start, end, extra_newl)
285 char_u *start;
286 char_u *end;
287 int extra_newl;
288{
289 int i;
290 /* Each of these variables takes up a char in "config[]",
291 * in the order they are here. */
292 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
293 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
294 char_u *p;
295#define NCONFIGS 16
296 int classid[NCONFIGS] = {
297 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
298 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
299 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
300 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
301 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200302 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303 char_u config[NCONFIGS][9] = {
304 "000000100", /* digit */
305 "100000100", /* non digit */
306 "011000100", /* hex-digit */
307 "111000100", /* non hex-digit */
308 "000001000", /* octal-digit */
309 "100001000", /* [^0-7] */
310 "000110110", /* [0-9A-Za-z_] */
311 "100110110", /* [^0-9A-Za-z_] */
312 "000110010", /* head of word */
313 "100110010", /* not head of word */
314 "000110000", /* alphabetic char a-z */
315 "100110000", /* non alphabetic char */
316 "000100000", /* lowercase letter */
317 "100100000", /* non lowercase */
318 "000010000", /* uppercase */
319 "100010000" /* non uppercase */
320 };
321
322 if (extra_newl == TRUE)
323 newl = TRUE;
324
325 if (*end != ']')
326 return FAIL;
327 p = start;
328 if (*p == '^')
329 {
330 not = TRUE;
331 p ++;
332 }
333
334 while (p < end)
335 {
336 if (p + 2 < end && *(p + 1) == '-')
337 {
338 switch (*p)
339 {
340 case '0':
341 if (*(p + 2) == '9')
342 {
343 o9 = TRUE;
344 break;
345 }
346 else
347 if (*(p + 2) == '7')
348 {
349 o7 = TRUE;
350 break;
351 }
352 case 'a':
353 if (*(p + 2) == 'z')
354 {
355 az = TRUE;
356 break;
357 }
358 else
359 if (*(p + 2) == 'f')
360 {
361 af = TRUE;
362 break;
363 }
364 case 'A':
365 if (*(p + 2) == 'Z')
366 {
367 AZ = TRUE;
368 break;
369 }
370 else
371 if (*(p + 2) == 'F')
372 {
373 AF = TRUE;
374 break;
375 }
376 /* FALLTHROUGH */
377 default:
378 return FAIL;
379 }
380 p += 3;
381 }
382 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
383 {
384 newl = TRUE;
385 p += 2;
386 }
387 else if (*p == '_')
388 {
389 underscore = TRUE;
390 p ++;
391 }
392 else if (*p == '\n')
393 {
394 newl = TRUE;
395 p ++;
396 }
397 else
398 return FAIL;
399 } /* while (p < end) */
400
401 if (p != end)
402 return FAIL;
403
404 /* build the config that represents the ranges we gathered */
405 STRCPY(myconfig, "000000000");
406 if (not == TRUE)
407 myconfig[0] = '1';
408 if (af == TRUE)
409 myconfig[1] = '1';
410 if (AF == TRUE)
411 myconfig[2] = '1';
412 if (az == TRUE)
413 myconfig[3] = '1';
414 if (AZ == TRUE)
415 myconfig[4] = '1';
416 if (o7 == TRUE)
417 myconfig[5] = '1';
418 if (o9 == TRUE)
419 myconfig[6] = '1';
420 if (underscore == TRUE)
421 myconfig[7] = '1';
422 if (newl == TRUE)
423 {
424 myconfig[8] = '1';
425 extra_newl = ADD_NL;
426 }
427 /* try to recognize character classes */
428 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200429 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200430 return classid[i] + extra_newl;
431
432 /* fallthrough => no success so far */
433 return FAIL;
434
435#undef NCONFIGS
436}
437
438/*
439 * Produce the bytes for equivalence class "c".
440 * Currently only handles latin1, latin9 and utf-8.
441 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
442 * equivalent to 'a OR b OR c'
443 *
444 * NOTE! When changing this function, also update reg_equi_class()
445 */
446 static int
447nfa_emit_equi_class(c, neg)
448 int c;
449 int neg;
450{
451 int first = TRUE;
452 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
453#define EMIT2(c) \
454 EMIT(c); \
455 if (neg == TRUE) { \
456 EMIT(NFA_NOT); \
457 } \
458 if (first == FALSE) \
459 EMIT(glue); \
460 else \
461 first = FALSE; \
462
463#ifdef FEAT_MBYTE
464 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
465 || STRCMP(p_enc, "iso-8859-15") == 0)
466#endif
467 {
468 switch (c)
469 {
470 case 'A': case '\300': case '\301': case '\302':
471 case '\303': case '\304': case '\305':
472 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
473 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
474 EMIT2('\305');
475 return OK;
476
477 case 'C': case '\307':
478 EMIT2('C'); EMIT2('\307');
479 return OK;
480
481 case 'E': case '\310': case '\311': case '\312': case '\313':
482 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
483 EMIT2('\312'); EMIT2('\313');
484 return OK;
485
486 case 'I': case '\314': case '\315': case '\316': case '\317':
487 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
488 EMIT2('\316'); EMIT2('\317');
489 return OK;
490
491 case 'N': case '\321':
492 EMIT2('N'); EMIT2('\321');
493 return OK;
494
495 case 'O': case '\322': case '\323': case '\324': case '\325':
496 case '\326':
497 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
498 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
499 return OK;
500
501 case 'U': case '\331': case '\332': case '\333': case '\334':
502 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
503 EMIT2('\333'); EMIT2('\334');
504 return OK;
505
506 case 'Y': case '\335':
507 EMIT2('Y'); EMIT2('\335');
508 return OK;
509
510 case 'a': case '\340': case '\341': case '\342':
511 case '\343': case '\344': case '\345':
512 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
513 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
514 EMIT2('\345');
515 return OK;
516
517 case 'c': case '\347':
518 EMIT2('c'); EMIT2('\347');
519 return OK;
520
521 case 'e': case '\350': case '\351': case '\352': case '\353':
522 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
523 EMIT2('\352'); EMIT2('\353');
524 return OK;
525
526 case 'i': case '\354': case '\355': case '\356': case '\357':
527 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
528 EMIT2('\356'); EMIT2('\357');
529 return OK;
530
531 case 'n': case '\361':
532 EMIT2('n'); EMIT2('\361');
533 return OK;
534
535 case 'o': case '\362': case '\363': case '\364': case '\365':
536 case '\366':
537 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
538 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
539 return OK;
540
541 case 'u': case '\371': case '\372': case '\373': case '\374':
542 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
543 EMIT2('\373'); EMIT2('\374');
544 return OK;
545
546 case 'y': case '\375': case '\377':
547 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
548 return OK;
549
550 default:
551 return FAIL;
552 }
553 }
554
555 EMIT(c);
556 return OK;
557#undef EMIT2
558}
559
560/*
561 * Code to parse regular expression.
562 *
563 * We try to reuse parsing functions in regexp.c to
564 * minimize surprise and keep the syntax consistent.
565 */
566
567/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200568 * Parse the lowest level.
569 *
570 * An atom can be one of a long list of items. Many atoms match one character
571 * in the text. It is often an ordinary character or a character class.
572 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
573 * is only for syntax highlighting.
574 *
575 * atom ::= ordinary-atom
576 * or \( pattern \)
577 * or \%( pattern \)
578 * or \z( pattern \)
579 */
580 static int
581nfa_regatom()
582{
583 int c;
584 int charclass;
585 int equiclass;
586 int collclass;
587 int got_coll_char;
588 char_u *p;
589 char_u *endp;
590#ifdef FEAT_MBYTE
591 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592#endif
593 int extra = 0;
594 int first;
595 int emit_range;
596 int negated;
597 int result;
598 int startc = -1;
599 int endc = -1;
600 int oldstartc = -1;
601 int cpo_lit; /* 'cpoptions' contains 'l' flag */
602 int cpo_bsl; /* 'cpoptions' contains '\' flag */
603 int glue; /* ID that will "glue" nodes together */
604
605 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
606 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
607
608 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609 switch (c)
610 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200611 case NUL:
612 syntax_error = TRUE;
613 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
614
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200615 case Magic('^'):
616 EMIT(NFA_BOL);
617 break;
618
619 case Magic('$'):
620 EMIT(NFA_EOL);
621#if defined(FEAT_SYN_HL) || defined(PROTO)
622 had_eol = TRUE;
623#endif
624 break;
625
626 case Magic('<'):
627 EMIT(NFA_BOW);
628 break;
629
630 case Magic('>'):
631 EMIT(NFA_EOW);
632 break;
633
634 case Magic('_'):
635 c = no_Magic(getchr());
636 if (c == '^') /* "\_^" is start-of-line */
637 {
638 EMIT(NFA_BOL);
639 break;
640 }
641 if (c == '$') /* "\_$" is end-of-line */
642 {
643 EMIT(NFA_EOL);
644#if defined(FEAT_SYN_HL) || defined(PROTO)
645 had_eol = TRUE;
646#endif
647 break;
648 }
649
650 extra = ADD_NL;
651
652 /* "\_[" is collection plus newline */
653 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200654 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200655
656 /* "\_x" is character class plus newline */
657 /*FALLTHROUGH*/
658
659 /*
660 * Character classes.
661 */
662 case Magic('.'):
663 case Magic('i'):
664 case Magic('I'):
665 case Magic('k'):
666 case Magic('K'):
667 case Magic('f'):
668 case Magic('F'):
669 case Magic('p'):
670 case Magic('P'):
671 case Magic('s'):
672 case Magic('S'):
673 case Magic('d'):
674 case Magic('D'):
675 case Magic('x'):
676 case Magic('X'):
677 case Magic('o'):
678 case Magic('O'):
679 case Magic('w'):
680 case Magic('W'):
681 case Magic('h'):
682 case Magic('H'):
683 case Magic('a'):
684 case Magic('A'):
685 case Magic('l'):
686 case Magic('L'):
687 case Magic('u'):
688 case Magic('U'):
689 p = vim_strchr(classchars, no_Magic(c));
690 if (p == NULL)
691 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200692 EMSGN("INTERNAL: Unknown character class char: %ld", c);
693 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200694 }
695#ifdef FEAT_MBYTE
696 /* When '.' is followed by a composing char ignore the dot, so that
697 * the composing char is matched here. */
698 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
699 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200700 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200701 c = getchr();
702 goto nfa_do_multibyte;
703 }
704#endif
705 EMIT(nfa_classcodes[p - classchars]);
706 if (extra == ADD_NL)
707 {
708 EMIT(NFA_NEWL);
709 EMIT(NFA_OR);
710 regflags |= RF_HASNL;
711 }
712 break;
713
714 case Magic('n'):
715 if (reg_string)
716 /* In a string "\n" matches a newline character. */
717 EMIT(NL);
718 else
719 {
720 /* In buffer text "\n" matches the end of a line. */
721 EMIT(NFA_NEWL);
722 regflags |= RF_HASNL;
723 }
724 break;
725
726 case Magic('('):
727 if (nfa_reg(REG_PAREN) == FAIL)
728 return FAIL; /* cascaded error */
729 break;
730
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200731 case Magic('|'):
732 case Magic('&'):
733 case Magic(')'):
734 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200735 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200736 return FAIL;
737
738 case Magic('='):
739 case Magic('?'):
740 case Magic('+'):
741 case Magic('@'):
742 case Magic('*'):
743 case Magic('{'):
744 /* these should follow an atom, not form an atom */
745 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200746 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200747 return FAIL;
748
749 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200750 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200751 return FAIL;
752
Bram Moolenaar5714b802013-05-28 22:03:20 +0200753 case Magic('1'): EMIT(NFA_BACKREF1); break;
754 case Magic('2'): EMIT(NFA_BACKREF2); break;
755 case Magic('3'): EMIT(NFA_BACKREF3); break;
756 case Magic('4'): EMIT(NFA_BACKREF4); break;
757 case Magic('5'): EMIT(NFA_BACKREF5); break;
758 case Magic('6'): EMIT(NFA_BACKREF6); break;
759 case Magic('7'): EMIT(NFA_BACKREF7); break;
760 case Magic('8'): EMIT(NFA_BACKREF8); break;
761 case Magic('9'): EMIT(NFA_BACKREF9); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200762
763 case Magic('z'):
764 c = no_Magic(getchr());
765 switch (c)
766 {
767 case 's':
768 EMIT(NFA_ZSTART);
769 break;
770 case 'e':
771 EMIT(NFA_ZEND);
772 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200773 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200774 case '1':
775 case '2':
776 case '3':
777 case '4':
778 case '5':
779 case '6':
780 case '7':
781 case '8':
782 case '9':
783 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200784 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200785 return FAIL;
786 default:
787 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200788 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200789 no_Magic(c));
790 return FAIL;
791 }
792 break;
793
794 case Magic('%'):
795 c = no_Magic(getchr());
796 switch (c)
797 {
798 /* () without a back reference */
799 case '(':
800 if (nfa_reg(REG_NPAREN) == FAIL)
801 return FAIL;
802 EMIT(NFA_NOPEN);
803 break;
804
805 case 'd': /* %d123 decimal */
806 case 'o': /* %o123 octal */
807 case 'x': /* %xab hex 2 */
808 case 'u': /* %uabcd hex 4 */
809 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200810 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200811 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812
Bram Moolenaar47196582013-05-25 22:04:23 +0200813 switch (c)
814 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200815 case 'd': nr = getdecchrs(); break;
816 case 'o': nr = getoctchrs(); break;
817 case 'x': nr = gethexchrs(2); break;
818 case 'u': nr = gethexchrs(4); break;
819 case 'U': nr = gethexchrs(8); break;
820 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200821 }
822
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200823 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200824 EMSG2_RET_FAIL(
825 _("E678: Invalid character after %s%%[dxouU]"),
826 reg_magic == MAGIC_ALL);
827 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200828 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200829 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200830 break;
831
832 /* Catch \%^ and \%$ regardless of where they appear in the
833 * pattern -- regardless of whether or not it makes sense. */
834 case '^':
835 EMIT(NFA_BOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200836 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200837 return FAIL;
838 break;
839
840 case '$':
841 EMIT(NFA_EOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200842 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200843 return FAIL;
844 break;
845
846 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200847 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200848 break;
849
850 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200851 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200852 return FAIL;
853 break;
854
855 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200856 /* TODO: \%[abc] not supported yet */
857 return FAIL;
858
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200859 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200860 {
861 long_u n = 0;
862 int cmp = c;
863
864 if (c == '<' || c == '>')
865 c = getchr();
866 while (VIM_ISDIGIT(c))
867 {
868 n = n * 10 + (c - '0');
869 c = getchr();
870 }
871 if (c == 'l' || c == 'c' || c == 'v')
872 {
873 EMIT(n);
874 if (c == 'l')
875 EMIT(cmp == '<' ? NFA_LNUM_LT :
876 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
877 else if (c == 'c')
878 EMIT(cmp == '<' ? NFA_COL_LT :
879 cmp == '>' ? NFA_COL_GT : NFA_COL);
880 else
881 EMIT(cmp == '<' ? NFA_VCOL_LT :
882 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
883 break;
884 }
885 else if (c == '\'')
886 /* TODO: \%'m not supported yet */
887 return FAIL;
888 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200889 syntax_error = TRUE;
890 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
891 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200892 return FAIL;
893 }
894 break;
895
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200896 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200897collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200898 /*
899 * Glue is emitted between several atoms from the [].
900 * It is either NFA_OR, or NFA_CONCAT.
901 *
902 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
903 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
904 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
905 * notation)
906 *
907 */
908
909
910/* Emit negation atoms, if needed.
911 * The CONCAT below merges the NOT with the previous node. */
912#define TRY_NEG() \
913 if (negated == TRUE) \
914 { \
915 EMIT(NFA_NOT); \
916 }
917
918/* Emit glue between important nodes : CONCAT or OR. */
919#define EMIT_GLUE() \
920 if (first == FALSE) \
921 EMIT(glue); \
922 else \
923 first = FALSE;
924
925 p = regparse;
926 endp = skip_anyof(p);
927 if (*endp == ']')
928 {
929 /*
930 * Try to reverse engineer character classes. For example,
931 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
932 * and perform the necessary substitutions in the NFA.
933 */
934 result = nfa_recognize_char_class(regparse, endp,
935 extra == ADD_NL);
936 if (result != FAIL)
937 {
938 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
939 EMIT(result);
940 else /* must be char class + newline */
941 {
942 EMIT(result - ADD_NL);
943 EMIT(NFA_NEWL);
944 EMIT(NFA_OR);
945 }
946 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200947 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200948 return OK;
949 }
950 /*
951 * Failed to recognize a character class. Use the simple
952 * version that turns [abc] into 'a' OR 'b' OR 'c'
953 */
954 startc = endc = oldstartc = -1;
955 first = TRUE; /* Emitting first atom in this sequence? */
956 negated = FALSE;
957 glue = NFA_OR;
958 if (*regparse == '^') /* negated range */
959 {
960 negated = TRUE;
961 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200962 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200963 }
964 if (*regparse == '-')
965 {
966 startc = '-';
967 EMIT(startc);
968 TRY_NEG();
969 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +0200970 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 }
972 /* Emit the OR branches for each character in the [] */
973 emit_range = FALSE;
974 while (regparse < endp)
975 {
976 oldstartc = startc;
977 startc = -1;
978 got_coll_char = FALSE;
979 if (*regparse == '[')
980 {
981 /* Check for [: :], [= =], [. .] */
982 equiclass = collclass = 0;
983 charclass = get_char_class(&regparse);
984 if (charclass == CLASS_NONE)
985 {
986 equiclass = get_equi_class(&regparse);
987 if (equiclass == 0)
988 collclass = get_coll_element(&regparse);
989 }
990
991 /* Character class like [:alpha:] */
992 if (charclass != CLASS_NONE)
993 {
994 switch (charclass)
995 {
996 case CLASS_ALNUM:
997 EMIT(NFA_CLASS_ALNUM);
998 break;
999 case CLASS_ALPHA:
1000 EMIT(NFA_CLASS_ALPHA);
1001 break;
1002 case CLASS_BLANK:
1003 EMIT(NFA_CLASS_BLANK);
1004 break;
1005 case CLASS_CNTRL:
1006 EMIT(NFA_CLASS_CNTRL);
1007 break;
1008 case CLASS_DIGIT:
1009 EMIT(NFA_CLASS_DIGIT);
1010 break;
1011 case CLASS_GRAPH:
1012 EMIT(NFA_CLASS_GRAPH);
1013 break;
1014 case CLASS_LOWER:
1015 EMIT(NFA_CLASS_LOWER);
1016 break;
1017 case CLASS_PRINT:
1018 EMIT(NFA_CLASS_PRINT);
1019 break;
1020 case CLASS_PUNCT:
1021 EMIT(NFA_CLASS_PUNCT);
1022 break;
1023 case CLASS_SPACE:
1024 EMIT(NFA_CLASS_SPACE);
1025 break;
1026 case CLASS_UPPER:
1027 EMIT(NFA_CLASS_UPPER);
1028 break;
1029 case CLASS_XDIGIT:
1030 EMIT(NFA_CLASS_XDIGIT);
1031 break;
1032 case CLASS_TAB:
1033 EMIT(NFA_CLASS_TAB);
1034 break;
1035 case CLASS_RETURN:
1036 EMIT(NFA_CLASS_RETURN);
1037 break;
1038 case CLASS_BACKSPACE:
1039 EMIT(NFA_CLASS_BACKSPACE);
1040 break;
1041 case CLASS_ESCAPE:
1042 EMIT(NFA_CLASS_ESCAPE);
1043 break;
1044 }
1045 TRY_NEG();
1046 EMIT_GLUE();
1047 continue;
1048 }
1049 /* Try equivalence class [=a=] and the like */
1050 if (equiclass != 0)
1051 {
1052 result = nfa_emit_equi_class(equiclass, negated);
1053 if (result == FAIL)
1054 {
1055 /* should never happen */
1056 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1057 }
1058 EMIT_GLUE();
1059 continue;
1060 }
1061 /* Try collating class like [. .] */
1062 if (collclass != 0)
1063 {
1064 startc = collclass; /* allow [.a.]-x as a range */
1065 /* Will emit the proper atom at the end of the
1066 * while loop. */
1067 }
1068 }
1069 /* Try a range like 'a-x' or '\t-z' */
1070 if (*regparse == '-')
1071 {
1072 emit_range = TRUE;
1073 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001074 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001075 continue; /* reading the end of the range */
1076 }
1077
1078 /* Now handle simple and escaped characters.
1079 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1080 * accepts "\t", "\e", etc., but only when the 'l' flag in
1081 * 'cpoptions' is not included.
1082 * Posix doesn't recognize backslash at all.
1083 */
1084 if (*regparse == '\\'
1085 && !cpo_bsl
1086 && regparse + 1 <= endp
1087 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1088 || (!cpo_lit
1089 && vim_strchr(REGEXP_ABBR, regparse[1])
1090 != NULL)
1091 )
1092 )
1093 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001094 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001095
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001096 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001097 startc = reg_string ? NL : NFA_NEWL;
1098 else
1099 if (*regparse == 'd'
1100 || *regparse == 'o'
1101 || *regparse == 'x'
1102 || *regparse == 'u'
1103 || *regparse == 'U'
1104 )
1105 {
1106 /* TODO(RE) This needs more testing */
1107 startc = coll_get_char();
1108 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001109 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001110 }
1111 else
1112 {
1113 /* \r,\t,\e,\b */
1114 startc = backslash_trans(*regparse);
1115 }
1116 }
1117
1118 /* Normal printable char */
1119 if (startc == -1)
1120#ifdef FEAT_MBYTE
1121 startc = (*mb_ptr2char)(regparse);
1122#else
1123 startc = *regparse;
1124#endif
1125
1126 /* Previous char was '-', so this char is end of range. */
1127 if (emit_range)
1128 {
1129 endc = startc; startc = oldstartc;
1130 if (startc > endc)
1131 EMSG_RET_FAIL(_(e_invrange));
1132#ifdef FEAT_MBYTE
1133 if (has_mbyte && ((*mb_char2len)(startc) > 1
1134 || (*mb_char2len)(endc) > 1))
1135 {
1136 if (endc > startc + 256)
1137 EMSG_RET_FAIL(_(e_invrange));
1138 /* Emit the range. "startc" was already emitted, so
1139 * skip it. */
1140 for (c = startc + 1; c <= endc; c++)
1141 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001142 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001143 TRY_NEG();
1144 EMIT_GLUE();
1145 }
1146 emit_range = FALSE;
1147 }
1148 else
1149#endif
1150 {
1151#ifdef EBCDIC
1152 int alpha_only = FALSE;
1153
1154 /* for alphabetical range skip the gaps
1155 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1156 if (isalpha(startc) && isalpha(endc))
1157 alpha_only = TRUE;
1158#endif
1159 /* Emit the range. "startc" was already emitted, so
1160 * skip it. */
1161 for (c = startc + 1; c <= endc; c++)
1162#ifdef EBCDIC
1163 if (!alpha_only || isalpha(startc))
1164#endif
1165 {
1166 EMIT(c);
1167 TRY_NEG();
1168 EMIT_GLUE();
1169 }
1170 emit_range = FALSE;
1171 }
1172 }
1173 else
1174 {
1175 /*
1176 * This char (startc) is not part of a range. Just
1177 * emit it.
1178 *
1179 * Normally, simply emit startc. But if we get char
1180 * code=0 from a collating char, then replace it with
1181 * 0x0a.
1182 *
1183 * This is needed to completely mimic the behaviour of
1184 * the backtracking engine.
1185 */
1186 if (got_coll_char == TRUE && startc == 0)
1187 EMIT(0x0a);
1188 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001189 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190 TRY_NEG();
1191 EMIT_GLUE();
1192 }
1193
Bram Moolenaar51a29832013-05-28 22:30:35 +02001194 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001195 } /* while (p < endp) */
1196
Bram Moolenaar51a29832013-05-28 22:30:35 +02001197 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001198 if (*regparse == '-') /* if last, '-' is just a char */
1199 {
1200 EMIT('-');
1201 TRY_NEG();
1202 EMIT_GLUE();
1203 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001204 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 /* skip the trailing ] */
1207 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001208 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001209 if (negated == TRUE)
1210 {
1211 /* Mark end of negated char range */
1212 EMIT(NFA_END_NEG_RANGE);
1213 EMIT(NFA_CONCAT);
1214 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001215
1216 /* \_[] also matches \n but it's not negated */
1217 if (extra == ADD_NL)
1218 {
1219 EMIT(reg_string ? NL : NFA_NEWL);
1220 EMIT(NFA_OR);
1221 }
1222
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001224 } /* if exists closing ] */
1225
1226 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 {
1228 syntax_error = TRUE;
1229 EMSG_RET_FAIL(_(e_missingbracket));
1230 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001231 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001232
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001233 default:
1234 {
1235#ifdef FEAT_MBYTE
1236 int plen;
1237
1238nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001239 /* plen is length of current char with composing chars */
1240 if (enc_utf8 && ((*mb_char2len)(c)
1241 != (plen = (*mb_ptr2len)(old_regparse))
1242 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001244 int i = 0;
1245
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001246 /* A base character plus composing characters, or just one
1247 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001248 * This requires creating a separate atom as if enclosing
1249 * the characters in (), where NFA_COMPOSING is the ( and
1250 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 * building the postfix form, not the NFA itself;
1252 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001253 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001254 for (;;)
1255 {
1256 EMIT(c);
1257 if (i > 0)
1258 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001259 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001260 break;
1261 c = utf_ptr2char(old_regparse + i);
1262 }
1263 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 regparse = old_regparse + plen;
1265 }
1266 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267#endif
1268 {
1269 c = no_Magic(c);
1270 EMIT(c);
1271 }
1272 return OK;
1273 }
1274 }
1275
1276#undef TRY_NEG
1277#undef EMIT_GLUE
1278
1279 return OK;
1280}
1281
1282/*
1283 * Parse something followed by possible [*+=].
1284 *
1285 * A piece is an atom, possibly followed by a multi, an indication of how many
1286 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1287 * characters: "", "a", "aa", etc.
1288 *
1289 * piece ::= atom
1290 * or atom multi
1291 */
1292 static int
1293nfa_regpiece()
1294{
1295 int i;
1296 int op;
1297 int ret;
1298 long minval, maxval;
1299 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1300 char_u *old_regparse, *new_regparse;
1301 int c2;
1302 int *old_post_ptr, *my_post_start;
1303 int old_regnpar;
1304 int quest;
1305
1306 /* Save the current position in the regexp, so that we can use it if
1307 * <atom>{m,n} is next. */
1308 old_regparse = regparse;
1309 /* Save current number of open parenthesis, so we can use it if
1310 * <atom>{m,n} is next */
1311 old_regnpar = regnpar;
1312 /* store current pos in the postfix form, for \{m,n} involving 0s */
1313 my_post_start = post_ptr;
1314
1315 ret = nfa_regatom();
1316 if (ret == FAIL)
1317 return FAIL; /* cascaded error */
1318
1319 op = peekchr();
1320 if (re_multi_type(op) == NOT_MULTI)
1321 return OK;
1322
1323 skipchr();
1324 switch (op)
1325 {
1326 case Magic('*'):
1327 EMIT(NFA_STAR);
1328 break;
1329
1330 case Magic('+'):
1331 /*
1332 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1333 * first and only submatch would be "aaa". But the backtracking
1334 * engine interprets the plus as "try matching one more time", and
1335 * a* matches a second time at the end of the input, the empty
1336 * string.
1337 * The submatch will the empty string.
1338 *
1339 * In order to be consistent with the old engine, we disable
1340 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1341 */
1342 /* EMIT(NFA_PLUS); */
1343 regnpar = old_regnpar;
1344 regparse = old_regparse;
1345 curchr = -1;
1346 if (nfa_regatom() == FAIL)
1347 return FAIL;
1348 EMIT(NFA_STAR);
1349 EMIT(NFA_CONCAT);
1350 skipchr(); /* skip the \+ */
1351 break;
1352
1353 case Magic('@'):
1354 op = no_Magic(getchr());
1355 switch(op)
1356 {
1357 case '=':
1358 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1359 break;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001360 case '0':
1361 case '1':
1362 case '2':
1363 case '3':
1364 case '4':
1365 case '5':
1366 case '6':
1367 case '7':
1368 case '8':
1369 case '9':
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001370 case '!':
1371 case '<':
1372 case '>':
1373 /* Not supported yet */
1374 return FAIL;
1375 default:
1376 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001377 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001378 return FAIL;
1379 }
1380 break;
1381
1382 case Magic('?'):
1383 case Magic('='):
1384 EMIT(NFA_QUEST);
1385 break;
1386
1387 case Magic('{'):
1388 /* a{2,5} will expand to 'aaa?a?a?'
1389 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1390 * version of '?'
1391 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1392 * parenthesis have the same id
1393 */
1394
1395 greedy = TRUE;
1396 c2 = peekchr();
1397 if (c2 == '-' || c2 == Magic('-'))
1398 {
1399 skipchr();
1400 greedy = FALSE;
1401 }
1402 if (!read_limits(&minval, &maxval))
1403 {
1404 syntax_error = TRUE;
1405 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1406 }
1407 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1408 * <atom>* */
1409 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1410 {
1411 EMIT(NFA_STAR);
1412 break;
1413 }
1414
1415 if (maxval > NFA_BRACES_MAXLIMIT)
1416 {
1417 /* This would yield a huge automaton and use too much memory.
1418 * Revert to old engine */
1419 return FAIL;
1420 }
1421
1422 /* Special case: x{0} or x{-0} */
1423 if (maxval == 0)
1424 {
1425 /* Ignore result of previous call to nfa_regatom() */
1426 post_ptr = my_post_start;
1427 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1428 EMIT(NFA_SKIP_CHAR);
1429 return OK;
1430 }
1431
1432 /* Ignore previous call to nfa_regatom() */
1433 post_ptr = my_post_start;
1434 /* Save pos after the repeated atom and the \{} */
1435 new_regparse = regparse;
1436
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1438 for (i = 0; i < maxval; i++)
1439 {
1440 /* Goto beginning of the repeated atom */
1441 regparse = old_regparse;
1442 curchr = -1;
1443 /* Restore count of parenthesis */
1444 regnpar = old_regnpar;
1445 old_post_ptr = post_ptr;
1446 if (nfa_regatom() == FAIL)
1447 return FAIL;
1448 /* after "minval" times, atoms are optional */
1449 if (i + 1 > minval)
1450 EMIT(quest);
1451 if (old_post_ptr != my_post_start)
1452 EMIT(NFA_CONCAT);
1453 }
1454
1455 /* Go to just after the repeated atom and the \{} */
1456 regparse = new_regparse;
1457 curchr = -1;
1458
1459 break;
1460
1461
1462 default:
1463 break;
1464 } /* end switch */
1465
1466 if (re_multi_type(peekchr()) != NOT_MULTI)
1467 {
1468 /* Can't have a multi follow a multi. */
1469 syntax_error = TRUE;
1470 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1471 }
1472
1473 return OK;
1474}
1475
1476/*
1477 * Parse one or more pieces, concatenated. It matches a match for the
1478 * first piece, followed by a match for the second piece, etc. Example:
1479 * "f[0-9]b", first matches "f", then a digit and then "b".
1480 *
1481 * concat ::= piece
1482 * or piece piece
1483 * or piece piece piece
1484 * etc.
1485 */
1486 static int
1487nfa_regconcat()
1488{
1489 int cont = TRUE;
1490 int first = TRUE;
1491
1492 while (cont)
1493 {
1494 switch (peekchr())
1495 {
1496 case NUL:
1497 case Magic('|'):
1498 case Magic('&'):
1499 case Magic(')'):
1500 cont = FALSE;
1501 break;
1502
1503 case Magic('Z'):
1504#ifdef FEAT_MBYTE
1505 regflags |= RF_ICOMBINE;
1506#endif
1507 skipchr_keepstart();
1508 break;
1509 case Magic('c'):
1510 regflags |= RF_ICASE;
1511 skipchr_keepstart();
1512 break;
1513 case Magic('C'):
1514 regflags |= RF_NOICASE;
1515 skipchr_keepstart();
1516 break;
1517 case Magic('v'):
1518 reg_magic = MAGIC_ALL;
1519 skipchr_keepstart();
1520 curchr = -1;
1521 break;
1522 case Magic('m'):
1523 reg_magic = MAGIC_ON;
1524 skipchr_keepstart();
1525 curchr = -1;
1526 break;
1527 case Magic('M'):
1528 reg_magic = MAGIC_OFF;
1529 skipchr_keepstart();
1530 curchr = -1;
1531 break;
1532 case Magic('V'):
1533 reg_magic = MAGIC_NONE;
1534 skipchr_keepstart();
1535 curchr = -1;
1536 break;
1537
1538 default:
1539 if (nfa_regpiece() == FAIL)
1540 return FAIL;
1541 if (first == FALSE)
1542 EMIT(NFA_CONCAT);
1543 else
1544 first = FALSE;
1545 break;
1546 }
1547 }
1548
1549 return OK;
1550}
1551
1552/*
1553 * Parse a branch, one or more concats, separated by "\&". It matches the
1554 * last concat, but only if all the preceding concats also match at the same
1555 * position. Examples:
1556 * "foobeep\&..." matches "foo" in "foobeep".
1557 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1558 *
1559 * branch ::= concat
1560 * or concat \& concat
1561 * or concat \& concat \& concat
1562 * etc.
1563 */
1564 static int
1565nfa_regbranch()
1566{
1567 int ch;
1568 int *old_post_ptr;
1569
1570 old_post_ptr = post_ptr;
1571
1572 /* First branch, possibly the only one */
1573 if (nfa_regconcat() == FAIL)
1574 return FAIL;
1575
1576 ch = peekchr();
1577 /* Try next concats */
1578 while (ch == Magic('&'))
1579 {
1580 skipchr();
1581 EMIT(NFA_NOPEN);
1582 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1583 old_post_ptr = post_ptr;
1584 if (nfa_regconcat() == FAIL)
1585 return FAIL;
1586 /* if concat is empty, skip a input char. But do emit a node */
1587 if (old_post_ptr == post_ptr)
1588 EMIT(NFA_SKIP_CHAR);
1589 EMIT(NFA_CONCAT);
1590 ch = peekchr();
1591 }
1592
1593 /* Even if a branch is empty, emit one node for it */
1594 if (old_post_ptr == post_ptr)
1595 EMIT(NFA_SKIP_CHAR);
1596
1597 return OK;
1598}
1599
1600/*
1601 * Parse a pattern, one or more branches, separated by "\|". It matches
1602 * anything that matches one of the branches. Example: "foo\|beep" matches
1603 * "foo" and matches "beep". If more than one branch matches, the first one
1604 * is used.
1605 *
1606 * pattern ::= branch
1607 * or branch \| branch
1608 * or branch \| branch \| branch
1609 * etc.
1610 */
1611 static int
1612nfa_reg(paren)
1613 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1614{
1615 int parno = 0;
1616
1617#ifdef FEAT_SYN_HL
1618#endif
1619 if (paren == REG_PAREN)
1620 {
1621 if (regnpar >= NSUBEXP) /* Too many `(' */
1622 {
1623 syntax_error = TRUE;
1624 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1625 }
1626 parno = regnpar++;
1627 }
1628
1629 if (nfa_regbranch() == FAIL)
1630 return FAIL; /* cascaded error */
1631
1632 while (peekchr() == Magic('|'))
1633 {
1634 skipchr();
1635 if (nfa_regbranch() == FAIL)
1636 return FAIL; /* cascaded error */
1637 EMIT(NFA_OR);
1638 }
1639
1640 /* Check for proper termination. */
1641 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1642 {
1643 syntax_error = TRUE;
1644 if (paren == REG_NPAREN)
1645 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1646 else
1647 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1648 }
1649 else if (paren == REG_NOPAREN && peekchr() != NUL)
1650 {
1651 syntax_error = TRUE;
1652 if (peekchr() == Magic(')'))
1653 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1654 else
1655 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1656 }
1657 /*
1658 * Here we set the flag allowing back references to this set of
1659 * parentheses.
1660 */
1661 if (paren == REG_PAREN)
1662 {
1663 had_endbrace[parno] = TRUE; /* have seen the close paren */
1664 EMIT(NFA_MOPEN + parno);
1665 }
1666
1667 return OK;
1668}
1669
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001670#ifdef DEBUG
1671static char_u code[50];
1672
1673 static void
1674nfa_set_code(c)
1675 int c;
1676{
1677 int addnl = FALSE;
1678
1679 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1680 {
1681 addnl = TRUE;
1682 c -= ADD_NL;
1683 }
1684
1685 STRCPY(code, "");
1686 switch (c)
1687 {
1688 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1689 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1690 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1691 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1692 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1693 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1694
Bram Moolenaar5714b802013-05-28 22:03:20 +02001695 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1696 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1697 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1698 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1699 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1700 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1701 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1702 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1703 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1704 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1705
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001706 case NFA_PREV_ATOM_NO_WIDTH:
1707 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001708 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1709 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001710 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1711 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1712 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1713 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1714
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001715 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1716 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1717
1718 case NFA_MOPEN + 0:
1719 case NFA_MOPEN + 1:
1720 case NFA_MOPEN + 2:
1721 case NFA_MOPEN + 3:
1722 case NFA_MOPEN + 4:
1723 case NFA_MOPEN + 5:
1724 case NFA_MOPEN + 6:
1725 case NFA_MOPEN + 7:
1726 case NFA_MOPEN + 8:
1727 case NFA_MOPEN + 9:
1728 STRCPY(code, "NFA_MOPEN(x)");
1729 code[10] = c - NFA_MOPEN + '0';
1730 break;
1731 case NFA_MCLOSE + 0:
1732 case NFA_MCLOSE + 1:
1733 case NFA_MCLOSE + 2:
1734 case NFA_MCLOSE + 3:
1735 case NFA_MCLOSE + 4:
1736 case NFA_MCLOSE + 5:
1737 case NFA_MCLOSE + 6:
1738 case NFA_MCLOSE + 7:
1739 case NFA_MCLOSE + 8:
1740 case NFA_MCLOSE + 9:
1741 STRCPY(code, "NFA_MCLOSE(x)");
1742 code[11] = c - NFA_MCLOSE + '0';
1743 break;
1744 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1745 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1746 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1747 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1748 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1749 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1750 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1751 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1752 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1753 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1754 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1755 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1756 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1757 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1758 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1759 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1760 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1761 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1762 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1763 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1764 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1765 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1766 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1767 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1768 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1769 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1770 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1771 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1772
1773 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1774 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1775 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1776 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1777 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1778 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1779 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1780 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1781 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1782 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1783 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1784 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1785 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1786 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1787 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1788 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1789 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1790 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1791 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1792 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1793 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1794 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1795 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1796 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1797 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1798 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1799 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1800
1801 default:
1802 STRCPY(code, "CHAR(x)");
1803 code[5] = c;
1804 }
1805
1806 if (addnl == TRUE)
1807 STRCAT(code, " + NEWLINE ");
1808
1809}
1810
1811#ifdef ENABLE_LOG
1812static FILE *log_fd;
1813
1814/*
1815 * Print the postfix notation of the current regexp.
1816 */
1817 static void
1818nfa_postfix_dump(expr, retval)
1819 char_u *expr;
1820 int retval;
1821{
1822 int *p;
1823 FILE *f;
1824
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001825 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001826 if (f != NULL)
1827 {
1828 fprintf(f, "\n-------------------------\n");
1829 if (retval == FAIL)
1830 fprintf(f, ">>> NFA engine failed ... \n");
1831 else if (retval == OK)
1832 fprintf(f, ">>> NFA engine succeeded !\n");
1833 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001834 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001835 {
1836 nfa_set_code(*p);
1837 fprintf(f, "%s, ", code);
1838 }
1839 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001840 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001841 fprintf(f, "%d ", *p);
1842 fprintf(f, "\n\n");
1843 fclose(f);
1844 }
1845}
1846
1847/*
1848 * Print the NFA starting with a root node "state".
1849 */
1850 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001851nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001852 FILE *debugf;
1853 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001854{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001855 garray_T indent;
1856
1857 ga_init2(&indent, 1, 64);
1858 ga_append(&indent, '\0');
1859 nfa_print_state2(debugf, state, &indent);
1860 ga_clear(&indent);
1861}
1862
1863 static void
1864nfa_print_state2(debugf, state, indent)
1865 FILE *debugf;
1866 nfa_state_T *state;
1867 garray_T *indent;
1868{
1869 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870
1871 if (state == NULL)
1872 return;
1873
1874 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001875
1876 /* Output indent */
1877 p = (char_u *)indent->ga_data;
1878 if (indent->ga_len >= 3)
1879 {
1880 int last = indent->ga_len - 3;
1881 char_u save[2];
1882
1883 STRNCPY(save, &p[last], 2);
1884 STRNCPY(&p[last], "+-", 2);
1885 fprintf(debugf, " %s", p);
1886 STRNCPY(&p[last], save, 2);
1887 }
1888 else
1889 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890
1891 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001892 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1893 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001894 if (state->id < 0)
1895 return;
1896
1897 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001898
1899 /* grow indent for state->out */
1900 indent->ga_len -= 1;
1901 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001902 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001903 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001904 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001905 ga_append(indent, '\0');
1906
1907 nfa_print_state2(debugf, state->out, indent);
1908
1909 /* replace last part of indent for state->out1 */
1910 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001911 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001912 ga_append(indent, '\0');
1913
1914 nfa_print_state2(debugf, state->out1, indent);
1915
1916 /* shrink indent */
1917 indent->ga_len -= 3;
1918 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919}
1920
1921/*
1922 * Print the NFA state machine.
1923 */
1924 static void
1925nfa_dump(prog)
1926 nfa_regprog_T *prog;
1927{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001928 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929
1930 if (debugf != NULL)
1931 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001932 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 fclose(debugf);
1934 }
1935}
1936#endif /* ENABLE_LOG */
1937#endif /* DEBUG */
1938
1939/*
1940 * Parse r.e. @expr and convert it into postfix form.
1941 * Return the postfix string on success, NULL otherwise.
1942 */
1943 static int *
1944re2post()
1945{
1946 if (nfa_reg(REG_NOPAREN) == FAIL)
1947 return NULL;
1948 EMIT(NFA_MOPEN);
1949 return post_start;
1950}
1951
1952/* NB. Some of the code below is inspired by Russ's. */
1953
1954/*
1955 * Represents an NFA state plus zero or one or two arrows exiting.
1956 * if c == MATCH, no arrows out; matching state.
1957 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1958 * If c < 256, labeled arrow with character c to out.
1959 */
1960
1961static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1962
1963/*
1964 * Allocate and initialize nfa_state_T.
1965 */
1966 static nfa_state_T *
1967new_state(c, out, out1)
1968 int c;
1969 nfa_state_T *out;
1970 nfa_state_T *out1;
1971{
1972 nfa_state_T *s;
1973
1974 if (istate >= nstate)
1975 return NULL;
1976
1977 s = &state_ptr[istate++];
1978
1979 s->c = c;
1980 s->out = out;
1981 s->out1 = out1;
1982
1983 s->id = istate;
1984 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001985 s->negated = FALSE;
1986
1987 return s;
1988}
1989
1990/*
1991 * A partially built NFA without the matching state filled in.
1992 * Frag_T.start points at the start state.
1993 * Frag_T.out is a list of places that need to be set to the
1994 * next state for this fragment.
1995 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001996
1997/* Since the out pointers in the list are always
1998 * uninitialized, we use the pointers themselves
1999 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002000typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002001union Ptrlist
2002{
2003 Ptrlist *next;
2004 nfa_state_T *s;
2005};
2006
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007struct Frag
2008{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002009 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002010 Ptrlist *out;
2011};
2012typedef struct Frag Frag_T;
2013
2014static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2015static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2016static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2017static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2018static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2019static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2020
2021/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002022 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 */
2024 static Frag_T
2025frag(start, out)
2026 nfa_state_T *start;
2027 Ptrlist *out;
2028{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002029 Frag_T n;
2030
2031 n.start = start;
2032 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033 return n;
2034}
2035
2036/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037 * Create singleton list containing just outp.
2038 */
2039 static Ptrlist *
2040list1(outp)
2041 nfa_state_T **outp;
2042{
2043 Ptrlist *l;
2044
2045 l = (Ptrlist *)outp;
2046 l->next = NULL;
2047 return l;
2048}
2049
2050/*
2051 * Patch the list of states at out to point to start.
2052 */
2053 static void
2054patch(l, s)
2055 Ptrlist *l;
2056 nfa_state_T *s;
2057{
2058 Ptrlist *next;
2059
2060 for (; l; l = next)
2061 {
2062 next = l->next;
2063 l->s = s;
2064 }
2065}
2066
2067
2068/*
2069 * Join the two lists l1 and l2, returning the combination.
2070 */
2071 static Ptrlist *
2072append(l1, l2)
2073 Ptrlist *l1;
2074 Ptrlist *l2;
2075{
2076 Ptrlist *oldl1;
2077
2078 oldl1 = l1;
2079 while (l1->next)
2080 l1 = l1->next;
2081 l1->next = l2;
2082 return oldl1;
2083}
2084
2085/*
2086 * Stack used for transforming postfix form into NFA.
2087 */
2088static Frag_T empty;
2089
2090 static void
2091st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002092 int *postfix UNUSED;
2093 int *end UNUSED;
2094 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002095{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002096#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002097 FILE *df;
2098 int *p2;
2099
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002100 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002101 if (df)
2102 {
2103 fprintf(df, "Error popping the stack!\n");
2104#ifdef DEBUG
2105 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2106#endif
2107 fprintf(df, "Postfix form is: ");
2108#ifdef DEBUG
2109 for (p2 = postfix; p2 < end; p2++)
2110 {
2111 nfa_set_code(*p2);
2112 fprintf(df, "%s, ", code);
2113 }
2114 nfa_set_code(*p);
2115 fprintf(df, "\nCurrent position is: ");
2116 for (p2 = postfix; p2 <= p; p2 ++)
2117 {
2118 nfa_set_code(*p2);
2119 fprintf(df, "%s, ", code);
2120 }
2121#else
2122 for (p2 = postfix; p2 < end; p2++)
2123 {
2124 fprintf(df, "%d, ", *p2);
2125 }
2126 fprintf(df, "\nCurrent position is: ");
2127 for (p2 = postfix; p2 <= p; p2 ++)
2128 {
2129 fprintf(df, "%d, ", *p2);
2130 }
2131#endif
2132 fprintf(df, "\n--------------------------\n");
2133 fclose(df);
2134 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002135#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002136 EMSG(_("E874: (NFA) Could not pop the stack !"));
2137}
2138
2139/*
2140 * Push an item onto the stack.
2141 */
2142 static void
2143st_push(s, p, stack_end)
2144 Frag_T s;
2145 Frag_T **p;
2146 Frag_T *stack_end;
2147{
2148 Frag_T *stackp = *p;
2149
2150 if (stackp >= stack_end)
2151 return;
2152 *stackp = s;
2153 *p = *p + 1;
2154}
2155
2156/*
2157 * Pop an item from the stack.
2158 */
2159 static Frag_T
2160st_pop(p, stack)
2161 Frag_T **p;
2162 Frag_T *stack;
2163{
2164 Frag_T *stackp;
2165
2166 *p = *p - 1;
2167 stackp = *p;
2168 if (stackp < stack)
2169 return empty;
2170 return **p;
2171}
2172
2173/*
2174 * Convert a postfix form into its equivalent NFA.
2175 * Return the NFA start state on success, NULL otherwise.
2176 */
2177 static nfa_state_T *
2178post2nfa(postfix, end, nfa_calc_size)
2179 int *postfix;
2180 int *end;
2181 int nfa_calc_size;
2182{
2183 int *p;
2184 int mopen;
2185 int mclose;
2186 Frag_T *stack = NULL;
2187 Frag_T *stackp = NULL;
2188 Frag_T *stack_end = NULL;
2189 Frag_T e1;
2190 Frag_T e2;
2191 Frag_T e;
2192 nfa_state_T *s;
2193 nfa_state_T *s1;
2194 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002195 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002196
2197 if (postfix == NULL)
2198 return NULL;
2199
Bram Moolenaar053bb602013-05-20 13:55:21 +02002200#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201#define POP() st_pop(&stackp, stack); \
2202 if (stackp < stack) \
2203 { \
2204 st_error(postfix, end, p); \
2205 return NULL; \
2206 }
2207
2208 if (nfa_calc_size == FALSE)
2209 {
2210 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002211 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002212 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002213 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002214 }
2215
2216 for (p = postfix; p < end; ++p)
2217 {
2218 switch (*p)
2219 {
2220 case NFA_CONCAT:
2221 /* Catenation.
2222 * Pay attention: this operator does not exist
2223 * in the r.e. itself (it is implicit, really).
2224 * It is added when r.e. is translated to postfix
2225 * form in re2post().
2226 *
2227 * No new state added here. */
2228 if (nfa_calc_size == TRUE)
2229 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002230 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002231 break;
2232 }
2233 e2 = POP();
2234 e1 = POP();
2235 patch(e1.out, e2.start);
2236 PUSH(frag(e1.start, e2.out));
2237 break;
2238
2239 case NFA_NOT:
2240 /* Negation of a character */
2241 if (nfa_calc_size == TRUE)
2242 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002243 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002244 break;
2245 }
2246 e1 = POP();
2247 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002248#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002249 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002250 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002251#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002252 PUSH(e1);
2253 break;
2254
2255 case NFA_OR:
2256 /* Alternation */
2257 if (nfa_calc_size == TRUE)
2258 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002259 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002260 break;
2261 }
2262 e2 = POP();
2263 e1 = POP();
2264 s = new_state(NFA_SPLIT, e1.start, e2.start);
2265 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002266 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002267 PUSH(frag(s, append(e1.out, e2.out)));
2268 break;
2269
2270 case NFA_STAR:
2271 /* Zero or more */
2272 if (nfa_calc_size == TRUE)
2273 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002274 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002275 break;
2276 }
2277 e = POP();
2278 s = new_state(NFA_SPLIT, e.start, NULL);
2279 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002280 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002281 patch(e.out, s);
2282 PUSH(frag(s, list1(&s->out1)));
2283 break;
2284
2285 case NFA_QUEST:
2286 /* one or zero atoms=> greedy match */
2287 if (nfa_calc_size == TRUE)
2288 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002289 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002290 break;
2291 }
2292 e = POP();
2293 s = new_state(NFA_SPLIT, e.start, NULL);
2294 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002295 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002296 PUSH(frag(s, append(e.out, list1(&s->out1))));
2297 break;
2298
2299 case NFA_QUEST_NONGREEDY:
2300 /* zero or one atoms => non-greedy match */
2301 if (nfa_calc_size == TRUE)
2302 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002303 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 break;
2305 }
2306 e = POP();
2307 s = new_state(NFA_SPLIT, NULL, e.start);
2308 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002309 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002310 PUSH(frag(s, append(e.out, list1(&s->out))));
2311 break;
2312
2313 case NFA_PLUS:
2314 /* One or more */
2315 if (nfa_calc_size == TRUE)
2316 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002317 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002318 break;
2319 }
2320 e = POP();
2321 s = new_state(NFA_SPLIT, e.start, NULL);
2322 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002323 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324 patch(e.out, s);
2325 PUSH(frag(e.start, list1(&s->out1)));
2326 break;
2327
2328 case NFA_SKIP_CHAR:
2329 /* Symbol of 0-length, Used in a repetition
2330 * with max/min count of 0 */
2331 if (nfa_calc_size == TRUE)
2332 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002333 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 break;
2335 }
2336 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2337 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002338 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 PUSH(frag(s, list1(&s->out)));
2340 break;
2341
2342 case NFA_PREV_ATOM_NO_WIDTH:
2343 /* The \@= operator: match the preceding atom with 0 width.
2344 * Surrounds the preceding atom with START_INVISIBLE and
2345 * END_INVISIBLE, similarly to MOPEN.
2346 */
2347 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002348 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349
2350 if (nfa_calc_size == TRUE)
2351 {
2352 nstate += 2;
2353 break;
2354 }
2355 e = POP();
2356 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2357 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002358 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002359 patch(e.out, s1);
2360
2361 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2362 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002363 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364 PUSH(frag(s, list1(&s1->out)));
2365 break;
2366
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002367#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002368 case NFA_COMPOSING: /* char with composing char */
2369#if 0
2370 /* TODO */
2371 if (regflags & RF_ICOMBINE)
2372 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002373 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002374 }
2375#endif
2376 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002377#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002378
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379 case NFA_MOPEN + 0: /* Submatch */
2380 case NFA_MOPEN + 1:
2381 case NFA_MOPEN + 2:
2382 case NFA_MOPEN + 3:
2383 case NFA_MOPEN + 4:
2384 case NFA_MOPEN + 5:
2385 case NFA_MOPEN + 6:
2386 case NFA_MOPEN + 7:
2387 case NFA_MOPEN + 8:
2388 case NFA_MOPEN + 9:
2389 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002390 if (nfa_calc_size == TRUE)
2391 {
2392 nstate += 2;
2393 break;
2394 }
2395
2396 mopen = *p;
2397 switch (*p)
2398 {
2399 case NFA_NOPEN:
2400 mclose = NFA_NCLOSE;
2401 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002402#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002403 case NFA_COMPOSING:
2404 mclose = NFA_END_COMPOSING;
2405 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002406#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002407 default:
2408 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2409 mclose = *p + NSUBEXP;
2410 break;
2411 }
2412
2413 /* Allow "NFA_MOPEN" as a valid postfix representation for
2414 * the empty regexp "". In this case, the NFA will be
2415 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2416 * empty groups of parenthesis, and empty mbyte chars */
2417 if (stackp == stack)
2418 {
2419 s = new_state(mopen, NULL, NULL);
2420 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002421 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002422 s1 = new_state(mclose, NULL, NULL);
2423 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002424 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425 patch(list1(&s->out), s1);
2426 PUSH(frag(s, list1(&s1->out)));
2427 break;
2428 }
2429
2430 /* At least one node was emitted before NFA_MOPEN, so
2431 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2432 e = POP();
2433 s = new_state(mopen, e.start, NULL); /* `(' */
2434 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002435 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436
2437 s1 = new_state(mclose, NULL, NULL); /* `)' */
2438 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002439 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440 patch(e.out, s1);
2441
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002442#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002443 if (mopen == NFA_COMPOSING)
2444 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002446#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447
2448 PUSH(frag(s, list1(&s1->out)));
2449 break;
2450
Bram Moolenaar5714b802013-05-28 22:03:20 +02002451 case NFA_BACKREF1:
2452 case NFA_BACKREF2:
2453 case NFA_BACKREF3:
2454 case NFA_BACKREF4:
2455 case NFA_BACKREF5:
2456 case NFA_BACKREF6:
2457 case NFA_BACKREF7:
2458 case NFA_BACKREF8:
2459 case NFA_BACKREF9:
2460 if (nfa_calc_size == TRUE)
2461 {
2462 nstate += 2;
2463 break;
2464 }
2465 s = new_state(*p, NULL, NULL);
2466 if (s == NULL)
2467 goto theend;
2468 s1 = new_state(NFA_SKIP, NULL, NULL);
2469 if (s1 == NULL)
2470 goto theend;
2471 patch(list1(&s->out), s1);
2472 PUSH(frag(s, list1(&s1->out)));
2473 break;
2474
Bram Moolenaar423532e2013-05-29 21:14:42 +02002475 case NFA_LNUM:
2476 case NFA_LNUM_GT:
2477 case NFA_LNUM_LT:
2478 case NFA_VCOL:
2479 case NFA_VCOL_GT:
2480 case NFA_VCOL_LT:
2481 case NFA_COL:
2482 case NFA_COL_GT:
2483 case NFA_COL_LT:
2484 if (nfa_calc_size == TRUE)
2485 {
2486 nstate += 1;
2487 break;
2488 }
2489 e1 = POP();
2490 s = new_state(*p, NULL, NULL);
2491 if (s == NULL)
2492 goto theend;
2493 s->val = e1.start->c;
2494 PUSH(frag(s, list1(&s->out)));
2495 break;
2496
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002497 case NFA_ZSTART:
2498 case NFA_ZEND:
2499 default:
2500 /* Operands */
2501 if (nfa_calc_size == TRUE)
2502 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002503 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504 break;
2505 }
2506 s = new_state(*p, NULL, NULL);
2507 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002508 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002509 PUSH(frag(s, list1(&s->out)));
2510 break;
2511
2512 } /* switch(*p) */
2513
2514 } /* for(p = postfix; *p; ++p) */
2515
2516 if (nfa_calc_size == TRUE)
2517 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002518 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002519 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520 }
2521
2522 e = POP();
2523 if (stackp != stack)
2524 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2525
2526 if (istate >= nstate)
2527 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2528
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002529 matchstate = &state_ptr[istate++]; /* the match state */
2530 matchstate->c = NFA_MATCH;
2531 matchstate->out = matchstate->out1 = NULL;
2532
2533 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002534 ret = e.start;
2535
2536theend:
2537 vim_free(stack);
2538 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539
2540#undef POP1
2541#undef PUSH1
2542#undef POP2
2543#undef PUSH2
2544#undef POP
2545#undef PUSH
2546}
2547
2548/****************************************************************
2549 * NFA execution code.
2550 ****************************************************************/
2551
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002552typedef struct
2553{
2554 int in_use; /* number of subexpr with useful info */
2555
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002556 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002557 union
2558 {
2559 struct multipos
2560 {
2561 lpos_T start;
2562 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002563 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002564 struct linepos
2565 {
2566 char_u *start;
2567 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002568 } line[NSUBEXP];
2569 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002570} regsub_T;
2571
Bram Moolenaar963fee22013-05-26 21:47:28 +02002572/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002573typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002574{
2575 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002576 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002577 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002578} nfa_thread_T;
2579
Bram Moolenaar963fee22013-05-26 21:47:28 +02002580/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002581typedef struct
2582{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002583 nfa_thread_T *t; /* allocated array of states */
2584 int n; /* nr of states in "t" */
2585 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002586} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002587
Bram Moolenaar5714b802013-05-28 22:03:20 +02002588#ifdef ENABLE_LOG
2589 static void
2590log_subexpr(sub)
2591 regsub_T *sub;
2592{
2593 int j;
2594
2595 for (j = 0; j < sub->in_use; j++)
2596 if (REG_MULTI)
2597 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2598 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002599 sub->list.multi[j].start.col,
2600 (int)sub->list.multi[j].start.lnum,
2601 sub->list.multi[j].end.col,
2602 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002603 else
2604 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2605 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002606 (char *)sub->list.line[j].start,
2607 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002608 fprintf(log_fd, "\n");
2609}
2610#endif
2611
Bram Moolenaar963fee22013-05-26 21:47:28 +02002612/* Used during execution: whether a match has been found. */
2613static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002614
Bram Moolenaar5714b802013-05-28 22:03:20 +02002615static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2616static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002617
2618 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002619addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002620 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002621 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002622 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002623 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002624{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002625 int subidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002626 nfa_thread_T *lastthread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002627 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002628 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002629 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002630 int i;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002631
2632 if (l == NULL || state == NULL)
2633 return;
2634
2635 switch (state->c)
2636 {
2637 case NFA_SPLIT:
2638 case NFA_NOT:
2639 case NFA_NOPEN:
2640 case NFA_NCLOSE:
2641 case NFA_MCLOSE:
2642 case NFA_MCLOSE + 1:
2643 case NFA_MCLOSE + 2:
2644 case NFA_MCLOSE + 3:
2645 case NFA_MCLOSE + 4:
2646 case NFA_MCLOSE + 5:
2647 case NFA_MCLOSE + 6:
2648 case NFA_MCLOSE + 7:
2649 case NFA_MCLOSE + 8:
2650 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002651 /* These nodes are not added themselves but their "out" and/or
2652 * "out1" may be added below. */
2653 break;
2654
2655 case NFA_MOPEN:
2656 case NFA_MOPEN + 1:
2657 case NFA_MOPEN + 2:
2658 case NFA_MOPEN + 3:
2659 case NFA_MOPEN + 4:
2660 case NFA_MOPEN + 5:
2661 case NFA_MOPEN + 6:
2662 case NFA_MOPEN + 7:
2663 case NFA_MOPEN + 8:
2664 case NFA_MOPEN + 9:
2665 /* These nodes do not need to be added, but we need to bail out
2666 * when it was tried to be added to this list before. */
2667 if (state->lastlist == l->id)
2668 return;
2669 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670 break;
2671
2672 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002673 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002675 /* This state is already in the list, don't add it again,
2676 * unless it is an MOPEN that is used for a backreference. */
2677 return;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002678 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002679
2680 /* add the state to the list */
2681 state->lastlist = l->id;
2682 lastthread = &l->t[l->n++];
2683 lastthread->state = state;
2684 lastthread->sub.in_use = sub->in_use;
2685 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002686 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002687 /* Copy the match start and end positions. */
2688 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002689 mch_memmove(&lastthread->sub.list.multi[0],
2690 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002691 sizeof(struct multipos) * sub->in_use);
2692 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002693 mch_memmove(&lastthread->sub.list.line[0],
2694 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002695 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696 }
2697 }
2698
2699#ifdef ENABLE_LOG
2700 nfa_set_code(state->c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002701 fprintf(log_fd, "> Adding state %d to list. Character %d: %s\n",
2702 abs(state->id), state->c, code);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002703#endif
2704 switch (state->c)
2705 {
2706 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002707 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002708 break;
2709
2710 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002711 addstate(l, state->out, sub, off);
2712 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002713 break;
2714
2715#if 0
2716 case NFA_END_NEG_RANGE:
2717 /* Nothing to handle here. nfa_regmatch() will take care of it */
2718 break;
2719
2720 case NFA_NOT:
2721 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2722#ifdef ENABLE_LOG
2723 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2724#endif
2725 break;
2726
2727 case NFA_COMPOSING:
2728 /* nfa_regmatch() will match all the bytes of this composing char. */
2729 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730#endif
2731
Bram Moolenaar5714b802013-05-28 22:03:20 +02002732 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002733 case NFA_NOPEN:
2734 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002735 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002736 break;
2737
2738 /* If this state is reached, then a recursive call of nfa_regmatch()
2739 * succeeded. the next call saves the found submatches in the
2740 * first state after the "invisible" branch. */
2741#if 0
2742 case NFA_END_INVISIBLE:
2743 break;
2744#endif
2745
2746 case NFA_MOPEN + 0:
2747 case NFA_MOPEN + 1:
2748 case NFA_MOPEN + 2:
2749 case NFA_MOPEN + 3:
2750 case NFA_MOPEN + 4:
2751 case NFA_MOPEN + 5:
2752 case NFA_MOPEN + 6:
2753 case NFA_MOPEN + 7:
2754 case NFA_MOPEN + 8:
2755 case NFA_MOPEN + 9:
2756 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002757 if (state->c == NFA_ZSTART)
2758 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002759 else
2760 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002761
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002762 /* Set the position (with "off") in the subexpression. Save and
2763 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002764 if (REG_MULTI)
2765 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002766 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002767 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002768 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002769 save_in_use = -1;
2770 }
2771 else
2772 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002773 save_in_use = sub->in_use;
2774 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002775 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002776 sub->list.multi[i].start.lnum = -1;
2777 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002778 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002779 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002780 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002781 if (off == -1)
2782 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002783 sub->list.multi[subidx].start.lnum = reglnum + 1;
2784 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002785 }
2786 else
2787 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002788 sub->list.multi[subidx].start.lnum = reglnum;
2789 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002790 (colnr_T)(reginput - regline + off);
2791 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002792 }
2793 else
2794 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002795 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002796 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002797 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002798 save_in_use = -1;
2799 }
2800 else
2801 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002802 save_in_use = sub->in_use;
2803 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002804 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002805 sub->list.line[i].start = NULL;
2806 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002807 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002808 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002809 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002810 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811 }
2812
Bram Moolenaar5714b802013-05-28 22:03:20 +02002813 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002814
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002815 if (save_in_use == -1)
2816 {
2817 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002818 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002819 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002820 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002821 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002823 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824 break;
2825
2826 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002827 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002829 /* Do not overwrite the position set by \ze. If no \ze
2830 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002831 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832 break;
2833 }
2834 case NFA_MCLOSE + 1:
2835 case NFA_MCLOSE + 2:
2836 case NFA_MCLOSE + 3:
2837 case NFA_MCLOSE + 4:
2838 case NFA_MCLOSE + 5:
2839 case NFA_MCLOSE + 6:
2840 case NFA_MCLOSE + 7:
2841 case NFA_MCLOSE + 8:
2842 case NFA_MCLOSE + 9:
2843 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002844 if (state->c == NFA_ZEND)
2845 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002846 else
2847 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002848
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002849 /* We don't fill in gaps here, there must have been an MOPEN that
2850 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002851 save_in_use = sub->in_use;
2852 if (sub->in_use <= subidx)
2853 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854 if (REG_MULTI)
2855 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002856 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002857 if (off == -1)
2858 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002859 sub->list.multi[subidx].end.lnum = reglnum + 1;
2860 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002861 }
2862 else
2863 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002864 sub->list.multi[subidx].end.lnum = reglnum;
2865 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02002866 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002867 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002868 }
2869 else
2870 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002871 save_ptr = sub->list.line[subidx].end;
2872 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873 }
2874
Bram Moolenaar5714b802013-05-28 22:03:20 +02002875 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876
2877 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002878 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002879 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002880 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002881 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 break;
2883 }
2884}
2885
2886/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002887 * Like addstate(), but the new state(s) are put at position "*ip".
2888 * Used for zero-width matches, next state to use is the added one.
2889 * This makes sure the order of states to be tried does not change, which
2890 * matters for alternatives.
2891 */
2892 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002893addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002894 nfa_list_T *l; /* runtime state list */
2895 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002896 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002897 int *ip;
2898{
2899 int tlen = l->n;
2900 int count;
2901 int i = *ip;
2902
2903 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002904 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02002905
2906 /* when "*ip" was at the end of the list, nothing to do */
2907 if (i + 1 == tlen)
2908 return;
2909
2910 /* re-order to put the new state at the current position */
2911 count = l->n - tlen;
2912 if (count > 1)
2913 {
2914 /* make space for new states, then move them from the
2915 * end to the current position */
2916 mch_memmove(&(l->t[i + count]),
2917 &(l->t[i + 1]),
2918 sizeof(nfa_thread_T) * (l->n - i - 1));
2919 mch_memmove(&(l->t[i]),
2920 &(l->t[l->n - 1]),
2921 sizeof(nfa_thread_T) * count);
2922 }
2923 else
2924 {
2925 /* overwrite the current state */
2926 l->t[i] = l->t[l->n - 1];
2927 }
2928 --l->n;
2929 *ip = i - 1;
2930}
2931
2932/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002933 * Check character class "class" against current character c.
2934 */
2935 static int
2936check_char_class(class, c)
2937 int class;
2938 int c;
2939{
2940 switch (class)
2941 {
2942 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002943 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002944 return OK;
2945 break;
2946 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002947 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002948 return OK;
2949 break;
2950 case NFA_CLASS_BLANK:
2951 if (c == ' ' || c == '\t')
2952 return OK;
2953 break;
2954 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002955 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956 return OK;
2957 break;
2958 case NFA_CLASS_DIGIT:
2959 if (VIM_ISDIGIT(c))
2960 return OK;
2961 break;
2962 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002963 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002964 return OK;
2965 break;
2966 case NFA_CLASS_LOWER:
2967 if (MB_ISLOWER(c))
2968 return OK;
2969 break;
2970 case NFA_CLASS_PRINT:
2971 if (vim_isprintc(c))
2972 return OK;
2973 break;
2974 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002975 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976 return OK;
2977 break;
2978 case NFA_CLASS_SPACE:
2979 if ((c >=9 && c <= 13) || (c == ' '))
2980 return OK;
2981 break;
2982 case NFA_CLASS_UPPER:
2983 if (MB_ISUPPER(c))
2984 return OK;
2985 break;
2986 case NFA_CLASS_XDIGIT:
2987 if (vim_isxdigit(c))
2988 return OK;
2989 break;
2990 case NFA_CLASS_TAB:
2991 if (c == '\t')
2992 return OK;
2993 break;
2994 case NFA_CLASS_RETURN:
2995 if (c == '\r')
2996 return OK;
2997 break;
2998 case NFA_CLASS_BACKSPACE:
2999 if (c == '\b')
3000 return OK;
3001 break;
3002 case NFA_CLASS_ESCAPE:
3003 if (c == '\033')
3004 return OK;
3005 break;
3006
3007 default:
3008 /* should not be here :P */
3009 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3010 }
3011 return FAIL;
3012}
3013
Bram Moolenaar5714b802013-05-28 22:03:20 +02003014static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3015
3016/*
3017 * Check for a match with subexpression "subidx".
3018 * return TRUE if it matches.
3019 */
3020 static int
3021match_backref(sub, subidx, bytelen)
3022 regsub_T *sub; /* pointers to subexpressions */
3023 int subidx;
3024 int *bytelen; /* out: length of match in bytes */
3025{
3026 int len;
3027
3028 if (sub->in_use <= subidx)
3029 {
3030retempty:
3031 /* backref was not set, match an empty string */
3032 *bytelen = 0;
3033 return TRUE;
3034 }
3035
3036 if (REG_MULTI)
3037 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003038 if (sub->list.multi[subidx].start.lnum < 0
3039 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003040 goto retempty;
3041 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003042 len = sub->list.multi[subidx].end.col
3043 - sub->list.multi[subidx].start.col;
3044 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003045 reginput, &len) == 0)
3046 {
3047 *bytelen = len;
3048 return TRUE;
3049 }
3050 }
3051 else
3052 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003053 if (sub->list.line[subidx].start == NULL
3054 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003055 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003056 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3057 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003058 {
3059 *bytelen = len;
3060 return TRUE;
3061 }
3062 }
3063 return FALSE;
3064}
3065
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003066/*
3067 * Set all NFA nodes' list ID equal to -1.
3068 */
3069 static void
3070nfa_set_neg_listids(start)
3071 nfa_state_T *start;
3072{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003073 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003074 {
3075 start->lastlist = -1;
3076 nfa_set_neg_listids(start->out);
3077 nfa_set_neg_listids(start->out1);
3078 }
3079}
3080
3081/*
3082 * Set all NFA nodes' list ID equal to 0.
3083 */
3084 static void
3085nfa_set_null_listids(start)
3086 nfa_state_T *start;
3087{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003088 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003089 {
3090 start->lastlist = 0;
3091 nfa_set_null_listids(start->out);
3092 nfa_set_null_listids(start->out1);
3093 }
3094}
3095
3096/*
3097 * Save list IDs for all NFA states in "list".
3098 */
3099 static void
3100nfa_save_listids(start, list)
3101 nfa_state_T *start;
3102 int *list;
3103{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003104 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003105 {
3106 list[abs(start->id)] = start->lastlist;
3107 start->lastlist = -1;
3108 nfa_save_listids(start->out, list);
3109 nfa_save_listids(start->out1, list);
3110 }
3111}
3112
3113/*
3114 * Restore list IDs from "list" to all NFA states.
3115 */
3116 static void
3117nfa_restore_listids(start, list)
3118 nfa_state_T *start;
3119 int *list;
3120{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003121 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122 {
3123 start->lastlist = list[abs(start->id)];
3124 nfa_restore_listids(start->out, list);
3125 nfa_restore_listids(start->out1, list);
3126 }
3127}
3128
Bram Moolenaar423532e2013-05-29 21:14:42 +02003129 static int
3130nfa_re_num_cmp(val, op, pos)
3131 long_u val;
3132 int op;
3133 long_u pos;
3134{
3135 if (op == 1) return pos > val;
3136 if (op == 2) return pos < val;
3137 return val == pos;
3138}
3139
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003140static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3141
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003142/*
3143 * Main matching routine.
3144 *
3145 * Run NFA to determine whether it matches reginput.
3146 *
3147 * Return TRUE if there is a match, FALSE otherwise.
3148 * Note: Caller must ensure that: start != NULL.
3149 */
3150 static int
3151nfa_regmatch(start, submatch, m)
3152 nfa_state_T *start;
3153 regsub_T *submatch;
3154 regsub_T *m;
3155{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003156 int result;
3157 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003158 int flag = 0;
3159 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003160 int go_to_nextline = FALSE;
3161 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003162 char_u *old_reginput = NULL;
3163 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003164 nfa_list_T list[3];
3165 nfa_list_T *listtbl[2][2];
3166 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003167 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003168 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003169 nfa_list_T *thislist;
3170 nfa_list_T *nextlist;
3171 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003172 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003173#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003174 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003175
3176 if (debug == NULL)
3177 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003178 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 return FALSE;
3180 }
3181#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003182 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003183
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003184 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003185 size = (nstate + 1) * sizeof(nfa_thread_T);
3186 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
3187 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
3188 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3190 goto theend;
3191 vim_memset(list[0].t, 0, size);
3192 vim_memset(list[1].t, 0, size);
3193 vim_memset(list[2].t, 0, size);
3194
3195#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003196 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003197 if (log_fd != NULL)
3198 {
3199 fprintf(log_fd, "**********************************\n");
3200 nfa_set_code(start->c);
3201 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3202 abs(start->id), code);
3203 fprintf(log_fd, "**********************************\n");
3204 }
3205 else
3206 {
3207 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3208 log_fd = stderr;
3209 }
3210#endif
3211
3212 thislist = &list[0];
3213 thislist->n = 0;
3214 nextlist = &list[1];
3215 nextlist->n = 0;
3216 neglist = &list[2];
3217 neglist->n = 0;
3218#ifdef ENABLE_LOG
3219 fprintf(log_fd, "(---) STARTSTATE\n");
3220#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003221 thislist->id = listid;
3222 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223
3224 /* There are two cases when the NFA advances: 1. input char matches the
3225 * NFA node and 2. input char does not match the NFA node, but the next
3226 * node is NFA_NOT. The following macro calls addstate() according to
3227 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3228 listtbl[0][0] = NULL;
3229 listtbl[0][1] = neglist;
3230 listtbl[1][0] = nextlist;
3231 listtbl[1][1] = NULL;
3232#define ADD_POS_NEG_STATE(node) \
3233 ll = listtbl[result ? 1 : 0][node->negated]; \
3234 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003235 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236
3237
3238 /*
3239 * Run for each character.
3240 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003241 for (;;)
3242 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003243 int curc;
3244 int clen;
3245
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003246#ifdef FEAT_MBYTE
3247 if (has_mbyte)
3248 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003249 curc = (*mb_ptr2char)(reginput);
3250 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251 }
3252 else
3253#endif
3254 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003255 curc = *reginput;
3256 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003257 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003258 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003259 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003260 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003261 go_to_nextline = FALSE;
3262 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263
3264 /* swap lists */
3265 thislist = &list[flag];
3266 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003267 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 listtbl[1][0] = nextlist;
3269 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003270 thislist->id = listid;
3271 nextlist->id = listid + 1;
3272 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273
3274#ifdef ENABLE_LOG
3275 fprintf(log_fd, "------------------------------------------\n");
3276 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003277 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003279 {
3280 int i;
3281
3282 for (i = 0; i < thislist->n; i++)
3283 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3284 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003285 fprintf(log_fd, "\n");
3286#endif
3287
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003288#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289 fprintf(debug, "\n-------------------\n");
3290#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003291 /*
3292 * If the state lists are empty we can stop.
3293 */
3294 if (thislist->n == 0 && neglist->n == 0)
3295 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296
3297 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003298 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 {
3300 if (neglist->n > 0)
3301 {
3302 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003303 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003304 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 }
3306 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003307 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003309#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 nfa_set_code(t->state->c);
3311 fprintf(debug, "%s, ", code);
3312#endif
3313#ifdef ENABLE_LOG
3314 nfa_set_code(t->state->c);
3315 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3316 code, (int)t->state->c);
3317#endif
3318
3319 /*
3320 * Handle the possible codes of the current state.
3321 * The most important is NFA_MATCH.
3322 */
3323 switch (t->state->c)
3324 {
3325 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003326 {
3327 int j;
3328
Bram Moolenaar963fee22013-05-26 21:47:28 +02003329 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003330 submatch->in_use = t->sub.in_use;
3331 if (REG_MULTI)
3332 for (j = 0; j < submatch->in_use; j++)
3333 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003334 submatch->list.multi[j].start =
3335 t->sub.list.multi[j].start;
3336 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003337 }
3338 else
3339 for (j = 0; j < submatch->in_use; j++)
3340 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003341 submatch->list.line[j].start =
3342 t->sub.list.line[j].start;
3343 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003344 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003345#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003346 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003347#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003348 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003349 * states at this position. When the list of states is going
3350 * to be empty quit without advancing, so that "reginput" is
3351 * correct. */
3352 if (nextlist->n == 0 && neglist->n == 0)
3353 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003354 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003355 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003356
3357 case NFA_END_INVISIBLE:
3358 /* This is only encountered after a NFA_START_INVISIBLE node.
3359 * They surround a zero-width group, used with "\@=" and "\&".
3360 * If we got here, it means that the current "invisible" group
3361 * finished successfully, so return control to the parent
3362 * nfa_regmatch(). Submatches are stored in *m, and used in
3363 * the parent call. */
3364 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003365 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003366 else
3367 {
3368 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003369 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370 }
3371 break;
3372
3373 case NFA_START_INVISIBLE:
3374 /* Save global variables, and call nfa_regmatch() to check if
3375 * the current concat matches at this position. The concat
3376 * ends with the node NFA_END_INVISIBLE */
3377 old_reginput = reginput;
3378 old_regline = regline;
3379 old_reglnum = reglnum;
3380 if (listids == NULL)
3381 {
3382 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3383 if (listids == NULL)
3384 {
3385 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3386 return 0;
3387 }
3388 }
3389#ifdef ENABLE_LOG
3390 if (log_fd != stderr)
3391 fclose(log_fd);
3392 log_fd = NULL;
3393#endif
3394 /* Have to clear the listid field of the NFA nodes, so that
3395 * nfa_regmatch() and addstate() can run properly after
3396 * recursion. */
3397 nfa_save_listids(start, listids);
3398 nfa_set_null_listids(start);
3399 result = nfa_regmatch(t->state->out, submatch, m);
3400 nfa_set_neg_listids(start);
3401 nfa_restore_listids(start, listids);
3402
3403#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003404 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003405 if (log_fd != NULL)
3406 {
3407 fprintf(log_fd, "****************************\n");
3408 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3409 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3410 fprintf(log_fd, "****************************\n");
3411 }
3412 else
3413 {
3414 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3415 log_fd = stderr;
3416 }
3417#endif
3418 if (result == TRUE)
3419 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003420 int j;
3421
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 /* Restore position in input text */
3423 reginput = old_reginput;
3424 regline = old_regline;
3425 reglnum = old_reglnum;
3426 /* Copy submatch info from the recursive call */
3427 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003428 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003430 t->sub.list.multi[j].start = m->list.multi[j].start;
3431 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003432 }
3433 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003434 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003435 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003436 t->sub.list.line[j].start = m->list.line[j].start;
3437 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003438 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003439 t->sub.in_use = m->in_use;
3440
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003441 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003442 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003443 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 }
3445 else
3446 {
3447 /* continue with next input char */
3448 reginput = old_reginput;
3449 }
3450 break;
3451
3452 case NFA_BOL:
3453 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003454 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 break;
3456
3457 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003458 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003459 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460 break;
3461
3462 case NFA_BOW:
3463 {
3464 int bow = TRUE;
3465
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003466 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003467 bow = FALSE;
3468#ifdef FEAT_MBYTE
3469 else if (has_mbyte)
3470 {
3471 int this_class;
3472
3473 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003474 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 if (this_class <= 1)
3476 bow = FALSE;
3477 else if (reg_prev_class() == this_class)
3478 bow = FALSE;
3479 }
3480#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003481 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003482 || (reginput > regline
3483 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484 bow = FALSE;
3485 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003486 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 break;
3488 }
3489
3490 case NFA_EOW:
3491 {
3492 int eow = TRUE;
3493
3494 if (reginput == regline)
3495 eow = FALSE;
3496#ifdef FEAT_MBYTE
3497 else if (has_mbyte)
3498 {
3499 int this_class, prev_class;
3500
3501 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003502 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503 prev_class = reg_prev_class();
3504 if (this_class == prev_class
3505 || prev_class == 0 || prev_class == 1)
3506 eow = FALSE;
3507 }
3508#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003509 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003510 || (reginput[0] != NUL
3511 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003512 eow = FALSE;
3513 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003514 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003515 break;
3516 }
3517
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003518#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003520 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003521 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003522 int len = 0;
3523 nfa_state_T *end;
3524 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003525 int cchars[MAX_MCO];
3526 int ccount = 0;
3527 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003528
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003530 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003531 if (utf_iscomposing(sta->c))
3532 {
3533 /* Only match composing character(s), ignore base
3534 * character. Used for ".{composing}" and "{composing}"
3535 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003536 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003537 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003538 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003539 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003540 /* If \Z was present, then ignore composing characters.
3541 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003542 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003543 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003544 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003545 else
3546 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003547 while (sta->c != NFA_END_COMPOSING)
3548 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003549 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003550
3551 /* Check base character matches first, unless ignored. */
3552 else if (len > 0 || mc == sta->c)
3553 {
3554 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003555 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003556 len += mb_char2len(mc);
3557 sta = sta->out;
3558 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003559
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003560 /* We don't care about the order of composing characters.
3561 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003562 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003563 {
3564 mc = mb_ptr2char(reginput + len);
3565 cchars[ccount++] = mc;
3566 len += mb_char2len(mc);
3567 if (ccount == MAX_MCO)
3568 break;
3569 }
3570
3571 /* Check that each composing char in the pattern matches a
3572 * composing char in the text. We do not check if all
3573 * composing chars are matched. */
3574 result = OK;
3575 while (sta->c != NFA_END_COMPOSING)
3576 {
3577 for (j = 0; j < ccount; ++j)
3578 if (cchars[j] == sta->c)
3579 break;
3580 if (j == ccount)
3581 {
3582 result = FAIL;
3583 break;
3584 }
3585 sta = sta->out;
3586 }
3587 }
3588 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003589 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003590
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003591 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592 ADD_POS_NEG_STATE(end);
3593 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003594 }
3595#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596
3597 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003598 if (curc == NUL && !reg_line_lbr && REG_MULTI
3599 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003601 go_to_nextline = TRUE;
3602 /* Pass -1 for the offset, which means taking the position
3603 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003604 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003606 else if (curc == '\n' && reg_line_lbr)
3607 {
3608 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003609 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003610 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003611 break;
3612
3613 case NFA_CLASS_ALNUM:
3614 case NFA_CLASS_ALPHA:
3615 case NFA_CLASS_BLANK:
3616 case NFA_CLASS_CNTRL:
3617 case NFA_CLASS_DIGIT:
3618 case NFA_CLASS_GRAPH:
3619 case NFA_CLASS_LOWER:
3620 case NFA_CLASS_PRINT:
3621 case NFA_CLASS_PUNCT:
3622 case NFA_CLASS_SPACE:
3623 case NFA_CLASS_UPPER:
3624 case NFA_CLASS_XDIGIT:
3625 case NFA_CLASS_TAB:
3626 case NFA_CLASS_RETURN:
3627 case NFA_CLASS_BACKSPACE:
3628 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003629 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 ADD_POS_NEG_STATE(t->state);
3631 break;
3632
3633 case NFA_END_NEG_RANGE:
3634 /* This follows a series of negated nodes, like:
3635 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003636 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003637 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003638 break;
3639
3640 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003641 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003642 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003643 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 break;
3645
3646 /*
3647 * Character classes like \a for alpha, \d for digit etc.
3648 */
3649 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003650 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651 ADD_POS_NEG_STATE(t->state);
3652 break;
3653
3654 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003655 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003656 ADD_POS_NEG_STATE(t->state);
3657 break;
3658
3659 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003660 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003661 ADD_POS_NEG_STATE(t->state);
3662 break;
3663
3664 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003665 result = !VIM_ISDIGIT(curc)
3666 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003667 ADD_POS_NEG_STATE(t->state);
3668 break;
3669
3670 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003671 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 ADD_POS_NEG_STATE(t->state);
3673 break;
3674
3675 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003676 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003677 ADD_POS_NEG_STATE(t->state);
3678 break;
3679
3680 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003681 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003682 ADD_POS_NEG_STATE(t->state);
3683 break;
3684
3685 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003686 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003687 ADD_POS_NEG_STATE(t->state);
3688 break;
3689
3690 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003691 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003692 ADD_POS_NEG_STATE(t->state);
3693 break;
3694
3695 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003696 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697 ADD_POS_NEG_STATE(t->state);
3698 break;
3699
3700 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003701 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003702 ADD_POS_NEG_STATE(t->state);
3703 break;
3704
3705 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003706 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003707 ADD_POS_NEG_STATE(t->state);
3708 break;
3709
3710 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003711 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712 ADD_POS_NEG_STATE(t->state);
3713 break;
3714
3715 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003716 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717 ADD_POS_NEG_STATE(t->state);
3718 break;
3719
3720 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003721 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 ADD_POS_NEG_STATE(t->state);
3723 break;
3724
3725 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003726 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003727 ADD_POS_NEG_STATE(t->state);
3728 break;
3729
3730 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003731 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003732 ADD_POS_NEG_STATE(t->state);
3733 break;
3734
3735 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003736 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003737 ADD_POS_NEG_STATE(t->state);
3738 break;
3739
3740 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003741 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003742 ADD_POS_NEG_STATE(t->state);
3743 break;
3744
3745 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003746 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747 ADD_POS_NEG_STATE(t->state);
3748 break;
3749
3750 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003751 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752 ADD_POS_NEG_STATE(t->state);
3753 break;
3754
3755 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003756 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003757 ADD_POS_NEG_STATE(t->state);
3758 break;
3759
3760 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003761 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003762 ADD_POS_NEG_STATE(t->state);
3763 break;
3764
3765 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003766 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767 ADD_POS_NEG_STATE(t->state);
3768 break;
3769
3770 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003771 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003772 ADD_POS_NEG_STATE(t->state);
3773 break;
3774
3775 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003776 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003777 ADD_POS_NEG_STATE(t->state);
3778 break;
3779
Bram Moolenaar5714b802013-05-28 22:03:20 +02003780 case NFA_BACKREF1:
3781 case NFA_BACKREF2:
3782 case NFA_BACKREF3:
3783 case NFA_BACKREF4:
3784 case NFA_BACKREF5:
3785 case NFA_BACKREF6:
3786 case NFA_BACKREF7:
3787 case NFA_BACKREF8:
3788 case NFA_BACKREF9:
3789 /* \1 .. \9 */
3790 {
3791 int subidx = t->state->c - NFA_BACKREF1 + 1;
3792 int bytelen;
3793
3794 result = match_backref(&t->sub, subidx, &bytelen);
3795 if (result)
3796 {
3797 if (bytelen == 0)
3798 {
3799 /* empty match always works, add NFA_SKIP with zero to
3800 * be used next */
3801 addstate_here(thislist, t->state->out, &t->sub,
3802 &listidx);
3803 thislist->t[listidx + 1].count = 0;
3804 }
3805 else if (bytelen <= clen)
3806 {
3807 /* match current character, jump ahead to out of
3808 * NFA_SKIP */
3809 addstate(nextlist, t->state->out->out, &t->sub, clen);
3810#ifdef ENABLE_LOG
3811 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3812#endif
3813 }
3814 else
3815 {
3816 /* skip ofer the matched characters, set character
3817 * count in NFA_SKIP */
3818 addstate(nextlist, t->state->out, &t->sub, bytelen);
3819 nextlist->t[nextlist->n - 1].count = bytelen - clen;
3820#ifdef ENABLE_LOG
3821 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3822#endif
3823 }
3824
3825 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02003826 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003827 }
3828 case NFA_SKIP:
3829 /* charater of previous matching \1 .. \9 */
3830 if (t->count - clen <= 0)
3831 {
3832 /* end of match, go to what follows */
3833 addstate(nextlist, t->state->out, &t->sub, clen);
3834#ifdef ENABLE_LOG
3835 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3836#endif
3837 }
3838 else
3839 {
3840 /* add state again with decremented count */
3841 addstate(nextlist, t->state, &t->sub, 0);
3842 nextlist->t[nextlist->n - 1].count = t->count - clen;
3843#ifdef ENABLE_LOG
3844 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3845#endif
3846 }
3847 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003848
3849 case NFA_SKIP_CHAR:
3850 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003851 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02003852 /* TODO: should not happen? */
3853 break;
3854
Bram Moolenaar423532e2013-05-29 21:14:42 +02003855 case NFA_LNUM:
3856 case NFA_LNUM_GT:
3857 case NFA_LNUM_LT:
3858 result = (REG_MULTI &&
3859 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
3860 (long_u)(reglnum + reg_firstlnum)));
3861 if (result)
3862 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3863 break;
3864
3865 case NFA_COL:
3866 case NFA_COL_GT:
3867 case NFA_COL_LT:
3868 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
3869 (long_u)(reginput - regline) + 1);
3870 if (result)
3871 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3872 break;
3873
3874 case NFA_VCOL:
3875 case NFA_VCOL_GT:
3876 case NFA_VCOL_LT:
3877 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
3878 (long_u)win_linetabsize(
3879 reg_win == NULL ? curwin : reg_win,
3880 regline, (colnr_T)(reginput - regline)) + 1);
3881 if (result)
3882 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3883 break;
3884
3885 case NFA_CURSOR:
3886 result = (reg_win != NULL
3887 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
3888 && ((colnr_T)(reginput - regline)
3889 == reg_win->w_cursor.col));
3890 if (result)
3891 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3892 break;
3893
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003894 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003895 {
3896 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003897
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003898 /* TODO: put this in #ifdef later */
3899 if (c < -256)
3900 EMSGN("INTERNAL: Negative state char: %ld", c);
3901 if (is_Magic(c))
3902 c = un_Magic(c);
3903 result = (c == curc);
3904
3905 if (!result && ireg_ic)
3906 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003907#ifdef FEAT_MBYTE
3908 /* If there is a composing character which is not being
3909 * ignored there can be no match. Match with composing
3910 * character uses NFA_COMPOSING above. */
3911 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003912 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003913 result = FALSE;
3914#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003915 ADD_POS_NEG_STATE(t->state);
3916 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003917 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918 }
3919
3920 } /* for (thislist = thislist; thislist->state; thislist++) */
3921
Bram Moolenaare23febd2013-05-26 18:40:14 +02003922 /* Look for the start of a match in the current position by adding the
3923 * start state to the list of states.
3924 * The first found match is the leftmost one, thus the order of states
3925 * matters!
3926 * Do not add the start state in recursive calls of nfa_regmatch(),
3927 * because recursive calls should only start in the first position.
3928 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003929 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003930 && reglnum == 0 && clen != 0
3931 && (ireg_maxcol == 0
3932 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003933 {
3934#ifdef ENABLE_LOG
3935 fprintf(log_fd, "(---) STARTSTATE\n");
3936#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003937 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003938 }
3939
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003940#ifdef ENABLE_LOG
3941 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003942 {
3943 int i;
3944
3945 for (i = 0; i < thislist->n; i++)
3946 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3947 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003948 fprintf(log_fd, "\n");
3949#endif
3950
3951nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003952 /* Advance to the next character, or advance to the next line, or
3953 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003954 if (clen != 0)
3955 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003956 else if (go_to_nextline)
3957 reg_nextline();
3958 else
3959 break;
3960 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003961
3962#ifdef ENABLE_LOG
3963 if (log_fd != stderr)
3964 fclose(log_fd);
3965 log_fd = NULL;
3966#endif
3967
3968theend:
3969 /* Free memory */
3970 vim_free(list[0].t);
3971 vim_free(list[1].t);
3972 vim_free(list[2].t);
3973 list[0].t = list[1].t = list[2].t = NULL;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003974 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003975#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003976#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003977 fclose(debug);
3978#endif
3979
Bram Moolenaar963fee22013-05-26 21:47:28 +02003980 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003981}
3982
3983/*
3984 * Try match of "prog" with at regline["col"].
3985 * Returns 0 for failure, number of lines contained in the match otherwise.
3986 */
3987 static long
3988nfa_regtry(start, col)
3989 nfa_state_T *start;
3990 colnr_T col;
3991{
3992 int i;
3993 regsub_T sub, m;
3994#ifdef ENABLE_LOG
3995 FILE *f;
3996#endif
3997
3998 reginput = regline + col;
3999 need_clear_subexpr = TRUE;
4000
4001#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004002 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004003 if (f != NULL)
4004 {
4005 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4006 fprintf(f, " =======================================================\n");
4007#ifdef DEBUG
4008 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4009#endif
4010 fprintf(f, "\tInput text is \"%s\" \n", reginput);
4011 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004012 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004013 fprintf(f, "\n\n");
4014 fclose(f);
4015 }
4016 else
4017 EMSG(_("Could not open temporary log file for writing "));
4018#endif
4019
4020 if (REG_MULTI)
4021 {
4022 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004023 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4024 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004025 }
4026 else
4027 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004028 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4029 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004030 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004031 sub.in_use = 0;
4032 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004033
4034 if (nfa_regmatch(start, &sub, &m) == FALSE)
4035 return 0;
4036
4037 cleanup_subexpr();
4038 if (REG_MULTI)
4039 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004040 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004041 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004042 reg_startpos[i] = sub.list.multi[i].start;
4043 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004044 }
4045
4046 if (reg_startpos[0].lnum < 0)
4047 {
4048 reg_startpos[0].lnum = 0;
4049 reg_startpos[0].col = col;
4050 }
4051 if (reg_endpos[0].lnum < 0)
4052 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004053 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004054 reg_endpos[0].lnum = reglnum;
4055 reg_endpos[0].col = (int)(reginput - regline);
4056 }
4057 else
4058 /* Use line number of "\ze". */
4059 reglnum = reg_endpos[0].lnum;
4060 }
4061 else
4062 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004063 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004064 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004065 reg_startp[i] = sub.list.line[i].start;
4066 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067 }
4068
4069 if (reg_startp[0] == NULL)
4070 reg_startp[0] = regline + col;
4071 if (reg_endp[0] == NULL)
4072 reg_endp[0] = reginput;
4073 }
4074
4075 return 1 + reglnum;
4076}
4077
4078/*
4079 * Match a regexp against a string ("line" points to the string) or multiple
4080 * lines ("line" is NULL, use reg_getline()).
4081 *
4082 * Returns 0 for failure, number of lines contained in the match otherwise.
4083 */
4084 static long
4085nfa_regexec_both(line, col)
4086 char_u *line;
4087 colnr_T col; /* column to start looking for match */
4088{
4089 nfa_regprog_T *prog;
4090 long retval = 0L;
4091 int i;
4092
4093 if (REG_MULTI)
4094 {
4095 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4096 line = reg_getline((linenr_T)0); /* relative to the cursor */
4097 reg_startpos = reg_mmatch->startpos;
4098 reg_endpos = reg_mmatch->endpos;
4099 }
4100 else
4101 {
4102 prog = (nfa_regprog_T *)reg_match->regprog;
4103 reg_startp = reg_match->startp;
4104 reg_endp = reg_match->endp;
4105 }
4106
4107 /* Be paranoid... */
4108 if (prog == NULL || line == NULL)
4109 {
4110 EMSG(_(e_null));
4111 goto theend;
4112 }
4113
4114 /* If the start column is past the maximum column: no need to try. */
4115 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4116 goto theend;
4117
4118 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4119 if (prog->regflags & RF_ICASE)
4120 ireg_ic = TRUE;
4121 else if (prog->regflags & RF_NOICASE)
4122 ireg_ic = FALSE;
4123
4124#ifdef FEAT_MBYTE
4125 /* If pattern contains "\Z" overrule value of ireg_icombine */
4126 if (prog->regflags & RF_ICOMBINE)
4127 ireg_icombine = TRUE;
4128#endif
4129
4130 regline = line;
4131 reglnum = 0; /* relative to line */
4132
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004133 nfa_has_zend = prog->has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004134 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004136 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004137 for (i = 0; i < nstate; ++i)
4138 {
4139 prog->state[i].id = i;
4140 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141 }
4142
4143 retval = nfa_regtry(prog->start, col);
4144
4145theend:
4146 return retval;
4147}
4148
4149/*
4150 * Compile a regular expression into internal code for the NFA matcher.
4151 * Returns the program in allocated space. Returns NULL for an error.
4152 */
4153 static regprog_T *
4154nfa_regcomp(expr, re_flags)
4155 char_u *expr;
4156 int re_flags;
4157{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004158 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004159 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004160 int *postfix;
4161
4162 if (expr == NULL)
4163 return NULL;
4164
4165#ifdef DEBUG
4166 nfa_regengine.expr = expr;
4167#endif
4168
4169 init_class_tab();
4170
4171 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4172 return NULL;
4173
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004174 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004175 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004176 postfix = re2post();
4177 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004178 {
4179 /* TODO: only give this error for debugging? */
4180 if (post_ptr >= post_end)
4181 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004183 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004184
4185 /*
4186 * In order to build the NFA, we parse the input regexp twice:
4187 * 1. first pass to count size (so we can allocate space)
4188 * 2. second to emit code
4189 */
4190#ifdef ENABLE_LOG
4191 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004192 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004193
4194 if (f != NULL)
4195 {
4196 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4197 fclose(f);
4198 }
4199 }
4200#endif
4201
4202 /*
4203 * PASS 1
4204 * Count number of NFA states in "nstate". Do not build the NFA.
4205 */
4206 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004207
4208 /* Space for compiled regexp */
4209 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4210 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4211 if (prog == NULL)
4212 goto fail;
4213 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004214 state_ptr = prog->state;
4215
4216 /*
4217 * PASS 2
4218 * Build the NFA
4219 */
4220 prog->start = post2nfa(postfix, post_ptr, FALSE);
4221 if (prog->start == NULL)
4222 goto fail;
4223
4224 prog->regflags = regflags;
4225 prog->engine = &nfa_regengine;
4226 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004227 prog->has_zend = nfa_has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004228 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004229#ifdef ENABLE_LOG
4230 nfa_postfix_dump(expr, OK);
4231 nfa_dump(prog);
4232#endif
4233
4234out:
4235 vim_free(post_start);
4236 post_start = post_ptr = post_end = NULL;
4237 state_ptr = NULL;
4238 return (regprog_T *)prog;
4239
4240fail:
4241 vim_free(prog);
4242 prog = NULL;
4243#ifdef ENABLE_LOG
4244 nfa_postfix_dump(expr, FAIL);
4245#endif
4246#ifdef DEBUG
4247 nfa_regengine.expr = NULL;
4248#endif
4249 goto out;
4250}
4251
4252
4253/*
4254 * Match a regexp against a string.
4255 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4256 * Uses curbuf for line count and 'iskeyword'.
4257 *
4258 * Return TRUE if there is a match, FALSE if not.
4259 */
4260 static int
4261nfa_regexec(rmp, line, col)
4262 regmatch_T *rmp;
4263 char_u *line; /* string to match against */
4264 colnr_T col; /* column to start looking for match */
4265{
4266 reg_match = rmp;
4267 reg_mmatch = NULL;
4268 reg_maxline = 0;
4269 reg_line_lbr = FALSE;
4270 reg_buf = curbuf;
4271 reg_win = NULL;
4272 ireg_ic = rmp->rm_ic;
4273#ifdef FEAT_MBYTE
4274 ireg_icombine = FALSE;
4275#endif
4276 ireg_maxcol = 0;
4277 return (nfa_regexec_both(line, col) != 0);
4278}
4279
4280#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4281 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4282
4283static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4284
4285/*
4286 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4287 */
4288 static int
4289nfa_regexec_nl(rmp, line, col)
4290 regmatch_T *rmp;
4291 char_u *line; /* string to match against */
4292 colnr_T col; /* column to start looking for match */
4293{
4294 reg_match = rmp;
4295 reg_mmatch = NULL;
4296 reg_maxline = 0;
4297 reg_line_lbr = TRUE;
4298 reg_buf = curbuf;
4299 reg_win = NULL;
4300 ireg_ic = rmp->rm_ic;
4301#ifdef FEAT_MBYTE
4302 ireg_icombine = FALSE;
4303#endif
4304 ireg_maxcol = 0;
4305 return (nfa_regexec_both(line, col) != 0);
4306}
4307#endif
4308
4309
4310/*
4311 * Match a regexp against multiple lines.
4312 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4313 * Uses curbuf for line count and 'iskeyword'.
4314 *
4315 * Return zero if there is no match. Return number of lines contained in the
4316 * match otherwise.
4317 *
4318 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4319 *
4320 * ! Also NOTE : match may actually be in another line. e.g.:
4321 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4322 *
4323 * +-------------------------+
4324 * |a |
4325 * |b |
4326 * |c |
4327 * | |
4328 * +-------------------------+
4329 *
4330 * then nfa_regexec_multi() returns 3. while the original
4331 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4332 *
4333 * FIXME if this behavior is not compatible.
4334 */
4335 static long
4336nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4337 regmmatch_T *rmp;
4338 win_T *win; /* window in which to search or NULL */
4339 buf_T *buf; /* buffer in which to search */
4340 linenr_T lnum; /* nr of line to start looking for match */
4341 colnr_T col; /* column to start looking for match */
4342 proftime_T *tm UNUSED; /* timeout limit or NULL */
4343{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004344 reg_match = NULL;
4345 reg_mmatch = rmp;
4346 reg_buf = buf;
4347 reg_win = win;
4348 reg_firstlnum = lnum;
4349 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4350 reg_line_lbr = FALSE;
4351 ireg_ic = rmp->rmm_ic;
4352#ifdef FEAT_MBYTE
4353 ireg_icombine = FALSE;
4354#endif
4355 ireg_maxcol = rmp->rmm_maxcol;
4356
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004357 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004358}
4359
4360#ifdef DEBUG
4361# undef ENABLE_LOG
4362#endif