blob: ae6281d09476f2bbcc101c22898619cdc437a81e [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 { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200236 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237 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.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200259 * When it is still not enough realloc_post_list() will be used. */
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 Moolenaar16299b52013-05-30 18:45:23 +0200263 postfix_size = sizeof(int) * 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/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200280 * Allocate more space for post_start. Called when
281 * running above the estimated number of states.
282 */
283 static int
284realloc_post_list()
285{
286 int nstate_max = post_end - post_start;
287 int new_max = nstate_max + 1000;
288 int *new_start;
289 int *old_start;
290
291 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
292 if (new_start == NULL)
293 return FAIL;
294 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
295 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
296 old_start = post_start;
297 post_start = new_start;
298 post_ptr = new_start + (post_ptr - old_start);
299 post_end = post_start + new_max;
300 vim_free(old_start);
301 return OK;
302}
303
304/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305 * Search between "start" and "end" and try to recognize a
306 * character class in expanded form. For example [0-9].
307 * On success, return the id the character class to be emitted.
308 * On failure, return 0 (=FAIL)
309 * Start points to the first char of the range, while end should point
310 * to the closing brace.
311 */
312 static int
313nfa_recognize_char_class(start, end, extra_newl)
314 char_u *start;
315 char_u *end;
316 int extra_newl;
317{
318 int i;
319 /* Each of these variables takes up a char in "config[]",
320 * in the order they are here. */
321 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
322 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
323 char_u *p;
324#define NCONFIGS 16
325 int classid[NCONFIGS] = {
326 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
327 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
328 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
329 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
330 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200331 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200332 char_u config[NCONFIGS][9] = {
333 "000000100", /* digit */
334 "100000100", /* non digit */
335 "011000100", /* hex-digit */
336 "111000100", /* non hex-digit */
337 "000001000", /* octal-digit */
338 "100001000", /* [^0-7] */
339 "000110110", /* [0-9A-Za-z_] */
340 "100110110", /* [^0-9A-Za-z_] */
341 "000110010", /* head of word */
342 "100110010", /* not head of word */
343 "000110000", /* alphabetic char a-z */
344 "100110000", /* non alphabetic char */
345 "000100000", /* lowercase letter */
346 "100100000", /* non lowercase */
347 "000010000", /* uppercase */
348 "100010000" /* non uppercase */
349 };
350
351 if (extra_newl == TRUE)
352 newl = TRUE;
353
354 if (*end != ']')
355 return FAIL;
356 p = start;
357 if (*p == '^')
358 {
359 not = TRUE;
360 p ++;
361 }
362
363 while (p < end)
364 {
365 if (p + 2 < end && *(p + 1) == '-')
366 {
367 switch (*p)
368 {
369 case '0':
370 if (*(p + 2) == '9')
371 {
372 o9 = TRUE;
373 break;
374 }
375 else
376 if (*(p + 2) == '7')
377 {
378 o7 = TRUE;
379 break;
380 }
381 case 'a':
382 if (*(p + 2) == 'z')
383 {
384 az = TRUE;
385 break;
386 }
387 else
388 if (*(p + 2) == 'f')
389 {
390 af = TRUE;
391 break;
392 }
393 case 'A':
394 if (*(p + 2) == 'Z')
395 {
396 AZ = TRUE;
397 break;
398 }
399 else
400 if (*(p + 2) == 'F')
401 {
402 AF = TRUE;
403 break;
404 }
405 /* FALLTHROUGH */
406 default:
407 return FAIL;
408 }
409 p += 3;
410 }
411 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
412 {
413 newl = TRUE;
414 p += 2;
415 }
416 else if (*p == '_')
417 {
418 underscore = TRUE;
419 p ++;
420 }
421 else if (*p == '\n')
422 {
423 newl = TRUE;
424 p ++;
425 }
426 else
427 return FAIL;
428 } /* while (p < end) */
429
430 if (p != end)
431 return FAIL;
432
433 /* build the config that represents the ranges we gathered */
434 STRCPY(myconfig, "000000000");
435 if (not == TRUE)
436 myconfig[0] = '1';
437 if (af == TRUE)
438 myconfig[1] = '1';
439 if (AF == TRUE)
440 myconfig[2] = '1';
441 if (az == TRUE)
442 myconfig[3] = '1';
443 if (AZ == TRUE)
444 myconfig[4] = '1';
445 if (o7 == TRUE)
446 myconfig[5] = '1';
447 if (o9 == TRUE)
448 myconfig[6] = '1';
449 if (underscore == TRUE)
450 myconfig[7] = '1';
451 if (newl == TRUE)
452 {
453 myconfig[8] = '1';
454 extra_newl = ADD_NL;
455 }
456 /* try to recognize character classes */
457 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200458 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200459 return classid[i] + extra_newl;
460
461 /* fallthrough => no success so far */
462 return FAIL;
463
464#undef NCONFIGS
465}
466
467/*
468 * Produce the bytes for equivalence class "c".
469 * Currently only handles latin1, latin9 and utf-8.
470 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
471 * equivalent to 'a OR b OR c'
472 *
473 * NOTE! When changing this function, also update reg_equi_class()
474 */
475 static int
476nfa_emit_equi_class(c, neg)
477 int c;
478 int neg;
479{
480 int first = TRUE;
481 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
482#define EMIT2(c) \
483 EMIT(c); \
484 if (neg == TRUE) { \
485 EMIT(NFA_NOT); \
486 } \
487 if (first == FALSE) \
488 EMIT(glue); \
489 else \
490 first = FALSE; \
491
492#ifdef FEAT_MBYTE
493 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
494 || STRCMP(p_enc, "iso-8859-15") == 0)
495#endif
496 {
497 switch (c)
498 {
499 case 'A': case '\300': case '\301': case '\302':
500 case '\303': case '\304': case '\305':
501 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
502 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
503 EMIT2('\305');
504 return OK;
505
506 case 'C': case '\307':
507 EMIT2('C'); EMIT2('\307');
508 return OK;
509
510 case 'E': case '\310': case '\311': case '\312': case '\313':
511 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
512 EMIT2('\312'); EMIT2('\313');
513 return OK;
514
515 case 'I': case '\314': case '\315': case '\316': case '\317':
516 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
517 EMIT2('\316'); EMIT2('\317');
518 return OK;
519
520 case 'N': case '\321':
521 EMIT2('N'); EMIT2('\321');
522 return OK;
523
524 case 'O': case '\322': case '\323': case '\324': case '\325':
525 case '\326':
526 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
527 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
528 return OK;
529
530 case 'U': case '\331': case '\332': case '\333': case '\334':
531 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
532 EMIT2('\333'); EMIT2('\334');
533 return OK;
534
535 case 'Y': case '\335':
536 EMIT2('Y'); EMIT2('\335');
537 return OK;
538
539 case 'a': case '\340': case '\341': case '\342':
540 case '\343': case '\344': case '\345':
541 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
542 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
543 EMIT2('\345');
544 return OK;
545
546 case 'c': case '\347':
547 EMIT2('c'); EMIT2('\347');
548 return OK;
549
550 case 'e': case '\350': case '\351': case '\352': case '\353':
551 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
552 EMIT2('\352'); EMIT2('\353');
553 return OK;
554
555 case 'i': case '\354': case '\355': case '\356': case '\357':
556 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
557 EMIT2('\356'); EMIT2('\357');
558 return OK;
559
560 case 'n': case '\361':
561 EMIT2('n'); EMIT2('\361');
562 return OK;
563
564 case 'o': case '\362': case '\363': case '\364': case '\365':
565 case '\366':
566 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
567 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
568 return OK;
569
570 case 'u': case '\371': case '\372': case '\373': case '\374':
571 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
572 EMIT2('\373'); EMIT2('\374');
573 return OK;
574
575 case 'y': case '\375': case '\377':
576 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
577 return OK;
578
579 default:
580 return FAIL;
581 }
582 }
583
584 EMIT(c);
585 return OK;
586#undef EMIT2
587}
588
589/*
590 * Code to parse regular expression.
591 *
592 * We try to reuse parsing functions in regexp.c to
593 * minimize surprise and keep the syntax consistent.
594 */
595
596/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 * Parse the lowest level.
598 *
599 * An atom can be one of a long list of items. Many atoms match one character
600 * in the text. It is often an ordinary character or a character class.
601 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
602 * is only for syntax highlighting.
603 *
604 * atom ::= ordinary-atom
605 * or \( pattern \)
606 * or \%( pattern \)
607 * or \z( pattern \)
608 */
609 static int
610nfa_regatom()
611{
612 int c;
613 int charclass;
614 int equiclass;
615 int collclass;
616 int got_coll_char;
617 char_u *p;
618 char_u *endp;
619#ifdef FEAT_MBYTE
620 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200621#endif
622 int extra = 0;
623 int first;
624 int emit_range;
625 int negated;
626 int result;
627 int startc = -1;
628 int endc = -1;
629 int oldstartc = -1;
630 int cpo_lit; /* 'cpoptions' contains 'l' flag */
631 int cpo_bsl; /* 'cpoptions' contains '\' flag */
632 int glue; /* ID that will "glue" nodes together */
633
634 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
635 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
636
637 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 switch (c)
639 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200640 case NUL:
641 syntax_error = TRUE;
642 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
643
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 case Magic('^'):
645 EMIT(NFA_BOL);
646 break;
647
648 case Magic('$'):
649 EMIT(NFA_EOL);
650#if defined(FEAT_SYN_HL) || defined(PROTO)
651 had_eol = TRUE;
652#endif
653 break;
654
655 case Magic('<'):
656 EMIT(NFA_BOW);
657 break;
658
659 case Magic('>'):
660 EMIT(NFA_EOW);
661 break;
662
663 case Magic('_'):
664 c = no_Magic(getchr());
665 if (c == '^') /* "\_^" is start-of-line */
666 {
667 EMIT(NFA_BOL);
668 break;
669 }
670 if (c == '$') /* "\_$" is end-of-line */
671 {
672 EMIT(NFA_EOL);
673#if defined(FEAT_SYN_HL) || defined(PROTO)
674 had_eol = TRUE;
675#endif
676 break;
677 }
678
679 extra = ADD_NL;
680
681 /* "\_[" is collection plus newline */
682 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200683 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200684
685 /* "\_x" is character class plus newline */
686 /*FALLTHROUGH*/
687
688 /*
689 * Character classes.
690 */
691 case Magic('.'):
692 case Magic('i'):
693 case Magic('I'):
694 case Magic('k'):
695 case Magic('K'):
696 case Magic('f'):
697 case Magic('F'):
698 case Magic('p'):
699 case Magic('P'):
700 case Magic('s'):
701 case Magic('S'):
702 case Magic('d'):
703 case Magic('D'):
704 case Magic('x'):
705 case Magic('X'):
706 case Magic('o'):
707 case Magic('O'):
708 case Magic('w'):
709 case Magic('W'):
710 case Magic('h'):
711 case Magic('H'):
712 case Magic('a'):
713 case Magic('A'):
714 case Magic('l'):
715 case Magic('L'):
716 case Magic('u'):
717 case Magic('U'):
718 p = vim_strchr(classchars, no_Magic(c));
719 if (p == NULL)
720 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200721 EMSGN("INTERNAL: Unknown character class char: %ld", c);
722 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200723 }
724#ifdef FEAT_MBYTE
725 /* When '.' is followed by a composing char ignore the dot, so that
726 * the composing char is matched here. */
727 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
728 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200729 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730 c = getchr();
731 goto nfa_do_multibyte;
732 }
733#endif
734 EMIT(nfa_classcodes[p - classchars]);
735 if (extra == ADD_NL)
736 {
737 EMIT(NFA_NEWL);
738 EMIT(NFA_OR);
739 regflags |= RF_HASNL;
740 }
741 break;
742
743 case Magic('n'):
744 if (reg_string)
745 /* In a string "\n" matches a newline character. */
746 EMIT(NL);
747 else
748 {
749 /* In buffer text "\n" matches the end of a line. */
750 EMIT(NFA_NEWL);
751 regflags |= RF_HASNL;
752 }
753 break;
754
755 case Magic('('):
756 if (nfa_reg(REG_PAREN) == FAIL)
757 return FAIL; /* cascaded error */
758 break;
759
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200760 case Magic('|'):
761 case Magic('&'):
762 case Magic(')'):
763 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200764 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200765 return FAIL;
766
767 case Magic('='):
768 case Magic('?'):
769 case Magic('+'):
770 case Magic('@'):
771 case Magic('*'):
772 case Magic('{'):
773 /* these should follow an atom, not form an atom */
774 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200775 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 return FAIL;
777
778 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200779 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200780 return FAIL;
781
Bram Moolenaar428e9872013-05-30 17:05:39 +0200782 case Magic('1'):
783 case Magic('2'):
784 case Magic('3'):
785 case Magic('4'):
786 case Magic('5'):
787 case Magic('6'):
788 case Magic('7'):
789 case Magic('8'):
790 case Magic('9'):
791 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
792 nfa_has_backref = TRUE;
793 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200794
795 case Magic('z'):
796 c = no_Magic(getchr());
797 switch (c)
798 {
799 case 's':
800 EMIT(NFA_ZSTART);
801 break;
802 case 'e':
803 EMIT(NFA_ZEND);
804 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200805 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200806 case '1':
807 case '2':
808 case '3':
809 case '4':
810 case '5':
811 case '6':
812 case '7':
813 case '8':
814 case '9':
815 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200816 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 return FAIL;
818 default:
819 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200820 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 no_Magic(c));
822 return FAIL;
823 }
824 break;
825
826 case Magic('%'):
827 c = no_Magic(getchr());
828 switch (c)
829 {
830 /* () without a back reference */
831 case '(':
832 if (nfa_reg(REG_NPAREN) == FAIL)
833 return FAIL;
834 EMIT(NFA_NOPEN);
835 break;
836
837 case 'd': /* %d123 decimal */
838 case 'o': /* %o123 octal */
839 case 'x': /* %xab hex 2 */
840 case 'u': /* %uabcd hex 4 */
841 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200842 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200843 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200844
Bram Moolenaar47196582013-05-25 22:04:23 +0200845 switch (c)
846 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200847 case 'd': nr = getdecchrs(); break;
848 case 'o': nr = getoctchrs(); break;
849 case 'x': nr = gethexchrs(2); break;
850 case 'u': nr = gethexchrs(4); break;
851 case 'U': nr = gethexchrs(8); break;
852 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200853 }
854
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200855 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200856 EMSG2_RET_FAIL(
857 _("E678: Invalid character after %s%%[dxouU]"),
858 reg_magic == MAGIC_ALL);
859 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200860 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200861 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200862 break;
863
864 /* Catch \%^ and \%$ regardless of where they appear in the
865 * pattern -- regardless of whether or not it makes sense. */
866 case '^':
867 EMIT(NFA_BOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200868 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200869 return FAIL;
870 break;
871
872 case '$':
873 EMIT(NFA_EOF);
Bram Moolenaar5714b802013-05-28 22:03:20 +0200874 /* TODO: Not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 return FAIL;
876 break;
877
878 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200879 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 break;
881
882 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200883 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 return FAIL;
885 break;
886
887 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200888 /* TODO: \%[abc] not supported yet */
889 return FAIL;
890
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200891 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200892 {
893 long_u n = 0;
894 int cmp = c;
895
896 if (c == '<' || c == '>')
897 c = getchr();
898 while (VIM_ISDIGIT(c))
899 {
900 n = n * 10 + (c - '0');
901 c = getchr();
902 }
903 if (c == 'l' || c == 'c' || c == 'v')
904 {
905 EMIT(n);
906 if (c == 'l')
907 EMIT(cmp == '<' ? NFA_LNUM_LT :
908 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
909 else if (c == 'c')
910 EMIT(cmp == '<' ? NFA_COL_LT :
911 cmp == '>' ? NFA_COL_GT : NFA_COL);
912 else
913 EMIT(cmp == '<' ? NFA_VCOL_LT :
914 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
915 break;
916 }
917 else if (c == '\'')
918 /* TODO: \%'m not supported yet */
919 return FAIL;
920 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200921 syntax_error = TRUE;
922 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
923 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200924 return FAIL;
925 }
926 break;
927
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200928 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200929collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200930 /*
931 * Glue is emitted between several atoms from the [].
932 * It is either NFA_OR, or NFA_CONCAT.
933 *
934 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
935 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
936 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
937 * notation)
938 *
939 */
940
941
942/* Emit negation atoms, if needed.
943 * The CONCAT below merges the NOT with the previous node. */
944#define TRY_NEG() \
945 if (negated == TRUE) \
946 { \
947 EMIT(NFA_NOT); \
948 }
949
950/* Emit glue between important nodes : CONCAT or OR. */
951#define EMIT_GLUE() \
952 if (first == FALSE) \
953 EMIT(glue); \
954 else \
955 first = FALSE;
956
957 p = regparse;
958 endp = skip_anyof(p);
959 if (*endp == ']')
960 {
961 /*
962 * Try to reverse engineer character classes. For example,
963 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
964 * and perform the necessary substitutions in the NFA.
965 */
966 result = nfa_recognize_char_class(regparse, endp,
967 extra == ADD_NL);
968 if (result != FAIL)
969 {
970 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
971 EMIT(result);
972 else /* must be char class + newline */
973 {
974 EMIT(result - ADD_NL);
975 EMIT(NFA_NEWL);
976 EMIT(NFA_OR);
977 }
978 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200979 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200980 return OK;
981 }
982 /*
983 * Failed to recognize a character class. Use the simple
984 * version that turns [abc] into 'a' OR 'b' OR 'c'
985 */
986 startc = endc = oldstartc = -1;
987 first = TRUE; /* Emitting first atom in this sequence? */
988 negated = FALSE;
989 glue = NFA_OR;
990 if (*regparse == '^') /* negated range */
991 {
992 negated = TRUE;
993 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200994 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200995 }
996 if (*regparse == '-')
997 {
998 startc = '-';
999 EMIT(startc);
1000 TRY_NEG();
1001 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001002 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001003 }
1004 /* Emit the OR branches for each character in the [] */
1005 emit_range = FALSE;
1006 while (regparse < endp)
1007 {
1008 oldstartc = startc;
1009 startc = -1;
1010 got_coll_char = FALSE;
1011 if (*regparse == '[')
1012 {
1013 /* Check for [: :], [= =], [. .] */
1014 equiclass = collclass = 0;
1015 charclass = get_char_class(&regparse);
1016 if (charclass == CLASS_NONE)
1017 {
1018 equiclass = get_equi_class(&regparse);
1019 if (equiclass == 0)
1020 collclass = get_coll_element(&regparse);
1021 }
1022
1023 /* Character class like [:alpha:] */
1024 if (charclass != CLASS_NONE)
1025 {
1026 switch (charclass)
1027 {
1028 case CLASS_ALNUM:
1029 EMIT(NFA_CLASS_ALNUM);
1030 break;
1031 case CLASS_ALPHA:
1032 EMIT(NFA_CLASS_ALPHA);
1033 break;
1034 case CLASS_BLANK:
1035 EMIT(NFA_CLASS_BLANK);
1036 break;
1037 case CLASS_CNTRL:
1038 EMIT(NFA_CLASS_CNTRL);
1039 break;
1040 case CLASS_DIGIT:
1041 EMIT(NFA_CLASS_DIGIT);
1042 break;
1043 case CLASS_GRAPH:
1044 EMIT(NFA_CLASS_GRAPH);
1045 break;
1046 case CLASS_LOWER:
1047 EMIT(NFA_CLASS_LOWER);
1048 break;
1049 case CLASS_PRINT:
1050 EMIT(NFA_CLASS_PRINT);
1051 break;
1052 case CLASS_PUNCT:
1053 EMIT(NFA_CLASS_PUNCT);
1054 break;
1055 case CLASS_SPACE:
1056 EMIT(NFA_CLASS_SPACE);
1057 break;
1058 case CLASS_UPPER:
1059 EMIT(NFA_CLASS_UPPER);
1060 break;
1061 case CLASS_XDIGIT:
1062 EMIT(NFA_CLASS_XDIGIT);
1063 break;
1064 case CLASS_TAB:
1065 EMIT(NFA_CLASS_TAB);
1066 break;
1067 case CLASS_RETURN:
1068 EMIT(NFA_CLASS_RETURN);
1069 break;
1070 case CLASS_BACKSPACE:
1071 EMIT(NFA_CLASS_BACKSPACE);
1072 break;
1073 case CLASS_ESCAPE:
1074 EMIT(NFA_CLASS_ESCAPE);
1075 break;
1076 }
1077 TRY_NEG();
1078 EMIT_GLUE();
1079 continue;
1080 }
1081 /* Try equivalence class [=a=] and the like */
1082 if (equiclass != 0)
1083 {
1084 result = nfa_emit_equi_class(equiclass, negated);
1085 if (result == FAIL)
1086 {
1087 /* should never happen */
1088 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1089 }
1090 EMIT_GLUE();
1091 continue;
1092 }
1093 /* Try collating class like [. .] */
1094 if (collclass != 0)
1095 {
1096 startc = collclass; /* allow [.a.]-x as a range */
1097 /* Will emit the proper atom at the end of the
1098 * while loop. */
1099 }
1100 }
1101 /* Try a range like 'a-x' or '\t-z' */
1102 if (*regparse == '-')
1103 {
1104 emit_range = TRUE;
1105 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001106 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001107 continue; /* reading the end of the range */
1108 }
1109
1110 /* Now handle simple and escaped characters.
1111 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1112 * accepts "\t", "\e", etc., but only when the 'l' flag in
1113 * 'cpoptions' is not included.
1114 * Posix doesn't recognize backslash at all.
1115 */
1116 if (*regparse == '\\'
1117 && !cpo_bsl
1118 && regparse + 1 <= endp
1119 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1120 || (!cpo_lit
1121 && vim_strchr(REGEXP_ABBR, regparse[1])
1122 != NULL)
1123 )
1124 )
1125 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001126 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001127
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001128 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001129 startc = reg_string ? NL : NFA_NEWL;
1130 else
1131 if (*regparse == 'd'
1132 || *regparse == 'o'
1133 || *regparse == 'x'
1134 || *regparse == 'u'
1135 || *regparse == 'U'
1136 )
1137 {
1138 /* TODO(RE) This needs more testing */
1139 startc = coll_get_char();
1140 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001141 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001142 }
1143 else
1144 {
1145 /* \r,\t,\e,\b */
1146 startc = backslash_trans(*regparse);
1147 }
1148 }
1149
1150 /* Normal printable char */
1151 if (startc == -1)
1152#ifdef FEAT_MBYTE
1153 startc = (*mb_ptr2char)(regparse);
1154#else
1155 startc = *regparse;
1156#endif
1157
1158 /* Previous char was '-', so this char is end of range. */
1159 if (emit_range)
1160 {
1161 endc = startc; startc = oldstartc;
1162 if (startc > endc)
1163 EMSG_RET_FAIL(_(e_invrange));
1164#ifdef FEAT_MBYTE
1165 if (has_mbyte && ((*mb_char2len)(startc) > 1
1166 || (*mb_char2len)(endc) > 1))
1167 {
1168 if (endc > startc + 256)
1169 EMSG_RET_FAIL(_(e_invrange));
1170 /* Emit the range. "startc" was already emitted, so
1171 * skip it. */
1172 for (c = startc + 1; c <= endc; c++)
1173 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001174 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001175 TRY_NEG();
1176 EMIT_GLUE();
1177 }
1178 emit_range = FALSE;
1179 }
1180 else
1181#endif
1182 {
1183#ifdef EBCDIC
1184 int alpha_only = FALSE;
1185
1186 /* for alphabetical range skip the gaps
1187 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1188 if (isalpha(startc) && isalpha(endc))
1189 alpha_only = TRUE;
1190#endif
1191 /* Emit the range. "startc" was already emitted, so
1192 * skip it. */
1193 for (c = startc + 1; c <= endc; c++)
1194#ifdef EBCDIC
1195 if (!alpha_only || isalpha(startc))
1196#endif
1197 {
1198 EMIT(c);
1199 TRY_NEG();
1200 EMIT_GLUE();
1201 }
1202 emit_range = FALSE;
1203 }
1204 }
1205 else
1206 {
1207 /*
1208 * This char (startc) is not part of a range. Just
1209 * emit it.
1210 *
1211 * Normally, simply emit startc. But if we get char
1212 * code=0 from a collating char, then replace it with
1213 * 0x0a.
1214 *
1215 * This is needed to completely mimic the behaviour of
1216 * the backtracking engine.
1217 */
1218 if (got_coll_char == TRUE && startc == 0)
1219 EMIT(0x0a);
1220 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001221 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001222 TRY_NEG();
1223 EMIT_GLUE();
1224 }
1225
Bram Moolenaar51a29832013-05-28 22:30:35 +02001226 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 } /* while (p < endp) */
1228
Bram Moolenaar51a29832013-05-28 22:30:35 +02001229 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001230 if (*regparse == '-') /* if last, '-' is just a char */
1231 {
1232 EMIT('-');
1233 TRY_NEG();
1234 EMIT_GLUE();
1235 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001236 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001237
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 /* skip the trailing ] */
1239 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001240 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 if (negated == TRUE)
1242 {
1243 /* Mark end of negated char range */
1244 EMIT(NFA_END_NEG_RANGE);
1245 EMIT(NFA_CONCAT);
1246 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001247
1248 /* \_[] also matches \n but it's not negated */
1249 if (extra == ADD_NL)
1250 {
1251 EMIT(reg_string ? NL : NFA_NEWL);
1252 EMIT(NFA_OR);
1253 }
1254
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001256 } /* if exists closing ] */
1257
1258 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259 {
1260 syntax_error = TRUE;
1261 EMSG_RET_FAIL(_(e_missingbracket));
1262 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001263 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001265 default:
1266 {
1267#ifdef FEAT_MBYTE
1268 int plen;
1269
1270nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001271 /* plen is length of current char with composing chars */
1272 if (enc_utf8 && ((*mb_char2len)(c)
1273 != (plen = (*mb_ptr2len)(old_regparse))
1274 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001275 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001276 int i = 0;
1277
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001278 /* A base character plus composing characters, or just one
1279 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001280 * This requires creating a separate atom as if enclosing
1281 * the characters in (), where NFA_COMPOSING is the ( and
1282 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001283 * building the postfix form, not the NFA itself;
1284 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001285 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001286 for (;;)
1287 {
1288 EMIT(c);
1289 if (i > 0)
1290 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001291 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001292 break;
1293 c = utf_ptr2char(old_regparse + i);
1294 }
1295 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001296 regparse = old_regparse + plen;
1297 }
1298 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001299#endif
1300 {
1301 c = no_Magic(c);
1302 EMIT(c);
1303 }
1304 return OK;
1305 }
1306 }
1307
1308#undef TRY_NEG
1309#undef EMIT_GLUE
1310
1311 return OK;
1312}
1313
1314/*
1315 * Parse something followed by possible [*+=].
1316 *
1317 * A piece is an atom, possibly followed by a multi, an indication of how many
1318 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1319 * characters: "", "a", "aa", etc.
1320 *
1321 * piece ::= atom
1322 * or atom multi
1323 */
1324 static int
1325nfa_regpiece()
1326{
1327 int i;
1328 int op;
1329 int ret;
1330 long minval, maxval;
1331 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1332 char_u *old_regparse, *new_regparse;
1333 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001334 int old_post_pos;
1335 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 int old_regnpar;
1337 int quest;
1338
1339 /* Save the current position in the regexp, so that we can use it if
1340 * <atom>{m,n} is next. */
1341 old_regparse = regparse;
1342 /* Save current number of open parenthesis, so we can use it if
1343 * <atom>{m,n} is next */
1344 old_regnpar = regnpar;
1345 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001346 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001347
1348 ret = nfa_regatom();
1349 if (ret == FAIL)
1350 return FAIL; /* cascaded error */
1351
1352 op = peekchr();
1353 if (re_multi_type(op) == NOT_MULTI)
1354 return OK;
1355
1356 skipchr();
1357 switch (op)
1358 {
1359 case Magic('*'):
1360 EMIT(NFA_STAR);
1361 break;
1362
1363 case Magic('+'):
1364 /*
1365 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1366 * first and only submatch would be "aaa". But the backtracking
1367 * engine interprets the plus as "try matching one more time", and
1368 * a* matches a second time at the end of the input, the empty
1369 * string.
1370 * The submatch will the empty string.
1371 *
1372 * In order to be consistent with the old engine, we disable
1373 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1374 */
1375 /* EMIT(NFA_PLUS); */
1376 regnpar = old_regnpar;
1377 regparse = old_regparse;
1378 curchr = -1;
1379 if (nfa_regatom() == FAIL)
1380 return FAIL;
1381 EMIT(NFA_STAR);
1382 EMIT(NFA_CONCAT);
1383 skipchr(); /* skip the \+ */
1384 break;
1385
1386 case Magic('@'):
1387 op = no_Magic(getchr());
1388 switch(op)
1389 {
1390 case '=':
1391 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1392 break;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001393 case '0':
1394 case '1':
1395 case '2':
1396 case '3':
1397 case '4':
1398 case '5':
1399 case '6':
1400 case '7':
1401 case '8':
1402 case '9':
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 case '!':
1404 case '<':
1405 case '>':
1406 /* Not supported yet */
1407 return FAIL;
1408 default:
1409 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001410 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001411 return FAIL;
1412 }
1413 break;
1414
1415 case Magic('?'):
1416 case Magic('='):
1417 EMIT(NFA_QUEST);
1418 break;
1419
1420 case Magic('{'):
1421 /* a{2,5} will expand to 'aaa?a?a?'
1422 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1423 * version of '?'
1424 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1425 * parenthesis have the same id
1426 */
1427
1428 greedy = TRUE;
1429 c2 = peekchr();
1430 if (c2 == '-' || c2 == Magic('-'))
1431 {
1432 skipchr();
1433 greedy = FALSE;
1434 }
1435 if (!read_limits(&minval, &maxval))
1436 {
1437 syntax_error = TRUE;
1438 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1439 }
1440 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1441 * <atom>* */
1442 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1443 {
1444 EMIT(NFA_STAR);
1445 break;
1446 }
1447
1448 if (maxval > NFA_BRACES_MAXLIMIT)
1449 {
1450 /* This would yield a huge automaton and use too much memory.
1451 * Revert to old engine */
1452 return FAIL;
1453 }
1454
1455 /* Special case: x{0} or x{-0} */
1456 if (maxval == 0)
1457 {
1458 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001459 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001460 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1461 EMIT(NFA_SKIP_CHAR);
1462 return OK;
1463 }
1464
1465 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001466 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 /* Save pos after the repeated atom and the \{} */
1468 new_regparse = regparse;
1469
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001470 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1471 for (i = 0; i < maxval; i++)
1472 {
1473 /* Goto beginning of the repeated atom */
1474 regparse = old_regparse;
1475 curchr = -1;
1476 /* Restore count of parenthesis */
1477 regnpar = old_regnpar;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001478 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001479 if (nfa_regatom() == FAIL)
1480 return FAIL;
1481 /* after "minval" times, atoms are optional */
1482 if (i + 1 > minval)
1483 EMIT(quest);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001484 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 EMIT(NFA_CONCAT);
1486 }
1487
1488 /* Go to just after the repeated atom and the \{} */
1489 regparse = new_regparse;
1490 curchr = -1;
1491
1492 break;
1493
1494
1495 default:
1496 break;
1497 } /* end switch */
1498
1499 if (re_multi_type(peekchr()) != NOT_MULTI)
1500 {
1501 /* Can't have a multi follow a multi. */
1502 syntax_error = TRUE;
1503 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1504 }
1505
1506 return OK;
1507}
1508
1509/*
1510 * Parse one or more pieces, concatenated. It matches a match for the
1511 * first piece, followed by a match for the second piece, etc. Example:
1512 * "f[0-9]b", first matches "f", then a digit and then "b".
1513 *
1514 * concat ::= piece
1515 * or piece piece
1516 * or piece piece piece
1517 * etc.
1518 */
1519 static int
1520nfa_regconcat()
1521{
1522 int cont = TRUE;
1523 int first = TRUE;
1524
1525 while (cont)
1526 {
1527 switch (peekchr())
1528 {
1529 case NUL:
1530 case Magic('|'):
1531 case Magic('&'):
1532 case Magic(')'):
1533 cont = FALSE;
1534 break;
1535
1536 case Magic('Z'):
1537#ifdef FEAT_MBYTE
1538 regflags |= RF_ICOMBINE;
1539#endif
1540 skipchr_keepstart();
1541 break;
1542 case Magic('c'):
1543 regflags |= RF_ICASE;
1544 skipchr_keepstart();
1545 break;
1546 case Magic('C'):
1547 regflags |= RF_NOICASE;
1548 skipchr_keepstart();
1549 break;
1550 case Magic('v'):
1551 reg_magic = MAGIC_ALL;
1552 skipchr_keepstart();
1553 curchr = -1;
1554 break;
1555 case Magic('m'):
1556 reg_magic = MAGIC_ON;
1557 skipchr_keepstart();
1558 curchr = -1;
1559 break;
1560 case Magic('M'):
1561 reg_magic = MAGIC_OFF;
1562 skipchr_keepstart();
1563 curchr = -1;
1564 break;
1565 case Magic('V'):
1566 reg_magic = MAGIC_NONE;
1567 skipchr_keepstart();
1568 curchr = -1;
1569 break;
1570
1571 default:
1572 if (nfa_regpiece() == FAIL)
1573 return FAIL;
1574 if (first == FALSE)
1575 EMIT(NFA_CONCAT);
1576 else
1577 first = FALSE;
1578 break;
1579 }
1580 }
1581
1582 return OK;
1583}
1584
1585/*
1586 * Parse a branch, one or more concats, separated by "\&". It matches the
1587 * last concat, but only if all the preceding concats also match at the same
1588 * position. Examples:
1589 * "foobeep\&..." matches "foo" in "foobeep".
1590 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1591 *
1592 * branch ::= concat
1593 * or concat \& concat
1594 * or concat \& concat \& concat
1595 * etc.
1596 */
1597 static int
1598nfa_regbranch()
1599{
1600 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001601 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602
Bram Moolenaar16299b52013-05-30 18:45:23 +02001603 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604
1605 /* First branch, possibly the only one */
1606 if (nfa_regconcat() == FAIL)
1607 return FAIL;
1608
1609 ch = peekchr();
1610 /* Try next concats */
1611 while (ch == Magic('&'))
1612 {
1613 skipchr();
1614 EMIT(NFA_NOPEN);
1615 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001616 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001617 if (nfa_regconcat() == FAIL)
1618 return FAIL;
1619 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001620 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001621 EMIT(NFA_SKIP_CHAR);
1622 EMIT(NFA_CONCAT);
1623 ch = peekchr();
1624 }
1625
1626 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001627 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 EMIT(NFA_SKIP_CHAR);
1629
1630 return OK;
1631}
1632
1633/*
1634 * Parse a pattern, one or more branches, separated by "\|". It matches
1635 * anything that matches one of the branches. Example: "foo\|beep" matches
1636 * "foo" and matches "beep". If more than one branch matches, the first one
1637 * is used.
1638 *
1639 * pattern ::= branch
1640 * or branch \| branch
1641 * or branch \| branch \| branch
1642 * etc.
1643 */
1644 static int
1645nfa_reg(paren)
1646 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1647{
1648 int parno = 0;
1649
1650#ifdef FEAT_SYN_HL
1651#endif
1652 if (paren == REG_PAREN)
1653 {
1654 if (regnpar >= NSUBEXP) /* Too many `(' */
1655 {
1656 syntax_error = TRUE;
1657 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1658 }
1659 parno = regnpar++;
1660 }
1661
1662 if (nfa_regbranch() == FAIL)
1663 return FAIL; /* cascaded error */
1664
1665 while (peekchr() == Magic('|'))
1666 {
1667 skipchr();
1668 if (nfa_regbranch() == FAIL)
1669 return FAIL; /* cascaded error */
1670 EMIT(NFA_OR);
1671 }
1672
1673 /* Check for proper termination. */
1674 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1675 {
1676 syntax_error = TRUE;
1677 if (paren == REG_NPAREN)
1678 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1679 else
1680 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1681 }
1682 else if (paren == REG_NOPAREN && peekchr() != NUL)
1683 {
1684 syntax_error = TRUE;
1685 if (peekchr() == Magic(')'))
1686 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1687 else
1688 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1689 }
1690 /*
1691 * Here we set the flag allowing back references to this set of
1692 * parentheses.
1693 */
1694 if (paren == REG_PAREN)
1695 {
1696 had_endbrace[parno] = TRUE; /* have seen the close paren */
1697 EMIT(NFA_MOPEN + parno);
1698 }
1699
1700 return OK;
1701}
1702
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001703#ifdef DEBUG
1704static char_u code[50];
1705
1706 static void
1707nfa_set_code(c)
1708 int c;
1709{
1710 int addnl = FALSE;
1711
1712 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1713 {
1714 addnl = TRUE;
1715 c -= ADD_NL;
1716 }
1717
1718 STRCPY(code, "");
1719 switch (c)
1720 {
1721 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1722 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1723 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1724 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1725 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1726 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1727
Bram Moolenaar5714b802013-05-28 22:03:20 +02001728 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1729 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1730 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1731 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1732 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1733 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1734 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1735 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1736 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1737 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1738
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 case NFA_PREV_ATOM_NO_WIDTH:
1740 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001741 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1742 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1744 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1745 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1746 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1747
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001748 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1749 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1750
1751 case NFA_MOPEN + 0:
1752 case NFA_MOPEN + 1:
1753 case NFA_MOPEN + 2:
1754 case NFA_MOPEN + 3:
1755 case NFA_MOPEN + 4:
1756 case NFA_MOPEN + 5:
1757 case NFA_MOPEN + 6:
1758 case NFA_MOPEN + 7:
1759 case NFA_MOPEN + 8:
1760 case NFA_MOPEN + 9:
1761 STRCPY(code, "NFA_MOPEN(x)");
1762 code[10] = c - NFA_MOPEN + '0';
1763 break;
1764 case NFA_MCLOSE + 0:
1765 case NFA_MCLOSE + 1:
1766 case NFA_MCLOSE + 2:
1767 case NFA_MCLOSE + 3:
1768 case NFA_MCLOSE + 4:
1769 case NFA_MCLOSE + 5:
1770 case NFA_MCLOSE + 6:
1771 case NFA_MCLOSE + 7:
1772 case NFA_MCLOSE + 8:
1773 case NFA_MCLOSE + 9:
1774 STRCPY(code, "NFA_MCLOSE(x)");
1775 code[11] = c - NFA_MCLOSE + '0';
1776 break;
1777 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1778 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1779 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1780 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1781 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1782 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1783 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1784 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1785 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1786 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1787 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1788 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1789 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1790 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1791 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1792 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1793 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1794 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1795 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1796 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1797 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1798 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1799 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1800 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1801 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1802 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1803 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1804 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1805
1806 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1807 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1808 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1809 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1810 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1811 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1812 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1813 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1814 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1815 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1816 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1817 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1818 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1819 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1820 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1821 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1822 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1823 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1824 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1825 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1826 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1827 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1828 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1829 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1830 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1831 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1832 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1833
1834 default:
1835 STRCPY(code, "CHAR(x)");
1836 code[5] = c;
1837 }
1838
1839 if (addnl == TRUE)
1840 STRCAT(code, " + NEWLINE ");
1841
1842}
1843
1844#ifdef ENABLE_LOG
1845static FILE *log_fd;
1846
1847/*
1848 * Print the postfix notation of the current regexp.
1849 */
1850 static void
1851nfa_postfix_dump(expr, retval)
1852 char_u *expr;
1853 int retval;
1854{
1855 int *p;
1856 FILE *f;
1857
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001858 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859 if (f != NULL)
1860 {
1861 fprintf(f, "\n-------------------------\n");
1862 if (retval == FAIL)
1863 fprintf(f, ">>> NFA engine failed ... \n");
1864 else if (retval == OK)
1865 fprintf(f, ">>> NFA engine succeeded !\n");
1866 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001867 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 {
1869 nfa_set_code(*p);
1870 fprintf(f, "%s, ", code);
1871 }
1872 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001873 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 fprintf(f, "%d ", *p);
1875 fprintf(f, "\n\n");
1876 fclose(f);
1877 }
1878}
1879
1880/*
1881 * Print the NFA starting with a root node "state".
1882 */
1883 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001884nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885 FILE *debugf;
1886 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001888 garray_T indent;
1889
1890 ga_init2(&indent, 1, 64);
1891 ga_append(&indent, '\0');
1892 nfa_print_state2(debugf, state, &indent);
1893 ga_clear(&indent);
1894}
1895
1896 static void
1897nfa_print_state2(debugf, state, indent)
1898 FILE *debugf;
1899 nfa_state_T *state;
1900 garray_T *indent;
1901{
1902 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903
1904 if (state == NULL)
1905 return;
1906
1907 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001908
1909 /* Output indent */
1910 p = (char_u *)indent->ga_data;
1911 if (indent->ga_len >= 3)
1912 {
1913 int last = indent->ga_len - 3;
1914 char_u save[2];
1915
1916 STRNCPY(save, &p[last], 2);
1917 STRNCPY(&p[last], "+-", 2);
1918 fprintf(debugf, " %s", p);
1919 STRNCPY(&p[last], save, 2);
1920 }
1921 else
1922 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923
1924 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001925 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1926 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 if (state->id < 0)
1928 return;
1929
1930 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001931
1932 /* grow indent for state->out */
1933 indent->ga_len -= 1;
1934 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001935 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001936 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001937 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001938 ga_append(indent, '\0');
1939
1940 nfa_print_state2(debugf, state->out, indent);
1941
1942 /* replace last part of indent for state->out1 */
1943 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001944 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001945 ga_append(indent, '\0');
1946
1947 nfa_print_state2(debugf, state->out1, indent);
1948
1949 /* shrink indent */
1950 indent->ga_len -= 3;
1951 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952}
1953
1954/*
1955 * Print the NFA state machine.
1956 */
1957 static void
1958nfa_dump(prog)
1959 nfa_regprog_T *prog;
1960{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001961 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962
1963 if (debugf != NULL)
1964 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001965 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966 fclose(debugf);
1967 }
1968}
1969#endif /* ENABLE_LOG */
1970#endif /* DEBUG */
1971
1972/*
1973 * Parse r.e. @expr and convert it into postfix form.
1974 * Return the postfix string on success, NULL otherwise.
1975 */
1976 static int *
1977re2post()
1978{
1979 if (nfa_reg(REG_NOPAREN) == FAIL)
1980 return NULL;
1981 EMIT(NFA_MOPEN);
1982 return post_start;
1983}
1984
1985/* NB. Some of the code below is inspired by Russ's. */
1986
1987/*
1988 * Represents an NFA state plus zero or one or two arrows exiting.
1989 * if c == MATCH, no arrows out; matching state.
1990 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1991 * If c < 256, labeled arrow with character c to out.
1992 */
1993
1994static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1995
1996/*
1997 * Allocate and initialize nfa_state_T.
1998 */
1999 static nfa_state_T *
2000new_state(c, out, out1)
2001 int c;
2002 nfa_state_T *out;
2003 nfa_state_T *out1;
2004{
2005 nfa_state_T *s;
2006
2007 if (istate >= nstate)
2008 return NULL;
2009
2010 s = &state_ptr[istate++];
2011
2012 s->c = c;
2013 s->out = out;
2014 s->out1 = out1;
2015
2016 s->id = istate;
2017 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018 s->negated = FALSE;
2019
2020 return s;
2021}
2022
2023/*
2024 * A partially built NFA without the matching state filled in.
2025 * Frag_T.start points at the start state.
2026 * Frag_T.out is a list of places that need to be set to the
2027 * next state for this fragment.
2028 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002029
2030/* Since the out pointers in the list are always
2031 * uninitialized, we use the pointers themselves
2032 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002034union Ptrlist
2035{
2036 Ptrlist *next;
2037 nfa_state_T *s;
2038};
2039
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040struct Frag
2041{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002042 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 Ptrlist *out;
2044};
2045typedef struct Frag Frag_T;
2046
2047static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2048static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2049static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2050static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2051static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2052static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2053
2054/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002055 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056 */
2057 static Frag_T
2058frag(start, out)
2059 nfa_state_T *start;
2060 Ptrlist *out;
2061{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002062 Frag_T n;
2063
2064 n.start = start;
2065 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002066 return n;
2067}
2068
2069/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 * Create singleton list containing just outp.
2071 */
2072 static Ptrlist *
2073list1(outp)
2074 nfa_state_T **outp;
2075{
2076 Ptrlist *l;
2077
2078 l = (Ptrlist *)outp;
2079 l->next = NULL;
2080 return l;
2081}
2082
2083/*
2084 * Patch the list of states at out to point to start.
2085 */
2086 static void
2087patch(l, s)
2088 Ptrlist *l;
2089 nfa_state_T *s;
2090{
2091 Ptrlist *next;
2092
2093 for (; l; l = next)
2094 {
2095 next = l->next;
2096 l->s = s;
2097 }
2098}
2099
2100
2101/*
2102 * Join the two lists l1 and l2, returning the combination.
2103 */
2104 static Ptrlist *
2105append(l1, l2)
2106 Ptrlist *l1;
2107 Ptrlist *l2;
2108{
2109 Ptrlist *oldl1;
2110
2111 oldl1 = l1;
2112 while (l1->next)
2113 l1 = l1->next;
2114 l1->next = l2;
2115 return oldl1;
2116}
2117
2118/*
2119 * Stack used for transforming postfix form into NFA.
2120 */
2121static Frag_T empty;
2122
2123 static void
2124st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002125 int *postfix UNUSED;
2126 int *end UNUSED;
2127 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002128{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002129#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002130 FILE *df;
2131 int *p2;
2132
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002133 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002134 if (df)
2135 {
2136 fprintf(df, "Error popping the stack!\n");
2137#ifdef DEBUG
2138 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2139#endif
2140 fprintf(df, "Postfix form is: ");
2141#ifdef DEBUG
2142 for (p2 = postfix; p2 < end; p2++)
2143 {
2144 nfa_set_code(*p2);
2145 fprintf(df, "%s, ", code);
2146 }
2147 nfa_set_code(*p);
2148 fprintf(df, "\nCurrent position is: ");
2149 for (p2 = postfix; p2 <= p; p2 ++)
2150 {
2151 nfa_set_code(*p2);
2152 fprintf(df, "%s, ", code);
2153 }
2154#else
2155 for (p2 = postfix; p2 < end; p2++)
2156 {
2157 fprintf(df, "%d, ", *p2);
2158 }
2159 fprintf(df, "\nCurrent position is: ");
2160 for (p2 = postfix; p2 <= p; p2 ++)
2161 {
2162 fprintf(df, "%d, ", *p2);
2163 }
2164#endif
2165 fprintf(df, "\n--------------------------\n");
2166 fclose(df);
2167 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002168#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002169 EMSG(_("E874: (NFA) Could not pop the stack !"));
2170}
2171
2172/*
2173 * Push an item onto the stack.
2174 */
2175 static void
2176st_push(s, p, stack_end)
2177 Frag_T s;
2178 Frag_T **p;
2179 Frag_T *stack_end;
2180{
2181 Frag_T *stackp = *p;
2182
2183 if (stackp >= stack_end)
2184 return;
2185 *stackp = s;
2186 *p = *p + 1;
2187}
2188
2189/*
2190 * Pop an item from the stack.
2191 */
2192 static Frag_T
2193st_pop(p, stack)
2194 Frag_T **p;
2195 Frag_T *stack;
2196{
2197 Frag_T *stackp;
2198
2199 *p = *p - 1;
2200 stackp = *p;
2201 if (stackp < stack)
2202 return empty;
2203 return **p;
2204}
2205
2206/*
2207 * Convert a postfix form into its equivalent NFA.
2208 * Return the NFA start state on success, NULL otherwise.
2209 */
2210 static nfa_state_T *
2211post2nfa(postfix, end, nfa_calc_size)
2212 int *postfix;
2213 int *end;
2214 int nfa_calc_size;
2215{
2216 int *p;
2217 int mopen;
2218 int mclose;
2219 Frag_T *stack = NULL;
2220 Frag_T *stackp = NULL;
2221 Frag_T *stack_end = NULL;
2222 Frag_T e1;
2223 Frag_T e2;
2224 Frag_T e;
2225 nfa_state_T *s;
2226 nfa_state_T *s1;
2227 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002228 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229
2230 if (postfix == NULL)
2231 return NULL;
2232
Bram Moolenaar053bb602013-05-20 13:55:21 +02002233#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234#define POP() st_pop(&stackp, stack); \
2235 if (stackp < stack) \
2236 { \
2237 st_error(postfix, end, p); \
2238 return NULL; \
2239 }
2240
2241 if (nfa_calc_size == FALSE)
2242 {
2243 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002244 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002245 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002246 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002247 }
2248
2249 for (p = postfix; p < end; ++p)
2250 {
2251 switch (*p)
2252 {
2253 case NFA_CONCAT:
2254 /* Catenation.
2255 * Pay attention: this operator does not exist
2256 * in the r.e. itself (it is implicit, really).
2257 * It is added when r.e. is translated to postfix
2258 * form in re2post().
2259 *
2260 * No new state added here. */
2261 if (nfa_calc_size == TRUE)
2262 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002263 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002264 break;
2265 }
2266 e2 = POP();
2267 e1 = POP();
2268 patch(e1.out, e2.start);
2269 PUSH(frag(e1.start, e2.out));
2270 break;
2271
2272 case NFA_NOT:
2273 /* Negation of a character */
2274 if (nfa_calc_size == TRUE)
2275 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002276 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277 break;
2278 }
2279 e1 = POP();
2280 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002281#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002282 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002284#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 PUSH(e1);
2286 break;
2287
2288 case NFA_OR:
2289 /* Alternation */
2290 if (nfa_calc_size == TRUE)
2291 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002292 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293 break;
2294 }
2295 e2 = POP();
2296 e1 = POP();
2297 s = new_state(NFA_SPLIT, e1.start, e2.start);
2298 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002299 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300 PUSH(frag(s, append(e1.out, e2.out)));
2301 break;
2302
2303 case NFA_STAR:
2304 /* Zero or more */
2305 if (nfa_calc_size == TRUE)
2306 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002307 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002308 break;
2309 }
2310 e = POP();
2311 s = new_state(NFA_SPLIT, e.start, NULL);
2312 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002313 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314 patch(e.out, s);
2315 PUSH(frag(s, list1(&s->out1)));
2316 break;
2317
2318 case NFA_QUEST:
2319 /* one or zero atoms=> greedy match */
2320 if (nfa_calc_size == TRUE)
2321 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002322 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002323 break;
2324 }
2325 e = POP();
2326 s = new_state(NFA_SPLIT, e.start, NULL);
2327 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002328 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329 PUSH(frag(s, append(e.out, list1(&s->out1))));
2330 break;
2331
2332 case NFA_QUEST_NONGREEDY:
2333 /* zero or one atoms => non-greedy match */
2334 if (nfa_calc_size == TRUE)
2335 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002336 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002337 break;
2338 }
2339 e = POP();
2340 s = new_state(NFA_SPLIT, NULL, e.start);
2341 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002342 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002343 PUSH(frag(s, append(e.out, list1(&s->out))));
2344 break;
2345
2346 case NFA_PLUS:
2347 /* One or more */
2348 if (nfa_calc_size == TRUE)
2349 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002350 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002351 break;
2352 }
2353 e = POP();
2354 s = new_state(NFA_SPLIT, e.start, NULL);
2355 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002356 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357 patch(e.out, s);
2358 PUSH(frag(e.start, list1(&s->out1)));
2359 break;
2360
2361 case NFA_SKIP_CHAR:
2362 /* Symbol of 0-length, Used in a repetition
2363 * with max/min count of 0 */
2364 if (nfa_calc_size == TRUE)
2365 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002366 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002367 break;
2368 }
2369 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2370 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002371 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 PUSH(frag(s, list1(&s->out)));
2373 break;
2374
2375 case NFA_PREV_ATOM_NO_WIDTH:
2376 /* The \@= operator: match the preceding atom with 0 width.
2377 * Surrounds the preceding atom with START_INVISIBLE and
2378 * END_INVISIBLE, similarly to MOPEN.
2379 */
2380 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002381 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002382
2383 if (nfa_calc_size == TRUE)
2384 {
2385 nstate += 2;
2386 break;
2387 }
2388 e = POP();
2389 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2390 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002391 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002392 patch(e.out, s1);
2393
2394 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2395 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002396 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002397 PUSH(frag(s, list1(&s1->out)));
2398 break;
2399
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002400#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002401 case NFA_COMPOSING: /* char with composing char */
2402#if 0
2403 /* TODO */
2404 if (regflags & RF_ICOMBINE)
2405 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002406 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002407 }
2408#endif
2409 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002410#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002411
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412 case NFA_MOPEN + 0: /* Submatch */
2413 case NFA_MOPEN + 1:
2414 case NFA_MOPEN + 2:
2415 case NFA_MOPEN + 3:
2416 case NFA_MOPEN + 4:
2417 case NFA_MOPEN + 5:
2418 case NFA_MOPEN + 6:
2419 case NFA_MOPEN + 7:
2420 case NFA_MOPEN + 8:
2421 case NFA_MOPEN + 9:
2422 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002423 if (nfa_calc_size == TRUE)
2424 {
2425 nstate += 2;
2426 break;
2427 }
2428
2429 mopen = *p;
2430 switch (*p)
2431 {
2432 case NFA_NOPEN:
2433 mclose = NFA_NCLOSE;
2434 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002435#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436 case NFA_COMPOSING:
2437 mclose = NFA_END_COMPOSING;
2438 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002439#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440 default:
2441 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2442 mclose = *p + NSUBEXP;
2443 break;
2444 }
2445
2446 /* Allow "NFA_MOPEN" as a valid postfix representation for
2447 * the empty regexp "". In this case, the NFA will be
2448 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2449 * empty groups of parenthesis, and empty mbyte chars */
2450 if (stackp == stack)
2451 {
2452 s = new_state(mopen, NULL, NULL);
2453 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002454 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002455 s1 = new_state(mclose, NULL, NULL);
2456 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002457 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458 patch(list1(&s->out), s1);
2459 PUSH(frag(s, list1(&s1->out)));
2460 break;
2461 }
2462
2463 /* At least one node was emitted before NFA_MOPEN, so
2464 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2465 e = POP();
2466 s = new_state(mopen, e.start, NULL); /* `(' */
2467 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002468 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469
2470 s1 = new_state(mclose, NULL, NULL); /* `)' */
2471 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002472 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 patch(e.out, s1);
2474
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002475#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002476 if (mopen == NFA_COMPOSING)
2477 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002478 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002479#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002480
2481 PUSH(frag(s, list1(&s1->out)));
2482 break;
2483
Bram Moolenaar5714b802013-05-28 22:03:20 +02002484 case NFA_BACKREF1:
2485 case NFA_BACKREF2:
2486 case NFA_BACKREF3:
2487 case NFA_BACKREF4:
2488 case NFA_BACKREF5:
2489 case NFA_BACKREF6:
2490 case NFA_BACKREF7:
2491 case NFA_BACKREF8:
2492 case NFA_BACKREF9:
2493 if (nfa_calc_size == TRUE)
2494 {
2495 nstate += 2;
2496 break;
2497 }
2498 s = new_state(*p, NULL, NULL);
2499 if (s == NULL)
2500 goto theend;
2501 s1 = new_state(NFA_SKIP, NULL, NULL);
2502 if (s1 == NULL)
2503 goto theend;
2504 patch(list1(&s->out), s1);
2505 PUSH(frag(s, list1(&s1->out)));
2506 break;
2507
Bram Moolenaar423532e2013-05-29 21:14:42 +02002508 case NFA_LNUM:
2509 case NFA_LNUM_GT:
2510 case NFA_LNUM_LT:
2511 case NFA_VCOL:
2512 case NFA_VCOL_GT:
2513 case NFA_VCOL_LT:
2514 case NFA_COL:
2515 case NFA_COL_GT:
2516 case NFA_COL_LT:
2517 if (nfa_calc_size == TRUE)
2518 {
2519 nstate += 1;
2520 break;
2521 }
2522 e1 = POP();
2523 s = new_state(*p, NULL, NULL);
2524 if (s == NULL)
2525 goto theend;
2526 s->val = e1.start->c;
2527 PUSH(frag(s, list1(&s->out)));
2528 break;
2529
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002530 case NFA_ZSTART:
2531 case NFA_ZEND:
2532 default:
2533 /* Operands */
2534 if (nfa_calc_size == TRUE)
2535 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002536 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002537 break;
2538 }
2539 s = new_state(*p, NULL, NULL);
2540 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002541 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002542 PUSH(frag(s, list1(&s->out)));
2543 break;
2544
2545 } /* switch(*p) */
2546
2547 } /* for(p = postfix; *p; ++p) */
2548
2549 if (nfa_calc_size == TRUE)
2550 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002551 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002552 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002553 }
2554
2555 e = POP();
2556 if (stackp != stack)
2557 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2558
2559 if (istate >= nstate)
2560 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562 matchstate = &state_ptr[istate++]; /* the match state */
2563 matchstate->c = NFA_MATCH;
2564 matchstate->out = matchstate->out1 = NULL;
2565
2566 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002567 ret = e.start;
2568
2569theend:
2570 vim_free(stack);
2571 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002572
2573#undef POP1
2574#undef PUSH1
2575#undef POP2
2576#undef PUSH2
2577#undef POP
2578#undef PUSH
2579}
2580
2581/****************************************************************
2582 * NFA execution code.
2583 ****************************************************************/
2584
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002585typedef struct
2586{
2587 int in_use; /* number of subexpr with useful info */
2588
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002589 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002590 union
2591 {
2592 struct multipos
2593 {
2594 lpos_T start;
2595 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002596 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002597 struct linepos
2598 {
2599 char_u *start;
2600 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002601 } line[NSUBEXP];
2602 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002603} regsub_T;
2604
Bram Moolenaar963fee22013-05-26 21:47:28 +02002605/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002606typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002607{
2608 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002609 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002610 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002611} nfa_thread_T;
2612
Bram Moolenaar963fee22013-05-26 21:47:28 +02002613/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614typedef struct
2615{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002616 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002617 int n; /* nr of states currently in "t" */
2618 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002619 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002620} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002621
Bram Moolenaar5714b802013-05-28 22:03:20 +02002622#ifdef ENABLE_LOG
2623 static void
2624log_subexpr(sub)
2625 regsub_T *sub;
2626{
2627 int j;
2628
2629 for (j = 0; j < sub->in_use; j++)
2630 if (REG_MULTI)
2631 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2632 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002633 sub->list.multi[j].start.col,
2634 (int)sub->list.multi[j].start.lnum,
2635 sub->list.multi[j].end.col,
2636 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002637 else
2638 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2639 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002640 (char *)sub->list.line[j].start,
2641 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002642 fprintf(log_fd, "\n");
2643}
2644#endif
2645
Bram Moolenaar963fee22013-05-26 21:47:28 +02002646/* Used during execution: whether a match has been found. */
2647static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002648
Bram Moolenaar428e9872013-05-30 17:05:39 +02002649static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002650static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2651static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002652
Bram Moolenaar428e9872013-05-30 17:05:39 +02002653/*
2654 * Return TRUE if "sub1" and "sub2" have the same positions.
2655 */
2656 static int
2657sub_equal(sub1, sub2)
2658 regsub_T *sub1;
2659 regsub_T *sub2;
2660{
2661 int i;
2662 int todo;
2663 linenr_T s1, e1;
2664 linenr_T s2, e2;
2665 char_u *sp1, *ep1;
2666 char_u *sp2, *ep2;
2667
2668 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2669 if (REG_MULTI)
2670 {
2671 for (i = 0; i < todo; ++i)
2672 {
2673 if (i < sub1->in_use)
2674 {
2675 s1 = sub1->list.multi[i].start.lnum;
2676 e1 = sub1->list.multi[i].end.lnum;
2677 }
2678 else
2679 {
2680 s1 = 0;
2681 e1 = 0;
2682 }
2683 if (i < sub2->in_use)
2684 {
2685 s2 = sub2->list.multi[i].start.lnum;
2686 e2 = sub2->list.multi[i].end.lnum;
2687 }
2688 else
2689 {
2690 s2 = 0;
2691 e2 = 0;
2692 }
2693 if (s1 != s2 || e1 != e2)
2694 return FALSE;
2695 if (s1 != 0 && sub1->list.multi[i].start.col
2696 != sub2->list.multi[i].start.col)
2697 return FALSE;
2698 if (e1 != 0 && sub1->list.multi[i].end.col
2699 != sub2->list.multi[i].end.col)
2700 return FALSE;
2701 }
2702 }
2703 else
2704 {
2705 for (i = 0; i < todo; ++i)
2706 {
2707 if (i < sub1->in_use)
2708 {
2709 sp1 = sub1->list.line[i].start;
2710 ep1 = sub1->list.line[i].end;
2711 }
2712 else
2713 {
2714 sp1 = NULL;
2715 ep1 = NULL;
2716 }
2717 if (i < sub2->in_use)
2718 {
2719 sp2 = sub2->list.line[i].start;
2720 ep2 = sub2->list.line[i].end;
2721 }
2722 else
2723 {
2724 sp2 = NULL;
2725 ep2 = NULL;
2726 }
2727 if (sp1 != sp2 || ep1 != ep2)
2728 return FALSE;
2729 }
2730 }
2731
2732 return TRUE;
2733}
2734
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002735 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002736addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002737 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002739 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002740 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002741{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002742 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002743 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002744 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002745 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002746 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002747 int i;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748
2749 if (l == NULL || state == NULL)
2750 return;
2751
2752 switch (state->c)
2753 {
2754 case NFA_SPLIT:
2755 case NFA_NOT:
2756 case NFA_NOPEN:
2757 case NFA_NCLOSE:
2758 case NFA_MCLOSE:
2759 case NFA_MCLOSE + 1:
2760 case NFA_MCLOSE + 2:
2761 case NFA_MCLOSE + 3:
2762 case NFA_MCLOSE + 4:
2763 case NFA_MCLOSE + 5:
2764 case NFA_MCLOSE + 6:
2765 case NFA_MCLOSE + 7:
2766 case NFA_MCLOSE + 8:
2767 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002768 /* These nodes are not added themselves but their "out" and/or
2769 * "out1" may be added below. */
2770 break;
2771
2772 case NFA_MOPEN:
2773 case NFA_MOPEN + 1:
2774 case NFA_MOPEN + 2:
2775 case NFA_MOPEN + 3:
2776 case NFA_MOPEN + 4:
2777 case NFA_MOPEN + 5:
2778 case NFA_MOPEN + 6:
2779 case NFA_MOPEN + 7:
2780 case NFA_MOPEN + 8:
2781 case NFA_MOPEN + 9:
2782 /* These nodes do not need to be added, but we need to bail out
2783 * when it was tried to be added to this list before. */
2784 if (state->lastlist == l->id)
2785 return;
2786 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002787 break;
2788
2789 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002790 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002791 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002792 /* This state is already in the list, don't add it again,
2793 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002794 if (!nfa_has_backref)
2795 return;
2796
2797 /* See if the same state is already in the list with the same
2798 * positions. */
2799 for (i = 0; i < l->n; ++i)
2800 {
2801 thread = &l->t[i];
2802 if (thread->state->id == state->id
2803 && sub_equal(&thread->sub, sub))
2804 return;
2805 }
2806 }
2807
2808 /* when there are backreferences the number of states may be (a
2809 * lot) bigger */
2810 if (nfa_has_backref && l->n == l->len)
2811 {
2812 int newlen = l->len * 3 / 2 + 50;
2813
2814 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2815 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002816 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002817
2818 /* add the state to the list */
2819 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002820 thread = &l->t[l->n++];
2821 thread->state = state;
2822 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002823 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002825 /* Copy the match start and end positions. */
2826 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002827 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002828 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002829 sizeof(struct multipos) * sub->in_use);
2830 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002831 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002832 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002833 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834 }
2835 }
2836
2837#ifdef ENABLE_LOG
2838 nfa_set_code(state->c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002839 fprintf(log_fd, "> Adding state %d to list. Character %d: %s\n",
2840 abs(state->id), state->c, code);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002841#endif
2842 switch (state->c)
2843 {
2844 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002845 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002846 break;
2847
2848 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002849 addstate(l, state->out, sub, off);
2850 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851 break;
2852
2853#if 0
2854 case NFA_END_NEG_RANGE:
2855 /* Nothing to handle here. nfa_regmatch() will take care of it */
2856 break;
2857
2858 case NFA_NOT:
2859 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2860#ifdef ENABLE_LOG
2861 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2862#endif
2863 break;
2864
2865 case NFA_COMPOSING:
2866 /* nfa_regmatch() will match all the bytes of this composing char. */
2867 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002868#endif
2869
Bram Moolenaar5714b802013-05-28 22:03:20 +02002870 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002871 case NFA_NOPEN:
2872 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002873 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874 break;
2875
2876 /* If this state is reached, then a recursive call of nfa_regmatch()
2877 * succeeded. the next call saves the found submatches in the
2878 * first state after the "invisible" branch. */
2879#if 0
2880 case NFA_END_INVISIBLE:
2881 break;
2882#endif
2883
2884 case NFA_MOPEN + 0:
2885 case NFA_MOPEN + 1:
2886 case NFA_MOPEN + 2:
2887 case NFA_MOPEN + 3:
2888 case NFA_MOPEN + 4:
2889 case NFA_MOPEN + 5:
2890 case NFA_MOPEN + 6:
2891 case NFA_MOPEN + 7:
2892 case NFA_MOPEN + 8:
2893 case NFA_MOPEN + 9:
2894 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002895 if (state->c == NFA_ZSTART)
2896 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002897 else
2898 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002899
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002900 /* Set the position (with "off") in the subexpression. Save and
2901 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02002902 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002903 if (REG_MULTI)
2904 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002905 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002906 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002907 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002908 save_in_use = -1;
2909 }
2910 else
2911 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002912 save_in_use = sub->in_use;
2913 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002914 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002915 sub->list.multi[i].start.lnum = -1;
2916 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002917 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002918 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002919 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002920 if (off == -1)
2921 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002922 sub->list.multi[subidx].start.lnum = reglnum + 1;
2923 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002924 }
2925 else
2926 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002927 sub->list.multi[subidx].start.lnum = reglnum;
2928 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002929 (colnr_T)(reginput - regline + off);
2930 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931 }
2932 else
2933 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002934 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002935 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002936 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002937 save_in_use = -1;
2938 }
2939 else
2940 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002941 save_in_use = sub->in_use;
2942 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002943 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002944 sub->list.line[i].start = NULL;
2945 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002946 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002947 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002948 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002949 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950 }
2951
Bram Moolenaar5714b802013-05-28 22:03:20 +02002952 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002954 if (save_in_use == -1)
2955 {
2956 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002957 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002958 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002959 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002960 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002961 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002962 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002963 break;
2964
2965 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002966 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002967 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002968 /* Do not overwrite the position set by \ze. If no \ze
2969 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002970 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002971 break;
2972 }
2973 case NFA_MCLOSE + 1:
2974 case NFA_MCLOSE + 2:
2975 case NFA_MCLOSE + 3:
2976 case NFA_MCLOSE + 4:
2977 case NFA_MCLOSE + 5:
2978 case NFA_MCLOSE + 6:
2979 case NFA_MCLOSE + 7:
2980 case NFA_MCLOSE + 8:
2981 case NFA_MCLOSE + 9:
2982 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002983 if (state->c == NFA_ZEND)
2984 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002985 else
2986 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002987
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002988 /* We don't fill in gaps here, there must have been an MOPEN that
2989 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002990 save_in_use = sub->in_use;
2991 if (sub->in_use <= subidx)
2992 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002993 if (REG_MULTI)
2994 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002995 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002996 if (off == -1)
2997 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002998 sub->list.multi[subidx].end.lnum = reglnum + 1;
2999 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003000 }
3001 else
3002 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003003 sub->list.multi[subidx].end.lnum = reglnum;
3004 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003005 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003006 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003007 }
3008 else
3009 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003010 save_ptr = sub->list.line[subidx].end;
3011 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003012 }
3013
Bram Moolenaar5714b802013-05-28 22:03:20 +02003014 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003015
3016 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003017 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003018 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003019 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003020 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003021 break;
3022 }
3023}
3024
3025/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003026 * Like addstate(), but the new state(s) are put at position "*ip".
3027 * Used for zero-width matches, next state to use is the added one.
3028 * This makes sure the order of states to be tried does not change, which
3029 * matters for alternatives.
3030 */
3031 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003032addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003033 nfa_list_T *l; /* runtime state list */
3034 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003035 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003036 int *ip;
3037{
3038 int tlen = l->n;
3039 int count;
3040 int i = *ip;
3041
3042 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003043 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003044
3045 /* when "*ip" was at the end of the list, nothing to do */
3046 if (i + 1 == tlen)
3047 return;
3048
3049 /* re-order to put the new state at the current position */
3050 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003051 if (count == 1)
3052 {
3053 /* overwrite the current state */
3054 l->t[i] = l->t[l->n - 1];
3055 }
3056 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003057 {
3058 /* make space for new states, then move them from the
3059 * end to the current position */
3060 mch_memmove(&(l->t[i + count]),
3061 &(l->t[i + 1]),
3062 sizeof(nfa_thread_T) * (l->n - i - 1));
3063 mch_memmove(&(l->t[i]),
3064 &(l->t[l->n - 1]),
3065 sizeof(nfa_thread_T) * count);
3066 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003067 --l->n;
3068 *ip = i - 1;
3069}
3070
3071/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003072 * Check character class "class" against current character c.
3073 */
3074 static int
3075check_char_class(class, c)
3076 int class;
3077 int c;
3078{
3079 switch (class)
3080 {
3081 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003082 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003083 return OK;
3084 break;
3085 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003086 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003087 return OK;
3088 break;
3089 case NFA_CLASS_BLANK:
3090 if (c == ' ' || c == '\t')
3091 return OK;
3092 break;
3093 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003094 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003095 return OK;
3096 break;
3097 case NFA_CLASS_DIGIT:
3098 if (VIM_ISDIGIT(c))
3099 return OK;
3100 break;
3101 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003102 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003103 return OK;
3104 break;
3105 case NFA_CLASS_LOWER:
3106 if (MB_ISLOWER(c))
3107 return OK;
3108 break;
3109 case NFA_CLASS_PRINT:
3110 if (vim_isprintc(c))
3111 return OK;
3112 break;
3113 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003114 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003115 return OK;
3116 break;
3117 case NFA_CLASS_SPACE:
3118 if ((c >=9 && c <= 13) || (c == ' '))
3119 return OK;
3120 break;
3121 case NFA_CLASS_UPPER:
3122 if (MB_ISUPPER(c))
3123 return OK;
3124 break;
3125 case NFA_CLASS_XDIGIT:
3126 if (vim_isxdigit(c))
3127 return OK;
3128 break;
3129 case NFA_CLASS_TAB:
3130 if (c == '\t')
3131 return OK;
3132 break;
3133 case NFA_CLASS_RETURN:
3134 if (c == '\r')
3135 return OK;
3136 break;
3137 case NFA_CLASS_BACKSPACE:
3138 if (c == '\b')
3139 return OK;
3140 break;
3141 case NFA_CLASS_ESCAPE:
3142 if (c == '\033')
3143 return OK;
3144 break;
3145
3146 default:
3147 /* should not be here :P */
3148 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3149 }
3150 return FAIL;
3151}
3152
Bram Moolenaar5714b802013-05-28 22:03:20 +02003153static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3154
3155/*
3156 * Check for a match with subexpression "subidx".
3157 * return TRUE if it matches.
3158 */
3159 static int
3160match_backref(sub, subidx, bytelen)
3161 regsub_T *sub; /* pointers to subexpressions */
3162 int subidx;
3163 int *bytelen; /* out: length of match in bytes */
3164{
3165 int len;
3166
3167 if (sub->in_use <= subidx)
3168 {
3169retempty:
3170 /* backref was not set, match an empty string */
3171 *bytelen = 0;
3172 return TRUE;
3173 }
3174
3175 if (REG_MULTI)
3176 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003177 if (sub->list.multi[subidx].start.lnum < 0
3178 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003179 goto retempty;
3180 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003181 len = sub->list.multi[subidx].end.col
3182 - sub->list.multi[subidx].start.col;
3183 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003184 reginput, &len) == 0)
3185 {
3186 *bytelen = len;
3187 return TRUE;
3188 }
3189 }
3190 else
3191 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003192 if (sub->list.line[subidx].start == NULL
3193 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003194 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003195 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3196 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003197 {
3198 *bytelen = len;
3199 return TRUE;
3200 }
3201 }
3202 return FALSE;
3203}
3204
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205/*
3206 * Set all NFA nodes' list ID equal to -1.
3207 */
3208 static void
3209nfa_set_neg_listids(start)
3210 nfa_state_T *start;
3211{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003212 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213 {
3214 start->lastlist = -1;
3215 nfa_set_neg_listids(start->out);
3216 nfa_set_neg_listids(start->out1);
3217 }
3218}
3219
3220/*
3221 * Set all NFA nodes' list ID equal to 0.
3222 */
3223 static void
3224nfa_set_null_listids(start)
3225 nfa_state_T *start;
3226{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003227 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003228 {
3229 start->lastlist = 0;
3230 nfa_set_null_listids(start->out);
3231 nfa_set_null_listids(start->out1);
3232 }
3233}
3234
3235/*
3236 * Save list IDs for all NFA states in "list".
3237 */
3238 static void
3239nfa_save_listids(start, list)
3240 nfa_state_T *start;
3241 int *list;
3242{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003243 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 {
3245 list[abs(start->id)] = start->lastlist;
3246 start->lastlist = -1;
3247 nfa_save_listids(start->out, list);
3248 nfa_save_listids(start->out1, list);
3249 }
3250}
3251
3252/*
3253 * Restore list IDs from "list" to all NFA states.
3254 */
3255 static void
3256nfa_restore_listids(start, list)
3257 nfa_state_T *start;
3258 int *list;
3259{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003260 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 {
3262 start->lastlist = list[abs(start->id)];
3263 nfa_restore_listids(start->out, list);
3264 nfa_restore_listids(start->out1, list);
3265 }
3266}
3267
Bram Moolenaar423532e2013-05-29 21:14:42 +02003268 static int
3269nfa_re_num_cmp(val, op, pos)
3270 long_u val;
3271 int op;
3272 long_u pos;
3273{
3274 if (op == 1) return pos > val;
3275 if (op == 2) return pos < val;
3276 return val == pos;
3277}
3278
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003279static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3280
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281/*
3282 * Main matching routine.
3283 *
3284 * Run NFA to determine whether it matches reginput.
3285 *
3286 * Return TRUE if there is a match, FALSE otherwise.
3287 * Note: Caller must ensure that: start != NULL.
3288 */
3289 static int
3290nfa_regmatch(start, submatch, m)
3291 nfa_state_T *start;
3292 regsub_T *submatch;
3293 regsub_T *m;
3294{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295 int result;
3296 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 int flag = 0;
3298 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003299 int go_to_nextline = FALSE;
3300 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 char_u *old_reginput = NULL;
3302 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003303 nfa_list_T list[3];
3304 nfa_list_T *listtbl[2][2];
3305 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003307 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003308 nfa_list_T *thislist;
3309 nfa_list_T *nextlist;
3310 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003312#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003313 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314
3315 if (debug == NULL)
3316 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003317 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 return FALSE;
3319 }
3320#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003321 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003323 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003324 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003325 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3326 list[0].len = nstate + 1;
3327 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3328 list[1].len = nstate + 1;
3329 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3330 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003331 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3332 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333
3334#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003335 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003336 if (log_fd != NULL)
3337 {
3338 fprintf(log_fd, "**********************************\n");
3339 nfa_set_code(start->c);
3340 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3341 abs(start->id), code);
3342 fprintf(log_fd, "**********************************\n");
3343 }
3344 else
3345 {
3346 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3347 log_fd = stderr;
3348 }
3349#endif
3350
3351 thislist = &list[0];
3352 thislist->n = 0;
3353 nextlist = &list[1];
3354 nextlist->n = 0;
3355 neglist = &list[2];
3356 neglist->n = 0;
3357#ifdef ENABLE_LOG
3358 fprintf(log_fd, "(---) STARTSTATE\n");
3359#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003360 thislist->id = listid;
3361 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362
3363 /* There are two cases when the NFA advances: 1. input char matches the
3364 * NFA node and 2. input char does not match the NFA node, but the next
3365 * node is NFA_NOT. The following macro calls addstate() according to
3366 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3367 listtbl[0][0] = NULL;
3368 listtbl[0][1] = neglist;
3369 listtbl[1][0] = nextlist;
3370 listtbl[1][1] = NULL;
3371#define ADD_POS_NEG_STATE(node) \
3372 ll = listtbl[result ? 1 : 0][node->negated]; \
3373 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003374 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003375
3376
3377 /*
3378 * Run for each character.
3379 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003380 for (;;)
3381 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003382 int curc;
3383 int clen;
3384
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385#ifdef FEAT_MBYTE
3386 if (has_mbyte)
3387 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003388 curc = (*mb_ptr2char)(reginput);
3389 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003390 }
3391 else
3392#endif
3393 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003394 curc = *reginput;
3395 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003396 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003397 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003398 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003399 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003400 go_to_nextline = FALSE;
3401 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402
3403 /* swap lists */
3404 thislist = &list[flag];
3405 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003406 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003407 listtbl[1][0] = nextlist;
3408 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003409 thislist->id = listid;
3410 nextlist->id = listid + 1;
3411 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412
3413#ifdef ENABLE_LOG
3414 fprintf(log_fd, "------------------------------------------\n");
3415 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003416 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003418 {
3419 int i;
3420
3421 for (i = 0; i < thislist->n; i++)
3422 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3423 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 fprintf(log_fd, "\n");
3425#endif
3426
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003427#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428 fprintf(debug, "\n-------------------\n");
3429#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003430 /*
3431 * If the state lists are empty we can stop.
3432 */
3433 if (thislist->n == 0 && neglist->n == 0)
3434 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003435
3436 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003437 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003438 {
3439 if (neglist->n > 0)
3440 {
3441 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003442 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003443 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 }
3445 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003446 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003447
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003448#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449 nfa_set_code(t->state->c);
3450 fprintf(debug, "%s, ", code);
3451#endif
3452#ifdef ENABLE_LOG
3453 nfa_set_code(t->state->c);
3454 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3455 code, (int)t->state->c);
3456#endif
3457
3458 /*
3459 * Handle the possible codes of the current state.
3460 * The most important is NFA_MATCH.
3461 */
3462 switch (t->state->c)
3463 {
3464 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003465 {
3466 int j;
3467
Bram Moolenaar963fee22013-05-26 21:47:28 +02003468 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003469 submatch->in_use = t->sub.in_use;
3470 if (REG_MULTI)
3471 for (j = 0; j < submatch->in_use; j++)
3472 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003473 submatch->list.multi[j].start =
3474 t->sub.list.multi[j].start;
3475 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003476 }
3477 else
3478 for (j = 0; j < submatch->in_use; j++)
3479 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003480 submatch->list.line[j].start =
3481 t->sub.list.line[j].start;
3482 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003483 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003484#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003485 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003486#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003487 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003488 * states at this position. When the list of states is going
3489 * to be empty quit without advancing, so that "reginput" is
3490 * correct. */
3491 if (nextlist->n == 0 && neglist->n == 0)
3492 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003493 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003494 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003495
3496 case NFA_END_INVISIBLE:
3497 /* This is only encountered after a NFA_START_INVISIBLE node.
3498 * They surround a zero-width group, used with "\@=" and "\&".
3499 * If we got here, it means that the current "invisible" group
3500 * finished successfully, so return control to the parent
3501 * nfa_regmatch(). Submatches are stored in *m, and used in
3502 * the parent call. */
3503 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003504 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003505 else
3506 {
3507 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003508 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509 }
3510 break;
3511
3512 case NFA_START_INVISIBLE:
3513 /* Save global variables, and call nfa_regmatch() to check if
3514 * the current concat matches at this position. The concat
3515 * ends with the node NFA_END_INVISIBLE */
3516 old_reginput = reginput;
3517 old_regline = regline;
3518 old_reglnum = reglnum;
3519 if (listids == NULL)
3520 {
3521 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3522 if (listids == NULL)
3523 {
3524 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3525 return 0;
3526 }
3527 }
3528#ifdef ENABLE_LOG
3529 if (log_fd != stderr)
3530 fclose(log_fd);
3531 log_fd = NULL;
3532#endif
3533 /* Have to clear the listid field of the NFA nodes, so that
3534 * nfa_regmatch() and addstate() can run properly after
3535 * recursion. */
3536 nfa_save_listids(start, listids);
3537 nfa_set_null_listids(start);
3538 result = nfa_regmatch(t->state->out, submatch, m);
3539 nfa_set_neg_listids(start);
3540 nfa_restore_listids(start, listids);
3541
3542#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003543 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003544 if (log_fd != NULL)
3545 {
3546 fprintf(log_fd, "****************************\n");
3547 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3548 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3549 fprintf(log_fd, "****************************\n");
3550 }
3551 else
3552 {
3553 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3554 log_fd = stderr;
3555 }
3556#endif
3557 if (result == TRUE)
3558 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003559 int j;
3560
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003561 /* Restore position in input text */
3562 reginput = old_reginput;
3563 regline = old_regline;
3564 reglnum = old_reglnum;
3565 /* Copy submatch info from the recursive call */
3566 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003567 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003568 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003569 t->sub.list.multi[j].start = m->list.multi[j].start;
3570 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 }
3572 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003573 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003574 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003575 t->sub.list.line[j].start = m->list.line[j].start;
3576 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003577 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003578 t->sub.in_use = m->in_use;
3579
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003581 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003582 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003583 }
3584 else
3585 {
3586 /* continue with next input char */
3587 reginput = old_reginput;
3588 }
3589 break;
3590
3591 case NFA_BOL:
3592 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003593 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 break;
3595
3596 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003597 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003598 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 break;
3600
3601 case NFA_BOW:
3602 {
3603 int bow = TRUE;
3604
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003605 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003606 bow = FALSE;
3607#ifdef FEAT_MBYTE
3608 else if (has_mbyte)
3609 {
3610 int this_class;
3611
3612 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003613 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003614 if (this_class <= 1)
3615 bow = FALSE;
3616 else if (reg_prev_class() == this_class)
3617 bow = FALSE;
3618 }
3619#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003620 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003621 || (reginput > regline
3622 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 bow = FALSE;
3624 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003625 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003626 break;
3627 }
3628
3629 case NFA_EOW:
3630 {
3631 int eow = TRUE;
3632
3633 if (reginput == regline)
3634 eow = FALSE;
3635#ifdef FEAT_MBYTE
3636 else if (has_mbyte)
3637 {
3638 int this_class, prev_class;
3639
3640 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003641 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642 prev_class = reg_prev_class();
3643 if (this_class == prev_class
3644 || prev_class == 0 || prev_class == 1)
3645 eow = FALSE;
3646 }
3647#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003648 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003649 || (reginput[0] != NUL
3650 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651 eow = FALSE;
3652 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003653 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003654 break;
3655 }
3656
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003657#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003658 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003659 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003660 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003661 int len = 0;
3662 nfa_state_T *end;
3663 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003664 int cchars[MAX_MCO];
3665 int ccount = 0;
3666 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003667
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003668 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003669 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003670 if (utf_iscomposing(sta->c))
3671 {
3672 /* Only match composing character(s), ignore base
3673 * character. Used for ".{composing}" and "{composing}"
3674 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003675 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003676 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003677 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003679 /* If \Z was present, then ignore composing characters.
3680 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003681 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003682 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003683 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003684 else
3685 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003686 while (sta->c != NFA_END_COMPOSING)
3687 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003689
3690 /* Check base character matches first, unless ignored. */
3691 else if (len > 0 || mc == sta->c)
3692 {
3693 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003694 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003695 len += mb_char2len(mc);
3696 sta = sta->out;
3697 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003698
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003699 /* We don't care about the order of composing characters.
3700 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003701 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003702 {
3703 mc = mb_ptr2char(reginput + len);
3704 cchars[ccount++] = mc;
3705 len += mb_char2len(mc);
3706 if (ccount == MAX_MCO)
3707 break;
3708 }
3709
3710 /* Check that each composing char in the pattern matches a
3711 * composing char in the text. We do not check if all
3712 * composing chars are matched. */
3713 result = OK;
3714 while (sta->c != NFA_END_COMPOSING)
3715 {
3716 for (j = 0; j < ccount; ++j)
3717 if (cchars[j] == sta->c)
3718 break;
3719 if (j == ccount)
3720 {
3721 result = FAIL;
3722 break;
3723 }
3724 sta = sta->out;
3725 }
3726 }
3727 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003728 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003729
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003730 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003731 ADD_POS_NEG_STATE(end);
3732 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003733 }
3734#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003735
3736 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003737 if (curc == NUL && !reg_line_lbr && REG_MULTI
3738 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003740 go_to_nextline = TRUE;
3741 /* Pass -1 for the offset, which means taking the position
3742 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003743 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003744 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003745 else if (curc == '\n' && reg_line_lbr)
3746 {
3747 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003748 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003749 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750 break;
3751
3752 case NFA_CLASS_ALNUM:
3753 case NFA_CLASS_ALPHA:
3754 case NFA_CLASS_BLANK:
3755 case NFA_CLASS_CNTRL:
3756 case NFA_CLASS_DIGIT:
3757 case NFA_CLASS_GRAPH:
3758 case NFA_CLASS_LOWER:
3759 case NFA_CLASS_PRINT:
3760 case NFA_CLASS_PUNCT:
3761 case NFA_CLASS_SPACE:
3762 case NFA_CLASS_UPPER:
3763 case NFA_CLASS_XDIGIT:
3764 case NFA_CLASS_TAB:
3765 case NFA_CLASS_RETURN:
3766 case NFA_CLASS_BACKSPACE:
3767 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003768 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003769 ADD_POS_NEG_STATE(t->state);
3770 break;
3771
3772 case NFA_END_NEG_RANGE:
3773 /* This follows a series of negated nodes, like:
3774 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003775 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003776 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003777 break;
3778
3779 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003780 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003781 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003782 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003783 break;
3784
3785 /*
3786 * Character classes like \a for alpha, \d for digit etc.
3787 */
3788 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003789 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003790 ADD_POS_NEG_STATE(t->state);
3791 break;
3792
3793 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003794 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795 ADD_POS_NEG_STATE(t->state);
3796 break;
3797
3798 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003799 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 ADD_POS_NEG_STATE(t->state);
3801 break;
3802
3803 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003804 result = !VIM_ISDIGIT(curc)
3805 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003806 ADD_POS_NEG_STATE(t->state);
3807 break;
3808
3809 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003810 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003811 ADD_POS_NEG_STATE(t->state);
3812 break;
3813
3814 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003815 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003816 ADD_POS_NEG_STATE(t->state);
3817 break;
3818
3819 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003820 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003821 ADD_POS_NEG_STATE(t->state);
3822 break;
3823
3824 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003825 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003826 ADD_POS_NEG_STATE(t->state);
3827 break;
3828
3829 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003830 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831 ADD_POS_NEG_STATE(t->state);
3832 break;
3833
3834 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003835 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003836 ADD_POS_NEG_STATE(t->state);
3837 break;
3838
3839 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003840 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003841 ADD_POS_NEG_STATE(t->state);
3842 break;
3843
3844 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003845 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846 ADD_POS_NEG_STATE(t->state);
3847 break;
3848
3849 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003850 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003851 ADD_POS_NEG_STATE(t->state);
3852 break;
3853
3854 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003855 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003856 ADD_POS_NEG_STATE(t->state);
3857 break;
3858
3859 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003860 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003861 ADD_POS_NEG_STATE(t->state);
3862 break;
3863
3864 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003865 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003866 ADD_POS_NEG_STATE(t->state);
3867 break;
3868
3869 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003870 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003871 ADD_POS_NEG_STATE(t->state);
3872 break;
3873
3874 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003875 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003876 ADD_POS_NEG_STATE(t->state);
3877 break;
3878
3879 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003880 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881 ADD_POS_NEG_STATE(t->state);
3882 break;
3883
3884 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003885 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003886 ADD_POS_NEG_STATE(t->state);
3887 break;
3888
3889 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003890 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003891 ADD_POS_NEG_STATE(t->state);
3892 break;
3893
3894 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003895 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003896 ADD_POS_NEG_STATE(t->state);
3897 break;
3898
3899 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003900 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003901 ADD_POS_NEG_STATE(t->state);
3902 break;
3903
3904 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003905 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003906 ADD_POS_NEG_STATE(t->state);
3907 break;
3908
3909 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003910 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003911 ADD_POS_NEG_STATE(t->state);
3912 break;
3913
3914 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003915 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003916 ADD_POS_NEG_STATE(t->state);
3917 break;
3918
Bram Moolenaar5714b802013-05-28 22:03:20 +02003919 case NFA_BACKREF1:
3920 case NFA_BACKREF2:
3921 case NFA_BACKREF3:
3922 case NFA_BACKREF4:
3923 case NFA_BACKREF5:
3924 case NFA_BACKREF6:
3925 case NFA_BACKREF7:
3926 case NFA_BACKREF8:
3927 case NFA_BACKREF9:
3928 /* \1 .. \9 */
3929 {
3930 int subidx = t->state->c - NFA_BACKREF1 + 1;
3931 int bytelen;
3932
3933 result = match_backref(&t->sub, subidx, &bytelen);
3934 if (result)
3935 {
3936 if (bytelen == 0)
3937 {
3938 /* empty match always works, add NFA_SKIP with zero to
3939 * be used next */
3940 addstate_here(thislist, t->state->out, &t->sub,
3941 &listidx);
3942 thislist->t[listidx + 1].count = 0;
3943 }
3944 else if (bytelen <= clen)
3945 {
3946 /* match current character, jump ahead to out of
3947 * NFA_SKIP */
3948 addstate(nextlist, t->state->out->out, &t->sub, clen);
3949#ifdef ENABLE_LOG
3950 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3951#endif
3952 }
3953 else
3954 {
3955 /* skip ofer the matched characters, set character
3956 * count in NFA_SKIP */
3957 addstate(nextlist, t->state->out, &t->sub, bytelen);
3958 nextlist->t[nextlist->n - 1].count = bytelen - clen;
3959#ifdef ENABLE_LOG
3960 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3961#endif
3962 }
3963
3964 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02003965 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003966 }
3967 case NFA_SKIP:
3968 /* charater of previous matching \1 .. \9 */
3969 if (t->count - clen <= 0)
3970 {
3971 /* end of match, go to what follows */
3972 addstate(nextlist, t->state->out, &t->sub, clen);
3973#ifdef ENABLE_LOG
3974 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3975#endif
3976 }
3977 else
3978 {
3979 /* add state again with decremented count */
3980 addstate(nextlist, t->state, &t->sub, 0);
3981 nextlist->t[nextlist->n - 1].count = t->count - clen;
3982#ifdef ENABLE_LOG
3983 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3984#endif
3985 }
3986 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003987
3988 case NFA_SKIP_CHAR:
3989 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003990 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02003991 /* TODO: should not happen? */
3992 break;
3993
Bram Moolenaar423532e2013-05-29 21:14:42 +02003994 case NFA_LNUM:
3995 case NFA_LNUM_GT:
3996 case NFA_LNUM_LT:
3997 result = (REG_MULTI &&
3998 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
3999 (long_u)(reglnum + reg_firstlnum)));
4000 if (result)
4001 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4002 break;
4003
4004 case NFA_COL:
4005 case NFA_COL_GT:
4006 case NFA_COL_LT:
4007 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4008 (long_u)(reginput - regline) + 1);
4009 if (result)
4010 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4011 break;
4012
4013 case NFA_VCOL:
4014 case NFA_VCOL_GT:
4015 case NFA_VCOL_LT:
4016 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4017 (long_u)win_linetabsize(
4018 reg_win == NULL ? curwin : reg_win,
4019 regline, (colnr_T)(reginput - regline)) + 1);
4020 if (result)
4021 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4022 break;
4023
4024 case NFA_CURSOR:
4025 result = (reg_win != NULL
4026 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4027 && ((colnr_T)(reginput - regline)
4028 == reg_win->w_cursor.col));
4029 if (result)
4030 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4031 break;
4032
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004033 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004034 {
4035 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004036
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004037 /* TODO: put this in #ifdef later */
4038 if (c < -256)
4039 EMSGN("INTERNAL: Negative state char: %ld", c);
4040 if (is_Magic(c))
4041 c = un_Magic(c);
4042 result = (c == curc);
4043
4044 if (!result && ireg_ic)
4045 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004046#ifdef FEAT_MBYTE
4047 /* If there is a composing character which is not being
4048 * ignored there can be no match. Match with composing
4049 * character uses NFA_COMPOSING above. */
4050 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004051 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004052 result = FALSE;
4053#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004054 ADD_POS_NEG_STATE(t->state);
4055 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004056 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004057 }
4058
4059 } /* for (thislist = thislist; thislist->state; thislist++) */
4060
Bram Moolenaare23febd2013-05-26 18:40:14 +02004061 /* Look for the start of a match in the current position by adding the
4062 * start state to the list of states.
4063 * The first found match is the leftmost one, thus the order of states
4064 * matters!
4065 * Do not add the start state in recursive calls of nfa_regmatch(),
4066 * because recursive calls should only start in the first position.
4067 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004068 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004069 && reglnum == 0 && clen != 0
4070 && (ireg_maxcol == 0
4071 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004072 {
4073#ifdef ENABLE_LOG
4074 fprintf(log_fd, "(---) STARTSTATE\n");
4075#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004076 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004077 }
4078
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004079#ifdef ENABLE_LOG
4080 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004081 {
4082 int i;
4083
4084 for (i = 0; i < thislist->n; i++)
4085 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4086 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004087 fprintf(log_fd, "\n");
4088#endif
4089
4090nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004091 /* Advance to the next character, or advance to the next line, or
4092 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004093 if (clen != 0)
4094 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004095 else if (go_to_nextline)
4096 reg_nextline();
4097 else
4098 break;
4099 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004100
4101#ifdef ENABLE_LOG
4102 if (log_fd != stderr)
4103 fclose(log_fd);
4104 log_fd = NULL;
4105#endif
4106
4107theend:
4108 /* Free memory */
4109 vim_free(list[0].t);
4110 vim_free(list[1].t);
4111 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004112 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004114#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004115 fclose(debug);
4116#endif
4117
Bram Moolenaar963fee22013-05-26 21:47:28 +02004118 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004119}
4120
4121/*
4122 * Try match of "prog" with at regline["col"].
4123 * Returns 0 for failure, number of lines contained in the match otherwise.
4124 */
4125 static long
4126nfa_regtry(start, col)
4127 nfa_state_T *start;
4128 colnr_T col;
4129{
4130 int i;
4131 regsub_T sub, m;
4132#ifdef ENABLE_LOG
4133 FILE *f;
4134#endif
4135
4136 reginput = regline + col;
4137 need_clear_subexpr = TRUE;
4138
4139#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004140 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141 if (f != NULL)
4142 {
4143 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4144 fprintf(f, " =======================================================\n");
4145#ifdef DEBUG
4146 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4147#endif
4148 fprintf(f, "\tInput text is \"%s\" \n", reginput);
4149 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004150 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004151 fprintf(f, "\n\n");
4152 fclose(f);
4153 }
4154 else
4155 EMSG(_("Could not open temporary log file for writing "));
4156#endif
4157
4158 if (REG_MULTI)
4159 {
4160 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004161 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4162 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004163 }
4164 else
4165 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004166 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4167 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004168 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004169 sub.in_use = 0;
4170 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004171
4172 if (nfa_regmatch(start, &sub, &m) == FALSE)
4173 return 0;
4174
4175 cleanup_subexpr();
4176 if (REG_MULTI)
4177 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004178 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004179 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004180 reg_startpos[i] = sub.list.multi[i].start;
4181 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182 }
4183
4184 if (reg_startpos[0].lnum < 0)
4185 {
4186 reg_startpos[0].lnum = 0;
4187 reg_startpos[0].col = col;
4188 }
4189 if (reg_endpos[0].lnum < 0)
4190 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004191 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004192 reg_endpos[0].lnum = reglnum;
4193 reg_endpos[0].col = (int)(reginput - regline);
4194 }
4195 else
4196 /* Use line number of "\ze". */
4197 reglnum = reg_endpos[0].lnum;
4198 }
4199 else
4200 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004201 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004202 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004203 reg_startp[i] = sub.list.line[i].start;
4204 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004205 }
4206
4207 if (reg_startp[0] == NULL)
4208 reg_startp[0] = regline + col;
4209 if (reg_endp[0] == NULL)
4210 reg_endp[0] = reginput;
4211 }
4212
4213 return 1 + reglnum;
4214}
4215
4216/*
4217 * Match a regexp against a string ("line" points to the string) or multiple
4218 * lines ("line" is NULL, use reg_getline()).
4219 *
4220 * Returns 0 for failure, number of lines contained in the match otherwise.
4221 */
4222 static long
4223nfa_regexec_both(line, col)
4224 char_u *line;
4225 colnr_T col; /* column to start looking for match */
4226{
4227 nfa_regprog_T *prog;
4228 long retval = 0L;
4229 int i;
4230
4231 if (REG_MULTI)
4232 {
4233 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4234 line = reg_getline((linenr_T)0); /* relative to the cursor */
4235 reg_startpos = reg_mmatch->startpos;
4236 reg_endpos = reg_mmatch->endpos;
4237 }
4238 else
4239 {
4240 prog = (nfa_regprog_T *)reg_match->regprog;
4241 reg_startp = reg_match->startp;
4242 reg_endp = reg_match->endp;
4243 }
4244
4245 /* Be paranoid... */
4246 if (prog == NULL || line == NULL)
4247 {
4248 EMSG(_(e_null));
4249 goto theend;
4250 }
4251
4252 /* If the start column is past the maximum column: no need to try. */
4253 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4254 goto theend;
4255
4256 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4257 if (prog->regflags & RF_ICASE)
4258 ireg_ic = TRUE;
4259 else if (prog->regflags & RF_NOICASE)
4260 ireg_ic = FALSE;
4261
4262#ifdef FEAT_MBYTE
4263 /* If pattern contains "\Z" overrule value of ireg_icombine */
4264 if (prog->regflags & RF_ICOMBINE)
4265 ireg_icombine = TRUE;
4266#endif
4267
4268 regline = line;
4269 reglnum = 0; /* relative to line */
4270
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004271 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004272 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004273 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004274
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004275 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004276 for (i = 0; i < nstate; ++i)
4277 {
4278 prog->state[i].id = i;
4279 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004280 }
4281
4282 retval = nfa_regtry(prog->start, col);
4283
4284theend:
4285 return retval;
4286}
4287
4288/*
4289 * Compile a regular expression into internal code for the NFA matcher.
4290 * Returns the program in allocated space. Returns NULL for an error.
4291 */
4292 static regprog_T *
4293nfa_regcomp(expr, re_flags)
4294 char_u *expr;
4295 int re_flags;
4296{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004297 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004298 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004299 int *postfix;
4300
4301 if (expr == NULL)
4302 return NULL;
4303
4304#ifdef DEBUG
4305 nfa_regengine.expr = expr;
4306#endif
4307
4308 init_class_tab();
4309
4310 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4311 return NULL;
4312
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004313 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004314 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004315 postfix = re2post();
4316 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004317 {
4318 /* TODO: only give this error for debugging? */
4319 if (post_ptr >= post_end)
4320 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004322 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323
4324 /*
4325 * In order to build the NFA, we parse the input regexp twice:
4326 * 1. first pass to count size (so we can allocate space)
4327 * 2. second to emit code
4328 */
4329#ifdef ENABLE_LOG
4330 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004331 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004332
4333 if (f != NULL)
4334 {
4335 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4336 fclose(f);
4337 }
4338 }
4339#endif
4340
4341 /*
4342 * PASS 1
4343 * Count number of NFA states in "nstate". Do not build the NFA.
4344 */
4345 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004346
4347 /* Space for compiled regexp */
4348 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4349 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4350 if (prog == NULL)
4351 goto fail;
4352 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004353 state_ptr = prog->state;
4354
4355 /*
4356 * PASS 2
4357 * Build the NFA
4358 */
4359 prog->start = post2nfa(postfix, post_ptr, FALSE);
4360 if (prog->start == NULL)
4361 goto fail;
4362
4363 prog->regflags = regflags;
4364 prog->engine = &nfa_regengine;
4365 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004366 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004367 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004368 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004369#ifdef ENABLE_LOG
4370 nfa_postfix_dump(expr, OK);
4371 nfa_dump(prog);
4372#endif
4373
4374out:
4375 vim_free(post_start);
4376 post_start = post_ptr = post_end = NULL;
4377 state_ptr = NULL;
4378 return (regprog_T *)prog;
4379
4380fail:
4381 vim_free(prog);
4382 prog = NULL;
4383#ifdef ENABLE_LOG
4384 nfa_postfix_dump(expr, FAIL);
4385#endif
4386#ifdef DEBUG
4387 nfa_regengine.expr = NULL;
4388#endif
4389 goto out;
4390}
4391
4392
4393/*
4394 * Match a regexp against a string.
4395 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4396 * Uses curbuf for line count and 'iskeyword'.
4397 *
4398 * Return TRUE if there is a match, FALSE if not.
4399 */
4400 static int
4401nfa_regexec(rmp, line, col)
4402 regmatch_T *rmp;
4403 char_u *line; /* string to match against */
4404 colnr_T col; /* column to start looking for match */
4405{
4406 reg_match = rmp;
4407 reg_mmatch = NULL;
4408 reg_maxline = 0;
4409 reg_line_lbr = FALSE;
4410 reg_buf = curbuf;
4411 reg_win = NULL;
4412 ireg_ic = rmp->rm_ic;
4413#ifdef FEAT_MBYTE
4414 ireg_icombine = FALSE;
4415#endif
4416 ireg_maxcol = 0;
4417 return (nfa_regexec_both(line, col) != 0);
4418}
4419
4420#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4421 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4422
4423static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4424
4425/*
4426 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4427 */
4428 static int
4429nfa_regexec_nl(rmp, line, col)
4430 regmatch_T *rmp;
4431 char_u *line; /* string to match against */
4432 colnr_T col; /* column to start looking for match */
4433{
4434 reg_match = rmp;
4435 reg_mmatch = NULL;
4436 reg_maxline = 0;
4437 reg_line_lbr = TRUE;
4438 reg_buf = curbuf;
4439 reg_win = NULL;
4440 ireg_ic = rmp->rm_ic;
4441#ifdef FEAT_MBYTE
4442 ireg_icombine = FALSE;
4443#endif
4444 ireg_maxcol = 0;
4445 return (nfa_regexec_both(line, col) != 0);
4446}
4447#endif
4448
4449
4450/*
4451 * Match a regexp against multiple lines.
4452 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4453 * Uses curbuf for line count and 'iskeyword'.
4454 *
4455 * Return zero if there is no match. Return number of lines contained in the
4456 * match otherwise.
4457 *
4458 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4459 *
4460 * ! Also NOTE : match may actually be in another line. e.g.:
4461 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4462 *
4463 * +-------------------------+
4464 * |a |
4465 * |b |
4466 * |c |
4467 * | |
4468 * +-------------------------+
4469 *
4470 * then nfa_regexec_multi() returns 3. while the original
4471 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4472 *
4473 * FIXME if this behavior is not compatible.
4474 */
4475 static long
4476nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4477 regmmatch_T *rmp;
4478 win_T *win; /* window in which to search or NULL */
4479 buf_T *buf; /* buffer in which to search */
4480 linenr_T lnum; /* nr of line to start looking for match */
4481 colnr_T col; /* column to start looking for match */
4482 proftime_T *tm UNUSED; /* timeout limit or NULL */
4483{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004484 reg_match = NULL;
4485 reg_mmatch = rmp;
4486 reg_buf = buf;
4487 reg_win = win;
4488 reg_firstlnum = lnum;
4489 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4490 reg_line_lbr = FALSE;
4491 ireg_ic = rmp->rmm_ic;
4492#ifdef FEAT_MBYTE
4493 ireg_icombine = FALSE;
4494#endif
4495 ireg_maxcol = rmp->rmm_maxcol;
4496
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004497 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004498}
4499
4500#ifdef DEBUG
4501# undef ENABLE_LOG
4502#endif