blob: 9bf608478fed42f48ceea73844520972971a35b8 [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 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200893 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200894 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 Moolenaar2d5e1122013-05-30 21:42:13 +02001743 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1744 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745 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:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002376 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002377 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002378 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379
2380 if (nfa_calc_size == TRUE)
2381 {
2382 nstate += 2;
2383 break;
2384 }
2385 e = POP();
2386 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2387 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002388 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389 patch(e.out, s1);
2390
2391 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2392 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002393 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002394 PUSH(frag(s, list1(&s1->out)));
2395 break;
2396
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002397#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002398 case NFA_COMPOSING: /* char with composing char */
2399#if 0
2400 /* TODO */
2401 if (regflags & RF_ICOMBINE)
2402 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002403 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002404 }
2405#endif
2406 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002407#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002408
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002409 case NFA_MOPEN + 0: /* Submatch */
2410 case NFA_MOPEN + 1:
2411 case NFA_MOPEN + 2:
2412 case NFA_MOPEN + 3:
2413 case NFA_MOPEN + 4:
2414 case NFA_MOPEN + 5:
2415 case NFA_MOPEN + 6:
2416 case NFA_MOPEN + 7:
2417 case NFA_MOPEN + 8:
2418 case NFA_MOPEN + 9:
2419 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002420 if (nfa_calc_size == TRUE)
2421 {
2422 nstate += 2;
2423 break;
2424 }
2425
2426 mopen = *p;
2427 switch (*p)
2428 {
2429 case NFA_NOPEN:
2430 mclose = NFA_NCLOSE;
2431 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002432#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433 case NFA_COMPOSING:
2434 mclose = NFA_END_COMPOSING;
2435 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002436#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002437 default:
2438 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2439 mclose = *p + NSUBEXP;
2440 break;
2441 }
2442
2443 /* Allow "NFA_MOPEN" as a valid postfix representation for
2444 * the empty regexp "". In this case, the NFA will be
2445 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2446 * empty groups of parenthesis, and empty mbyte chars */
2447 if (stackp == stack)
2448 {
2449 s = new_state(mopen, NULL, NULL);
2450 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002451 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 s1 = new_state(mclose, NULL, NULL);
2453 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002454 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002455 patch(list1(&s->out), s1);
2456 PUSH(frag(s, list1(&s1->out)));
2457 break;
2458 }
2459
2460 /* At least one node was emitted before NFA_MOPEN, so
2461 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2462 e = POP();
2463 s = new_state(mopen, e.start, NULL); /* `(' */
2464 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002465 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466
2467 s1 = new_state(mclose, NULL, NULL); /* `)' */
2468 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002469 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002470 patch(e.out, s1);
2471
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002472#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002473 if (mopen == NFA_COMPOSING)
2474 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002475 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002476#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002477
2478 PUSH(frag(s, list1(&s1->out)));
2479 break;
2480
Bram Moolenaar5714b802013-05-28 22:03:20 +02002481 case NFA_BACKREF1:
2482 case NFA_BACKREF2:
2483 case NFA_BACKREF3:
2484 case NFA_BACKREF4:
2485 case NFA_BACKREF5:
2486 case NFA_BACKREF6:
2487 case NFA_BACKREF7:
2488 case NFA_BACKREF8:
2489 case NFA_BACKREF9:
2490 if (nfa_calc_size == TRUE)
2491 {
2492 nstate += 2;
2493 break;
2494 }
2495 s = new_state(*p, NULL, NULL);
2496 if (s == NULL)
2497 goto theend;
2498 s1 = new_state(NFA_SKIP, NULL, NULL);
2499 if (s1 == NULL)
2500 goto theend;
2501 patch(list1(&s->out), s1);
2502 PUSH(frag(s, list1(&s1->out)));
2503 break;
2504
Bram Moolenaar423532e2013-05-29 21:14:42 +02002505 case NFA_LNUM:
2506 case NFA_LNUM_GT:
2507 case NFA_LNUM_LT:
2508 case NFA_VCOL:
2509 case NFA_VCOL_GT:
2510 case NFA_VCOL_LT:
2511 case NFA_COL:
2512 case NFA_COL_GT:
2513 case NFA_COL_LT:
2514 if (nfa_calc_size == TRUE)
2515 {
2516 nstate += 1;
2517 break;
2518 }
2519 e1 = POP();
2520 s = new_state(*p, NULL, NULL);
2521 if (s == NULL)
2522 goto theend;
2523 s->val = e1.start->c;
2524 PUSH(frag(s, list1(&s->out)));
2525 break;
2526
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002527 case NFA_ZSTART:
2528 case NFA_ZEND:
2529 default:
2530 /* Operands */
2531 if (nfa_calc_size == TRUE)
2532 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002533 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002534 break;
2535 }
2536 s = new_state(*p, NULL, NULL);
2537 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002538 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539 PUSH(frag(s, list1(&s->out)));
2540 break;
2541
2542 } /* switch(*p) */
2543
2544 } /* for(p = postfix; *p; ++p) */
2545
2546 if (nfa_calc_size == TRUE)
2547 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002548 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002549 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550 }
2551
2552 e = POP();
2553 if (stackp != stack)
2554 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2555
2556 if (istate >= nstate)
2557 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2558
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002559 matchstate = &state_ptr[istate++]; /* the match state */
2560 matchstate->c = NFA_MATCH;
2561 matchstate->out = matchstate->out1 = NULL;
2562
2563 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002564 ret = e.start;
2565
2566theend:
2567 vim_free(stack);
2568 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002569
2570#undef POP1
2571#undef PUSH1
2572#undef POP2
2573#undef PUSH2
2574#undef POP
2575#undef PUSH
2576}
2577
2578/****************************************************************
2579 * NFA execution code.
2580 ****************************************************************/
2581
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002582typedef struct
2583{
2584 int in_use; /* number of subexpr with useful info */
2585
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002586 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002587 union
2588 {
2589 struct multipos
2590 {
2591 lpos_T start;
2592 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002593 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002594 struct linepos
2595 {
2596 char_u *start;
2597 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002598 } line[NSUBEXP];
2599 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002600} regsub_T;
2601
Bram Moolenaar963fee22013-05-26 21:47:28 +02002602/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002603typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002604{
2605 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002606 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002607 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002608} nfa_thread_T;
2609
Bram Moolenaar963fee22013-05-26 21:47:28 +02002610/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002611typedef struct
2612{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002613 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002614 int n; /* nr of states currently in "t" */
2615 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002616 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002617} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618
Bram Moolenaar5714b802013-05-28 22:03:20 +02002619#ifdef ENABLE_LOG
2620 static void
2621log_subexpr(sub)
2622 regsub_T *sub;
2623{
2624 int j;
2625
2626 for (j = 0; j < sub->in_use; j++)
2627 if (REG_MULTI)
2628 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2629 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002630 sub->list.multi[j].start.col,
2631 (int)sub->list.multi[j].start.lnum,
2632 sub->list.multi[j].end.col,
2633 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002634 else
2635 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2636 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002637 (char *)sub->list.line[j].start,
2638 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002639 fprintf(log_fd, "\n");
2640}
2641#endif
2642
Bram Moolenaar963fee22013-05-26 21:47:28 +02002643/* Used during execution: whether a match has been found. */
2644static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002645
Bram Moolenaar428e9872013-05-30 17:05:39 +02002646static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002647static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2648static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649
Bram Moolenaar428e9872013-05-30 17:05:39 +02002650/*
2651 * Return TRUE if "sub1" and "sub2" have the same positions.
2652 */
2653 static int
2654sub_equal(sub1, sub2)
2655 regsub_T *sub1;
2656 regsub_T *sub2;
2657{
2658 int i;
2659 int todo;
2660 linenr_T s1, e1;
2661 linenr_T s2, e2;
2662 char_u *sp1, *ep1;
2663 char_u *sp2, *ep2;
2664
2665 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2666 if (REG_MULTI)
2667 {
2668 for (i = 0; i < todo; ++i)
2669 {
2670 if (i < sub1->in_use)
2671 {
2672 s1 = sub1->list.multi[i].start.lnum;
2673 e1 = sub1->list.multi[i].end.lnum;
2674 }
2675 else
2676 {
2677 s1 = 0;
2678 e1 = 0;
2679 }
2680 if (i < sub2->in_use)
2681 {
2682 s2 = sub2->list.multi[i].start.lnum;
2683 e2 = sub2->list.multi[i].end.lnum;
2684 }
2685 else
2686 {
2687 s2 = 0;
2688 e2 = 0;
2689 }
2690 if (s1 != s2 || e1 != e2)
2691 return FALSE;
2692 if (s1 != 0 && sub1->list.multi[i].start.col
2693 != sub2->list.multi[i].start.col)
2694 return FALSE;
2695 if (e1 != 0 && sub1->list.multi[i].end.col
2696 != sub2->list.multi[i].end.col)
2697 return FALSE;
2698 }
2699 }
2700 else
2701 {
2702 for (i = 0; i < todo; ++i)
2703 {
2704 if (i < sub1->in_use)
2705 {
2706 sp1 = sub1->list.line[i].start;
2707 ep1 = sub1->list.line[i].end;
2708 }
2709 else
2710 {
2711 sp1 = NULL;
2712 ep1 = NULL;
2713 }
2714 if (i < sub2->in_use)
2715 {
2716 sp2 = sub2->list.line[i].start;
2717 ep2 = sub2->list.line[i].end;
2718 }
2719 else
2720 {
2721 sp2 = NULL;
2722 ep2 = NULL;
2723 }
2724 if (sp1 != sp2 || ep1 != ep2)
2725 return FALSE;
2726 }
2727 }
2728
2729 return TRUE;
2730}
2731
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002733addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002734 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002735 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002736 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002737 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002739 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002740 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002741 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002742 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002743 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002744 int i;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002745#ifdef ENABLE_LOG
2746 int did_print = FALSE;
2747#endif
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)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002785 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002786 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)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002795 {
2796skip_add:
2797#ifdef ENABLE_LOG
2798 nfa_set_code(state->c);
2799 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
2800 abs(state->id), l->id, state->c, code);
2801#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02002802 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002803 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02002804
2805 /* See if the same state is already in the list with the same
2806 * positions. */
2807 for (i = 0; i < l->n; ++i)
2808 {
2809 thread = &l->t[i];
2810 if (thread->state->id == state->id
2811 && sub_equal(&thread->sub, sub))
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002812 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002813 }
2814 }
2815
2816 /* when there are backreferences the number of states may be (a
2817 * lot) bigger */
2818 if (nfa_has_backref && l->n == l->len)
2819 {
2820 int newlen = l->len * 3 / 2 + 50;
2821
2822 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2823 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002825
2826 /* add the state to the list */
2827 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002828 thread = &l->t[l->n++];
2829 thread->state = state;
2830 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002831 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002833 /* Copy the match start and end positions. */
2834 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002835 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002836 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002837 sizeof(struct multipos) * sub->in_use);
2838 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002839 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002840 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002841 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002843#ifdef ENABLE_LOG
2844 {
2845 int col;
2846
2847 if (thread->sub.in_use <= 0)
2848 col = -1;
2849 else if (REG_MULTI)
2850 col = thread->sub.list.multi[0].start.col;
2851 else
2852 col = (int)(thread->sub.list.line[0].start - regline);
2853 nfa_set_code(state->c);
2854 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
2855 abs(state->id), l->id, state->c, code, col);
2856 did_print = TRUE;
2857 }
2858#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002859 }
2860
2861#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002862 if (!did_print)
2863 {
2864 int col;
2865
2866 if (sub->in_use <= 0)
2867 col = -1;
2868 else if (REG_MULTI)
2869 col = sub->list.multi[0].start.col;
2870 else
2871 col = (int)(sub->list.line[0].start - regline);
2872 nfa_set_code(state->c);
2873 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
2874 abs(state->id), l->id, state->c, code, col);
2875 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876#endif
2877 switch (state->c)
2878 {
2879 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002880 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881 break;
2882
2883 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002884 addstate(l, state->out, sub, off);
2885 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 break;
2887
2888#if 0
2889 case NFA_END_NEG_RANGE:
2890 /* Nothing to handle here. nfa_regmatch() will take care of it */
2891 break;
2892
2893 case NFA_NOT:
2894 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2895#ifdef ENABLE_LOG
2896 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2897#endif
2898 break;
2899
2900 case NFA_COMPOSING:
2901 /* nfa_regmatch() will match all the bytes of this composing char. */
2902 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002903#endif
2904
Bram Moolenaar5714b802013-05-28 22:03:20 +02002905 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002906 case NFA_NOPEN:
2907 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002908 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002909 break;
2910
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002911 case NFA_MOPEN + 0:
2912 case NFA_MOPEN + 1:
2913 case NFA_MOPEN + 2:
2914 case NFA_MOPEN + 3:
2915 case NFA_MOPEN + 4:
2916 case NFA_MOPEN + 5:
2917 case NFA_MOPEN + 6:
2918 case NFA_MOPEN + 7:
2919 case NFA_MOPEN + 8:
2920 case NFA_MOPEN + 9:
2921 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002922 if (state->c == NFA_ZSTART)
2923 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002924 else
2925 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002927 /* Set the position (with "off") in the subexpression. Save and
2928 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02002929 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002930 if (REG_MULTI)
2931 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002932 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002933 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002934 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002935 save_in_use = -1;
2936 }
2937 else
2938 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002939 save_in_use = sub->in_use;
2940 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002941 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002942 sub->list.multi[i].start.lnum = -1;
2943 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002944 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002945 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002946 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002947 if (off == -1)
2948 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002949 sub->list.multi[subidx].start.lnum = reglnum + 1;
2950 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002951 }
2952 else
2953 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002954 sub->list.multi[subidx].start.lnum = reglnum;
2955 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002956 (colnr_T)(reginput - regline + off);
2957 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002958 }
2959 else
2960 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002961 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002962 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002963 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002964 save_in_use = -1;
2965 }
2966 else
2967 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002968 save_in_use = sub->in_use;
2969 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002970 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002971 sub->list.line[i].start = NULL;
2972 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002973 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002974 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002975 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002976 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002977 }
2978
Bram Moolenaar5714b802013-05-28 22:03:20 +02002979 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002980
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002981 if (save_in_use == -1)
2982 {
2983 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002984 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002985 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002986 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002987 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002988 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02002989 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002990 break;
2991
2992 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002993 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002994 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002995 /* Do not overwrite the position set by \ze. If no \ze
2996 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002997 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002998 break;
2999 }
3000 case NFA_MCLOSE + 1:
3001 case NFA_MCLOSE + 2:
3002 case NFA_MCLOSE + 3:
3003 case NFA_MCLOSE + 4:
3004 case NFA_MCLOSE + 5:
3005 case NFA_MCLOSE + 6:
3006 case NFA_MCLOSE + 7:
3007 case NFA_MCLOSE + 8:
3008 case NFA_MCLOSE + 9:
3009 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003010 if (state->c == NFA_ZEND)
3011 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003012 else
3013 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003015 /* We don't fill in gaps here, there must have been an MOPEN that
3016 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003017 save_in_use = sub->in_use;
3018 if (sub->in_use <= subidx)
3019 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003020 if (REG_MULTI)
3021 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003022 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003023 if (off == -1)
3024 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003025 sub->list.multi[subidx].end.lnum = reglnum + 1;
3026 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003027 }
3028 else
3029 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003030 sub->list.multi[subidx].end.lnum = reglnum;
3031 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003032 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003033 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003034 }
3035 else
3036 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003037 save_ptr = sub->list.line[subidx].end;
3038 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003039 }
3040
Bram Moolenaar5714b802013-05-28 22:03:20 +02003041 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003042
3043 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003044 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003046 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003047 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003048 break;
3049 }
3050}
3051
3052/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003053 * Like addstate(), but the new state(s) are put at position "*ip".
3054 * Used for zero-width matches, next state to use is the added one.
3055 * This makes sure the order of states to be tried does not change, which
3056 * matters for alternatives.
3057 */
3058 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003059addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003060 nfa_list_T *l; /* runtime state list */
3061 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003062 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003063 int *ip;
3064{
3065 int tlen = l->n;
3066 int count;
3067 int i = *ip;
3068
3069 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003070 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003071
3072 /* when "*ip" was at the end of the list, nothing to do */
3073 if (i + 1 == tlen)
3074 return;
3075
3076 /* re-order to put the new state at the current position */
3077 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003078 if (count == 1)
3079 {
3080 /* overwrite the current state */
3081 l->t[i] = l->t[l->n - 1];
3082 }
3083 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003084 {
3085 /* make space for new states, then move them from the
3086 * end to the current position */
3087 mch_memmove(&(l->t[i + count]),
3088 &(l->t[i + 1]),
3089 sizeof(nfa_thread_T) * (l->n - i - 1));
3090 mch_memmove(&(l->t[i]),
3091 &(l->t[l->n - 1]),
3092 sizeof(nfa_thread_T) * count);
3093 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003094 --l->n;
3095 *ip = i - 1;
3096}
3097
3098/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003099 * Check character class "class" against current character c.
3100 */
3101 static int
3102check_char_class(class, c)
3103 int class;
3104 int c;
3105{
3106 switch (class)
3107 {
3108 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003109 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003110 return OK;
3111 break;
3112 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003113 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114 return OK;
3115 break;
3116 case NFA_CLASS_BLANK:
3117 if (c == ' ' || c == '\t')
3118 return OK;
3119 break;
3120 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003121 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122 return OK;
3123 break;
3124 case NFA_CLASS_DIGIT:
3125 if (VIM_ISDIGIT(c))
3126 return OK;
3127 break;
3128 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003129 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003130 return OK;
3131 break;
3132 case NFA_CLASS_LOWER:
3133 if (MB_ISLOWER(c))
3134 return OK;
3135 break;
3136 case NFA_CLASS_PRINT:
3137 if (vim_isprintc(c))
3138 return OK;
3139 break;
3140 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003141 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003142 return OK;
3143 break;
3144 case NFA_CLASS_SPACE:
3145 if ((c >=9 && c <= 13) || (c == ' '))
3146 return OK;
3147 break;
3148 case NFA_CLASS_UPPER:
3149 if (MB_ISUPPER(c))
3150 return OK;
3151 break;
3152 case NFA_CLASS_XDIGIT:
3153 if (vim_isxdigit(c))
3154 return OK;
3155 break;
3156 case NFA_CLASS_TAB:
3157 if (c == '\t')
3158 return OK;
3159 break;
3160 case NFA_CLASS_RETURN:
3161 if (c == '\r')
3162 return OK;
3163 break;
3164 case NFA_CLASS_BACKSPACE:
3165 if (c == '\b')
3166 return OK;
3167 break;
3168 case NFA_CLASS_ESCAPE:
3169 if (c == '\033')
3170 return OK;
3171 break;
3172
3173 default:
3174 /* should not be here :P */
3175 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3176 }
3177 return FAIL;
3178}
3179
Bram Moolenaar5714b802013-05-28 22:03:20 +02003180static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3181
3182/*
3183 * Check for a match with subexpression "subidx".
3184 * return TRUE if it matches.
3185 */
3186 static int
3187match_backref(sub, subidx, bytelen)
3188 regsub_T *sub; /* pointers to subexpressions */
3189 int subidx;
3190 int *bytelen; /* out: length of match in bytes */
3191{
3192 int len;
3193
3194 if (sub->in_use <= subidx)
3195 {
3196retempty:
3197 /* backref was not set, match an empty string */
3198 *bytelen = 0;
3199 return TRUE;
3200 }
3201
3202 if (REG_MULTI)
3203 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003204 if (sub->list.multi[subidx].start.lnum < 0
3205 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003206 goto retempty;
3207 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003208 len = sub->list.multi[subidx].end.col
3209 - sub->list.multi[subidx].start.col;
3210 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003211 reginput, &len) == 0)
3212 {
3213 *bytelen = len;
3214 return TRUE;
3215 }
3216 }
3217 else
3218 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003219 if (sub->list.line[subidx].start == NULL
3220 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003221 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003222 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3223 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003224 {
3225 *bytelen = len;
3226 return TRUE;
3227 }
3228 }
3229 return FALSE;
3230}
3231
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232/*
3233 * Set all NFA nodes' list ID equal to -1.
3234 */
3235 static void
3236nfa_set_neg_listids(start)
3237 nfa_state_T *start;
3238{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003239 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 {
3241 start->lastlist = -1;
3242 nfa_set_neg_listids(start->out);
3243 nfa_set_neg_listids(start->out1);
3244 }
3245}
3246
3247/*
3248 * Set all NFA nodes' list ID equal to 0.
3249 */
3250 static void
3251nfa_set_null_listids(start)
3252 nfa_state_T *start;
3253{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003254 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003255 {
3256 start->lastlist = 0;
3257 nfa_set_null_listids(start->out);
3258 nfa_set_null_listids(start->out1);
3259 }
3260}
3261
3262/*
3263 * Save list IDs for all NFA states in "list".
3264 */
3265 static void
3266nfa_save_listids(start, list)
3267 nfa_state_T *start;
3268 int *list;
3269{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003270 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 {
3272 list[abs(start->id)] = start->lastlist;
3273 start->lastlist = -1;
3274 nfa_save_listids(start->out, list);
3275 nfa_save_listids(start->out1, list);
3276 }
3277}
3278
3279/*
3280 * Restore list IDs from "list" to all NFA states.
3281 */
3282 static void
3283nfa_restore_listids(start, list)
3284 nfa_state_T *start;
3285 int *list;
3286{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003287 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288 {
3289 start->lastlist = list[abs(start->id)];
3290 nfa_restore_listids(start->out, list);
3291 nfa_restore_listids(start->out1, list);
3292 }
3293}
3294
Bram Moolenaar423532e2013-05-29 21:14:42 +02003295 static int
3296nfa_re_num_cmp(val, op, pos)
3297 long_u val;
3298 int op;
3299 long_u pos;
3300{
3301 if (op == 1) return pos > val;
3302 if (op == 2) return pos < val;
3303 return val == pos;
3304}
3305
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003306static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
3307
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308/*
3309 * Main matching routine.
3310 *
3311 * Run NFA to determine whether it matches reginput.
3312 *
3313 * Return TRUE if there is a match, FALSE otherwise.
3314 * Note: Caller must ensure that: start != NULL.
3315 */
3316 static int
3317nfa_regmatch(start, submatch, m)
3318 nfa_state_T *start;
3319 regsub_T *submatch;
3320 regsub_T *m;
3321{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322 int result;
3323 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003324 int flag = 0;
3325 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003326 int go_to_nextline = FALSE;
3327 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328 char_u *old_reginput = NULL;
3329 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003330 nfa_list_T list[3];
3331 nfa_list_T *listtbl[2][2];
3332 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003334 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003335 nfa_list_T *thislist;
3336 nfa_list_T *nextlist;
3337 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003338 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003339#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003340 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003341
3342 if (debug == NULL)
3343 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003344 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003345 return FALSE;
3346 }
3347#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003348 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003349
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003350 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003351 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003352 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3353 list[0].len = nstate + 1;
3354 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3355 list[1].len = nstate + 1;
3356 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3357 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3359 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360
3361#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003362 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 if (log_fd != NULL)
3364 {
3365 fprintf(log_fd, "**********************************\n");
3366 nfa_set_code(start->c);
3367 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3368 abs(start->id), code);
3369 fprintf(log_fd, "**********************************\n");
3370 }
3371 else
3372 {
3373 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3374 log_fd = stderr;
3375 }
3376#endif
3377
3378 thislist = &list[0];
3379 thislist->n = 0;
3380 nextlist = &list[1];
3381 nextlist->n = 0;
3382 neglist = &list[2];
3383 neglist->n = 0;
3384#ifdef ENABLE_LOG
3385 fprintf(log_fd, "(---) STARTSTATE\n");
3386#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003387 thislist->id = listid;
3388 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003389
3390 /* There are two cases when the NFA advances: 1. input char matches the
3391 * NFA node and 2. input char does not match the NFA node, but the next
3392 * node is NFA_NOT. The following macro calls addstate() according to
3393 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3394 listtbl[0][0] = NULL;
3395 listtbl[0][1] = neglist;
3396 listtbl[1][0] = nextlist;
3397 listtbl[1][1] = NULL;
3398#define ADD_POS_NEG_STATE(node) \
3399 ll = listtbl[result ? 1 : 0][node->negated]; \
3400 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003401 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402
3403
3404 /*
3405 * Run for each character.
3406 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003407 for (;;)
3408 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003409 int curc;
3410 int clen;
3411
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412#ifdef FEAT_MBYTE
3413 if (has_mbyte)
3414 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003415 curc = (*mb_ptr2char)(reginput);
3416 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 }
3418 else
3419#endif
3420 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003421 curc = *reginput;
3422 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003423 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003424 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003425 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003426 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003427 go_to_nextline = FALSE;
3428 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429
3430 /* swap lists */
3431 thislist = &list[flag];
3432 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003433 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003434 listtbl[1][0] = nextlist;
3435 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003436 thislist->id = listid;
3437 nextlist->id = listid + 1;
3438 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439
3440#ifdef ENABLE_LOG
3441 fprintf(log_fd, "------------------------------------------\n");
3442 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003443 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003445 {
3446 int i;
3447
3448 for (i = 0; i < thislist->n; i++)
3449 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3450 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003451 fprintf(log_fd, "\n");
3452#endif
3453
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003454#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 fprintf(debug, "\n-------------------\n");
3456#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003457 /*
3458 * If the state lists are empty we can stop.
3459 */
3460 if (thislist->n == 0 && neglist->n == 0)
3461 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462
3463 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003464 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 {
3466 if (neglist->n > 0)
3467 {
3468 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003469 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003470 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471 }
3472 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003473 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003475#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003476 nfa_set_code(t->state->c);
3477 fprintf(debug, "%s, ", code);
3478#endif
3479#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003480 {
3481 int col;
3482
3483 if (t->sub.in_use <= 0)
3484 col = -1;
3485 else if (REG_MULTI)
3486 col = t->sub.list.multi[0].start.col;
3487 else
3488 col = (int)(t->sub.list.line[0].start - regline);
3489 nfa_set_code(t->state->c);
3490 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3491 abs(t->state->id), (int)t->state->c, code, col);
3492 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493#endif
3494
3495 /*
3496 * Handle the possible codes of the current state.
3497 * The most important is NFA_MATCH.
3498 */
3499 switch (t->state->c)
3500 {
3501 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003502 {
3503 int j;
3504
Bram Moolenaar963fee22013-05-26 21:47:28 +02003505 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003506 submatch->in_use = t->sub.in_use;
3507 if (REG_MULTI)
3508 for (j = 0; j < submatch->in_use; j++)
3509 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003510 submatch->list.multi[j].start =
3511 t->sub.list.multi[j].start;
3512 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003513 }
3514 else
3515 for (j = 0; j < submatch->in_use; j++)
3516 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003517 submatch->list.line[j].start =
3518 t->sub.list.line[j].start;
3519 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003520 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003521#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003522 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003523#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003524 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003525 * states at this position. When the list of states is going
3526 * to be empty quit without advancing, so that "reginput" is
3527 * correct. */
3528 if (nextlist->n == 0 && neglist->n == 0)
3529 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003530 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003531 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003532
3533 case NFA_END_INVISIBLE:
3534 /* This is only encountered after a NFA_START_INVISIBLE node.
3535 * They surround a zero-width group, used with "\@=" and "\&".
3536 * If we got here, it means that the current "invisible" group
3537 * finished successfully, so return control to the parent
3538 * nfa_regmatch(). Submatches are stored in *m, and used in
3539 * the parent call. */
3540 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003541 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542 else
3543 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003544 /* TODO: only copy positions in use. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003545 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003546 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003547 }
3548 break;
3549
3550 case NFA_START_INVISIBLE:
3551 /* Save global variables, and call nfa_regmatch() to check if
3552 * the current concat matches at this position. The concat
3553 * ends with the node NFA_END_INVISIBLE */
3554 old_reginput = reginput;
3555 old_regline = regline;
3556 old_reglnum = reglnum;
3557 if (listids == NULL)
3558 {
3559 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3560 if (listids == NULL)
3561 {
3562 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3563 return 0;
3564 }
3565 }
3566#ifdef ENABLE_LOG
3567 if (log_fd != stderr)
3568 fclose(log_fd);
3569 log_fd = NULL;
3570#endif
3571 /* Have to clear the listid field of the NFA nodes, so that
3572 * nfa_regmatch() and addstate() can run properly after
3573 * recursion. */
3574 nfa_save_listids(start, listids);
3575 nfa_set_null_listids(start);
3576 result = nfa_regmatch(t->state->out, submatch, m);
3577 nfa_set_neg_listids(start);
3578 nfa_restore_listids(start, listids);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003579 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580
3581#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003582 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003583 if (log_fd != NULL)
3584 {
3585 fprintf(log_fd, "****************************\n");
3586 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3587 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3588 fprintf(log_fd, "****************************\n");
3589 }
3590 else
3591 {
3592 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3593 log_fd = stderr;
3594 }
3595#endif
3596 if (result == TRUE)
3597 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003598 int j;
3599
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 /* Restore position in input text */
3601 reginput = old_reginput;
3602 regline = old_regline;
3603 reglnum = old_reglnum;
3604 /* Copy submatch info from the recursive call */
3605 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003606 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003607 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003608 t->sub.list.multi[j].start = m->list.multi[j].start;
3609 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 }
3611 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003612 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003614 t->sub.list.line[j].start = m->list.line[j].start;
3615 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003616 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003617 if (m->in_use > t->sub.in_use)
3618 t->sub.in_use = m->in_use;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003619
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003620 /* t->state->out1 is the corresponding END_INVISIBLE node;
3621 * Add it to the current list (zero-width match). */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003622 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003623 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003624 }
3625 else
3626 {
3627 /* continue with next input char */
3628 reginput = old_reginput;
3629 }
3630 break;
3631
3632 case NFA_BOL:
3633 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003634 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003635 break;
3636
3637 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003638 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003639 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640 break;
3641
3642 case NFA_BOW:
3643 {
3644 int bow = TRUE;
3645
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003646 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003647 bow = FALSE;
3648#ifdef FEAT_MBYTE
3649 else if (has_mbyte)
3650 {
3651 int this_class;
3652
3653 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003654 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655 if (this_class <= 1)
3656 bow = FALSE;
3657 else if (reg_prev_class() == this_class)
3658 bow = FALSE;
3659 }
3660#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003661 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003662 || (reginput > regline
3663 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003664 bow = FALSE;
3665 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003666 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003667 break;
3668 }
3669
3670 case NFA_EOW:
3671 {
3672 int eow = TRUE;
3673
3674 if (reginput == regline)
3675 eow = FALSE;
3676#ifdef FEAT_MBYTE
3677 else if (has_mbyte)
3678 {
3679 int this_class, prev_class;
3680
3681 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003682 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003683 prev_class = reg_prev_class();
3684 if (this_class == prev_class
3685 || prev_class == 0 || prev_class == 1)
3686 eow = FALSE;
3687 }
3688#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003689 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003690 || (reginput[0] != NUL
3691 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003692 eow = FALSE;
3693 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003694 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003695 break;
3696 }
3697
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003698#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003699 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003700 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003701 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003702 int len = 0;
3703 nfa_state_T *end;
3704 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003705 int cchars[MAX_MCO];
3706 int ccount = 0;
3707 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003708
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003710 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003711 if (utf_iscomposing(sta->c))
3712 {
3713 /* Only match composing character(s), ignore base
3714 * character. Used for ".{composing}" and "{composing}"
3715 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003716 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003717 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003718 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003719 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003720 /* If \Z was present, then ignore composing characters.
3721 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003722 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003723 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003724 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003725 else
3726 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003727 while (sta->c != NFA_END_COMPOSING)
3728 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003730
3731 /* Check base character matches first, unless ignored. */
3732 else if (len > 0 || mc == sta->c)
3733 {
3734 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003735 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003736 len += mb_char2len(mc);
3737 sta = sta->out;
3738 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003740 /* We don't care about the order of composing characters.
3741 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003742 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003743 {
3744 mc = mb_ptr2char(reginput + len);
3745 cchars[ccount++] = mc;
3746 len += mb_char2len(mc);
3747 if (ccount == MAX_MCO)
3748 break;
3749 }
3750
3751 /* Check that each composing char in the pattern matches a
3752 * composing char in the text. We do not check if all
3753 * composing chars are matched. */
3754 result = OK;
3755 while (sta->c != NFA_END_COMPOSING)
3756 {
3757 for (j = 0; j < ccount; ++j)
3758 if (cchars[j] == sta->c)
3759 break;
3760 if (j == ccount)
3761 {
3762 result = FAIL;
3763 break;
3764 }
3765 sta = sta->out;
3766 }
3767 }
3768 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003769 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003770
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003771 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003772 ADD_POS_NEG_STATE(end);
3773 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003774 }
3775#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003776
3777 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003778 if (curc == NUL && !reg_line_lbr && REG_MULTI
3779 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003780 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003781 go_to_nextline = TRUE;
3782 /* Pass -1 for the offset, which means taking the position
3783 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003784 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003785 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003786 else if (curc == '\n' && reg_line_lbr)
3787 {
3788 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003789 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003790 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003791 break;
3792
3793 case NFA_CLASS_ALNUM:
3794 case NFA_CLASS_ALPHA:
3795 case NFA_CLASS_BLANK:
3796 case NFA_CLASS_CNTRL:
3797 case NFA_CLASS_DIGIT:
3798 case NFA_CLASS_GRAPH:
3799 case NFA_CLASS_LOWER:
3800 case NFA_CLASS_PRINT:
3801 case NFA_CLASS_PUNCT:
3802 case NFA_CLASS_SPACE:
3803 case NFA_CLASS_UPPER:
3804 case NFA_CLASS_XDIGIT:
3805 case NFA_CLASS_TAB:
3806 case NFA_CLASS_RETURN:
3807 case NFA_CLASS_BACKSPACE:
3808 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003809 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003810 ADD_POS_NEG_STATE(t->state);
3811 break;
3812
3813 case NFA_END_NEG_RANGE:
3814 /* This follows a series of negated nodes, like:
3815 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003816 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003817 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003818 break;
3819
3820 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003821 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003822 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003823 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003824 break;
3825
3826 /*
3827 * Character classes like \a for alpha, \d for digit etc.
3828 */
3829 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003830 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831 ADD_POS_NEG_STATE(t->state);
3832 break;
3833
3834 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003835 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003836 ADD_POS_NEG_STATE(t->state);
3837 break;
3838
3839 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003840 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003841 ADD_POS_NEG_STATE(t->state);
3842 break;
3843
3844 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003845 result = !VIM_ISDIGIT(curc)
3846 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003847 ADD_POS_NEG_STATE(t->state);
3848 break;
3849
3850 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003851 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003852 ADD_POS_NEG_STATE(t->state);
3853 break;
3854
3855 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003856 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003857 ADD_POS_NEG_STATE(t->state);
3858 break;
3859
3860 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003861 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003862 ADD_POS_NEG_STATE(t->state);
3863 break;
3864
3865 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003866 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003867 ADD_POS_NEG_STATE(t->state);
3868 break;
3869
3870 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003871 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003872 ADD_POS_NEG_STATE(t->state);
3873 break;
3874
3875 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003876 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003877 ADD_POS_NEG_STATE(t->state);
3878 break;
3879
3880 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003881 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003882 ADD_POS_NEG_STATE(t->state);
3883 break;
3884
3885 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003886 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003887 ADD_POS_NEG_STATE(t->state);
3888 break;
3889
3890 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003891 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003892 ADD_POS_NEG_STATE(t->state);
3893 break;
3894
3895 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003896 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003897 ADD_POS_NEG_STATE(t->state);
3898 break;
3899
3900 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003901 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003902 ADD_POS_NEG_STATE(t->state);
3903 break;
3904
3905 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003906 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003907 ADD_POS_NEG_STATE(t->state);
3908 break;
3909
3910 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003911 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003912 ADD_POS_NEG_STATE(t->state);
3913 break;
3914
3915 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003916 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917 ADD_POS_NEG_STATE(t->state);
3918 break;
3919
3920 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003921 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922 ADD_POS_NEG_STATE(t->state);
3923 break;
3924
3925 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003926 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003927 ADD_POS_NEG_STATE(t->state);
3928 break;
3929
3930 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003931 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003932 ADD_POS_NEG_STATE(t->state);
3933 break;
3934
3935 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003936 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003937 ADD_POS_NEG_STATE(t->state);
3938 break;
3939
3940 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003941 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003942 ADD_POS_NEG_STATE(t->state);
3943 break;
3944
3945 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003946 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003947 ADD_POS_NEG_STATE(t->state);
3948 break;
3949
3950 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003951 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003952 ADD_POS_NEG_STATE(t->state);
3953 break;
3954
3955 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003956 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003957 ADD_POS_NEG_STATE(t->state);
3958 break;
3959
Bram Moolenaar5714b802013-05-28 22:03:20 +02003960 case NFA_BACKREF1:
3961 case NFA_BACKREF2:
3962 case NFA_BACKREF3:
3963 case NFA_BACKREF4:
3964 case NFA_BACKREF5:
3965 case NFA_BACKREF6:
3966 case NFA_BACKREF7:
3967 case NFA_BACKREF8:
3968 case NFA_BACKREF9:
3969 /* \1 .. \9 */
3970 {
3971 int subidx = t->state->c - NFA_BACKREF1 + 1;
3972 int bytelen;
3973
3974 result = match_backref(&t->sub, subidx, &bytelen);
3975 if (result)
3976 {
3977 if (bytelen == 0)
3978 {
3979 /* empty match always works, add NFA_SKIP with zero to
3980 * be used next */
3981 addstate_here(thislist, t->state->out, &t->sub,
3982 &listidx);
3983 thislist->t[listidx + 1].count = 0;
3984 }
3985 else if (bytelen <= clen)
3986 {
3987 /* match current character, jump ahead to out of
3988 * NFA_SKIP */
3989 addstate(nextlist, t->state->out->out, &t->sub, clen);
3990#ifdef ENABLE_LOG
3991 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
3992#endif
3993 }
3994 else
3995 {
3996 /* skip ofer the matched characters, set character
3997 * count in NFA_SKIP */
3998 addstate(nextlist, t->state->out, &t->sub, bytelen);
3999 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4000#ifdef ENABLE_LOG
4001 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4002#endif
4003 }
4004
4005 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004006 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004007 }
4008 case NFA_SKIP:
4009 /* charater of previous matching \1 .. \9 */
4010 if (t->count - clen <= 0)
4011 {
4012 /* end of match, go to what follows */
4013 addstate(nextlist, t->state->out, &t->sub, clen);
4014#ifdef ENABLE_LOG
4015 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4016#endif
4017 }
4018 else
4019 {
4020 /* add state again with decremented count */
4021 addstate(nextlist, t->state, &t->sub, 0);
4022 nextlist->t[nextlist->n - 1].count = t->count - clen;
4023#ifdef ENABLE_LOG
4024 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4025#endif
4026 }
4027 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004028
4029 case NFA_SKIP_CHAR:
4030 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004031 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004032 /* TODO: should not happen? */
4033 break;
4034
Bram Moolenaar423532e2013-05-29 21:14:42 +02004035 case NFA_LNUM:
4036 case NFA_LNUM_GT:
4037 case NFA_LNUM_LT:
4038 result = (REG_MULTI &&
4039 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4040 (long_u)(reglnum + reg_firstlnum)));
4041 if (result)
4042 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4043 break;
4044
4045 case NFA_COL:
4046 case NFA_COL_GT:
4047 case NFA_COL_LT:
4048 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4049 (long_u)(reginput - regline) + 1);
4050 if (result)
4051 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4052 break;
4053
4054 case NFA_VCOL:
4055 case NFA_VCOL_GT:
4056 case NFA_VCOL_LT:
4057 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4058 (long_u)win_linetabsize(
4059 reg_win == NULL ? curwin : reg_win,
4060 regline, (colnr_T)(reginput - regline)) + 1);
4061 if (result)
4062 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4063 break;
4064
4065 case NFA_CURSOR:
4066 result = (reg_win != NULL
4067 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4068 && ((colnr_T)(reginput - regline)
4069 == reg_win->w_cursor.col));
4070 if (result)
4071 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4072 break;
4073
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004074 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004075 {
4076 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004077
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004078 /* TODO: put this in #ifdef later */
4079 if (c < -256)
4080 EMSGN("INTERNAL: Negative state char: %ld", c);
4081 if (is_Magic(c))
4082 c = un_Magic(c);
4083 result = (c == curc);
4084
4085 if (!result && ireg_ic)
4086 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004087#ifdef FEAT_MBYTE
4088 /* If there is a composing character which is not being
4089 * ignored there can be no match. Match with composing
4090 * character uses NFA_COMPOSING above. */
4091 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004092 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004093 result = FALSE;
4094#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 ADD_POS_NEG_STATE(t->state);
4096 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004097 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004098 }
4099
4100 } /* for (thislist = thislist; thislist->state; thislist++) */
4101
Bram Moolenaare23febd2013-05-26 18:40:14 +02004102 /* Look for the start of a match in the current position by adding the
4103 * start state to the list of states.
4104 * The first found match is the leftmost one, thus the order of states
4105 * matters!
4106 * Do not add the start state in recursive calls of nfa_regmatch(),
4107 * because recursive calls should only start in the first position.
4108 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02004109 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaar75eb1612013-05-29 18:45:11 +02004110 && reglnum == 0 && clen != 0
4111 && (ireg_maxcol == 0
4112 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 {
4114#ifdef ENABLE_LOG
4115 fprintf(log_fd, "(---) STARTSTATE\n");
4116#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004117 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004118 }
4119
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004120#ifdef ENABLE_LOG
4121 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004122 {
4123 int i;
4124
4125 for (i = 0; i < thislist->n; i++)
4126 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4127 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004128 fprintf(log_fd, "\n");
4129#endif
4130
4131nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004132 /* Advance to the next character, or advance to the next line, or
4133 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004134 if (clen != 0)
4135 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004136 else if (go_to_nextline)
4137 reg_nextline();
4138 else
4139 break;
4140 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141
4142#ifdef ENABLE_LOG
4143 if (log_fd != stderr)
4144 fclose(log_fd);
4145 log_fd = NULL;
4146#endif
4147
4148theend:
4149 /* Free memory */
4150 vim_free(list[0].t);
4151 vim_free(list[1].t);
4152 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004153 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004154#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004155#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004156 fclose(debug);
4157#endif
4158
Bram Moolenaar963fee22013-05-26 21:47:28 +02004159 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004160}
4161
4162/*
4163 * Try match of "prog" with at regline["col"].
4164 * Returns 0 for failure, number of lines contained in the match otherwise.
4165 */
4166 static long
4167nfa_regtry(start, col)
4168 nfa_state_T *start;
4169 colnr_T col;
4170{
4171 int i;
4172 regsub_T sub, m;
4173#ifdef ENABLE_LOG
4174 FILE *f;
4175#endif
4176
4177 reginput = regline + col;
4178 need_clear_subexpr = TRUE;
4179
4180#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004181 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182 if (f != NULL)
4183 {
4184 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4185 fprintf(f, " =======================================================\n");
4186#ifdef DEBUG
4187 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4188#endif
4189 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004190 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004191 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004192 fprintf(f, "\n\n");
4193 fclose(f);
4194 }
4195 else
4196 EMSG(_("Could not open temporary log file for writing "));
4197#endif
4198
4199 if (REG_MULTI)
4200 {
4201 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004202 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4203 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004204 }
4205 else
4206 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004207 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4208 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004209 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004210 sub.in_use = 0;
4211 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004212
4213 if (nfa_regmatch(start, &sub, &m) == FALSE)
4214 return 0;
4215
4216 cleanup_subexpr();
4217 if (REG_MULTI)
4218 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004219 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004220 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004221 reg_startpos[i] = sub.list.multi[i].start;
4222 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004223 }
4224
4225 if (reg_startpos[0].lnum < 0)
4226 {
4227 reg_startpos[0].lnum = 0;
4228 reg_startpos[0].col = col;
4229 }
4230 if (reg_endpos[0].lnum < 0)
4231 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004232 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004233 reg_endpos[0].lnum = reglnum;
4234 reg_endpos[0].col = (int)(reginput - regline);
4235 }
4236 else
4237 /* Use line number of "\ze". */
4238 reglnum = reg_endpos[0].lnum;
4239 }
4240 else
4241 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004242 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004243 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004244 reg_startp[i] = sub.list.line[i].start;
4245 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004246 }
4247
4248 if (reg_startp[0] == NULL)
4249 reg_startp[0] = regline + col;
4250 if (reg_endp[0] == NULL)
4251 reg_endp[0] = reginput;
4252 }
4253
4254 return 1 + reglnum;
4255}
4256
4257/*
4258 * Match a regexp against a string ("line" points to the string) or multiple
4259 * lines ("line" is NULL, use reg_getline()).
4260 *
4261 * Returns 0 for failure, number of lines contained in the match otherwise.
4262 */
4263 static long
4264nfa_regexec_both(line, col)
4265 char_u *line;
4266 colnr_T col; /* column to start looking for match */
4267{
4268 nfa_regprog_T *prog;
4269 long retval = 0L;
4270 int i;
4271
4272 if (REG_MULTI)
4273 {
4274 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4275 line = reg_getline((linenr_T)0); /* relative to the cursor */
4276 reg_startpos = reg_mmatch->startpos;
4277 reg_endpos = reg_mmatch->endpos;
4278 }
4279 else
4280 {
4281 prog = (nfa_regprog_T *)reg_match->regprog;
4282 reg_startp = reg_match->startp;
4283 reg_endp = reg_match->endp;
4284 }
4285
4286 /* Be paranoid... */
4287 if (prog == NULL || line == NULL)
4288 {
4289 EMSG(_(e_null));
4290 goto theend;
4291 }
4292
4293 /* If the start column is past the maximum column: no need to try. */
4294 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4295 goto theend;
4296
4297 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4298 if (prog->regflags & RF_ICASE)
4299 ireg_ic = TRUE;
4300 else if (prog->regflags & RF_NOICASE)
4301 ireg_ic = FALSE;
4302
4303#ifdef FEAT_MBYTE
4304 /* If pattern contains "\Z" overrule value of ireg_icombine */
4305 if (prog->regflags & RF_ICOMBINE)
4306 ireg_icombine = TRUE;
4307#endif
4308
4309 regline = line;
4310 reglnum = 0; /* relative to line */
4311
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004312 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004313 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004314 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004315
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004316 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004317 for (i = 0; i < nstate; ++i)
4318 {
4319 prog->state[i].id = i;
4320 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 }
4322
4323 retval = nfa_regtry(prog->start, col);
4324
4325theend:
4326 return retval;
4327}
4328
4329/*
4330 * Compile a regular expression into internal code for the NFA matcher.
4331 * Returns the program in allocated space. Returns NULL for an error.
4332 */
4333 static regprog_T *
4334nfa_regcomp(expr, re_flags)
4335 char_u *expr;
4336 int re_flags;
4337{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004338 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004339 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004340 int *postfix;
4341
4342 if (expr == NULL)
4343 return NULL;
4344
4345#ifdef DEBUG
4346 nfa_regengine.expr = expr;
4347#endif
4348
4349 init_class_tab();
4350
4351 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4352 return NULL;
4353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004355 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004356 postfix = re2post();
4357 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004358 {
4359 /* TODO: only give this error for debugging? */
4360 if (post_ptr >= post_end)
4361 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004362 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004363 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004364
4365 /*
4366 * In order to build the NFA, we parse the input regexp twice:
4367 * 1. first pass to count size (so we can allocate space)
4368 * 2. second to emit code
4369 */
4370#ifdef ENABLE_LOG
4371 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004372 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004373
4374 if (f != NULL)
4375 {
4376 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4377 fclose(f);
4378 }
4379 }
4380#endif
4381
4382 /*
4383 * PASS 1
4384 * Count number of NFA states in "nstate". Do not build the NFA.
4385 */
4386 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004387
4388 /* Space for compiled regexp */
4389 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4390 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4391 if (prog == NULL)
4392 goto fail;
4393 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004394 state_ptr = prog->state;
4395
4396 /*
4397 * PASS 2
4398 * Build the NFA
4399 */
4400 prog->start = post2nfa(postfix, post_ptr, FALSE);
4401 if (prog->start == NULL)
4402 goto fail;
4403
4404 prog->regflags = regflags;
4405 prog->engine = &nfa_regengine;
4406 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004407 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004408 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004409 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004410#ifdef ENABLE_LOG
4411 nfa_postfix_dump(expr, OK);
4412 nfa_dump(prog);
4413#endif
4414
4415out:
4416 vim_free(post_start);
4417 post_start = post_ptr = post_end = NULL;
4418 state_ptr = NULL;
4419 return (regprog_T *)prog;
4420
4421fail:
4422 vim_free(prog);
4423 prog = NULL;
4424#ifdef ENABLE_LOG
4425 nfa_postfix_dump(expr, FAIL);
4426#endif
4427#ifdef DEBUG
4428 nfa_regengine.expr = NULL;
4429#endif
4430 goto out;
4431}
4432
4433
4434/*
4435 * Match a regexp against a string.
4436 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4437 * Uses curbuf for line count and 'iskeyword'.
4438 *
4439 * Return TRUE if there is a match, FALSE if not.
4440 */
4441 static int
4442nfa_regexec(rmp, line, col)
4443 regmatch_T *rmp;
4444 char_u *line; /* string to match against */
4445 colnr_T col; /* column to start looking for match */
4446{
4447 reg_match = rmp;
4448 reg_mmatch = NULL;
4449 reg_maxline = 0;
4450 reg_line_lbr = FALSE;
4451 reg_buf = curbuf;
4452 reg_win = NULL;
4453 ireg_ic = rmp->rm_ic;
4454#ifdef FEAT_MBYTE
4455 ireg_icombine = FALSE;
4456#endif
4457 ireg_maxcol = 0;
4458 return (nfa_regexec_both(line, col) != 0);
4459}
4460
4461#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4462 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4463
4464static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4465
4466/*
4467 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4468 */
4469 static int
4470nfa_regexec_nl(rmp, line, col)
4471 regmatch_T *rmp;
4472 char_u *line; /* string to match against */
4473 colnr_T col; /* column to start looking for match */
4474{
4475 reg_match = rmp;
4476 reg_mmatch = NULL;
4477 reg_maxline = 0;
4478 reg_line_lbr = TRUE;
4479 reg_buf = curbuf;
4480 reg_win = NULL;
4481 ireg_ic = rmp->rm_ic;
4482#ifdef FEAT_MBYTE
4483 ireg_icombine = FALSE;
4484#endif
4485 ireg_maxcol = 0;
4486 return (nfa_regexec_both(line, col) != 0);
4487}
4488#endif
4489
4490
4491/*
4492 * Match a regexp against multiple lines.
4493 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4494 * Uses curbuf for line count and 'iskeyword'.
4495 *
4496 * Return zero if there is no match. Return number of lines contained in the
4497 * match otherwise.
4498 *
4499 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4500 *
4501 * ! Also NOTE : match may actually be in another line. e.g.:
4502 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4503 *
4504 * +-------------------------+
4505 * |a |
4506 * |b |
4507 * |c |
4508 * | |
4509 * +-------------------------+
4510 *
4511 * then nfa_regexec_multi() returns 3. while the original
4512 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4513 *
4514 * FIXME if this behavior is not compatible.
4515 */
4516 static long
4517nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4518 regmmatch_T *rmp;
4519 win_T *win; /* window in which to search or NULL */
4520 buf_T *buf; /* buffer in which to search */
4521 linenr_T lnum; /* nr of line to start looking for match */
4522 colnr_T col; /* column to start looking for match */
4523 proftime_T *tm UNUSED; /* timeout limit or NULL */
4524{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 reg_match = NULL;
4526 reg_mmatch = rmp;
4527 reg_buf = buf;
4528 reg_win = win;
4529 reg_firstlnum = lnum;
4530 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4531 reg_line_lbr = FALSE;
4532 ireg_ic = rmp->rmm_ic;
4533#ifdef FEAT_MBYTE
4534 ireg_icombine = FALSE;
4535#endif
4536 ireg_maxcol = rmp->rmm_maxcol;
4537
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004538 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539}
4540
4541#ifdef DEBUG
4542# undef ENABLE_LOG
4543#endif