blob: fd72e9e0b9dce7673fe9bffdcdf973bb1c5aa429 [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 Moolenaar4b6ebe62013-05-30 17:49:24 +02002876 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877 if (REG_MULTI)
2878 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002879 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002880 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002881 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002882 save_in_use = -1;
2883 }
2884 else
2885 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002886 save_in_use = sub->in_use;
2887 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002888 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002889 sub->list.multi[i].start.lnum = -1;
2890 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002891 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002892 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002893 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002894 if (off == -1)
2895 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002896 sub->list.multi[subidx].start.lnum = reglnum + 1;
2897 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002898 }
2899 else
2900 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002901 sub->list.multi[subidx].start.lnum = reglnum;
2902 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002903 (colnr_T)(reginput - regline + off);
2904 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002905 }
2906 else
2907 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002908 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002909 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002910 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002911 save_in_use = -1;
2912 }
2913 else
2914 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002915 save_in_use = sub->in_use;
2916 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002917 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002918 sub->list.line[i].start = NULL;
2919 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002920 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002921 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002922 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002923 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002924 }
2925
Bram Moolenaar5714b802013-05-28 22:03:20 +02002926 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002927
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002928 if (save_in_use == -1)
2929 {
2930 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002931 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002932 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002933 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002934 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002935 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002936 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002937 break;
2938
2939 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002940 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002941 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002942 /* Do not overwrite the position set by \ze. If no \ze
2943 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002944 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945 break;
2946 }
2947 case NFA_MCLOSE + 1:
2948 case NFA_MCLOSE + 2:
2949 case NFA_MCLOSE + 3:
2950 case NFA_MCLOSE + 4:
2951 case NFA_MCLOSE + 5:
2952 case NFA_MCLOSE + 6:
2953 case NFA_MCLOSE + 7:
2954 case NFA_MCLOSE + 8:
2955 case NFA_MCLOSE + 9:
2956 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002957 if (state->c == NFA_ZEND)
2958 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002959 else
2960 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002961
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002962 /* We don't fill in gaps here, there must have been an MOPEN that
2963 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002964 save_in_use = sub->in_use;
2965 if (sub->in_use <= subidx)
2966 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002967 if (REG_MULTI)
2968 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002969 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002970 if (off == -1)
2971 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002972 sub->list.multi[subidx].end.lnum = reglnum + 1;
2973 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002974 }
2975 else
2976 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002977 sub->list.multi[subidx].end.lnum = reglnum;
2978 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02002979 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002980 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002981 }
2982 else
2983 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002984 save_ptr = sub->list.line[subidx].end;
2985 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002986 }
2987
Bram Moolenaar5714b802013-05-28 22:03:20 +02002988 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002989
2990 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002991 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002993 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002994 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002995 break;
2996 }
2997}
2998
2999/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003000 * Like addstate(), but the new state(s) are put at position "*ip".
3001 * Used for zero-width matches, next state to use is the added one.
3002 * This makes sure the order of states to be tried does not change, which
3003 * matters for alternatives.
3004 */
3005 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003006addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003007 nfa_list_T *l; /* runtime state list */
3008 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003009 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003010 int *ip;
3011{
3012 int tlen = l->n;
3013 int count;
3014 int i = *ip;
3015
3016 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003017 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003018
3019 /* when "*ip" was at the end of the list, nothing to do */
3020 if (i + 1 == tlen)
3021 return;
3022
3023 /* re-order to put the new state at the current position */
3024 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003025 if (count == 1)
3026 {
3027 /* overwrite the current state */
3028 l->t[i] = l->t[l->n - 1];
3029 }
3030 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003031 {
3032 /* make space for new states, then move them from the
3033 * end to the current position */
3034 mch_memmove(&(l->t[i + count]),
3035 &(l->t[i + 1]),
3036 sizeof(nfa_thread_T) * (l->n - i - 1));
3037 mch_memmove(&(l->t[i]),
3038 &(l->t[l->n - 1]),
3039 sizeof(nfa_thread_T) * count);
3040 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003041 --l->n;
3042 *ip = i - 1;
3043}
3044
3045/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003046 * Check character class "class" against current character c.
3047 */
3048 static int
3049check_char_class(class, c)
3050 int class;
3051 int c;
3052{
3053 switch (class)
3054 {
3055 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003056 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003057 return OK;
3058 break;
3059 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003060 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003061 return OK;
3062 break;
3063 case NFA_CLASS_BLANK:
3064 if (c == ' ' || c == '\t')
3065 return OK;
3066 break;
3067 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003068 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003069 return OK;
3070 break;
3071 case NFA_CLASS_DIGIT:
3072 if (VIM_ISDIGIT(c))
3073 return OK;
3074 break;
3075 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003076 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003077 return OK;
3078 break;
3079 case NFA_CLASS_LOWER:
3080 if (MB_ISLOWER(c))
3081 return OK;
3082 break;
3083 case NFA_CLASS_PRINT:
3084 if (vim_isprintc(c))
3085 return OK;
3086 break;
3087 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003088 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003089 return OK;
3090 break;
3091 case NFA_CLASS_SPACE:
3092 if ((c >=9 && c <= 13) || (c == ' '))
3093 return OK;
3094 break;
3095 case NFA_CLASS_UPPER:
3096 if (MB_ISUPPER(c))
3097 return OK;
3098 break;
3099 case NFA_CLASS_XDIGIT:
3100 if (vim_isxdigit(c))
3101 return OK;
3102 break;
3103 case NFA_CLASS_TAB:
3104 if (c == '\t')
3105 return OK;
3106 break;
3107 case NFA_CLASS_RETURN:
3108 if (c == '\r')
3109 return OK;
3110 break;
3111 case NFA_CLASS_BACKSPACE:
3112 if (c == '\b')
3113 return OK;
3114 break;
3115 case NFA_CLASS_ESCAPE:
3116 if (c == '\033')
3117 return OK;
3118 break;
3119
3120 default:
3121 /* should not be here :P */
3122 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3123 }
3124 return FAIL;
3125}
3126
Bram Moolenaar5714b802013-05-28 22:03:20 +02003127static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3128
3129/*
3130 * Check for a match with subexpression "subidx".
3131 * return TRUE if it matches.
3132 */
3133 static int
3134match_backref(sub, subidx, bytelen)
3135 regsub_T *sub; /* pointers to subexpressions */
3136 int subidx;
3137 int *bytelen; /* out: length of match in bytes */
3138{
3139 int len;
3140
3141 if (sub->in_use <= subidx)
3142 {
3143retempty:
3144 /* backref was not set, match an empty string */
3145 *bytelen = 0;
3146 return TRUE;
3147 }
3148
3149 if (REG_MULTI)
3150 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003151 if (sub->list.multi[subidx].start.lnum < 0
3152 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003153 goto retempty;
3154 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003155 len = sub->list.multi[subidx].end.col
3156 - sub->list.multi[subidx].start.col;
3157 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003158 reginput, &len) == 0)
3159 {
3160 *bytelen = len;
3161 return TRUE;
3162 }
3163 }
3164 else
3165 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003166 if (sub->list.line[subidx].start == NULL
3167 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003168 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003169 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3170 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003171 {
3172 *bytelen = len;
3173 return TRUE;
3174 }
3175 }
3176 return FALSE;
3177}
3178
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179/*
3180 * Set all NFA nodes' list ID equal to -1.
3181 */
3182 static void
3183nfa_set_neg_listids(start)
3184 nfa_state_T *start;
3185{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003186 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003187 {
3188 start->lastlist = -1;
3189 nfa_set_neg_listids(start->out);
3190 nfa_set_neg_listids(start->out1);
3191 }
3192}
3193
3194/*
3195 * Set all NFA nodes' list ID equal to 0.
3196 */
3197 static void
3198nfa_set_null_listids(start)
3199 nfa_state_T *start;
3200{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003201 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202 {
3203 start->lastlist = 0;
3204 nfa_set_null_listids(start->out);
3205 nfa_set_null_listids(start->out1);
3206 }
3207}
3208
3209/*
3210 * Save list IDs for all NFA states in "list".
3211 */
3212 static void
3213nfa_save_listids(start, list)
3214 nfa_state_T *start;
3215 int *list;
3216{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003217 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 {
3219 list[abs(start->id)] = start->lastlist;
3220 start->lastlist = -1;
3221 nfa_save_listids(start->out, list);
3222 nfa_save_listids(start->out1, list);
3223 }
3224}
3225
3226/*
3227 * Restore list IDs from "list" to all NFA states.
3228 */
3229 static void
3230nfa_restore_listids(start, list)
3231 nfa_state_T *start;
3232 int *list;
3233{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003234 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003235 {
3236 start->lastlist = list[abs(start->id)];
3237 nfa_restore_listids(start->out, list);
3238 nfa_restore_listids(start->out1, list);
3239 }
3240}
3241
Bram Moolenaar423532e2013-05-29 21:14:42 +02003242 static int
3243nfa_re_num_cmp(val, op, pos)
3244 long_u val;
3245 int op;
3246 long_u pos;
3247{
3248 if (op == 1) return pos > val;
3249 if (op == 2) return pos < val;
3250 return val == pos;
3251}
3252
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003253static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3254
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003255/*
3256 * Main matching routine.
3257 *
3258 * Run NFA to determine whether it matches reginput.
3259 *
3260 * Return TRUE if there is a match, FALSE otherwise.
3261 * Note: Caller must ensure that: start != NULL.
3262 */
3263 static int
3264nfa_regmatch(start, submatch, m)
3265 nfa_state_T *start;
3266 regsub_T *submatch;
3267 regsub_T *m;
3268{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 int result;
3270 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 int flag = 0;
3272 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003273 int go_to_nextline = FALSE;
3274 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 char_u *old_reginput = NULL;
3276 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003277 nfa_list_T list[3];
3278 nfa_list_T *listtbl[2][2];
3279 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003280 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003281 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003282 nfa_list_T *thislist;
3283 nfa_list_T *nextlist;
3284 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003285 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003286#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003287 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288
3289 if (debug == NULL)
3290 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003291 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 return FALSE;
3293 }
3294#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003295 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003297 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003298 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003299 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3300 list[0].len = nstate + 1;
3301 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3302 list[1].len = nstate + 1;
3303 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3304 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3306 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307
3308#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003309 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 if (log_fd != NULL)
3311 {
3312 fprintf(log_fd, "**********************************\n");
3313 nfa_set_code(start->c);
3314 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3315 abs(start->id), code);
3316 fprintf(log_fd, "**********************************\n");
3317 }
3318 else
3319 {
3320 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3321 log_fd = stderr;
3322 }
3323#endif
3324
3325 thislist = &list[0];
3326 thislist->n = 0;
3327 nextlist = &list[1];
3328 nextlist->n = 0;
3329 neglist = &list[2];
3330 neglist->n = 0;
3331#ifdef ENABLE_LOG
3332 fprintf(log_fd, "(---) STARTSTATE\n");
3333#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003334 thislist->id = listid;
3335 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003336
3337 /* There are two cases when the NFA advances: 1. input char matches the
3338 * NFA node and 2. input char does not match the NFA node, but the next
3339 * node is NFA_NOT. The following macro calls addstate() according to
3340 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3341 listtbl[0][0] = NULL;
3342 listtbl[0][1] = neglist;
3343 listtbl[1][0] = nextlist;
3344 listtbl[1][1] = NULL;
3345#define ADD_POS_NEG_STATE(node) \
3346 ll = listtbl[result ? 1 : 0][node->negated]; \
3347 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003348 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003349
3350
3351 /*
3352 * Run for each character.
3353 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003354 for (;;)
3355 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003356 int curc;
3357 int clen;
3358
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003359#ifdef FEAT_MBYTE
3360 if (has_mbyte)
3361 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003362 curc = (*mb_ptr2char)(reginput);
3363 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 }
3365 else
3366#endif
3367 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003368 curc = *reginput;
3369 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003371 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003372 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003373 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003374 go_to_nextline = FALSE;
3375 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376
3377 /* swap lists */
3378 thislist = &list[flag];
3379 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003380 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003381 listtbl[1][0] = nextlist;
3382 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003383 thislist->id = listid;
3384 nextlist->id = listid + 1;
3385 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386
3387#ifdef ENABLE_LOG
3388 fprintf(log_fd, "------------------------------------------\n");
3389 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003390 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003391 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003392 {
3393 int i;
3394
3395 for (i = 0; i < thislist->n; i++)
3396 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3397 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003398 fprintf(log_fd, "\n");
3399#endif
3400
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003401#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402 fprintf(debug, "\n-------------------\n");
3403#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003404 /*
3405 * If the state lists are empty we can stop.
3406 */
3407 if (thislist->n == 0 && neglist->n == 0)
3408 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003409
3410 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003411 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 {
3413 if (neglist->n > 0)
3414 {
3415 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003416 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003417 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 }
3419 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003420 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003421
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003422#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003423 nfa_set_code(t->state->c);
3424 fprintf(debug, "%s, ", code);
3425#endif
3426#ifdef ENABLE_LOG
3427 nfa_set_code(t->state->c);
3428 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3429 code, (int)t->state->c);
3430#endif
3431
3432 /*
3433 * Handle the possible codes of the current state.
3434 * The most important is NFA_MATCH.
3435 */
3436 switch (t->state->c)
3437 {
3438 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003439 {
3440 int j;
3441
Bram Moolenaar963fee22013-05-26 21:47:28 +02003442 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003443 submatch->in_use = t->sub.in_use;
3444 if (REG_MULTI)
3445 for (j = 0; j < submatch->in_use; j++)
3446 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003447 submatch->list.multi[j].start =
3448 t->sub.list.multi[j].start;
3449 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003450 }
3451 else
3452 for (j = 0; j < submatch->in_use; j++)
3453 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003454 submatch->list.line[j].start =
3455 t->sub.list.line[j].start;
3456 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003457 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003459 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003461 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003462 * states at this position. When the list of states is going
3463 * to be empty quit without advancing, so that "reginput" is
3464 * correct. */
3465 if (nextlist->n == 0 && neglist->n == 0)
3466 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003467 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003468 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469
3470 case NFA_END_INVISIBLE:
3471 /* This is only encountered after a NFA_START_INVISIBLE node.
3472 * They surround a zero-width group, used with "\@=" and "\&".
3473 * If we got here, it means that the current "invisible" group
3474 * finished successfully, so return control to the parent
3475 * nfa_regmatch(). Submatches are stored in *m, and used in
3476 * the parent call. */
3477 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003478 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 else
3480 {
3481 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003482 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003483 }
3484 break;
3485
3486 case NFA_START_INVISIBLE:
3487 /* Save global variables, and call nfa_regmatch() to check if
3488 * the current concat matches at this position. The concat
3489 * ends with the node NFA_END_INVISIBLE */
3490 old_reginput = reginput;
3491 old_regline = regline;
3492 old_reglnum = reglnum;
3493 if (listids == NULL)
3494 {
3495 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3496 if (listids == NULL)
3497 {
3498 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3499 return 0;
3500 }
3501 }
3502#ifdef ENABLE_LOG
3503 if (log_fd != stderr)
3504 fclose(log_fd);
3505 log_fd = NULL;
3506#endif
3507 /* Have to clear the listid field of the NFA nodes, so that
3508 * nfa_regmatch() and addstate() can run properly after
3509 * recursion. */
3510 nfa_save_listids(start, listids);
3511 nfa_set_null_listids(start);
3512 result = nfa_regmatch(t->state->out, submatch, m);
3513 nfa_set_neg_listids(start);
3514 nfa_restore_listids(start, listids);
3515
3516#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003517 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 if (log_fd != NULL)
3519 {
3520 fprintf(log_fd, "****************************\n");
3521 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3522 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3523 fprintf(log_fd, "****************************\n");
3524 }
3525 else
3526 {
3527 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3528 log_fd = stderr;
3529 }
3530#endif
3531 if (result == TRUE)
3532 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003533 int j;
3534
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003535 /* Restore position in input text */
3536 reginput = old_reginput;
3537 regline = old_regline;
3538 reglnum = old_reglnum;
3539 /* Copy submatch info from the recursive call */
3540 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003541 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003543 t->sub.list.multi[j].start = m->list.multi[j].start;
3544 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003545 }
3546 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003547 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003548 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003549 t->sub.list.line[j].start = m->list.line[j].start;
3550 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003551 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003552 t->sub.in_use = m->in_use;
3553
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003554 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003555 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003556 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003557 }
3558 else
3559 {
3560 /* continue with next input char */
3561 reginput = old_reginput;
3562 }
3563 break;
3564
3565 case NFA_BOL:
3566 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003567 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003568 break;
3569
3570 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003571 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003572 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003573 break;
3574
3575 case NFA_BOW:
3576 {
3577 int bow = TRUE;
3578
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003579 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 bow = FALSE;
3581#ifdef FEAT_MBYTE
3582 else if (has_mbyte)
3583 {
3584 int this_class;
3585
3586 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003587 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 if (this_class <= 1)
3589 bow = FALSE;
3590 else if (reg_prev_class() == this_class)
3591 bow = FALSE;
3592 }
3593#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003594 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003595 || (reginput > regline
3596 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003597 bow = FALSE;
3598 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003599 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 break;
3601 }
3602
3603 case NFA_EOW:
3604 {
3605 int eow = TRUE;
3606
3607 if (reginput == regline)
3608 eow = FALSE;
3609#ifdef FEAT_MBYTE
3610 else if (has_mbyte)
3611 {
3612 int this_class, prev_class;
3613
3614 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003615 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003616 prev_class = reg_prev_class();
3617 if (this_class == prev_class
3618 || prev_class == 0 || prev_class == 1)
3619 eow = FALSE;
3620 }
3621#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003622 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003623 || (reginput[0] != NUL
3624 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003625 eow = FALSE;
3626 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003627 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003628 break;
3629 }
3630
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003631#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003633 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003634 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003635 int len = 0;
3636 nfa_state_T *end;
3637 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003638 int cchars[MAX_MCO];
3639 int ccount = 0;
3640 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003641
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003643 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003644 if (utf_iscomposing(sta->c))
3645 {
3646 /* Only match composing character(s), ignore base
3647 * character. Used for ".{composing}" and "{composing}"
3648 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003649 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003650 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003651 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003652 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003653 /* If \Z was present, then ignore composing characters.
3654 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003655 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003656 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003657 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003658 else
3659 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003660 while (sta->c != NFA_END_COMPOSING)
3661 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003662 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003663
3664 /* Check base character matches first, unless ignored. */
3665 else if (len > 0 || mc == sta->c)
3666 {
3667 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003668 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003669 len += mb_char2len(mc);
3670 sta = sta->out;
3671 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003673 /* We don't care about the order of composing characters.
3674 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003675 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003676 {
3677 mc = mb_ptr2char(reginput + len);
3678 cchars[ccount++] = mc;
3679 len += mb_char2len(mc);
3680 if (ccount == MAX_MCO)
3681 break;
3682 }
3683
3684 /* Check that each composing char in the pattern matches a
3685 * composing char in the text. We do not check if all
3686 * composing chars are matched. */
3687 result = OK;
3688 while (sta->c != NFA_END_COMPOSING)
3689 {
3690 for (j = 0; j < ccount; ++j)
3691 if (cchars[j] == sta->c)
3692 break;
3693 if (j == ccount)
3694 {
3695 result = FAIL;
3696 break;
3697 }
3698 sta = sta->out;
3699 }
3700 }
3701 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003702 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003703
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003704 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 ADD_POS_NEG_STATE(end);
3706 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003707 }
3708#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709
3710 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003711 if (curc == NUL && !reg_line_lbr && REG_MULTI
3712 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003713 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003714 go_to_nextline = TRUE;
3715 /* Pass -1 for the offset, which means taking the position
3716 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003717 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003718 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003719 else if (curc == '\n' && reg_line_lbr)
3720 {
3721 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003722 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003723 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 break;
3725
3726 case NFA_CLASS_ALNUM:
3727 case NFA_CLASS_ALPHA:
3728 case NFA_CLASS_BLANK:
3729 case NFA_CLASS_CNTRL:
3730 case NFA_CLASS_DIGIT:
3731 case NFA_CLASS_GRAPH:
3732 case NFA_CLASS_LOWER:
3733 case NFA_CLASS_PRINT:
3734 case NFA_CLASS_PUNCT:
3735 case NFA_CLASS_SPACE:
3736 case NFA_CLASS_UPPER:
3737 case NFA_CLASS_XDIGIT:
3738 case NFA_CLASS_TAB:
3739 case NFA_CLASS_RETURN:
3740 case NFA_CLASS_BACKSPACE:
3741 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003742 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003743 ADD_POS_NEG_STATE(t->state);
3744 break;
3745
3746 case NFA_END_NEG_RANGE:
3747 /* This follows a series of negated nodes, like:
3748 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003749 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003750 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003751 break;
3752
3753 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003754 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003755 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003756 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003757 break;
3758
3759 /*
3760 * Character classes like \a for alpha, \d for digit etc.
3761 */
3762 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003763 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764 ADD_POS_NEG_STATE(t->state);
3765 break;
3766
3767 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003768 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003769 ADD_POS_NEG_STATE(t->state);
3770 break;
3771
3772 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003773 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774 ADD_POS_NEG_STATE(t->state);
3775 break;
3776
3777 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003778 result = !VIM_ISDIGIT(curc)
3779 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003780 ADD_POS_NEG_STATE(t->state);
3781 break;
3782
3783 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003784 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003785 ADD_POS_NEG_STATE(t->state);
3786 break;
3787
3788 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003789 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003790 ADD_POS_NEG_STATE(t->state);
3791 break;
3792
3793 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003794 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795 ADD_POS_NEG_STATE(t->state);
3796 break;
3797
3798 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003799 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 ADD_POS_NEG_STATE(t->state);
3801 break;
3802
3803 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003804 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003805 ADD_POS_NEG_STATE(t->state);
3806 break;
3807
3808 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003809 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003810 ADD_POS_NEG_STATE(t->state);
3811 break;
3812
3813 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003814 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003815 ADD_POS_NEG_STATE(t->state);
3816 break;
3817
3818 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003819 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003820 ADD_POS_NEG_STATE(t->state);
3821 break;
3822
3823 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003824 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003825 ADD_POS_NEG_STATE(t->state);
3826 break;
3827
3828 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003829 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003830 ADD_POS_NEG_STATE(t->state);
3831 break;
3832
3833 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003834 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003835 ADD_POS_NEG_STATE(t->state);
3836 break;
3837
3838 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003839 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003840 ADD_POS_NEG_STATE(t->state);
3841 break;
3842
3843 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003844 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003845 ADD_POS_NEG_STATE(t->state);
3846 break;
3847
3848 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003849 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003850 ADD_POS_NEG_STATE(t->state);
3851 break;
3852
3853 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003854 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003855 ADD_POS_NEG_STATE(t->state);
3856 break;
3857
3858 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003859 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003860 ADD_POS_NEG_STATE(t->state);
3861 break;
3862
3863 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003864 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003865 ADD_POS_NEG_STATE(t->state);
3866 break;
3867
3868 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003869 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003870 ADD_POS_NEG_STATE(t->state);
3871 break;
3872
3873 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003874 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875 ADD_POS_NEG_STATE(t->state);
3876 break;
3877
3878 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003879 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003880 ADD_POS_NEG_STATE(t->state);
3881 break;
3882
3883 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003884 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003885 ADD_POS_NEG_STATE(t->state);
3886 break;
3887
3888 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003889 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003890 ADD_POS_NEG_STATE(t->state);
3891 break;
3892
Bram Moolenaar5714b802013-05-28 22:03:20 +02003893 case NFA_BACKREF1:
3894 case NFA_BACKREF2:
3895 case NFA_BACKREF3:
3896 case NFA_BACKREF4:
3897 case NFA_BACKREF5:
3898 case NFA_BACKREF6:
3899 case NFA_BACKREF7:
3900 case NFA_BACKREF8:
3901 case NFA_BACKREF9:
3902 /* \1 .. \9 */
3903 {
3904 int subidx = t->state->c - NFA_BACKREF1 + 1;
3905 int bytelen;
3906
3907 result = match_backref(&t->sub, subidx, &bytelen);
3908 if (result)
3909 {
3910 if (bytelen == 0)
3911 {
3912 /* empty match always works, add NFA_SKIP with zero to
3913 * be used next */
3914 addstate_here(thislist, t->state->out, &t->sub,
3915 &listidx);
3916 thislist->t[listidx + 1].count = 0;
3917 }
3918 else if (bytelen <= clen)
3919 {
3920 /* match current character, jump ahead to out of
3921 * NFA_SKIP */
3922 addstate(nextlist, t->state->out->out, &t->sub, clen);
3923#ifdef ENABLE_LOG
3924 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3925#endif
3926 }
3927 else
3928 {
3929 /* skip ofer the matched characters, set character
3930 * count in NFA_SKIP */
3931 addstate(nextlist, t->state->out, &t->sub, bytelen);
3932 nextlist->t[nextlist->n - 1].count = bytelen - clen;
3933#ifdef ENABLE_LOG
3934 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3935#endif
3936 }
3937
3938 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02003939 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003940 }
3941 case NFA_SKIP:
3942 /* charater of previous matching \1 .. \9 */
3943 if (t->count - clen <= 0)
3944 {
3945 /* end of match, go to what follows */
3946 addstate(nextlist, t->state->out, &t->sub, clen);
3947#ifdef ENABLE_LOG
3948 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3949#endif
3950 }
3951 else
3952 {
3953 /* add state again with decremented count */
3954 addstate(nextlist, t->state, &t->sub, 0);
3955 nextlist->t[nextlist->n - 1].count = t->count - clen;
3956#ifdef ENABLE_LOG
3957 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3958#endif
3959 }
3960 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003961
3962 case NFA_SKIP_CHAR:
3963 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003964 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02003965 /* TODO: should not happen? */
3966 break;
3967
Bram Moolenaar423532e2013-05-29 21:14:42 +02003968 case NFA_LNUM:
3969 case NFA_LNUM_GT:
3970 case NFA_LNUM_LT:
3971 result = (REG_MULTI &&
3972 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
3973 (long_u)(reglnum + reg_firstlnum)));
3974 if (result)
3975 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3976 break;
3977
3978 case NFA_COL:
3979 case NFA_COL_GT:
3980 case NFA_COL_LT:
3981 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
3982 (long_u)(reginput - regline) + 1);
3983 if (result)
3984 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3985 break;
3986
3987 case NFA_VCOL:
3988 case NFA_VCOL_GT:
3989 case NFA_VCOL_LT:
3990 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
3991 (long_u)win_linetabsize(
3992 reg_win == NULL ? curwin : reg_win,
3993 regline, (colnr_T)(reginput - regline)) + 1);
3994 if (result)
3995 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3996 break;
3997
3998 case NFA_CURSOR:
3999 result = (reg_win != NULL
4000 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4001 && ((colnr_T)(reginput - regline)
4002 == reg_win->w_cursor.col));
4003 if (result)
4004 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4005 break;
4006
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004007 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004008 {
4009 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004010
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004011 /* TODO: put this in #ifdef later */
4012 if (c < -256)
4013 EMSGN("INTERNAL: Negative state char: %ld", c);
4014 if (is_Magic(c))
4015 c = un_Magic(c);
4016 result = (c == curc);
4017
4018 if (!result && ireg_ic)
4019 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004020#ifdef FEAT_MBYTE
4021 /* If there is a composing character which is not being
4022 * ignored there can be no match. Match with composing
4023 * character uses NFA_COMPOSING above. */
4024 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004025 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004026 result = FALSE;
4027#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004028 ADD_POS_NEG_STATE(t->state);
4029 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004030 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004031 }
4032
4033 } /* for (thislist = thislist; thislist->state; thislist++) */
4034
Bram Moolenaare23febd2013-05-26 18:40:14 +02004035 /* Look for the start of a match in the current position by adding the
4036 * start state to the list of states.
4037 * The first found match is the leftmost one, thus the order of states
4038 * matters!
4039 * Do not add the start state in recursive calls of nfa_regmatch(),
4040 * because recursive calls should only start in the first position.
4041 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004042 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004043 && reglnum == 0 && clen != 0
4044 && (ireg_maxcol == 0
4045 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046 {
4047#ifdef ENABLE_LOG
4048 fprintf(log_fd, "(---) STARTSTATE\n");
4049#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004050 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004051 }
4052
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004053#ifdef ENABLE_LOG
4054 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004055 {
4056 int i;
4057
4058 for (i = 0; i < thislist->n; i++)
4059 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4060 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004061 fprintf(log_fd, "\n");
4062#endif
4063
4064nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004065 /* Advance to the next character, or advance to the next line, or
4066 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004067 if (clen != 0)
4068 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004069 else if (go_to_nextline)
4070 reg_nextline();
4071 else
4072 break;
4073 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004074
4075#ifdef ENABLE_LOG
4076 if (log_fd != stderr)
4077 fclose(log_fd);
4078 log_fd = NULL;
4079#endif
4080
4081theend:
4082 /* Free memory */
4083 vim_free(list[0].t);
4084 vim_free(list[1].t);
4085 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004086 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004087#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004088#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089 fclose(debug);
4090#endif
4091
Bram Moolenaar963fee22013-05-26 21:47:28 +02004092 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004093}
4094
4095/*
4096 * Try match of "prog" with at regline["col"].
4097 * Returns 0 for failure, number of lines contained in the match otherwise.
4098 */
4099 static long
4100nfa_regtry(start, col)
4101 nfa_state_T *start;
4102 colnr_T col;
4103{
4104 int i;
4105 regsub_T sub, m;
4106#ifdef ENABLE_LOG
4107 FILE *f;
4108#endif
4109
4110 reginput = regline + col;
4111 need_clear_subexpr = TRUE;
4112
4113#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004114 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004115 if (f != NULL)
4116 {
4117 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4118 fprintf(f, " =======================================================\n");
4119#ifdef DEBUG
4120 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4121#endif
4122 fprintf(f, "\tInput text is \"%s\" \n", reginput);
4123 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004124 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004125 fprintf(f, "\n\n");
4126 fclose(f);
4127 }
4128 else
4129 EMSG(_("Could not open temporary log file for writing "));
4130#endif
4131
4132 if (REG_MULTI)
4133 {
4134 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004135 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4136 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004137 }
4138 else
4139 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004140 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4141 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004142 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004143 sub.in_use = 0;
4144 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004145
4146 if (nfa_regmatch(start, &sub, &m) == FALSE)
4147 return 0;
4148
4149 cleanup_subexpr();
4150 if (REG_MULTI)
4151 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004152 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004153 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004154 reg_startpos[i] = sub.list.multi[i].start;
4155 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004156 }
4157
4158 if (reg_startpos[0].lnum < 0)
4159 {
4160 reg_startpos[0].lnum = 0;
4161 reg_startpos[0].col = col;
4162 }
4163 if (reg_endpos[0].lnum < 0)
4164 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004165 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004166 reg_endpos[0].lnum = reglnum;
4167 reg_endpos[0].col = (int)(reginput - regline);
4168 }
4169 else
4170 /* Use line number of "\ze". */
4171 reglnum = reg_endpos[0].lnum;
4172 }
4173 else
4174 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004175 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004176 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004177 reg_startp[i] = sub.list.line[i].start;
4178 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004179 }
4180
4181 if (reg_startp[0] == NULL)
4182 reg_startp[0] = regline + col;
4183 if (reg_endp[0] == NULL)
4184 reg_endp[0] = reginput;
4185 }
4186
4187 return 1 + reglnum;
4188}
4189
4190/*
4191 * Match a regexp against a string ("line" points to the string) or multiple
4192 * lines ("line" is NULL, use reg_getline()).
4193 *
4194 * Returns 0 for failure, number of lines contained in the match otherwise.
4195 */
4196 static long
4197nfa_regexec_both(line, col)
4198 char_u *line;
4199 colnr_T col; /* column to start looking for match */
4200{
4201 nfa_regprog_T *prog;
4202 long retval = 0L;
4203 int i;
4204
4205 if (REG_MULTI)
4206 {
4207 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4208 line = reg_getline((linenr_T)0); /* relative to the cursor */
4209 reg_startpos = reg_mmatch->startpos;
4210 reg_endpos = reg_mmatch->endpos;
4211 }
4212 else
4213 {
4214 prog = (nfa_regprog_T *)reg_match->regprog;
4215 reg_startp = reg_match->startp;
4216 reg_endp = reg_match->endp;
4217 }
4218
4219 /* Be paranoid... */
4220 if (prog == NULL || line == NULL)
4221 {
4222 EMSG(_(e_null));
4223 goto theend;
4224 }
4225
4226 /* If the start column is past the maximum column: no need to try. */
4227 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4228 goto theend;
4229
4230 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4231 if (prog->regflags & RF_ICASE)
4232 ireg_ic = TRUE;
4233 else if (prog->regflags & RF_NOICASE)
4234 ireg_ic = FALSE;
4235
4236#ifdef FEAT_MBYTE
4237 /* If pattern contains "\Z" overrule value of ireg_icombine */
4238 if (prog->regflags & RF_ICOMBINE)
4239 ireg_icombine = TRUE;
4240#endif
4241
4242 regline = line;
4243 reglnum = 0; /* relative to line */
4244
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004245 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004246 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004247 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004249 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004250 for (i = 0; i < nstate; ++i)
4251 {
4252 prog->state[i].id = i;
4253 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004254 }
4255
4256 retval = nfa_regtry(prog->start, col);
4257
4258theend:
4259 return retval;
4260}
4261
4262/*
4263 * Compile a regular expression into internal code for the NFA matcher.
4264 * Returns the program in allocated space. Returns NULL for an error.
4265 */
4266 static regprog_T *
4267nfa_regcomp(expr, re_flags)
4268 char_u *expr;
4269 int re_flags;
4270{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004271 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004272 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004273 int *postfix;
4274
4275 if (expr == NULL)
4276 return NULL;
4277
4278#ifdef DEBUG
4279 nfa_regengine.expr = expr;
4280#endif
4281
4282 init_class_tab();
4283
4284 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4285 return NULL;
4286
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004287 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004288 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004289 postfix = re2post();
4290 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004291 {
4292 /* TODO: only give this error for debugging? */
4293 if (post_ptr >= post_end)
4294 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004296 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004297
4298 /*
4299 * In order to build the NFA, we parse the input regexp twice:
4300 * 1. first pass to count size (so we can allocate space)
4301 * 2. second to emit code
4302 */
4303#ifdef ENABLE_LOG
4304 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004305 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004306
4307 if (f != NULL)
4308 {
4309 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4310 fclose(f);
4311 }
4312 }
4313#endif
4314
4315 /*
4316 * PASS 1
4317 * Count number of NFA states in "nstate". Do not build the NFA.
4318 */
4319 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004320
4321 /* Space for compiled regexp */
4322 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4323 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4324 if (prog == NULL)
4325 goto fail;
4326 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004327 state_ptr = prog->state;
4328
4329 /*
4330 * PASS 2
4331 * Build the NFA
4332 */
4333 prog->start = post2nfa(postfix, post_ptr, FALSE);
4334 if (prog->start == NULL)
4335 goto fail;
4336
4337 prog->regflags = regflags;
4338 prog->engine = &nfa_regengine;
4339 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004340 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004341 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004342 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004343#ifdef ENABLE_LOG
4344 nfa_postfix_dump(expr, OK);
4345 nfa_dump(prog);
4346#endif
4347
4348out:
4349 vim_free(post_start);
4350 post_start = post_ptr = post_end = NULL;
4351 state_ptr = NULL;
4352 return (regprog_T *)prog;
4353
4354fail:
4355 vim_free(prog);
4356 prog = NULL;
4357#ifdef ENABLE_LOG
4358 nfa_postfix_dump(expr, FAIL);
4359#endif
4360#ifdef DEBUG
4361 nfa_regengine.expr = NULL;
4362#endif
4363 goto out;
4364}
4365
4366
4367/*
4368 * Match a regexp against a string.
4369 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4370 * Uses curbuf for line count and 'iskeyword'.
4371 *
4372 * Return TRUE if there is a match, FALSE if not.
4373 */
4374 static int
4375nfa_regexec(rmp, line, col)
4376 regmatch_T *rmp;
4377 char_u *line; /* string to match against */
4378 colnr_T col; /* column to start looking for match */
4379{
4380 reg_match = rmp;
4381 reg_mmatch = NULL;
4382 reg_maxline = 0;
4383 reg_line_lbr = FALSE;
4384 reg_buf = curbuf;
4385 reg_win = NULL;
4386 ireg_ic = rmp->rm_ic;
4387#ifdef FEAT_MBYTE
4388 ireg_icombine = FALSE;
4389#endif
4390 ireg_maxcol = 0;
4391 return (nfa_regexec_both(line, col) != 0);
4392}
4393
4394#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4395 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4396
4397static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4398
4399/*
4400 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4401 */
4402 static int
4403nfa_regexec_nl(rmp, line, col)
4404 regmatch_T *rmp;
4405 char_u *line; /* string to match against */
4406 colnr_T col; /* column to start looking for match */
4407{
4408 reg_match = rmp;
4409 reg_mmatch = NULL;
4410 reg_maxline = 0;
4411 reg_line_lbr = TRUE;
4412 reg_buf = curbuf;
4413 reg_win = NULL;
4414 ireg_ic = rmp->rm_ic;
4415#ifdef FEAT_MBYTE
4416 ireg_icombine = FALSE;
4417#endif
4418 ireg_maxcol = 0;
4419 return (nfa_regexec_both(line, col) != 0);
4420}
4421#endif
4422
4423
4424/*
4425 * Match a regexp against multiple lines.
4426 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4427 * Uses curbuf for line count and 'iskeyword'.
4428 *
4429 * Return zero if there is no match. Return number of lines contained in the
4430 * match otherwise.
4431 *
4432 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4433 *
4434 * ! Also NOTE : match may actually be in another line. e.g.:
4435 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4436 *
4437 * +-------------------------+
4438 * |a |
4439 * |b |
4440 * |c |
4441 * | |
4442 * +-------------------------+
4443 *
4444 * then nfa_regexec_multi() returns 3. while the original
4445 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4446 *
4447 * FIXME if this behavior is not compatible.
4448 */
4449 static long
4450nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4451 regmmatch_T *rmp;
4452 win_T *win; /* window in which to search or NULL */
4453 buf_T *buf; /* buffer in which to search */
4454 linenr_T lnum; /* nr of line to start looking for match */
4455 colnr_T col; /* column to start looking for match */
4456 proftime_T *tm UNUSED; /* timeout limit or NULL */
4457{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004458 reg_match = NULL;
4459 reg_mmatch = rmp;
4460 reg_buf = buf;
4461 reg_win = win;
4462 reg_firstlnum = lnum;
4463 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4464 reg_line_lbr = FALSE;
4465 ireg_ic = rmp->rmm_ic;
4466#ifdef FEAT_MBYTE
4467 ireg_icombine = FALSE;
4468#endif
4469 ireg_maxcol = rmp->rmm_maxcol;
4470
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004471 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004472}
4473
4474#ifdef DEBUG
4475# undef ENABLE_LOG
4476#endif