blob: dbc2902adae77f2b0b7795e702f16e73dcd7d837 [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 Moolenaar428e9872013-05-30 17:05:39 +0200187/* NFA regexp \1 .. \9 encountered. */
188static int nfa_has_backref;
189
Bram Moolenaar963fee22013-05-26 21:47:28 +0200190/* Number of sub expressions actually being used during execution. 1 if only
191 * the whole match (subexpr 0) is used. */
192static int nfa_nsubexpr;
193
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200194static int *post_start; /* holds the postfix form of r.e. */
195static int *post_end;
196static int *post_ptr;
197
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200198static int nstate; /* Number of states in the NFA. Also used when
199 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200200static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200201
202
203static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
204static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
205static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200206static int nfa_regatom __ARGS((void));
207static int nfa_regpiece __ARGS((void));
208static int nfa_regconcat __ARGS((void));
209static int nfa_regbranch __ARGS((void));
210static int nfa_reg __ARGS((int paren));
211#ifdef DEBUG
212static void nfa_set_code __ARGS((int c));
213static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200214static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
215static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200216static void nfa_dump __ARGS((nfa_regprog_T *prog));
217#endif
218static int *re2post __ARGS((void));
219static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
220static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
221static int check_char_class __ARGS((int class, int c));
222static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200223static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
224static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200225static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
226static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200227static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200228static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
229static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
230static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
231static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
232static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
233
234/* helper functions used when doing re2post() ... regatom() parsing */
235#define EMIT(c) do { \
236 if (post_ptr >= post_end) \
237 return FAIL; \
238 *post_ptr++ = c; \
239 } while (0)
240
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200241/*
242 * Initialize internal variables before NFA compilation.
243 * Return OK on success, FAIL otherwise.
244 */
245 static int
246nfa_regcomp_start(expr, re_flags)
247 char_u *expr;
248 int re_flags; /* see vim_regcomp() */
249{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200250 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200251 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
253 nstate = 0;
254 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200255 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200256 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200258 /* Some items blow up in size, such as [A-z]. Add more space for that.
259 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200260 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200261
262 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200263 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265 post_start = (int *)lalloc(postfix_size, TRUE);
266 if (post_start == NULL)
267 return FAIL;
268 vim_memset(post_start, 0, postfix_size);
269 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200270 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200272 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273
274 regcomp_start(expr, re_flags);
275
276 return OK;
277}
278
279/*
280 * Search between "start" and "end" and try to recognize a
281 * character class in expanded form. For example [0-9].
282 * On success, return the id the character class to be emitted.
283 * On failure, return 0 (=FAIL)
284 * Start points to the first char of the range, while end should point
285 * to the closing brace.
286 */
287 static int
288nfa_recognize_char_class(start, end, extra_newl)
289 char_u *start;
290 char_u *end;
291 int extra_newl;
292{
293 int i;
294 /* Each of these variables takes up a char in "config[]",
295 * in the order they are here. */
296 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
297 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
298 char_u *p;
299#define NCONFIGS 16
300 int classid[NCONFIGS] = {
301 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
302 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
303 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
304 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
305 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200306 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307 char_u config[NCONFIGS][9] = {
308 "000000100", /* digit */
309 "100000100", /* non digit */
310 "011000100", /* hex-digit */
311 "111000100", /* non hex-digit */
312 "000001000", /* octal-digit */
313 "100001000", /* [^0-7] */
314 "000110110", /* [0-9A-Za-z_] */
315 "100110110", /* [^0-9A-Za-z_] */
316 "000110010", /* head of word */
317 "100110010", /* not head of word */
318 "000110000", /* alphabetic char a-z */
319 "100110000", /* non alphabetic char */
320 "000100000", /* lowercase letter */
321 "100100000", /* non lowercase */
322 "000010000", /* uppercase */
323 "100010000" /* non uppercase */
324 };
325
326 if (extra_newl == TRUE)
327 newl = TRUE;
328
329 if (*end != ']')
330 return FAIL;
331 p = start;
332 if (*p == '^')
333 {
334 not = TRUE;
335 p ++;
336 }
337
338 while (p < end)
339 {
340 if (p + 2 < end && *(p + 1) == '-')
341 {
342 switch (*p)
343 {
344 case '0':
345 if (*(p + 2) == '9')
346 {
347 o9 = TRUE;
348 break;
349 }
350 else
351 if (*(p + 2) == '7')
352 {
353 o7 = TRUE;
354 break;
355 }
356 case 'a':
357 if (*(p + 2) == 'z')
358 {
359 az = TRUE;
360 break;
361 }
362 else
363 if (*(p + 2) == 'f')
364 {
365 af = TRUE;
366 break;
367 }
368 case 'A':
369 if (*(p + 2) == 'Z')
370 {
371 AZ = TRUE;
372 break;
373 }
374 else
375 if (*(p + 2) == 'F')
376 {
377 AF = TRUE;
378 break;
379 }
380 /* FALLTHROUGH */
381 default:
382 return FAIL;
383 }
384 p += 3;
385 }
386 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
387 {
388 newl = TRUE;
389 p += 2;
390 }
391 else if (*p == '_')
392 {
393 underscore = TRUE;
394 p ++;
395 }
396 else if (*p == '\n')
397 {
398 newl = TRUE;
399 p ++;
400 }
401 else
402 return FAIL;
403 } /* while (p < end) */
404
405 if (p != end)
406 return FAIL;
407
408 /* build the config that represents the ranges we gathered */
409 STRCPY(myconfig, "000000000");
410 if (not == TRUE)
411 myconfig[0] = '1';
412 if (af == TRUE)
413 myconfig[1] = '1';
414 if (AF == TRUE)
415 myconfig[2] = '1';
416 if (az == TRUE)
417 myconfig[3] = '1';
418 if (AZ == TRUE)
419 myconfig[4] = '1';
420 if (o7 == TRUE)
421 myconfig[5] = '1';
422 if (o9 == TRUE)
423 myconfig[6] = '1';
424 if (underscore == TRUE)
425 myconfig[7] = '1';
426 if (newl == TRUE)
427 {
428 myconfig[8] = '1';
429 extra_newl = ADD_NL;
430 }
431 /* try to recognize character classes */
432 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200433 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200434 return classid[i] + extra_newl;
435
436 /* fallthrough => no success so far */
437 return FAIL;
438
439#undef NCONFIGS
440}
441
442/*
443 * Produce the bytes for equivalence class "c".
444 * Currently only handles latin1, latin9 and utf-8.
445 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
446 * equivalent to 'a OR b OR c'
447 *
448 * NOTE! When changing this function, also update reg_equi_class()
449 */
450 static int
451nfa_emit_equi_class(c, neg)
452 int c;
453 int neg;
454{
455 int first = TRUE;
456 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
457#define EMIT2(c) \
458 EMIT(c); \
459 if (neg == TRUE) { \
460 EMIT(NFA_NOT); \
461 } \
462 if (first == FALSE) \
463 EMIT(glue); \
464 else \
465 first = FALSE; \
466
467#ifdef FEAT_MBYTE
468 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
469 || STRCMP(p_enc, "iso-8859-15") == 0)
470#endif
471 {
472 switch (c)
473 {
474 case 'A': case '\300': case '\301': case '\302':
475 case '\303': case '\304': case '\305':
476 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
477 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
478 EMIT2('\305');
479 return OK;
480
481 case 'C': case '\307':
482 EMIT2('C'); EMIT2('\307');
483 return OK;
484
485 case 'E': case '\310': case '\311': case '\312': case '\313':
486 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
487 EMIT2('\312'); EMIT2('\313');
488 return OK;
489
490 case 'I': case '\314': case '\315': case '\316': case '\317':
491 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
492 EMIT2('\316'); EMIT2('\317');
493 return OK;
494
495 case 'N': case '\321':
496 EMIT2('N'); EMIT2('\321');
497 return OK;
498
499 case 'O': case '\322': case '\323': case '\324': case '\325':
500 case '\326':
501 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
502 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
503 return OK;
504
505 case 'U': case '\331': case '\332': case '\333': case '\334':
506 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
507 EMIT2('\333'); EMIT2('\334');
508 return OK;
509
510 case 'Y': case '\335':
511 EMIT2('Y'); EMIT2('\335');
512 return OK;
513
514 case 'a': case '\340': case '\341': case '\342':
515 case '\343': case '\344': case '\345':
516 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
517 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
518 EMIT2('\345');
519 return OK;
520
521 case 'c': case '\347':
522 EMIT2('c'); EMIT2('\347');
523 return OK;
524
525 case 'e': case '\350': case '\351': case '\352': case '\353':
526 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
527 EMIT2('\352'); EMIT2('\353');
528 return OK;
529
530 case 'i': case '\354': case '\355': case '\356': case '\357':
531 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
532 EMIT2('\356'); EMIT2('\357');
533 return OK;
534
535 case 'n': case '\361':
536 EMIT2('n'); EMIT2('\361');
537 return OK;
538
539 case 'o': case '\362': case '\363': case '\364': case '\365':
540 case '\366':
541 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
542 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
543 return OK;
544
545 case 'u': case '\371': case '\372': case '\373': case '\374':
546 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
547 EMIT2('\373'); EMIT2('\374');
548 return OK;
549
550 case 'y': case '\375': case '\377':
551 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
552 return OK;
553
554 default:
555 return FAIL;
556 }
557 }
558
559 EMIT(c);
560 return OK;
561#undef EMIT2
562}
563
564/*
565 * Code to parse regular expression.
566 *
567 * We try to reuse parsing functions in regexp.c to
568 * minimize surprise and keep the syntax consistent.
569 */
570
571/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200572 * Parse the lowest level.
573 *
574 * An atom can be one of a long list of items. Many atoms match one character
575 * in the text. It is often an ordinary character or a character class.
576 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
577 * is only for syntax highlighting.
578 *
579 * atom ::= ordinary-atom
580 * or \( pattern \)
581 * or \%( pattern \)
582 * or \z( pattern \)
583 */
584 static int
585nfa_regatom()
586{
587 int c;
588 int charclass;
589 int equiclass;
590 int collclass;
591 int got_coll_char;
592 char_u *p;
593 char_u *endp;
594#ifdef FEAT_MBYTE
595 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200596#endif
597 int extra = 0;
598 int first;
599 int emit_range;
600 int negated;
601 int result;
602 int startc = -1;
603 int endc = -1;
604 int oldstartc = -1;
605 int cpo_lit; /* 'cpoptions' contains 'l' flag */
606 int cpo_bsl; /* 'cpoptions' contains '\' flag */
607 int glue; /* ID that will "glue" nodes together */
608
609 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
610 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
611
612 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200613 switch (c)
614 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200615 case NUL:
616 syntax_error = TRUE;
617 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
618
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200619 case Magic('^'):
620 EMIT(NFA_BOL);
621 break;
622
623 case Magic('$'):
624 EMIT(NFA_EOL);
625#if defined(FEAT_SYN_HL) || defined(PROTO)
626 had_eol = TRUE;
627#endif
628 break;
629
630 case Magic('<'):
631 EMIT(NFA_BOW);
632 break;
633
634 case Magic('>'):
635 EMIT(NFA_EOW);
636 break;
637
638 case Magic('_'):
639 c = no_Magic(getchr());
640 if (c == '^') /* "\_^" is start-of-line */
641 {
642 EMIT(NFA_BOL);
643 break;
644 }
645 if (c == '$') /* "\_$" is end-of-line */
646 {
647 EMIT(NFA_EOL);
648#if defined(FEAT_SYN_HL) || defined(PROTO)
649 had_eol = TRUE;
650#endif
651 break;
652 }
653
654 extra = ADD_NL;
655
656 /* "\_[" is collection plus newline */
657 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200658 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200659
660 /* "\_x" is character class plus newline */
661 /*FALLTHROUGH*/
662
663 /*
664 * Character classes.
665 */
666 case Magic('.'):
667 case Magic('i'):
668 case Magic('I'):
669 case Magic('k'):
670 case Magic('K'):
671 case Magic('f'):
672 case Magic('F'):
673 case Magic('p'):
674 case Magic('P'):
675 case Magic('s'):
676 case Magic('S'):
677 case Magic('d'):
678 case Magic('D'):
679 case Magic('x'):
680 case Magic('X'):
681 case Magic('o'):
682 case Magic('O'):
683 case Magic('w'):
684 case Magic('W'):
685 case Magic('h'):
686 case Magic('H'):
687 case Magic('a'):
688 case Magic('A'):
689 case Magic('l'):
690 case Magic('L'):
691 case Magic('u'):
692 case Magic('U'):
693 p = vim_strchr(classchars, no_Magic(c));
694 if (p == NULL)
695 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200696 EMSGN("INTERNAL: Unknown character class char: %ld", c);
697 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 }
699#ifdef FEAT_MBYTE
700 /* When '.' is followed by a composing char ignore the dot, so that
701 * the composing char is matched here. */
702 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
703 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200704 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200705 c = getchr();
706 goto nfa_do_multibyte;
707 }
708#endif
709 EMIT(nfa_classcodes[p - classchars]);
710 if (extra == ADD_NL)
711 {
712 EMIT(NFA_NEWL);
713 EMIT(NFA_OR);
714 regflags |= RF_HASNL;
715 }
716 break;
717
718 case Magic('n'):
719 if (reg_string)
720 /* In a string "\n" matches a newline character. */
721 EMIT(NL);
722 else
723 {
724 /* In buffer text "\n" matches the end of a line. */
725 EMIT(NFA_NEWL);
726 regflags |= RF_HASNL;
727 }
728 break;
729
730 case Magic('('):
731 if (nfa_reg(REG_PAREN) == FAIL)
732 return FAIL; /* cascaded error */
733 break;
734
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735 case Magic('|'):
736 case Magic('&'):
737 case Magic(')'):
738 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200739 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200740 return FAIL;
741
742 case Magic('='):
743 case Magic('?'):
744 case Magic('+'):
745 case Magic('@'):
746 case Magic('*'):
747 case Magic('{'):
748 /* these should follow an atom, not form an atom */
749 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200750 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200751 return FAIL;
752
753 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200754 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200755 return FAIL;
756
Bram Moolenaar428e9872013-05-30 17:05:39 +0200757 case Magic('1'):
758 case Magic('2'):
759 case Magic('3'):
760 case Magic('4'):
761 case Magic('5'):
762 case Magic('6'):
763 case Magic('7'):
764 case Magic('8'):
765 case Magic('9'):
766 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
767 nfa_has_backref = TRUE;
768 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200769
770 case Magic('z'):
771 c = no_Magic(getchr());
772 switch (c)
773 {
774 case 's':
775 EMIT(NFA_ZSTART);
776 break;
777 case 'e':
778 EMIT(NFA_ZEND);
779 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200780 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200781 case '1':
782 case '2':
783 case '3':
784 case '4':
785 case '5':
786 case '6':
787 case '7':
788 case '8':
789 case '9':
790 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200791 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200792 return FAIL;
793 default:
794 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200795 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200796 no_Magic(c));
797 return FAIL;
798 }
799 break;
800
801 case Magic('%'):
802 c = no_Magic(getchr());
803 switch (c)
804 {
805 /* () without a back reference */
806 case '(':
807 if (nfa_reg(REG_NPAREN) == FAIL)
808 return FAIL;
809 EMIT(NFA_NOPEN);
810 break;
811
812 case 'd': /* %d123 decimal */
813 case 'o': /* %o123 octal */
814 case 'x': /* %xab hex 2 */
815 case 'u': /* %uabcd hex 4 */
816 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200817 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200818 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200819
Bram Moolenaar47196582013-05-25 22:04:23 +0200820 switch (c)
821 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200822 case 'd': nr = getdecchrs(); break;
823 case 'o': nr = getoctchrs(); break;
824 case 'x': nr = gethexchrs(2); break;
825 case 'u': nr = gethexchrs(4); break;
826 case 'U': nr = gethexchrs(8); break;
827 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200828 }
829
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200830 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200831 EMSG2_RET_FAIL(
832 _("E678: Invalid character after %s%%[dxouU]"),
833 reg_magic == MAGIC_ALL);
834 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200835 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200836 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200837 break;
838
839 /* Catch \%^ and \%$ regardless of where they appear in the
840 * pattern -- regardless of whether or not it makes sense. */
841 case '^':
842 EMIT(NFA_BOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200843 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200844 return FAIL;
845 break;
846
847 case '$':
848 EMIT(NFA_EOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200849 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200850 return FAIL;
851 break;
852
853 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200854 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200855 break;
856
857 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200858 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200859 return FAIL;
860 break;
861
862 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200863 /* TODO: \%[abc] not supported yet */
864 return FAIL;
865
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200866 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200867 {
868 long_u n = 0;
869 int cmp = c;
870
871 if (c == '<' || c == '>')
872 c = getchr();
873 while (VIM_ISDIGIT(c))
874 {
875 n = n * 10 + (c - '0');
876 c = getchr();
877 }
878 if (c == 'l' || c == 'c' || c == 'v')
879 {
880 EMIT(n);
881 if (c == 'l')
882 EMIT(cmp == '<' ? NFA_LNUM_LT :
883 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
884 else if (c == 'c')
885 EMIT(cmp == '<' ? NFA_COL_LT :
886 cmp == '>' ? NFA_COL_GT : NFA_COL);
887 else
888 EMIT(cmp == '<' ? NFA_VCOL_LT :
889 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
890 break;
891 }
892 else if (c == '\'')
893 /* TODO: \%'m not supported yet */
894 return FAIL;
895 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200896 syntax_error = TRUE;
897 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
898 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200899 return FAIL;
900 }
901 break;
902
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200903 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200904collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200905 /*
906 * Glue is emitted between several atoms from the [].
907 * It is either NFA_OR, or NFA_CONCAT.
908 *
909 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
910 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
911 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
912 * notation)
913 *
914 */
915
916
917/* Emit negation atoms, if needed.
918 * The CONCAT below merges the NOT with the previous node. */
919#define TRY_NEG() \
920 if (negated == TRUE) \
921 { \
922 EMIT(NFA_NOT); \
923 }
924
925/* Emit glue between important nodes : CONCAT or OR. */
926#define EMIT_GLUE() \
927 if (first == FALSE) \
928 EMIT(glue); \
929 else \
930 first = FALSE;
931
932 p = regparse;
933 endp = skip_anyof(p);
934 if (*endp == ']')
935 {
936 /*
937 * Try to reverse engineer character classes. For example,
938 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
939 * and perform the necessary substitutions in the NFA.
940 */
941 result = nfa_recognize_char_class(regparse, endp,
942 extra == ADD_NL);
943 if (result != FAIL)
944 {
945 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
946 EMIT(result);
947 else /* must be char class + newline */
948 {
949 EMIT(result - ADD_NL);
950 EMIT(NFA_NEWL);
951 EMIT(NFA_OR);
952 }
953 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200954 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200955 return OK;
956 }
957 /*
958 * Failed to recognize a character class. Use the simple
959 * version that turns [abc] into 'a' OR 'b' OR 'c'
960 */
961 startc = endc = oldstartc = -1;
962 first = TRUE; /* Emitting first atom in this sequence? */
963 negated = FALSE;
964 glue = NFA_OR;
965 if (*regparse == '^') /* negated range */
966 {
967 negated = TRUE;
968 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200969 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200970 }
971 if (*regparse == '-')
972 {
973 startc = '-';
974 EMIT(startc);
975 TRY_NEG();
976 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +0200977 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200978 }
979 /* Emit the OR branches for each character in the [] */
980 emit_range = FALSE;
981 while (regparse < endp)
982 {
983 oldstartc = startc;
984 startc = -1;
985 got_coll_char = FALSE;
986 if (*regparse == '[')
987 {
988 /* Check for [: :], [= =], [. .] */
989 equiclass = collclass = 0;
990 charclass = get_char_class(&regparse);
991 if (charclass == CLASS_NONE)
992 {
993 equiclass = get_equi_class(&regparse);
994 if (equiclass == 0)
995 collclass = get_coll_element(&regparse);
996 }
997
998 /* Character class like [:alpha:] */
999 if (charclass != CLASS_NONE)
1000 {
1001 switch (charclass)
1002 {
1003 case CLASS_ALNUM:
1004 EMIT(NFA_CLASS_ALNUM);
1005 break;
1006 case CLASS_ALPHA:
1007 EMIT(NFA_CLASS_ALPHA);
1008 break;
1009 case CLASS_BLANK:
1010 EMIT(NFA_CLASS_BLANK);
1011 break;
1012 case CLASS_CNTRL:
1013 EMIT(NFA_CLASS_CNTRL);
1014 break;
1015 case CLASS_DIGIT:
1016 EMIT(NFA_CLASS_DIGIT);
1017 break;
1018 case CLASS_GRAPH:
1019 EMIT(NFA_CLASS_GRAPH);
1020 break;
1021 case CLASS_LOWER:
1022 EMIT(NFA_CLASS_LOWER);
1023 break;
1024 case CLASS_PRINT:
1025 EMIT(NFA_CLASS_PRINT);
1026 break;
1027 case CLASS_PUNCT:
1028 EMIT(NFA_CLASS_PUNCT);
1029 break;
1030 case CLASS_SPACE:
1031 EMIT(NFA_CLASS_SPACE);
1032 break;
1033 case CLASS_UPPER:
1034 EMIT(NFA_CLASS_UPPER);
1035 break;
1036 case CLASS_XDIGIT:
1037 EMIT(NFA_CLASS_XDIGIT);
1038 break;
1039 case CLASS_TAB:
1040 EMIT(NFA_CLASS_TAB);
1041 break;
1042 case CLASS_RETURN:
1043 EMIT(NFA_CLASS_RETURN);
1044 break;
1045 case CLASS_BACKSPACE:
1046 EMIT(NFA_CLASS_BACKSPACE);
1047 break;
1048 case CLASS_ESCAPE:
1049 EMIT(NFA_CLASS_ESCAPE);
1050 break;
1051 }
1052 TRY_NEG();
1053 EMIT_GLUE();
1054 continue;
1055 }
1056 /* Try equivalence class [=a=] and the like */
1057 if (equiclass != 0)
1058 {
1059 result = nfa_emit_equi_class(equiclass, negated);
1060 if (result == FAIL)
1061 {
1062 /* should never happen */
1063 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1064 }
1065 EMIT_GLUE();
1066 continue;
1067 }
1068 /* Try collating class like [. .] */
1069 if (collclass != 0)
1070 {
1071 startc = collclass; /* allow [.a.]-x as a range */
1072 /* Will emit the proper atom at the end of the
1073 * while loop. */
1074 }
1075 }
1076 /* Try a range like 'a-x' or '\t-z' */
1077 if (*regparse == '-')
1078 {
1079 emit_range = TRUE;
1080 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001081 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001082 continue; /* reading the end of the range */
1083 }
1084
1085 /* Now handle simple and escaped characters.
1086 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1087 * accepts "\t", "\e", etc., but only when the 'l' flag in
1088 * 'cpoptions' is not included.
1089 * Posix doesn't recognize backslash at all.
1090 */
1091 if (*regparse == '\\'
1092 && !cpo_bsl
1093 && regparse + 1 <= endp
1094 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1095 || (!cpo_lit
1096 && vim_strchr(REGEXP_ABBR, regparse[1])
1097 != NULL)
1098 )
1099 )
1100 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001101 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001102
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001103 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001104 startc = reg_string ? NL : NFA_NEWL;
1105 else
1106 if (*regparse == 'd'
1107 || *regparse == 'o'
1108 || *regparse == 'x'
1109 || *regparse == 'u'
1110 || *regparse == 'U'
1111 )
1112 {
1113 /* TODO(RE) This needs more testing */
1114 startc = coll_get_char();
1115 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001116 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001117 }
1118 else
1119 {
1120 /* \r,\t,\e,\b */
1121 startc = backslash_trans(*regparse);
1122 }
1123 }
1124
1125 /* Normal printable char */
1126 if (startc == -1)
1127#ifdef FEAT_MBYTE
1128 startc = (*mb_ptr2char)(regparse);
1129#else
1130 startc = *regparse;
1131#endif
1132
1133 /* Previous char was '-', so this char is end of range. */
1134 if (emit_range)
1135 {
1136 endc = startc; startc = oldstartc;
1137 if (startc > endc)
1138 EMSG_RET_FAIL(_(e_invrange));
1139#ifdef FEAT_MBYTE
1140 if (has_mbyte && ((*mb_char2len)(startc) > 1
1141 || (*mb_char2len)(endc) > 1))
1142 {
1143 if (endc > startc + 256)
1144 EMSG_RET_FAIL(_(e_invrange));
1145 /* Emit the range. "startc" was already emitted, so
1146 * skip it. */
1147 for (c = startc + 1; c <= endc; c++)
1148 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001149 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001150 TRY_NEG();
1151 EMIT_GLUE();
1152 }
1153 emit_range = FALSE;
1154 }
1155 else
1156#endif
1157 {
1158#ifdef EBCDIC
1159 int alpha_only = FALSE;
1160
1161 /* for alphabetical range skip the gaps
1162 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1163 if (isalpha(startc) && isalpha(endc))
1164 alpha_only = TRUE;
1165#endif
1166 /* Emit the range. "startc" was already emitted, so
1167 * skip it. */
1168 for (c = startc + 1; c <= endc; c++)
1169#ifdef EBCDIC
1170 if (!alpha_only || isalpha(startc))
1171#endif
1172 {
1173 EMIT(c);
1174 TRY_NEG();
1175 EMIT_GLUE();
1176 }
1177 emit_range = FALSE;
1178 }
1179 }
1180 else
1181 {
1182 /*
1183 * This char (startc) is not part of a range. Just
1184 * emit it.
1185 *
1186 * Normally, simply emit startc. But if we get char
1187 * code=0 from a collating char, then replace it with
1188 * 0x0a.
1189 *
1190 * This is needed to completely mimic the behaviour of
1191 * the backtracking engine.
1192 */
1193 if (got_coll_char == TRUE && startc == 0)
1194 EMIT(0x0a);
1195 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001196 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001197 TRY_NEG();
1198 EMIT_GLUE();
1199 }
1200
Bram Moolenaar51a29832013-05-28 22:30:35 +02001201 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001202 } /* while (p < endp) */
1203
Bram Moolenaar51a29832013-05-28 22:30:35 +02001204 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205 if (*regparse == '-') /* if last, '-' is just a char */
1206 {
1207 EMIT('-');
1208 TRY_NEG();
1209 EMIT_GLUE();
1210 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001211 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 /* skip the trailing ] */
1214 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001215 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001216 if (negated == TRUE)
1217 {
1218 /* Mark end of negated char range */
1219 EMIT(NFA_END_NEG_RANGE);
1220 EMIT(NFA_CONCAT);
1221 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001222
1223 /* \_[] also matches \n but it's not negated */
1224 if (extra == ADD_NL)
1225 {
1226 EMIT(reg_string ? NL : NFA_NEWL);
1227 EMIT(NFA_OR);
1228 }
1229
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001230 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001231 } /* if exists closing ] */
1232
1233 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 {
1235 syntax_error = TRUE;
1236 EMSG_RET_FAIL(_(e_missingbracket));
1237 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001238 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001239
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001240 default:
1241 {
1242#ifdef FEAT_MBYTE
1243 int plen;
1244
1245nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001246 /* plen is length of current char with composing chars */
1247 if (enc_utf8 && ((*mb_char2len)(c)
1248 != (plen = (*mb_ptr2len)(old_regparse))
1249 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001250 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001251 int i = 0;
1252
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001253 /* A base character plus composing characters, or just one
1254 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001255 * This requires creating a separate atom as if enclosing
1256 * the characters in (), where NFA_COMPOSING is the ( and
1257 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001258 * building the postfix form, not the NFA itself;
1259 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001260 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001261 for (;;)
1262 {
1263 EMIT(c);
1264 if (i > 0)
1265 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001266 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001267 break;
1268 c = utf_ptr2char(old_regparse + i);
1269 }
1270 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271 regparse = old_regparse + plen;
1272 }
1273 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001274#endif
1275 {
1276 c = no_Magic(c);
1277 EMIT(c);
1278 }
1279 return OK;
1280 }
1281 }
1282
1283#undef TRY_NEG
1284#undef EMIT_GLUE
1285
1286 return OK;
1287}
1288
1289/*
1290 * Parse something followed by possible [*+=].
1291 *
1292 * A piece is an atom, possibly followed by a multi, an indication of how many
1293 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1294 * characters: "", "a", "aa", etc.
1295 *
1296 * piece ::= atom
1297 * or atom multi
1298 */
1299 static int
1300nfa_regpiece()
1301{
1302 int i;
1303 int op;
1304 int ret;
1305 long minval, maxval;
1306 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1307 char_u *old_regparse, *new_regparse;
1308 int c2;
1309 int *old_post_ptr, *my_post_start;
1310 int old_regnpar;
1311 int quest;
1312
1313 /* Save the current position in the regexp, so that we can use it if
1314 * <atom>{m,n} is next. */
1315 old_regparse = regparse;
1316 /* Save current number of open parenthesis, so we can use it if
1317 * <atom>{m,n} is next */
1318 old_regnpar = regnpar;
1319 /* store current pos in the postfix form, for \{m,n} involving 0s */
1320 my_post_start = post_ptr;
1321
1322 ret = nfa_regatom();
1323 if (ret == FAIL)
1324 return FAIL; /* cascaded error */
1325
1326 op = peekchr();
1327 if (re_multi_type(op) == NOT_MULTI)
1328 return OK;
1329
1330 skipchr();
1331 switch (op)
1332 {
1333 case Magic('*'):
1334 EMIT(NFA_STAR);
1335 break;
1336
1337 case Magic('+'):
1338 /*
1339 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1340 * first and only submatch would be "aaa". But the backtracking
1341 * engine interprets the plus as "try matching one more time", and
1342 * a* matches a second time at the end of the input, the empty
1343 * string.
1344 * The submatch will the empty string.
1345 *
1346 * In order to be consistent with the old engine, we disable
1347 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1348 */
1349 /* EMIT(NFA_PLUS); */
1350 regnpar = old_regnpar;
1351 regparse = old_regparse;
1352 curchr = -1;
1353 if (nfa_regatom() == FAIL)
1354 return FAIL;
1355 EMIT(NFA_STAR);
1356 EMIT(NFA_CONCAT);
1357 skipchr(); /* skip the \+ */
1358 break;
1359
1360 case Magic('@'):
1361 op = no_Magic(getchr());
1362 switch(op)
1363 {
1364 case '=':
1365 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1366 break;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001367 case '0':
1368 case '1':
1369 case '2':
1370 case '3':
1371 case '4':
1372 case '5':
1373 case '6':
1374 case '7':
1375 case '8':
1376 case '9':
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377 case '!':
1378 case '<':
1379 case '>':
1380 /* Not supported yet */
1381 return FAIL;
1382 default:
1383 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001384 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385 return FAIL;
1386 }
1387 break;
1388
1389 case Magic('?'):
1390 case Magic('='):
1391 EMIT(NFA_QUEST);
1392 break;
1393
1394 case Magic('{'):
1395 /* a{2,5} will expand to 'aaa?a?a?'
1396 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1397 * version of '?'
1398 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1399 * parenthesis have the same id
1400 */
1401
1402 greedy = TRUE;
1403 c2 = peekchr();
1404 if (c2 == '-' || c2 == Magic('-'))
1405 {
1406 skipchr();
1407 greedy = FALSE;
1408 }
1409 if (!read_limits(&minval, &maxval))
1410 {
1411 syntax_error = TRUE;
1412 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1413 }
1414 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1415 * <atom>* */
1416 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1417 {
1418 EMIT(NFA_STAR);
1419 break;
1420 }
1421
1422 if (maxval > NFA_BRACES_MAXLIMIT)
1423 {
1424 /* This would yield a huge automaton and use too much memory.
1425 * Revert to old engine */
1426 return FAIL;
1427 }
1428
1429 /* Special case: x{0} or x{-0} */
1430 if (maxval == 0)
1431 {
1432 /* Ignore result of previous call to nfa_regatom() */
1433 post_ptr = my_post_start;
1434 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1435 EMIT(NFA_SKIP_CHAR);
1436 return OK;
1437 }
1438
1439 /* Ignore previous call to nfa_regatom() */
1440 post_ptr = my_post_start;
1441 /* Save pos after the repeated atom and the \{} */
1442 new_regparse = regparse;
1443
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1445 for (i = 0; i < maxval; i++)
1446 {
1447 /* Goto beginning of the repeated atom */
1448 regparse = old_regparse;
1449 curchr = -1;
1450 /* Restore count of parenthesis */
1451 regnpar = old_regnpar;
1452 old_post_ptr = post_ptr;
1453 if (nfa_regatom() == FAIL)
1454 return FAIL;
1455 /* after "minval" times, atoms are optional */
1456 if (i + 1 > minval)
1457 EMIT(quest);
1458 if (old_post_ptr != my_post_start)
1459 EMIT(NFA_CONCAT);
1460 }
1461
1462 /* Go to just after the repeated atom and the \{} */
1463 regparse = new_regparse;
1464 curchr = -1;
1465
1466 break;
1467
1468
1469 default:
1470 break;
1471 } /* end switch */
1472
1473 if (re_multi_type(peekchr()) != NOT_MULTI)
1474 {
1475 /* Can't have a multi follow a multi. */
1476 syntax_error = TRUE;
1477 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1478 }
1479
1480 return OK;
1481}
1482
1483/*
1484 * Parse one or more pieces, concatenated. It matches a match for the
1485 * first piece, followed by a match for the second piece, etc. Example:
1486 * "f[0-9]b", first matches "f", then a digit and then "b".
1487 *
1488 * concat ::= piece
1489 * or piece piece
1490 * or piece piece piece
1491 * etc.
1492 */
1493 static int
1494nfa_regconcat()
1495{
1496 int cont = TRUE;
1497 int first = TRUE;
1498
1499 while (cont)
1500 {
1501 switch (peekchr())
1502 {
1503 case NUL:
1504 case Magic('|'):
1505 case Magic('&'):
1506 case Magic(')'):
1507 cont = FALSE;
1508 break;
1509
1510 case Magic('Z'):
1511#ifdef FEAT_MBYTE
1512 regflags |= RF_ICOMBINE;
1513#endif
1514 skipchr_keepstart();
1515 break;
1516 case Magic('c'):
1517 regflags |= RF_ICASE;
1518 skipchr_keepstart();
1519 break;
1520 case Magic('C'):
1521 regflags |= RF_NOICASE;
1522 skipchr_keepstart();
1523 break;
1524 case Magic('v'):
1525 reg_magic = MAGIC_ALL;
1526 skipchr_keepstart();
1527 curchr = -1;
1528 break;
1529 case Magic('m'):
1530 reg_magic = MAGIC_ON;
1531 skipchr_keepstart();
1532 curchr = -1;
1533 break;
1534 case Magic('M'):
1535 reg_magic = MAGIC_OFF;
1536 skipchr_keepstart();
1537 curchr = -1;
1538 break;
1539 case Magic('V'):
1540 reg_magic = MAGIC_NONE;
1541 skipchr_keepstart();
1542 curchr = -1;
1543 break;
1544
1545 default:
1546 if (nfa_regpiece() == FAIL)
1547 return FAIL;
1548 if (first == FALSE)
1549 EMIT(NFA_CONCAT);
1550 else
1551 first = FALSE;
1552 break;
1553 }
1554 }
1555
1556 return OK;
1557}
1558
1559/*
1560 * Parse a branch, one or more concats, separated by "\&". It matches the
1561 * last concat, but only if all the preceding concats also match at the same
1562 * position. Examples:
1563 * "foobeep\&..." matches "foo" in "foobeep".
1564 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1565 *
1566 * branch ::= concat
1567 * or concat \& concat
1568 * or concat \& concat \& concat
1569 * etc.
1570 */
1571 static int
1572nfa_regbranch()
1573{
1574 int ch;
1575 int *old_post_ptr;
1576
1577 old_post_ptr = post_ptr;
1578
1579 /* First branch, possibly the only one */
1580 if (nfa_regconcat() == FAIL)
1581 return FAIL;
1582
1583 ch = peekchr();
1584 /* Try next concats */
1585 while (ch == Magic('&'))
1586 {
1587 skipchr();
1588 EMIT(NFA_NOPEN);
1589 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1590 old_post_ptr = post_ptr;
1591 if (nfa_regconcat() == FAIL)
1592 return FAIL;
1593 /* if concat is empty, skip a input char. But do emit a node */
1594 if (old_post_ptr == post_ptr)
1595 EMIT(NFA_SKIP_CHAR);
1596 EMIT(NFA_CONCAT);
1597 ch = peekchr();
1598 }
1599
1600 /* Even if a branch is empty, emit one node for it */
1601 if (old_post_ptr == post_ptr)
1602 EMIT(NFA_SKIP_CHAR);
1603
1604 return OK;
1605}
1606
1607/*
1608 * Parse a pattern, one or more branches, separated by "\|". It matches
1609 * anything that matches one of the branches. Example: "foo\|beep" matches
1610 * "foo" and matches "beep". If more than one branch matches, the first one
1611 * is used.
1612 *
1613 * pattern ::= branch
1614 * or branch \| branch
1615 * or branch \| branch \| branch
1616 * etc.
1617 */
1618 static int
1619nfa_reg(paren)
1620 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1621{
1622 int parno = 0;
1623
1624#ifdef FEAT_SYN_HL
1625#endif
1626 if (paren == REG_PAREN)
1627 {
1628 if (regnpar >= NSUBEXP) /* Too many `(' */
1629 {
1630 syntax_error = TRUE;
1631 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1632 }
1633 parno = regnpar++;
1634 }
1635
1636 if (nfa_regbranch() == FAIL)
1637 return FAIL; /* cascaded error */
1638
1639 while (peekchr() == Magic('|'))
1640 {
1641 skipchr();
1642 if (nfa_regbranch() == FAIL)
1643 return FAIL; /* cascaded error */
1644 EMIT(NFA_OR);
1645 }
1646
1647 /* Check for proper termination. */
1648 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1649 {
1650 syntax_error = TRUE;
1651 if (paren == REG_NPAREN)
1652 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1653 else
1654 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1655 }
1656 else if (paren == REG_NOPAREN && peekchr() != NUL)
1657 {
1658 syntax_error = TRUE;
1659 if (peekchr() == Magic(')'))
1660 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1661 else
1662 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1663 }
1664 /*
1665 * Here we set the flag allowing back references to this set of
1666 * parentheses.
1667 */
1668 if (paren == REG_PAREN)
1669 {
1670 had_endbrace[parno] = TRUE; /* have seen the close paren */
1671 EMIT(NFA_MOPEN + parno);
1672 }
1673
1674 return OK;
1675}
1676
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677#ifdef DEBUG
1678static char_u code[50];
1679
1680 static void
1681nfa_set_code(c)
1682 int c;
1683{
1684 int addnl = FALSE;
1685
1686 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1687 {
1688 addnl = TRUE;
1689 c -= ADD_NL;
1690 }
1691
1692 STRCPY(code, "");
1693 switch (c)
1694 {
1695 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1696 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1697 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1698 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1699 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1700 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1701
Bram Moolenaar5714b802013-05-28 22:03:20 +02001702 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1703 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1704 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1705 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1706 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1707 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1708 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1709 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1710 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1711 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1712
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713 case NFA_PREV_ATOM_NO_WIDTH:
1714 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001715 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1716 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001717 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1718 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1719 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1720 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1721
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001722 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1723 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1724
1725 case NFA_MOPEN + 0:
1726 case NFA_MOPEN + 1:
1727 case NFA_MOPEN + 2:
1728 case NFA_MOPEN + 3:
1729 case NFA_MOPEN + 4:
1730 case NFA_MOPEN + 5:
1731 case NFA_MOPEN + 6:
1732 case NFA_MOPEN + 7:
1733 case NFA_MOPEN + 8:
1734 case NFA_MOPEN + 9:
1735 STRCPY(code, "NFA_MOPEN(x)");
1736 code[10] = c - NFA_MOPEN + '0';
1737 break;
1738 case NFA_MCLOSE + 0:
1739 case NFA_MCLOSE + 1:
1740 case NFA_MCLOSE + 2:
1741 case NFA_MCLOSE + 3:
1742 case NFA_MCLOSE + 4:
1743 case NFA_MCLOSE + 5:
1744 case NFA_MCLOSE + 6:
1745 case NFA_MCLOSE + 7:
1746 case NFA_MCLOSE + 8:
1747 case NFA_MCLOSE + 9:
1748 STRCPY(code, "NFA_MCLOSE(x)");
1749 code[11] = c - NFA_MCLOSE + '0';
1750 break;
1751 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1752 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1753 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1754 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1755 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1756 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1757 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1758 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1759 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1760 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1761 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1762 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1763 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1764 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1765 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1766 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1767 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1768 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1769 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1770 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1771 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1772 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1773 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1774 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1775 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1776 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1777 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1778 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1779
1780 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1781 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1782 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1783 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1784 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1785 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1786 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1787 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1788 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1789 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1790 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1791 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1792 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1793 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1794 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1795 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1796 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1797 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1798 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1799 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1800 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1801 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1802 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1803 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1804 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1805 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1806 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1807
1808 default:
1809 STRCPY(code, "CHAR(x)");
1810 code[5] = c;
1811 }
1812
1813 if (addnl == TRUE)
1814 STRCAT(code, " + NEWLINE ");
1815
1816}
1817
1818#ifdef ENABLE_LOG
1819static FILE *log_fd;
1820
1821/*
1822 * Print the postfix notation of the current regexp.
1823 */
1824 static void
1825nfa_postfix_dump(expr, retval)
1826 char_u *expr;
1827 int retval;
1828{
1829 int *p;
1830 FILE *f;
1831
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001832 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001833 if (f != NULL)
1834 {
1835 fprintf(f, "\n-------------------------\n");
1836 if (retval == FAIL)
1837 fprintf(f, ">>> NFA engine failed ... \n");
1838 else if (retval == OK)
1839 fprintf(f, ">>> NFA engine succeeded !\n");
1840 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001841 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001842 {
1843 nfa_set_code(*p);
1844 fprintf(f, "%s, ", code);
1845 }
1846 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001847 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001848 fprintf(f, "%d ", *p);
1849 fprintf(f, "\n\n");
1850 fclose(f);
1851 }
1852}
1853
1854/*
1855 * Print the NFA starting with a root node "state".
1856 */
1857 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001858nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859 FILE *debugf;
1860 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001861{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001862 garray_T indent;
1863
1864 ga_init2(&indent, 1, 64);
1865 ga_append(&indent, '\0');
1866 nfa_print_state2(debugf, state, &indent);
1867 ga_clear(&indent);
1868}
1869
1870 static void
1871nfa_print_state2(debugf, state, indent)
1872 FILE *debugf;
1873 nfa_state_T *state;
1874 garray_T *indent;
1875{
1876 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877
1878 if (state == NULL)
1879 return;
1880
1881 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001882
1883 /* Output indent */
1884 p = (char_u *)indent->ga_data;
1885 if (indent->ga_len >= 3)
1886 {
1887 int last = indent->ga_len - 3;
1888 char_u save[2];
1889
1890 STRNCPY(save, &p[last], 2);
1891 STRNCPY(&p[last], "+-", 2);
1892 fprintf(debugf, " %s", p);
1893 STRNCPY(&p[last], save, 2);
1894 }
1895 else
1896 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897
1898 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001899 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1900 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 if (state->id < 0)
1902 return;
1903
1904 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001905
1906 /* grow indent for state->out */
1907 indent->ga_len -= 1;
1908 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001909 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001910 else
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->out, indent);
1915
1916 /* replace last part of indent for state->out1 */
1917 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001918 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001919 ga_append(indent, '\0');
1920
1921 nfa_print_state2(debugf, state->out1, indent);
1922
1923 /* shrink indent */
1924 indent->ga_len -= 3;
1925 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926}
1927
1928/*
1929 * Print the NFA state machine.
1930 */
1931 static void
1932nfa_dump(prog)
1933 nfa_regprog_T *prog;
1934{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001935 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936
1937 if (debugf != NULL)
1938 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001939 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940 fclose(debugf);
1941 }
1942}
1943#endif /* ENABLE_LOG */
1944#endif /* DEBUG */
1945
1946/*
1947 * Parse r.e. @expr and convert it into postfix form.
1948 * Return the postfix string on success, NULL otherwise.
1949 */
1950 static int *
1951re2post()
1952{
1953 if (nfa_reg(REG_NOPAREN) == FAIL)
1954 return NULL;
1955 EMIT(NFA_MOPEN);
1956 return post_start;
1957}
1958
1959/* NB. Some of the code below is inspired by Russ's. */
1960
1961/*
1962 * Represents an NFA state plus zero or one or two arrows exiting.
1963 * if c == MATCH, no arrows out; matching state.
1964 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1965 * If c < 256, labeled arrow with character c to out.
1966 */
1967
1968static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1969
1970/*
1971 * Allocate and initialize nfa_state_T.
1972 */
1973 static nfa_state_T *
1974new_state(c, out, out1)
1975 int c;
1976 nfa_state_T *out;
1977 nfa_state_T *out1;
1978{
1979 nfa_state_T *s;
1980
1981 if (istate >= nstate)
1982 return NULL;
1983
1984 s = &state_ptr[istate++];
1985
1986 s->c = c;
1987 s->out = out;
1988 s->out1 = out1;
1989
1990 s->id = istate;
1991 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001992 s->negated = FALSE;
1993
1994 return s;
1995}
1996
1997/*
1998 * A partially built NFA without the matching state filled in.
1999 * Frag_T.start points at the start state.
2000 * Frag_T.out is a list of places that need to be set to the
2001 * next state for this fragment.
2002 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002003
2004/* Since the out pointers in the list are always
2005 * uninitialized, we use the pointers themselves
2006 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002008union Ptrlist
2009{
2010 Ptrlist *next;
2011 nfa_state_T *s;
2012};
2013
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014struct Frag
2015{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002016 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002017 Ptrlist *out;
2018};
2019typedef struct Frag Frag_T;
2020
2021static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2022static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2023static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2024static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2025static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2026static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2027
2028/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002029 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002030 */
2031 static Frag_T
2032frag(start, out)
2033 nfa_state_T *start;
2034 Ptrlist *out;
2035{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002036 Frag_T n;
2037
2038 n.start = start;
2039 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040 return n;
2041}
2042
2043/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 * Create singleton list containing just outp.
2045 */
2046 static Ptrlist *
2047list1(outp)
2048 nfa_state_T **outp;
2049{
2050 Ptrlist *l;
2051
2052 l = (Ptrlist *)outp;
2053 l->next = NULL;
2054 return l;
2055}
2056
2057/*
2058 * Patch the list of states at out to point to start.
2059 */
2060 static void
2061patch(l, s)
2062 Ptrlist *l;
2063 nfa_state_T *s;
2064{
2065 Ptrlist *next;
2066
2067 for (; l; l = next)
2068 {
2069 next = l->next;
2070 l->s = s;
2071 }
2072}
2073
2074
2075/*
2076 * Join the two lists l1 and l2, returning the combination.
2077 */
2078 static Ptrlist *
2079append(l1, l2)
2080 Ptrlist *l1;
2081 Ptrlist *l2;
2082{
2083 Ptrlist *oldl1;
2084
2085 oldl1 = l1;
2086 while (l1->next)
2087 l1 = l1->next;
2088 l1->next = l2;
2089 return oldl1;
2090}
2091
2092/*
2093 * Stack used for transforming postfix form into NFA.
2094 */
2095static Frag_T empty;
2096
2097 static void
2098st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002099 int *postfix UNUSED;
2100 int *end UNUSED;
2101 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002102{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002103#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104 FILE *df;
2105 int *p2;
2106
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002107 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002108 if (df)
2109 {
2110 fprintf(df, "Error popping the stack!\n");
2111#ifdef DEBUG
2112 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2113#endif
2114 fprintf(df, "Postfix form is: ");
2115#ifdef DEBUG
2116 for (p2 = postfix; p2 < end; p2++)
2117 {
2118 nfa_set_code(*p2);
2119 fprintf(df, "%s, ", code);
2120 }
2121 nfa_set_code(*p);
2122 fprintf(df, "\nCurrent position is: ");
2123 for (p2 = postfix; p2 <= p; p2 ++)
2124 {
2125 nfa_set_code(*p2);
2126 fprintf(df, "%s, ", code);
2127 }
2128#else
2129 for (p2 = postfix; p2 < end; p2++)
2130 {
2131 fprintf(df, "%d, ", *p2);
2132 }
2133 fprintf(df, "\nCurrent position is: ");
2134 for (p2 = postfix; p2 <= p; p2 ++)
2135 {
2136 fprintf(df, "%d, ", *p2);
2137 }
2138#endif
2139 fprintf(df, "\n--------------------------\n");
2140 fclose(df);
2141 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002142#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002143 EMSG(_("E874: (NFA) Could not pop the stack !"));
2144}
2145
2146/*
2147 * Push an item onto the stack.
2148 */
2149 static void
2150st_push(s, p, stack_end)
2151 Frag_T s;
2152 Frag_T **p;
2153 Frag_T *stack_end;
2154{
2155 Frag_T *stackp = *p;
2156
2157 if (stackp >= stack_end)
2158 return;
2159 *stackp = s;
2160 *p = *p + 1;
2161}
2162
2163/*
2164 * Pop an item from the stack.
2165 */
2166 static Frag_T
2167st_pop(p, stack)
2168 Frag_T **p;
2169 Frag_T *stack;
2170{
2171 Frag_T *stackp;
2172
2173 *p = *p - 1;
2174 stackp = *p;
2175 if (stackp < stack)
2176 return empty;
2177 return **p;
2178}
2179
2180/*
2181 * Convert a postfix form into its equivalent NFA.
2182 * Return the NFA start state on success, NULL otherwise.
2183 */
2184 static nfa_state_T *
2185post2nfa(postfix, end, nfa_calc_size)
2186 int *postfix;
2187 int *end;
2188 int nfa_calc_size;
2189{
2190 int *p;
2191 int mopen;
2192 int mclose;
2193 Frag_T *stack = NULL;
2194 Frag_T *stackp = NULL;
2195 Frag_T *stack_end = NULL;
2196 Frag_T e1;
2197 Frag_T e2;
2198 Frag_T e;
2199 nfa_state_T *s;
2200 nfa_state_T *s1;
2201 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002202 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002203
2204 if (postfix == NULL)
2205 return NULL;
2206
Bram Moolenaar053bb602013-05-20 13:55:21 +02002207#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002208#define POP() st_pop(&stackp, stack); \
2209 if (stackp < stack) \
2210 { \
2211 st_error(postfix, end, p); \
2212 return NULL; \
2213 }
2214
2215 if (nfa_calc_size == FALSE)
2216 {
2217 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002218 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002219 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002220 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221 }
2222
2223 for (p = postfix; p < end; ++p)
2224 {
2225 switch (*p)
2226 {
2227 case NFA_CONCAT:
2228 /* Catenation.
2229 * Pay attention: this operator does not exist
2230 * in the r.e. itself (it is implicit, really).
2231 * It is added when r.e. is translated to postfix
2232 * form in re2post().
2233 *
2234 * No new state added here. */
2235 if (nfa_calc_size == TRUE)
2236 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002237 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002238 break;
2239 }
2240 e2 = POP();
2241 e1 = POP();
2242 patch(e1.out, e2.start);
2243 PUSH(frag(e1.start, e2.out));
2244 break;
2245
2246 case NFA_NOT:
2247 /* Negation of a character */
2248 if (nfa_calc_size == TRUE)
2249 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002250 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002251 break;
2252 }
2253 e1 = POP();
2254 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002255#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002256 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002257 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002258#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259 PUSH(e1);
2260 break;
2261
2262 case NFA_OR:
2263 /* Alternation */
2264 if (nfa_calc_size == TRUE)
2265 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002266 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002267 break;
2268 }
2269 e2 = POP();
2270 e1 = POP();
2271 s = new_state(NFA_SPLIT, e1.start, e2.start);
2272 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002273 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274 PUSH(frag(s, append(e1.out, e2.out)));
2275 break;
2276
2277 case NFA_STAR:
2278 /* Zero or more */
2279 if (nfa_calc_size == TRUE)
2280 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002281 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282 break;
2283 }
2284 e = POP();
2285 s = new_state(NFA_SPLIT, e.start, NULL);
2286 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002287 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288 patch(e.out, s);
2289 PUSH(frag(s, list1(&s->out1)));
2290 break;
2291
2292 case NFA_QUEST:
2293 /* one or zero atoms=> greedy match */
2294 if (nfa_calc_size == TRUE)
2295 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002296 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002297 break;
2298 }
2299 e = POP();
2300 s = new_state(NFA_SPLIT, e.start, NULL);
2301 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002302 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002303 PUSH(frag(s, append(e.out, list1(&s->out1))));
2304 break;
2305
2306 case NFA_QUEST_NONGREEDY:
2307 /* zero or one atoms => non-greedy match */
2308 if (nfa_calc_size == TRUE)
2309 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002310 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002311 break;
2312 }
2313 e = POP();
2314 s = new_state(NFA_SPLIT, NULL, e.start);
2315 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002316 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317 PUSH(frag(s, append(e.out, list1(&s->out))));
2318 break;
2319
2320 case NFA_PLUS:
2321 /* One or more */
2322 if (nfa_calc_size == TRUE)
2323 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002324 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325 break;
2326 }
2327 e = POP();
2328 s = new_state(NFA_SPLIT, e.start, NULL);
2329 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002330 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331 patch(e.out, s);
2332 PUSH(frag(e.start, list1(&s->out1)));
2333 break;
2334
2335 case NFA_SKIP_CHAR:
2336 /* Symbol of 0-length, Used in a repetition
2337 * with max/min count of 0 */
2338 if (nfa_calc_size == TRUE)
2339 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002340 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002341 break;
2342 }
2343 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2344 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002345 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002346 PUSH(frag(s, list1(&s->out)));
2347 break;
2348
2349 case NFA_PREV_ATOM_NO_WIDTH:
2350 /* The \@= operator: match the preceding atom with 0 width.
2351 * Surrounds the preceding atom with START_INVISIBLE and
2352 * END_INVISIBLE, similarly to MOPEN.
2353 */
2354 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002355 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002356
2357 if (nfa_calc_size == TRUE)
2358 {
2359 nstate += 2;
2360 break;
2361 }
2362 e = POP();
2363 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2364 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002365 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002366 patch(e.out, s1);
2367
2368 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2369 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002370 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 PUSH(frag(s, list1(&s1->out)));
2372 break;
2373
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002374#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002375 case NFA_COMPOSING: /* char with composing char */
2376#if 0
2377 /* TODO */
2378 if (regflags & RF_ICOMBINE)
2379 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002380 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002381 }
2382#endif
2383 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002384#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002385
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002386 case NFA_MOPEN + 0: /* Submatch */
2387 case NFA_MOPEN + 1:
2388 case NFA_MOPEN + 2:
2389 case NFA_MOPEN + 3:
2390 case NFA_MOPEN + 4:
2391 case NFA_MOPEN + 5:
2392 case NFA_MOPEN + 6:
2393 case NFA_MOPEN + 7:
2394 case NFA_MOPEN + 8:
2395 case NFA_MOPEN + 9:
2396 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002397 if (nfa_calc_size == TRUE)
2398 {
2399 nstate += 2;
2400 break;
2401 }
2402
2403 mopen = *p;
2404 switch (*p)
2405 {
2406 case NFA_NOPEN:
2407 mclose = NFA_NCLOSE;
2408 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002409#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002410 case NFA_COMPOSING:
2411 mclose = NFA_END_COMPOSING;
2412 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002413#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002414 default:
2415 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2416 mclose = *p + NSUBEXP;
2417 break;
2418 }
2419
2420 /* Allow "NFA_MOPEN" as a valid postfix representation for
2421 * the empty regexp "". In this case, the NFA will be
2422 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2423 * empty groups of parenthesis, and empty mbyte chars */
2424 if (stackp == stack)
2425 {
2426 s = new_state(mopen, NULL, NULL);
2427 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002428 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002429 s1 = new_state(mclose, NULL, NULL);
2430 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002431 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432 patch(list1(&s->out), s1);
2433 PUSH(frag(s, list1(&s1->out)));
2434 break;
2435 }
2436
2437 /* At least one node was emitted before NFA_MOPEN, so
2438 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2439 e = POP();
2440 s = new_state(mopen, e.start, NULL); /* `(' */
2441 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002442 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443
2444 s1 = new_state(mclose, NULL, NULL); /* `)' */
2445 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002446 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447 patch(e.out, s1);
2448
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002449#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002450 if (mopen == NFA_COMPOSING)
2451 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002453#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002454
2455 PUSH(frag(s, list1(&s1->out)));
2456 break;
2457
Bram Moolenaar5714b802013-05-28 22:03:20 +02002458 case NFA_BACKREF1:
2459 case NFA_BACKREF2:
2460 case NFA_BACKREF3:
2461 case NFA_BACKREF4:
2462 case NFA_BACKREF5:
2463 case NFA_BACKREF6:
2464 case NFA_BACKREF7:
2465 case NFA_BACKREF8:
2466 case NFA_BACKREF9:
2467 if (nfa_calc_size == TRUE)
2468 {
2469 nstate += 2;
2470 break;
2471 }
2472 s = new_state(*p, NULL, NULL);
2473 if (s == NULL)
2474 goto theend;
2475 s1 = new_state(NFA_SKIP, NULL, NULL);
2476 if (s1 == NULL)
2477 goto theend;
2478 patch(list1(&s->out), s1);
2479 PUSH(frag(s, list1(&s1->out)));
2480 break;
2481
Bram Moolenaar423532e2013-05-29 21:14:42 +02002482 case NFA_LNUM:
2483 case NFA_LNUM_GT:
2484 case NFA_LNUM_LT:
2485 case NFA_VCOL:
2486 case NFA_VCOL_GT:
2487 case NFA_VCOL_LT:
2488 case NFA_COL:
2489 case NFA_COL_GT:
2490 case NFA_COL_LT:
2491 if (nfa_calc_size == TRUE)
2492 {
2493 nstate += 1;
2494 break;
2495 }
2496 e1 = POP();
2497 s = new_state(*p, NULL, NULL);
2498 if (s == NULL)
2499 goto theend;
2500 s->val = e1.start->c;
2501 PUSH(frag(s, list1(&s->out)));
2502 break;
2503
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504 case NFA_ZSTART:
2505 case NFA_ZEND:
2506 default:
2507 /* Operands */
2508 if (nfa_calc_size == TRUE)
2509 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002510 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002511 break;
2512 }
2513 s = new_state(*p, NULL, NULL);
2514 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002515 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002516 PUSH(frag(s, list1(&s->out)));
2517 break;
2518
2519 } /* switch(*p) */
2520
2521 } /* for(p = postfix; *p; ++p) */
2522
2523 if (nfa_calc_size == TRUE)
2524 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002525 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002526 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002527 }
2528
2529 e = POP();
2530 if (stackp != stack)
2531 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2532
2533 if (istate >= nstate)
2534 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2535
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002536 matchstate = &state_ptr[istate++]; /* the match state */
2537 matchstate->c = NFA_MATCH;
2538 matchstate->out = matchstate->out1 = NULL;
2539
2540 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002541 ret = e.start;
2542
2543theend:
2544 vim_free(stack);
2545 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002546
2547#undef POP1
2548#undef PUSH1
2549#undef POP2
2550#undef PUSH2
2551#undef POP
2552#undef PUSH
2553}
2554
2555/****************************************************************
2556 * NFA execution code.
2557 ****************************************************************/
2558
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002559typedef struct
2560{
2561 int in_use; /* number of subexpr with useful info */
2562
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002563 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002564 union
2565 {
2566 struct multipos
2567 {
2568 lpos_T start;
2569 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002570 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002571 struct linepos
2572 {
2573 char_u *start;
2574 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002575 } line[NSUBEXP];
2576 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002577} regsub_T;
2578
Bram Moolenaar963fee22013-05-26 21:47:28 +02002579/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002580typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002581{
2582 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002583 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002584 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002585} nfa_thread_T;
2586
Bram Moolenaar963fee22013-05-26 21:47:28 +02002587/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002588typedef struct
2589{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002590 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002591 int n; /* nr of states currently in "t" */
2592 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002593 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002594} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002595
Bram Moolenaar5714b802013-05-28 22:03:20 +02002596#ifdef ENABLE_LOG
2597 static void
2598log_subexpr(sub)
2599 regsub_T *sub;
2600{
2601 int j;
2602
2603 for (j = 0; j < sub->in_use; j++)
2604 if (REG_MULTI)
2605 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2606 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002607 sub->list.multi[j].start.col,
2608 (int)sub->list.multi[j].start.lnum,
2609 sub->list.multi[j].end.col,
2610 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002611 else
2612 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2613 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002614 (char *)sub->list.line[j].start,
2615 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002616 fprintf(log_fd, "\n");
2617}
2618#endif
2619
Bram Moolenaar963fee22013-05-26 21:47:28 +02002620/* Used during execution: whether a match has been found. */
2621static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002622
Bram Moolenaar428e9872013-05-30 17:05:39 +02002623static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002624static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2625static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002626
Bram Moolenaar428e9872013-05-30 17:05:39 +02002627/*
2628 * Return TRUE if "sub1" and "sub2" have the same positions.
2629 */
2630 static int
2631sub_equal(sub1, sub2)
2632 regsub_T *sub1;
2633 regsub_T *sub2;
2634{
2635 int i;
2636 int todo;
2637 linenr_T s1, e1;
2638 linenr_T s2, e2;
2639 char_u *sp1, *ep1;
2640 char_u *sp2, *ep2;
2641
2642 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2643 if (REG_MULTI)
2644 {
2645 for (i = 0; i < todo; ++i)
2646 {
2647 if (i < sub1->in_use)
2648 {
2649 s1 = sub1->list.multi[i].start.lnum;
2650 e1 = sub1->list.multi[i].end.lnum;
2651 }
2652 else
2653 {
2654 s1 = 0;
2655 e1 = 0;
2656 }
2657 if (i < sub2->in_use)
2658 {
2659 s2 = sub2->list.multi[i].start.lnum;
2660 e2 = sub2->list.multi[i].end.lnum;
2661 }
2662 else
2663 {
2664 s2 = 0;
2665 e2 = 0;
2666 }
2667 if (s1 != s2 || e1 != e2)
2668 return FALSE;
2669 if (s1 != 0 && sub1->list.multi[i].start.col
2670 != sub2->list.multi[i].start.col)
2671 return FALSE;
2672 if (e1 != 0 && sub1->list.multi[i].end.col
2673 != sub2->list.multi[i].end.col)
2674 return FALSE;
2675 }
2676 }
2677 else
2678 {
2679 for (i = 0; i < todo; ++i)
2680 {
2681 if (i < sub1->in_use)
2682 {
2683 sp1 = sub1->list.line[i].start;
2684 ep1 = sub1->list.line[i].end;
2685 }
2686 else
2687 {
2688 sp1 = NULL;
2689 ep1 = NULL;
2690 }
2691 if (i < sub2->in_use)
2692 {
2693 sp2 = sub2->list.line[i].start;
2694 ep2 = sub2->list.line[i].end;
2695 }
2696 else
2697 {
2698 sp2 = NULL;
2699 ep2 = NULL;
2700 }
2701 if (sp1 != sp2 || ep1 != ep2)
2702 return FALSE;
2703 }
2704 }
2705
2706 return TRUE;
2707}
2708
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002709 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002710addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002711 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002713 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002714 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002716 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002717 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002718 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002719 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002720 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002721 int i;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002722
2723 if (l == NULL || state == NULL)
2724 return;
2725
2726 switch (state->c)
2727 {
2728 case NFA_SPLIT:
2729 case NFA_NOT:
2730 case NFA_NOPEN:
2731 case NFA_NCLOSE:
2732 case NFA_MCLOSE:
2733 case NFA_MCLOSE + 1:
2734 case NFA_MCLOSE + 2:
2735 case NFA_MCLOSE + 3:
2736 case NFA_MCLOSE + 4:
2737 case NFA_MCLOSE + 5:
2738 case NFA_MCLOSE + 6:
2739 case NFA_MCLOSE + 7:
2740 case NFA_MCLOSE + 8:
2741 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002742 /* These nodes are not added themselves but their "out" and/or
2743 * "out1" may be added below. */
2744 break;
2745
2746 case NFA_MOPEN:
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 /* These nodes do not need to be added, but we need to bail out
2757 * when it was tried to be added to this list before. */
2758 if (state->lastlist == l->id)
2759 return;
2760 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002761 break;
2762
2763 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002764 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002766 /* This state is already in the list, don't add it again,
2767 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002768 if (!nfa_has_backref)
2769 return;
2770
2771 /* See if the same state is already in the list with the same
2772 * positions. */
2773 for (i = 0; i < l->n; ++i)
2774 {
2775 thread = &l->t[i];
2776 if (thread->state->id == state->id
2777 && sub_equal(&thread->sub, sub))
2778 return;
2779 }
2780 }
2781
2782 /* when there are backreferences the number of states may be (a
2783 * lot) bigger */
2784 if (nfa_has_backref && l->n == l->len)
2785 {
2786 int newlen = l->len * 3 / 2 + 50;
2787
2788 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2789 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002790 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002791
2792 /* add the state to the list */
2793 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002794 thread = &l->t[l->n++];
2795 thread->state = state;
2796 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002797 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002799 /* Copy the match start and end positions. */
2800 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002801 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002802 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002803 sizeof(struct multipos) * sub->in_use);
2804 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002805 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002806 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002807 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002808 }
2809 }
2810
2811#ifdef ENABLE_LOG
2812 nfa_set_code(state->c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002813 fprintf(log_fd, "> Adding state %d to list. Character %d: %s\n",
2814 abs(state->id), state->c, code);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002815#endif
2816 switch (state->c)
2817 {
2818 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002819 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002820 break;
2821
2822 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002823 addstate(l, state->out, sub, off);
2824 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002825 break;
2826
2827#if 0
2828 case NFA_END_NEG_RANGE:
2829 /* Nothing to handle here. nfa_regmatch() will take care of it */
2830 break;
2831
2832 case NFA_NOT:
2833 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2834#ifdef ENABLE_LOG
2835 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2836#endif
2837 break;
2838
2839 case NFA_COMPOSING:
2840 /* nfa_regmatch() will match all the bytes of this composing char. */
2841 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842#endif
2843
Bram Moolenaar5714b802013-05-28 22:03:20 +02002844 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845 case NFA_NOPEN:
2846 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002847 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002848 break;
2849
2850 /* If this state is reached, then a recursive call of nfa_regmatch()
2851 * succeeded. the next call saves the found submatches in the
2852 * first state after the "invisible" branch. */
2853#if 0
2854 case NFA_END_INVISIBLE:
2855 break;
2856#endif
2857
2858 case NFA_MOPEN + 0:
2859 case NFA_MOPEN + 1:
2860 case NFA_MOPEN + 2:
2861 case NFA_MOPEN + 3:
2862 case NFA_MOPEN + 4:
2863 case NFA_MOPEN + 5:
2864 case NFA_MOPEN + 6:
2865 case NFA_MOPEN + 7:
2866 case NFA_MOPEN + 8:
2867 case NFA_MOPEN + 9:
2868 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869 if (state->c == NFA_ZSTART)
2870 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002871 else
2872 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002874 /* Set the position (with "off") in the subexpression. Save and
2875 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876 if (REG_MULTI)
2877 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002878 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002879 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002880 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002881 save_in_use = -1;
2882 }
2883 else
2884 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002885 save_in_use = sub->in_use;
2886 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002887 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002888 sub->list.multi[i].start.lnum = -1;
2889 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002890 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002891 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002892 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002893 if (off == -1)
2894 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002895 sub->list.multi[subidx].start.lnum = reglnum + 1;
2896 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002897 }
2898 else
2899 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002900 sub->list.multi[subidx].start.lnum = reglnum;
2901 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002902 (colnr_T)(reginput - regline + off);
2903 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904 }
2905 else
2906 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002907 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002908 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002909 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002910 save_in_use = -1;
2911 }
2912 else
2913 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002914 save_in_use = sub->in_use;
2915 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002916 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002917 sub->list.line[i].start = NULL;
2918 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002919 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002920 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002921 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002922 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002923 }
2924
Bram Moolenaar5714b802013-05-28 22:03:20 +02002925 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002927 if (save_in_use == -1)
2928 {
2929 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002930 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002931 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002932 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002933 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002935 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002936 break;
2937
2938 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002939 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002941 /* Do not overwrite the position set by \ze. If no \ze
2942 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002943 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002944 break;
2945 }
2946 case NFA_MCLOSE + 1:
2947 case NFA_MCLOSE + 2:
2948 case NFA_MCLOSE + 3:
2949 case NFA_MCLOSE + 4:
2950 case NFA_MCLOSE + 5:
2951 case NFA_MCLOSE + 6:
2952 case NFA_MCLOSE + 7:
2953 case NFA_MCLOSE + 8:
2954 case NFA_MCLOSE + 9:
2955 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956 if (state->c == NFA_ZEND)
2957 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002958 else
2959 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002960
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002961 /* We don't fill in gaps here, there must have been an MOPEN that
2962 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002963 save_in_use = sub->in_use;
2964 if (sub->in_use <= subidx)
2965 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002966 if (REG_MULTI)
2967 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002968 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002969 if (off == -1)
2970 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002971 sub->list.multi[subidx].end.lnum = reglnum + 1;
2972 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002973 }
2974 else
2975 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002976 sub->list.multi[subidx].end.lnum = reglnum;
2977 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02002978 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002979 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002980 }
2981 else
2982 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002983 save_ptr = sub->list.line[subidx].end;
2984 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002985 }
2986
Bram Moolenaar5714b802013-05-28 22:03:20 +02002987 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002988
2989 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002990 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002991 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002992 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002993 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002994 break;
2995 }
2996}
2997
2998/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002999 * Like addstate(), but the new state(s) are put at position "*ip".
3000 * Used for zero-width matches, next state to use is the added one.
3001 * This makes sure the order of states to be tried does not change, which
3002 * matters for alternatives.
3003 */
3004 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003005addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003006 nfa_list_T *l; /* runtime state list */
3007 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003008 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003009 int *ip;
3010{
3011 int tlen = l->n;
3012 int count;
3013 int i = *ip;
3014
3015 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003016 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003017
3018 /* when "*ip" was at the end of the list, nothing to do */
3019 if (i + 1 == tlen)
3020 return;
3021
3022 /* re-order to put the new state at the current position */
3023 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003024 if (count == 1)
3025 {
3026 /* overwrite the current state */
3027 l->t[i] = l->t[l->n - 1];
3028 }
3029 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003030 {
3031 /* make space for new states, then move them from the
3032 * end to the current position */
3033 mch_memmove(&(l->t[i + count]),
3034 &(l->t[i + 1]),
3035 sizeof(nfa_thread_T) * (l->n - i - 1));
3036 mch_memmove(&(l->t[i]),
3037 &(l->t[l->n - 1]),
3038 sizeof(nfa_thread_T) * count);
3039 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003040 --l->n;
3041 *ip = i - 1;
3042}
3043
3044/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045 * Check character class "class" against current character c.
3046 */
3047 static int
3048check_char_class(class, c)
3049 int class;
3050 int c;
3051{
3052 switch (class)
3053 {
3054 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003055 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003056 return OK;
3057 break;
3058 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003059 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003060 return OK;
3061 break;
3062 case NFA_CLASS_BLANK:
3063 if (c == ' ' || c == '\t')
3064 return OK;
3065 break;
3066 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003067 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003068 return OK;
3069 break;
3070 case NFA_CLASS_DIGIT:
3071 if (VIM_ISDIGIT(c))
3072 return OK;
3073 break;
3074 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003075 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003076 return OK;
3077 break;
3078 case NFA_CLASS_LOWER:
3079 if (MB_ISLOWER(c))
3080 return OK;
3081 break;
3082 case NFA_CLASS_PRINT:
3083 if (vim_isprintc(c))
3084 return OK;
3085 break;
3086 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003087 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003088 return OK;
3089 break;
3090 case NFA_CLASS_SPACE:
3091 if ((c >=9 && c <= 13) || (c == ' '))
3092 return OK;
3093 break;
3094 case NFA_CLASS_UPPER:
3095 if (MB_ISUPPER(c))
3096 return OK;
3097 break;
3098 case NFA_CLASS_XDIGIT:
3099 if (vim_isxdigit(c))
3100 return OK;
3101 break;
3102 case NFA_CLASS_TAB:
3103 if (c == '\t')
3104 return OK;
3105 break;
3106 case NFA_CLASS_RETURN:
3107 if (c == '\r')
3108 return OK;
3109 break;
3110 case NFA_CLASS_BACKSPACE:
3111 if (c == '\b')
3112 return OK;
3113 break;
3114 case NFA_CLASS_ESCAPE:
3115 if (c == '\033')
3116 return OK;
3117 break;
3118
3119 default:
3120 /* should not be here :P */
3121 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3122 }
3123 return FAIL;
3124}
3125
Bram Moolenaar5714b802013-05-28 22:03:20 +02003126static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3127
3128/*
3129 * Check for a match with subexpression "subidx".
3130 * return TRUE if it matches.
3131 */
3132 static int
3133match_backref(sub, subidx, bytelen)
3134 regsub_T *sub; /* pointers to subexpressions */
3135 int subidx;
3136 int *bytelen; /* out: length of match in bytes */
3137{
3138 int len;
3139
3140 if (sub->in_use <= subidx)
3141 {
3142retempty:
3143 /* backref was not set, match an empty string */
3144 *bytelen = 0;
3145 return TRUE;
3146 }
3147
3148 if (REG_MULTI)
3149 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003150 if (sub->list.multi[subidx].start.lnum < 0
3151 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003152 goto retempty;
3153 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003154 len = sub->list.multi[subidx].end.col
3155 - sub->list.multi[subidx].start.col;
3156 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003157 reginput, &len) == 0)
3158 {
3159 *bytelen = len;
3160 return TRUE;
3161 }
3162 }
3163 else
3164 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003165 if (sub->list.line[subidx].start == NULL
3166 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003167 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003168 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3169 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003170 {
3171 *bytelen = len;
3172 return TRUE;
3173 }
3174 }
3175 return FALSE;
3176}
3177
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003178/*
3179 * Set all NFA nodes' list ID equal to -1.
3180 */
3181 static void
3182nfa_set_neg_listids(start)
3183 nfa_state_T *start;
3184{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003185 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003186 {
3187 start->lastlist = -1;
3188 nfa_set_neg_listids(start->out);
3189 nfa_set_neg_listids(start->out1);
3190 }
3191}
3192
3193/*
3194 * Set all NFA nodes' list ID equal to 0.
3195 */
3196 static void
3197nfa_set_null_listids(start)
3198 nfa_state_T *start;
3199{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003200 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003201 {
3202 start->lastlist = 0;
3203 nfa_set_null_listids(start->out);
3204 nfa_set_null_listids(start->out1);
3205 }
3206}
3207
3208/*
3209 * Save list IDs for all NFA states in "list".
3210 */
3211 static void
3212nfa_save_listids(start, list)
3213 nfa_state_T *start;
3214 int *list;
3215{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003216 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217 {
3218 list[abs(start->id)] = start->lastlist;
3219 start->lastlist = -1;
3220 nfa_save_listids(start->out, list);
3221 nfa_save_listids(start->out1, list);
3222 }
3223}
3224
3225/*
3226 * Restore list IDs from "list" to all NFA states.
3227 */
3228 static void
3229nfa_restore_listids(start, list)
3230 nfa_state_T *start;
3231 int *list;
3232{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003233 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 {
3235 start->lastlist = list[abs(start->id)];
3236 nfa_restore_listids(start->out, list);
3237 nfa_restore_listids(start->out1, list);
3238 }
3239}
3240
Bram Moolenaar423532e2013-05-29 21:14:42 +02003241 static int
3242nfa_re_num_cmp(val, op, pos)
3243 long_u val;
3244 int op;
3245 long_u pos;
3246{
3247 if (op == 1) return pos > val;
3248 if (op == 2) return pos < val;
3249 return val == pos;
3250}
3251
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003252static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3253
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254/*
3255 * Main matching routine.
3256 *
3257 * Run NFA to determine whether it matches reginput.
3258 *
3259 * Return TRUE if there is a match, FALSE otherwise.
3260 * Note: Caller must ensure that: start != NULL.
3261 */
3262 static int
3263nfa_regmatch(start, submatch, m)
3264 nfa_state_T *start;
3265 regsub_T *submatch;
3266 regsub_T *m;
3267{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 int result;
3269 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 int flag = 0;
3271 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003272 int go_to_nextline = FALSE;
3273 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003274 char_u *old_reginput = NULL;
3275 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003276 nfa_list_T list[3];
3277 nfa_list_T *listtbl[2][2];
3278 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003279 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003280 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003281 nfa_list_T *thislist;
3282 nfa_list_T *nextlist;
3283 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003285#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003286 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003287
3288 if (debug == NULL)
3289 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003290 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 return FALSE;
3292 }
3293#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003294 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003296 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003297 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003298 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3299 list[0].len = nstate + 1;
3300 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3301 list[1].len = nstate + 1;
3302 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3303 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3305 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306
3307#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003308 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003309 if (log_fd != NULL)
3310 {
3311 fprintf(log_fd, "**********************************\n");
3312 nfa_set_code(start->c);
3313 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3314 abs(start->id), code);
3315 fprintf(log_fd, "**********************************\n");
3316 }
3317 else
3318 {
3319 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3320 log_fd = stderr;
3321 }
3322#endif
3323
3324 thislist = &list[0];
3325 thislist->n = 0;
3326 nextlist = &list[1];
3327 nextlist->n = 0;
3328 neglist = &list[2];
3329 neglist->n = 0;
3330#ifdef ENABLE_LOG
3331 fprintf(log_fd, "(---) STARTSTATE\n");
3332#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003333 thislist->id = listid;
3334 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003335
3336 /* There are two cases when the NFA advances: 1. input char matches the
3337 * NFA node and 2. input char does not match the NFA node, but the next
3338 * node is NFA_NOT. The following macro calls addstate() according to
3339 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3340 listtbl[0][0] = NULL;
3341 listtbl[0][1] = neglist;
3342 listtbl[1][0] = nextlist;
3343 listtbl[1][1] = NULL;
3344#define ADD_POS_NEG_STATE(node) \
3345 ll = listtbl[result ? 1 : 0][node->negated]; \
3346 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003347 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348
3349
3350 /*
3351 * Run for each character.
3352 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003353 for (;;)
3354 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003355 int curc;
3356 int clen;
3357
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358#ifdef FEAT_MBYTE
3359 if (has_mbyte)
3360 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003361 curc = (*mb_ptr2char)(reginput);
3362 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 }
3364 else
3365#endif
3366 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003367 curc = *reginput;
3368 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003370 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003371 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003372 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003373 go_to_nextline = FALSE;
3374 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003375
3376 /* swap lists */
3377 thislist = &list[flag];
3378 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003379 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003380 listtbl[1][0] = nextlist;
3381 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003382 thislist->id = listid;
3383 nextlist->id = listid + 1;
3384 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385
3386#ifdef ENABLE_LOG
3387 fprintf(log_fd, "------------------------------------------\n");
3388 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003389 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003390 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003391 {
3392 int i;
3393
3394 for (i = 0; i < thislist->n; i++)
3395 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3396 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003397 fprintf(log_fd, "\n");
3398#endif
3399
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003400#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003401 fprintf(debug, "\n-------------------\n");
3402#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003403 /*
3404 * If the state lists are empty we can stop.
3405 */
3406 if (thislist->n == 0 && neglist->n == 0)
3407 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003408
3409 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003410 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411 {
3412 if (neglist->n > 0)
3413 {
3414 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003415 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003416 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 }
3418 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003419 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003420
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003421#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 nfa_set_code(t->state->c);
3423 fprintf(debug, "%s, ", code);
3424#endif
3425#ifdef ENABLE_LOG
3426 nfa_set_code(t->state->c);
3427 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3428 code, (int)t->state->c);
3429#endif
3430
3431 /*
3432 * Handle the possible codes of the current state.
3433 * The most important is NFA_MATCH.
3434 */
3435 switch (t->state->c)
3436 {
3437 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003438 {
3439 int j;
3440
Bram Moolenaar963fee22013-05-26 21:47:28 +02003441 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003442 submatch->in_use = t->sub.in_use;
3443 if (REG_MULTI)
3444 for (j = 0; j < submatch->in_use; j++)
3445 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003446 submatch->list.multi[j].start =
3447 t->sub.list.multi[j].start;
3448 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003449 }
3450 else
3451 for (j = 0; j < submatch->in_use; j++)
3452 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003453 submatch->list.line[j].start =
3454 t->sub.list.line[j].start;
3455 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003456 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003457#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003458 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003460 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003461 * states at this position. When the list of states is going
3462 * to be empty quit without advancing, so that "reginput" is
3463 * correct. */
3464 if (nextlist->n == 0 && neglist->n == 0)
3465 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003466 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003467 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003468
3469 case NFA_END_INVISIBLE:
3470 /* This is only encountered after a NFA_START_INVISIBLE node.
3471 * They surround a zero-width group, used with "\@=" and "\&".
3472 * If we got here, it means that the current "invisible" group
3473 * finished successfully, so return control to the parent
3474 * nfa_regmatch(). Submatches are stored in *m, and used in
3475 * the parent call. */
3476 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003477 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003478 else
3479 {
3480 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003481 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 }
3483 break;
3484
3485 case NFA_START_INVISIBLE:
3486 /* Save global variables, and call nfa_regmatch() to check if
3487 * the current concat matches at this position. The concat
3488 * ends with the node NFA_END_INVISIBLE */
3489 old_reginput = reginput;
3490 old_regline = regline;
3491 old_reglnum = reglnum;
3492 if (listids == NULL)
3493 {
3494 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3495 if (listids == NULL)
3496 {
3497 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3498 return 0;
3499 }
3500 }
3501#ifdef ENABLE_LOG
3502 if (log_fd != stderr)
3503 fclose(log_fd);
3504 log_fd = NULL;
3505#endif
3506 /* Have to clear the listid field of the NFA nodes, so that
3507 * nfa_regmatch() and addstate() can run properly after
3508 * recursion. */
3509 nfa_save_listids(start, listids);
3510 nfa_set_null_listids(start);
3511 result = nfa_regmatch(t->state->out, submatch, m);
3512 nfa_set_neg_listids(start);
3513 nfa_restore_listids(start, listids);
3514
3515#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003516 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003517 if (log_fd != NULL)
3518 {
3519 fprintf(log_fd, "****************************\n");
3520 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3521 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3522 fprintf(log_fd, "****************************\n");
3523 }
3524 else
3525 {
3526 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3527 log_fd = stderr;
3528 }
3529#endif
3530 if (result == TRUE)
3531 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003532 int j;
3533
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003534 /* Restore position in input text */
3535 reginput = old_reginput;
3536 regline = old_regline;
3537 reglnum = old_reglnum;
3538 /* Copy submatch info from the recursive call */
3539 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003540 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003541 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003542 t->sub.list.multi[j].start = m->list.multi[j].start;
3543 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003544 }
3545 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003546 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003547 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003548 t->sub.list.line[j].start = m->list.line[j].start;
3549 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003550 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003551 t->sub.in_use = m->in_use;
3552
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003553 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003554 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003555 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003556 }
3557 else
3558 {
3559 /* continue with next input char */
3560 reginput = old_reginput;
3561 }
3562 break;
3563
3564 case NFA_BOL:
3565 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003566 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003567 break;
3568
3569 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003570 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003571 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003572 break;
3573
3574 case NFA_BOW:
3575 {
3576 int bow = TRUE;
3577
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003578 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003579 bow = FALSE;
3580#ifdef FEAT_MBYTE
3581 else if (has_mbyte)
3582 {
3583 int this_class;
3584
3585 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003586 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003587 if (this_class <= 1)
3588 bow = FALSE;
3589 else if (reg_prev_class() == this_class)
3590 bow = FALSE;
3591 }
3592#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003593 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003594 || (reginput > regline
3595 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596 bow = FALSE;
3597 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003598 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 break;
3600 }
3601
3602 case NFA_EOW:
3603 {
3604 int eow = TRUE;
3605
3606 if (reginput == regline)
3607 eow = FALSE;
3608#ifdef FEAT_MBYTE
3609 else if (has_mbyte)
3610 {
3611 int this_class, prev_class;
3612
3613 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003614 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615 prev_class = reg_prev_class();
3616 if (this_class == prev_class
3617 || prev_class == 0 || prev_class == 1)
3618 eow = FALSE;
3619 }
3620#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003621 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003622 || (reginput[0] != NUL
3623 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003624 eow = FALSE;
3625 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003626 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003627 break;
3628 }
3629
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003630#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003631 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003632 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003633 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003634 int len = 0;
3635 nfa_state_T *end;
3636 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003637 int cchars[MAX_MCO];
3638 int ccount = 0;
3639 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003641 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003642 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003643 if (utf_iscomposing(sta->c))
3644 {
3645 /* Only match composing character(s), ignore base
3646 * character. Used for ".{composing}" and "{composing}"
3647 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003648 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003649 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003650 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003652 /* If \Z was present, then ignore composing characters.
3653 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003654 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003655 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003656 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003657 else
3658 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003659 while (sta->c != NFA_END_COMPOSING)
3660 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003661 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003662
3663 /* Check base character matches first, unless ignored. */
3664 else if (len > 0 || mc == sta->c)
3665 {
3666 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003667 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003668 len += mb_char2len(mc);
3669 sta = sta->out;
3670 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003672 /* We don't care about the order of composing characters.
3673 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003674 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003675 {
3676 mc = mb_ptr2char(reginput + len);
3677 cchars[ccount++] = mc;
3678 len += mb_char2len(mc);
3679 if (ccount == MAX_MCO)
3680 break;
3681 }
3682
3683 /* Check that each composing char in the pattern matches a
3684 * composing char in the text. We do not check if all
3685 * composing chars are matched. */
3686 result = OK;
3687 while (sta->c != NFA_END_COMPOSING)
3688 {
3689 for (j = 0; j < ccount; ++j)
3690 if (cchars[j] == sta->c)
3691 break;
3692 if (j == ccount)
3693 {
3694 result = FAIL;
3695 break;
3696 }
3697 sta = sta->out;
3698 }
3699 }
3700 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003701 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003702
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003703 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003704 ADD_POS_NEG_STATE(end);
3705 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003706 }
3707#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003708
3709 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003710 if (curc == NUL && !reg_line_lbr && REG_MULTI
3711 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003713 go_to_nextline = TRUE;
3714 /* Pass -1 for the offset, which means taking the position
3715 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003716 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003718 else if (curc == '\n' && reg_line_lbr)
3719 {
3720 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003721 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003722 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003723 break;
3724
3725 case NFA_CLASS_ALNUM:
3726 case NFA_CLASS_ALPHA:
3727 case NFA_CLASS_BLANK:
3728 case NFA_CLASS_CNTRL:
3729 case NFA_CLASS_DIGIT:
3730 case NFA_CLASS_GRAPH:
3731 case NFA_CLASS_LOWER:
3732 case NFA_CLASS_PRINT:
3733 case NFA_CLASS_PUNCT:
3734 case NFA_CLASS_SPACE:
3735 case NFA_CLASS_UPPER:
3736 case NFA_CLASS_XDIGIT:
3737 case NFA_CLASS_TAB:
3738 case NFA_CLASS_RETURN:
3739 case NFA_CLASS_BACKSPACE:
3740 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003741 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003742 ADD_POS_NEG_STATE(t->state);
3743 break;
3744
3745 case NFA_END_NEG_RANGE:
3746 /* This follows a series of negated nodes, like:
3747 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003748 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003749 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750 break;
3751
3752 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003753 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003754 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003755 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003756 break;
3757
3758 /*
3759 * Character classes like \a for alpha, \d for digit etc.
3760 */
3761 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003762 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003763 ADD_POS_NEG_STATE(t->state);
3764 break;
3765
3766 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003767 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003768 ADD_POS_NEG_STATE(t->state);
3769 break;
3770
3771 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003772 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003773 ADD_POS_NEG_STATE(t->state);
3774 break;
3775
3776 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003777 result = !VIM_ISDIGIT(curc)
3778 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779 ADD_POS_NEG_STATE(t->state);
3780 break;
3781
3782 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003783 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003784 ADD_POS_NEG_STATE(t->state);
3785 break;
3786
3787 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003788 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003789 ADD_POS_NEG_STATE(t->state);
3790 break;
3791
3792 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003793 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003794 ADD_POS_NEG_STATE(t->state);
3795 break;
3796
3797 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003798 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003799 ADD_POS_NEG_STATE(t->state);
3800 break;
3801
3802 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003803 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003804 ADD_POS_NEG_STATE(t->state);
3805 break;
3806
3807 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003808 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003809 ADD_POS_NEG_STATE(t->state);
3810 break;
3811
3812 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003813 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003814 ADD_POS_NEG_STATE(t->state);
3815 break;
3816
3817 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003818 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003819 ADD_POS_NEG_STATE(t->state);
3820 break;
3821
3822 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003823 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003824 ADD_POS_NEG_STATE(t->state);
3825 break;
3826
3827 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003828 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003829 ADD_POS_NEG_STATE(t->state);
3830 break;
3831
3832 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003833 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003834 ADD_POS_NEG_STATE(t->state);
3835 break;
3836
3837 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003838 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003839 ADD_POS_NEG_STATE(t->state);
3840 break;
3841
3842 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003843 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003844 ADD_POS_NEG_STATE(t->state);
3845 break;
3846
3847 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003848 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849 ADD_POS_NEG_STATE(t->state);
3850 break;
3851
3852 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003853 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 ADD_POS_NEG_STATE(t->state);
3855 break;
3856
3857 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003858 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003859 ADD_POS_NEG_STATE(t->state);
3860 break;
3861
3862 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003863 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003864 ADD_POS_NEG_STATE(t->state);
3865 break;
3866
3867 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003868 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003869 ADD_POS_NEG_STATE(t->state);
3870 break;
3871
3872 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003873 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003874 ADD_POS_NEG_STATE(t->state);
3875 break;
3876
3877 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003878 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003879 ADD_POS_NEG_STATE(t->state);
3880 break;
3881
3882 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003883 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003884 ADD_POS_NEG_STATE(t->state);
3885 break;
3886
3887 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003888 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003889 ADD_POS_NEG_STATE(t->state);
3890 break;
3891
Bram Moolenaar5714b802013-05-28 22:03:20 +02003892 case NFA_BACKREF1:
3893 case NFA_BACKREF2:
3894 case NFA_BACKREF3:
3895 case NFA_BACKREF4:
3896 case NFA_BACKREF5:
3897 case NFA_BACKREF6:
3898 case NFA_BACKREF7:
3899 case NFA_BACKREF8:
3900 case NFA_BACKREF9:
3901 /* \1 .. \9 */
3902 {
3903 int subidx = t->state->c - NFA_BACKREF1 + 1;
3904 int bytelen;
3905
3906 result = match_backref(&t->sub, subidx, &bytelen);
3907 if (result)
3908 {
3909 if (bytelen == 0)
3910 {
3911 /* empty match always works, add NFA_SKIP with zero to
3912 * be used next */
3913 addstate_here(thislist, t->state->out, &t->sub,
3914 &listidx);
3915 thislist->t[listidx + 1].count = 0;
3916 }
3917 else if (bytelen <= clen)
3918 {
3919 /* match current character, jump ahead to out of
3920 * NFA_SKIP */
3921 addstate(nextlist, t->state->out->out, &t->sub, clen);
3922#ifdef ENABLE_LOG
3923 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3924#endif
3925 }
3926 else
3927 {
3928 /* skip ofer the matched characters, set character
3929 * count in NFA_SKIP */
3930 addstate(nextlist, t->state->out, &t->sub, bytelen);
3931 nextlist->t[nextlist->n - 1].count = bytelen - clen;
3932#ifdef ENABLE_LOG
3933 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3934#endif
3935 }
3936
3937 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02003938 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003939 }
3940 case NFA_SKIP:
3941 /* charater of previous matching \1 .. \9 */
3942 if (t->count - clen <= 0)
3943 {
3944 /* end of match, go to what follows */
3945 addstate(nextlist, t->state->out, &t->sub, clen);
3946#ifdef ENABLE_LOG
3947 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3948#endif
3949 }
3950 else
3951 {
3952 /* add state again with decremented count */
3953 addstate(nextlist, t->state, &t->sub, 0);
3954 nextlist->t[nextlist->n - 1].count = t->count - clen;
3955#ifdef ENABLE_LOG
3956 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3957#endif
3958 }
3959 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003960
3961 case NFA_SKIP_CHAR:
3962 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003963 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02003964 /* TODO: should not happen? */
3965 break;
3966
Bram Moolenaar423532e2013-05-29 21:14:42 +02003967 case NFA_LNUM:
3968 case NFA_LNUM_GT:
3969 case NFA_LNUM_LT:
3970 result = (REG_MULTI &&
3971 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
3972 (long_u)(reglnum + reg_firstlnum)));
3973 if (result)
3974 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3975 break;
3976
3977 case NFA_COL:
3978 case NFA_COL_GT:
3979 case NFA_COL_LT:
3980 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
3981 (long_u)(reginput - regline) + 1);
3982 if (result)
3983 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3984 break;
3985
3986 case NFA_VCOL:
3987 case NFA_VCOL_GT:
3988 case NFA_VCOL_LT:
3989 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
3990 (long_u)win_linetabsize(
3991 reg_win == NULL ? curwin : reg_win,
3992 regline, (colnr_T)(reginput - regline)) + 1);
3993 if (result)
3994 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3995 break;
3996
3997 case NFA_CURSOR:
3998 result = (reg_win != NULL
3999 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4000 && ((colnr_T)(reginput - regline)
4001 == reg_win->w_cursor.col));
4002 if (result)
4003 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4004 break;
4005
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004006 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004007 {
4008 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004009
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004010 /* TODO: put this in #ifdef later */
4011 if (c < -256)
4012 EMSGN("INTERNAL: Negative state char: %ld", c);
4013 if (is_Magic(c))
4014 c = un_Magic(c);
4015 result = (c == curc);
4016
4017 if (!result && ireg_ic)
4018 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004019#ifdef FEAT_MBYTE
4020 /* If there is a composing character which is not being
4021 * ignored there can be no match. Match with composing
4022 * character uses NFA_COMPOSING above. */
4023 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004024 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004025 result = FALSE;
4026#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004027 ADD_POS_NEG_STATE(t->state);
4028 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004029 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004030 }
4031
4032 } /* for (thislist = thislist; thislist->state; thislist++) */
4033
Bram Moolenaare23febd2013-05-26 18:40:14 +02004034 /* Look for the start of a match in the current position by adding the
4035 * start state to the list of states.
4036 * The first found match is the leftmost one, thus the order of states
4037 * matters!
4038 * Do not add the start state in recursive calls of nfa_regmatch(),
4039 * because recursive calls should only start in the first position.
4040 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004041 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004042 && reglnum == 0 && clen != 0
4043 && (ireg_maxcol == 0
4044 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004045 {
4046#ifdef ENABLE_LOG
4047 fprintf(log_fd, "(---) STARTSTATE\n");
4048#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004049 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004050 }
4051
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004052#ifdef ENABLE_LOG
4053 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004054 {
4055 int i;
4056
4057 for (i = 0; i < thislist->n; i++)
4058 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4059 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004060 fprintf(log_fd, "\n");
4061#endif
4062
4063nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004064 /* Advance to the next character, or advance to the next line, or
4065 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004066 if (clen != 0)
4067 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004068 else if (go_to_nextline)
4069 reg_nextline();
4070 else
4071 break;
4072 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004073
4074#ifdef ENABLE_LOG
4075 if (log_fd != stderr)
4076 fclose(log_fd);
4077 log_fd = NULL;
4078#endif
4079
4080theend:
4081 /* Free memory */
4082 vim_free(list[0].t);
4083 vim_free(list[1].t);
4084 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004085 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004086#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004087#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004088 fclose(debug);
4089#endif
4090
Bram Moolenaar963fee22013-05-26 21:47:28 +02004091 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004092}
4093
4094/*
4095 * Try match of "prog" with at regline["col"].
4096 * Returns 0 for failure, number of lines contained in the match otherwise.
4097 */
4098 static long
4099nfa_regtry(start, col)
4100 nfa_state_T *start;
4101 colnr_T col;
4102{
4103 int i;
4104 regsub_T sub, m;
4105#ifdef ENABLE_LOG
4106 FILE *f;
4107#endif
4108
4109 reginput = regline + col;
4110 need_clear_subexpr = TRUE;
4111
4112#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004113 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004114 if (f != NULL)
4115 {
4116 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4117 fprintf(f, " =======================================================\n");
4118#ifdef DEBUG
4119 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4120#endif
4121 fprintf(f, "\tInput text is \"%s\" \n", reginput);
4122 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004123 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004124 fprintf(f, "\n\n");
4125 fclose(f);
4126 }
4127 else
4128 EMSG(_("Could not open temporary log file for writing "));
4129#endif
4130
4131 if (REG_MULTI)
4132 {
4133 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004134 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4135 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004136 }
4137 else
4138 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004139 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4140 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004142 sub.in_use = 0;
4143 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004144
4145 if (nfa_regmatch(start, &sub, &m) == FALSE)
4146 return 0;
4147
4148 cleanup_subexpr();
4149 if (REG_MULTI)
4150 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004151 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004152 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004153 reg_startpos[i] = sub.list.multi[i].start;
4154 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004155 }
4156
4157 if (reg_startpos[0].lnum < 0)
4158 {
4159 reg_startpos[0].lnum = 0;
4160 reg_startpos[0].col = col;
4161 }
4162 if (reg_endpos[0].lnum < 0)
4163 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004164 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004165 reg_endpos[0].lnum = reglnum;
4166 reg_endpos[0].col = (int)(reginput - regline);
4167 }
4168 else
4169 /* Use line number of "\ze". */
4170 reglnum = reg_endpos[0].lnum;
4171 }
4172 else
4173 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004174 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004175 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004176 reg_startp[i] = sub.list.line[i].start;
4177 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004178 }
4179
4180 if (reg_startp[0] == NULL)
4181 reg_startp[0] = regline + col;
4182 if (reg_endp[0] == NULL)
4183 reg_endp[0] = reginput;
4184 }
4185
4186 return 1 + reglnum;
4187}
4188
4189/*
4190 * Match a regexp against a string ("line" points to the string) or multiple
4191 * lines ("line" is NULL, use reg_getline()).
4192 *
4193 * Returns 0 for failure, number of lines contained in the match otherwise.
4194 */
4195 static long
4196nfa_regexec_both(line, col)
4197 char_u *line;
4198 colnr_T col; /* column to start looking for match */
4199{
4200 nfa_regprog_T *prog;
4201 long retval = 0L;
4202 int i;
4203
4204 if (REG_MULTI)
4205 {
4206 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4207 line = reg_getline((linenr_T)0); /* relative to the cursor */
4208 reg_startpos = reg_mmatch->startpos;
4209 reg_endpos = reg_mmatch->endpos;
4210 }
4211 else
4212 {
4213 prog = (nfa_regprog_T *)reg_match->regprog;
4214 reg_startp = reg_match->startp;
4215 reg_endp = reg_match->endp;
4216 }
4217
4218 /* Be paranoid... */
4219 if (prog == NULL || line == NULL)
4220 {
4221 EMSG(_(e_null));
4222 goto theend;
4223 }
4224
4225 /* If the start column is past the maximum column: no need to try. */
4226 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4227 goto theend;
4228
4229 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4230 if (prog->regflags & RF_ICASE)
4231 ireg_ic = TRUE;
4232 else if (prog->regflags & RF_NOICASE)
4233 ireg_ic = FALSE;
4234
4235#ifdef FEAT_MBYTE
4236 /* If pattern contains "\Z" overrule value of ireg_icombine */
4237 if (prog->regflags & RF_ICOMBINE)
4238 ireg_icombine = TRUE;
4239#endif
4240
4241 regline = line;
4242 reglnum = 0; /* relative to line */
4243
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004244 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004245 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004246 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004247
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004248 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004249 for (i = 0; i < nstate; ++i)
4250 {
4251 prog->state[i].id = i;
4252 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004253 }
4254
4255 retval = nfa_regtry(prog->start, col);
4256
4257theend:
4258 return retval;
4259}
4260
4261/*
4262 * Compile a regular expression into internal code for the NFA matcher.
4263 * Returns the program in allocated space. Returns NULL for an error.
4264 */
4265 static regprog_T *
4266nfa_regcomp(expr, re_flags)
4267 char_u *expr;
4268 int re_flags;
4269{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004270 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004271 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004272 int *postfix;
4273
4274 if (expr == NULL)
4275 return NULL;
4276
4277#ifdef DEBUG
4278 nfa_regengine.expr = expr;
4279#endif
4280
4281 init_class_tab();
4282
4283 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4284 return NULL;
4285
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004286 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004287 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004288 postfix = re2post();
4289 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004290 {
4291 /* TODO: only give this error for debugging? */
4292 if (post_ptr >= post_end)
4293 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004295 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004296
4297 /*
4298 * In order to build the NFA, we parse the input regexp twice:
4299 * 1. first pass to count size (so we can allocate space)
4300 * 2. second to emit code
4301 */
4302#ifdef ENABLE_LOG
4303 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004304 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004305
4306 if (f != NULL)
4307 {
4308 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4309 fclose(f);
4310 }
4311 }
4312#endif
4313
4314 /*
4315 * PASS 1
4316 * Count number of NFA states in "nstate". Do not build the NFA.
4317 */
4318 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004319
4320 /* Space for compiled regexp */
4321 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4322 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4323 if (prog == NULL)
4324 goto fail;
4325 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326 state_ptr = prog->state;
4327
4328 /*
4329 * PASS 2
4330 * Build the NFA
4331 */
4332 prog->start = post2nfa(postfix, post_ptr, FALSE);
4333 if (prog->start == NULL)
4334 goto fail;
4335
4336 prog->regflags = regflags;
4337 prog->engine = &nfa_regengine;
4338 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004339 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004340 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004341 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004342#ifdef ENABLE_LOG
4343 nfa_postfix_dump(expr, OK);
4344 nfa_dump(prog);
4345#endif
4346
4347out:
4348 vim_free(post_start);
4349 post_start = post_ptr = post_end = NULL;
4350 state_ptr = NULL;
4351 return (regprog_T *)prog;
4352
4353fail:
4354 vim_free(prog);
4355 prog = NULL;
4356#ifdef ENABLE_LOG
4357 nfa_postfix_dump(expr, FAIL);
4358#endif
4359#ifdef DEBUG
4360 nfa_regengine.expr = NULL;
4361#endif
4362 goto out;
4363}
4364
4365
4366/*
4367 * Match a regexp against a string.
4368 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4369 * Uses curbuf for line count and 'iskeyword'.
4370 *
4371 * Return TRUE if there is a match, FALSE if not.
4372 */
4373 static int
4374nfa_regexec(rmp, line, col)
4375 regmatch_T *rmp;
4376 char_u *line; /* string to match against */
4377 colnr_T col; /* column to start looking for match */
4378{
4379 reg_match = rmp;
4380 reg_mmatch = NULL;
4381 reg_maxline = 0;
4382 reg_line_lbr = FALSE;
4383 reg_buf = curbuf;
4384 reg_win = NULL;
4385 ireg_ic = rmp->rm_ic;
4386#ifdef FEAT_MBYTE
4387 ireg_icombine = FALSE;
4388#endif
4389 ireg_maxcol = 0;
4390 return (nfa_regexec_both(line, col) != 0);
4391}
4392
4393#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4394 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4395
4396static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4397
4398/*
4399 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4400 */
4401 static int
4402nfa_regexec_nl(rmp, line, col)
4403 regmatch_T *rmp;
4404 char_u *line; /* string to match against */
4405 colnr_T col; /* column to start looking for match */
4406{
4407 reg_match = rmp;
4408 reg_mmatch = NULL;
4409 reg_maxline = 0;
4410 reg_line_lbr = TRUE;
4411 reg_buf = curbuf;
4412 reg_win = NULL;
4413 ireg_ic = rmp->rm_ic;
4414#ifdef FEAT_MBYTE
4415 ireg_icombine = FALSE;
4416#endif
4417 ireg_maxcol = 0;
4418 return (nfa_regexec_both(line, col) != 0);
4419}
4420#endif
4421
4422
4423/*
4424 * Match a regexp against multiple lines.
4425 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4426 * Uses curbuf for line count and 'iskeyword'.
4427 *
4428 * Return zero if there is no match. Return number of lines contained in the
4429 * match otherwise.
4430 *
4431 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4432 *
4433 * ! Also NOTE : match may actually be in another line. e.g.:
4434 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4435 *
4436 * +-------------------------+
4437 * |a |
4438 * |b |
4439 * |c |
4440 * | |
4441 * +-------------------------+
4442 *
4443 * then nfa_regexec_multi() returns 3. while the original
4444 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4445 *
4446 * FIXME if this behavior is not compatible.
4447 */
4448 static long
4449nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4450 regmmatch_T *rmp;
4451 win_T *win; /* window in which to search or NULL */
4452 buf_T *buf; /* buffer in which to search */
4453 linenr_T lnum; /* nr of line to start looking for match */
4454 colnr_T col; /* column to start looking for match */
4455 proftime_T *tm UNUSED; /* timeout limit or NULL */
4456{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004457 reg_match = NULL;
4458 reg_mmatch = rmp;
4459 reg_buf = buf;
4460 reg_win = win;
4461 reg_firstlnum = lnum;
4462 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4463 reg_line_lbr = FALSE;
4464 ireg_ic = rmp->rmm_ic;
4465#ifdef FEAT_MBYTE
4466 ireg_icombine = FALSE;
4467#endif
4468 ireg_maxcol = rmp->rmm_maxcol;
4469
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004470 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004471}
4472
4473#ifdef DEBUG
4474# undef ENABLE_LOG
4475#endif