blob: 225b7d42bf6b4d398c84786f219219fe544f10d2 [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 */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200181 NFA_MARK, /* Match mark */
182 NFA_MARK_GT, /* Match > mark */
183 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200184 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200185
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200186 NFA_FIRST_NL = NFA_ANY + ADD_NL,
187 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
188
189 /* Character classes [:alnum:] etc */
190 NFA_CLASS_ALNUM,
191 NFA_CLASS_ALPHA,
192 NFA_CLASS_BLANK,
193 NFA_CLASS_CNTRL,
194 NFA_CLASS_DIGIT,
195 NFA_CLASS_GRAPH,
196 NFA_CLASS_LOWER,
197 NFA_CLASS_PRINT,
198 NFA_CLASS_PUNCT,
199 NFA_CLASS_SPACE,
200 NFA_CLASS_UPPER,
201 NFA_CLASS_XDIGIT,
202 NFA_CLASS_TAB,
203 NFA_CLASS_RETURN,
204 NFA_CLASS_BACKSPACE,
205 NFA_CLASS_ESCAPE
206};
207
208/* Keep in sync with classchars. */
209static int nfa_classcodes[] = {
210 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
211 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
212 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
213 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
214 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
215 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
216 NFA_UPPER, NFA_NUPPER
217};
218
219static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
220
221/*
222 * NFA errors can be of 3 types:
223 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
224 * silently and revert the to backtracking engine.
225 * syntax_error = FALSE;
226 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
227 * The NFA engine displays an error message, and nothing else happens.
228 * syntax_error = TRUE
229 * *** Unsupported features, when the input regexp uses an operator that is not
230 * implemented in the NFA. The NFA engine fails silently, and reverts to the
231 * old backtracking engine.
232 * syntax_error = FALSE
233 * "The NFA fails" means that "compiling the regexp with the NFA fails":
234 * nfa_regcomp() returns FAIL.
235 */
236static int syntax_error = FALSE;
237
238/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200239static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200240
Bram Moolenaar428e9872013-05-30 17:05:39 +0200241/* NFA regexp \1 .. \9 encountered. */
242static int nfa_has_backref;
243
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200244#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200245/* NFA regexp has \z( ), set zsubexpr. */
246static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200247#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200248
Bram Moolenaar963fee22013-05-26 21:47:28 +0200249/* Number of sub expressions actually being used during execution. 1 if only
250 * the whole match (subexpr 0) is used. */
251static int nfa_nsubexpr;
252
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200253static int *post_start; /* holds the postfix form of r.e. */
254static int *post_end;
255static int *post_ptr;
256
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200257static int nstate; /* Number of states in the NFA. Also used when
258 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200259static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaar307aa162013-06-02 16:34:21 +0200261/* If not NULL match must end at this position */
262static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200263
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200264/* listid is global, so that it increases on recursive calls to
265 * nfa_regmatch(), which means we don't have to clear the lastlist field of
266 * all the states. */
267static int nfa_listid;
268static int nfa_alt_listid;
269
270/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
271static int nfa_ll_index = 0;
272
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
274static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
275static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276static int nfa_regatom __ARGS((void));
277static int nfa_regpiece __ARGS((void));
278static int nfa_regconcat __ARGS((void));
279static int nfa_regbranch __ARGS((void));
280static int nfa_reg __ARGS((int paren));
281#ifdef DEBUG
282static void nfa_set_code __ARGS((int c));
283static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200284static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
285static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286static void nfa_dump __ARGS((nfa_regprog_T *prog));
287#endif
288static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200289static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
291static int check_char_class __ARGS((int class, int c));
292static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200293static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
294static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200295static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200296static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
298static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
299static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
300static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
301
302/* helper functions used when doing re2post() ... regatom() parsing */
303#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200304 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305 return FAIL; \
306 *post_ptr++ = c; \
307 } while (0)
308
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200309/*
310 * Initialize internal variables before NFA compilation.
311 * Return OK on success, FAIL otherwise.
312 */
313 static int
314nfa_regcomp_start(expr, re_flags)
315 char_u *expr;
316 int re_flags; /* see vim_regcomp() */
317{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200318 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200319 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200320
321 nstate = 0;
322 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200323 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200324 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200325
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200326 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200327 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200328 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200329
330 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200331 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200332
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200333 post_start = (int *)lalloc(postfix_size, TRUE);
334 if (post_start == NULL)
335 return FAIL;
336 vim_memset(post_start, 0, postfix_size);
337 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200338 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200340 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200342 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200343 regcomp_start(expr, re_flags);
344
345 return OK;
346}
347
348/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200349 * Allocate more space for post_start. Called when
350 * running above the estimated number of states.
351 */
352 static int
353realloc_post_list()
354{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200355 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200356 int new_max = nstate_max + 1000;
357 int *new_start;
358 int *old_start;
359
360 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
361 if (new_start == NULL)
362 return FAIL;
363 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
364 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
365 old_start = post_start;
366 post_start = new_start;
367 post_ptr = new_start + (post_ptr - old_start);
368 post_end = post_start + new_max;
369 vim_free(old_start);
370 return OK;
371}
372
373/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200374 * Search between "start" and "end" and try to recognize a
375 * character class in expanded form. For example [0-9].
376 * On success, return the id the character class to be emitted.
377 * On failure, return 0 (=FAIL)
378 * Start points to the first char of the range, while end should point
379 * to the closing brace.
380 */
381 static int
382nfa_recognize_char_class(start, end, extra_newl)
383 char_u *start;
384 char_u *end;
385 int extra_newl;
386{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200387# define CLASS_not 0x80
388# define CLASS_af 0x40
389# define CLASS_AF 0x20
390# define CLASS_az 0x10
391# define CLASS_AZ 0x08
392# define CLASS_o7 0x04
393# define CLASS_o9 0x02
394# define CLASS_underscore 0x01
395
396 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200397 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200398 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200399
400 if (extra_newl == TRUE)
401 newl = TRUE;
402
403 if (*end != ']')
404 return FAIL;
405 p = start;
406 if (*p == '^')
407 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200408 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200409 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200410 }
411
412 while (p < end)
413 {
414 if (p + 2 < end && *(p + 1) == '-')
415 {
416 switch (*p)
417 {
418 case '0':
419 if (*(p + 2) == '9')
420 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200421 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200422 break;
423 }
424 else
425 if (*(p + 2) == '7')
426 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200427 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200428 break;
429 }
430 case 'a':
431 if (*(p + 2) == 'z')
432 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200433 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200434 break;
435 }
436 else
437 if (*(p + 2) == 'f')
438 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200439 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200440 break;
441 }
442 case 'A':
443 if (*(p + 2) == 'Z')
444 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200445 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200446 break;
447 }
448 else
449 if (*(p + 2) == 'F')
450 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200451 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200452 break;
453 }
454 /* FALLTHROUGH */
455 default:
456 return FAIL;
457 }
458 p += 3;
459 }
460 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
461 {
462 newl = TRUE;
463 p += 2;
464 }
465 else if (*p == '_')
466 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200467 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200468 p ++;
469 }
470 else if (*p == '\n')
471 {
472 newl = TRUE;
473 p ++;
474 }
475 else
476 return FAIL;
477 } /* while (p < end) */
478
479 if (p != end)
480 return FAIL;
481
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200482 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200483 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200484
485 switch (config)
486 {
487 case CLASS_o9:
488 return extra_newl + NFA_DIGIT;
489 case CLASS_not | CLASS_o9:
490 return extra_newl + NFA_NDIGIT;
491 case CLASS_af | CLASS_AF | CLASS_o9:
492 return extra_newl + NFA_HEX;
493 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
494 return extra_newl + NFA_NHEX;
495 case CLASS_o7:
496 return extra_newl + NFA_OCTAL;
497 case CLASS_not | CLASS_o7:
498 return extra_newl + NFA_NOCTAL;
499 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
500 return extra_newl + NFA_WORD;
501 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
502 return extra_newl + NFA_NWORD;
503 case CLASS_az | CLASS_AZ | CLASS_underscore:
504 return extra_newl + NFA_HEAD;
505 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
506 return extra_newl + NFA_NHEAD;
507 case CLASS_az | CLASS_AZ:
508 return extra_newl + NFA_ALPHA;
509 case CLASS_not | CLASS_az | CLASS_AZ:
510 return extra_newl + NFA_NALPHA;
511 case CLASS_az:
512 return extra_newl + NFA_LOWER;
513 case CLASS_not | CLASS_az:
514 return extra_newl + NFA_NLOWER;
515 case CLASS_AZ:
516 return extra_newl + NFA_UPPER;
517 case CLASS_not | CLASS_AZ:
518 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200519 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200520 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200521}
522
523/*
524 * Produce the bytes for equivalence class "c".
525 * Currently only handles latin1, latin9 and utf-8.
526 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
527 * equivalent to 'a OR b OR c'
528 *
529 * NOTE! When changing this function, also update reg_equi_class()
530 */
531 static int
532nfa_emit_equi_class(c, neg)
533 int c;
534 int neg;
535{
536 int first = TRUE;
537 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
538#define EMIT2(c) \
539 EMIT(c); \
540 if (neg == TRUE) { \
541 EMIT(NFA_NOT); \
542 } \
543 if (first == FALSE) \
544 EMIT(glue); \
545 else \
546 first = FALSE; \
547
548#ifdef FEAT_MBYTE
549 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
550 || STRCMP(p_enc, "iso-8859-15") == 0)
551#endif
552 {
553 switch (c)
554 {
555 case 'A': case '\300': case '\301': case '\302':
556 case '\303': case '\304': case '\305':
557 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
558 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
559 EMIT2('\305');
560 return OK;
561
562 case 'C': case '\307':
563 EMIT2('C'); EMIT2('\307');
564 return OK;
565
566 case 'E': case '\310': case '\311': case '\312': case '\313':
567 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
568 EMIT2('\312'); EMIT2('\313');
569 return OK;
570
571 case 'I': case '\314': case '\315': case '\316': case '\317':
572 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
573 EMIT2('\316'); EMIT2('\317');
574 return OK;
575
576 case 'N': case '\321':
577 EMIT2('N'); EMIT2('\321');
578 return OK;
579
580 case 'O': case '\322': case '\323': case '\324': case '\325':
581 case '\326':
582 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
583 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
584 return OK;
585
586 case 'U': case '\331': case '\332': case '\333': case '\334':
587 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
588 EMIT2('\333'); EMIT2('\334');
589 return OK;
590
591 case 'Y': case '\335':
592 EMIT2('Y'); EMIT2('\335');
593 return OK;
594
595 case 'a': case '\340': case '\341': case '\342':
596 case '\343': case '\344': case '\345':
597 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
598 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
599 EMIT2('\345');
600 return OK;
601
602 case 'c': case '\347':
603 EMIT2('c'); EMIT2('\347');
604 return OK;
605
606 case 'e': case '\350': case '\351': case '\352': case '\353':
607 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
608 EMIT2('\352'); EMIT2('\353');
609 return OK;
610
611 case 'i': case '\354': case '\355': case '\356': case '\357':
612 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
613 EMIT2('\356'); EMIT2('\357');
614 return OK;
615
616 case 'n': case '\361':
617 EMIT2('n'); EMIT2('\361');
618 return OK;
619
620 case 'o': case '\362': case '\363': case '\364': case '\365':
621 case '\366':
622 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
623 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
624 return OK;
625
626 case 'u': case '\371': case '\372': case '\373': case '\374':
627 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
628 EMIT2('\373'); EMIT2('\374');
629 return OK;
630
631 case 'y': case '\375': case '\377':
632 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
633 return OK;
634
635 default:
636 return FAIL;
637 }
638 }
639
640 EMIT(c);
641 return OK;
642#undef EMIT2
643}
644
645/*
646 * Code to parse regular expression.
647 *
648 * We try to reuse parsing functions in regexp.c to
649 * minimize surprise and keep the syntax consistent.
650 */
651
652/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200653 * Parse the lowest level.
654 *
655 * An atom can be one of a long list of items. Many atoms match one character
656 * in the text. It is often an ordinary character or a character class.
657 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
658 * is only for syntax highlighting.
659 *
660 * atom ::= ordinary-atom
661 * or \( pattern \)
662 * or \%( pattern \)
663 * or \z( pattern \)
664 */
665 static int
666nfa_regatom()
667{
668 int c;
669 int charclass;
670 int equiclass;
671 int collclass;
672 int got_coll_char;
673 char_u *p;
674 char_u *endp;
675#ifdef FEAT_MBYTE
676 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677#endif
678 int extra = 0;
679 int first;
680 int emit_range;
681 int negated;
682 int result;
683 int startc = -1;
684 int endc = -1;
685 int oldstartc = -1;
686 int cpo_lit; /* 'cpoptions' contains 'l' flag */
687 int cpo_bsl; /* 'cpoptions' contains '\' flag */
688 int glue; /* ID that will "glue" nodes together */
689
690 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
691 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
692
693 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200694 switch (c)
695 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200696 case NUL:
697 syntax_error = TRUE;
698 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
699
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200700 case Magic('^'):
701 EMIT(NFA_BOL);
702 break;
703
704 case Magic('$'):
705 EMIT(NFA_EOL);
706#if defined(FEAT_SYN_HL) || defined(PROTO)
707 had_eol = TRUE;
708#endif
709 break;
710
711 case Magic('<'):
712 EMIT(NFA_BOW);
713 break;
714
715 case Magic('>'):
716 EMIT(NFA_EOW);
717 break;
718
719 case Magic('_'):
720 c = no_Magic(getchr());
721 if (c == '^') /* "\_^" is start-of-line */
722 {
723 EMIT(NFA_BOL);
724 break;
725 }
726 if (c == '$') /* "\_$" is end-of-line */
727 {
728 EMIT(NFA_EOL);
729#if defined(FEAT_SYN_HL) || defined(PROTO)
730 had_eol = TRUE;
731#endif
732 break;
733 }
734
735 extra = ADD_NL;
736
737 /* "\_[" is collection plus newline */
738 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200739 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200740
741 /* "\_x" is character class plus newline */
742 /*FALLTHROUGH*/
743
744 /*
745 * Character classes.
746 */
747 case Magic('.'):
748 case Magic('i'):
749 case Magic('I'):
750 case Magic('k'):
751 case Magic('K'):
752 case Magic('f'):
753 case Magic('F'):
754 case Magic('p'):
755 case Magic('P'):
756 case Magic('s'):
757 case Magic('S'):
758 case Magic('d'):
759 case Magic('D'):
760 case Magic('x'):
761 case Magic('X'):
762 case Magic('o'):
763 case Magic('O'):
764 case Magic('w'):
765 case Magic('W'):
766 case Magic('h'):
767 case Magic('H'):
768 case Magic('a'):
769 case Magic('A'):
770 case Magic('l'):
771 case Magic('L'):
772 case Magic('u'):
773 case Magic('U'):
774 p = vim_strchr(classchars, no_Magic(c));
775 if (p == NULL)
776 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200777 EMSGN("INTERNAL: Unknown character class char: %ld", c);
778 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200779 }
780#ifdef FEAT_MBYTE
781 /* When '.' is followed by a composing char ignore the dot, so that
782 * the composing char is matched here. */
783 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
784 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200785 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200786 c = getchr();
787 goto nfa_do_multibyte;
788 }
789#endif
790 EMIT(nfa_classcodes[p - classchars]);
791 if (extra == ADD_NL)
792 {
793 EMIT(NFA_NEWL);
794 EMIT(NFA_OR);
795 regflags |= RF_HASNL;
796 }
797 break;
798
799 case Magic('n'):
800 if (reg_string)
801 /* In a string "\n" matches a newline character. */
802 EMIT(NL);
803 else
804 {
805 /* In buffer text "\n" matches the end of a line. */
806 EMIT(NFA_NEWL);
807 regflags |= RF_HASNL;
808 }
809 break;
810
811 case Magic('('):
812 if (nfa_reg(REG_PAREN) == FAIL)
813 return FAIL; /* cascaded error */
814 break;
815
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 case Magic('|'):
817 case Magic('&'):
818 case Magic(')'):
819 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200820 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 return FAIL;
822
823 case Magic('='):
824 case Magic('?'):
825 case Magic('+'):
826 case Magic('@'):
827 case Magic('*'):
828 case Magic('{'):
829 /* these should follow an atom, not form an atom */
830 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200831 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200832 return FAIL;
833
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200834 case Magic('~'):
835 {
836 char_u *lp;
837
838 /* Previous substitute pattern.
839 * Generated as "\%(pattern\)". */
840 if (reg_prev_sub == NULL)
841 {
842 EMSG(_(e_nopresub));
843 return FAIL;
844 }
845 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
846 {
847 EMIT(PTR2CHAR(lp));
848 if (lp != reg_prev_sub)
849 EMIT(NFA_CONCAT);
850 }
851 EMIT(NFA_NOPEN);
852 break;
853 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200854
Bram Moolenaar428e9872013-05-30 17:05:39 +0200855 case Magic('1'):
856 case Magic('2'):
857 case Magic('3'):
858 case Magic('4'):
859 case Magic('5'):
860 case Magic('6'):
861 case Magic('7'):
862 case Magic('8'):
863 case Magic('9'):
864 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
865 nfa_has_backref = TRUE;
866 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867
868 case Magic('z'):
869 c = no_Magic(getchr());
870 switch (c)
871 {
872 case 's':
873 EMIT(NFA_ZSTART);
874 break;
875 case 'e':
876 EMIT(NFA_ZEND);
877 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200878 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200879#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 case '1':
881 case '2':
882 case '3':
883 case '4':
884 case '5':
885 case '6':
886 case '7':
887 case '8':
888 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200889 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200890 if (reg_do_extmatch != REX_USE)
891 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200892 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
893 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +0200894 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200895 re_has_z = REX_USE;
896 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200897 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200898 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200899 if (reg_do_extmatch != REX_SET)
900 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200901 if (nfa_reg(REG_ZPAREN) == FAIL)
902 return FAIL; /* cascaded error */
903 re_has_z = REX_SET;
904 break;
905#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906 default:
907 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200908 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200909 no_Magic(c));
910 return FAIL;
911 }
912 break;
913
914 case Magic('%'):
915 c = no_Magic(getchr());
916 switch (c)
917 {
918 /* () without a back reference */
919 case '(':
920 if (nfa_reg(REG_NPAREN) == FAIL)
921 return FAIL;
922 EMIT(NFA_NOPEN);
923 break;
924
925 case 'd': /* %d123 decimal */
926 case 'o': /* %o123 octal */
927 case 'x': /* %xab hex 2 */
928 case 'u': /* %uabcd hex 4 */
929 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200930 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200931 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200932
Bram Moolenaar47196582013-05-25 22:04:23 +0200933 switch (c)
934 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200935 case 'd': nr = getdecchrs(); break;
936 case 'o': nr = getoctchrs(); break;
937 case 'x': nr = gethexchrs(2); break;
938 case 'u': nr = gethexchrs(4); break;
939 case 'U': nr = gethexchrs(8); break;
940 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200941 }
942
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200943 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200944 EMSG2_RET_FAIL(
945 _("E678: Invalid character after %s%%[dxouU]"),
946 reg_magic == MAGIC_ALL);
947 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200948 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200949 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200950 break;
951
952 /* Catch \%^ and \%$ regardless of where they appear in the
953 * pattern -- regardless of whether or not it makes sense. */
954 case '^':
955 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200956 break;
957
958 case '$':
959 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200960 break;
961
962 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200963 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200964 break;
965
966 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200967 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200968 break;
969
970 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200971 /* TODO: \%[abc] not supported yet */
972 return FAIL;
973
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200974 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200975 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200976 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200977 int cmp = c;
978
979 if (c == '<' || c == '>')
980 c = getchr();
981 while (VIM_ISDIGIT(c))
982 {
983 n = n * 10 + (c - '0');
984 c = getchr();
985 }
986 if (c == 'l' || c == 'c' || c == 'v')
987 {
988 EMIT(n);
989 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200990 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200991 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200992 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200993 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200994 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200995 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200996 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200997 else
Bram Moolenaar044aa292013-06-04 21:27:38 +0200998 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200999 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001000 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001001 break;
1002 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001003 else if (c == '\'' && n == 0)
1004 {
1005 /* \%'m \%<'m \%>'m */
1006 EMIT(getchr());
1007 EMIT(cmp == '<' ? NFA_MARK_LT :
1008 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
1009 break;
1010 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001011 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001012 syntax_error = TRUE;
1013 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1014 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001015 return FAIL;
1016 }
1017 break;
1018
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001019 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001020collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001021 /*
1022 * Glue is emitted between several atoms from the [].
1023 * It is either NFA_OR, or NFA_CONCAT.
1024 *
1025 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1026 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1027 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1028 * notation)
1029 *
1030 */
1031
1032
1033/* Emit negation atoms, if needed.
1034 * The CONCAT below merges the NOT with the previous node. */
1035#define TRY_NEG() \
1036 if (negated == TRUE) \
1037 { \
1038 EMIT(NFA_NOT); \
1039 }
1040
1041/* Emit glue between important nodes : CONCAT or OR. */
1042#define EMIT_GLUE() \
1043 if (first == FALSE) \
1044 EMIT(glue); \
1045 else \
1046 first = FALSE;
1047
1048 p = regparse;
1049 endp = skip_anyof(p);
1050 if (*endp == ']')
1051 {
1052 /*
1053 * Try to reverse engineer character classes. For example,
1054 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1055 * and perform the necessary substitutions in the NFA.
1056 */
1057 result = nfa_recognize_char_class(regparse, endp,
1058 extra == ADD_NL);
1059 if (result != FAIL)
1060 {
1061 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1062 EMIT(result);
1063 else /* must be char class + newline */
1064 {
1065 EMIT(result - ADD_NL);
1066 EMIT(NFA_NEWL);
1067 EMIT(NFA_OR);
1068 }
1069 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001070 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001071 return OK;
1072 }
1073 /*
1074 * Failed to recognize a character class. Use the simple
1075 * version that turns [abc] into 'a' OR 'b' OR 'c'
1076 */
1077 startc = endc = oldstartc = -1;
1078 first = TRUE; /* Emitting first atom in this sequence? */
1079 negated = FALSE;
1080 glue = NFA_OR;
1081 if (*regparse == '^') /* negated range */
1082 {
1083 negated = TRUE;
1084 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001085 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 }
1087 if (*regparse == '-')
1088 {
1089 startc = '-';
1090 EMIT(startc);
1091 TRY_NEG();
1092 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001093 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001094 }
1095 /* Emit the OR branches for each character in the [] */
1096 emit_range = FALSE;
1097 while (regparse < endp)
1098 {
1099 oldstartc = startc;
1100 startc = -1;
1101 got_coll_char = FALSE;
1102 if (*regparse == '[')
1103 {
1104 /* Check for [: :], [= =], [. .] */
1105 equiclass = collclass = 0;
1106 charclass = get_char_class(&regparse);
1107 if (charclass == CLASS_NONE)
1108 {
1109 equiclass = get_equi_class(&regparse);
1110 if (equiclass == 0)
1111 collclass = get_coll_element(&regparse);
1112 }
1113
1114 /* Character class like [:alpha:] */
1115 if (charclass != CLASS_NONE)
1116 {
1117 switch (charclass)
1118 {
1119 case CLASS_ALNUM:
1120 EMIT(NFA_CLASS_ALNUM);
1121 break;
1122 case CLASS_ALPHA:
1123 EMIT(NFA_CLASS_ALPHA);
1124 break;
1125 case CLASS_BLANK:
1126 EMIT(NFA_CLASS_BLANK);
1127 break;
1128 case CLASS_CNTRL:
1129 EMIT(NFA_CLASS_CNTRL);
1130 break;
1131 case CLASS_DIGIT:
1132 EMIT(NFA_CLASS_DIGIT);
1133 break;
1134 case CLASS_GRAPH:
1135 EMIT(NFA_CLASS_GRAPH);
1136 break;
1137 case CLASS_LOWER:
1138 EMIT(NFA_CLASS_LOWER);
1139 break;
1140 case CLASS_PRINT:
1141 EMIT(NFA_CLASS_PRINT);
1142 break;
1143 case CLASS_PUNCT:
1144 EMIT(NFA_CLASS_PUNCT);
1145 break;
1146 case CLASS_SPACE:
1147 EMIT(NFA_CLASS_SPACE);
1148 break;
1149 case CLASS_UPPER:
1150 EMIT(NFA_CLASS_UPPER);
1151 break;
1152 case CLASS_XDIGIT:
1153 EMIT(NFA_CLASS_XDIGIT);
1154 break;
1155 case CLASS_TAB:
1156 EMIT(NFA_CLASS_TAB);
1157 break;
1158 case CLASS_RETURN:
1159 EMIT(NFA_CLASS_RETURN);
1160 break;
1161 case CLASS_BACKSPACE:
1162 EMIT(NFA_CLASS_BACKSPACE);
1163 break;
1164 case CLASS_ESCAPE:
1165 EMIT(NFA_CLASS_ESCAPE);
1166 break;
1167 }
1168 TRY_NEG();
1169 EMIT_GLUE();
1170 continue;
1171 }
1172 /* Try equivalence class [=a=] and the like */
1173 if (equiclass != 0)
1174 {
1175 result = nfa_emit_equi_class(equiclass, negated);
1176 if (result == FAIL)
1177 {
1178 /* should never happen */
1179 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1180 }
1181 EMIT_GLUE();
1182 continue;
1183 }
1184 /* Try collating class like [. .] */
1185 if (collclass != 0)
1186 {
1187 startc = collclass; /* allow [.a.]-x as a range */
1188 /* Will emit the proper atom at the end of the
1189 * while loop. */
1190 }
1191 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001192 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1193 * start character. */
1194 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001195 {
1196 emit_range = TRUE;
1197 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001198 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001199 continue; /* reading the end of the range */
1200 }
1201
1202 /* Now handle simple and escaped characters.
1203 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1204 * accepts "\t", "\e", etc., but only when the 'l' flag in
1205 * 'cpoptions' is not included.
1206 * Posix doesn't recognize backslash at all.
1207 */
1208 if (*regparse == '\\'
1209 && !cpo_bsl
1210 && regparse + 1 <= endp
1211 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1212 || (!cpo_lit
1213 && vim_strchr(REGEXP_ABBR, regparse[1])
1214 != NULL)
1215 )
1216 )
1217 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001218 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001220 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 startc = reg_string ? NL : NFA_NEWL;
1222 else
1223 if (*regparse == 'd'
1224 || *regparse == 'o'
1225 || *regparse == 'x'
1226 || *regparse == 'u'
1227 || *regparse == 'U'
1228 )
1229 {
1230 /* TODO(RE) This needs more testing */
1231 startc = coll_get_char();
1232 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001233 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 }
1235 else
1236 {
1237 /* \r,\t,\e,\b */
1238 startc = backslash_trans(*regparse);
1239 }
1240 }
1241
1242 /* Normal printable char */
1243 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001244 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245
1246 /* Previous char was '-', so this char is end of range. */
1247 if (emit_range)
1248 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001249 endc = startc;
1250 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 if (startc > endc)
1252 EMSG_RET_FAIL(_(e_invrange));
1253#ifdef FEAT_MBYTE
1254 if (has_mbyte && ((*mb_char2len)(startc) > 1
1255 || (*mb_char2len)(endc) > 1))
1256 {
1257 if (endc > startc + 256)
1258 EMSG_RET_FAIL(_(e_invrange));
1259 /* Emit the range. "startc" was already emitted, so
1260 * skip it. */
1261 for (c = startc + 1; c <= endc; c++)
1262 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001263 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 TRY_NEG();
1265 EMIT_GLUE();
1266 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267 }
1268 else
1269#endif
1270 {
1271#ifdef EBCDIC
1272 int alpha_only = FALSE;
1273
1274 /* for alphabetical range skip the gaps
1275 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1276 if (isalpha(startc) && isalpha(endc))
1277 alpha_only = TRUE;
1278#endif
1279 /* Emit the range. "startc" was already emitted, so
1280 * skip it. */
1281 for (c = startc + 1; c <= endc; c++)
1282#ifdef EBCDIC
1283 if (!alpha_only || isalpha(startc))
1284#endif
1285 {
1286 EMIT(c);
1287 TRY_NEG();
1288 EMIT_GLUE();
1289 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001290 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001291 emit_range = FALSE;
1292 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293 }
1294 else
1295 {
1296 /*
1297 * This char (startc) is not part of a range. Just
1298 * emit it.
1299 *
1300 * Normally, simply emit startc. But if we get char
1301 * code=0 from a collating char, then replace it with
1302 * 0x0a.
1303 *
1304 * This is needed to completely mimic the behaviour of
1305 * the backtracking engine.
1306 */
1307 if (got_coll_char == TRUE && startc == 0)
1308 EMIT(0x0a);
1309 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001310 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001311 TRY_NEG();
1312 EMIT_GLUE();
1313 }
1314
Bram Moolenaar51a29832013-05-28 22:30:35 +02001315 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001316 } /* while (p < endp) */
1317
Bram Moolenaar51a29832013-05-28 22:30:35 +02001318 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 if (*regparse == '-') /* if last, '-' is just a char */
1320 {
1321 EMIT('-');
1322 TRY_NEG();
1323 EMIT_GLUE();
1324 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001325 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001327 /* skip the trailing ] */
1328 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001329 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001330 if (negated == TRUE)
1331 {
1332 /* Mark end of negated char range */
1333 EMIT(NFA_END_NEG_RANGE);
1334 EMIT(NFA_CONCAT);
1335 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001336
1337 /* \_[] also matches \n but it's not negated */
1338 if (extra == ADD_NL)
1339 {
1340 EMIT(reg_string ? NL : NFA_NEWL);
1341 EMIT(NFA_OR);
1342 }
1343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001345 } /* if exists closing ] */
1346
1347 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 {
1349 syntax_error = TRUE;
1350 EMSG_RET_FAIL(_(e_missingbracket));
1351 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001352 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001354 default:
1355 {
1356#ifdef FEAT_MBYTE
1357 int plen;
1358
1359nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001360 /* plen is length of current char with composing chars */
1361 if (enc_utf8 && ((*mb_char2len)(c)
1362 != (plen = (*mb_ptr2len)(old_regparse))
1363 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001365 int i = 0;
1366
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001367 /* A base character plus composing characters, or just one
1368 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001369 * This requires creating a separate atom as if enclosing
1370 * the characters in (), where NFA_COMPOSING is the ( and
1371 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001372 * building the postfix form, not the NFA itself;
1373 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001374 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001375 for (;;)
1376 {
1377 EMIT(c);
1378 if (i > 0)
1379 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001380 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001381 break;
1382 c = utf_ptr2char(old_regparse + i);
1383 }
1384 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385 regparse = old_regparse + plen;
1386 }
1387 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001388#endif
1389 {
1390 c = no_Magic(c);
1391 EMIT(c);
1392 }
1393 return OK;
1394 }
1395 }
1396
1397#undef TRY_NEG
1398#undef EMIT_GLUE
1399
1400 return OK;
1401}
1402
1403/*
1404 * Parse something followed by possible [*+=].
1405 *
1406 * A piece is an atom, possibly followed by a multi, an indication of how many
1407 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1408 * characters: "", "a", "aa", etc.
1409 *
1410 * piece ::= atom
1411 * or atom multi
1412 */
1413 static int
1414nfa_regpiece()
1415{
1416 int i;
1417 int op;
1418 int ret;
1419 long minval, maxval;
1420 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001421 parse_state_T old_state;
1422 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001424 int old_post_pos;
1425 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001426 int quest;
1427
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001428 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1429 * next. */
1430 save_parse_state(&old_state);
1431
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001432 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001433 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434
1435 ret = nfa_regatom();
1436 if (ret == FAIL)
1437 return FAIL; /* cascaded error */
1438
1439 op = peekchr();
1440 if (re_multi_type(op) == NOT_MULTI)
1441 return OK;
1442
1443 skipchr();
1444 switch (op)
1445 {
1446 case Magic('*'):
1447 EMIT(NFA_STAR);
1448 break;
1449
1450 case Magic('+'):
1451 /*
1452 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1453 * first and only submatch would be "aaa". But the backtracking
1454 * engine interprets the plus as "try matching one more time", and
1455 * a* matches a second time at the end of the input, the empty
1456 * string.
1457 * The submatch will the empty string.
1458 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001459 * In order to be consistent with the old engine, we replace
1460 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001462 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001463 curchr = -1;
1464 if (nfa_regatom() == FAIL)
1465 return FAIL;
1466 EMIT(NFA_STAR);
1467 EMIT(NFA_CONCAT);
1468 skipchr(); /* skip the \+ */
1469 break;
1470
1471 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001472 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001473 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001474 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 switch(op)
1476 {
1477 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001478 /* \@= */
1479 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001480 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001481 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001482 /* \@! */
1483 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001484 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001486 op = no_Magic(getchr());
1487 if (op == '=')
1488 /* \@<= */
1489 i = NFA_PREV_ATOM_JUST_BEFORE;
1490 else if (op == '!')
1491 /* \@<! */
1492 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1493 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001495 /* \@> Not supported yet */
1496 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 return FAIL;
1498 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001499 if (i == 0)
1500 {
1501 syntax_error = TRUE;
1502 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1503 return FAIL;
1504 }
1505 EMIT(i);
1506 if (i == NFA_PREV_ATOM_JUST_BEFORE
1507 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1508 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 break;
1510
1511 case Magic('?'):
1512 case Magic('='):
1513 EMIT(NFA_QUEST);
1514 break;
1515
1516 case Magic('{'):
1517 /* a{2,5} will expand to 'aaa?a?a?'
1518 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1519 * version of '?'
1520 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1521 * parenthesis have the same id
1522 */
1523
1524 greedy = TRUE;
1525 c2 = peekchr();
1526 if (c2 == '-' || c2 == Magic('-'))
1527 {
1528 skipchr();
1529 greedy = FALSE;
1530 }
1531 if (!read_limits(&minval, &maxval))
1532 {
1533 syntax_error = TRUE;
1534 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1535 }
1536 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1537 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001538 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001540 if (greedy)
1541 /* \{}, \{0,} */
1542 EMIT(NFA_STAR);
1543 else
1544 /* \{-}, \{-0,} */
1545 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001546 break;
1547 }
1548
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549 /* Special case: x{0} or x{-0} */
1550 if (maxval == 0)
1551 {
1552 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001553 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001554 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1555 EMIT(NFA_SKIP_CHAR);
1556 return OK;
1557 }
1558
1559 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001560 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001561 /* Save parse state after the repeated atom and the \{} */
1562 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1565 for (i = 0; i < maxval; i++)
1566 {
1567 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001568 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001569 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001570 if (nfa_regatom() == FAIL)
1571 return FAIL;
1572 /* after "minval" times, atoms are optional */
1573 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001574 {
1575 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001576 {
1577 if (greedy)
1578 EMIT(NFA_STAR);
1579 else
1580 EMIT(NFA_STAR_NONGREEDY);
1581 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001582 else
1583 EMIT(quest);
1584 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001585 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001586 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001587 if (i + 1 > minval && maxval == MAX_LIMIT)
1588 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001589 }
1590
1591 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001592 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 curchr = -1;
1594
1595 break;
1596
1597
1598 default:
1599 break;
1600 } /* end switch */
1601
1602 if (re_multi_type(peekchr()) != NOT_MULTI)
1603 {
1604 /* Can't have a multi follow a multi. */
1605 syntax_error = TRUE;
1606 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1607 }
1608
1609 return OK;
1610}
1611
1612/*
1613 * Parse one or more pieces, concatenated. It matches a match for the
1614 * first piece, followed by a match for the second piece, etc. Example:
1615 * "f[0-9]b", first matches "f", then a digit and then "b".
1616 *
1617 * concat ::= piece
1618 * or piece piece
1619 * or piece piece piece
1620 * etc.
1621 */
1622 static int
1623nfa_regconcat()
1624{
1625 int cont = TRUE;
1626 int first = TRUE;
1627
1628 while (cont)
1629 {
1630 switch (peekchr())
1631 {
1632 case NUL:
1633 case Magic('|'):
1634 case Magic('&'):
1635 case Magic(')'):
1636 cont = FALSE;
1637 break;
1638
1639 case Magic('Z'):
1640#ifdef FEAT_MBYTE
1641 regflags |= RF_ICOMBINE;
1642#endif
1643 skipchr_keepstart();
1644 break;
1645 case Magic('c'):
1646 regflags |= RF_ICASE;
1647 skipchr_keepstart();
1648 break;
1649 case Magic('C'):
1650 regflags |= RF_NOICASE;
1651 skipchr_keepstart();
1652 break;
1653 case Magic('v'):
1654 reg_magic = MAGIC_ALL;
1655 skipchr_keepstart();
1656 curchr = -1;
1657 break;
1658 case Magic('m'):
1659 reg_magic = MAGIC_ON;
1660 skipchr_keepstart();
1661 curchr = -1;
1662 break;
1663 case Magic('M'):
1664 reg_magic = MAGIC_OFF;
1665 skipchr_keepstart();
1666 curchr = -1;
1667 break;
1668 case Magic('V'):
1669 reg_magic = MAGIC_NONE;
1670 skipchr_keepstart();
1671 curchr = -1;
1672 break;
1673
1674 default:
1675 if (nfa_regpiece() == FAIL)
1676 return FAIL;
1677 if (first == FALSE)
1678 EMIT(NFA_CONCAT);
1679 else
1680 first = FALSE;
1681 break;
1682 }
1683 }
1684
1685 return OK;
1686}
1687
1688/*
1689 * Parse a branch, one or more concats, separated by "\&". It matches the
1690 * last concat, but only if all the preceding concats also match at the same
1691 * position. Examples:
1692 * "foobeep\&..." matches "foo" in "foobeep".
1693 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1694 *
1695 * branch ::= concat
1696 * or concat \& concat
1697 * or concat \& concat \& concat
1698 * etc.
1699 */
1700 static int
1701nfa_regbranch()
1702{
1703 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001704 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001705
Bram Moolenaar16299b52013-05-30 18:45:23 +02001706 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707
1708 /* First branch, possibly the only one */
1709 if (nfa_regconcat() == FAIL)
1710 return FAIL;
1711
1712 ch = peekchr();
1713 /* Try next concats */
1714 while (ch == Magic('&'))
1715 {
1716 skipchr();
1717 EMIT(NFA_NOPEN);
1718 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001719 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720 if (nfa_regconcat() == FAIL)
1721 return FAIL;
1722 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001723 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001724 EMIT(NFA_SKIP_CHAR);
1725 EMIT(NFA_CONCAT);
1726 ch = peekchr();
1727 }
1728
1729 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001730 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001731 EMIT(NFA_SKIP_CHAR);
1732
1733 return OK;
1734}
1735
1736/*
1737 * Parse a pattern, one or more branches, separated by "\|". It matches
1738 * anything that matches one of the branches. Example: "foo\|beep" matches
1739 * "foo" and matches "beep". If more than one branch matches, the first one
1740 * is used.
1741 *
1742 * pattern ::= branch
1743 * or branch \| branch
1744 * or branch \| branch \| branch
1745 * etc.
1746 */
1747 static int
1748nfa_reg(paren)
1749 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1750{
1751 int parno = 0;
1752
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001753 if (paren == REG_PAREN)
1754 {
1755 if (regnpar >= NSUBEXP) /* Too many `(' */
1756 {
1757 syntax_error = TRUE;
1758 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1759 }
1760 parno = regnpar++;
1761 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001762#ifdef FEAT_SYN_HL
1763 else if (paren == REG_ZPAREN)
1764 {
1765 /* Make a ZOPEN node. */
1766 if (regnzpar >= NSUBEXP)
1767 {
1768 syntax_error = TRUE;
1769 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1770 }
1771 parno = regnzpar++;
1772 }
1773#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001774
1775 if (nfa_regbranch() == FAIL)
1776 return FAIL; /* cascaded error */
1777
1778 while (peekchr() == Magic('|'))
1779 {
1780 skipchr();
1781 if (nfa_regbranch() == FAIL)
1782 return FAIL; /* cascaded error */
1783 EMIT(NFA_OR);
1784 }
1785
1786 /* Check for proper termination. */
1787 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1788 {
1789 syntax_error = TRUE;
1790 if (paren == REG_NPAREN)
1791 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1792 else
1793 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1794 }
1795 else if (paren == REG_NOPAREN && peekchr() != NUL)
1796 {
1797 syntax_error = TRUE;
1798 if (peekchr() == Magic(')'))
1799 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1800 else
1801 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1802 }
1803 /*
1804 * Here we set the flag allowing back references to this set of
1805 * parentheses.
1806 */
1807 if (paren == REG_PAREN)
1808 {
1809 had_endbrace[parno] = TRUE; /* have seen the close paren */
1810 EMIT(NFA_MOPEN + parno);
1811 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001812#ifdef FEAT_SYN_HL
1813 else if (paren == REG_ZPAREN)
1814 EMIT(NFA_ZOPEN + parno);
1815#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816
1817 return OK;
1818}
1819
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820#ifdef DEBUG
1821static char_u code[50];
1822
1823 static void
1824nfa_set_code(c)
1825 int c;
1826{
1827 int addnl = FALSE;
1828
1829 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1830 {
1831 addnl = TRUE;
1832 c -= ADD_NL;
1833 }
1834
1835 STRCPY(code, "");
1836 switch (c)
1837 {
1838 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1839 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1840 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1841 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1842 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1843 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1844
Bram Moolenaar5714b802013-05-28 22:03:20 +02001845 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1846 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1847 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1848 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1849 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1850 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1851 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1852 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1853 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001854#ifdef FEAT_SYN_HL
1855 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1856 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1857 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1858 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1859 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1860 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1861 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1862 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1863 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1864#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001865 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1866
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001867 case NFA_PREV_ATOM_NO_WIDTH:
1868 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001869 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1870 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001871 case NFA_PREV_ATOM_JUST_BEFORE:
1872 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1873 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1874 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001875 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1876 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001878 case NFA_START_INVISIBLE_BEFORE:
1879 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1881
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1883 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1884
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001885 case NFA_MOPEN:
1886 case NFA_MOPEN1:
1887 case NFA_MOPEN2:
1888 case NFA_MOPEN3:
1889 case NFA_MOPEN4:
1890 case NFA_MOPEN5:
1891 case NFA_MOPEN6:
1892 case NFA_MOPEN7:
1893 case NFA_MOPEN8:
1894 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 STRCPY(code, "NFA_MOPEN(x)");
1896 code[10] = c - NFA_MOPEN + '0';
1897 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001898 case NFA_MCLOSE:
1899 case NFA_MCLOSE1:
1900 case NFA_MCLOSE2:
1901 case NFA_MCLOSE3:
1902 case NFA_MCLOSE4:
1903 case NFA_MCLOSE5:
1904 case NFA_MCLOSE6:
1905 case NFA_MCLOSE7:
1906 case NFA_MCLOSE8:
1907 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 STRCPY(code, "NFA_MCLOSE(x)");
1909 code[11] = c - NFA_MCLOSE + '0';
1910 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001911#ifdef FEAT_SYN_HL
1912 case NFA_ZOPEN:
1913 case NFA_ZOPEN1:
1914 case NFA_ZOPEN2:
1915 case NFA_ZOPEN3:
1916 case NFA_ZOPEN4:
1917 case NFA_ZOPEN5:
1918 case NFA_ZOPEN6:
1919 case NFA_ZOPEN7:
1920 case NFA_ZOPEN8:
1921 case NFA_ZOPEN9:
1922 STRCPY(code, "NFA_ZOPEN(x)");
1923 code[10] = c - NFA_ZOPEN + '0';
1924 break;
1925 case NFA_ZCLOSE:
1926 case NFA_ZCLOSE1:
1927 case NFA_ZCLOSE2:
1928 case NFA_ZCLOSE3:
1929 case NFA_ZCLOSE4:
1930 case NFA_ZCLOSE5:
1931 case NFA_ZCLOSE6:
1932 case NFA_ZCLOSE7:
1933 case NFA_ZCLOSE8:
1934 case NFA_ZCLOSE9:
1935 STRCPY(code, "NFA_ZCLOSE(x)");
1936 code[11] = c - NFA_ZCLOSE + '0';
1937 break;
1938#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1940 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1941 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1942 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001943 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1944 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001945 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1946 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1947 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1948 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1949 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1950 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1951 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1952 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1953 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1954 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1955 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1956 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1957 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
1958 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
1959
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001961 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1962 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1963 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1965 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1966 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001967 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1968 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1969 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1970 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1971 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1972 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1973 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1974 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1975 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1976 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1977 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1978 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1979 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1980 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1981 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1982 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1983 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1984
1985 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1986 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1987 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1988 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1989 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1990 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1991 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1992 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1993 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1994 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1995 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1996 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1997 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1998 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1999 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2000 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2001 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2002 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2003 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2004 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2005 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2006 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2007 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2008 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2009 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2010 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2011 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2012
2013 default:
2014 STRCPY(code, "CHAR(x)");
2015 code[5] = c;
2016 }
2017
2018 if (addnl == TRUE)
2019 STRCAT(code, " + NEWLINE ");
2020
2021}
2022
2023#ifdef ENABLE_LOG
2024static FILE *log_fd;
2025
2026/*
2027 * Print the postfix notation of the current regexp.
2028 */
2029 static void
2030nfa_postfix_dump(expr, retval)
2031 char_u *expr;
2032 int retval;
2033{
2034 int *p;
2035 FILE *f;
2036
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002037 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 if (f != NULL)
2039 {
2040 fprintf(f, "\n-------------------------\n");
2041 if (retval == FAIL)
2042 fprintf(f, ">>> NFA engine failed ... \n");
2043 else if (retval == OK)
2044 fprintf(f, ">>> NFA engine succeeded !\n");
2045 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002046 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002047 {
2048 nfa_set_code(*p);
2049 fprintf(f, "%s, ", code);
2050 }
2051 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002052 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 fprintf(f, "%d ", *p);
2054 fprintf(f, "\n\n");
2055 fclose(f);
2056 }
2057}
2058
2059/*
2060 * Print the NFA starting with a root node "state".
2061 */
2062 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002063nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002064 FILE *debugf;
2065 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002066{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002067 garray_T indent;
2068
2069 ga_init2(&indent, 1, 64);
2070 ga_append(&indent, '\0');
2071 nfa_print_state2(debugf, state, &indent);
2072 ga_clear(&indent);
2073}
2074
2075 static void
2076nfa_print_state2(debugf, state, indent)
2077 FILE *debugf;
2078 nfa_state_T *state;
2079 garray_T *indent;
2080{
2081 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082
2083 if (state == NULL)
2084 return;
2085
2086 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002087
2088 /* Output indent */
2089 p = (char_u *)indent->ga_data;
2090 if (indent->ga_len >= 3)
2091 {
2092 int last = indent->ga_len - 3;
2093 char_u save[2];
2094
2095 STRNCPY(save, &p[last], 2);
2096 STRNCPY(&p[last], "+-", 2);
2097 fprintf(debugf, " %s", p);
2098 STRNCPY(&p[last], save, 2);
2099 }
2100 else
2101 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002102
2103 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002104 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2105 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002106 if (state->id < 0)
2107 return;
2108
2109 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002110
2111 /* grow indent for state->out */
2112 indent->ga_len -= 1;
2113 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002114 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002115 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002116 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002117 ga_append(indent, '\0');
2118
2119 nfa_print_state2(debugf, state->out, indent);
2120
2121 /* replace last part of indent for state->out1 */
2122 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002123 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002124 ga_append(indent, '\0');
2125
2126 nfa_print_state2(debugf, state->out1, indent);
2127
2128 /* shrink indent */
2129 indent->ga_len -= 3;
2130 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002131}
2132
2133/*
2134 * Print the NFA state machine.
2135 */
2136 static void
2137nfa_dump(prog)
2138 nfa_regprog_T *prog;
2139{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002140 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002141
2142 if (debugf != NULL)
2143 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002144 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002145 fclose(debugf);
2146 }
2147}
2148#endif /* ENABLE_LOG */
2149#endif /* DEBUG */
2150
2151/*
2152 * Parse r.e. @expr and convert it into postfix form.
2153 * Return the postfix string on success, NULL otherwise.
2154 */
2155 static int *
2156re2post()
2157{
2158 if (nfa_reg(REG_NOPAREN) == FAIL)
2159 return NULL;
2160 EMIT(NFA_MOPEN);
2161 return post_start;
2162}
2163
2164/* NB. Some of the code below is inspired by Russ's. */
2165
2166/*
2167 * Represents an NFA state plus zero or one or two arrows exiting.
2168 * if c == MATCH, no arrows out; matching state.
2169 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2170 * If c < 256, labeled arrow with character c to out.
2171 */
2172
2173static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2174
2175/*
2176 * Allocate and initialize nfa_state_T.
2177 */
2178 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002179alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 int c;
2181 nfa_state_T *out;
2182 nfa_state_T *out1;
2183{
2184 nfa_state_T *s;
2185
2186 if (istate >= nstate)
2187 return NULL;
2188
2189 s = &state_ptr[istate++];
2190
2191 s->c = c;
2192 s->out = out;
2193 s->out1 = out1;
2194
2195 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002196 s->lastlist[0] = 0;
2197 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198 s->negated = FALSE;
2199
2200 return s;
2201}
2202
2203/*
2204 * A partially built NFA without the matching state filled in.
2205 * Frag_T.start points at the start state.
2206 * Frag_T.out is a list of places that need to be set to the
2207 * next state for this fragment.
2208 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002209
2210/* Since the out pointers in the list are always
2211 * uninitialized, we use the pointers themselves
2212 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002213typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002214union Ptrlist
2215{
2216 Ptrlist *next;
2217 nfa_state_T *s;
2218};
2219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220struct Frag
2221{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002222 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002223 Ptrlist *out;
2224};
2225typedef struct Frag Frag_T;
2226
2227static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2228static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2229static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2230static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2231static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2232static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2233
2234/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002235 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236 */
2237 static Frag_T
2238frag(start, out)
2239 nfa_state_T *start;
2240 Ptrlist *out;
2241{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002242 Frag_T n;
2243
2244 n.start = start;
2245 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002246 return n;
2247}
2248
2249/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002250 * Create singleton list containing just outp.
2251 */
2252 static Ptrlist *
2253list1(outp)
2254 nfa_state_T **outp;
2255{
2256 Ptrlist *l;
2257
2258 l = (Ptrlist *)outp;
2259 l->next = NULL;
2260 return l;
2261}
2262
2263/*
2264 * Patch the list of states at out to point to start.
2265 */
2266 static void
2267patch(l, s)
2268 Ptrlist *l;
2269 nfa_state_T *s;
2270{
2271 Ptrlist *next;
2272
2273 for (; l; l = next)
2274 {
2275 next = l->next;
2276 l->s = s;
2277 }
2278}
2279
2280
2281/*
2282 * Join the two lists l1 and l2, returning the combination.
2283 */
2284 static Ptrlist *
2285append(l1, l2)
2286 Ptrlist *l1;
2287 Ptrlist *l2;
2288{
2289 Ptrlist *oldl1;
2290
2291 oldl1 = l1;
2292 while (l1->next)
2293 l1 = l1->next;
2294 l1->next = l2;
2295 return oldl1;
2296}
2297
2298/*
2299 * Stack used for transforming postfix form into NFA.
2300 */
2301static Frag_T empty;
2302
2303 static void
2304st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002305 int *postfix UNUSED;
2306 int *end UNUSED;
2307 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002308{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002309#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002310 FILE *df;
2311 int *p2;
2312
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002313 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314 if (df)
2315 {
2316 fprintf(df, "Error popping the stack!\n");
2317#ifdef DEBUG
2318 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2319#endif
2320 fprintf(df, "Postfix form is: ");
2321#ifdef DEBUG
2322 for (p2 = postfix; p2 < end; p2++)
2323 {
2324 nfa_set_code(*p2);
2325 fprintf(df, "%s, ", code);
2326 }
2327 nfa_set_code(*p);
2328 fprintf(df, "\nCurrent position is: ");
2329 for (p2 = postfix; p2 <= p; p2 ++)
2330 {
2331 nfa_set_code(*p2);
2332 fprintf(df, "%s, ", code);
2333 }
2334#else
2335 for (p2 = postfix; p2 < end; p2++)
2336 {
2337 fprintf(df, "%d, ", *p2);
2338 }
2339 fprintf(df, "\nCurrent position is: ");
2340 for (p2 = postfix; p2 <= p; p2 ++)
2341 {
2342 fprintf(df, "%d, ", *p2);
2343 }
2344#endif
2345 fprintf(df, "\n--------------------------\n");
2346 fclose(df);
2347 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002348#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349 EMSG(_("E874: (NFA) Could not pop the stack !"));
2350}
2351
2352/*
2353 * Push an item onto the stack.
2354 */
2355 static void
2356st_push(s, p, stack_end)
2357 Frag_T s;
2358 Frag_T **p;
2359 Frag_T *stack_end;
2360{
2361 Frag_T *stackp = *p;
2362
2363 if (stackp >= stack_end)
2364 return;
2365 *stackp = s;
2366 *p = *p + 1;
2367}
2368
2369/*
2370 * Pop an item from the stack.
2371 */
2372 static Frag_T
2373st_pop(p, stack)
2374 Frag_T **p;
2375 Frag_T *stack;
2376{
2377 Frag_T *stackp;
2378
2379 *p = *p - 1;
2380 stackp = *p;
2381 if (stackp < stack)
2382 return empty;
2383 return **p;
2384}
2385
2386/*
2387 * Convert a postfix form into its equivalent NFA.
2388 * Return the NFA start state on success, NULL otherwise.
2389 */
2390 static nfa_state_T *
2391post2nfa(postfix, end, nfa_calc_size)
2392 int *postfix;
2393 int *end;
2394 int nfa_calc_size;
2395{
2396 int *p;
2397 int mopen;
2398 int mclose;
2399 Frag_T *stack = NULL;
2400 Frag_T *stackp = NULL;
2401 Frag_T *stack_end = NULL;
2402 Frag_T e1;
2403 Frag_T e2;
2404 Frag_T e;
2405 nfa_state_T *s;
2406 nfa_state_T *s1;
2407 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002408 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002409
2410 if (postfix == NULL)
2411 return NULL;
2412
Bram Moolenaar053bb602013-05-20 13:55:21 +02002413#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002414#define POP() st_pop(&stackp, stack); \
2415 if (stackp < stack) \
2416 { \
2417 st_error(postfix, end, p); \
2418 return NULL; \
2419 }
2420
2421 if (nfa_calc_size == FALSE)
2422 {
2423 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002424 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002426 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002427 }
2428
2429 for (p = postfix; p < end; ++p)
2430 {
2431 switch (*p)
2432 {
2433 case NFA_CONCAT:
2434 /* Catenation.
2435 * Pay attention: this operator does not exist
2436 * in the r.e. itself (it is implicit, really).
2437 * It is added when r.e. is translated to postfix
2438 * form in re2post().
2439 *
2440 * No new state added here. */
2441 if (nfa_calc_size == TRUE)
2442 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002443 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 break;
2445 }
2446 e2 = POP();
2447 e1 = POP();
2448 patch(e1.out, e2.start);
2449 PUSH(frag(e1.start, e2.out));
2450 break;
2451
2452 case NFA_NOT:
2453 /* Negation of a character */
2454 if (nfa_calc_size == TRUE)
2455 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002456 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002457 break;
2458 }
2459 e1 = POP();
2460 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002461#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002462 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002464#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465 PUSH(e1);
2466 break;
2467
2468 case NFA_OR:
2469 /* Alternation */
2470 if (nfa_calc_size == TRUE)
2471 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002472 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 break;
2474 }
2475 e2 = POP();
2476 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002477 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002478 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002479 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002480 PUSH(frag(s, append(e1.out, e2.out)));
2481 break;
2482
2483 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002484 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002485 if (nfa_calc_size == TRUE)
2486 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002487 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002488 break;
2489 }
2490 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002491 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002492 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002493 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494 patch(e.out, s);
2495 PUSH(frag(s, list1(&s->out1)));
2496 break;
2497
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002498 case NFA_STAR_NONGREEDY:
2499 /* Zero or more, prefer zero */
2500 if (nfa_calc_size == TRUE)
2501 {
2502 nstate++;
2503 break;
2504 }
2505 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002506 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002507 if (s == NULL)
2508 goto theend;
2509 patch(e.out, s);
2510 PUSH(frag(s, list1(&s->out)));
2511 break;
2512
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002513 case NFA_QUEST:
2514 /* one or zero atoms=> greedy match */
2515 if (nfa_calc_size == TRUE)
2516 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002517 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002518 break;
2519 }
2520 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002521 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002523 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002524 PUSH(frag(s, append(e.out, list1(&s->out1))));
2525 break;
2526
2527 case NFA_QUEST_NONGREEDY:
2528 /* zero or one atoms => non-greedy match */
2529 if (nfa_calc_size == TRUE)
2530 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002531 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002532 break;
2533 }
2534 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002535 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002536 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002537 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538 PUSH(frag(s, append(e.out, list1(&s->out))));
2539 break;
2540
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002541 case NFA_SKIP_CHAR:
2542 /* Symbol of 0-length, Used in a repetition
2543 * with max/min count of 0 */
2544 if (nfa_calc_size == TRUE)
2545 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002546 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 break;
2548 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002549 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002551 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002552 PUSH(frag(s, list1(&s->out)));
2553 break;
2554
2555 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002556 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002557 case NFA_PREV_ATOM_JUST_BEFORE:
2558 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002559 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002560 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002561 * The \@<= operator: match for the preceding atom.
2562 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002563 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002564 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002565
2566 if (nfa_calc_size == TRUE)
2567 {
2568 nstate += 2;
2569 break;
2570 }
2571 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002572 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002574 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002575 patch(e.out, s1);
2576
Bram Moolenaar525666f2013-06-02 16:40:55 +02002577 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002578 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002579 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002580 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2581 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002582 {
2583 s->negated = TRUE;
2584 s1->negated = TRUE;
2585 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002586 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2587 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2588 {
2589 s->val = *++p; /* get the count */
2590 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2591 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002592
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002593 PUSH(frag(s, list1(&s1->out)));
2594 break;
2595
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002596#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002597 case NFA_COMPOSING: /* char with composing char */
2598#if 0
2599 /* TODO */
2600 if (regflags & RF_ICOMBINE)
2601 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002602 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002603 }
2604#endif
2605 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002606#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002607
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002608 case NFA_MOPEN: /* \( \) Submatch */
2609 case NFA_MOPEN1:
2610 case NFA_MOPEN2:
2611 case NFA_MOPEN3:
2612 case NFA_MOPEN4:
2613 case NFA_MOPEN5:
2614 case NFA_MOPEN6:
2615 case NFA_MOPEN7:
2616 case NFA_MOPEN8:
2617 case NFA_MOPEN9:
2618#ifdef FEAT_SYN_HL
2619 case NFA_ZOPEN: /* \z( \) Submatch */
2620 case NFA_ZOPEN1:
2621 case NFA_ZOPEN2:
2622 case NFA_ZOPEN3:
2623 case NFA_ZOPEN4:
2624 case NFA_ZOPEN5:
2625 case NFA_ZOPEN6:
2626 case NFA_ZOPEN7:
2627 case NFA_ZOPEN8:
2628 case NFA_ZOPEN9:
2629#endif
2630 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002631 if (nfa_calc_size == TRUE)
2632 {
2633 nstate += 2;
2634 break;
2635 }
2636
2637 mopen = *p;
2638 switch (*p)
2639 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002640 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2641#ifdef FEAT_SYN_HL
2642 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2643 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2644 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2645 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2646 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2647 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2648 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2649 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2650 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2651 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2652#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002653#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002654 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002655#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002656 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002657 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 mclose = *p + NSUBEXP;
2659 break;
2660 }
2661
2662 /* Allow "NFA_MOPEN" as a valid postfix representation for
2663 * the empty regexp "". In this case, the NFA will be
2664 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2665 * empty groups of parenthesis, and empty mbyte chars */
2666 if (stackp == stack)
2667 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002668 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002669 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002670 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002671 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002673 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674 patch(list1(&s->out), s1);
2675 PUSH(frag(s, list1(&s1->out)));
2676 break;
2677 }
2678
2679 /* At least one node was emitted before NFA_MOPEN, so
2680 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2681 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002682 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002683 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002684 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002685
Bram Moolenaar525666f2013-06-02 16:40:55 +02002686 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002687 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002688 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002689 patch(e.out, s1);
2690
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002691#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002692 if (mopen == NFA_COMPOSING)
2693 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002694 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002695#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696
2697 PUSH(frag(s, list1(&s1->out)));
2698 break;
2699
Bram Moolenaar5714b802013-05-28 22:03:20 +02002700 case NFA_BACKREF1:
2701 case NFA_BACKREF2:
2702 case NFA_BACKREF3:
2703 case NFA_BACKREF4:
2704 case NFA_BACKREF5:
2705 case NFA_BACKREF6:
2706 case NFA_BACKREF7:
2707 case NFA_BACKREF8:
2708 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002709#ifdef FEAT_SYN_HL
2710 case NFA_ZREF1:
2711 case NFA_ZREF2:
2712 case NFA_ZREF3:
2713 case NFA_ZREF4:
2714 case NFA_ZREF5:
2715 case NFA_ZREF6:
2716 case NFA_ZREF7:
2717 case NFA_ZREF8:
2718 case NFA_ZREF9:
2719#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002720 if (nfa_calc_size == TRUE)
2721 {
2722 nstate += 2;
2723 break;
2724 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002725 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002726 if (s == NULL)
2727 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002728 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002729 if (s1 == NULL)
2730 goto theend;
2731 patch(list1(&s->out), s1);
2732 PUSH(frag(s, list1(&s1->out)));
2733 break;
2734
Bram Moolenaar423532e2013-05-29 21:14:42 +02002735 case NFA_LNUM:
2736 case NFA_LNUM_GT:
2737 case NFA_LNUM_LT:
2738 case NFA_VCOL:
2739 case NFA_VCOL_GT:
2740 case NFA_VCOL_LT:
2741 case NFA_COL:
2742 case NFA_COL_GT:
2743 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002744 case NFA_MARK:
2745 case NFA_MARK_GT:
2746 case NFA_MARK_LT:
Bram Moolenaar423532e2013-05-29 21:14:42 +02002747 if (nfa_calc_size == TRUE)
2748 {
2749 nstate += 1;
2750 break;
2751 }
2752 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002753 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002754 if (s == NULL)
2755 goto theend;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002756 s->val = e1.start->c; /* lnum, col or mark name */
Bram Moolenaar423532e2013-05-29 21:14:42 +02002757 PUSH(frag(s, list1(&s->out)));
2758 break;
2759
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002760 case NFA_ZSTART:
2761 case NFA_ZEND:
2762 default:
2763 /* Operands */
2764 if (nfa_calc_size == TRUE)
2765 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002766 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002767 break;
2768 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002769 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002770 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002771 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002772 PUSH(frag(s, list1(&s->out)));
2773 break;
2774
2775 } /* switch(*p) */
2776
2777 } /* for(p = postfix; *p; ++p) */
2778
2779 if (nfa_calc_size == TRUE)
2780 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002781 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002782 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002783 }
2784
2785 e = POP();
2786 if (stackp != stack)
2787 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2788
2789 if (istate >= nstate)
2790 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2791
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002792 matchstate = &state_ptr[istate++]; /* the match state */
2793 matchstate->c = NFA_MATCH;
2794 matchstate->out = matchstate->out1 = NULL;
2795
2796 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002797 ret = e.start;
2798
2799theend:
2800 vim_free(stack);
2801 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002802
2803#undef POP1
2804#undef PUSH1
2805#undef POP2
2806#undef PUSH2
2807#undef POP
2808#undef PUSH
2809}
2810
2811/****************************************************************
2812 * NFA execution code.
2813 ****************************************************************/
2814
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002815typedef struct
2816{
2817 int in_use; /* number of subexpr with useful info */
2818
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002819 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002820 union
2821 {
2822 struct multipos
2823 {
2824 lpos_T start;
2825 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002826 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002827 struct linepos
2828 {
2829 char_u *start;
2830 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002831 } line[NSUBEXP];
2832 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002833} regsub_T;
2834
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002835typedef struct
2836{
2837 regsub_T norm; /* \( .. \) matches */
2838#ifdef FEAT_SYN_HL
2839 regsub_T synt; /* \z( .. \) matches */
2840#endif
2841} regsubs_T;
2842
Bram Moolenaara2d95102013-06-04 14:23:05 +02002843/* nfa_pim_T stores a Postponed Invisible Match. */
2844typedef struct nfa_pim_S nfa_pim_T;
2845struct nfa_pim_S
2846{
2847 nfa_state_T *state;
2848 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2849 nfa_pim_T *pim; /* another PIM at the same position */
2850 regsubs_T subs; /* submatch info, only party used */
2851};
2852
2853/* Values for done in nfa_pim_T. */
2854#define NFA_PIM_TODO 0
2855#define NFA_PIM_MATCH 1
2856#define NFA_PIM_NOMATCH -1
2857
2858
Bram Moolenaar963fee22013-05-26 21:47:28 +02002859/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002860typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002861{
2862 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002863 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002864 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002865 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002866} nfa_thread_T;
2867
Bram Moolenaar963fee22013-05-26 21:47:28 +02002868/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869typedef struct
2870{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002871 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002872 int n; /* nr of states currently in "t" */
2873 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002874 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002875} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876
Bram Moolenaar5714b802013-05-28 22:03:20 +02002877#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002878static void log_subsexpr __ARGS((regsubs_T *subs));
2879static void log_subexpr __ARGS((regsub_T *sub));
2880
2881 static void
2882log_subsexpr(subs)
2883 regsubs_T *subs;
2884{
2885 log_subexpr(&subs->norm);
2886# ifdef FEAT_SYN_HL
2887 log_subexpr(&subs->synt);
2888# endif
2889}
2890
Bram Moolenaar5714b802013-05-28 22:03:20 +02002891 static void
2892log_subexpr(sub)
2893 regsub_T *sub;
2894{
2895 int j;
2896
2897 for (j = 0; j < sub->in_use; j++)
2898 if (REG_MULTI)
2899 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2900 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002901 sub->list.multi[j].start.col,
2902 (int)sub->list.multi[j].start.lnum,
2903 sub->list.multi[j].end.col,
2904 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002905 else
2906 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2907 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002908 (char *)sub->list.line[j].start,
2909 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002910 fprintf(log_fd, "\n");
2911}
2912#endif
2913
Bram Moolenaar963fee22013-05-26 21:47:28 +02002914/* Used during execution: whether a match has been found. */
2915static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002916
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002917static void clear_sub __ARGS((regsub_T *sub));
2918static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2919static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002920static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002921static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02002922static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002923
2924 static void
2925clear_sub(sub)
2926 regsub_T *sub;
2927{
2928 if (REG_MULTI)
2929 /* Use 0xff to set lnum to -1 */
2930 vim_memset(sub->list.multi, 0xff,
2931 sizeof(struct multipos) * nfa_nsubexpr);
2932 else
2933 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2934 sub->in_use = 0;
2935}
2936
2937/*
2938 * Copy the submatches from "from" to "to".
2939 */
2940 static void
2941copy_sub(to, from)
2942 regsub_T *to;
2943 regsub_T *from;
2944{
2945 to->in_use = from->in_use;
2946 if (from->in_use > 0)
2947 {
2948 /* Copy the match start and end positions. */
2949 if (REG_MULTI)
2950 mch_memmove(&to->list.multi[0],
2951 &from->list.multi[0],
2952 sizeof(struct multipos) * from->in_use);
2953 else
2954 mch_memmove(&to->list.line[0],
2955 &from->list.line[0],
2956 sizeof(struct linepos) * from->in_use);
2957 }
2958}
2959
2960/*
2961 * Like copy_sub() but exclude the main match.
2962 */
2963 static void
2964copy_sub_off(to, from)
2965 regsub_T *to;
2966 regsub_T *from;
2967{
2968 if (to->in_use < from->in_use)
2969 to->in_use = from->in_use;
2970 if (from->in_use > 1)
2971 {
2972 /* Copy the match start and end positions. */
2973 if (REG_MULTI)
2974 mch_memmove(&to->list.multi[1],
2975 &from->list.multi[1],
2976 sizeof(struct multipos) * (from->in_use - 1));
2977 else
2978 mch_memmove(&to->list.line[1],
2979 &from->list.line[1],
2980 sizeof(struct linepos) * (from->in_use - 1));
2981 }
2982}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002983
Bram Moolenaar428e9872013-05-30 17:05:39 +02002984/*
2985 * Return TRUE if "sub1" and "sub2" have the same positions.
2986 */
2987 static int
2988sub_equal(sub1, sub2)
2989 regsub_T *sub1;
2990 regsub_T *sub2;
2991{
2992 int i;
2993 int todo;
2994 linenr_T s1, e1;
2995 linenr_T s2, e2;
2996 char_u *sp1, *ep1;
2997 char_u *sp2, *ep2;
2998
2999 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3000 if (REG_MULTI)
3001 {
3002 for (i = 0; i < todo; ++i)
3003 {
3004 if (i < sub1->in_use)
3005 {
3006 s1 = sub1->list.multi[i].start.lnum;
3007 e1 = sub1->list.multi[i].end.lnum;
3008 }
3009 else
3010 {
3011 s1 = 0;
3012 e1 = 0;
3013 }
3014 if (i < sub2->in_use)
3015 {
3016 s2 = sub2->list.multi[i].start.lnum;
3017 e2 = sub2->list.multi[i].end.lnum;
3018 }
3019 else
3020 {
3021 s2 = 0;
3022 e2 = 0;
3023 }
3024 if (s1 != s2 || e1 != e2)
3025 return FALSE;
3026 if (s1 != 0 && sub1->list.multi[i].start.col
3027 != sub2->list.multi[i].start.col)
3028 return FALSE;
3029 if (e1 != 0 && sub1->list.multi[i].end.col
3030 != sub2->list.multi[i].end.col)
3031 return FALSE;
3032 }
3033 }
3034 else
3035 {
3036 for (i = 0; i < todo; ++i)
3037 {
3038 if (i < sub1->in_use)
3039 {
3040 sp1 = sub1->list.line[i].start;
3041 ep1 = sub1->list.line[i].end;
3042 }
3043 else
3044 {
3045 sp1 = NULL;
3046 ep1 = NULL;
3047 }
3048 if (i < sub2->in_use)
3049 {
3050 sp2 = sub2->list.line[i].start;
3051 ep2 = sub2->list.line[i].end;
3052 }
3053 else
3054 {
3055 sp2 = NULL;
3056 ep2 = NULL;
3057 }
3058 if (sp1 != sp2 || ep1 != ep2)
3059 return FALSE;
3060 }
3061 }
3062
3063 return TRUE;
3064}
3065
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003066#ifdef ENABLE_LOG
3067 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003068report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003069{
3070 int col;
3071
3072 if (sub->in_use <= 0)
3073 col = -1;
3074 else if (REG_MULTI)
3075 col = sub->list.multi[0].start.col;
3076 else
3077 col = (int)(sub->list.line[0].start - regline);
3078 nfa_set_code(state->c);
3079 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3080 action, abs(state->id), lid, state->c, code, col);
3081}
3082#endif
3083
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003084 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003085addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003086 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003087 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003088 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003089 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003090{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003091 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003092 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003093 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003094 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003095 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003096 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003097 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003098#ifdef ENABLE_LOG
3099 int did_print = FALSE;
3100#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003101
3102 if (l == NULL || state == NULL)
3103 return;
3104
3105 switch (state->c)
3106 {
3107 case NFA_SPLIT:
3108 case NFA_NOT:
3109 case NFA_NOPEN:
3110 case NFA_NCLOSE:
3111 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003112 case NFA_MCLOSE1:
3113 case NFA_MCLOSE2:
3114 case NFA_MCLOSE3:
3115 case NFA_MCLOSE4:
3116 case NFA_MCLOSE5:
3117 case NFA_MCLOSE6:
3118 case NFA_MCLOSE7:
3119 case NFA_MCLOSE8:
3120 case NFA_MCLOSE9:
3121#ifdef FEAT_SYN_HL
3122 case NFA_ZCLOSE:
3123 case NFA_ZCLOSE1:
3124 case NFA_ZCLOSE2:
3125 case NFA_ZCLOSE3:
3126 case NFA_ZCLOSE4:
3127 case NFA_ZCLOSE5:
3128 case NFA_ZCLOSE6:
3129 case NFA_ZCLOSE7:
3130 case NFA_ZCLOSE8:
3131 case NFA_ZCLOSE9:
3132#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003133 /* These nodes are not added themselves but their "out" and/or
3134 * "out1" may be added below. */
3135 break;
3136
3137 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003138 case NFA_MOPEN1:
3139 case NFA_MOPEN2:
3140 case NFA_MOPEN3:
3141 case NFA_MOPEN4:
3142 case NFA_MOPEN5:
3143 case NFA_MOPEN6:
3144 case NFA_MOPEN7:
3145 case NFA_MOPEN8:
3146 case NFA_MOPEN9:
3147#ifdef FEAT_SYN_HL
3148 case NFA_ZOPEN:
3149 case NFA_ZOPEN1:
3150 case NFA_ZOPEN2:
3151 case NFA_ZOPEN3:
3152 case NFA_ZOPEN4:
3153 case NFA_ZOPEN5:
3154 case NFA_ZOPEN6:
3155 case NFA_ZOPEN7:
3156 case NFA_ZOPEN8:
3157 case NFA_ZOPEN9:
3158#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003159 /* These nodes do not need to be added, but we need to bail out
3160 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003161 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003162 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003163 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003164 break;
3165
Bram Moolenaar307aa162013-06-02 16:34:21 +02003166 case NFA_BOL:
3167 case NFA_BOF:
3168 /* "^" won't match past end-of-line, don't bother trying.
3169 * Except when we are going to the next line for a look-behind
3170 * match. */
3171 if (reginput > regline
3172 && (nfa_endp == NULL
3173 || !REG_MULTI
3174 || reglnum == nfa_endp->se_u.pos.lnum))
3175 goto skip_add;
3176 /* FALLTHROUGH */
3177
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003178 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003179 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003181 /* This state is already in the list, don't add it again,
3182 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003183 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003184 {
3185skip_add:
3186#ifdef ENABLE_LOG
3187 nfa_set_code(state->c);
3188 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3189 abs(state->id), l->id, state->c, code);
3190#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003191 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003192 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003193
3194 /* See if the same state is already in the list with the same
3195 * positions. */
3196 for (i = 0; i < l->n; ++i)
3197 {
3198 thread = &l->t[i];
3199 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003200 && sub_equal(&thread->subs.norm, &subs->norm)
3201#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003202 && (!nfa_has_zsubexpr ||
3203 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003204#endif
3205 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003206 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003207 }
3208 }
3209
Bram Moolenaara2d95102013-06-04 14:23:05 +02003210 /* when there are backreferences or look-behind matches the number
3211 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003212 if (nfa_has_backref && l->n == l->len)
3213 {
3214 int newlen = l->len * 3 / 2 + 50;
3215
3216 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3217 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003219
3220 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003221 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003222 thread = &l->t[l->n++];
3223 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003224 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003225 copy_sub(&thread->subs.norm, &subs->norm);
3226#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003227 if (nfa_has_zsubexpr)
3228 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003229#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003230#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003231 report_state("Adding", &thread->subs.norm, state, l->id);
3232 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003233#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 }
3235
3236#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003237 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003238 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239#endif
3240 switch (state->c)
3241 {
3242 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003243 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 break;
3245
3246 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003247 /* order matters here */
3248 addstate(l, state->out, subs, off);
3249 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003250 break;
3251
Bram Moolenaar5714b802013-05-28 22:03:20 +02003252 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253 case NFA_NOPEN:
3254 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003255 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 break;
3257
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003258 case NFA_MOPEN:
3259 case NFA_MOPEN1:
3260 case NFA_MOPEN2:
3261 case NFA_MOPEN3:
3262 case NFA_MOPEN4:
3263 case NFA_MOPEN5:
3264 case NFA_MOPEN6:
3265 case NFA_MOPEN7:
3266 case NFA_MOPEN8:
3267 case NFA_MOPEN9:
3268#ifdef FEAT_SYN_HL
3269 case NFA_ZOPEN:
3270 case NFA_ZOPEN1:
3271 case NFA_ZOPEN2:
3272 case NFA_ZOPEN3:
3273 case NFA_ZOPEN4:
3274 case NFA_ZOPEN5:
3275 case NFA_ZOPEN6:
3276 case NFA_ZOPEN7:
3277 case NFA_ZOPEN8:
3278 case NFA_ZOPEN9:
3279#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003280 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003282 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003284 sub = &subs->norm;
3285 }
3286#ifdef FEAT_SYN_HL
3287 else if (state->c >= NFA_ZOPEN)
3288 {
3289 subidx = state->c - NFA_ZOPEN;
3290 sub = &subs->synt;
3291 }
3292#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003293 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003294 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003295 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003296 sub = &subs->norm;
3297 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003299 /* Set the position (with "off") in the subexpression. Save and
3300 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003301 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 if (REG_MULTI)
3303 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003304 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003305 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003306 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003307 save_in_use = -1;
3308 }
3309 else
3310 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003311 save_in_use = sub->in_use;
3312 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003313 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003314 sub->list.multi[i].start.lnum = -1;
3315 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003316 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003317 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003318 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003319 if (off == -1)
3320 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003321 sub->list.multi[subidx].start.lnum = reglnum + 1;
3322 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003323 }
3324 else
3325 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003326 sub->list.multi[subidx].start.lnum = reglnum;
3327 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003328 (colnr_T)(reginput - regline + off);
3329 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003330 }
3331 else
3332 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003333 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003334 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003335 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003336 save_in_use = -1;
3337 }
3338 else
3339 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003340 save_in_use = sub->in_use;
3341 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003342 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003343 sub->list.line[i].start = NULL;
3344 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003345 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003346 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003347 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003348 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003349 }
3350
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003351 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003352
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003353 if (save_in_use == -1)
3354 {
3355 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003356 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003357 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003358 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003359 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003361 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 break;
3363
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003364 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003365 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003366 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003367 /* Do not overwrite the position set by \ze. If no \ze
3368 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003369 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370 break;
3371 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003372 case NFA_MCLOSE1:
3373 case NFA_MCLOSE2:
3374 case NFA_MCLOSE3:
3375 case NFA_MCLOSE4:
3376 case NFA_MCLOSE5:
3377 case NFA_MCLOSE6:
3378 case NFA_MCLOSE7:
3379 case NFA_MCLOSE8:
3380 case NFA_MCLOSE9:
3381#ifdef FEAT_SYN_HL
3382 case NFA_ZCLOSE:
3383 case NFA_ZCLOSE1:
3384 case NFA_ZCLOSE2:
3385 case NFA_ZCLOSE3:
3386 case NFA_ZCLOSE4:
3387 case NFA_ZCLOSE5:
3388 case NFA_ZCLOSE6:
3389 case NFA_ZCLOSE7:
3390 case NFA_ZCLOSE8:
3391 case NFA_ZCLOSE9:
3392#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003393 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003394 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003395 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003396 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003397 sub = &subs->norm;
3398 }
3399#ifdef FEAT_SYN_HL
3400 else if (state->c >= NFA_ZCLOSE)
3401 {
3402 subidx = state->c - NFA_ZCLOSE;
3403 sub = &subs->synt;
3404 }
3405#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003406 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003407 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003408 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003409 sub = &subs->norm;
3410 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003412 /* We don't fill in gaps here, there must have been an MOPEN that
3413 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003414 save_in_use = sub->in_use;
3415 if (sub->in_use <= subidx)
3416 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 if (REG_MULTI)
3418 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003419 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003420 if (off == -1)
3421 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003422 sub->list.multi[subidx].end.lnum = reglnum + 1;
3423 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003424 }
3425 else
3426 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003427 sub->list.multi[subidx].end.lnum = reglnum;
3428 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003429 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003430 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003431 }
3432 else
3433 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003434 save_ptr = sub->list.line[subidx].end;
3435 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003436 }
3437
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003438 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439
3440 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003441 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003442 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003443 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003444 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445 break;
3446 }
3447}
3448
3449/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003450 * Like addstate(), but the new state(s) are put at position "*ip".
3451 * Used for zero-width matches, next state to use is the added one.
3452 * This makes sure the order of states to be tried does not change, which
3453 * matters for alternatives.
3454 */
3455 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003456addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003457 nfa_list_T *l; /* runtime state list */
3458 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003459 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003460 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003461 int *ip;
3462{
3463 int tlen = l->n;
3464 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003465 int listidx = *ip;
3466 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003467
3468 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003469 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003470
Bram Moolenaara2d95102013-06-04 14:23:05 +02003471 /* fill in the "pim" field in the new states */
3472 if (pim != NULL)
3473 for (i = tlen; i < l->n; ++i)
3474 l->t[i].pim = pim;
3475
Bram Moolenaar4b417062013-05-25 20:19:50 +02003476 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003477 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003478 return;
3479
3480 /* re-order to put the new state at the current position */
3481 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003482 if (count == 1)
3483 {
3484 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003485 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003486 }
3487 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003488 {
3489 /* make space for new states, then move them from the
3490 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003491 mch_memmove(&(l->t[listidx + count]),
3492 &(l->t[listidx + 1]),
3493 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3494 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003495 &(l->t[l->n - 1]),
3496 sizeof(nfa_thread_T) * count);
3497 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003498 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003499 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003500}
3501
3502/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503 * Check character class "class" against current character c.
3504 */
3505 static int
3506check_char_class(class, c)
3507 int class;
3508 int c;
3509{
3510 switch (class)
3511 {
3512 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003513 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 return OK;
3515 break;
3516 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003517 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 return OK;
3519 break;
3520 case NFA_CLASS_BLANK:
3521 if (c == ' ' || c == '\t')
3522 return OK;
3523 break;
3524 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003525 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003526 return OK;
3527 break;
3528 case NFA_CLASS_DIGIT:
3529 if (VIM_ISDIGIT(c))
3530 return OK;
3531 break;
3532 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003533 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003534 return OK;
3535 break;
3536 case NFA_CLASS_LOWER:
3537 if (MB_ISLOWER(c))
3538 return OK;
3539 break;
3540 case NFA_CLASS_PRINT:
3541 if (vim_isprintc(c))
3542 return OK;
3543 break;
3544 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003545 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003546 return OK;
3547 break;
3548 case NFA_CLASS_SPACE:
3549 if ((c >=9 && c <= 13) || (c == ' '))
3550 return OK;
3551 break;
3552 case NFA_CLASS_UPPER:
3553 if (MB_ISUPPER(c))
3554 return OK;
3555 break;
3556 case NFA_CLASS_XDIGIT:
3557 if (vim_isxdigit(c))
3558 return OK;
3559 break;
3560 case NFA_CLASS_TAB:
3561 if (c == '\t')
3562 return OK;
3563 break;
3564 case NFA_CLASS_RETURN:
3565 if (c == '\r')
3566 return OK;
3567 break;
3568 case NFA_CLASS_BACKSPACE:
3569 if (c == '\b')
3570 return OK;
3571 break;
3572 case NFA_CLASS_ESCAPE:
3573 if (c == '\033')
3574 return OK;
3575 break;
3576
3577 default:
3578 /* should not be here :P */
3579 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3580 }
3581 return FAIL;
3582}
3583
Bram Moolenaar5714b802013-05-28 22:03:20 +02003584static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3585
3586/*
3587 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003588 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003589 */
3590 static int
3591match_backref(sub, subidx, bytelen)
3592 regsub_T *sub; /* pointers to subexpressions */
3593 int subidx;
3594 int *bytelen; /* out: length of match in bytes */
3595{
3596 int len;
3597
3598 if (sub->in_use <= subidx)
3599 {
3600retempty:
3601 /* backref was not set, match an empty string */
3602 *bytelen = 0;
3603 return TRUE;
3604 }
3605
3606 if (REG_MULTI)
3607 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003608 if (sub->list.multi[subidx].start.lnum < 0
3609 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003610 goto retempty;
3611 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003612 len = sub->list.multi[subidx].end.col
3613 - sub->list.multi[subidx].start.col;
3614 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003615 reginput, &len) == 0)
3616 {
3617 *bytelen = len;
3618 return TRUE;
3619 }
3620 }
3621 else
3622 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003623 if (sub->list.line[subidx].start == NULL
3624 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003625 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003626 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3627 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003628 {
3629 *bytelen = len;
3630 return TRUE;
3631 }
3632 }
3633 return FALSE;
3634}
3635
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003636#ifdef FEAT_SYN_HL
3637
3638static int match_zref __ARGS((int subidx, int *bytelen));
3639
3640/*
3641 * Check for a match with \z subexpression "subidx".
3642 * Return TRUE if it matches.
3643 */
3644 static int
3645match_zref(subidx, bytelen)
3646 int subidx;
3647 int *bytelen; /* out: length of match in bytes */
3648{
3649 int len;
3650
3651 cleanup_zsubexpr();
3652 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3653 {
3654 /* backref was not set, match an empty string */
3655 *bytelen = 0;
3656 return TRUE;
3657 }
3658
3659 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3660 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3661 {
3662 *bytelen = len;
3663 return TRUE;
3664 }
3665 return FALSE;
3666}
3667#endif
3668
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003670 * Save list IDs for all NFA states of "prog" into "list".
3671 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003672 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003673 */
3674 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003675nfa_save_listids(prog, list)
3676 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003677 int *list;
3678{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003679 int i;
3680 nfa_state_T *p;
3681
3682 /* Order in the list is reverse, it's a bit faster that way. */
3683 p = &prog->state[0];
3684 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003686 list[i] = p->lastlist[1];
3687 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003688 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003689 }
3690}
3691
3692/*
3693 * Restore list IDs from "list" to all NFA states.
3694 */
3695 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003696nfa_restore_listids(prog, list)
3697 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003698 int *list;
3699{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003700 int i;
3701 nfa_state_T *p;
3702
3703 p = &prog->state[0];
3704 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003706 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003707 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003708 }
3709}
3710
Bram Moolenaar423532e2013-05-29 21:14:42 +02003711 static int
3712nfa_re_num_cmp(val, op, pos)
3713 long_u val;
3714 int op;
3715 long_u pos;
3716{
3717 if (op == 1) return pos > val;
3718 if (op == 2) return pos < val;
3719 return val == pos;
3720}
3721
Bram Moolenaarf46da702013-06-02 22:37:42 +02003722static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003723static int nfa_regmatch __ARGS((nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m));
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003724
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003725/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003726 * Recursively call nfa_regmatch()
3727 */
3728 static int
3729recursive_regmatch(state, prog, submatch, m, listids)
3730 nfa_state_T *state;
3731 nfa_regprog_T *prog;
3732 regsubs_T *submatch;
3733 regsubs_T *m;
3734 int **listids;
3735{
3736 char_u *save_reginput = reginput;
3737 char_u *save_regline = regline;
3738 int save_reglnum = reglnum;
3739 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003740 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003741 save_se_T *save_nfa_endp = nfa_endp;
3742 save_se_T endpos;
3743 save_se_T *endposp = NULL;
3744 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003745 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003746
3747 if (state->c == NFA_START_INVISIBLE_BEFORE)
3748 {
3749 /* The recursive match must end at the current position. */
3750 endposp = &endpos;
3751 if (REG_MULTI)
3752 {
3753 endpos.se_u.pos.col = (int)(reginput - regline);
3754 endpos.se_u.pos.lnum = reglnum;
3755 }
3756 else
3757 endpos.se_u.ptr = reginput;
3758
3759 /* Go back the specified number of bytes, or as far as the
3760 * start of the previous line, to try matching "\@<=" or
3761 * not matching "\@<!".
3762 * TODO: This is very inefficient! Would be better to
3763 * first check for a match with what follows. */
3764 if (state->val <= 0)
3765 {
3766 if (REG_MULTI)
3767 {
3768 regline = reg_getline(--reglnum);
3769 if (regline == NULL)
3770 /* can't go before the first line */
3771 regline = reg_getline(++reglnum);
3772 }
3773 reginput = regline;
3774 }
3775 else
3776 {
3777 if (REG_MULTI && (int)(reginput - regline) < state->val)
3778 {
3779 /* Not enough bytes in this line, go to end of
3780 * previous line. */
3781 regline = reg_getline(--reglnum);
3782 if (regline == NULL)
3783 {
3784 /* can't go before the first line */
3785 regline = reg_getline(++reglnum);
3786 reginput = regline;
3787 }
3788 else
3789 reginput = regline + STRLEN(regline);
3790 }
3791 if ((int)(reginput - regline) >= state->val)
3792 {
3793 reginput -= state->val;
3794#ifdef FEAT_MBYTE
3795 if (has_mbyte)
3796 reginput -= mb_head_off(regline, reginput);
3797#endif
3798 }
3799 else
3800 reginput = regline;
3801 }
3802 }
3803
Bram Moolenaarf46da702013-06-02 22:37:42 +02003804#ifdef ENABLE_LOG
3805 if (log_fd != stderr)
3806 fclose(log_fd);
3807 log_fd = NULL;
3808#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003809 /* Have to clear the lastlist field of the NFA nodes, so that
3810 * nfa_regmatch() and addstate() can run properly after recursion. */
3811 if (nfa_ll_index == 1)
3812 {
3813 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3814 * values and clear them. */
3815 if (*listids == NULL)
3816 {
3817 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3818 if (*listids == NULL)
3819 {
3820 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3821 return 0;
3822 }
3823 }
3824 nfa_save_listids(prog, *listids);
3825 need_restore = TRUE;
3826 /* any value of nfa_listid will do */
3827 }
3828 else
3829 {
3830 /* First recursive nfa_regmatch() call, switch to the second lastlist
3831 * entry. Make sure nfa_listid is different from a previous recursive
3832 * call, because some states may still have this ID. */
3833 ++nfa_ll_index;
3834 if (nfa_listid <= nfa_alt_listid)
3835 nfa_listid = nfa_alt_listid;
3836 }
3837
3838 /* Call nfa_regmatch() to check if the current concat matches at this
3839 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003840 nfa_endp = endposp;
3841 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003842
3843 if (need_restore)
3844 nfa_restore_listids(prog, *listids);
3845 else
3846 {
3847 --nfa_ll_index;
3848 nfa_alt_listid = nfa_listid;
3849 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003850
3851 /* restore position in input text */
3852 reginput = save_reginput;
3853 regline = save_regline;
3854 reglnum = save_reglnum;
3855 nfa_match = save_nfa_match;
3856 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003857 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003858
3859#ifdef ENABLE_LOG
3860 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3861 if (log_fd != NULL)
3862 {
3863 fprintf(log_fd, "****************************\n");
3864 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3865 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3866 fprintf(log_fd, "****************************\n");
3867 }
3868 else
3869 {
3870 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3871 log_fd = stderr;
3872 }
3873#endif
3874
3875 return result;
3876}
3877
Bram Moolenaara2d95102013-06-04 14:23:05 +02003878static int failure_chance __ARGS((nfa_state_T *state, int depth));
3879
3880/*
3881 * Estimate the chance of a match with "state" failing.
3882 * NFA_ANY: 1
3883 * specific character: 99
3884 */
3885 static int
3886failure_chance(state, depth)
3887 nfa_state_T *state;
3888 int depth;
3889{
3890 int c = state->c;
3891 int l, r;
3892
3893 /* detect looping */
3894 if (depth > 4)
3895 return 1;
3896
3897 if (c == NFA_SPLIT)
3898 {
3899 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3900 return 1;
3901 l = failure_chance(state->out, depth + 1);
3902 r = failure_chance(state->out1, depth + 1);
3903 return l < r ? l : r;
3904 }
3905 if (c == NFA_ANY)
3906 return 1;
3907 if (c > 0)
3908 return 99;
3909 if ((c >= NFA_MOPEN && c <= NFA_MOPEN9)
3910 || (c >= NFA_ZOPEN && c <= NFA_ZOPEN9)
3911 || c == NFA_NOPEN)
3912 return failure_chance(state->out, depth + 1);
3913 /* something else */
3914 return 50;
3915}
3916
Bram Moolenaarf46da702013-06-02 22:37:42 +02003917/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918 * Main matching routine.
3919 *
3920 * Run NFA to determine whether it matches reginput.
3921 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003922 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003923 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003924 * Return TRUE if there is a match, FALSE otherwise.
3925 * Note: Caller must ensure that: start != NULL.
3926 */
3927 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003928nfa_regmatch(prog, start, submatch, m)
3929 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003930 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003931 regsubs_T *submatch;
3932 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003933{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003934 int result;
3935 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003936 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003937 int go_to_nextline = FALSE;
3938 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003939 nfa_list_T list[3];
3940 nfa_list_T *listtbl[2][2];
3941 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003942 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003943 nfa_list_T *thislist;
3944 nfa_list_T *nextlist;
3945 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003946 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003947 nfa_state_T *add_state;
3948 int add_count;
3949 int add_off;
3950 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003951#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003952 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003953
3954 if (debug == NULL)
3955 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003956 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003957 return FALSE;
3958 }
3959#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003960 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003961 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003962
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003963 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003964 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003965 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3966 list[0].len = nstate + 1;
3967 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3968 list[1].len = nstate + 1;
3969 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3970 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003971 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3972 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003973
3974#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003975 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003976 if (log_fd != NULL)
3977 {
3978 fprintf(log_fd, "**********************************\n");
3979 nfa_set_code(start->c);
3980 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3981 abs(start->id), code);
3982 fprintf(log_fd, "**********************************\n");
3983 }
3984 else
3985 {
3986 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3987 log_fd = stderr;
3988 }
3989#endif
3990
3991 thislist = &list[0];
3992 thislist->n = 0;
3993 nextlist = &list[1];
3994 nextlist->n = 0;
3995 neglist = &list[2];
3996 neglist->n = 0;
3997#ifdef ENABLE_LOG
3998 fprintf(log_fd, "(---) STARTSTATE\n");
3999#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004000 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004001 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002
4003 /* There are two cases when the NFA advances: 1. input char matches the
4004 * NFA node and 2. input char does not match the NFA node, but the next
4005 * node is NFA_NOT. The following macro calls addstate() according to
4006 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4007 listtbl[0][0] = NULL;
4008 listtbl[0][1] = neglist;
4009 listtbl[1][0] = nextlist;
4010 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004011#define ADD_POS_NEG_STATE(state) \
4012 ll = listtbl[result ? 1 : 0][state->negated]; \
4013 if (ll != NULL) { \
4014 add_state = state->out; \
4015 add_off = clen; \
4016 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004017
4018 /*
4019 * Run for each character.
4020 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004021 for (;;)
4022 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004023 int curc;
4024 int clen;
4025
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004026#ifdef FEAT_MBYTE
4027 if (has_mbyte)
4028 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004029 curc = (*mb_ptr2char)(reginput);
4030 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004031 }
4032 else
4033#endif
4034 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004035 curc = *reginput;
4036 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004038 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004039 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004040 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004041 go_to_nextline = FALSE;
4042 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004043
4044 /* swap lists */
4045 thislist = &list[flag];
4046 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004047 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004048 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004049 ++nfa_listid;
4050 thislist->id = nfa_listid;
4051 nextlist->id = nfa_listid + 1;
4052 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004053
Bram Moolenaara2d95102013-06-04 14:23:05 +02004054 pimlist.ga_len = 0;
4055
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004056#ifdef ENABLE_LOG
4057 fprintf(log_fd, "------------------------------------------\n");
4058 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004059 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004060 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004061 {
4062 int i;
4063
4064 for (i = 0; i < thislist->n; i++)
4065 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4066 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067 fprintf(log_fd, "\n");
4068#endif
4069
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004070#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004071 fprintf(debug, "\n-------------------\n");
4072#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004073 /*
4074 * If the state lists are empty we can stop.
4075 */
4076 if (thislist->n == 0 && neglist->n == 0)
4077 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004078
4079 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004080 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004081 {
4082 if (neglist->n > 0)
4083 {
4084 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004085 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004086 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004087 }
4088 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004089 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004090
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004091#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004092 nfa_set_code(t->state->c);
4093 fprintf(debug, "%s, ", code);
4094#endif
4095#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004096 {
4097 int col;
4098
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004099 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004100 col = -1;
4101 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004102 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004103 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004104 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004105 nfa_set_code(t->state->c);
4106 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4107 abs(t->state->id), (int)t->state->c, code, col);
4108 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004109#endif
4110
4111 /*
4112 * Handle the possible codes of the current state.
4113 * The most important is NFA_MATCH.
4114 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004115 add_state = NULL;
4116 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117 switch (t->state->c)
4118 {
4119 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004120 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004121 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004122 copy_sub(&submatch->norm, &t->subs.norm);
4123#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004124 if (nfa_has_zsubexpr)
4125 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004126#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004127#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004128 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004129#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004130 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004131 * states at this position. When the list of states is going
4132 * to be empty quit without advancing, so that "reginput" is
4133 * correct. */
4134 if (nextlist->n == 0 && neglist->n == 0)
4135 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004136 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004137 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004138
4139 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004140 /*
4141 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004142 * NFA_START_INVISIBLE_BEFORE node.
4143 * They surround a zero-width group, used with "\@=", "\&",
4144 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004145 * If we got here, it means that the current "invisible" group
4146 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004147 * nfa_regmatch(). For a look-behind match only when it ends
4148 * in the position in "nfa_endp".
4149 * Submatches are stored in *m, and used in the parent call.
4150 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004151#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004152 if (nfa_endp != NULL)
4153 {
4154 if (REG_MULTI)
4155 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4156 (int)reglnum,
4157 (int)nfa_endp->se_u.pos.lnum,
4158 (int)(reginput - regline),
4159 nfa_endp->se_u.pos.col);
4160 else
4161 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4162 (int)(reginput - regline),
4163 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004164 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004165#endif
4166 /* It's only a match if it ends at "nfa_endp" */
4167 if (nfa_endp != NULL && (REG_MULTI
4168 ? (reglnum != nfa_endp->se_u.pos.lnum
4169 || (int)(reginput - regline)
4170 != nfa_endp->se_u.pos.col)
4171 : reginput != nfa_endp->se_u.ptr))
4172 break;
4173
4174 /* do not set submatches for \@! */
4175 if (!t->state->negated)
4176 {
4177 copy_sub(&m->norm, &t->subs.norm);
4178#ifdef FEAT_SYN_HL
4179 if (nfa_has_zsubexpr)
4180 copy_sub(&m->synt, &t->subs.synt);
4181#endif
4182 }
4183 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004184 break;
4185
4186 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004187 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2d95102013-06-04 14:23:05 +02004188 /* If invisible match has a higher chance to fail, do it
4189 * right away. Otherwise postpone it until what follows is
4190 * matching and causes addstate(nextlist, ..) to be called.
4191 * This is indicated by the "pim" field. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004192 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004193 nfa_pim_T *pim;
4194 int cout = t->state->out1->out->c;
4195
4196 /* Do it directly when what follows is possibly end of
4197 * match (closing paren).
4198 * Postpone when it is \@<= or \@<!, these are expensive.
4199 * TODO: remove the check for t->pim and check multiple
4200 * where it's used?
4201 * Otherwise first do the one that has the highest chance
4202 * of failing. */
4203 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
4204 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
4205 || cout == NFA_NCLOSE
4206 || t->pim != NULL
4207 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4208 && failure_chance(t->state->out1->out, 0)
4209 < failure_chance(t->state->out, 0)))
4210 {
4211 /*
4212 * First try matching the invisible match, then what
4213 * follows.
4214 */
4215 result = recursive_regmatch(t->state, prog,
4216 submatch, m, &listids);
4217
4218 /* for \@! it is a match when result is FALSE */
4219 if (result != t->state->negated)
4220 {
4221 /* Copy submatch info from the recursive call */
4222 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004223#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004224 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004225#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004226
Bram Moolenaara2d95102013-06-04 14:23:05 +02004227 /* t->state->out1 is the corresponding
4228 * END_INVISIBLE node; Add its out to the current
4229 * list (zero-width match). */
4230 addstate_here(thislist, t->state->out1->out,
4231 &t->subs, t->pim, &listidx);
4232 }
4233 }
4234 else
4235 {
4236 /*
4237 * First try matching what follows at the current
4238 * position. Only if a match is found, addstate() is
4239 * called, then verify the invisible match matches.
4240 * Add a nfa_pim_T to the following states, it
4241 * contains info about the invisible match.
4242 */
4243 if (ga_grow(&pimlist, 1) == FAIL)
4244 goto theend;
4245 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4246 ++pimlist.ga_len;
4247 pim->state = t->state;
4248 pim->pim = NULL;
4249 pim->result = NFA_PIM_TODO;
4250
4251 /* t->state->out1 is the corresponding END_INVISIBLE
4252 * node; Add its out to the current list (zero-width
4253 * match). */
4254 addstate_here(thislist, t->state->out1->out, &t->subs,
4255 pim, &listidx);
4256 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004258 break;
4259
4260 case NFA_BOL:
4261 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004262 addstate_here(thislist, t->state->out, &t->subs,
4263 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004264 break;
4265
4266 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004267 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004268 addstate_here(thislist, t->state->out, &t->subs,
4269 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004270 break;
4271
4272 case NFA_BOW:
4273 {
4274 int bow = TRUE;
4275
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004276 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004277 bow = FALSE;
4278#ifdef FEAT_MBYTE
4279 else if (has_mbyte)
4280 {
4281 int this_class;
4282
4283 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004284 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004285 if (this_class <= 1)
4286 bow = FALSE;
4287 else if (reg_prev_class() == this_class)
4288 bow = FALSE;
4289 }
4290#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004291 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004292 || (reginput > regline
4293 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294 bow = FALSE;
4295 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004296 addstate_here(thislist, t->state->out, &t->subs,
4297 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298 break;
4299 }
4300
4301 case NFA_EOW:
4302 {
4303 int eow = TRUE;
4304
4305 if (reginput == regline)
4306 eow = FALSE;
4307#ifdef FEAT_MBYTE
4308 else if (has_mbyte)
4309 {
4310 int this_class, prev_class;
4311
4312 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004313 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004314 prev_class = reg_prev_class();
4315 if (this_class == prev_class
4316 || prev_class == 0 || prev_class == 1)
4317 eow = FALSE;
4318 }
4319#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004320 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004321 || (reginput[0] != NUL
4322 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323 eow = FALSE;
4324 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004325 addstate_here(thislist, t->state->out, &t->subs,
4326 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004327 break;
4328 }
4329
Bram Moolenaar4b780632013-05-31 22:14:52 +02004330 case NFA_BOF:
4331 if (reglnum == 0 && reginput == regline
4332 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004333 addstate_here(thislist, t->state->out, &t->subs,
4334 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004335 break;
4336
4337 case NFA_EOF:
4338 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004339 addstate_here(thislist, t->state->out, &t->subs,
4340 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004341 break;
4342
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004343#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004344 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004345 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004346 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004347 int len = 0;
4348 nfa_state_T *end;
4349 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004350 int cchars[MAX_MCO];
4351 int ccount = 0;
4352 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004355 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004356 if (utf_iscomposing(sta->c))
4357 {
4358 /* Only match composing character(s), ignore base
4359 * character. Used for ".{composing}" and "{composing}"
4360 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004361 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004362 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004363 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004364 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004365 /* If \Z was present, then ignore composing characters.
4366 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004367 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004368 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004369 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004370 else
4371 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004372 while (sta->c != NFA_END_COMPOSING)
4373 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004374 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004375
4376 /* Check base character matches first, unless ignored. */
4377 else if (len > 0 || mc == sta->c)
4378 {
4379 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004380 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004381 len += mb_char2len(mc);
4382 sta = sta->out;
4383 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004384
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004385 /* We don't care about the order of composing characters.
4386 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004387 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004388 {
4389 mc = mb_ptr2char(reginput + len);
4390 cchars[ccount++] = mc;
4391 len += mb_char2len(mc);
4392 if (ccount == MAX_MCO)
4393 break;
4394 }
4395
4396 /* Check that each composing char in the pattern matches a
4397 * composing char in the text. We do not check if all
4398 * composing chars are matched. */
4399 result = OK;
4400 while (sta->c != NFA_END_COMPOSING)
4401 {
4402 for (j = 0; j < ccount; ++j)
4403 if (cchars[j] == sta->c)
4404 break;
4405 if (j == ccount)
4406 {
4407 result = FAIL;
4408 break;
4409 }
4410 sta = sta->out;
4411 }
4412 }
4413 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004414 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004415
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004416 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004417 ADD_POS_NEG_STATE(end);
4418 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004419 }
4420#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004421
4422 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004423 if (curc == NUL && !reg_line_lbr && REG_MULTI
4424 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004425 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004426 go_to_nextline = TRUE;
4427 /* Pass -1 for the offset, which means taking the position
4428 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004429 ll = nextlist;
4430 add_state = t->state->out;
4431 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004432 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004433 else if (curc == '\n' && reg_line_lbr)
4434 {
4435 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004436 ll = nextlist;
4437 add_state = t->state->out;
4438 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004439 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004440 break;
4441
4442 case NFA_CLASS_ALNUM:
4443 case NFA_CLASS_ALPHA:
4444 case NFA_CLASS_BLANK:
4445 case NFA_CLASS_CNTRL:
4446 case NFA_CLASS_DIGIT:
4447 case NFA_CLASS_GRAPH:
4448 case NFA_CLASS_LOWER:
4449 case NFA_CLASS_PRINT:
4450 case NFA_CLASS_PUNCT:
4451 case NFA_CLASS_SPACE:
4452 case NFA_CLASS_UPPER:
4453 case NFA_CLASS_XDIGIT:
4454 case NFA_CLASS_TAB:
4455 case NFA_CLASS_RETURN:
4456 case NFA_CLASS_BACKSPACE:
4457 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004458 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004459 ADD_POS_NEG_STATE(t->state);
4460 break;
4461
4462 case NFA_END_NEG_RANGE:
4463 /* This follows a series of negated nodes, like:
4464 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004465 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004466 {
4467 ll = nextlist;
4468 add_state = t->state->out;
4469 add_off = clen;
4470 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004471 break;
4472
4473 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004474 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004475 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004476 {
4477 ll = nextlist;
4478 add_state = t->state->out;
4479 add_off = clen;
4480 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004481 break;
4482
4483 /*
4484 * Character classes like \a for alpha, \d for digit etc.
4485 */
4486 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004487 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004488 ADD_POS_NEG_STATE(t->state);
4489 break;
4490
4491 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004492 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004493 ADD_POS_NEG_STATE(t->state);
4494 break;
4495
4496 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004497 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004498 ADD_POS_NEG_STATE(t->state);
4499 break;
4500
4501 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004502 result = !VIM_ISDIGIT(curc)
4503 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004504 ADD_POS_NEG_STATE(t->state);
4505 break;
4506
4507 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004508 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004509 ADD_POS_NEG_STATE(t->state);
4510 break;
4511
4512 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004513 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004514 ADD_POS_NEG_STATE(t->state);
4515 break;
4516
4517 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004518 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 ADD_POS_NEG_STATE(t->state);
4520 break;
4521
4522 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004523 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 ADD_POS_NEG_STATE(t->state);
4525 break;
4526
4527 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004528 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529 ADD_POS_NEG_STATE(t->state);
4530 break;
4531
4532 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004533 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004534 ADD_POS_NEG_STATE(t->state);
4535 break;
4536
4537 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004538 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539 ADD_POS_NEG_STATE(t->state);
4540 break;
4541
4542 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004543 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004544 ADD_POS_NEG_STATE(t->state);
4545 break;
4546
4547 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004548 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004549 ADD_POS_NEG_STATE(t->state);
4550 break;
4551
4552 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004553 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 ADD_POS_NEG_STATE(t->state);
4555 break;
4556
4557 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004558 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 ADD_POS_NEG_STATE(t->state);
4560 break;
4561
4562 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004563 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004564 ADD_POS_NEG_STATE(t->state);
4565 break;
4566
4567 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004568 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004569 ADD_POS_NEG_STATE(t->state);
4570 break;
4571
4572 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004573 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004574 ADD_POS_NEG_STATE(t->state);
4575 break;
4576
4577 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004578 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004579 ADD_POS_NEG_STATE(t->state);
4580 break;
4581
4582 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004583 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004584 ADD_POS_NEG_STATE(t->state);
4585 break;
4586
4587 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004588 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004589 ADD_POS_NEG_STATE(t->state);
4590 break;
4591
4592 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004593 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004594 ADD_POS_NEG_STATE(t->state);
4595 break;
4596
4597 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004598 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004599 ADD_POS_NEG_STATE(t->state);
4600 break;
4601
4602 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004603 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004604 ADD_POS_NEG_STATE(t->state);
4605 break;
4606
4607 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004608 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 ADD_POS_NEG_STATE(t->state);
4610 break;
4611
4612 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004613 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004614 ADD_POS_NEG_STATE(t->state);
4615 break;
4616
Bram Moolenaar5714b802013-05-28 22:03:20 +02004617 case NFA_BACKREF1:
4618 case NFA_BACKREF2:
4619 case NFA_BACKREF3:
4620 case NFA_BACKREF4:
4621 case NFA_BACKREF5:
4622 case NFA_BACKREF6:
4623 case NFA_BACKREF7:
4624 case NFA_BACKREF8:
4625 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004626#ifdef FEAT_SYN_HL
4627 case NFA_ZREF1:
4628 case NFA_ZREF2:
4629 case NFA_ZREF3:
4630 case NFA_ZREF4:
4631 case NFA_ZREF5:
4632 case NFA_ZREF6:
4633 case NFA_ZREF7:
4634 case NFA_ZREF8:
4635 case NFA_ZREF9:
4636#endif
4637 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004638 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004639 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004640 int bytelen;
4641
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004642 if (t->state->c <= NFA_BACKREF9)
4643 {
4644 subidx = t->state->c - NFA_BACKREF1 + 1;
4645 result = match_backref(&t->subs.norm, subidx, &bytelen);
4646 }
4647#ifdef FEAT_SYN_HL
4648 else
4649 {
4650 subidx = t->state->c - NFA_ZREF1 + 1;
4651 result = match_zref(subidx, &bytelen);
4652 }
4653#endif
4654
Bram Moolenaar5714b802013-05-28 22:03:20 +02004655 if (result)
4656 {
4657 if (bytelen == 0)
4658 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004659 /* empty match always works, output of NFA_SKIP to be
4660 * used next */
4661 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004662 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004663 }
4664 else if (bytelen <= clen)
4665 {
4666 /* match current character, jump ahead to out of
4667 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004668 ll = nextlist;
4669 add_state = t->state->out->out;
4670 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004671#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004672 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004673#endif
4674 }
4675 else
4676 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004677 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004678 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004679 ll = nextlist;
4680 add_state = t->state->out;
4681 add_off = bytelen;
4682 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004683#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004684 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004685#endif
4686 }
4687
4688 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004689 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004690 }
4691 case NFA_SKIP:
4692 /* charater of previous matching \1 .. \9 */
4693 if (t->count - clen <= 0)
4694 {
4695 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004696 ll = nextlist;
4697 add_state = t->state->out;
4698 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004699#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004700 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004701#endif
4702 }
4703 else
4704 {
4705 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004706 ll = nextlist;
4707 add_state = t->state;
4708 add_off = 0;
4709 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004710#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004711 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004712#endif
4713 }
4714 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004715
4716 case NFA_SKIP_CHAR:
4717 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004718 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004719 /* TODO: should not happen? */
4720 break;
4721
Bram Moolenaar423532e2013-05-29 21:14:42 +02004722 case NFA_LNUM:
4723 case NFA_LNUM_GT:
4724 case NFA_LNUM_LT:
4725 result = (REG_MULTI &&
4726 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4727 (long_u)(reglnum + reg_firstlnum)));
4728 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004729 addstate_here(thislist, t->state->out, &t->subs,
4730 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004731 break;
4732
4733 case NFA_COL:
4734 case NFA_COL_GT:
4735 case NFA_COL_LT:
4736 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4737 (long_u)(reginput - regline) + 1);
4738 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004739 addstate_here(thislist, t->state->out, &t->subs,
4740 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004741 break;
4742
4743 case NFA_VCOL:
4744 case NFA_VCOL_GT:
4745 case NFA_VCOL_LT:
4746 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4747 (long_u)win_linetabsize(
4748 reg_win == NULL ? curwin : reg_win,
4749 regline, (colnr_T)(reginput - regline)) + 1);
4750 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004751 addstate_here(thislist, t->state->out, &t->subs,
4752 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004753 break;
4754
Bram Moolenaar044aa292013-06-04 21:27:38 +02004755 case NFA_MARK:
4756 case NFA_MARK_GT:
4757 case NFA_MARK_LT:
4758 {
4759 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
4760
4761 /* Compare the mark position to the match position. */
4762 result = (pos != NULL /* mark doesn't exist */
4763 && pos->lnum > 0 /* mark isn't set in reg_buf */
4764 && (pos->lnum == reglnum + reg_firstlnum
4765 ? (pos->col == (colnr_T)(reginput - regline)
4766 ? t->state->c == NFA_MARK
4767 : (pos->col < (colnr_T)(reginput - regline)
4768 ? t->state->c == NFA_MARK_GT
4769 : t->state->c == NFA_MARK_LT))
4770 : (pos->lnum < reglnum + reg_firstlnum
4771 ? t->state->c == NFA_MARK_GT
4772 : t->state->c == NFA_MARK_LT)));
4773 if (result)
4774 addstate_here(thislist, t->state->out, &t->subs,
4775 t->pim, &listidx);
4776 break;
4777 }
4778
Bram Moolenaar423532e2013-05-29 21:14:42 +02004779 case NFA_CURSOR:
4780 result = (reg_win != NULL
4781 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4782 && ((colnr_T)(reginput - regline)
4783 == reg_win->w_cursor.col));
4784 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004785 addstate_here(thislist, t->state->out, &t->subs,
4786 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004787 break;
4788
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004789 case NFA_VISUAL:
4790 result = reg_match_visual();
4791 if (result)
4792 addstate_here(thislist, t->state->out, &t->subs,
4793 t->pim, &listidx);
4794 break;
4795
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004796 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004797 {
4798 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004799
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004800 /* TODO: put this in #ifdef later */
4801 if (c < -256)
4802 EMSGN("INTERNAL: Negative state char: %ld", c);
4803 if (is_Magic(c))
4804 c = un_Magic(c);
4805 result = (c == curc);
4806
4807 if (!result && ireg_ic)
4808 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004809#ifdef FEAT_MBYTE
4810 /* If there is a composing character which is not being
4811 * ignored there can be no match. Match with composing
4812 * character uses NFA_COMPOSING above. */
4813 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004814 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004815 result = FALSE;
4816#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004817 ADD_POS_NEG_STATE(t->state);
4818 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004819 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02004820
4821 } /* switch (t->state->c) */
4822
4823 if (add_state != NULL)
4824 {
4825 if (t->pim != NULL)
4826 {
4827 /* postponed invisible match */
4828 /* TODO: also do t->pim->pim recursively? */
4829 if (t->pim->result == NFA_PIM_TODO)
4830 {
4831#ifdef ENABLE_LOG
4832 fprintf(log_fd, "\n");
4833 fprintf(log_fd, "==================================\n");
4834 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
4835 fprintf(log_fd, "\n");
4836#endif
4837 result = recursive_regmatch(t->pim->state,
4838 prog, submatch, m, &listids);
4839 t->pim->result = result ? NFA_PIM_MATCH
4840 : NFA_PIM_NOMATCH;
4841 /* for \@! it is a match when result is FALSE */
4842 if (result != t->pim->state->negated)
4843 {
4844 /* Copy submatch info from the recursive call */
4845 copy_sub_off(&t->pim->subs.norm, &m->norm);
4846#ifdef FEAT_SYN_HL
4847 copy_sub_off(&t->pim->subs.synt, &m->synt);
4848#endif
4849 }
4850 }
4851 else
4852 {
4853 result = (t->pim->result == NFA_PIM_MATCH);
4854#ifdef ENABLE_LOG
4855 fprintf(log_fd, "\n");
4856 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
4857 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4858 fprintf(log_fd, "\n");
4859#endif
4860 }
4861
4862 /* for \@! it is a match when result is FALSE */
4863 if (result != t->pim->state->negated)
4864 {
4865 /* Copy submatch info from the recursive call */
4866 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
4867#ifdef FEAT_SYN_HL
4868 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
4869#endif
4870 }
4871 else
4872 /* look-behind match failed, don't add the state */
4873 continue;
4874 }
4875
4876 addstate(ll, add_state, &t->subs, add_off);
4877 if (add_count > 0)
4878 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004879 }
4880
4881 } /* for (thislist = thislist; thislist->state; thislist++) */
4882
Bram Moolenaare23febd2013-05-26 18:40:14 +02004883 /* Look for the start of a match in the current position by adding the
4884 * start state to the list of states.
4885 * The first found match is the leftmost one, thus the order of states
4886 * matters!
4887 * Do not add the start state in recursive calls of nfa_regmatch(),
4888 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004889 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004890 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004891 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004892 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004893 && reglnum == 0
4894 && clen != 0
4895 && (ireg_maxcol == 0
4896 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004897 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004898 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004899 ? (reglnum < nfa_endp->se_u.pos.lnum
4900 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004901 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004902 < nfa_endp->se_u.pos.col))
4903 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004904 {
4905#ifdef ENABLE_LOG
4906 fprintf(log_fd, "(---) STARTSTATE\n");
4907#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004908 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004909 }
4910
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004911#ifdef ENABLE_LOG
4912 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004913 {
4914 int i;
4915
4916 for (i = 0; i < thislist->n; i++)
4917 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4918 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004919 fprintf(log_fd, "\n");
4920#endif
4921
4922nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004923 /* Advance to the next character, or advance to the next line, or
4924 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004925 if (clen != 0)
4926 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004927 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4928 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004929 reg_nextline();
4930 else
4931 break;
4932 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004933
4934#ifdef ENABLE_LOG
4935 if (log_fd != stderr)
4936 fclose(log_fd);
4937 log_fd = NULL;
4938#endif
4939
4940theend:
4941 /* Free memory */
4942 vim_free(list[0].t);
4943 vim_free(list[1].t);
4944 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004945 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004946 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004947#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004948#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004949 fclose(debug);
4950#endif
4951
Bram Moolenaar963fee22013-05-26 21:47:28 +02004952 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004953}
4954
4955/*
4956 * Try match of "prog" with at regline["col"].
4957 * Returns 0 for failure, number of lines contained in the match otherwise.
4958 */
4959 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004960nfa_regtry(prog, col)
4961 nfa_regprog_T *prog;
4962 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004963{
4964 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004965 regsubs_T subs, m;
4966 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004967#ifdef ENABLE_LOG
4968 FILE *f;
4969#endif
4970
4971 reginput = regline + col;
4972 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004973#ifdef FEAT_SYN_HL
4974 /* Clear the external match subpointers if necessary. */
4975 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004976 {
4977 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004978 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004979 }
4980 else
4981 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004982#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004983
4984#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004985 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004986 if (f != NULL)
4987 {
4988 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4989 fprintf(f, " =======================================================\n");
4990#ifdef DEBUG
4991 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4992#endif
4993 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004994 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004995 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004996 fprintf(f, "\n\n");
4997 fclose(f);
4998 }
4999 else
5000 EMSG(_("Could not open temporary log file for writing "));
5001#endif
5002
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005003 clear_sub(&subs.norm);
5004 clear_sub(&m.norm);
5005#ifdef FEAT_SYN_HL
5006 clear_sub(&subs.synt);
5007 clear_sub(&m.synt);
5008#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005009
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005010 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005011 return 0;
5012
5013 cleanup_subexpr();
5014 if (REG_MULTI)
5015 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005016 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005017 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005018 reg_startpos[i] = subs.norm.list.multi[i].start;
5019 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005020 }
5021
5022 if (reg_startpos[0].lnum < 0)
5023 {
5024 reg_startpos[0].lnum = 0;
5025 reg_startpos[0].col = col;
5026 }
5027 if (reg_endpos[0].lnum < 0)
5028 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005029 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030 reg_endpos[0].lnum = reglnum;
5031 reg_endpos[0].col = (int)(reginput - regline);
5032 }
5033 else
5034 /* Use line number of "\ze". */
5035 reglnum = reg_endpos[0].lnum;
5036 }
5037 else
5038 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005039 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005040 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005041 reg_startp[i] = subs.norm.list.line[i].start;
5042 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005043 }
5044
5045 if (reg_startp[0] == NULL)
5046 reg_startp[0] = regline + col;
5047 if (reg_endp[0] == NULL)
5048 reg_endp[0] = reginput;
5049 }
5050
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005051#ifdef FEAT_SYN_HL
5052 /* Package any found \z(...\) matches for export. Default is none. */
5053 unref_extmatch(re_extmatch_out);
5054 re_extmatch_out = NULL;
5055
5056 if (prog->reghasz == REX_SET)
5057 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005058 cleanup_zsubexpr();
5059 re_extmatch_out = make_extmatch();
5060 for (i = 0; i < subs.synt.in_use; i++)
5061 {
5062 if (REG_MULTI)
5063 {
5064 struct multipos *mpos = &subs.synt.list.multi[i];
5065
5066 /* Only accept single line matches. */
5067 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5068 re_extmatch_out->matches[i] =
5069 vim_strnsave(reg_getline(mpos->start.lnum)
5070 + mpos->start.col,
5071 mpos->end.col - mpos->start.col);
5072 }
5073 else
5074 {
5075 struct linepos *lpos = &subs.synt.list.line[i];
5076
5077 if (lpos->start != NULL && lpos->end != NULL)
5078 re_extmatch_out->matches[i] =
5079 vim_strnsave(lpos->start,
5080 (int)(lpos->end - lpos->start));
5081 }
5082 }
5083 }
5084#endif
5085
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005086 return 1 + reglnum;
5087}
5088
5089/*
5090 * Match a regexp against a string ("line" points to the string) or multiple
5091 * lines ("line" is NULL, use reg_getline()).
5092 *
5093 * Returns 0 for failure, number of lines contained in the match otherwise.
5094 */
5095 static long
5096nfa_regexec_both(line, col)
5097 char_u *line;
5098 colnr_T col; /* column to start looking for match */
5099{
5100 nfa_regprog_T *prog;
5101 long retval = 0L;
5102 int i;
5103
5104 if (REG_MULTI)
5105 {
5106 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5107 line = reg_getline((linenr_T)0); /* relative to the cursor */
5108 reg_startpos = reg_mmatch->startpos;
5109 reg_endpos = reg_mmatch->endpos;
5110 }
5111 else
5112 {
5113 prog = (nfa_regprog_T *)reg_match->regprog;
5114 reg_startp = reg_match->startp;
5115 reg_endp = reg_match->endp;
5116 }
5117
5118 /* Be paranoid... */
5119 if (prog == NULL || line == NULL)
5120 {
5121 EMSG(_(e_null));
5122 goto theend;
5123 }
5124
5125 /* If the start column is past the maximum column: no need to try. */
5126 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5127 goto theend;
5128
5129 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5130 if (prog->regflags & RF_ICASE)
5131 ireg_ic = TRUE;
5132 else if (prog->regflags & RF_NOICASE)
5133 ireg_ic = FALSE;
5134
5135#ifdef FEAT_MBYTE
5136 /* If pattern contains "\Z" overrule value of ireg_icombine */
5137 if (prog->regflags & RF_ICOMBINE)
5138 ireg_icombine = TRUE;
5139#endif
5140
5141 regline = line;
5142 reglnum = 0; /* relative to line */
5143
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005144 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005145 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005146 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005147 nfa_listid = 1;
5148 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005149#ifdef DEBUG
5150 nfa_regengine.expr = prog->pattern;
5151#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005152
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005153 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005154 for (i = 0; i < nstate; ++i)
5155 {
5156 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005157 prog->state[i].lastlist[0] = 0;
5158 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005159 }
5160
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005161 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005162
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005163#ifdef DEBUG
5164 nfa_regengine.expr = NULL;
5165#endif
5166
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005167theend:
5168 return retval;
5169}
5170
5171/*
5172 * Compile a regular expression into internal code for the NFA matcher.
5173 * Returns the program in allocated space. Returns NULL for an error.
5174 */
5175 static regprog_T *
5176nfa_regcomp(expr, re_flags)
5177 char_u *expr;
5178 int re_flags;
5179{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005180 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005181 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005182 int *postfix;
5183
5184 if (expr == NULL)
5185 return NULL;
5186
5187#ifdef DEBUG
5188 nfa_regengine.expr = expr;
5189#endif
5190
5191 init_class_tab();
5192
5193 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5194 return NULL;
5195
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005196 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005197 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005198 postfix = re2post();
5199 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005200 {
5201 /* TODO: only give this error for debugging? */
5202 if (post_ptr >= post_end)
5203 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005204 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005205 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005206
5207 /*
5208 * In order to build the NFA, we parse the input regexp twice:
5209 * 1. first pass to count size (so we can allocate space)
5210 * 2. second to emit code
5211 */
5212#ifdef ENABLE_LOG
5213 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005214 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005215
5216 if (f != NULL)
5217 {
5218 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5219 fclose(f);
5220 }
5221 }
5222#endif
5223
5224 /*
5225 * PASS 1
5226 * Count number of NFA states in "nstate". Do not build the NFA.
5227 */
5228 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005229
5230 /* Space for compiled regexp */
5231 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5232 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5233 if (prog == NULL)
5234 goto fail;
5235 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005236 state_ptr = prog->state;
5237
5238 /*
5239 * PASS 2
5240 * Build the NFA
5241 */
5242 prog->start = post2nfa(postfix, post_ptr, FALSE);
5243 if (prog->start == NULL)
5244 goto fail;
5245
5246 prog->regflags = regflags;
5247 prog->engine = &nfa_regengine;
5248 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005249 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005250 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005251 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005252#ifdef ENABLE_LOG
5253 nfa_postfix_dump(expr, OK);
5254 nfa_dump(prog);
5255#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005256#ifdef FEAT_SYN_HL
5257 /* Remember whether this pattern has any \z specials in it. */
5258 prog->reghasz = re_has_z;
5259#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005260#ifdef DEBUG
5261 prog->pattern = vim_strsave(expr); /* memory will leak */
5262 nfa_regengine.expr = NULL;
5263#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005264
5265out:
5266 vim_free(post_start);
5267 post_start = post_ptr = post_end = NULL;
5268 state_ptr = NULL;
5269 return (regprog_T *)prog;
5270
5271fail:
5272 vim_free(prog);
5273 prog = NULL;
5274#ifdef ENABLE_LOG
5275 nfa_postfix_dump(expr, FAIL);
5276#endif
5277#ifdef DEBUG
5278 nfa_regengine.expr = NULL;
5279#endif
5280 goto out;
5281}
5282
5283
5284/*
5285 * Match a regexp against a string.
5286 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5287 * Uses curbuf for line count and 'iskeyword'.
5288 *
5289 * Return TRUE if there is a match, FALSE if not.
5290 */
5291 static int
5292nfa_regexec(rmp, line, col)
5293 regmatch_T *rmp;
5294 char_u *line; /* string to match against */
5295 colnr_T col; /* column to start looking for match */
5296{
5297 reg_match = rmp;
5298 reg_mmatch = NULL;
5299 reg_maxline = 0;
5300 reg_line_lbr = FALSE;
5301 reg_buf = curbuf;
5302 reg_win = NULL;
5303 ireg_ic = rmp->rm_ic;
5304#ifdef FEAT_MBYTE
5305 ireg_icombine = FALSE;
5306#endif
5307 ireg_maxcol = 0;
5308 return (nfa_regexec_both(line, col) != 0);
5309}
5310
5311#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5312 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5313
5314static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5315
5316/*
5317 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5318 */
5319 static int
5320nfa_regexec_nl(rmp, line, col)
5321 regmatch_T *rmp;
5322 char_u *line; /* string to match against */
5323 colnr_T col; /* column to start looking for match */
5324{
5325 reg_match = rmp;
5326 reg_mmatch = NULL;
5327 reg_maxline = 0;
5328 reg_line_lbr = TRUE;
5329 reg_buf = curbuf;
5330 reg_win = NULL;
5331 ireg_ic = rmp->rm_ic;
5332#ifdef FEAT_MBYTE
5333 ireg_icombine = FALSE;
5334#endif
5335 ireg_maxcol = 0;
5336 return (nfa_regexec_both(line, col) != 0);
5337}
5338#endif
5339
5340
5341/*
5342 * Match a regexp against multiple lines.
5343 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5344 * Uses curbuf for line count and 'iskeyword'.
5345 *
5346 * Return zero if there is no match. Return number of lines contained in the
5347 * match otherwise.
5348 *
5349 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5350 *
5351 * ! Also NOTE : match may actually be in another line. e.g.:
5352 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5353 *
5354 * +-------------------------+
5355 * |a |
5356 * |b |
5357 * |c |
5358 * | |
5359 * +-------------------------+
5360 *
5361 * then nfa_regexec_multi() returns 3. while the original
5362 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5363 *
5364 * FIXME if this behavior is not compatible.
5365 */
5366 static long
5367nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5368 regmmatch_T *rmp;
5369 win_T *win; /* window in which to search or NULL */
5370 buf_T *buf; /* buffer in which to search */
5371 linenr_T lnum; /* nr of line to start looking for match */
5372 colnr_T col; /* column to start looking for match */
5373 proftime_T *tm UNUSED; /* timeout limit or NULL */
5374{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005375 reg_match = NULL;
5376 reg_mmatch = rmp;
5377 reg_buf = buf;
5378 reg_win = win;
5379 reg_firstlnum = lnum;
5380 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5381 reg_line_lbr = FALSE;
5382 ireg_ic = rmp->rmm_ic;
5383#ifdef FEAT_MBYTE
5384 ireg_icombine = FALSE;
5385#endif
5386 ireg_maxcol = rmp->rmm_maxcol;
5387
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005388 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005389}
5390
5391#ifdef DEBUG
5392# undef ENABLE_LOG
5393#endif