blob: 3699331d672fbecd53c5d1d966ea498b2d1d4b71 [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
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020032enum
33{
34 NFA_SPLIT = -1024,
35 NFA_MATCH,
36 NFA_SKIP_CHAR, /* matches a 0-length char */
37 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
38
39 NFA_CONCAT,
40 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020041 NFA_STAR, /* greedy * */
42 NFA_STAR_NONGREEDY, /* non-greedy * */
43 NFA_QUEST, /* greedy \? */
44 NFA_QUEST_NONGREEDY, /* non-greedy \? */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020045 NFA_NOT, /* used for [^ab] negated char ranges */
46
47 NFA_BOL, /* ^ Begin line */
48 NFA_EOL, /* $ End line */
49 NFA_BOW, /* \< Begin word */
50 NFA_EOW, /* \> End word */
51 NFA_BOF, /* \%^ Begin file */
52 NFA_EOF, /* \%$ End file */
53 NFA_NEWL,
54 NFA_ZSTART, /* Used for \zs */
55 NFA_ZEND, /* Used for \ze */
56 NFA_NOPEN, /* Start of subexpression marked with \%( */
57 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
58 NFA_START_INVISIBLE,
Bram Moolenaar61602c52013-06-01 19:54:43 +020059 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020060 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020061 NFA_COMPOSING, /* Next nodes in NFA are part of the
62 composing multibyte char */
63 NFA_END_COMPOSING, /* End of a composing char in the NFA */
64
65 /* The following are used only in the postfix form, not in the NFA */
66 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
67 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
68 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
69 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
70 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
71
Bram Moolenaar5714b802013-05-28 22:03:20 +020072 NFA_BACKREF1, /* \1 */
73 NFA_BACKREF2, /* \2 */
74 NFA_BACKREF3, /* \3 */
75 NFA_BACKREF4, /* \4 */
76 NFA_BACKREF5, /* \5 */
77 NFA_BACKREF6, /* \6 */
78 NFA_BACKREF7, /* \7 */
79 NFA_BACKREF8, /* \8 */
80 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020081#ifdef FEAT_SYN_HL
82 NFA_ZREF1, /* \z1 */
83 NFA_ZREF2, /* \z2 */
84 NFA_ZREF3, /* \z3 */
85 NFA_ZREF4, /* \z4 */
86 NFA_ZREF5, /* \z5 */
87 NFA_ZREF6, /* \z6 */
88 NFA_ZREF7, /* \z7 */
89 NFA_ZREF8, /* \z8 */
90 NFA_ZREF9, /* \z9 */
91#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +020092 NFA_SKIP, /* Skip characters */
93
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020094 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +020095 NFA_MOPEN1,
96 NFA_MOPEN2,
97 NFA_MOPEN3,
98 NFA_MOPEN4,
99 NFA_MOPEN5,
100 NFA_MOPEN6,
101 NFA_MOPEN7,
102 NFA_MOPEN8,
103 NFA_MOPEN9,
104
105 NFA_MCLOSE,
106 NFA_MCLOSE1,
107 NFA_MCLOSE2,
108 NFA_MCLOSE3,
109 NFA_MCLOSE4,
110 NFA_MCLOSE5,
111 NFA_MCLOSE6,
112 NFA_MCLOSE7,
113 NFA_MCLOSE8,
114 NFA_MCLOSE9,
115
116#ifdef FEAT_SYN_HL
117 NFA_ZOPEN,
118 NFA_ZOPEN1,
119 NFA_ZOPEN2,
120 NFA_ZOPEN3,
121 NFA_ZOPEN4,
122 NFA_ZOPEN5,
123 NFA_ZOPEN6,
124 NFA_ZOPEN7,
125 NFA_ZOPEN8,
126 NFA_ZOPEN9,
127
128 NFA_ZCLOSE,
129 NFA_ZCLOSE1,
130 NFA_ZCLOSE2,
131 NFA_ZCLOSE3,
132 NFA_ZCLOSE4,
133 NFA_ZCLOSE5,
134 NFA_ZCLOSE6,
135 NFA_ZCLOSE7,
136 NFA_ZCLOSE8,
137 NFA_ZCLOSE9,
138#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200139
140 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200141 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200142 NFA_ANYOF, /* Match any character in this string. */
143 NFA_ANYBUT, /* Match any character not in this string. */
144 NFA_IDENT, /* Match identifier char */
145 NFA_SIDENT, /* Match identifier char but no digit */
146 NFA_KWORD, /* Match keyword char */
147 NFA_SKWORD, /* Match word char but no digit */
148 NFA_FNAME, /* Match file name char */
149 NFA_SFNAME, /* Match file name char but no digit */
150 NFA_PRINT, /* Match printable char */
151 NFA_SPRINT, /* Match printable char but no digit */
152 NFA_WHITE, /* Match whitespace char */
153 NFA_NWHITE, /* Match non-whitespace char */
154 NFA_DIGIT, /* Match digit char */
155 NFA_NDIGIT, /* Match non-digit char */
156 NFA_HEX, /* Match hex char */
157 NFA_NHEX, /* Match non-hex char */
158 NFA_OCTAL, /* Match octal char */
159 NFA_NOCTAL, /* Match non-octal char */
160 NFA_WORD, /* Match word char */
161 NFA_NWORD, /* Match non-word char */
162 NFA_HEAD, /* Match head char */
163 NFA_NHEAD, /* Match non-head char */
164 NFA_ALPHA, /* Match alpha char */
165 NFA_NALPHA, /* Match non-alpha char */
166 NFA_LOWER, /* Match lowercase char */
167 NFA_NLOWER, /* Match non-lowercase char */
168 NFA_UPPER, /* Match uppercase char */
169 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200170
171 NFA_CURSOR, /* Match cursor pos */
172 NFA_LNUM, /* Match line number */
173 NFA_LNUM_GT, /* Match > line number */
174 NFA_LNUM_LT, /* Match < line number */
175 NFA_COL, /* Match cursor column */
176 NFA_COL_GT, /* Match > cursor column */
177 NFA_COL_LT, /* Match < cursor column */
178 NFA_VCOL, /* Match cursor virtual column */
179 NFA_VCOL_GT, /* Match > cursor virtual column */
180 NFA_VCOL_LT, /* Match < cursor virtual column */
181
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200182 NFA_FIRST_NL = NFA_ANY + ADD_NL,
183 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
184
185 /* Character classes [:alnum:] etc */
186 NFA_CLASS_ALNUM,
187 NFA_CLASS_ALPHA,
188 NFA_CLASS_BLANK,
189 NFA_CLASS_CNTRL,
190 NFA_CLASS_DIGIT,
191 NFA_CLASS_GRAPH,
192 NFA_CLASS_LOWER,
193 NFA_CLASS_PRINT,
194 NFA_CLASS_PUNCT,
195 NFA_CLASS_SPACE,
196 NFA_CLASS_UPPER,
197 NFA_CLASS_XDIGIT,
198 NFA_CLASS_TAB,
199 NFA_CLASS_RETURN,
200 NFA_CLASS_BACKSPACE,
201 NFA_CLASS_ESCAPE
202};
203
204/* Keep in sync with classchars. */
205static int nfa_classcodes[] = {
206 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
207 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
208 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
209 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
210 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
211 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
212 NFA_UPPER, NFA_NUPPER
213};
214
215static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
216
217/*
218 * NFA errors can be of 3 types:
219 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
220 * silently and revert the to backtracking engine.
221 * syntax_error = FALSE;
222 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
223 * The NFA engine displays an error message, and nothing else happens.
224 * syntax_error = TRUE
225 * *** Unsupported features, when the input regexp uses an operator that is not
226 * implemented in the NFA. The NFA engine fails silently, and reverts to the
227 * old backtracking engine.
228 * syntax_error = FALSE
229 * "The NFA fails" means that "compiling the regexp with the NFA fails":
230 * nfa_regcomp() returns FAIL.
231 */
232static int syntax_error = FALSE;
233
234/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200235static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236
Bram Moolenaar428e9872013-05-30 17:05:39 +0200237/* NFA regexp \1 .. \9 encountered. */
238static int nfa_has_backref;
239
Bram Moolenaar963fee22013-05-26 21:47:28 +0200240/* Number of sub expressions actually being used during execution. 1 if only
241 * the whole match (subexpr 0) is used. */
242static int nfa_nsubexpr;
243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static int *post_start; /* holds the postfix form of r.e. */
245static int *post_end;
246static int *post_ptr;
247
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200248static int nstate; /* Number of states in the NFA. Also used when
249 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200251
252
253static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
254static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
255static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200256static int nfa_regatom __ARGS((void));
257static int nfa_regpiece __ARGS((void));
258static int nfa_regconcat __ARGS((void));
259static int nfa_regbranch __ARGS((void));
260static int nfa_reg __ARGS((int paren));
261#ifdef DEBUG
262static void nfa_set_code __ARGS((int c));
263static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200264static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
265static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266static void nfa_dump __ARGS((nfa_regprog_T *prog));
267#endif
268static int *re2post __ARGS((void));
269static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
270static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
271static int check_char_class __ARGS((int class, int c));
272static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200273static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
274static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
276static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200277static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200278static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
280static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
281static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
282static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
283
284/* helper functions used when doing re2post() ... regatom() parsing */
285#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200286 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287 return FAIL; \
288 *post_ptr++ = c; \
289 } while (0)
290
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200291/*
292 * Initialize internal variables before NFA compilation.
293 * Return OK on success, FAIL otherwise.
294 */
295 static int
296nfa_regcomp_start(expr, re_flags)
297 char_u *expr;
298 int re_flags; /* see vim_regcomp() */
299{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200300 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200301 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302
303 nstate = 0;
304 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200305 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200306 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200308 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200309 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200310 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200311
312 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200313 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200314
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200315 post_start = (int *)lalloc(postfix_size, TRUE);
316 if (post_start == NULL)
317 return FAIL;
318 vim_memset(post_start, 0, postfix_size);
319 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200320 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200321 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200322 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200323
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200324 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200325 regcomp_start(expr, re_flags);
326
327 return OK;
328}
329
330/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200331 * Allocate more space for post_start. Called when
332 * running above the estimated number of states.
333 */
334 static int
335realloc_post_list()
336{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200337 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200338 int new_max = nstate_max + 1000;
339 int *new_start;
340 int *old_start;
341
342 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
343 if (new_start == NULL)
344 return FAIL;
345 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
346 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
347 old_start = post_start;
348 post_start = new_start;
349 post_ptr = new_start + (post_ptr - old_start);
350 post_end = post_start + new_max;
351 vim_free(old_start);
352 return OK;
353}
354
355/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200356 * Search between "start" and "end" and try to recognize a
357 * character class in expanded form. For example [0-9].
358 * On success, return the id the character class to be emitted.
359 * On failure, return 0 (=FAIL)
360 * Start points to the first char of the range, while end should point
361 * to the closing brace.
362 */
363 static int
364nfa_recognize_char_class(start, end, extra_newl)
365 char_u *start;
366 char_u *end;
367 int extra_newl;
368{
369 int i;
370 /* Each of these variables takes up a char in "config[]",
371 * in the order they are here. */
372 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
373 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
374 char_u *p;
375#define NCONFIGS 16
376 int classid[NCONFIGS] = {
377 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
378 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
379 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
380 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
381 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200382 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200383 char_u config[NCONFIGS][9] = {
384 "000000100", /* digit */
385 "100000100", /* non digit */
386 "011000100", /* hex-digit */
387 "111000100", /* non hex-digit */
388 "000001000", /* octal-digit */
389 "100001000", /* [^0-7] */
390 "000110110", /* [0-9A-Za-z_] */
391 "100110110", /* [^0-9A-Za-z_] */
392 "000110010", /* head of word */
393 "100110010", /* not head of word */
394 "000110000", /* alphabetic char a-z */
395 "100110000", /* non alphabetic char */
396 "000100000", /* lowercase letter */
397 "100100000", /* non lowercase */
398 "000010000", /* uppercase */
399 "100010000" /* non uppercase */
400 };
401
402 if (extra_newl == TRUE)
403 newl = TRUE;
404
405 if (*end != ']')
406 return FAIL;
407 p = start;
408 if (*p == '^')
409 {
410 not = TRUE;
411 p ++;
412 }
413
414 while (p < end)
415 {
416 if (p + 2 < end && *(p + 1) == '-')
417 {
418 switch (*p)
419 {
420 case '0':
421 if (*(p + 2) == '9')
422 {
423 o9 = TRUE;
424 break;
425 }
426 else
427 if (*(p + 2) == '7')
428 {
429 o7 = TRUE;
430 break;
431 }
432 case 'a':
433 if (*(p + 2) == 'z')
434 {
435 az = TRUE;
436 break;
437 }
438 else
439 if (*(p + 2) == 'f')
440 {
441 af = TRUE;
442 break;
443 }
444 case 'A':
445 if (*(p + 2) == 'Z')
446 {
447 AZ = TRUE;
448 break;
449 }
450 else
451 if (*(p + 2) == 'F')
452 {
453 AF = TRUE;
454 break;
455 }
456 /* FALLTHROUGH */
457 default:
458 return FAIL;
459 }
460 p += 3;
461 }
462 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
463 {
464 newl = TRUE;
465 p += 2;
466 }
467 else if (*p == '_')
468 {
469 underscore = TRUE;
470 p ++;
471 }
472 else if (*p == '\n')
473 {
474 newl = TRUE;
475 p ++;
476 }
477 else
478 return FAIL;
479 } /* while (p < end) */
480
481 if (p != end)
482 return FAIL;
483
484 /* build the config that represents the ranges we gathered */
485 STRCPY(myconfig, "000000000");
486 if (not == TRUE)
487 myconfig[0] = '1';
488 if (af == TRUE)
489 myconfig[1] = '1';
490 if (AF == TRUE)
491 myconfig[2] = '1';
492 if (az == TRUE)
493 myconfig[3] = '1';
494 if (AZ == TRUE)
495 myconfig[4] = '1';
496 if (o7 == TRUE)
497 myconfig[5] = '1';
498 if (o9 == TRUE)
499 myconfig[6] = '1';
500 if (underscore == TRUE)
501 myconfig[7] = '1';
502 if (newl == TRUE)
503 {
504 myconfig[8] = '1';
505 extra_newl = ADD_NL;
506 }
507 /* try to recognize character classes */
508 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200509 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200510 return classid[i] + extra_newl;
511
512 /* fallthrough => no success so far */
513 return FAIL;
514
515#undef NCONFIGS
516}
517
518/*
519 * Produce the bytes for equivalence class "c".
520 * Currently only handles latin1, latin9 and utf-8.
521 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
522 * equivalent to 'a OR b OR c'
523 *
524 * NOTE! When changing this function, also update reg_equi_class()
525 */
526 static int
527nfa_emit_equi_class(c, neg)
528 int c;
529 int neg;
530{
531 int first = TRUE;
532 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
533#define EMIT2(c) \
534 EMIT(c); \
535 if (neg == TRUE) { \
536 EMIT(NFA_NOT); \
537 } \
538 if (first == FALSE) \
539 EMIT(glue); \
540 else \
541 first = FALSE; \
542
543#ifdef FEAT_MBYTE
544 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
545 || STRCMP(p_enc, "iso-8859-15") == 0)
546#endif
547 {
548 switch (c)
549 {
550 case 'A': case '\300': case '\301': case '\302':
551 case '\303': case '\304': case '\305':
552 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
553 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
554 EMIT2('\305');
555 return OK;
556
557 case 'C': case '\307':
558 EMIT2('C'); EMIT2('\307');
559 return OK;
560
561 case 'E': case '\310': case '\311': case '\312': case '\313':
562 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
563 EMIT2('\312'); EMIT2('\313');
564 return OK;
565
566 case 'I': case '\314': case '\315': case '\316': case '\317':
567 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
568 EMIT2('\316'); EMIT2('\317');
569 return OK;
570
571 case 'N': case '\321':
572 EMIT2('N'); EMIT2('\321');
573 return OK;
574
575 case 'O': case '\322': case '\323': case '\324': case '\325':
576 case '\326':
577 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
578 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
579 return OK;
580
581 case 'U': case '\331': case '\332': case '\333': case '\334':
582 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
583 EMIT2('\333'); EMIT2('\334');
584 return OK;
585
586 case 'Y': case '\335':
587 EMIT2('Y'); EMIT2('\335');
588 return OK;
589
590 case 'a': case '\340': case '\341': case '\342':
591 case '\343': case '\344': case '\345':
592 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
593 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
594 EMIT2('\345');
595 return OK;
596
597 case 'c': case '\347':
598 EMIT2('c'); EMIT2('\347');
599 return OK;
600
601 case 'e': case '\350': case '\351': case '\352': case '\353':
602 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
603 EMIT2('\352'); EMIT2('\353');
604 return OK;
605
606 case 'i': case '\354': case '\355': case '\356': case '\357':
607 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
608 EMIT2('\356'); EMIT2('\357');
609 return OK;
610
611 case 'n': case '\361':
612 EMIT2('n'); EMIT2('\361');
613 return OK;
614
615 case 'o': case '\362': case '\363': case '\364': case '\365':
616 case '\366':
617 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
618 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
619 return OK;
620
621 case 'u': case '\371': case '\372': case '\373': case '\374':
622 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
623 EMIT2('\373'); EMIT2('\374');
624 return OK;
625
626 case 'y': case '\375': case '\377':
627 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
628 return OK;
629
630 default:
631 return FAIL;
632 }
633 }
634
635 EMIT(c);
636 return OK;
637#undef EMIT2
638}
639
640/*
641 * Code to parse regular expression.
642 *
643 * We try to reuse parsing functions in regexp.c to
644 * minimize surprise and keep the syntax consistent.
645 */
646
647/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200648 * Parse the lowest level.
649 *
650 * An atom can be one of a long list of items. Many atoms match one character
651 * in the text. It is often an ordinary character or a character class.
652 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
653 * is only for syntax highlighting.
654 *
655 * atom ::= ordinary-atom
656 * or \( pattern \)
657 * or \%( pattern \)
658 * or \z( pattern \)
659 */
660 static int
661nfa_regatom()
662{
663 int c;
664 int charclass;
665 int equiclass;
666 int collclass;
667 int got_coll_char;
668 char_u *p;
669 char_u *endp;
670#ifdef FEAT_MBYTE
671 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200672#endif
673 int extra = 0;
674 int first;
675 int emit_range;
676 int negated;
677 int result;
678 int startc = -1;
679 int endc = -1;
680 int oldstartc = -1;
681 int cpo_lit; /* 'cpoptions' contains 'l' flag */
682 int cpo_bsl; /* 'cpoptions' contains '\' flag */
683 int glue; /* ID that will "glue" nodes together */
684
685 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
686 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
687
688 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689 switch (c)
690 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200691 case NUL:
692 syntax_error = TRUE;
693 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
694
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695 case Magic('^'):
696 EMIT(NFA_BOL);
697 break;
698
699 case Magic('$'):
700 EMIT(NFA_EOL);
701#if defined(FEAT_SYN_HL) || defined(PROTO)
702 had_eol = TRUE;
703#endif
704 break;
705
706 case Magic('<'):
707 EMIT(NFA_BOW);
708 break;
709
710 case Magic('>'):
711 EMIT(NFA_EOW);
712 break;
713
714 case Magic('_'):
715 c = no_Magic(getchr());
716 if (c == '^') /* "\_^" is start-of-line */
717 {
718 EMIT(NFA_BOL);
719 break;
720 }
721 if (c == '$') /* "\_$" is end-of-line */
722 {
723 EMIT(NFA_EOL);
724#if defined(FEAT_SYN_HL) || defined(PROTO)
725 had_eol = TRUE;
726#endif
727 break;
728 }
729
730 extra = ADD_NL;
731
732 /* "\_[" is collection plus newline */
733 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200734 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735
736 /* "\_x" is character class plus newline */
737 /*FALLTHROUGH*/
738
739 /*
740 * Character classes.
741 */
742 case Magic('.'):
743 case Magic('i'):
744 case Magic('I'):
745 case Magic('k'):
746 case Magic('K'):
747 case Magic('f'):
748 case Magic('F'):
749 case Magic('p'):
750 case Magic('P'):
751 case Magic('s'):
752 case Magic('S'):
753 case Magic('d'):
754 case Magic('D'):
755 case Magic('x'):
756 case Magic('X'):
757 case Magic('o'):
758 case Magic('O'):
759 case Magic('w'):
760 case Magic('W'):
761 case Magic('h'):
762 case Magic('H'):
763 case Magic('a'):
764 case Magic('A'):
765 case Magic('l'):
766 case Magic('L'):
767 case Magic('u'):
768 case Magic('U'):
769 p = vim_strchr(classchars, no_Magic(c));
770 if (p == NULL)
771 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200772 EMSGN("INTERNAL: Unknown character class char: %ld", c);
773 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200774 }
775#ifdef FEAT_MBYTE
776 /* When '.' is followed by a composing char ignore the dot, so that
777 * the composing char is matched here. */
778 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
779 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200780 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200781 c = getchr();
782 goto nfa_do_multibyte;
783 }
784#endif
785 EMIT(nfa_classcodes[p - classchars]);
786 if (extra == ADD_NL)
787 {
788 EMIT(NFA_NEWL);
789 EMIT(NFA_OR);
790 regflags |= RF_HASNL;
791 }
792 break;
793
794 case Magic('n'):
795 if (reg_string)
796 /* In a string "\n" matches a newline character. */
797 EMIT(NL);
798 else
799 {
800 /* In buffer text "\n" matches the end of a line. */
801 EMIT(NFA_NEWL);
802 regflags |= RF_HASNL;
803 }
804 break;
805
806 case Magic('('):
807 if (nfa_reg(REG_PAREN) == FAIL)
808 return FAIL; /* cascaded error */
809 break;
810
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200811 case Magic('|'):
812 case Magic('&'):
813 case Magic(')'):
814 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200815 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 return FAIL;
817
818 case Magic('='):
819 case Magic('?'):
820 case Magic('+'):
821 case Magic('@'):
822 case Magic('*'):
823 case Magic('{'):
824 /* these should follow an atom, not form an atom */
825 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200826 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200827 return FAIL;
828
829 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200830 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200831 return FAIL;
832
Bram Moolenaar428e9872013-05-30 17:05:39 +0200833 case Magic('1'):
834 case Magic('2'):
835 case Magic('3'):
836 case Magic('4'):
837 case Magic('5'):
838 case Magic('6'):
839 case Magic('7'):
840 case Magic('8'):
841 case Magic('9'):
842 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
843 nfa_has_backref = TRUE;
844 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200845
846 case Magic('z'):
847 c = no_Magic(getchr());
848 switch (c)
849 {
850 case 's':
851 EMIT(NFA_ZSTART);
852 break;
853 case 'e':
854 EMIT(NFA_ZEND);
855 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200856 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200857#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200858 case '1':
859 case '2':
860 case '3':
861 case '4':
862 case '5':
863 case '6':
864 case '7':
865 case '8':
866 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200867 /* \z1...\z9 */
868 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
869 /* No need to set nfa_has_backref, the sub-matches don't
870 * change when \z1 .. \z9 maches or not. */
871 re_has_z = REX_USE;
872 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200873 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200874 /* \z( */
875 if (nfa_reg(REG_ZPAREN) == FAIL)
876 return FAIL; /* cascaded error */
877 re_has_z = REX_SET;
878 break;
879#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 default:
881 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200882 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200883 no_Magic(c));
884 return FAIL;
885 }
886 break;
887
888 case Magic('%'):
889 c = no_Magic(getchr());
890 switch (c)
891 {
892 /* () without a back reference */
893 case '(':
894 if (nfa_reg(REG_NPAREN) == FAIL)
895 return FAIL;
896 EMIT(NFA_NOPEN);
897 break;
898
899 case 'd': /* %d123 decimal */
900 case 'o': /* %o123 octal */
901 case 'x': /* %xab hex 2 */
902 case 'u': /* %uabcd hex 4 */
903 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200904 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200905 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906
Bram Moolenaar47196582013-05-25 22:04:23 +0200907 switch (c)
908 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200909 case 'd': nr = getdecchrs(); break;
910 case 'o': nr = getoctchrs(); break;
911 case 'x': nr = gethexchrs(2); break;
912 case 'u': nr = gethexchrs(4); break;
913 case 'U': nr = gethexchrs(8); break;
914 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200915 }
916
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200917 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200918 EMSG2_RET_FAIL(
919 _("E678: Invalid character after %s%%[dxouU]"),
920 reg_magic == MAGIC_ALL);
921 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200922 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200923 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200924 break;
925
926 /* Catch \%^ and \%$ regardless of where they appear in the
927 * pattern -- regardless of whether or not it makes sense. */
928 case '^':
929 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200930 break;
931
932 case '$':
933 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200934 break;
935
936 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200937 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200938 break;
939
940 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200941 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200942 return FAIL;
943 break;
944
945 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200946 /* TODO: \%[abc] not supported yet */
947 return FAIL;
948
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200949 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200950 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200951 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200952 int cmp = c;
953
954 if (c == '<' || c == '>')
955 c = getchr();
956 while (VIM_ISDIGIT(c))
957 {
958 n = n * 10 + (c - '0');
959 c = getchr();
960 }
961 if (c == 'l' || c == 'c' || c == 'v')
962 {
963 EMIT(n);
964 if (c == 'l')
965 EMIT(cmp == '<' ? NFA_LNUM_LT :
966 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
967 else if (c == 'c')
968 EMIT(cmp == '<' ? NFA_COL_LT :
969 cmp == '>' ? NFA_COL_GT : NFA_COL);
970 else
971 EMIT(cmp == '<' ? NFA_VCOL_LT :
972 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
973 break;
974 }
975 else if (c == '\'')
976 /* TODO: \%'m not supported yet */
977 return FAIL;
978 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200979 syntax_error = TRUE;
980 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
981 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200982 return FAIL;
983 }
984 break;
985
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200986 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200987collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200988 /*
989 * Glue is emitted between several atoms from the [].
990 * It is either NFA_OR, or NFA_CONCAT.
991 *
992 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
993 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
994 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
995 * notation)
996 *
997 */
998
999
1000/* Emit negation atoms, if needed.
1001 * The CONCAT below merges the NOT with the previous node. */
1002#define TRY_NEG() \
1003 if (negated == TRUE) \
1004 { \
1005 EMIT(NFA_NOT); \
1006 }
1007
1008/* Emit glue between important nodes : CONCAT or OR. */
1009#define EMIT_GLUE() \
1010 if (first == FALSE) \
1011 EMIT(glue); \
1012 else \
1013 first = FALSE;
1014
1015 p = regparse;
1016 endp = skip_anyof(p);
1017 if (*endp == ']')
1018 {
1019 /*
1020 * Try to reverse engineer character classes. For example,
1021 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1022 * and perform the necessary substitutions in the NFA.
1023 */
1024 result = nfa_recognize_char_class(regparse, endp,
1025 extra == ADD_NL);
1026 if (result != FAIL)
1027 {
1028 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1029 EMIT(result);
1030 else /* must be char class + newline */
1031 {
1032 EMIT(result - ADD_NL);
1033 EMIT(NFA_NEWL);
1034 EMIT(NFA_OR);
1035 }
1036 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001037 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001038 return OK;
1039 }
1040 /*
1041 * Failed to recognize a character class. Use the simple
1042 * version that turns [abc] into 'a' OR 'b' OR 'c'
1043 */
1044 startc = endc = oldstartc = -1;
1045 first = TRUE; /* Emitting first atom in this sequence? */
1046 negated = FALSE;
1047 glue = NFA_OR;
1048 if (*regparse == '^') /* negated range */
1049 {
1050 negated = TRUE;
1051 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001052 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001053 }
1054 if (*regparse == '-')
1055 {
1056 startc = '-';
1057 EMIT(startc);
1058 TRY_NEG();
1059 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001060 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001061 }
1062 /* Emit the OR branches for each character in the [] */
1063 emit_range = FALSE;
1064 while (regparse < endp)
1065 {
1066 oldstartc = startc;
1067 startc = -1;
1068 got_coll_char = FALSE;
1069 if (*regparse == '[')
1070 {
1071 /* Check for [: :], [= =], [. .] */
1072 equiclass = collclass = 0;
1073 charclass = get_char_class(&regparse);
1074 if (charclass == CLASS_NONE)
1075 {
1076 equiclass = get_equi_class(&regparse);
1077 if (equiclass == 0)
1078 collclass = get_coll_element(&regparse);
1079 }
1080
1081 /* Character class like [:alpha:] */
1082 if (charclass != CLASS_NONE)
1083 {
1084 switch (charclass)
1085 {
1086 case CLASS_ALNUM:
1087 EMIT(NFA_CLASS_ALNUM);
1088 break;
1089 case CLASS_ALPHA:
1090 EMIT(NFA_CLASS_ALPHA);
1091 break;
1092 case CLASS_BLANK:
1093 EMIT(NFA_CLASS_BLANK);
1094 break;
1095 case CLASS_CNTRL:
1096 EMIT(NFA_CLASS_CNTRL);
1097 break;
1098 case CLASS_DIGIT:
1099 EMIT(NFA_CLASS_DIGIT);
1100 break;
1101 case CLASS_GRAPH:
1102 EMIT(NFA_CLASS_GRAPH);
1103 break;
1104 case CLASS_LOWER:
1105 EMIT(NFA_CLASS_LOWER);
1106 break;
1107 case CLASS_PRINT:
1108 EMIT(NFA_CLASS_PRINT);
1109 break;
1110 case CLASS_PUNCT:
1111 EMIT(NFA_CLASS_PUNCT);
1112 break;
1113 case CLASS_SPACE:
1114 EMIT(NFA_CLASS_SPACE);
1115 break;
1116 case CLASS_UPPER:
1117 EMIT(NFA_CLASS_UPPER);
1118 break;
1119 case CLASS_XDIGIT:
1120 EMIT(NFA_CLASS_XDIGIT);
1121 break;
1122 case CLASS_TAB:
1123 EMIT(NFA_CLASS_TAB);
1124 break;
1125 case CLASS_RETURN:
1126 EMIT(NFA_CLASS_RETURN);
1127 break;
1128 case CLASS_BACKSPACE:
1129 EMIT(NFA_CLASS_BACKSPACE);
1130 break;
1131 case CLASS_ESCAPE:
1132 EMIT(NFA_CLASS_ESCAPE);
1133 break;
1134 }
1135 TRY_NEG();
1136 EMIT_GLUE();
1137 continue;
1138 }
1139 /* Try equivalence class [=a=] and the like */
1140 if (equiclass != 0)
1141 {
1142 result = nfa_emit_equi_class(equiclass, negated);
1143 if (result == FAIL)
1144 {
1145 /* should never happen */
1146 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1147 }
1148 EMIT_GLUE();
1149 continue;
1150 }
1151 /* Try collating class like [. .] */
1152 if (collclass != 0)
1153 {
1154 startc = collclass; /* allow [.a.]-x as a range */
1155 /* Will emit the proper atom at the end of the
1156 * while loop. */
1157 }
1158 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001159 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1160 * start character. */
1161 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001162 {
1163 emit_range = TRUE;
1164 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001165 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001166 continue; /* reading the end of the range */
1167 }
1168
1169 /* Now handle simple and escaped characters.
1170 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1171 * accepts "\t", "\e", etc., but only when the 'l' flag in
1172 * 'cpoptions' is not included.
1173 * Posix doesn't recognize backslash at all.
1174 */
1175 if (*regparse == '\\'
1176 && !cpo_bsl
1177 && regparse + 1 <= endp
1178 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1179 || (!cpo_lit
1180 && vim_strchr(REGEXP_ABBR, regparse[1])
1181 != NULL)
1182 )
1183 )
1184 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001185 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001186
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001187 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001188 startc = reg_string ? NL : NFA_NEWL;
1189 else
1190 if (*regparse == 'd'
1191 || *regparse == 'o'
1192 || *regparse == 'x'
1193 || *regparse == 'u'
1194 || *regparse == 'U'
1195 )
1196 {
1197 /* TODO(RE) This needs more testing */
1198 startc = coll_get_char();
1199 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001200 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 }
1202 else
1203 {
1204 /* \r,\t,\e,\b */
1205 startc = backslash_trans(*regparse);
1206 }
1207 }
1208
1209 /* Normal printable char */
1210 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001211 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212
1213 /* Previous char was '-', so this char is end of range. */
1214 if (emit_range)
1215 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001216 endc = startc;
1217 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001218 if (startc > endc)
1219 EMSG_RET_FAIL(_(e_invrange));
1220#ifdef FEAT_MBYTE
1221 if (has_mbyte && ((*mb_char2len)(startc) > 1
1222 || (*mb_char2len)(endc) > 1))
1223 {
1224 if (endc > startc + 256)
1225 EMSG_RET_FAIL(_(e_invrange));
1226 /* Emit the range. "startc" was already emitted, so
1227 * skip it. */
1228 for (c = startc + 1; c <= endc; c++)
1229 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001230 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001231 TRY_NEG();
1232 EMIT_GLUE();
1233 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 }
1235 else
1236#endif
1237 {
1238#ifdef EBCDIC
1239 int alpha_only = FALSE;
1240
1241 /* for alphabetical range skip the gaps
1242 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1243 if (isalpha(startc) && isalpha(endc))
1244 alpha_only = TRUE;
1245#endif
1246 /* Emit the range. "startc" was already emitted, so
1247 * skip it. */
1248 for (c = startc + 1; c <= endc; c++)
1249#ifdef EBCDIC
1250 if (!alpha_only || isalpha(startc))
1251#endif
1252 {
1253 EMIT(c);
1254 TRY_NEG();
1255 EMIT_GLUE();
1256 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001257 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001258 emit_range = FALSE;
1259 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260 }
1261 else
1262 {
1263 /*
1264 * This char (startc) is not part of a range. Just
1265 * emit it.
1266 *
1267 * Normally, simply emit startc. But if we get char
1268 * code=0 from a collating char, then replace it with
1269 * 0x0a.
1270 *
1271 * This is needed to completely mimic the behaviour of
1272 * the backtracking engine.
1273 */
1274 if (got_coll_char == TRUE && startc == 0)
1275 EMIT(0x0a);
1276 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001277 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001278 TRY_NEG();
1279 EMIT_GLUE();
1280 }
1281
Bram Moolenaar51a29832013-05-28 22:30:35 +02001282 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001283 } /* while (p < endp) */
1284
Bram Moolenaar51a29832013-05-28 22:30:35 +02001285 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001286 if (*regparse == '-') /* if last, '-' is just a char */
1287 {
1288 EMIT('-');
1289 TRY_NEG();
1290 EMIT_GLUE();
1291 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001292 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001294 /* skip the trailing ] */
1295 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001296 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001297 if (negated == TRUE)
1298 {
1299 /* Mark end of negated char range */
1300 EMIT(NFA_END_NEG_RANGE);
1301 EMIT(NFA_CONCAT);
1302 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001303
1304 /* \_[] also matches \n but it's not negated */
1305 if (extra == ADD_NL)
1306 {
1307 EMIT(reg_string ? NL : NFA_NEWL);
1308 EMIT(NFA_OR);
1309 }
1310
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001311 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001312 } /* if exists closing ] */
1313
1314 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 {
1316 syntax_error = TRUE;
1317 EMSG_RET_FAIL(_(e_missingbracket));
1318 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001319 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321 default:
1322 {
1323#ifdef FEAT_MBYTE
1324 int plen;
1325
1326nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001327 /* plen is length of current char with composing chars */
1328 if (enc_utf8 && ((*mb_char2len)(c)
1329 != (plen = (*mb_ptr2len)(old_regparse))
1330 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001332 int i = 0;
1333
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001334 /* A base character plus composing characters, or just one
1335 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001336 * This requires creating a separate atom as if enclosing
1337 * the characters in (), where NFA_COMPOSING is the ( and
1338 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001339 * building the postfix form, not the NFA itself;
1340 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001341 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001342 for (;;)
1343 {
1344 EMIT(c);
1345 if (i > 0)
1346 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001347 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001348 break;
1349 c = utf_ptr2char(old_regparse + i);
1350 }
1351 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001352 regparse = old_regparse + plen;
1353 }
1354 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355#endif
1356 {
1357 c = no_Magic(c);
1358 EMIT(c);
1359 }
1360 return OK;
1361 }
1362 }
1363
1364#undef TRY_NEG
1365#undef EMIT_GLUE
1366
1367 return OK;
1368}
1369
1370/*
1371 * Parse something followed by possible [*+=].
1372 *
1373 * A piece is an atom, possibly followed by a multi, an indication of how many
1374 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1375 * characters: "", "a", "aa", etc.
1376 *
1377 * piece ::= atom
1378 * or atom multi
1379 */
1380 static int
1381nfa_regpiece()
1382{
1383 int i;
1384 int op;
1385 int ret;
1386 long minval, maxval;
1387 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001388 parse_state_T old_state;
1389 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001391 int old_post_pos;
1392 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001393 int quest;
1394
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001395 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1396 * next. */
1397 save_parse_state(&old_state);
1398
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001400 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001401
1402 ret = nfa_regatom();
1403 if (ret == FAIL)
1404 return FAIL; /* cascaded error */
1405
1406 op = peekchr();
1407 if (re_multi_type(op) == NOT_MULTI)
1408 return OK;
1409
1410 skipchr();
1411 switch (op)
1412 {
1413 case Magic('*'):
1414 EMIT(NFA_STAR);
1415 break;
1416
1417 case Magic('+'):
1418 /*
1419 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1420 * first and only submatch would be "aaa". But the backtracking
1421 * engine interprets the plus as "try matching one more time", and
1422 * a* matches a second time at the end of the input, the empty
1423 * string.
1424 * The submatch will the empty string.
1425 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001426 * In order to be consistent with the old engine, we replace
1427 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001428 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001429 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001430 curchr = -1;
1431 if (nfa_regatom() == FAIL)
1432 return FAIL;
1433 EMIT(NFA_STAR);
1434 EMIT(NFA_CONCAT);
1435 skipchr(); /* skip the \+ */
1436 break;
1437
1438 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001439 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001441 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 switch(op)
1443 {
1444 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001445 /* \@= */
1446 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001448 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001449 /* \@! */
1450 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001451 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001453 op = no_Magic(getchr());
1454 if (op == '=')
1455 /* \@<= */
1456 i = NFA_PREV_ATOM_JUST_BEFORE;
1457 else if (op == '!')
1458 /* \@<! */
1459 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1460 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001462 /* \@> Not supported yet */
1463 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001464 return FAIL;
1465 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001466 if (i == 0)
1467 {
1468 syntax_error = TRUE;
1469 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1470 return FAIL;
1471 }
1472 EMIT(i);
1473 if (i == NFA_PREV_ATOM_JUST_BEFORE
1474 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1475 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001476 break;
1477
1478 case Magic('?'):
1479 case Magic('='):
1480 EMIT(NFA_QUEST);
1481 break;
1482
1483 case Magic('{'):
1484 /* a{2,5} will expand to 'aaa?a?a?'
1485 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1486 * version of '?'
1487 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1488 * parenthesis have the same id
1489 */
1490
1491 greedy = TRUE;
1492 c2 = peekchr();
1493 if (c2 == '-' || c2 == Magic('-'))
1494 {
1495 skipchr();
1496 greedy = FALSE;
1497 }
1498 if (!read_limits(&minval, &maxval))
1499 {
1500 syntax_error = TRUE;
1501 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1502 }
1503 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1504 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001505 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001506 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001507 if (greedy)
1508 /* \{}, \{0,} */
1509 EMIT(NFA_STAR);
1510 else
1511 /* \{-}, \{-0,} */
1512 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001513 break;
1514 }
1515
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516 /* Special case: x{0} or x{-0} */
1517 if (maxval == 0)
1518 {
1519 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001520 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1522 EMIT(NFA_SKIP_CHAR);
1523 return OK;
1524 }
1525
1526 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001527 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001528 /* Save parse state after the repeated atom and the \{} */
1529 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001531 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1532 for (i = 0; i < maxval; i++)
1533 {
1534 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001535 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001536 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 if (nfa_regatom() == FAIL)
1538 return FAIL;
1539 /* after "minval" times, atoms are optional */
1540 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001541 {
1542 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001543 {
1544 if (greedy)
1545 EMIT(NFA_STAR);
1546 else
1547 EMIT(NFA_STAR_NONGREEDY);
1548 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001549 else
1550 EMIT(quest);
1551 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001552 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001554 if (i + 1 > minval && maxval == MAX_LIMIT)
1555 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001556 }
1557
1558 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001559 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001560 curchr = -1;
1561
1562 break;
1563
1564
1565 default:
1566 break;
1567 } /* end switch */
1568
1569 if (re_multi_type(peekchr()) != NOT_MULTI)
1570 {
1571 /* Can't have a multi follow a multi. */
1572 syntax_error = TRUE;
1573 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1574 }
1575
1576 return OK;
1577}
1578
1579/*
1580 * Parse one or more pieces, concatenated. It matches a match for the
1581 * first piece, followed by a match for the second piece, etc. Example:
1582 * "f[0-9]b", first matches "f", then a digit and then "b".
1583 *
1584 * concat ::= piece
1585 * or piece piece
1586 * or piece piece piece
1587 * etc.
1588 */
1589 static int
1590nfa_regconcat()
1591{
1592 int cont = TRUE;
1593 int first = TRUE;
1594
1595 while (cont)
1596 {
1597 switch (peekchr())
1598 {
1599 case NUL:
1600 case Magic('|'):
1601 case Magic('&'):
1602 case Magic(')'):
1603 cont = FALSE;
1604 break;
1605
1606 case Magic('Z'):
1607#ifdef FEAT_MBYTE
1608 regflags |= RF_ICOMBINE;
1609#endif
1610 skipchr_keepstart();
1611 break;
1612 case Magic('c'):
1613 regflags |= RF_ICASE;
1614 skipchr_keepstart();
1615 break;
1616 case Magic('C'):
1617 regflags |= RF_NOICASE;
1618 skipchr_keepstart();
1619 break;
1620 case Magic('v'):
1621 reg_magic = MAGIC_ALL;
1622 skipchr_keepstart();
1623 curchr = -1;
1624 break;
1625 case Magic('m'):
1626 reg_magic = MAGIC_ON;
1627 skipchr_keepstart();
1628 curchr = -1;
1629 break;
1630 case Magic('M'):
1631 reg_magic = MAGIC_OFF;
1632 skipchr_keepstart();
1633 curchr = -1;
1634 break;
1635 case Magic('V'):
1636 reg_magic = MAGIC_NONE;
1637 skipchr_keepstart();
1638 curchr = -1;
1639 break;
1640
1641 default:
1642 if (nfa_regpiece() == FAIL)
1643 return FAIL;
1644 if (first == FALSE)
1645 EMIT(NFA_CONCAT);
1646 else
1647 first = FALSE;
1648 break;
1649 }
1650 }
1651
1652 return OK;
1653}
1654
1655/*
1656 * Parse a branch, one or more concats, separated by "\&". It matches the
1657 * last concat, but only if all the preceding concats also match at the same
1658 * position. Examples:
1659 * "foobeep\&..." matches "foo" in "foobeep".
1660 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1661 *
1662 * branch ::= concat
1663 * or concat \& concat
1664 * or concat \& concat \& concat
1665 * etc.
1666 */
1667 static int
1668nfa_regbranch()
1669{
1670 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001671 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001672
Bram Moolenaar16299b52013-05-30 18:45:23 +02001673 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001674
1675 /* First branch, possibly the only one */
1676 if (nfa_regconcat() == FAIL)
1677 return FAIL;
1678
1679 ch = peekchr();
1680 /* Try next concats */
1681 while (ch == Magic('&'))
1682 {
1683 skipchr();
1684 EMIT(NFA_NOPEN);
1685 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001686 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001687 if (nfa_regconcat() == FAIL)
1688 return FAIL;
1689 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001690 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 EMIT(NFA_SKIP_CHAR);
1692 EMIT(NFA_CONCAT);
1693 ch = peekchr();
1694 }
1695
1696 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001697 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698 EMIT(NFA_SKIP_CHAR);
1699
1700 return OK;
1701}
1702
1703/*
1704 * Parse a pattern, one or more branches, separated by "\|". It matches
1705 * anything that matches one of the branches. Example: "foo\|beep" matches
1706 * "foo" and matches "beep". If more than one branch matches, the first one
1707 * is used.
1708 *
1709 * pattern ::= branch
1710 * or branch \| branch
1711 * or branch \| branch \| branch
1712 * etc.
1713 */
1714 static int
1715nfa_reg(paren)
1716 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1717{
1718 int parno = 0;
1719
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720 if (paren == REG_PAREN)
1721 {
1722 if (regnpar >= NSUBEXP) /* Too many `(' */
1723 {
1724 syntax_error = TRUE;
1725 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1726 }
1727 parno = regnpar++;
1728 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001729#ifdef FEAT_SYN_HL
1730 else if (paren == REG_ZPAREN)
1731 {
1732 /* Make a ZOPEN node. */
1733 if (regnzpar >= NSUBEXP)
1734 {
1735 syntax_error = TRUE;
1736 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1737 }
1738 parno = regnzpar++;
1739 }
1740#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001741
1742 if (nfa_regbranch() == FAIL)
1743 return FAIL; /* cascaded error */
1744
1745 while (peekchr() == Magic('|'))
1746 {
1747 skipchr();
1748 if (nfa_regbranch() == FAIL)
1749 return FAIL; /* cascaded error */
1750 EMIT(NFA_OR);
1751 }
1752
1753 /* Check for proper termination. */
1754 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1755 {
1756 syntax_error = TRUE;
1757 if (paren == REG_NPAREN)
1758 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1759 else
1760 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1761 }
1762 else if (paren == REG_NOPAREN && peekchr() != NUL)
1763 {
1764 syntax_error = TRUE;
1765 if (peekchr() == Magic(')'))
1766 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1767 else
1768 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1769 }
1770 /*
1771 * Here we set the flag allowing back references to this set of
1772 * parentheses.
1773 */
1774 if (paren == REG_PAREN)
1775 {
1776 had_endbrace[parno] = TRUE; /* have seen the close paren */
1777 EMIT(NFA_MOPEN + parno);
1778 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001779#ifdef FEAT_SYN_HL
1780 else if (paren == REG_ZPAREN)
1781 EMIT(NFA_ZOPEN + parno);
1782#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783
1784 return OK;
1785}
1786
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001787#ifdef DEBUG
1788static char_u code[50];
1789
1790 static void
1791nfa_set_code(c)
1792 int c;
1793{
1794 int addnl = FALSE;
1795
1796 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1797 {
1798 addnl = TRUE;
1799 c -= ADD_NL;
1800 }
1801
1802 STRCPY(code, "");
1803 switch (c)
1804 {
1805 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1806 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1807 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1808 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1809 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1810 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1811
Bram Moolenaar5714b802013-05-28 22:03:20 +02001812 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1813 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1814 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1815 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1816 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1817 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1818 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1819 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1820 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001821#ifdef FEAT_SYN_HL
1822 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1823 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1824 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1825 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1826 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1827 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1828 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1829 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1830 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1831#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001832 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1833
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834 case NFA_PREV_ATOM_NO_WIDTH:
1835 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001836 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1837 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001838 case NFA_PREV_ATOM_JUST_BEFORE:
1839 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1840 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1841 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001842 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1843 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001845 case NFA_START_INVISIBLE_BEFORE:
1846 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1848
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001849 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1850 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1851
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001852 case NFA_MOPEN:
1853 case NFA_MOPEN1:
1854 case NFA_MOPEN2:
1855 case NFA_MOPEN3:
1856 case NFA_MOPEN4:
1857 case NFA_MOPEN5:
1858 case NFA_MOPEN6:
1859 case NFA_MOPEN7:
1860 case NFA_MOPEN8:
1861 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862 STRCPY(code, "NFA_MOPEN(x)");
1863 code[10] = c - NFA_MOPEN + '0';
1864 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001865 case NFA_MCLOSE:
1866 case NFA_MCLOSE1:
1867 case NFA_MCLOSE2:
1868 case NFA_MCLOSE3:
1869 case NFA_MCLOSE4:
1870 case NFA_MCLOSE5:
1871 case NFA_MCLOSE6:
1872 case NFA_MCLOSE7:
1873 case NFA_MCLOSE8:
1874 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001875 STRCPY(code, "NFA_MCLOSE(x)");
1876 code[11] = c - NFA_MCLOSE + '0';
1877 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001878#ifdef FEAT_SYN_HL
1879 case NFA_ZOPEN:
1880 case NFA_ZOPEN1:
1881 case NFA_ZOPEN2:
1882 case NFA_ZOPEN3:
1883 case NFA_ZOPEN4:
1884 case NFA_ZOPEN5:
1885 case NFA_ZOPEN6:
1886 case NFA_ZOPEN7:
1887 case NFA_ZOPEN8:
1888 case NFA_ZOPEN9:
1889 STRCPY(code, "NFA_ZOPEN(x)");
1890 code[10] = c - NFA_ZOPEN + '0';
1891 break;
1892 case NFA_ZCLOSE:
1893 case NFA_ZCLOSE1:
1894 case NFA_ZCLOSE2:
1895 case NFA_ZCLOSE3:
1896 case NFA_ZCLOSE4:
1897 case NFA_ZCLOSE5:
1898 case NFA_ZCLOSE6:
1899 case NFA_ZCLOSE7:
1900 case NFA_ZCLOSE8:
1901 case NFA_ZCLOSE9:
1902 STRCPY(code, "NFA_ZCLOSE(x)");
1903 code[11] = c - NFA_ZCLOSE + '0';
1904 break;
1905#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1907 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1908 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1909 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001910 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1911 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001913 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1914 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1915 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001916 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1917 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1918 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1920 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1921 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1922 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1923 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1924 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1925 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1926 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1927 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1928 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1929 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1930 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1931 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1932 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1933 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1934 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1935 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1936
1937 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1938 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1939 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1940 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1941 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1942 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1943 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1944 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1945 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1946 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1947 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1948 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1949 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1950 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1951 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1952 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1953 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1954 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1955 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1956 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1957 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1958 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1959 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1960 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1961 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1962 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1963 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1964
1965 default:
1966 STRCPY(code, "CHAR(x)");
1967 code[5] = c;
1968 }
1969
1970 if (addnl == TRUE)
1971 STRCAT(code, " + NEWLINE ");
1972
1973}
1974
1975#ifdef ENABLE_LOG
1976static FILE *log_fd;
1977
1978/*
1979 * Print the postfix notation of the current regexp.
1980 */
1981 static void
1982nfa_postfix_dump(expr, retval)
1983 char_u *expr;
1984 int retval;
1985{
1986 int *p;
1987 FILE *f;
1988
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001989 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001990 if (f != NULL)
1991 {
1992 fprintf(f, "\n-------------------------\n");
1993 if (retval == FAIL)
1994 fprintf(f, ">>> NFA engine failed ... \n");
1995 else if (retval == OK)
1996 fprintf(f, ">>> NFA engine succeeded !\n");
1997 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001998 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999 {
2000 nfa_set_code(*p);
2001 fprintf(f, "%s, ", code);
2002 }
2003 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002004 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002005 fprintf(f, "%d ", *p);
2006 fprintf(f, "\n\n");
2007 fclose(f);
2008 }
2009}
2010
2011/*
2012 * Print the NFA starting with a root node "state".
2013 */
2014 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002015nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002016 FILE *debugf;
2017 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002019 garray_T indent;
2020
2021 ga_init2(&indent, 1, 64);
2022 ga_append(&indent, '\0');
2023 nfa_print_state2(debugf, state, &indent);
2024 ga_clear(&indent);
2025}
2026
2027 static void
2028nfa_print_state2(debugf, state, indent)
2029 FILE *debugf;
2030 nfa_state_T *state;
2031 garray_T *indent;
2032{
2033 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034
2035 if (state == NULL)
2036 return;
2037
2038 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002039
2040 /* Output indent */
2041 p = (char_u *)indent->ga_data;
2042 if (indent->ga_len >= 3)
2043 {
2044 int last = indent->ga_len - 3;
2045 char_u save[2];
2046
2047 STRNCPY(save, &p[last], 2);
2048 STRNCPY(&p[last], "+-", 2);
2049 fprintf(debugf, " %s", p);
2050 STRNCPY(&p[last], save, 2);
2051 }
2052 else
2053 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054
2055 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002056 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2057 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 if (state->id < 0)
2059 return;
2060
2061 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002062
2063 /* grow indent for state->out */
2064 indent->ga_len -= 1;
2065 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002066 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002067 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002068 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002069 ga_append(indent, '\0');
2070
2071 nfa_print_state2(debugf, state->out, indent);
2072
2073 /* replace last part of indent for state->out1 */
2074 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002075 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002076 ga_append(indent, '\0');
2077
2078 nfa_print_state2(debugf, state->out1, indent);
2079
2080 /* shrink indent */
2081 indent->ga_len -= 3;
2082 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083}
2084
2085/*
2086 * Print the NFA state machine.
2087 */
2088 static void
2089nfa_dump(prog)
2090 nfa_regprog_T *prog;
2091{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002092 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002093
2094 if (debugf != NULL)
2095 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002096 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002097 fclose(debugf);
2098 }
2099}
2100#endif /* ENABLE_LOG */
2101#endif /* DEBUG */
2102
2103/*
2104 * Parse r.e. @expr and convert it into postfix form.
2105 * Return the postfix string on success, NULL otherwise.
2106 */
2107 static int *
2108re2post()
2109{
2110 if (nfa_reg(REG_NOPAREN) == FAIL)
2111 return NULL;
2112 EMIT(NFA_MOPEN);
2113 return post_start;
2114}
2115
2116/* NB. Some of the code below is inspired by Russ's. */
2117
2118/*
2119 * Represents an NFA state plus zero or one or two arrows exiting.
2120 * if c == MATCH, no arrows out; matching state.
2121 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2122 * If c < 256, labeled arrow with character c to out.
2123 */
2124
2125static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2126
2127/*
2128 * Allocate and initialize nfa_state_T.
2129 */
2130 static nfa_state_T *
2131new_state(c, out, out1)
2132 int c;
2133 nfa_state_T *out;
2134 nfa_state_T *out1;
2135{
2136 nfa_state_T *s;
2137
2138 if (istate >= nstate)
2139 return NULL;
2140
2141 s = &state_ptr[istate++];
2142
2143 s->c = c;
2144 s->out = out;
2145 s->out1 = out1;
2146
2147 s->id = istate;
2148 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002149 s->negated = FALSE;
2150
2151 return s;
2152}
2153
2154/*
2155 * A partially built NFA without the matching state filled in.
2156 * Frag_T.start points at the start state.
2157 * Frag_T.out is a list of places that need to be set to the
2158 * next state for this fragment.
2159 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002160
2161/* Since the out pointers in the list are always
2162 * uninitialized, we use the pointers themselves
2163 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002165union Ptrlist
2166{
2167 Ptrlist *next;
2168 nfa_state_T *s;
2169};
2170
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002171struct Frag
2172{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002173 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002174 Ptrlist *out;
2175};
2176typedef struct Frag Frag_T;
2177
2178static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2179static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2180static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2181static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2182static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2183static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2184
2185/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002186 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187 */
2188 static Frag_T
2189frag(start, out)
2190 nfa_state_T *start;
2191 Ptrlist *out;
2192{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002193 Frag_T n;
2194
2195 n.start = start;
2196 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002197 return n;
2198}
2199
2200/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 * Create singleton list containing just outp.
2202 */
2203 static Ptrlist *
2204list1(outp)
2205 nfa_state_T **outp;
2206{
2207 Ptrlist *l;
2208
2209 l = (Ptrlist *)outp;
2210 l->next = NULL;
2211 return l;
2212}
2213
2214/*
2215 * Patch the list of states at out to point to start.
2216 */
2217 static void
2218patch(l, s)
2219 Ptrlist *l;
2220 nfa_state_T *s;
2221{
2222 Ptrlist *next;
2223
2224 for (; l; l = next)
2225 {
2226 next = l->next;
2227 l->s = s;
2228 }
2229}
2230
2231
2232/*
2233 * Join the two lists l1 and l2, returning the combination.
2234 */
2235 static Ptrlist *
2236append(l1, l2)
2237 Ptrlist *l1;
2238 Ptrlist *l2;
2239{
2240 Ptrlist *oldl1;
2241
2242 oldl1 = l1;
2243 while (l1->next)
2244 l1 = l1->next;
2245 l1->next = l2;
2246 return oldl1;
2247}
2248
2249/*
2250 * Stack used for transforming postfix form into NFA.
2251 */
2252static Frag_T empty;
2253
2254 static void
2255st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002256 int *postfix UNUSED;
2257 int *end UNUSED;
2258 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002260#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261 FILE *df;
2262 int *p2;
2263
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002264 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002265 if (df)
2266 {
2267 fprintf(df, "Error popping the stack!\n");
2268#ifdef DEBUG
2269 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2270#endif
2271 fprintf(df, "Postfix form is: ");
2272#ifdef DEBUG
2273 for (p2 = postfix; p2 < end; p2++)
2274 {
2275 nfa_set_code(*p2);
2276 fprintf(df, "%s, ", code);
2277 }
2278 nfa_set_code(*p);
2279 fprintf(df, "\nCurrent position is: ");
2280 for (p2 = postfix; p2 <= p; p2 ++)
2281 {
2282 nfa_set_code(*p2);
2283 fprintf(df, "%s, ", code);
2284 }
2285#else
2286 for (p2 = postfix; p2 < end; p2++)
2287 {
2288 fprintf(df, "%d, ", *p2);
2289 }
2290 fprintf(df, "\nCurrent position is: ");
2291 for (p2 = postfix; p2 <= p; p2 ++)
2292 {
2293 fprintf(df, "%d, ", *p2);
2294 }
2295#endif
2296 fprintf(df, "\n--------------------------\n");
2297 fclose(df);
2298 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002299#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300 EMSG(_("E874: (NFA) Could not pop the stack !"));
2301}
2302
2303/*
2304 * Push an item onto the stack.
2305 */
2306 static void
2307st_push(s, p, stack_end)
2308 Frag_T s;
2309 Frag_T **p;
2310 Frag_T *stack_end;
2311{
2312 Frag_T *stackp = *p;
2313
2314 if (stackp >= stack_end)
2315 return;
2316 *stackp = s;
2317 *p = *p + 1;
2318}
2319
2320/*
2321 * Pop an item from the stack.
2322 */
2323 static Frag_T
2324st_pop(p, stack)
2325 Frag_T **p;
2326 Frag_T *stack;
2327{
2328 Frag_T *stackp;
2329
2330 *p = *p - 1;
2331 stackp = *p;
2332 if (stackp < stack)
2333 return empty;
2334 return **p;
2335}
2336
2337/*
2338 * Convert a postfix form into its equivalent NFA.
2339 * Return the NFA start state on success, NULL otherwise.
2340 */
2341 static nfa_state_T *
2342post2nfa(postfix, end, nfa_calc_size)
2343 int *postfix;
2344 int *end;
2345 int nfa_calc_size;
2346{
2347 int *p;
2348 int mopen;
2349 int mclose;
2350 Frag_T *stack = NULL;
2351 Frag_T *stackp = NULL;
2352 Frag_T *stack_end = NULL;
2353 Frag_T e1;
2354 Frag_T e2;
2355 Frag_T e;
2356 nfa_state_T *s;
2357 nfa_state_T *s1;
2358 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002359 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002360
2361 if (postfix == NULL)
2362 return NULL;
2363
Bram Moolenaar053bb602013-05-20 13:55:21 +02002364#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365#define POP() st_pop(&stackp, stack); \
2366 if (stackp < stack) \
2367 { \
2368 st_error(postfix, end, p); \
2369 return NULL; \
2370 }
2371
2372 if (nfa_calc_size == FALSE)
2373 {
2374 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002375 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002376 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002377 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002378 }
2379
2380 for (p = postfix; p < end; ++p)
2381 {
2382 switch (*p)
2383 {
2384 case NFA_CONCAT:
2385 /* Catenation.
2386 * Pay attention: this operator does not exist
2387 * in the r.e. itself (it is implicit, really).
2388 * It is added when r.e. is translated to postfix
2389 * form in re2post().
2390 *
2391 * No new state added here. */
2392 if (nfa_calc_size == TRUE)
2393 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002394 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002395 break;
2396 }
2397 e2 = POP();
2398 e1 = POP();
2399 patch(e1.out, e2.start);
2400 PUSH(frag(e1.start, e2.out));
2401 break;
2402
2403 case NFA_NOT:
2404 /* Negation of a character */
2405 if (nfa_calc_size == TRUE)
2406 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002407 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002408 break;
2409 }
2410 e1 = POP();
2411 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002412#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002413 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002414 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002415#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002416 PUSH(e1);
2417 break;
2418
2419 case NFA_OR:
2420 /* Alternation */
2421 if (nfa_calc_size == TRUE)
2422 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002423 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002424 break;
2425 }
2426 e2 = POP();
2427 e1 = POP();
2428 s = new_state(NFA_SPLIT, e1.start, e2.start);
2429 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002430 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 PUSH(frag(s, append(e1.out, e2.out)));
2432 break;
2433
2434 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002435 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436 if (nfa_calc_size == TRUE)
2437 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002438 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 break;
2440 }
2441 e = POP();
2442 s = new_state(NFA_SPLIT, e.start, NULL);
2443 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002444 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 patch(e.out, s);
2446 PUSH(frag(s, list1(&s->out1)));
2447 break;
2448
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002449 case NFA_STAR_NONGREEDY:
2450 /* Zero or more, prefer zero */
2451 if (nfa_calc_size == TRUE)
2452 {
2453 nstate++;
2454 break;
2455 }
2456 e = POP();
2457 s = new_state(NFA_SPLIT, NULL, e.start);
2458 if (s == NULL)
2459 goto theend;
2460 patch(e.out, s);
2461 PUSH(frag(s, list1(&s->out)));
2462 break;
2463
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002464 case NFA_QUEST:
2465 /* one or zero atoms=> greedy match */
2466 if (nfa_calc_size == TRUE)
2467 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002468 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 break;
2470 }
2471 e = POP();
2472 s = new_state(NFA_SPLIT, e.start, NULL);
2473 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002474 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002475 PUSH(frag(s, append(e.out, list1(&s->out1))));
2476 break;
2477
2478 case NFA_QUEST_NONGREEDY:
2479 /* zero or one atoms => non-greedy match */
2480 if (nfa_calc_size == TRUE)
2481 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002482 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002483 break;
2484 }
2485 e = POP();
2486 s = new_state(NFA_SPLIT, NULL, e.start);
2487 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002488 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002489 PUSH(frag(s, append(e.out, list1(&s->out))));
2490 break;
2491
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002492 case NFA_SKIP_CHAR:
2493 /* Symbol of 0-length, Used in a repetition
2494 * with max/min count of 0 */
2495 if (nfa_calc_size == TRUE)
2496 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002497 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002498 break;
2499 }
2500 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2501 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002502 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503 PUSH(frag(s, list1(&s->out)));
2504 break;
2505
2506 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002507 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002508 case NFA_PREV_ATOM_JUST_BEFORE:
2509 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002510 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002511 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002512 * The \@<= operator: match for the preceding atom.
2513 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002514 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002515 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002516
2517 if (nfa_calc_size == TRUE)
2518 {
2519 nstate += 2;
2520 break;
2521 }
2522 e = POP();
2523 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2524 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002525 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002526 patch(e.out, s1);
2527
2528 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2529 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002530 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002531 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2532 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002533 {
2534 s->negated = TRUE;
2535 s1->negated = TRUE;
2536 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002537 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2538 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2539 {
2540 s->val = *++p; /* get the count */
2541 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2542 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002543
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544 PUSH(frag(s, list1(&s1->out)));
2545 break;
2546
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002547#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002548 case NFA_COMPOSING: /* char with composing char */
2549#if 0
2550 /* TODO */
2551 if (regflags & RF_ICOMBINE)
2552 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002553 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002554 }
2555#endif
2556 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002557#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002558
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002559 case NFA_MOPEN: /* \( \) Submatch */
2560 case NFA_MOPEN1:
2561 case NFA_MOPEN2:
2562 case NFA_MOPEN3:
2563 case NFA_MOPEN4:
2564 case NFA_MOPEN5:
2565 case NFA_MOPEN6:
2566 case NFA_MOPEN7:
2567 case NFA_MOPEN8:
2568 case NFA_MOPEN9:
2569#ifdef FEAT_SYN_HL
2570 case NFA_ZOPEN: /* \z( \) Submatch */
2571 case NFA_ZOPEN1:
2572 case NFA_ZOPEN2:
2573 case NFA_ZOPEN3:
2574 case NFA_ZOPEN4:
2575 case NFA_ZOPEN5:
2576 case NFA_ZOPEN6:
2577 case NFA_ZOPEN7:
2578 case NFA_ZOPEN8:
2579 case NFA_ZOPEN9:
2580#endif
2581 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002582 if (nfa_calc_size == TRUE)
2583 {
2584 nstate += 2;
2585 break;
2586 }
2587
2588 mopen = *p;
2589 switch (*p)
2590 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002591 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2592#ifdef FEAT_SYN_HL
2593 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2594 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2595 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2596 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2597 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2598 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2599 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2600 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2601 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2602 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2603#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002604#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002605 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002606#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002607 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002608 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002609 mclose = *p + NSUBEXP;
2610 break;
2611 }
2612
2613 /* Allow "NFA_MOPEN" as a valid postfix representation for
2614 * the empty regexp "". In this case, the NFA will be
2615 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2616 * empty groups of parenthesis, and empty mbyte chars */
2617 if (stackp == stack)
2618 {
2619 s = new_state(mopen, NULL, NULL);
2620 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002621 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002622 s1 = new_state(mclose, NULL, NULL);
2623 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002624 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002625 patch(list1(&s->out), s1);
2626 PUSH(frag(s, list1(&s1->out)));
2627 break;
2628 }
2629
2630 /* At least one node was emitted before NFA_MOPEN, so
2631 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2632 e = POP();
2633 s = new_state(mopen, e.start, NULL); /* `(' */
2634 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002635 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002636
2637 s1 = new_state(mclose, NULL, NULL); /* `)' */
2638 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002639 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002640 patch(e.out, s1);
2641
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002642#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002643 if (mopen == NFA_COMPOSING)
2644 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002645 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002646#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647
2648 PUSH(frag(s, list1(&s1->out)));
2649 break;
2650
Bram Moolenaar5714b802013-05-28 22:03:20 +02002651 case NFA_BACKREF1:
2652 case NFA_BACKREF2:
2653 case NFA_BACKREF3:
2654 case NFA_BACKREF4:
2655 case NFA_BACKREF5:
2656 case NFA_BACKREF6:
2657 case NFA_BACKREF7:
2658 case NFA_BACKREF8:
2659 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002660#ifdef FEAT_SYN_HL
2661 case NFA_ZREF1:
2662 case NFA_ZREF2:
2663 case NFA_ZREF3:
2664 case NFA_ZREF4:
2665 case NFA_ZREF5:
2666 case NFA_ZREF6:
2667 case NFA_ZREF7:
2668 case NFA_ZREF8:
2669 case NFA_ZREF9:
2670#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002671 if (nfa_calc_size == TRUE)
2672 {
2673 nstate += 2;
2674 break;
2675 }
2676 s = new_state(*p, NULL, NULL);
2677 if (s == NULL)
2678 goto theend;
2679 s1 = new_state(NFA_SKIP, NULL, NULL);
2680 if (s1 == NULL)
2681 goto theend;
2682 patch(list1(&s->out), s1);
2683 PUSH(frag(s, list1(&s1->out)));
2684 break;
2685
Bram Moolenaar423532e2013-05-29 21:14:42 +02002686 case NFA_LNUM:
2687 case NFA_LNUM_GT:
2688 case NFA_LNUM_LT:
2689 case NFA_VCOL:
2690 case NFA_VCOL_GT:
2691 case NFA_VCOL_LT:
2692 case NFA_COL:
2693 case NFA_COL_GT:
2694 case NFA_COL_LT:
2695 if (nfa_calc_size == TRUE)
2696 {
2697 nstate += 1;
2698 break;
2699 }
2700 e1 = POP();
2701 s = new_state(*p, NULL, NULL);
2702 if (s == NULL)
2703 goto theend;
2704 s->val = e1.start->c;
2705 PUSH(frag(s, list1(&s->out)));
2706 break;
2707
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002708 case NFA_ZSTART:
2709 case NFA_ZEND:
2710 default:
2711 /* Operands */
2712 if (nfa_calc_size == TRUE)
2713 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002714 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715 break;
2716 }
2717 s = new_state(*p, NULL, NULL);
2718 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002719 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002720 PUSH(frag(s, list1(&s->out)));
2721 break;
2722
2723 } /* switch(*p) */
2724
2725 } /* for(p = postfix; *p; ++p) */
2726
2727 if (nfa_calc_size == TRUE)
2728 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002729 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002730 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731 }
2732
2733 e = POP();
2734 if (stackp != stack)
2735 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2736
2737 if (istate >= nstate)
2738 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2739
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002740 matchstate = &state_ptr[istate++]; /* the match state */
2741 matchstate->c = NFA_MATCH;
2742 matchstate->out = matchstate->out1 = NULL;
2743
2744 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002745 ret = e.start;
2746
2747theend:
2748 vim_free(stack);
2749 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002750
2751#undef POP1
2752#undef PUSH1
2753#undef POP2
2754#undef PUSH2
2755#undef POP
2756#undef PUSH
2757}
2758
2759/****************************************************************
2760 * NFA execution code.
2761 ****************************************************************/
2762
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002763typedef struct
2764{
2765 int in_use; /* number of subexpr with useful info */
2766
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002767 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002768 union
2769 {
2770 struct multipos
2771 {
2772 lpos_T start;
2773 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002774 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002775 struct linepos
2776 {
2777 char_u *start;
2778 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002779 } line[NSUBEXP];
2780 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002781} regsub_T;
2782
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002783typedef struct
2784{
2785 regsub_T norm; /* \( .. \) matches */
2786#ifdef FEAT_SYN_HL
2787 regsub_T synt; /* \z( .. \) matches */
2788#endif
2789} regsubs_T;
2790
Bram Moolenaar963fee22013-05-26 21:47:28 +02002791/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002792typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002793{
2794 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002795 int count;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002796 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002797} nfa_thread_T;
2798
Bram Moolenaar963fee22013-05-26 21:47:28 +02002799/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800typedef struct
2801{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002802 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002803 int n; /* nr of states currently in "t" */
2804 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002805 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002806} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002807
Bram Moolenaar5714b802013-05-28 22:03:20 +02002808#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002809static void log_subsexpr __ARGS((regsubs_T *subs));
2810static void log_subexpr __ARGS((regsub_T *sub));
2811
2812 static void
2813log_subsexpr(subs)
2814 regsubs_T *subs;
2815{
2816 log_subexpr(&subs->norm);
2817# ifdef FEAT_SYN_HL
2818 log_subexpr(&subs->synt);
2819# endif
2820}
2821
Bram Moolenaar5714b802013-05-28 22:03:20 +02002822 static void
2823log_subexpr(sub)
2824 regsub_T *sub;
2825{
2826 int j;
2827
2828 for (j = 0; j < sub->in_use; j++)
2829 if (REG_MULTI)
2830 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2831 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002832 sub->list.multi[j].start.col,
2833 (int)sub->list.multi[j].start.lnum,
2834 sub->list.multi[j].end.col,
2835 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002836 else
2837 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2838 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002839 (char *)sub->list.line[j].start,
2840 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002841 fprintf(log_fd, "\n");
2842}
2843#endif
2844
Bram Moolenaar963fee22013-05-26 21:47:28 +02002845/* Used during execution: whether a match has been found. */
2846static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002847
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002848static void clear_sub __ARGS((regsub_T *sub));
2849static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2850static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002851static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002852static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
2853static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int *ip));
2854
2855 static void
2856clear_sub(sub)
2857 regsub_T *sub;
2858{
2859 if (REG_MULTI)
2860 /* Use 0xff to set lnum to -1 */
2861 vim_memset(sub->list.multi, 0xff,
2862 sizeof(struct multipos) * nfa_nsubexpr);
2863 else
2864 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2865 sub->in_use = 0;
2866}
2867
2868/*
2869 * Copy the submatches from "from" to "to".
2870 */
2871 static void
2872copy_sub(to, from)
2873 regsub_T *to;
2874 regsub_T *from;
2875{
2876 to->in_use = from->in_use;
2877 if (from->in_use > 0)
2878 {
2879 /* Copy the match start and end positions. */
2880 if (REG_MULTI)
2881 mch_memmove(&to->list.multi[0],
2882 &from->list.multi[0],
2883 sizeof(struct multipos) * from->in_use);
2884 else
2885 mch_memmove(&to->list.line[0],
2886 &from->list.line[0],
2887 sizeof(struct linepos) * from->in_use);
2888 }
2889}
2890
2891/*
2892 * Like copy_sub() but exclude the main match.
2893 */
2894 static void
2895copy_sub_off(to, from)
2896 regsub_T *to;
2897 regsub_T *from;
2898{
2899 if (to->in_use < from->in_use)
2900 to->in_use = from->in_use;
2901 if (from->in_use > 1)
2902 {
2903 /* Copy the match start and end positions. */
2904 if (REG_MULTI)
2905 mch_memmove(&to->list.multi[1],
2906 &from->list.multi[1],
2907 sizeof(struct multipos) * (from->in_use - 1));
2908 else
2909 mch_memmove(&to->list.line[1],
2910 &from->list.line[1],
2911 sizeof(struct linepos) * (from->in_use - 1));
2912 }
2913}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914
Bram Moolenaar428e9872013-05-30 17:05:39 +02002915/*
2916 * Return TRUE if "sub1" and "sub2" have the same positions.
2917 */
2918 static int
2919sub_equal(sub1, sub2)
2920 regsub_T *sub1;
2921 regsub_T *sub2;
2922{
2923 int i;
2924 int todo;
2925 linenr_T s1, e1;
2926 linenr_T s2, e2;
2927 char_u *sp1, *ep1;
2928 char_u *sp2, *ep2;
2929
2930 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2931 if (REG_MULTI)
2932 {
2933 for (i = 0; i < todo; ++i)
2934 {
2935 if (i < sub1->in_use)
2936 {
2937 s1 = sub1->list.multi[i].start.lnum;
2938 e1 = sub1->list.multi[i].end.lnum;
2939 }
2940 else
2941 {
2942 s1 = 0;
2943 e1 = 0;
2944 }
2945 if (i < sub2->in_use)
2946 {
2947 s2 = sub2->list.multi[i].start.lnum;
2948 e2 = sub2->list.multi[i].end.lnum;
2949 }
2950 else
2951 {
2952 s2 = 0;
2953 e2 = 0;
2954 }
2955 if (s1 != s2 || e1 != e2)
2956 return FALSE;
2957 if (s1 != 0 && sub1->list.multi[i].start.col
2958 != sub2->list.multi[i].start.col)
2959 return FALSE;
2960 if (e1 != 0 && sub1->list.multi[i].end.col
2961 != sub2->list.multi[i].end.col)
2962 return FALSE;
2963 }
2964 }
2965 else
2966 {
2967 for (i = 0; i < todo; ++i)
2968 {
2969 if (i < sub1->in_use)
2970 {
2971 sp1 = sub1->list.line[i].start;
2972 ep1 = sub1->list.line[i].end;
2973 }
2974 else
2975 {
2976 sp1 = NULL;
2977 ep1 = NULL;
2978 }
2979 if (i < sub2->in_use)
2980 {
2981 sp2 = sub2->list.line[i].start;
2982 ep2 = sub2->list.line[i].end;
2983 }
2984 else
2985 {
2986 sp2 = NULL;
2987 ep2 = NULL;
2988 }
2989 if (sp1 != sp2 || ep1 != ep2)
2990 return FALSE;
2991 }
2992 }
2993
2994 return TRUE;
2995}
2996
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002997 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002998addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002999 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003001 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003002 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003003{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003004 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003005 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003006 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003007 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003008 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003009 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003010 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003011#ifdef ENABLE_LOG
3012 int did_print = FALSE;
3013#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014
3015 if (l == NULL || state == NULL)
3016 return;
3017
3018 switch (state->c)
3019 {
3020 case NFA_SPLIT:
3021 case NFA_NOT:
3022 case NFA_NOPEN:
3023 case NFA_NCLOSE:
3024 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003025 case NFA_MCLOSE1:
3026 case NFA_MCLOSE2:
3027 case NFA_MCLOSE3:
3028 case NFA_MCLOSE4:
3029 case NFA_MCLOSE5:
3030 case NFA_MCLOSE6:
3031 case NFA_MCLOSE7:
3032 case NFA_MCLOSE8:
3033 case NFA_MCLOSE9:
3034#ifdef FEAT_SYN_HL
3035 case NFA_ZCLOSE:
3036 case NFA_ZCLOSE1:
3037 case NFA_ZCLOSE2:
3038 case NFA_ZCLOSE3:
3039 case NFA_ZCLOSE4:
3040 case NFA_ZCLOSE5:
3041 case NFA_ZCLOSE6:
3042 case NFA_ZCLOSE7:
3043 case NFA_ZCLOSE8:
3044 case NFA_ZCLOSE9:
3045#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003046 /* These nodes are not added themselves but their "out" and/or
3047 * "out1" may be added below. */
3048 break;
3049
3050 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003051 case NFA_MOPEN1:
3052 case NFA_MOPEN2:
3053 case NFA_MOPEN3:
3054 case NFA_MOPEN4:
3055 case NFA_MOPEN5:
3056 case NFA_MOPEN6:
3057 case NFA_MOPEN7:
3058 case NFA_MOPEN8:
3059 case NFA_MOPEN9:
3060#ifdef FEAT_SYN_HL
3061 case NFA_ZOPEN:
3062 case NFA_ZOPEN1:
3063 case NFA_ZOPEN2:
3064 case NFA_ZOPEN3:
3065 case NFA_ZOPEN4:
3066 case NFA_ZOPEN5:
3067 case NFA_ZOPEN6:
3068 case NFA_ZOPEN7:
3069 case NFA_ZOPEN8:
3070 case NFA_ZOPEN9:
3071#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003072 /* These nodes do not need to be added, but we need to bail out
3073 * when it was tried to be added to this list before. */
3074 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003075 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003076 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003077 break;
3078
3079 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003080 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003081 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003082 /* This state is already in the list, don't add it again,
3083 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003084 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003085 {
3086skip_add:
3087#ifdef ENABLE_LOG
3088 nfa_set_code(state->c);
3089 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3090 abs(state->id), l->id, state->c, code);
3091#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003092 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003093 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003094
3095 /* See if the same state is already in the list with the same
3096 * positions. */
3097 for (i = 0; i < l->n; ++i)
3098 {
3099 thread = &l->t[i];
3100 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003101 && sub_equal(&thread->subs.norm, &subs->norm)
3102#ifdef FEAT_SYN_HL
3103 && sub_equal(&thread->subs.synt, &subs->synt)
3104#endif
3105 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003106 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003107 }
3108 }
3109
3110 /* when there are backreferences the number of states may be (a
3111 * lot) bigger */
3112 if (nfa_has_backref && l->n == l->len)
3113 {
3114 int newlen = l->len * 3 / 2 + 50;
3115
3116 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3117 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003119
3120 /* add the state to the list */
3121 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003122 thread = &l->t[l->n++];
3123 thread->state = state;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003124 copy_sub(&thread->subs.norm, &subs->norm);
3125#ifdef FEAT_SYN_HL
3126 copy_sub(&thread->subs.synt, &subs->synt);
3127#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003128#ifdef ENABLE_LOG
3129 {
3130 int col;
3131
3132 if (thread->sub.in_use <= 0)
3133 col = -1;
3134 else if (REG_MULTI)
3135 col = thread->sub.list.multi[0].start.col;
3136 else
3137 col = (int)(thread->sub.list.line[0].start - regline);
3138 nfa_set_code(state->c);
3139 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
3140 abs(state->id), l->id, state->c, code, col);
3141 did_print = TRUE;
3142 }
3143#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003144 }
3145
3146#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003147 if (!did_print)
3148 {
3149 int col;
3150
3151 if (sub->in_use <= 0)
3152 col = -1;
3153 else if (REG_MULTI)
3154 col = sub->list.multi[0].start.col;
3155 else
3156 col = (int)(sub->list.line[0].start - regline);
3157 nfa_set_code(state->c);
3158 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
3159 abs(state->id), l->id, state->c, code, col);
3160 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003161#endif
3162 switch (state->c)
3163 {
3164 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003165 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003166 break;
3167
3168 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003169 /* order matters here */
3170 addstate(l, state->out, subs, off);
3171 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003172 break;
3173
Bram Moolenaar5714b802013-05-28 22:03:20 +02003174 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003175 case NFA_NOPEN:
3176 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003177 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003178 break;
3179
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003180 case NFA_MOPEN:
3181 case NFA_MOPEN1:
3182 case NFA_MOPEN2:
3183 case NFA_MOPEN3:
3184 case NFA_MOPEN4:
3185 case NFA_MOPEN5:
3186 case NFA_MOPEN6:
3187 case NFA_MOPEN7:
3188 case NFA_MOPEN8:
3189 case NFA_MOPEN9:
3190#ifdef FEAT_SYN_HL
3191 case NFA_ZOPEN:
3192 case NFA_ZOPEN1:
3193 case NFA_ZOPEN2:
3194 case NFA_ZOPEN3:
3195 case NFA_ZOPEN4:
3196 case NFA_ZOPEN5:
3197 case NFA_ZOPEN6:
3198 case NFA_ZOPEN7:
3199 case NFA_ZOPEN8:
3200 case NFA_ZOPEN9:
3201#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003204 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003206 sub = &subs->norm;
3207 }
3208#ifdef FEAT_SYN_HL
3209 else if (state->c >= NFA_ZOPEN)
3210 {
3211 subidx = state->c - NFA_ZOPEN;
3212 sub = &subs->synt;
3213 }
3214#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003215 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003216 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003217 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003218 sub = &subs->norm;
3219 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003221 /* Set the position (with "off") in the subexpression. Save and
3222 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003223 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003224 if (REG_MULTI)
3225 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003226 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003227 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003228 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003229 save_in_use = -1;
3230 }
3231 else
3232 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003233 save_in_use = sub->in_use;
3234 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003235 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003236 sub->list.multi[i].start.lnum = -1;
3237 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003238 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003239 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003240 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003241 if (off == -1)
3242 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003243 sub->list.multi[subidx].start.lnum = reglnum + 1;
3244 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003245 }
3246 else
3247 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003248 sub->list.multi[subidx].start.lnum = reglnum;
3249 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003250 (colnr_T)(reginput - regline + off);
3251 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003252 }
3253 else
3254 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003255 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003256 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003257 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003258 save_in_use = -1;
3259 }
3260 else
3261 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003262 save_in_use = sub->in_use;
3263 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003264 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003265 sub->list.line[i].start = NULL;
3266 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003267 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003268 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003269 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003270 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 }
3272
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003273 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003274
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003275 if (save_in_use == -1)
3276 {
3277 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003278 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003279 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003280 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003281 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003282 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003283 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 break;
3285
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003286 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003287 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003289 /* Do not overwrite the position set by \ze. If no \ze
3290 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003291 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 break;
3293 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003294 case NFA_MCLOSE1:
3295 case NFA_MCLOSE2:
3296 case NFA_MCLOSE3:
3297 case NFA_MCLOSE4:
3298 case NFA_MCLOSE5:
3299 case NFA_MCLOSE6:
3300 case NFA_MCLOSE7:
3301 case NFA_MCLOSE8:
3302 case NFA_MCLOSE9:
3303#ifdef FEAT_SYN_HL
3304 case NFA_ZCLOSE:
3305 case NFA_ZCLOSE1:
3306 case NFA_ZCLOSE2:
3307 case NFA_ZCLOSE3:
3308 case NFA_ZCLOSE4:
3309 case NFA_ZCLOSE5:
3310 case NFA_ZCLOSE6:
3311 case NFA_ZCLOSE7:
3312 case NFA_ZCLOSE8:
3313 case NFA_ZCLOSE9:
3314#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003315 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003317 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003319 sub = &subs->norm;
3320 }
3321#ifdef FEAT_SYN_HL
3322 else if (state->c >= NFA_ZCLOSE)
3323 {
3324 subidx = state->c - NFA_ZCLOSE;
3325 sub = &subs->synt;
3326 }
3327#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003328 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003329 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003330 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003331 sub = &subs->norm;
3332 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003334 /* We don't fill in gaps here, there must have been an MOPEN that
3335 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003336 save_in_use = sub->in_use;
3337 if (sub->in_use <= subidx)
3338 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339 if (REG_MULTI)
3340 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003341 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003342 if (off == -1)
3343 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003344 sub->list.multi[subidx].end.lnum = reglnum + 1;
3345 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003346 }
3347 else
3348 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003349 sub->list.multi[subidx].end.lnum = reglnum;
3350 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003351 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003352 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003353 }
3354 else
3355 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003356 save_ptr = sub->list.line[subidx].end;
3357 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 }
3359
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003360 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003361
3362 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003363 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003365 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003366 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367 break;
3368 }
3369}
3370
3371/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003372 * Like addstate(), but the new state(s) are put at position "*ip".
3373 * Used for zero-width matches, next state to use is the added one.
3374 * This makes sure the order of states to be tried does not change, which
3375 * matters for alternatives.
3376 */
3377 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003378addstate_here(l, state, subs, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003379 nfa_list_T *l; /* runtime state list */
3380 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003381 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003382 int *ip;
3383{
3384 int tlen = l->n;
3385 int count;
3386 int i = *ip;
3387
3388 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003389 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003390
3391 /* when "*ip" was at the end of the list, nothing to do */
3392 if (i + 1 == tlen)
3393 return;
3394
3395 /* re-order to put the new state at the current position */
3396 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003397 if (count == 1)
3398 {
3399 /* overwrite the current state */
3400 l->t[i] = l->t[l->n - 1];
3401 }
3402 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003403 {
3404 /* make space for new states, then move them from the
3405 * end to the current position */
3406 mch_memmove(&(l->t[i + count]),
3407 &(l->t[i + 1]),
3408 sizeof(nfa_thread_T) * (l->n - i - 1));
3409 mch_memmove(&(l->t[i]),
3410 &(l->t[l->n - 1]),
3411 sizeof(nfa_thread_T) * count);
3412 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003413 --l->n;
3414 *ip = i - 1;
3415}
3416
3417/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 * Check character class "class" against current character c.
3419 */
3420 static int
3421check_char_class(class, c)
3422 int class;
3423 int c;
3424{
3425 switch (class)
3426 {
3427 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003428 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429 return OK;
3430 break;
3431 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003432 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003433 return OK;
3434 break;
3435 case NFA_CLASS_BLANK:
3436 if (c == ' ' || c == '\t')
3437 return OK;
3438 break;
3439 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003440 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003441 return OK;
3442 break;
3443 case NFA_CLASS_DIGIT:
3444 if (VIM_ISDIGIT(c))
3445 return OK;
3446 break;
3447 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003448 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449 return OK;
3450 break;
3451 case NFA_CLASS_LOWER:
3452 if (MB_ISLOWER(c))
3453 return OK;
3454 break;
3455 case NFA_CLASS_PRINT:
3456 if (vim_isprintc(c))
3457 return OK;
3458 break;
3459 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003460 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461 return OK;
3462 break;
3463 case NFA_CLASS_SPACE:
3464 if ((c >=9 && c <= 13) || (c == ' '))
3465 return OK;
3466 break;
3467 case NFA_CLASS_UPPER:
3468 if (MB_ISUPPER(c))
3469 return OK;
3470 break;
3471 case NFA_CLASS_XDIGIT:
3472 if (vim_isxdigit(c))
3473 return OK;
3474 break;
3475 case NFA_CLASS_TAB:
3476 if (c == '\t')
3477 return OK;
3478 break;
3479 case NFA_CLASS_RETURN:
3480 if (c == '\r')
3481 return OK;
3482 break;
3483 case NFA_CLASS_BACKSPACE:
3484 if (c == '\b')
3485 return OK;
3486 break;
3487 case NFA_CLASS_ESCAPE:
3488 if (c == '\033')
3489 return OK;
3490 break;
3491
3492 default:
3493 /* should not be here :P */
3494 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3495 }
3496 return FAIL;
3497}
3498
Bram Moolenaar5714b802013-05-28 22:03:20 +02003499static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3500
3501/*
3502 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003503 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003504 */
3505 static int
3506match_backref(sub, subidx, bytelen)
3507 regsub_T *sub; /* pointers to subexpressions */
3508 int subidx;
3509 int *bytelen; /* out: length of match in bytes */
3510{
3511 int len;
3512
3513 if (sub->in_use <= subidx)
3514 {
3515retempty:
3516 /* backref was not set, match an empty string */
3517 *bytelen = 0;
3518 return TRUE;
3519 }
3520
3521 if (REG_MULTI)
3522 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003523 if (sub->list.multi[subidx].start.lnum < 0
3524 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003525 goto retempty;
3526 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003527 len = sub->list.multi[subidx].end.col
3528 - sub->list.multi[subidx].start.col;
3529 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003530 reginput, &len) == 0)
3531 {
3532 *bytelen = len;
3533 return TRUE;
3534 }
3535 }
3536 else
3537 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003538 if (sub->list.line[subidx].start == NULL
3539 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003540 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003541 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3542 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003543 {
3544 *bytelen = len;
3545 return TRUE;
3546 }
3547 }
3548 return FALSE;
3549}
3550
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003551#ifdef FEAT_SYN_HL
3552
3553static int match_zref __ARGS((int subidx, int *bytelen));
3554
3555/*
3556 * Check for a match with \z subexpression "subidx".
3557 * Return TRUE if it matches.
3558 */
3559 static int
3560match_zref(subidx, bytelen)
3561 int subidx;
3562 int *bytelen; /* out: length of match in bytes */
3563{
3564 int len;
3565
3566 cleanup_zsubexpr();
3567 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3568 {
3569 /* backref was not set, match an empty string */
3570 *bytelen = 0;
3571 return TRUE;
3572 }
3573
3574 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3575 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3576 {
3577 *bytelen = len;
3578 return TRUE;
3579 }
3580 return FALSE;
3581}
3582#endif
3583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003584/*
3585 * Set all NFA nodes' list ID equal to -1.
3586 */
3587 static void
3588nfa_set_neg_listids(start)
3589 nfa_state_T *start;
3590{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003591 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592 {
3593 start->lastlist = -1;
3594 nfa_set_neg_listids(start->out);
3595 nfa_set_neg_listids(start->out1);
3596 }
3597}
3598
3599/*
3600 * Set all NFA nodes' list ID equal to 0.
3601 */
3602 static void
3603nfa_set_null_listids(start)
3604 nfa_state_T *start;
3605{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003606 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003607 {
3608 start->lastlist = 0;
3609 nfa_set_null_listids(start->out);
3610 nfa_set_null_listids(start->out1);
3611 }
3612}
3613
3614/*
3615 * Save list IDs for all NFA states in "list".
3616 */
3617 static void
3618nfa_save_listids(start, list)
3619 nfa_state_T *start;
3620 int *list;
3621{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003622 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 {
3624 list[abs(start->id)] = start->lastlist;
3625 start->lastlist = -1;
3626 nfa_save_listids(start->out, list);
3627 nfa_save_listids(start->out1, list);
3628 }
3629}
3630
3631/*
3632 * Restore list IDs from "list" to all NFA states.
3633 */
3634 static void
3635nfa_restore_listids(start, list)
3636 nfa_state_T *start;
3637 int *list;
3638{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003639 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640 {
3641 start->lastlist = list[abs(start->id)];
3642 nfa_restore_listids(start->out, list);
3643 nfa_restore_listids(start->out1, list);
3644 }
3645}
3646
Bram Moolenaar423532e2013-05-29 21:14:42 +02003647 static int
3648nfa_re_num_cmp(val, op, pos)
3649 long_u val;
3650 int op;
3651 long_u pos;
3652{
3653 if (op == 1) return pos > val;
3654 if (op == 2) return pos < val;
3655 return val == pos;
3656}
3657
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003658static int nfa_regmatch __ARGS((nfa_state_T *start, regsubs_T *submatch, regsubs_T *m, save_se_T *endp));
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003659
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660/*
3661 * Main matching routine.
3662 *
3663 * Run NFA to determine whether it matches reginput.
3664 *
Bram Moolenaar61602c52013-06-01 19:54:43 +02003665 * When "endp" is not NULL it is a required end-of-match position.
3666 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003667 * Return TRUE if there is a match, FALSE otherwise.
3668 * Note: Caller must ensure that: start != NULL.
3669 */
3670 static int
Bram Moolenaar61602c52013-06-01 19:54:43 +02003671nfa_regmatch(start, submatch, m, endp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003673 regsubs_T *submatch;
3674 regsubs_T *m;
Bram Moolenaar61602c52013-06-01 19:54:43 +02003675 save_se_T *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003676{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003677 int result;
3678 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003680 int go_to_nextline = FALSE;
3681 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003682 nfa_list_T list[3];
3683 nfa_list_T *listtbl[2][2];
3684 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003686 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003687 nfa_list_T *thislist;
3688 nfa_list_T *nextlist;
3689 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003690 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003691#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003692 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003693
3694 if (debug == NULL)
3695 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003696 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697 return FALSE;
3698 }
3699#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003700 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003701
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003702 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003703 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003704 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3705 list[0].len = nstate + 1;
3706 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3707 list[1].len = nstate + 1;
3708 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3709 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003710 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3711 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712
3713#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003714 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003715 if (log_fd != NULL)
3716 {
3717 fprintf(log_fd, "**********************************\n");
3718 nfa_set_code(start->c);
3719 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3720 abs(start->id), code);
3721 fprintf(log_fd, "**********************************\n");
3722 }
3723 else
3724 {
3725 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3726 log_fd = stderr;
3727 }
3728#endif
3729
3730 thislist = &list[0];
3731 thislist->n = 0;
3732 nextlist = &list[1];
3733 nextlist->n = 0;
3734 neglist = &list[2];
3735 neglist->n = 0;
3736#ifdef ENABLE_LOG
3737 fprintf(log_fd, "(---) STARTSTATE\n");
3738#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003739 thislist->id = listid;
3740 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741
3742 /* There are two cases when the NFA advances: 1. input char matches the
3743 * NFA node and 2. input char does not match the NFA node, but the next
3744 * node is NFA_NOT. The following macro calls addstate() according to
3745 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3746 listtbl[0][0] = NULL;
3747 listtbl[0][1] = neglist;
3748 listtbl[1][0] = nextlist;
3749 listtbl[1][1] = NULL;
3750#define ADD_POS_NEG_STATE(node) \
3751 ll = listtbl[result ? 1 : 0][node->negated]; \
3752 if (ll != NULL) \
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003753 addstate(ll, node->out , &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754
3755
3756 /*
3757 * Run for each character.
3758 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003759 for (;;)
3760 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003761 int curc;
3762 int clen;
3763
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764#ifdef FEAT_MBYTE
3765 if (has_mbyte)
3766 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003767 curc = (*mb_ptr2char)(reginput);
3768 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003769 }
3770 else
3771#endif
3772 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003773 curc = *reginput;
3774 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003775 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003776 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003777 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003778 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003779 go_to_nextline = FALSE;
3780 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003781
3782 /* swap lists */
3783 thislist = &list[flag];
3784 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003785 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003786 listtbl[1][0] = nextlist;
3787 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003788 thislist->id = listid;
3789 nextlist->id = listid + 1;
3790 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003791
3792#ifdef ENABLE_LOG
3793 fprintf(log_fd, "------------------------------------------\n");
3794 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003795 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003796 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003797 {
3798 int i;
3799
3800 for (i = 0; i < thislist->n; i++)
3801 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3802 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003803 fprintf(log_fd, "\n");
3804#endif
3805
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003806#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807 fprintf(debug, "\n-------------------\n");
3808#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003809 /*
3810 * If the state lists are empty we can stop.
3811 */
3812 if (thislist->n == 0 && neglist->n == 0)
3813 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003814
3815 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003816 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003817 {
3818 if (neglist->n > 0)
3819 {
3820 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003821 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003822 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003823 }
3824 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003825 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003826
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003827#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003828 nfa_set_code(t->state->c);
3829 fprintf(debug, "%s, ", code);
3830#endif
3831#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003832 {
3833 int col;
3834
3835 if (t->sub.in_use <= 0)
3836 col = -1;
3837 else if (REG_MULTI)
3838 col = t->sub.list.multi[0].start.col;
3839 else
3840 col = (int)(t->sub.list.line[0].start - regline);
3841 nfa_set_code(t->state->c);
3842 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3843 abs(t->state->id), (int)t->state->c, code, col);
3844 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003845#endif
3846
3847 /*
3848 * Handle the possible codes of the current state.
3849 * The most important is NFA_MATCH.
3850 */
3851 switch (t->state->c)
3852 {
3853 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003854 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003855 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003856 copy_sub(&submatch->norm, &t->subs.norm);
3857#ifdef FEAT_SYN_HL
3858 copy_sub(&submatch->synt, &t->subs.synt);
3859#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003860#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003861 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003862#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003863 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003864 * states at this position. When the list of states is going
3865 * to be empty quit without advancing, so that "reginput" is
3866 * correct. */
3867 if (nextlist->n == 0 && neglist->n == 0)
3868 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003869 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003870 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003871
3872 case NFA_END_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003873 /* This is only encountered after a NFA_START_INVISIBLE or
3874 * NFA_START_INVISIBLE_BEFORE node.
3875 * They surround a zero-width group, used with "\@=", "\&",
3876 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003877 * If we got here, it means that the current "invisible" group
3878 * finished successfully, so return control to the parent
3879 * nfa_regmatch(). Submatches are stored in *m, and used in
3880 * the parent call. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003881 if (start->c == NFA_MOPEN)
Bram Moolenaar61602c52013-06-01 19:54:43 +02003882 /* TODO: do we ever get here? */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003883 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003884 else
3885 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003886#ifdef ENABLE_LOG
3887 if (endp != NULL)
3888 {
3889 if (REG_MULTI)
3890 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
3891 (int)reglnum,
3892 (int)endp->se_u.pos.lnum,
3893 (int)(reginput - regline),
3894 endp->se_u.pos.col);
3895 else
3896 fprintf(log_fd, "Current col: %d, endp col: %d\n",
3897 (int)(reginput - regline),
3898 (int)(endp->se_u.ptr - reginput));
3899 }
3900#endif
3901 /* It's only a match if it ends at "endp" */
3902 if (endp != NULL && (REG_MULTI
3903 ? (reglnum != endp->se_u.pos.lnum
3904 || (int)(reginput - regline)
3905 != endp->se_u.pos.col)
3906 : reginput != endp->se_u.ptr))
3907 break;
3908
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003909 /* do not set submatches for \@! */
3910 if (!t->state->negated)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003911 {
3912 copy_sub(&m->norm, &t->subs.norm);
3913#ifdef FEAT_SYN_HL
3914 copy_sub(&m->synt, &t->subs.synt);
3915#endif
3916 }
Bram Moolenaar963fee22013-05-26 21:47:28 +02003917 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918 }
3919 break;
3920
3921 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003922 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003923 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003924 char_u *save_reginput = reginput;
3925 char_u *save_regline = regline;
3926 int save_reglnum = reglnum;
3927 int save_nfa_match = nfa_match;
3928 save_se_T endpos;
3929 save_se_T *endposp = NULL;
3930
3931 if (t->state->c == NFA_START_INVISIBLE_BEFORE)
3932 {
3933 /* The recursive match must end at the current position. */
3934 endposp = &endpos;
3935 if (REG_MULTI)
3936 {
3937 endpos.se_u.pos.col = (int)(reginput - regline);
3938 endpos.se_u.pos.lnum = reglnum;
3939 }
3940 else
3941 endpos.se_u.ptr = reginput;
3942
3943 /* Go back the specified number of bytes, or as far as the
3944 * start of the previous line, to try matching "\@<=" or
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003945 * not matching "\@<!".
3946 * TODO: This is very inefficient! Would be better to
3947 * first check for a match with what follows. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003948 if (t->state->val <= 0)
3949 {
3950 if (REG_MULTI)
3951 {
3952 regline = reg_getline(--reglnum);
3953 if (regline == NULL)
3954 /* can't go before the first line */
3955 regline = reg_getline(++reglnum);
3956 }
3957 reginput = regline;
3958 }
3959 else
3960 {
3961 if (REG_MULTI
3962 && (int)(reginput - regline) < t->state->val)
3963 {
3964 /* Not enough bytes in this line, go to end of
3965 * previous line. */
3966 regline = reg_getline(--reglnum);
3967 if (regline == NULL)
3968 {
3969 /* can't go before the first line */
3970 regline = reg_getline(++reglnum);
3971 reginput = regline;
3972 }
3973 else
3974 reginput = regline + STRLEN(regline);
3975 }
3976 if ((int)(reginput - regline) >= t->state->val)
3977 {
3978 reginput -= t->state->val;
3979#ifdef FEAT_MBYTE
3980 if (has_mbyte)
3981 reginput -= mb_head_off(regline, reginput);
3982#endif
3983 }
3984 else
3985 reginput = regline;
3986 }
3987 }
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003988
3989 /* Call nfa_regmatch() to check if the current concat matches
3990 * at this position. The concat ends with the node
3991 * NFA_END_INVISIBLE */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003992 if (listids == NULL)
3993 {
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003994 listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003995 if (listids == NULL)
3996 {
3997 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3998 return 0;
3999 }
4000 }
4001#ifdef ENABLE_LOG
4002 if (log_fd != stderr)
4003 fclose(log_fd);
4004 log_fd = NULL;
4005#endif
4006 /* Have to clear the listid field of the NFA nodes, so that
4007 * nfa_regmatch() and addstate() can run properly after
4008 * recursion. */
4009 nfa_save_listids(start, listids);
4010 nfa_set_null_listids(start);
Bram Moolenaar61602c52013-06-01 19:54:43 +02004011 result = nfa_regmatch(t->state->out, submatch, m, endposp);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004012 nfa_set_neg_listids(start);
4013 nfa_restore_listids(start, listids);
Bram Moolenaar14f55c62013-05-31 21:45:09 +02004014
4015 /* restore position in input text */
4016 reginput = save_reginput;
4017 regline = save_regline;
4018 reglnum = save_reglnum;
4019 nfa_match = save_nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004020
4021#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004022 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004023 if (log_fd != NULL)
4024 {
4025 fprintf(log_fd, "****************************\n");
4026 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4027 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4028 fprintf(log_fd, "****************************\n");
4029 }
4030 else
4031 {
4032 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4033 log_fd = stderr;
4034 }
4035#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02004036 /* for \@! it is a match when result is FALSE */
4037 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004038 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004039 /* Copy submatch info from the recursive call */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004040 copy_sub_off(&t->subs.norm, &m->norm);
4041#ifdef FEAT_SYN_HL
4042 copy_sub_off(&t->subs.synt, &m->synt);
4043#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004044
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004045 /* t->state->out1 is the corresponding END_INVISIBLE node;
4046 * Add it to the current list (zero-width match). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004047 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004048 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004049 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004050 break;
Bram Moolenaar14f55c62013-05-31 21:45:09 +02004051 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004052
4053 case NFA_BOL:
4054 if (reginput == regline)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004055 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004056 break;
4057
4058 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004059 if (curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004060 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004061 break;
4062
4063 case NFA_BOW:
4064 {
4065 int bow = TRUE;
4066
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004067 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004068 bow = FALSE;
4069#ifdef FEAT_MBYTE
4070 else if (has_mbyte)
4071 {
4072 int this_class;
4073
4074 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004075 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004076 if (this_class <= 1)
4077 bow = FALSE;
4078 else if (reg_prev_class() == this_class)
4079 bow = FALSE;
4080 }
4081#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004082 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004083 || (reginput > regline
4084 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004085 bow = FALSE;
4086 if (bow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004087 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004088 break;
4089 }
4090
4091 case NFA_EOW:
4092 {
4093 int eow = TRUE;
4094
4095 if (reginput == regline)
4096 eow = FALSE;
4097#ifdef FEAT_MBYTE
4098 else if (has_mbyte)
4099 {
4100 int this_class, prev_class;
4101
4102 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004103 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004104 prev_class = reg_prev_class();
4105 if (this_class == prev_class
4106 || prev_class == 0 || prev_class == 1)
4107 eow = FALSE;
4108 }
4109#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004110 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004111 || (reginput[0] != NUL
4112 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 eow = FALSE;
4114 if (eow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004115 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004116 break;
4117 }
4118
Bram Moolenaar4b780632013-05-31 22:14:52 +02004119 case NFA_BOF:
4120 if (reglnum == 0 && reginput == regline
4121 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004122 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004123 break;
4124
4125 case NFA_EOF:
4126 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004127 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004128 break;
4129
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004130#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004132 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004133 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004134 int len = 0;
4135 nfa_state_T *end;
4136 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004137 int cchars[MAX_MCO];
4138 int ccount = 0;
4139 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004140
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004142 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004143 if (utf_iscomposing(sta->c))
4144 {
4145 /* Only match composing character(s), ignore base
4146 * character. Used for ".{composing}" and "{composing}"
4147 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004148 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004149 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004150 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004151 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004152 /* If \Z was present, then ignore composing characters.
4153 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004154 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004155 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004156 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004157 else
4158 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004159 while (sta->c != NFA_END_COMPOSING)
4160 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004161 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004162
4163 /* Check base character matches first, unless ignored. */
4164 else if (len > 0 || mc == sta->c)
4165 {
4166 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004167 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004168 len += mb_char2len(mc);
4169 sta = sta->out;
4170 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004171
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004172 /* We don't care about the order of composing characters.
4173 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004174 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004175 {
4176 mc = mb_ptr2char(reginput + len);
4177 cchars[ccount++] = mc;
4178 len += mb_char2len(mc);
4179 if (ccount == MAX_MCO)
4180 break;
4181 }
4182
4183 /* Check that each composing char in the pattern matches a
4184 * composing char in the text. We do not check if all
4185 * composing chars are matched. */
4186 result = OK;
4187 while (sta->c != NFA_END_COMPOSING)
4188 {
4189 for (j = 0; j < ccount; ++j)
4190 if (cchars[j] == sta->c)
4191 break;
4192 if (j == ccount)
4193 {
4194 result = FAIL;
4195 break;
4196 }
4197 sta = sta->out;
4198 }
4199 }
4200 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004201 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004202
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004203 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004204 ADD_POS_NEG_STATE(end);
4205 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004206 }
4207#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208
4209 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004210 if (curc == NUL && !reg_line_lbr && REG_MULTI
4211 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004212 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004213 go_to_nextline = TRUE;
4214 /* Pass -1 for the offset, which means taking the position
4215 * at the start of the next line. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004216 addstate(nextlist, t->state->out, &t->subs, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004217 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004218 else if (curc == '\n' && reg_line_lbr)
4219 {
4220 /* match \n as if it is an ordinary character */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004221 addstate(nextlist, t->state->out, &t->subs, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004222 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004223 break;
4224
4225 case NFA_CLASS_ALNUM:
4226 case NFA_CLASS_ALPHA:
4227 case NFA_CLASS_BLANK:
4228 case NFA_CLASS_CNTRL:
4229 case NFA_CLASS_DIGIT:
4230 case NFA_CLASS_GRAPH:
4231 case NFA_CLASS_LOWER:
4232 case NFA_CLASS_PRINT:
4233 case NFA_CLASS_PUNCT:
4234 case NFA_CLASS_SPACE:
4235 case NFA_CLASS_UPPER:
4236 case NFA_CLASS_XDIGIT:
4237 case NFA_CLASS_TAB:
4238 case NFA_CLASS_RETURN:
4239 case NFA_CLASS_BACKSPACE:
4240 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004241 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004242 ADD_POS_NEG_STATE(t->state);
4243 break;
4244
4245 case NFA_END_NEG_RANGE:
4246 /* This follows a series of negated nodes, like:
4247 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004248 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004249 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004250 break;
4251
4252 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004253 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004254 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004255 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 break;
4257
4258 /*
4259 * Character classes like \a for alpha, \d for digit etc.
4260 */
4261 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004262 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004263 ADD_POS_NEG_STATE(t->state);
4264 break;
4265
4266 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004267 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004268 ADD_POS_NEG_STATE(t->state);
4269 break;
4270
4271 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004272 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004273 ADD_POS_NEG_STATE(t->state);
4274 break;
4275
4276 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004277 result = !VIM_ISDIGIT(curc)
4278 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004279 ADD_POS_NEG_STATE(t->state);
4280 break;
4281
4282 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004283 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004284 ADD_POS_NEG_STATE(t->state);
4285 break;
4286
4287 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004288 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004289 ADD_POS_NEG_STATE(t->state);
4290 break;
4291
4292 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004293 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294 ADD_POS_NEG_STATE(t->state);
4295 break;
4296
4297 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004298 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004299 ADD_POS_NEG_STATE(t->state);
4300 break;
4301
4302 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004303 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304 ADD_POS_NEG_STATE(t->state);
4305 break;
4306
4307 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004308 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004309 ADD_POS_NEG_STATE(t->state);
4310 break;
4311
4312 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004313 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004314 ADD_POS_NEG_STATE(t->state);
4315 break;
4316
4317 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004318 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004319 ADD_POS_NEG_STATE(t->state);
4320 break;
4321
4322 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004323 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004324 ADD_POS_NEG_STATE(t->state);
4325 break;
4326
4327 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004328 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004329 ADD_POS_NEG_STATE(t->state);
4330 break;
4331
4332 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004333 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004334 ADD_POS_NEG_STATE(t->state);
4335 break;
4336
4337 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004338 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004339 ADD_POS_NEG_STATE(t->state);
4340 break;
4341
4342 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004343 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004344 ADD_POS_NEG_STATE(t->state);
4345 break;
4346
4347 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004348 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004349 ADD_POS_NEG_STATE(t->state);
4350 break;
4351
4352 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004353 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 ADD_POS_NEG_STATE(t->state);
4355 break;
4356
4357 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004358 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004359 ADD_POS_NEG_STATE(t->state);
4360 break;
4361
4362 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004363 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004364 ADD_POS_NEG_STATE(t->state);
4365 break;
4366
4367 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004368 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004369 ADD_POS_NEG_STATE(t->state);
4370 break;
4371
4372 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004373 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004374 ADD_POS_NEG_STATE(t->state);
4375 break;
4376
4377 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004378 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004379 ADD_POS_NEG_STATE(t->state);
4380 break;
4381
4382 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004383 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004384 ADD_POS_NEG_STATE(t->state);
4385 break;
4386
4387 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004388 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004389 ADD_POS_NEG_STATE(t->state);
4390 break;
4391
Bram Moolenaar5714b802013-05-28 22:03:20 +02004392 case NFA_BACKREF1:
4393 case NFA_BACKREF2:
4394 case NFA_BACKREF3:
4395 case NFA_BACKREF4:
4396 case NFA_BACKREF5:
4397 case NFA_BACKREF6:
4398 case NFA_BACKREF7:
4399 case NFA_BACKREF8:
4400 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004401#ifdef FEAT_SYN_HL
4402 case NFA_ZREF1:
4403 case NFA_ZREF2:
4404 case NFA_ZREF3:
4405 case NFA_ZREF4:
4406 case NFA_ZREF5:
4407 case NFA_ZREF6:
4408 case NFA_ZREF7:
4409 case NFA_ZREF8:
4410 case NFA_ZREF9:
4411#endif
4412 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004413 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004414 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004415 int bytelen;
4416
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004417 if (t->state->c <= NFA_BACKREF9)
4418 {
4419 subidx = t->state->c - NFA_BACKREF1 + 1;
4420 result = match_backref(&t->subs.norm, subidx, &bytelen);
4421 }
4422#ifdef FEAT_SYN_HL
4423 else
4424 {
4425 subidx = t->state->c - NFA_ZREF1 + 1;
4426 result = match_zref(subidx, &bytelen);
4427 }
4428#endif
4429
Bram Moolenaar5714b802013-05-28 22:03:20 +02004430 if (result)
4431 {
4432 if (bytelen == 0)
4433 {
4434 /* empty match always works, add NFA_SKIP with zero to
4435 * be used next */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004436 addstate_here(thislist, t->state->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004437 &listidx);
4438 thislist->t[listidx + 1].count = 0;
4439 }
4440 else if (bytelen <= clen)
4441 {
4442 /* match current character, jump ahead to out of
4443 * NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004444 addstate(nextlist, t->state->out->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004445#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004446 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004447#endif
4448 }
4449 else
4450 {
4451 /* skip ofer the matched characters, set character
4452 * count in NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004453 addstate(nextlist, t->state->out, &t->subs, bytelen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004454 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4455#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004456 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004457#endif
4458 }
4459
4460 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004461 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004462 }
4463 case NFA_SKIP:
4464 /* charater of previous matching \1 .. \9 */
4465 if (t->count - clen <= 0)
4466 {
4467 /* end of match, go to what follows */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004468 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004469#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004470 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004471#endif
4472 }
4473 else
4474 {
4475 /* add state again with decremented count */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004476 addstate(nextlist, t->state, &t->subs, 0);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004477 nextlist->t[nextlist->n - 1].count = t->count - clen;
4478#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004479 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004480#endif
4481 }
4482 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004483
4484 case NFA_SKIP_CHAR:
4485 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004486 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004487 /* TODO: should not happen? */
4488 break;
4489
Bram Moolenaar423532e2013-05-29 21:14:42 +02004490 case NFA_LNUM:
4491 case NFA_LNUM_GT:
4492 case NFA_LNUM_LT:
4493 result = (REG_MULTI &&
4494 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4495 (long_u)(reglnum + reg_firstlnum)));
4496 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004497 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004498 break;
4499
4500 case NFA_COL:
4501 case NFA_COL_GT:
4502 case NFA_COL_LT:
4503 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4504 (long_u)(reginput - regline) + 1);
4505 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004506 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004507 break;
4508
4509 case NFA_VCOL:
4510 case NFA_VCOL_GT:
4511 case NFA_VCOL_LT:
4512 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4513 (long_u)win_linetabsize(
4514 reg_win == NULL ? curwin : reg_win,
4515 regline, (colnr_T)(reginput - regline)) + 1);
4516 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004517 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004518 break;
4519
4520 case NFA_CURSOR:
4521 result = (reg_win != NULL
4522 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4523 && ((colnr_T)(reginput - regline)
4524 == reg_win->w_cursor.col));
4525 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004526 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004527 break;
4528
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004530 {
4531 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004532
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004533 /* TODO: put this in #ifdef later */
4534 if (c < -256)
4535 EMSGN("INTERNAL: Negative state char: %ld", c);
4536 if (is_Magic(c))
4537 c = un_Magic(c);
4538 result = (c == curc);
4539
4540 if (!result && ireg_ic)
4541 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004542#ifdef FEAT_MBYTE
4543 /* If there is a composing character which is not being
4544 * ignored there can be no match. Match with composing
4545 * character uses NFA_COMPOSING above. */
4546 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004547 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004548 result = FALSE;
4549#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550 ADD_POS_NEG_STATE(t->state);
4551 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004552 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 }
4554
4555 } /* for (thislist = thislist; thislist->state; thislist++) */
4556
Bram Moolenaare23febd2013-05-26 18:40:14 +02004557 /* Look for the start of a match in the current position by adding the
4558 * start state to the list of states.
4559 * The first found match is the leftmost one, thus the order of states
4560 * matters!
4561 * Do not add the start state in recursive calls of nfa_regmatch(),
4562 * because recursive calls should only start in the first position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004563 * Unless "endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004564 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004565 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004566 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004567 && reglnum == 0
4568 && clen != 0
4569 && (ireg_maxcol == 0
4570 || (colnr_T)(reginput - regline) < ireg_maxcol))
4571 || (endp != NULL
4572 && (REG_MULTI
4573 ? (reglnum < endp->se_u.pos.lnum
4574 || (reglnum == endp->se_u.pos.lnum
4575 && (int)(reginput - regline)
4576 < endp->se_u.pos.col))
4577 : reginput < endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578 {
4579#ifdef ENABLE_LOG
4580 fprintf(log_fd, "(---) STARTSTATE\n");
4581#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004582 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004583 }
4584
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004585#ifdef ENABLE_LOG
4586 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004587 {
4588 int i;
4589
4590 for (i = 0; i < thislist->n; i++)
4591 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4592 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004593 fprintf(log_fd, "\n");
4594#endif
4595
4596nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004597 /* Advance to the next character, or advance to the next line, or
4598 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004599 if (clen != 0)
4600 reginput += clen;
Bram Moolenaar61602c52013-06-01 19:54:43 +02004601 else if (go_to_nextline || (endp != NULL && REG_MULTI
4602 && reglnum < endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004603 reg_nextline();
4604 else
4605 break;
4606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004607
4608#ifdef ENABLE_LOG
4609 if (log_fd != stderr)
4610 fclose(log_fd);
4611 log_fd = NULL;
4612#endif
4613
4614theend:
4615 /* Free memory */
4616 vim_free(list[0].t);
4617 vim_free(list[1].t);
4618 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004619 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004620#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004621#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004622 fclose(debug);
4623#endif
4624
Bram Moolenaar963fee22013-05-26 21:47:28 +02004625 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004626}
4627
4628/*
4629 * Try match of "prog" with at regline["col"].
4630 * Returns 0 for failure, number of lines contained in the match otherwise.
4631 */
4632 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004633nfa_regtry(prog, col)
4634 nfa_regprog_T *prog;
4635 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004636{
4637 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004638 regsubs_T subs, m;
4639 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004640#ifdef ENABLE_LOG
4641 FILE *f;
4642#endif
4643
4644 reginput = regline + col;
4645 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004646#ifdef FEAT_SYN_HL
4647 /* Clear the external match subpointers if necessary. */
4648 if (prog->reghasz == REX_SET)
4649 need_clear_zsubexpr = TRUE;
4650#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651
4652#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004653 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004654 if (f != NULL)
4655 {
4656 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4657 fprintf(f, " =======================================================\n");
4658#ifdef DEBUG
4659 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4660#endif
4661 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004662 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004663 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004664 fprintf(f, "\n\n");
4665 fclose(f);
4666 }
4667 else
4668 EMSG(_("Could not open temporary log file for writing "));
4669#endif
4670
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004671 clear_sub(&subs.norm);
4672 clear_sub(&m.norm);
4673#ifdef FEAT_SYN_HL
4674 clear_sub(&subs.synt);
4675 clear_sub(&m.synt);
4676#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004678 if (nfa_regmatch(start, &subs, &m, NULL) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004679 return 0;
4680
4681 cleanup_subexpr();
4682 if (REG_MULTI)
4683 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004684 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004686 reg_startpos[i] = subs.norm.list.multi[i].start;
4687 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 }
4689
4690 if (reg_startpos[0].lnum < 0)
4691 {
4692 reg_startpos[0].lnum = 0;
4693 reg_startpos[0].col = col;
4694 }
4695 if (reg_endpos[0].lnum < 0)
4696 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004697 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 reg_endpos[0].lnum = reglnum;
4699 reg_endpos[0].col = (int)(reginput - regline);
4700 }
4701 else
4702 /* Use line number of "\ze". */
4703 reglnum = reg_endpos[0].lnum;
4704 }
4705 else
4706 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004707 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004709 reg_startp[i] = subs.norm.list.line[i].start;
4710 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 }
4712
4713 if (reg_startp[0] == NULL)
4714 reg_startp[0] = regline + col;
4715 if (reg_endp[0] == NULL)
4716 reg_endp[0] = reginput;
4717 }
4718
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004719#ifdef FEAT_SYN_HL
4720 /* Package any found \z(...\) matches for export. Default is none. */
4721 unref_extmatch(re_extmatch_out);
4722 re_extmatch_out = NULL;
4723
4724 if (prog->reghasz == REX_SET)
4725 {
4726 int i;
4727
4728 cleanup_zsubexpr();
4729 re_extmatch_out = make_extmatch();
4730 for (i = 0; i < subs.synt.in_use; i++)
4731 {
4732 if (REG_MULTI)
4733 {
4734 struct multipos *mpos = &subs.synt.list.multi[i];
4735
4736 /* Only accept single line matches. */
4737 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
4738 re_extmatch_out->matches[i] =
4739 vim_strnsave(reg_getline(mpos->start.lnum)
4740 + mpos->start.col,
4741 mpos->end.col - mpos->start.col);
4742 }
4743 else
4744 {
4745 struct linepos *lpos = &subs.synt.list.line[i];
4746
4747 if (lpos->start != NULL && lpos->end != NULL)
4748 re_extmatch_out->matches[i] =
4749 vim_strnsave(lpos->start,
4750 (int)(lpos->end - lpos->start));
4751 }
4752 }
4753 }
4754#endif
4755
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004756 return 1 + reglnum;
4757}
4758
4759/*
4760 * Match a regexp against a string ("line" points to the string) or multiple
4761 * lines ("line" is NULL, use reg_getline()).
4762 *
4763 * Returns 0 for failure, number of lines contained in the match otherwise.
4764 */
4765 static long
4766nfa_regexec_both(line, col)
4767 char_u *line;
4768 colnr_T col; /* column to start looking for match */
4769{
4770 nfa_regprog_T *prog;
4771 long retval = 0L;
4772 int i;
4773
4774 if (REG_MULTI)
4775 {
4776 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4777 line = reg_getline((linenr_T)0); /* relative to the cursor */
4778 reg_startpos = reg_mmatch->startpos;
4779 reg_endpos = reg_mmatch->endpos;
4780 }
4781 else
4782 {
4783 prog = (nfa_regprog_T *)reg_match->regprog;
4784 reg_startp = reg_match->startp;
4785 reg_endp = reg_match->endp;
4786 }
4787
4788 /* Be paranoid... */
4789 if (prog == NULL || line == NULL)
4790 {
4791 EMSG(_(e_null));
4792 goto theend;
4793 }
4794
4795 /* If the start column is past the maximum column: no need to try. */
4796 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4797 goto theend;
4798
4799 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4800 if (prog->regflags & RF_ICASE)
4801 ireg_ic = TRUE;
4802 else if (prog->regflags & RF_NOICASE)
4803 ireg_ic = FALSE;
4804
4805#ifdef FEAT_MBYTE
4806 /* If pattern contains "\Z" overrule value of ireg_icombine */
4807 if (prog->regflags & RF_ICOMBINE)
4808 ireg_icombine = TRUE;
4809#endif
4810
4811 regline = line;
4812 reglnum = 0; /* relative to line */
4813
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004814 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004815 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004816 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004817
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004818 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004819 for (i = 0; i < nstate; ++i)
4820 {
4821 prog->state[i].id = i;
4822 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004823 }
4824
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004825 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004826
4827theend:
4828 return retval;
4829}
4830
4831/*
4832 * Compile a regular expression into internal code for the NFA matcher.
4833 * Returns the program in allocated space. Returns NULL for an error.
4834 */
4835 static regprog_T *
4836nfa_regcomp(expr, re_flags)
4837 char_u *expr;
4838 int re_flags;
4839{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004840 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004841 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004842 int *postfix;
4843
4844 if (expr == NULL)
4845 return NULL;
4846
4847#ifdef DEBUG
4848 nfa_regengine.expr = expr;
4849#endif
4850
4851 init_class_tab();
4852
4853 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4854 return NULL;
4855
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004856 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004857 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858 postfix = re2post();
4859 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004860 {
4861 /* TODO: only give this error for debugging? */
4862 if (post_ptr >= post_end)
4863 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004864 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004865 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866
4867 /*
4868 * In order to build the NFA, we parse the input regexp twice:
4869 * 1. first pass to count size (so we can allocate space)
4870 * 2. second to emit code
4871 */
4872#ifdef ENABLE_LOG
4873 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004874 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004875
4876 if (f != NULL)
4877 {
4878 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4879 fclose(f);
4880 }
4881 }
4882#endif
4883
4884 /*
4885 * PASS 1
4886 * Count number of NFA states in "nstate". Do not build the NFA.
4887 */
4888 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004889
4890 /* Space for compiled regexp */
4891 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4892 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4893 if (prog == NULL)
4894 goto fail;
4895 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004896 state_ptr = prog->state;
4897
4898 /*
4899 * PASS 2
4900 * Build the NFA
4901 */
4902 prog->start = post2nfa(postfix, post_ptr, FALSE);
4903 if (prog->start == NULL)
4904 goto fail;
4905
4906 prog->regflags = regflags;
4907 prog->engine = &nfa_regengine;
4908 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004909 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004910 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004911 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004912#ifdef ENABLE_LOG
4913 nfa_postfix_dump(expr, OK);
4914 nfa_dump(prog);
4915#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004916#ifdef FEAT_SYN_HL
4917 /* Remember whether this pattern has any \z specials in it. */
4918 prog->reghasz = re_has_z;
4919#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920
4921out:
4922 vim_free(post_start);
4923 post_start = post_ptr = post_end = NULL;
4924 state_ptr = NULL;
4925 return (regprog_T *)prog;
4926
4927fail:
4928 vim_free(prog);
4929 prog = NULL;
4930#ifdef ENABLE_LOG
4931 nfa_postfix_dump(expr, FAIL);
4932#endif
4933#ifdef DEBUG
4934 nfa_regengine.expr = NULL;
4935#endif
4936 goto out;
4937}
4938
4939
4940/*
4941 * Match a regexp against a string.
4942 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4943 * Uses curbuf for line count and 'iskeyword'.
4944 *
4945 * Return TRUE if there is a match, FALSE if not.
4946 */
4947 static int
4948nfa_regexec(rmp, line, col)
4949 regmatch_T *rmp;
4950 char_u *line; /* string to match against */
4951 colnr_T col; /* column to start looking for match */
4952{
4953 reg_match = rmp;
4954 reg_mmatch = NULL;
4955 reg_maxline = 0;
4956 reg_line_lbr = FALSE;
4957 reg_buf = curbuf;
4958 reg_win = NULL;
4959 ireg_ic = rmp->rm_ic;
4960#ifdef FEAT_MBYTE
4961 ireg_icombine = FALSE;
4962#endif
4963 ireg_maxcol = 0;
4964 return (nfa_regexec_both(line, col) != 0);
4965}
4966
4967#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4968 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4969
4970static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4971
4972/*
4973 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4974 */
4975 static int
4976nfa_regexec_nl(rmp, line, col)
4977 regmatch_T *rmp;
4978 char_u *line; /* string to match against */
4979 colnr_T col; /* column to start looking for match */
4980{
4981 reg_match = rmp;
4982 reg_mmatch = NULL;
4983 reg_maxline = 0;
4984 reg_line_lbr = TRUE;
4985 reg_buf = curbuf;
4986 reg_win = NULL;
4987 ireg_ic = rmp->rm_ic;
4988#ifdef FEAT_MBYTE
4989 ireg_icombine = FALSE;
4990#endif
4991 ireg_maxcol = 0;
4992 return (nfa_regexec_both(line, col) != 0);
4993}
4994#endif
4995
4996
4997/*
4998 * Match a regexp against multiple lines.
4999 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5000 * Uses curbuf for line count and 'iskeyword'.
5001 *
5002 * Return zero if there is no match. Return number of lines contained in the
5003 * match otherwise.
5004 *
5005 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5006 *
5007 * ! Also NOTE : match may actually be in another line. e.g.:
5008 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5009 *
5010 * +-------------------------+
5011 * |a |
5012 * |b |
5013 * |c |
5014 * | |
5015 * +-------------------------+
5016 *
5017 * then nfa_regexec_multi() returns 3. while the original
5018 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5019 *
5020 * FIXME if this behavior is not compatible.
5021 */
5022 static long
5023nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5024 regmmatch_T *rmp;
5025 win_T *win; /* window in which to search or NULL */
5026 buf_T *buf; /* buffer in which to search */
5027 linenr_T lnum; /* nr of line to start looking for match */
5028 colnr_T col; /* column to start looking for match */
5029 proftime_T *tm UNUSED; /* timeout limit or NULL */
5030{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005031 reg_match = NULL;
5032 reg_mmatch = rmp;
5033 reg_buf = buf;
5034 reg_win = win;
5035 reg_firstlnum = lnum;
5036 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5037 reg_line_lbr = FALSE;
5038 ireg_ic = rmp->rmm_ic;
5039#ifdef FEAT_MBYTE
5040 ireg_icombine = FALSE;
5041#endif
5042 ireg_maxcol = rmp->rmm_maxcol;
5043
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005044 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005045}
5046
5047#ifdef DEBUG
5048# undef ENABLE_LOG
5049#endif