blob: e7db49930d1c82edb71183457e4e79106479bec0 [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 Moolenaar87953742013-06-05 18:52:40 +020060 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020061 NFA_END_INVISIBLE,
Bram Moolenaar87953742013-06-05 18:52:40 +020062 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020063 NFA_COMPOSING, /* Next nodes in NFA are part of the
64 composing multibyte char */
65 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020066 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020067
68 /* The following are used only in the postfix form, not in the NFA */
69 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
70 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
71 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
72 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
73 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
74
Bram Moolenaar5714b802013-05-28 22:03:20 +020075 NFA_BACKREF1, /* \1 */
76 NFA_BACKREF2, /* \2 */
77 NFA_BACKREF3, /* \3 */
78 NFA_BACKREF4, /* \4 */
79 NFA_BACKREF5, /* \5 */
80 NFA_BACKREF6, /* \6 */
81 NFA_BACKREF7, /* \7 */
82 NFA_BACKREF8, /* \8 */
83 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020084#ifdef FEAT_SYN_HL
85 NFA_ZREF1, /* \z1 */
86 NFA_ZREF2, /* \z2 */
87 NFA_ZREF3, /* \z3 */
88 NFA_ZREF4, /* \z4 */
89 NFA_ZREF5, /* \z5 */
90 NFA_ZREF6, /* \z6 */
91 NFA_ZREF7, /* \z7 */
92 NFA_ZREF8, /* \z8 */
93 NFA_ZREF9, /* \z9 */
94#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +020095 NFA_SKIP, /* Skip characters */
96
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020097 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +020098 NFA_MOPEN1,
99 NFA_MOPEN2,
100 NFA_MOPEN3,
101 NFA_MOPEN4,
102 NFA_MOPEN5,
103 NFA_MOPEN6,
104 NFA_MOPEN7,
105 NFA_MOPEN8,
106 NFA_MOPEN9,
107
108 NFA_MCLOSE,
109 NFA_MCLOSE1,
110 NFA_MCLOSE2,
111 NFA_MCLOSE3,
112 NFA_MCLOSE4,
113 NFA_MCLOSE5,
114 NFA_MCLOSE6,
115 NFA_MCLOSE7,
116 NFA_MCLOSE8,
117 NFA_MCLOSE9,
118
119#ifdef FEAT_SYN_HL
120 NFA_ZOPEN,
121 NFA_ZOPEN1,
122 NFA_ZOPEN2,
123 NFA_ZOPEN3,
124 NFA_ZOPEN4,
125 NFA_ZOPEN5,
126 NFA_ZOPEN6,
127 NFA_ZOPEN7,
128 NFA_ZOPEN8,
129 NFA_ZOPEN9,
130
131 NFA_ZCLOSE,
132 NFA_ZCLOSE1,
133 NFA_ZCLOSE2,
134 NFA_ZCLOSE3,
135 NFA_ZCLOSE4,
136 NFA_ZCLOSE5,
137 NFA_ZCLOSE6,
138 NFA_ZCLOSE7,
139 NFA_ZCLOSE8,
140 NFA_ZCLOSE9,
141#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200142
143 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200144 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200145 NFA_ANYOF, /* Match any character in this string. */
146 NFA_ANYBUT, /* Match any character not in this string. */
147 NFA_IDENT, /* Match identifier char */
148 NFA_SIDENT, /* Match identifier char but no digit */
149 NFA_KWORD, /* Match keyword char */
150 NFA_SKWORD, /* Match word char but no digit */
151 NFA_FNAME, /* Match file name char */
152 NFA_SFNAME, /* Match file name char but no digit */
153 NFA_PRINT, /* Match printable char */
154 NFA_SPRINT, /* Match printable char but no digit */
155 NFA_WHITE, /* Match whitespace char */
156 NFA_NWHITE, /* Match non-whitespace char */
157 NFA_DIGIT, /* Match digit char */
158 NFA_NDIGIT, /* Match non-digit char */
159 NFA_HEX, /* Match hex char */
160 NFA_NHEX, /* Match non-hex char */
161 NFA_OCTAL, /* Match octal char */
162 NFA_NOCTAL, /* Match non-octal char */
163 NFA_WORD, /* Match word char */
164 NFA_NWORD, /* Match non-word char */
165 NFA_HEAD, /* Match head char */
166 NFA_NHEAD, /* Match non-head char */
167 NFA_ALPHA, /* Match alpha char */
168 NFA_NALPHA, /* Match non-alpha char */
169 NFA_LOWER, /* Match lowercase char */
170 NFA_NLOWER, /* Match non-lowercase char */
171 NFA_UPPER, /* Match uppercase char */
172 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200173
174 NFA_CURSOR, /* Match cursor pos */
175 NFA_LNUM, /* Match line number */
176 NFA_LNUM_GT, /* Match > line number */
177 NFA_LNUM_LT, /* Match < line number */
178 NFA_COL, /* Match cursor column */
179 NFA_COL_GT, /* Match > cursor column */
180 NFA_COL_LT, /* Match < cursor column */
181 NFA_VCOL, /* Match cursor virtual column */
182 NFA_VCOL_GT, /* Match > cursor virtual column */
183 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200184 NFA_MARK, /* Match mark */
185 NFA_MARK_GT, /* Match > mark */
186 NFA_MARK_LT, /* Match < mark */
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200187#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200188 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200189#endif
Bram Moolenaar423532e2013-05-29 21:14:42 +0200190
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200191 NFA_FIRST_NL = NFA_ANY + ADD_NL,
192 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
193
194 /* Character classes [:alnum:] etc */
195 NFA_CLASS_ALNUM,
196 NFA_CLASS_ALPHA,
197 NFA_CLASS_BLANK,
198 NFA_CLASS_CNTRL,
199 NFA_CLASS_DIGIT,
200 NFA_CLASS_GRAPH,
201 NFA_CLASS_LOWER,
202 NFA_CLASS_PRINT,
203 NFA_CLASS_PUNCT,
204 NFA_CLASS_SPACE,
205 NFA_CLASS_UPPER,
206 NFA_CLASS_XDIGIT,
207 NFA_CLASS_TAB,
208 NFA_CLASS_RETURN,
209 NFA_CLASS_BACKSPACE,
210 NFA_CLASS_ESCAPE
211};
212
213/* Keep in sync with classchars. */
214static int nfa_classcodes[] = {
215 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
216 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
217 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
218 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
219 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
220 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
221 NFA_UPPER, NFA_NUPPER
222};
223
224static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
225
226/*
227 * NFA errors can be of 3 types:
228 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
229 * silently and revert the to backtracking engine.
230 * syntax_error = FALSE;
231 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
232 * The NFA engine displays an error message, and nothing else happens.
233 * syntax_error = TRUE
234 * *** Unsupported features, when the input regexp uses an operator that is not
235 * implemented in the NFA. The NFA engine fails silently, and reverts to the
236 * old backtracking engine.
237 * syntax_error = FALSE
238 * "The NFA fails" means that "compiling the regexp with the NFA fails":
239 * nfa_regcomp() returns FAIL.
240 */
241static int syntax_error = FALSE;
242
243/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200244static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200245
Bram Moolenaar428e9872013-05-30 17:05:39 +0200246/* NFA regexp \1 .. \9 encountered. */
247static int nfa_has_backref;
248
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200249#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200250/* NFA regexp has \z( ), set zsubexpr. */
251static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200252#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200253
Bram Moolenaar963fee22013-05-26 21:47:28 +0200254/* Number of sub expressions actually being used during execution. 1 if only
255 * the whole match (subexpr 0) is used. */
256static int nfa_nsubexpr;
257
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200258static int *post_start; /* holds the postfix form of r.e. */
259static int *post_end;
260static int *post_ptr;
261
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200262static int nstate; /* Number of states in the NFA. Also used when
263 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200264static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265
Bram Moolenaar307aa162013-06-02 16:34:21 +0200266/* If not NULL match must end at this position */
267static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200269/* listid is global, so that it increases on recursive calls to
270 * nfa_regmatch(), which means we don't have to clear the lastlist field of
271 * all the states. */
272static int nfa_listid;
273static int nfa_alt_listid;
274
275/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
276static int nfa_ll_index = 0;
277
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200278static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
279static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
280static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200281static int nfa_regatom __ARGS((void));
282static int nfa_regpiece __ARGS((void));
283static int nfa_regconcat __ARGS((void));
284static int nfa_regbranch __ARGS((void));
285static int nfa_reg __ARGS((int paren));
286#ifdef DEBUG
287static void nfa_set_code __ARGS((int c));
288static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200289static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
290static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200291static void nfa_dump __ARGS((nfa_regprog_T *prog));
292#endif
293static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200294static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
296static int check_char_class __ARGS((int class, int c));
297static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200298static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
299static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200300static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200301static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
303static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
304static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
305static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
306
307/* helper functions used when doing re2post() ... regatom() parsing */
308#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200309 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310 return FAIL; \
311 *post_ptr++ = c; \
312 } while (0)
313
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200314/*
315 * Initialize internal variables before NFA compilation.
316 * Return OK on success, FAIL otherwise.
317 */
318 static int
319nfa_regcomp_start(expr, re_flags)
320 char_u *expr;
321 int re_flags; /* see vim_regcomp() */
322{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200323 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200324 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200325
326 nstate = 0;
327 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200328 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200329 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200331 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200332 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200333 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200334
335 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200336 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200337
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338 post_start = (int *)lalloc(postfix_size, TRUE);
339 if (post_start == NULL)
340 return FAIL;
341 vim_memset(post_start, 0, postfix_size);
342 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200343 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200345 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200347 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200348 regcomp_start(expr, re_flags);
349
350 return OK;
351}
352
353/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200354 * Allocate more space for post_start. Called when
355 * running above the estimated number of states.
356 */
357 static int
358realloc_post_list()
359{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200360 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200361 int new_max = nstate_max + 1000;
362 int *new_start;
363 int *old_start;
364
365 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
366 if (new_start == NULL)
367 return FAIL;
368 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
369 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
370 old_start = post_start;
371 post_start = new_start;
372 post_ptr = new_start + (post_ptr - old_start);
373 post_end = post_start + new_max;
374 vim_free(old_start);
375 return OK;
376}
377
378/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200379 * Search between "start" and "end" and try to recognize a
380 * character class in expanded form. For example [0-9].
381 * On success, return the id the character class to be emitted.
382 * On failure, return 0 (=FAIL)
383 * Start points to the first char of the range, while end should point
384 * to the closing brace.
385 */
386 static int
387nfa_recognize_char_class(start, end, extra_newl)
388 char_u *start;
389 char_u *end;
390 int extra_newl;
391{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200392# define CLASS_not 0x80
393# define CLASS_af 0x40
394# define CLASS_AF 0x20
395# define CLASS_az 0x10
396# define CLASS_AZ 0x08
397# define CLASS_o7 0x04
398# define CLASS_o9 0x02
399# define CLASS_underscore 0x01
400
401 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200402 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200403 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200404
405 if (extra_newl == TRUE)
406 newl = TRUE;
407
408 if (*end != ']')
409 return FAIL;
410 p = start;
411 if (*p == '^')
412 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200413 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200414 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200415 }
416
417 while (p < end)
418 {
419 if (p + 2 < end && *(p + 1) == '-')
420 {
421 switch (*p)
422 {
423 case '0':
424 if (*(p + 2) == '9')
425 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200426 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200427 break;
428 }
429 else
430 if (*(p + 2) == '7')
431 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200432 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200433 break;
434 }
435 case 'a':
436 if (*(p + 2) == 'z')
437 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200438 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200439 break;
440 }
441 else
442 if (*(p + 2) == 'f')
443 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200444 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200445 break;
446 }
447 case 'A':
448 if (*(p + 2) == 'Z')
449 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200450 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200451 break;
452 }
453 else
454 if (*(p + 2) == 'F')
455 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200456 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200457 break;
458 }
459 /* FALLTHROUGH */
460 default:
461 return FAIL;
462 }
463 p += 3;
464 }
465 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
466 {
467 newl = TRUE;
468 p += 2;
469 }
470 else if (*p == '_')
471 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200472 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200473 p ++;
474 }
475 else if (*p == '\n')
476 {
477 newl = TRUE;
478 p ++;
479 }
480 else
481 return FAIL;
482 } /* while (p < end) */
483
484 if (p != end)
485 return FAIL;
486
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200487 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200488 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200489
490 switch (config)
491 {
492 case CLASS_o9:
493 return extra_newl + NFA_DIGIT;
494 case CLASS_not | CLASS_o9:
495 return extra_newl + NFA_NDIGIT;
496 case CLASS_af | CLASS_AF | CLASS_o9:
497 return extra_newl + NFA_HEX;
498 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
499 return extra_newl + NFA_NHEX;
500 case CLASS_o7:
501 return extra_newl + NFA_OCTAL;
502 case CLASS_not | CLASS_o7:
503 return extra_newl + NFA_NOCTAL;
504 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
505 return extra_newl + NFA_WORD;
506 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
507 return extra_newl + NFA_NWORD;
508 case CLASS_az | CLASS_AZ | CLASS_underscore:
509 return extra_newl + NFA_HEAD;
510 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
511 return extra_newl + NFA_NHEAD;
512 case CLASS_az | CLASS_AZ:
513 return extra_newl + NFA_ALPHA;
514 case CLASS_not | CLASS_az | CLASS_AZ:
515 return extra_newl + NFA_NALPHA;
516 case CLASS_az:
517 return extra_newl + NFA_LOWER;
518 case CLASS_not | CLASS_az:
519 return extra_newl + NFA_NLOWER;
520 case CLASS_AZ:
521 return extra_newl + NFA_UPPER;
522 case CLASS_not | CLASS_AZ:
523 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200524 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200525 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200526}
527
528/*
529 * Produce the bytes for equivalence class "c".
530 * Currently only handles latin1, latin9 and utf-8.
531 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
532 * equivalent to 'a OR b OR c'
533 *
534 * NOTE! When changing this function, also update reg_equi_class()
535 */
536 static int
537nfa_emit_equi_class(c, neg)
538 int c;
539 int neg;
540{
541 int first = TRUE;
542 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
543#define EMIT2(c) \
544 EMIT(c); \
545 if (neg == TRUE) { \
546 EMIT(NFA_NOT); \
547 } \
548 if (first == FALSE) \
549 EMIT(glue); \
550 else \
551 first = FALSE; \
552
553#ifdef FEAT_MBYTE
554 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
555 || STRCMP(p_enc, "iso-8859-15") == 0)
556#endif
557 {
558 switch (c)
559 {
560 case 'A': case '\300': case '\301': case '\302':
561 case '\303': case '\304': case '\305':
562 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
563 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
564 EMIT2('\305');
565 return OK;
566
567 case 'C': case '\307':
568 EMIT2('C'); EMIT2('\307');
569 return OK;
570
571 case 'E': case '\310': case '\311': case '\312': case '\313':
572 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
573 EMIT2('\312'); EMIT2('\313');
574 return OK;
575
576 case 'I': case '\314': case '\315': case '\316': case '\317':
577 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
578 EMIT2('\316'); EMIT2('\317');
579 return OK;
580
581 case 'N': case '\321':
582 EMIT2('N'); EMIT2('\321');
583 return OK;
584
585 case 'O': case '\322': case '\323': case '\324': case '\325':
586 case '\326':
587 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
588 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
589 return OK;
590
591 case 'U': case '\331': case '\332': case '\333': case '\334':
592 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
593 EMIT2('\333'); EMIT2('\334');
594 return OK;
595
596 case 'Y': case '\335':
597 EMIT2('Y'); EMIT2('\335');
598 return OK;
599
600 case 'a': case '\340': case '\341': case '\342':
601 case '\343': case '\344': case '\345':
602 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
603 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
604 EMIT2('\345');
605 return OK;
606
607 case 'c': case '\347':
608 EMIT2('c'); EMIT2('\347');
609 return OK;
610
611 case 'e': case '\350': case '\351': case '\352': case '\353':
612 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
613 EMIT2('\352'); EMIT2('\353');
614 return OK;
615
616 case 'i': case '\354': case '\355': case '\356': case '\357':
617 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
618 EMIT2('\356'); EMIT2('\357');
619 return OK;
620
621 case 'n': case '\361':
622 EMIT2('n'); EMIT2('\361');
623 return OK;
624
625 case 'o': case '\362': case '\363': case '\364': case '\365':
626 case '\366':
627 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
628 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
629 return OK;
630
631 case 'u': case '\371': case '\372': case '\373': case '\374':
632 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
633 EMIT2('\373'); EMIT2('\374');
634 return OK;
635
636 case 'y': case '\375': case '\377':
637 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
638 return OK;
639
640 default:
641 return FAIL;
642 }
643 }
644
645 EMIT(c);
646 return OK;
647#undef EMIT2
648}
649
650/*
651 * Code to parse regular expression.
652 *
653 * We try to reuse parsing functions in regexp.c to
654 * minimize surprise and keep the syntax consistent.
655 */
656
657/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200658 * Parse the lowest level.
659 *
660 * An atom can be one of a long list of items. Many atoms match one character
661 * in the text. It is often an ordinary character or a character class.
662 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
663 * is only for syntax highlighting.
664 *
665 * atom ::= ordinary-atom
666 * or \( pattern \)
667 * or \%( pattern \)
668 * or \z( pattern \)
669 */
670 static int
671nfa_regatom()
672{
673 int c;
674 int charclass;
675 int equiclass;
676 int collclass;
677 int got_coll_char;
678 char_u *p;
679 char_u *endp;
680#ifdef FEAT_MBYTE
681 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200682#endif
683 int extra = 0;
684 int first;
685 int emit_range;
686 int negated;
687 int result;
688 int startc = -1;
689 int endc = -1;
690 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200691 int glue; /* ID that will "glue" nodes together */
692
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200693 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
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200966#ifdef FEAT_VISUAL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200967 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200968 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200969 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200970#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971
972 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200973 {
974 int n;
975
976 /* \%[abc] */
977 for (n = 0; (c = getchr()) != ']'; ++n)
978 {
979 if (c == NUL)
980 EMSG2_RET_FAIL(_(e_missing_sb),
981 reg_magic == MAGIC_ALL);
982 EMIT(c);
983 }
984 EMIT(NFA_OPT_CHARS);
985 EMIT(n);
986 break;
987 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200988
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200989 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200990 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200991 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200992 int cmp = c;
993
994 if (c == '<' || c == '>')
995 c = getchr();
996 while (VIM_ISDIGIT(c))
997 {
998 n = n * 10 + (c - '0');
999 c = getchr();
1000 }
1001 if (c == 'l' || c == 'c' || c == 'v')
1002 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001003 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001004 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001005 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001006 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001007 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001008 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001009 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001010 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001011 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001012 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001013 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001014 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001015 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001016 break;
1017 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001018 else if (c == '\'' && n == 0)
1019 {
1020 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001021 EMIT(cmp == '<' ? NFA_MARK_LT :
1022 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001023 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001024 break;
1025 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001026 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001027 syntax_error = TRUE;
1028 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1029 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001030 return FAIL;
1031 }
1032 break;
1033
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001034 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001035collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001036 /*
1037 * Glue is emitted between several atoms from the [].
1038 * It is either NFA_OR, or NFA_CONCAT.
1039 *
1040 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1041 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1042 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1043 * notation)
1044 *
1045 */
1046
1047
1048/* Emit negation atoms, if needed.
1049 * The CONCAT below merges the NOT with the previous node. */
1050#define TRY_NEG() \
1051 if (negated == TRUE) \
1052 { \
1053 EMIT(NFA_NOT); \
1054 }
1055
1056/* Emit glue between important nodes : CONCAT or OR. */
1057#define EMIT_GLUE() \
1058 if (first == FALSE) \
1059 EMIT(glue); \
1060 else \
1061 first = FALSE;
1062
1063 p = regparse;
1064 endp = skip_anyof(p);
1065 if (*endp == ']')
1066 {
1067 /*
1068 * Try to reverse engineer character classes. For example,
1069 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1070 * and perform the necessary substitutions in the NFA.
1071 */
1072 result = nfa_recognize_char_class(regparse, endp,
1073 extra == ADD_NL);
1074 if (result != FAIL)
1075 {
1076 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1077 EMIT(result);
1078 else /* must be char class + newline */
1079 {
1080 EMIT(result - ADD_NL);
1081 EMIT(NFA_NEWL);
1082 EMIT(NFA_OR);
1083 }
1084 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001085 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 return OK;
1087 }
1088 /*
1089 * Failed to recognize a character class. Use the simple
1090 * version that turns [abc] into 'a' OR 'b' OR 'c'
1091 */
1092 startc = endc = oldstartc = -1;
1093 first = TRUE; /* Emitting first atom in this sequence? */
1094 negated = FALSE;
1095 glue = NFA_OR;
1096 if (*regparse == '^') /* negated range */
1097 {
1098 negated = TRUE;
1099 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001100 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001101 }
1102 if (*regparse == '-')
1103 {
1104 startc = '-';
1105 EMIT(startc);
1106 TRY_NEG();
1107 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001108 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001109 }
1110 /* Emit the OR branches for each character in the [] */
1111 emit_range = FALSE;
1112 while (regparse < endp)
1113 {
1114 oldstartc = startc;
1115 startc = -1;
1116 got_coll_char = FALSE;
1117 if (*regparse == '[')
1118 {
1119 /* Check for [: :], [= =], [. .] */
1120 equiclass = collclass = 0;
1121 charclass = get_char_class(&regparse);
1122 if (charclass == CLASS_NONE)
1123 {
1124 equiclass = get_equi_class(&regparse);
1125 if (equiclass == 0)
1126 collclass = get_coll_element(&regparse);
1127 }
1128
1129 /* Character class like [:alpha:] */
1130 if (charclass != CLASS_NONE)
1131 {
1132 switch (charclass)
1133 {
1134 case CLASS_ALNUM:
1135 EMIT(NFA_CLASS_ALNUM);
1136 break;
1137 case CLASS_ALPHA:
1138 EMIT(NFA_CLASS_ALPHA);
1139 break;
1140 case CLASS_BLANK:
1141 EMIT(NFA_CLASS_BLANK);
1142 break;
1143 case CLASS_CNTRL:
1144 EMIT(NFA_CLASS_CNTRL);
1145 break;
1146 case CLASS_DIGIT:
1147 EMIT(NFA_CLASS_DIGIT);
1148 break;
1149 case CLASS_GRAPH:
1150 EMIT(NFA_CLASS_GRAPH);
1151 break;
1152 case CLASS_LOWER:
1153 EMIT(NFA_CLASS_LOWER);
1154 break;
1155 case CLASS_PRINT:
1156 EMIT(NFA_CLASS_PRINT);
1157 break;
1158 case CLASS_PUNCT:
1159 EMIT(NFA_CLASS_PUNCT);
1160 break;
1161 case CLASS_SPACE:
1162 EMIT(NFA_CLASS_SPACE);
1163 break;
1164 case CLASS_UPPER:
1165 EMIT(NFA_CLASS_UPPER);
1166 break;
1167 case CLASS_XDIGIT:
1168 EMIT(NFA_CLASS_XDIGIT);
1169 break;
1170 case CLASS_TAB:
1171 EMIT(NFA_CLASS_TAB);
1172 break;
1173 case CLASS_RETURN:
1174 EMIT(NFA_CLASS_RETURN);
1175 break;
1176 case CLASS_BACKSPACE:
1177 EMIT(NFA_CLASS_BACKSPACE);
1178 break;
1179 case CLASS_ESCAPE:
1180 EMIT(NFA_CLASS_ESCAPE);
1181 break;
1182 }
1183 TRY_NEG();
1184 EMIT_GLUE();
1185 continue;
1186 }
1187 /* Try equivalence class [=a=] and the like */
1188 if (equiclass != 0)
1189 {
1190 result = nfa_emit_equi_class(equiclass, negated);
1191 if (result == FAIL)
1192 {
1193 /* should never happen */
1194 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1195 }
1196 EMIT_GLUE();
1197 continue;
1198 }
1199 /* Try collating class like [. .] */
1200 if (collclass != 0)
1201 {
1202 startc = collclass; /* allow [.a.]-x as a range */
1203 /* Will emit the proper atom at the end of the
1204 * while loop. */
1205 }
1206 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001207 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1208 * start character. */
1209 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210 {
1211 emit_range = TRUE;
1212 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001213 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 continue; /* reading the end of the range */
1215 }
1216
1217 /* Now handle simple and escaped characters.
1218 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1219 * accepts "\t", "\e", etc., but only when the 'l' flag in
1220 * 'cpoptions' is not included.
1221 * Posix doesn't recognize backslash at all.
1222 */
1223 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001224 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 && regparse + 1 <= endp
1226 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001227 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228 && vim_strchr(REGEXP_ABBR, regparse[1])
1229 != NULL)
1230 )
1231 )
1232 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001233 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001235 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001236 startc = reg_string ? NL : NFA_NEWL;
1237 else
1238 if (*regparse == 'd'
1239 || *regparse == 'o'
1240 || *regparse == 'x'
1241 || *regparse == 'u'
1242 || *regparse == 'U'
1243 )
1244 {
1245 /* TODO(RE) This needs more testing */
1246 startc = coll_get_char();
1247 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001248 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001249 }
1250 else
1251 {
1252 /* \r,\t,\e,\b */
1253 startc = backslash_trans(*regparse);
1254 }
1255 }
1256
1257 /* Normal printable char */
1258 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001259 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260
1261 /* Previous char was '-', so this char is end of range. */
1262 if (emit_range)
1263 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001264 endc = startc;
1265 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001266 if (startc > endc)
1267 EMSG_RET_FAIL(_(e_invrange));
1268#ifdef FEAT_MBYTE
1269 if (has_mbyte && ((*mb_char2len)(startc) > 1
1270 || (*mb_char2len)(endc) > 1))
1271 {
1272 if (endc > startc + 256)
1273 EMSG_RET_FAIL(_(e_invrange));
1274 /* Emit the range. "startc" was already emitted, so
1275 * skip it. */
1276 for (c = startc + 1; c <= endc; c++)
1277 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001278 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001279 TRY_NEG();
1280 EMIT_GLUE();
1281 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 }
1283 else
1284#endif
1285 {
1286#ifdef EBCDIC
1287 int alpha_only = FALSE;
1288
1289 /* for alphabetical range skip the gaps
1290 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1291 if (isalpha(startc) && isalpha(endc))
1292 alpha_only = TRUE;
1293#endif
1294 /* Emit the range. "startc" was already emitted, so
1295 * skip it. */
1296 for (c = startc + 1; c <= endc; c++)
1297#ifdef EBCDIC
1298 if (!alpha_only || isalpha(startc))
1299#endif
1300 {
1301 EMIT(c);
1302 TRY_NEG();
1303 EMIT_GLUE();
1304 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001305 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001306 emit_range = FALSE;
1307 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001308 }
1309 else
1310 {
1311 /*
1312 * This char (startc) is not part of a range. Just
1313 * emit it.
1314 *
1315 * Normally, simply emit startc. But if we get char
1316 * code=0 from a collating char, then replace it with
1317 * 0x0a.
1318 *
1319 * This is needed to completely mimic the behaviour of
1320 * the backtracking engine.
1321 */
1322 if (got_coll_char == TRUE && startc == 0)
1323 EMIT(0x0a);
1324 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001325 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001326 TRY_NEG();
1327 EMIT_GLUE();
1328 }
1329
Bram Moolenaar51a29832013-05-28 22:30:35 +02001330 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 } /* while (p < endp) */
1332
Bram Moolenaar51a29832013-05-28 22:30:35 +02001333 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001334 if (*regparse == '-') /* if last, '-' is just a char */
1335 {
1336 EMIT('-');
1337 TRY_NEG();
1338 EMIT_GLUE();
1339 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001340 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001341
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342 /* skip the trailing ] */
1343 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001344 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001345 if (negated == TRUE)
1346 {
1347 /* Mark end of negated char range */
1348 EMIT(NFA_END_NEG_RANGE);
1349 EMIT(NFA_CONCAT);
1350 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001351
1352 /* \_[] also matches \n but it's not negated */
1353 if (extra == ADD_NL)
1354 {
1355 EMIT(reg_string ? NL : NFA_NEWL);
1356 EMIT(NFA_OR);
1357 }
1358
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001360 } /* if exists closing ] */
1361
1362 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001363 {
1364 syntax_error = TRUE;
1365 EMSG_RET_FAIL(_(e_missingbracket));
1366 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001367 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001368
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001369 default:
1370 {
1371#ifdef FEAT_MBYTE
1372 int plen;
1373
1374nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001375 /* plen is length of current char with composing chars */
1376 if (enc_utf8 && ((*mb_char2len)(c)
1377 != (plen = (*mb_ptr2len)(old_regparse))
1378 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001379 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001380 int i = 0;
1381
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001382 /* A base character plus composing characters, or just one
1383 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001384 * This requires creating a separate atom as if enclosing
1385 * the characters in (), where NFA_COMPOSING is the ( and
1386 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001387 * building the postfix form, not the NFA itself;
1388 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001389 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001390 for (;;)
1391 {
1392 EMIT(c);
1393 if (i > 0)
1394 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001395 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001396 break;
1397 c = utf_ptr2char(old_regparse + i);
1398 }
1399 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001400 regparse = old_regparse + plen;
1401 }
1402 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403#endif
1404 {
1405 c = no_Magic(c);
1406 EMIT(c);
1407 }
1408 return OK;
1409 }
1410 }
1411
1412#undef TRY_NEG
1413#undef EMIT_GLUE
1414
1415 return OK;
1416}
1417
1418/*
1419 * Parse something followed by possible [*+=].
1420 *
1421 * A piece is an atom, possibly followed by a multi, an indication of how many
1422 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1423 * characters: "", "a", "aa", etc.
1424 *
1425 * piece ::= atom
1426 * or atom multi
1427 */
1428 static int
1429nfa_regpiece()
1430{
1431 int i;
1432 int op;
1433 int ret;
1434 long minval, maxval;
1435 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001436 parse_state_T old_state;
1437 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001438 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001439 int old_post_pos;
1440 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441 int quest;
1442
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001443 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1444 * next. */
1445 save_parse_state(&old_state);
1446
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001448 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001449
1450 ret = nfa_regatom();
1451 if (ret == FAIL)
1452 return FAIL; /* cascaded error */
1453
1454 op = peekchr();
1455 if (re_multi_type(op) == NOT_MULTI)
1456 return OK;
1457
1458 skipchr();
1459 switch (op)
1460 {
1461 case Magic('*'):
1462 EMIT(NFA_STAR);
1463 break;
1464
1465 case Magic('+'):
1466 /*
1467 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1468 * first and only submatch would be "aaa". But the backtracking
1469 * engine interprets the plus as "try matching one more time", and
1470 * a* matches a second time at the end of the input, the empty
1471 * string.
1472 * The submatch will the empty string.
1473 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001474 * In order to be consistent with the old engine, we replace
1475 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001476 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001477 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001478 curchr = -1;
1479 if (nfa_regatom() == FAIL)
1480 return FAIL;
1481 EMIT(NFA_STAR);
1482 EMIT(NFA_CONCAT);
1483 skipchr(); /* skip the \+ */
1484 break;
1485
1486 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001487 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001488 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001489 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001490 switch(op)
1491 {
1492 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001493 /* \@= */
1494 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001496 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001497 /* \@! */
1498 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001499 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001501 op = no_Magic(getchr());
1502 if (op == '=')
1503 /* \@<= */
1504 i = NFA_PREV_ATOM_JUST_BEFORE;
1505 else if (op == '!')
1506 /* \@<! */
1507 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1508 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001510 /* \@> */
1511 i = NFA_PREV_ATOM_LIKE_PATTERN;
1512 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001513 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001514 if (i == 0)
1515 {
1516 syntax_error = TRUE;
1517 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1518 return FAIL;
1519 }
1520 EMIT(i);
1521 if (i == NFA_PREV_ATOM_JUST_BEFORE
1522 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1523 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001524 break;
1525
1526 case Magic('?'):
1527 case Magic('='):
1528 EMIT(NFA_QUEST);
1529 break;
1530
1531 case Magic('{'):
1532 /* a{2,5} will expand to 'aaa?a?a?'
1533 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1534 * version of '?'
1535 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1536 * parenthesis have the same id
1537 */
1538
1539 greedy = TRUE;
1540 c2 = peekchr();
1541 if (c2 == '-' || c2 == Magic('-'))
1542 {
1543 skipchr();
1544 greedy = FALSE;
1545 }
1546 if (!read_limits(&minval, &maxval))
1547 {
1548 syntax_error = TRUE;
1549 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1550 }
1551 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1552 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001553 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001554 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001555 if (greedy)
1556 /* \{}, \{0,} */
1557 EMIT(NFA_STAR);
1558 else
1559 /* \{-}, \{-0,} */
1560 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561 break;
1562 }
1563
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 /* Special case: x{0} or x{-0} */
1565 if (maxval == 0)
1566 {
1567 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001568 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001569 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1570 EMIT(NFA_SKIP_CHAR);
1571 return OK;
1572 }
1573
1574 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001575 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001576 /* Save parse state after the repeated atom and the \{} */
1577 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001578
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001579 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1580 for (i = 0; i < maxval; i++)
1581 {
1582 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001583 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001584 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001585 if (nfa_regatom() == FAIL)
1586 return FAIL;
1587 /* after "minval" times, atoms are optional */
1588 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001589 {
1590 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001591 {
1592 if (greedy)
1593 EMIT(NFA_STAR);
1594 else
1595 EMIT(NFA_STAR_NONGREEDY);
1596 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001597 else
1598 EMIT(quest);
1599 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001600 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001601 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001602 if (i + 1 > minval && maxval == MAX_LIMIT)
1603 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 }
1605
1606 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001607 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001608 curchr = -1;
1609
1610 break;
1611
1612
1613 default:
1614 break;
1615 } /* end switch */
1616
1617 if (re_multi_type(peekchr()) != NOT_MULTI)
1618 {
1619 /* Can't have a multi follow a multi. */
1620 syntax_error = TRUE;
1621 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1622 }
1623
1624 return OK;
1625}
1626
1627/*
1628 * Parse one or more pieces, concatenated. It matches a match for the
1629 * first piece, followed by a match for the second piece, etc. Example:
1630 * "f[0-9]b", first matches "f", then a digit and then "b".
1631 *
1632 * concat ::= piece
1633 * or piece piece
1634 * or piece piece piece
1635 * etc.
1636 */
1637 static int
1638nfa_regconcat()
1639{
1640 int cont = TRUE;
1641 int first = TRUE;
1642
1643 while (cont)
1644 {
1645 switch (peekchr())
1646 {
1647 case NUL:
1648 case Magic('|'):
1649 case Magic('&'):
1650 case Magic(')'):
1651 cont = FALSE;
1652 break;
1653
1654 case Magic('Z'):
1655#ifdef FEAT_MBYTE
1656 regflags |= RF_ICOMBINE;
1657#endif
1658 skipchr_keepstart();
1659 break;
1660 case Magic('c'):
1661 regflags |= RF_ICASE;
1662 skipchr_keepstart();
1663 break;
1664 case Magic('C'):
1665 regflags |= RF_NOICASE;
1666 skipchr_keepstart();
1667 break;
1668 case Magic('v'):
1669 reg_magic = MAGIC_ALL;
1670 skipchr_keepstart();
1671 curchr = -1;
1672 break;
1673 case Magic('m'):
1674 reg_magic = MAGIC_ON;
1675 skipchr_keepstart();
1676 curchr = -1;
1677 break;
1678 case Magic('M'):
1679 reg_magic = MAGIC_OFF;
1680 skipchr_keepstart();
1681 curchr = -1;
1682 break;
1683 case Magic('V'):
1684 reg_magic = MAGIC_NONE;
1685 skipchr_keepstart();
1686 curchr = -1;
1687 break;
1688
1689 default:
1690 if (nfa_regpiece() == FAIL)
1691 return FAIL;
1692 if (first == FALSE)
1693 EMIT(NFA_CONCAT);
1694 else
1695 first = FALSE;
1696 break;
1697 }
1698 }
1699
1700 return OK;
1701}
1702
1703/*
1704 * Parse a branch, one or more concats, separated by "\&". It matches the
1705 * last concat, but only if all the preceding concats also match at the same
1706 * position. Examples:
1707 * "foobeep\&..." matches "foo" in "foobeep".
1708 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1709 *
1710 * branch ::= concat
1711 * or concat \& concat
1712 * or concat \& concat \& concat
1713 * etc.
1714 */
1715 static int
1716nfa_regbranch()
1717{
1718 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001719 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720
Bram Moolenaar16299b52013-05-30 18:45:23 +02001721 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001722
1723 /* First branch, possibly the only one */
1724 if (nfa_regconcat() == FAIL)
1725 return FAIL;
1726
1727 ch = peekchr();
1728 /* Try next concats */
1729 while (ch == Magic('&'))
1730 {
1731 skipchr();
1732 EMIT(NFA_NOPEN);
1733 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001734 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735 if (nfa_regconcat() == FAIL)
1736 return FAIL;
1737 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001738 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 EMIT(NFA_SKIP_CHAR);
1740 EMIT(NFA_CONCAT);
1741 ch = peekchr();
1742 }
1743
1744 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001745 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001746 EMIT(NFA_SKIP_CHAR);
1747
1748 return OK;
1749}
1750
1751/*
1752 * Parse a pattern, one or more branches, separated by "\|". It matches
1753 * anything that matches one of the branches. Example: "foo\|beep" matches
1754 * "foo" and matches "beep". If more than one branch matches, the first one
1755 * is used.
1756 *
1757 * pattern ::= branch
1758 * or branch \| branch
1759 * or branch \| branch \| branch
1760 * etc.
1761 */
1762 static int
1763nfa_reg(paren)
1764 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1765{
1766 int parno = 0;
1767
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001768 if (paren == REG_PAREN)
1769 {
1770 if (regnpar >= NSUBEXP) /* Too many `(' */
1771 {
1772 syntax_error = TRUE;
1773 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1774 }
1775 parno = regnpar++;
1776 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001777#ifdef FEAT_SYN_HL
1778 else if (paren == REG_ZPAREN)
1779 {
1780 /* Make a ZOPEN node. */
1781 if (regnzpar >= NSUBEXP)
1782 {
1783 syntax_error = TRUE;
1784 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1785 }
1786 parno = regnzpar++;
1787 }
1788#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789
1790 if (nfa_regbranch() == FAIL)
1791 return FAIL; /* cascaded error */
1792
1793 while (peekchr() == Magic('|'))
1794 {
1795 skipchr();
1796 if (nfa_regbranch() == FAIL)
1797 return FAIL; /* cascaded error */
1798 EMIT(NFA_OR);
1799 }
1800
1801 /* Check for proper termination. */
1802 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1803 {
1804 syntax_error = TRUE;
1805 if (paren == REG_NPAREN)
1806 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1807 else
1808 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1809 }
1810 else if (paren == REG_NOPAREN && peekchr() != NUL)
1811 {
1812 syntax_error = TRUE;
1813 if (peekchr() == Magic(')'))
1814 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1815 else
1816 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1817 }
1818 /*
1819 * Here we set the flag allowing back references to this set of
1820 * parentheses.
1821 */
1822 if (paren == REG_PAREN)
1823 {
1824 had_endbrace[parno] = TRUE; /* have seen the close paren */
1825 EMIT(NFA_MOPEN + parno);
1826 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001827#ifdef FEAT_SYN_HL
1828 else if (paren == REG_ZPAREN)
1829 EMIT(NFA_ZOPEN + parno);
1830#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831
1832 return OK;
1833}
1834
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001835#ifdef DEBUG
1836static char_u code[50];
1837
1838 static void
1839nfa_set_code(c)
1840 int c;
1841{
1842 int addnl = FALSE;
1843
1844 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1845 {
1846 addnl = TRUE;
1847 c -= ADD_NL;
1848 }
1849
1850 STRCPY(code, "");
1851 switch (c)
1852 {
1853 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1854 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1855 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1856 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1857 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1858 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1859
Bram Moolenaar5714b802013-05-28 22:03:20 +02001860 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1861 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1862 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1863 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1864 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1865 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1866 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1867 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1868 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001869#ifdef FEAT_SYN_HL
1870 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1871 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1872 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1873 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1874 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1875 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1876 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1877 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1878 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1879#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001880 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1881
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 case NFA_PREV_ATOM_NO_WIDTH:
1883 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001884 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1885 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001886 case NFA_PREV_ATOM_JUST_BEFORE:
1887 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1888 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1889 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001890 case NFA_PREV_ATOM_LIKE_PATTERN:
1891 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
1892
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001893 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1894 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001896 case NFA_START_INVISIBLE_BEFORE:
1897 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001898 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001900 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1903 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001904 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001906 case NFA_MOPEN:
1907 case NFA_MOPEN1:
1908 case NFA_MOPEN2:
1909 case NFA_MOPEN3:
1910 case NFA_MOPEN4:
1911 case NFA_MOPEN5:
1912 case NFA_MOPEN6:
1913 case NFA_MOPEN7:
1914 case NFA_MOPEN8:
1915 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001916 STRCPY(code, "NFA_MOPEN(x)");
1917 code[10] = c - NFA_MOPEN + '0';
1918 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001919 case NFA_MCLOSE:
1920 case NFA_MCLOSE1:
1921 case NFA_MCLOSE2:
1922 case NFA_MCLOSE3:
1923 case NFA_MCLOSE4:
1924 case NFA_MCLOSE5:
1925 case NFA_MCLOSE6:
1926 case NFA_MCLOSE7:
1927 case NFA_MCLOSE8:
1928 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929 STRCPY(code, "NFA_MCLOSE(x)");
1930 code[11] = c - NFA_MCLOSE + '0';
1931 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001932#ifdef FEAT_SYN_HL
1933 case NFA_ZOPEN:
1934 case NFA_ZOPEN1:
1935 case NFA_ZOPEN2:
1936 case NFA_ZOPEN3:
1937 case NFA_ZOPEN4:
1938 case NFA_ZOPEN5:
1939 case NFA_ZOPEN6:
1940 case NFA_ZOPEN7:
1941 case NFA_ZOPEN8:
1942 case NFA_ZOPEN9:
1943 STRCPY(code, "NFA_ZOPEN(x)");
1944 code[10] = c - NFA_ZOPEN + '0';
1945 break;
1946 case NFA_ZCLOSE:
1947 case NFA_ZCLOSE1:
1948 case NFA_ZCLOSE2:
1949 case NFA_ZCLOSE3:
1950 case NFA_ZCLOSE4:
1951 case NFA_ZCLOSE5:
1952 case NFA_ZCLOSE6:
1953 case NFA_ZCLOSE7:
1954 case NFA_ZCLOSE8:
1955 case NFA_ZCLOSE9:
1956 STRCPY(code, "NFA_ZCLOSE(x)");
1957 code[11] = c - NFA_ZCLOSE + '0';
1958 break;
1959#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1961 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1962 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1963 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001964 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1965 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001966 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1967 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1968 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1969 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1970 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1971 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1972 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1973 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1974 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1975 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1976 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1977 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1978 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001979#ifdef FEAT_VISUAL
Bram Moolenaar044aa292013-06-04 21:27:38 +02001980 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001981#endif
Bram Moolenaar044aa292013-06-04 21:27:38 +02001982
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001984 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1985 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1986 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001987 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1988 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1989 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001990 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1991 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1992 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1993 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1994 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1995 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1996 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1997 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1998 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1999 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2000 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2001 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2002 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2003 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2004 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2005 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2006 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2007
2008 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2009 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2010 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2011 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2012 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2013 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2014 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2015 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2016 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2017 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2018 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2019 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2020 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2021 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2022 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2023 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2024 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2025 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2026 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2027 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2028 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2029 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2030 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2031 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2032 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2033 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2034 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2035
2036 default:
2037 STRCPY(code, "CHAR(x)");
2038 code[5] = c;
2039 }
2040
2041 if (addnl == TRUE)
2042 STRCAT(code, " + NEWLINE ");
2043
2044}
2045
2046#ifdef ENABLE_LOG
2047static FILE *log_fd;
2048
2049/*
2050 * Print the postfix notation of the current regexp.
2051 */
2052 static void
2053nfa_postfix_dump(expr, retval)
2054 char_u *expr;
2055 int retval;
2056{
2057 int *p;
2058 FILE *f;
2059
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002060 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061 if (f != NULL)
2062 {
2063 fprintf(f, "\n-------------------------\n");
2064 if (retval == FAIL)
2065 fprintf(f, ">>> NFA engine failed ... \n");
2066 else if (retval == OK)
2067 fprintf(f, ">>> NFA engine succeeded !\n");
2068 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002069 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 {
2071 nfa_set_code(*p);
2072 fprintf(f, "%s, ", code);
2073 }
2074 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002075 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002076 fprintf(f, "%d ", *p);
2077 fprintf(f, "\n\n");
2078 fclose(f);
2079 }
2080}
2081
2082/*
2083 * Print the NFA starting with a root node "state".
2084 */
2085 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002086nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 FILE *debugf;
2088 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002089{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002090 garray_T indent;
2091
2092 ga_init2(&indent, 1, 64);
2093 ga_append(&indent, '\0');
2094 nfa_print_state2(debugf, state, &indent);
2095 ga_clear(&indent);
2096}
2097
2098 static void
2099nfa_print_state2(debugf, state, indent)
2100 FILE *debugf;
2101 nfa_state_T *state;
2102 garray_T *indent;
2103{
2104 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002105
2106 if (state == NULL)
2107 return;
2108
2109 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002110
2111 /* Output indent */
2112 p = (char_u *)indent->ga_data;
2113 if (indent->ga_len >= 3)
2114 {
2115 int last = indent->ga_len - 3;
2116 char_u save[2];
2117
2118 STRNCPY(save, &p[last], 2);
2119 STRNCPY(&p[last], "+-", 2);
2120 fprintf(debugf, " %s", p);
2121 STRNCPY(&p[last], save, 2);
2122 }
2123 else
2124 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002125
2126 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002127 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2128 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002129 if (state->id < 0)
2130 return;
2131
2132 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002133
2134 /* grow indent for state->out */
2135 indent->ga_len -= 1;
2136 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002137 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002138 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002139 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002140 ga_append(indent, '\0');
2141
2142 nfa_print_state2(debugf, state->out, indent);
2143
2144 /* replace last part of indent for state->out1 */
2145 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002146 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002147 ga_append(indent, '\0');
2148
2149 nfa_print_state2(debugf, state->out1, indent);
2150
2151 /* shrink indent */
2152 indent->ga_len -= 3;
2153 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002154}
2155
2156/*
2157 * Print the NFA state machine.
2158 */
2159 static void
2160nfa_dump(prog)
2161 nfa_regprog_T *prog;
2162{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002163 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164
2165 if (debugf != NULL)
2166 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002167 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002168 fclose(debugf);
2169 }
2170}
2171#endif /* ENABLE_LOG */
2172#endif /* DEBUG */
2173
2174/*
2175 * Parse r.e. @expr and convert it into postfix form.
2176 * Return the postfix string on success, NULL otherwise.
2177 */
2178 static int *
2179re2post()
2180{
2181 if (nfa_reg(REG_NOPAREN) == FAIL)
2182 return NULL;
2183 EMIT(NFA_MOPEN);
2184 return post_start;
2185}
2186
2187/* NB. Some of the code below is inspired by Russ's. */
2188
2189/*
2190 * Represents an NFA state plus zero or one or two arrows exiting.
2191 * if c == MATCH, no arrows out; matching state.
2192 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2193 * If c < 256, labeled arrow with character c to out.
2194 */
2195
2196static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2197
2198/*
2199 * Allocate and initialize nfa_state_T.
2200 */
2201 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002202alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002203 int c;
2204 nfa_state_T *out;
2205 nfa_state_T *out1;
2206{
2207 nfa_state_T *s;
2208
2209 if (istate >= nstate)
2210 return NULL;
2211
2212 s = &state_ptr[istate++];
2213
2214 s->c = c;
2215 s->out = out;
2216 s->out1 = out1;
2217
2218 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002219 s->lastlist[0] = 0;
2220 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221 s->negated = FALSE;
2222
2223 return s;
2224}
2225
2226/*
2227 * A partially built NFA without the matching state filled in.
2228 * Frag_T.start points at the start state.
2229 * Frag_T.out is a list of places that need to be set to the
2230 * next state for this fragment.
2231 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002232
2233/* Since the out pointers in the list are always
2234 * uninitialized, we use the pointers themselves
2235 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002237union Ptrlist
2238{
2239 Ptrlist *next;
2240 nfa_state_T *s;
2241};
2242
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243struct Frag
2244{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002245 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002246 Ptrlist *out;
2247};
2248typedef struct Frag Frag_T;
2249
2250static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2251static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2252static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2253static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2254static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2255static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2256
2257/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002258 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259 */
2260 static Frag_T
2261frag(start, out)
2262 nfa_state_T *start;
2263 Ptrlist *out;
2264{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002265 Frag_T n;
2266
2267 n.start = start;
2268 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002269 return n;
2270}
2271
2272/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002273 * Create singleton list containing just outp.
2274 */
2275 static Ptrlist *
2276list1(outp)
2277 nfa_state_T **outp;
2278{
2279 Ptrlist *l;
2280
2281 l = (Ptrlist *)outp;
2282 l->next = NULL;
2283 return l;
2284}
2285
2286/*
2287 * Patch the list of states at out to point to start.
2288 */
2289 static void
2290patch(l, s)
2291 Ptrlist *l;
2292 nfa_state_T *s;
2293{
2294 Ptrlist *next;
2295
2296 for (; l; l = next)
2297 {
2298 next = l->next;
2299 l->s = s;
2300 }
2301}
2302
2303
2304/*
2305 * Join the two lists l1 and l2, returning the combination.
2306 */
2307 static Ptrlist *
2308append(l1, l2)
2309 Ptrlist *l1;
2310 Ptrlist *l2;
2311{
2312 Ptrlist *oldl1;
2313
2314 oldl1 = l1;
2315 while (l1->next)
2316 l1 = l1->next;
2317 l1->next = l2;
2318 return oldl1;
2319}
2320
2321/*
2322 * Stack used for transforming postfix form into NFA.
2323 */
2324static Frag_T empty;
2325
2326 static void
2327st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002328 int *postfix UNUSED;
2329 int *end UNUSED;
2330 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002332#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002333 FILE *df;
2334 int *p2;
2335
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002336 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002337 if (df)
2338 {
2339 fprintf(df, "Error popping the stack!\n");
2340#ifdef DEBUG
2341 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2342#endif
2343 fprintf(df, "Postfix form is: ");
2344#ifdef DEBUG
2345 for (p2 = postfix; p2 < end; p2++)
2346 {
2347 nfa_set_code(*p2);
2348 fprintf(df, "%s, ", code);
2349 }
2350 nfa_set_code(*p);
2351 fprintf(df, "\nCurrent position is: ");
2352 for (p2 = postfix; p2 <= p; p2 ++)
2353 {
2354 nfa_set_code(*p2);
2355 fprintf(df, "%s, ", code);
2356 }
2357#else
2358 for (p2 = postfix; p2 < end; p2++)
2359 {
2360 fprintf(df, "%d, ", *p2);
2361 }
2362 fprintf(df, "\nCurrent position is: ");
2363 for (p2 = postfix; p2 <= p; p2 ++)
2364 {
2365 fprintf(df, "%d, ", *p2);
2366 }
2367#endif
2368 fprintf(df, "\n--------------------------\n");
2369 fclose(df);
2370 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002371#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 EMSG(_("E874: (NFA) Could not pop the stack !"));
2373}
2374
2375/*
2376 * Push an item onto the stack.
2377 */
2378 static void
2379st_push(s, p, stack_end)
2380 Frag_T s;
2381 Frag_T **p;
2382 Frag_T *stack_end;
2383{
2384 Frag_T *stackp = *p;
2385
2386 if (stackp >= stack_end)
2387 return;
2388 *stackp = s;
2389 *p = *p + 1;
2390}
2391
2392/*
2393 * Pop an item from the stack.
2394 */
2395 static Frag_T
2396st_pop(p, stack)
2397 Frag_T **p;
2398 Frag_T *stack;
2399{
2400 Frag_T *stackp;
2401
2402 *p = *p - 1;
2403 stackp = *p;
2404 if (stackp < stack)
2405 return empty;
2406 return **p;
2407}
2408
2409/*
2410 * Convert a postfix form into its equivalent NFA.
2411 * Return the NFA start state on success, NULL otherwise.
2412 */
2413 static nfa_state_T *
2414post2nfa(postfix, end, nfa_calc_size)
2415 int *postfix;
2416 int *end;
2417 int nfa_calc_size;
2418{
2419 int *p;
2420 int mopen;
2421 int mclose;
2422 Frag_T *stack = NULL;
2423 Frag_T *stackp = NULL;
2424 Frag_T *stack_end = NULL;
2425 Frag_T e1;
2426 Frag_T e2;
2427 Frag_T e;
2428 nfa_state_T *s;
2429 nfa_state_T *s1;
2430 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002431 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432
2433 if (postfix == NULL)
2434 return NULL;
2435
Bram Moolenaar053bb602013-05-20 13:55:21 +02002436#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002437#define POP() st_pop(&stackp, stack); \
2438 if (stackp < stack) \
2439 { \
2440 st_error(postfix, end, p); \
2441 return NULL; \
2442 }
2443
2444 if (nfa_calc_size == FALSE)
2445 {
2446 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002447 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002449 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450 }
2451
2452 for (p = postfix; p < end; ++p)
2453 {
2454 switch (*p)
2455 {
2456 case NFA_CONCAT:
2457 /* Catenation.
2458 * Pay attention: this operator does not exist
2459 * in the r.e. itself (it is implicit, really).
2460 * It is added when r.e. is translated to postfix
2461 * form in re2post().
2462 *
2463 * No new state added here. */
2464 if (nfa_calc_size == TRUE)
2465 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002466 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002467 break;
2468 }
2469 e2 = POP();
2470 e1 = POP();
2471 patch(e1.out, e2.start);
2472 PUSH(frag(e1.start, e2.out));
2473 break;
2474
2475 case NFA_NOT:
2476 /* Negation of a character */
2477 if (nfa_calc_size == TRUE)
2478 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002479 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002480 break;
2481 }
2482 e1 = POP();
2483 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002484#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002485 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002487#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002488 PUSH(e1);
2489 break;
2490
2491 case NFA_OR:
2492 /* Alternation */
2493 if (nfa_calc_size == TRUE)
2494 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002495 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002496 break;
2497 }
2498 e2 = POP();
2499 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002500 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002501 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002502 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503 PUSH(frag(s, append(e1.out, e2.out)));
2504 break;
2505
2506 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002507 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002508 if (nfa_calc_size == TRUE)
2509 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002510 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002511 break;
2512 }
2513 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002514 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002516 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002517 patch(e.out, s);
2518 PUSH(frag(s, list1(&s->out1)));
2519 break;
2520
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002521 case NFA_STAR_NONGREEDY:
2522 /* Zero or more, prefer zero */
2523 if (nfa_calc_size == TRUE)
2524 {
2525 nstate++;
2526 break;
2527 }
2528 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002529 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002530 if (s == NULL)
2531 goto theend;
2532 patch(e.out, s);
2533 PUSH(frag(s, list1(&s->out)));
2534 break;
2535
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002536 case NFA_QUEST:
2537 /* one or zero atoms=> greedy match */
2538 if (nfa_calc_size == TRUE)
2539 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002540 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002541 break;
2542 }
2543 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002544 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002545 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002546 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 PUSH(frag(s, append(e.out, list1(&s->out1))));
2548 break;
2549
2550 case NFA_QUEST_NONGREEDY:
2551 /* zero or one atoms => non-greedy match */
2552 if (nfa_calc_size == TRUE)
2553 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002554 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002555 break;
2556 }
2557 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002558 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002559 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002560 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002561 PUSH(frag(s, append(e.out, list1(&s->out))));
2562 break;
2563
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002564 case NFA_SKIP_CHAR:
2565 /* Symbol of 0-length, Used in a repetition
2566 * with max/min count of 0 */
2567 if (nfa_calc_size == TRUE)
2568 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002569 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002570 break;
2571 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002572 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002574 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002575 PUSH(frag(s, list1(&s->out)));
2576 break;
2577
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002578 case NFA_OPT_CHARS:
2579 {
2580 int n;
2581
2582 /* \%[abc] */
2583 n = *++p; /* get number of characters */
2584 if (nfa_calc_size == TRUE)
2585 {
2586 nstate += n;
2587 break;
2588 }
2589 e1.out = NULL; /* stores list with out1's */
2590 s1 = NULL; /* previous NFA_SPLIT to connect to */
2591 while (n-- > 0)
2592 {
2593 e = POP(); /* get character */
2594 s = alloc_state(NFA_SPLIT, e.start, NULL);
2595 if (s == NULL)
2596 goto theend;
2597 if (e1.out == NULL)
2598 e1 = e;
2599 patch(e.out, s1);
2600 append(e1.out, list1(&s->out1));
2601 s1 = s;
2602 }
2603 PUSH(frag(s, e1.out));
2604 break;
2605 }
2606
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002607 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002608 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002609 case NFA_PREV_ATOM_JUST_BEFORE:
2610 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002611 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002612 {
2613 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2614 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2615 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2616 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002617 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
2618 int start_state = NFA_START_INVISIBLE;
2619 int end_state = NFA_END_INVISIBLE;
2620 int n = 0;
2621 nfa_state_T *zend;
2622 nfa_state_T *skip;
2623
2624 if (before)
2625 start_state = NFA_START_INVISIBLE_BEFORE;
2626 else if (pattern)
2627 {
2628 start_state = NFA_START_PATTERN;
2629 end_state = NFA_END_PATTERN;
2630 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002631
2632 if (before)
2633 n = *++p; /* get the count */
2634
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002635 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002636 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002637 * The \@<= operator: match for the preceding atom.
2638 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002639 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002640 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641
2642 if (nfa_calc_size == TRUE)
2643 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002644 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002645 break;
2646 }
2647 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002648 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002650 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002651
Bram Moolenaar87953742013-06-05 18:52:40 +02002652 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002653 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002654 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002655 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002656 {
2657 s->negated = TRUE;
2658 s1->negated = TRUE;
2659 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002660 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002661 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002662 if (pattern)
2663 {
2664 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2665 skip = alloc_state(NFA_SKIP, NULL, NULL);
2666 zend = alloc_state(NFA_ZEND, s1, NULL);
2667 s1->out= skip;
2668 patch(e.out, zend);
2669 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002670 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002671 else
2672 {
2673 patch(e.out, s1);
2674 PUSH(frag(s, list1(&s1->out)));
2675 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002676 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002677 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002678
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002679#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002680 case NFA_COMPOSING: /* char with composing char */
2681#if 0
2682 /* TODO */
2683 if (regflags & RF_ICOMBINE)
2684 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002685 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002686 }
2687#endif
2688 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002689#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002690
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002691 case NFA_MOPEN: /* \( \) Submatch */
2692 case NFA_MOPEN1:
2693 case NFA_MOPEN2:
2694 case NFA_MOPEN3:
2695 case NFA_MOPEN4:
2696 case NFA_MOPEN5:
2697 case NFA_MOPEN6:
2698 case NFA_MOPEN7:
2699 case NFA_MOPEN8:
2700 case NFA_MOPEN9:
2701#ifdef FEAT_SYN_HL
2702 case NFA_ZOPEN: /* \z( \) Submatch */
2703 case NFA_ZOPEN1:
2704 case NFA_ZOPEN2:
2705 case NFA_ZOPEN3:
2706 case NFA_ZOPEN4:
2707 case NFA_ZOPEN5:
2708 case NFA_ZOPEN6:
2709 case NFA_ZOPEN7:
2710 case NFA_ZOPEN8:
2711 case NFA_ZOPEN9:
2712#endif
2713 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714 if (nfa_calc_size == TRUE)
2715 {
2716 nstate += 2;
2717 break;
2718 }
2719
2720 mopen = *p;
2721 switch (*p)
2722 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002723 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2724#ifdef FEAT_SYN_HL
2725 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2726 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2727 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2728 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2729 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2730 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2731 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2732 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2733 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2734 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2735#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002736#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002737 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002738#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002740 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002741 mclose = *p + NSUBEXP;
2742 break;
2743 }
2744
2745 /* Allow "NFA_MOPEN" as a valid postfix representation for
2746 * the empty regexp "". In this case, the NFA will be
2747 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2748 * empty groups of parenthesis, and empty mbyte chars */
2749 if (stackp == stack)
2750 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002751 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002753 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002754 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002755 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002756 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002757 patch(list1(&s->out), s1);
2758 PUSH(frag(s, list1(&s1->out)));
2759 break;
2760 }
2761
2762 /* At least one node was emitted before NFA_MOPEN, so
2763 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2764 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002765 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002766 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002767 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002768
Bram Moolenaar525666f2013-06-02 16:40:55 +02002769 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002770 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002771 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002772 patch(e.out, s1);
2773
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002774#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002775 if (mopen == NFA_COMPOSING)
2776 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002777 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002778#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002779
2780 PUSH(frag(s, list1(&s1->out)));
2781 break;
2782
Bram Moolenaar5714b802013-05-28 22:03:20 +02002783 case NFA_BACKREF1:
2784 case NFA_BACKREF2:
2785 case NFA_BACKREF3:
2786 case NFA_BACKREF4:
2787 case NFA_BACKREF5:
2788 case NFA_BACKREF6:
2789 case NFA_BACKREF7:
2790 case NFA_BACKREF8:
2791 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002792#ifdef FEAT_SYN_HL
2793 case NFA_ZREF1:
2794 case NFA_ZREF2:
2795 case NFA_ZREF3:
2796 case NFA_ZREF4:
2797 case NFA_ZREF5:
2798 case NFA_ZREF6:
2799 case NFA_ZREF7:
2800 case NFA_ZREF8:
2801 case NFA_ZREF9:
2802#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002803 if (nfa_calc_size == TRUE)
2804 {
2805 nstate += 2;
2806 break;
2807 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002808 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002809 if (s == NULL)
2810 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002811 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002812 if (s1 == NULL)
2813 goto theend;
2814 patch(list1(&s->out), s1);
2815 PUSH(frag(s, list1(&s1->out)));
2816 break;
2817
Bram Moolenaar423532e2013-05-29 21:14:42 +02002818 case NFA_LNUM:
2819 case NFA_LNUM_GT:
2820 case NFA_LNUM_LT:
2821 case NFA_VCOL:
2822 case NFA_VCOL_GT:
2823 case NFA_VCOL_LT:
2824 case NFA_COL:
2825 case NFA_COL_GT:
2826 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002827 case NFA_MARK:
2828 case NFA_MARK_GT:
2829 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002830 {
2831 int n = *++p; /* lnum, col or mark name */
2832
Bram Moolenaar423532e2013-05-29 21:14:42 +02002833 if (nfa_calc_size == TRUE)
2834 {
2835 nstate += 1;
2836 break;
2837 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002838 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002839 if (s == NULL)
2840 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002841 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002842 PUSH(frag(s, list1(&s->out)));
2843 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002844 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002845
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002846 case NFA_ZSTART:
2847 case NFA_ZEND:
2848 default:
2849 /* Operands */
2850 if (nfa_calc_size == TRUE)
2851 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002852 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002853 break;
2854 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002855 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002856 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002857 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002858 PUSH(frag(s, list1(&s->out)));
2859 break;
2860
2861 } /* switch(*p) */
2862
2863 } /* for(p = postfix; *p; ++p) */
2864
2865 if (nfa_calc_size == TRUE)
2866 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002867 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002868 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869 }
2870
2871 e = POP();
2872 if (stackp != stack)
2873 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2874
2875 if (istate >= nstate)
2876 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2877
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002878 matchstate = &state_ptr[istate++]; /* the match state */
2879 matchstate->c = NFA_MATCH;
2880 matchstate->out = matchstate->out1 = NULL;
2881
2882 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002883 ret = e.start;
2884
2885theend:
2886 vim_free(stack);
2887 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888
2889#undef POP1
2890#undef PUSH1
2891#undef POP2
2892#undef PUSH2
2893#undef POP
2894#undef PUSH
2895}
2896
2897/****************************************************************
2898 * NFA execution code.
2899 ****************************************************************/
2900
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002901typedef struct
2902{
2903 int in_use; /* number of subexpr with useful info */
2904
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002905 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002906 union
2907 {
2908 struct multipos
2909 {
2910 lpos_T start;
2911 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002912 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002913 struct linepos
2914 {
2915 char_u *start;
2916 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002917 } line[NSUBEXP];
2918 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002919} regsub_T;
2920
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002921typedef struct
2922{
2923 regsub_T norm; /* \( .. \) matches */
2924#ifdef FEAT_SYN_HL
2925 regsub_T synt; /* \z( .. \) matches */
2926#endif
2927} regsubs_T;
2928
Bram Moolenaara2d95102013-06-04 14:23:05 +02002929/* nfa_pim_T stores a Postponed Invisible Match. */
2930typedef struct nfa_pim_S nfa_pim_T;
2931struct nfa_pim_S
2932{
2933 nfa_state_T *state;
2934 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2935 nfa_pim_T *pim; /* another PIM at the same position */
2936 regsubs_T subs; /* submatch info, only party used */
2937};
2938
2939/* Values for done in nfa_pim_T. */
2940#define NFA_PIM_TODO 0
2941#define NFA_PIM_MATCH 1
2942#define NFA_PIM_NOMATCH -1
2943
2944
Bram Moolenaar963fee22013-05-26 21:47:28 +02002945/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002946typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002947{
2948 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002949 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002950 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002951 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002952} nfa_thread_T;
2953
Bram Moolenaar963fee22013-05-26 21:47:28 +02002954/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002955typedef struct
2956{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002957 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002958 int n; /* nr of states currently in "t" */
2959 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002960 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002961} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002962
Bram Moolenaar5714b802013-05-28 22:03:20 +02002963#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002964static void log_subsexpr __ARGS((regsubs_T *subs));
2965static void log_subexpr __ARGS((regsub_T *sub));
2966
2967 static void
2968log_subsexpr(subs)
2969 regsubs_T *subs;
2970{
2971 log_subexpr(&subs->norm);
2972# ifdef FEAT_SYN_HL
2973 log_subexpr(&subs->synt);
2974# endif
2975}
2976
Bram Moolenaar5714b802013-05-28 22:03:20 +02002977 static void
2978log_subexpr(sub)
2979 regsub_T *sub;
2980{
2981 int j;
2982
2983 for (j = 0; j < sub->in_use; j++)
2984 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02002985 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002986 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002987 sub->list.multi[j].start.col,
2988 (int)sub->list.multi[j].start.lnum,
2989 sub->list.multi[j].end.col,
2990 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002991 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002992 {
2993 char *s = (char *)sub->list.line[j].start;
2994 char *e = (char *)sub->list.line[j].end;
2995
Bram Moolenaar87953742013-06-05 18:52:40 +02002996 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002997 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002998 s == NULL ? "NULL" : s,
2999 e == NULL ? "NULL" : e);
3000 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003001}
3002#endif
3003
Bram Moolenaar963fee22013-05-26 21:47:28 +02003004/* Used during execution: whether a match has been found. */
3005static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003006
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003007static void clear_sub __ARGS((regsub_T *sub));
3008static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3009static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003010static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003011static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003012static 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 +02003013
3014 static void
3015clear_sub(sub)
3016 regsub_T *sub;
3017{
3018 if (REG_MULTI)
3019 /* Use 0xff to set lnum to -1 */
3020 vim_memset(sub->list.multi, 0xff,
3021 sizeof(struct multipos) * nfa_nsubexpr);
3022 else
3023 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3024 sub->in_use = 0;
3025}
3026
3027/*
3028 * Copy the submatches from "from" to "to".
3029 */
3030 static void
3031copy_sub(to, from)
3032 regsub_T *to;
3033 regsub_T *from;
3034{
3035 to->in_use = from->in_use;
3036 if (from->in_use > 0)
3037 {
3038 /* Copy the match start and end positions. */
3039 if (REG_MULTI)
3040 mch_memmove(&to->list.multi[0],
3041 &from->list.multi[0],
3042 sizeof(struct multipos) * from->in_use);
3043 else
3044 mch_memmove(&to->list.line[0],
3045 &from->list.line[0],
3046 sizeof(struct linepos) * from->in_use);
3047 }
3048}
3049
3050/*
3051 * Like copy_sub() but exclude the main match.
3052 */
3053 static void
3054copy_sub_off(to, from)
3055 regsub_T *to;
3056 regsub_T *from;
3057{
3058 if (to->in_use < from->in_use)
3059 to->in_use = from->in_use;
3060 if (from->in_use > 1)
3061 {
3062 /* Copy the match start and end positions. */
3063 if (REG_MULTI)
3064 mch_memmove(&to->list.multi[1],
3065 &from->list.multi[1],
3066 sizeof(struct multipos) * (from->in_use - 1));
3067 else
3068 mch_memmove(&to->list.line[1],
3069 &from->list.line[1],
3070 sizeof(struct linepos) * (from->in_use - 1));
3071 }
3072}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003073
Bram Moolenaar428e9872013-05-30 17:05:39 +02003074/*
3075 * Return TRUE if "sub1" and "sub2" have the same positions.
3076 */
3077 static int
3078sub_equal(sub1, sub2)
3079 regsub_T *sub1;
3080 regsub_T *sub2;
3081{
3082 int i;
3083 int todo;
3084 linenr_T s1, e1;
3085 linenr_T s2, e2;
3086 char_u *sp1, *ep1;
3087 char_u *sp2, *ep2;
3088
3089 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3090 if (REG_MULTI)
3091 {
3092 for (i = 0; i < todo; ++i)
3093 {
3094 if (i < sub1->in_use)
3095 {
3096 s1 = sub1->list.multi[i].start.lnum;
3097 e1 = sub1->list.multi[i].end.lnum;
3098 }
3099 else
3100 {
3101 s1 = 0;
3102 e1 = 0;
3103 }
3104 if (i < sub2->in_use)
3105 {
3106 s2 = sub2->list.multi[i].start.lnum;
3107 e2 = sub2->list.multi[i].end.lnum;
3108 }
3109 else
3110 {
3111 s2 = 0;
3112 e2 = 0;
3113 }
3114 if (s1 != s2 || e1 != e2)
3115 return FALSE;
3116 if (s1 != 0 && sub1->list.multi[i].start.col
3117 != sub2->list.multi[i].start.col)
3118 return FALSE;
3119 if (e1 != 0 && sub1->list.multi[i].end.col
3120 != sub2->list.multi[i].end.col)
3121 return FALSE;
3122 }
3123 }
3124 else
3125 {
3126 for (i = 0; i < todo; ++i)
3127 {
3128 if (i < sub1->in_use)
3129 {
3130 sp1 = sub1->list.line[i].start;
3131 ep1 = sub1->list.line[i].end;
3132 }
3133 else
3134 {
3135 sp1 = NULL;
3136 ep1 = NULL;
3137 }
3138 if (i < sub2->in_use)
3139 {
3140 sp2 = sub2->list.line[i].start;
3141 ep2 = sub2->list.line[i].end;
3142 }
3143 else
3144 {
3145 sp2 = NULL;
3146 ep2 = NULL;
3147 }
3148 if (sp1 != sp2 || ep1 != ep2)
3149 return FALSE;
3150 }
3151 }
3152
3153 return TRUE;
3154}
3155
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003156#ifdef ENABLE_LOG
3157 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003158report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003159{
3160 int col;
3161
3162 if (sub->in_use <= 0)
3163 col = -1;
3164 else if (REG_MULTI)
3165 col = sub->list.multi[0].start.col;
3166 else
3167 col = (int)(sub->list.line[0].start - regline);
3168 nfa_set_code(state->c);
3169 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3170 action, abs(state->id), lid, state->c, code, col);
3171}
3172#endif
3173
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003174 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003175addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003176 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003177 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003178 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003179 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003181 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003182 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003183 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003184 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003185 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003186 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003187 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003188#ifdef ENABLE_LOG
3189 int did_print = FALSE;
3190#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003191
3192 if (l == NULL || state == NULL)
3193 return;
3194
3195 switch (state->c)
3196 {
3197 case NFA_SPLIT:
3198 case NFA_NOT:
3199 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003200 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003201 case NFA_NCLOSE:
3202 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003203 case NFA_MCLOSE1:
3204 case NFA_MCLOSE2:
3205 case NFA_MCLOSE3:
3206 case NFA_MCLOSE4:
3207 case NFA_MCLOSE5:
3208 case NFA_MCLOSE6:
3209 case NFA_MCLOSE7:
3210 case NFA_MCLOSE8:
3211 case NFA_MCLOSE9:
3212#ifdef FEAT_SYN_HL
3213 case NFA_ZCLOSE:
3214 case NFA_ZCLOSE1:
3215 case NFA_ZCLOSE2:
3216 case NFA_ZCLOSE3:
3217 case NFA_ZCLOSE4:
3218 case NFA_ZCLOSE5:
3219 case NFA_ZCLOSE6:
3220 case NFA_ZCLOSE7:
3221 case NFA_ZCLOSE8:
3222 case NFA_ZCLOSE9:
3223#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003224 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003225 /* These nodes are not added themselves but their "out" and/or
3226 * "out1" may be added below. */
3227 break;
3228
3229 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003230 case NFA_MOPEN1:
3231 case NFA_MOPEN2:
3232 case NFA_MOPEN3:
3233 case NFA_MOPEN4:
3234 case NFA_MOPEN5:
3235 case NFA_MOPEN6:
3236 case NFA_MOPEN7:
3237 case NFA_MOPEN8:
3238 case NFA_MOPEN9:
3239#ifdef FEAT_SYN_HL
3240 case NFA_ZOPEN:
3241 case NFA_ZOPEN1:
3242 case NFA_ZOPEN2:
3243 case NFA_ZOPEN3:
3244 case NFA_ZOPEN4:
3245 case NFA_ZOPEN5:
3246 case NFA_ZOPEN6:
3247 case NFA_ZOPEN7:
3248 case NFA_ZOPEN8:
3249 case NFA_ZOPEN9:
3250#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003251 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003252 /* These nodes do not need to be added, but we need to bail out
3253 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003254 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003255 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003256 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003257 break;
3258
Bram Moolenaar307aa162013-06-02 16:34:21 +02003259 case NFA_BOL:
3260 case NFA_BOF:
3261 /* "^" won't match past end-of-line, don't bother trying.
3262 * Except when we are going to the next line for a look-behind
3263 * match. */
3264 if (reginput > regline
3265 && (nfa_endp == NULL
3266 || !REG_MULTI
3267 || reglnum == nfa_endp->se_u.pos.lnum))
3268 goto skip_add;
3269 /* FALLTHROUGH */
3270
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003272 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003274 /* This state is already in the list, don't add it again,
3275 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003276 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003277 {
3278skip_add:
3279#ifdef ENABLE_LOG
3280 nfa_set_code(state->c);
3281 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3282 abs(state->id), l->id, state->c, code);
3283#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003284 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003285 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003286
3287 /* See if the same state is already in the list with the same
3288 * positions. */
3289 for (i = 0; i < l->n; ++i)
3290 {
3291 thread = &l->t[i];
3292 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003293 && sub_equal(&thread->subs.norm, &subs->norm)
3294#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003295 && (!nfa_has_zsubexpr ||
3296 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003297#endif
3298 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003299 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003300 }
3301 }
3302
Bram Moolenaara2d95102013-06-04 14:23:05 +02003303 /* when there are backreferences or look-behind matches the number
3304 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003305 if (nfa_has_backref && l->n == l->len)
3306 {
3307 int newlen = l->len * 3 / 2 + 50;
3308
3309 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3310 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003312
3313 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003314 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003315 thread = &l->t[l->n++];
3316 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003317 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003318 copy_sub(&thread->subs.norm, &subs->norm);
3319#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003320 if (nfa_has_zsubexpr)
3321 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003322#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003323#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003324 report_state("Adding", &thread->subs.norm, state, l->id);
3325 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003326#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003327 }
3328
3329#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003330 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003331 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332#endif
3333 switch (state->c)
3334 {
3335 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003336 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003337 break;
3338
3339 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003340 /* order matters here */
3341 addstate(l, state->out, subs, off);
3342 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003343 break;
3344
Bram Moolenaar5714b802013-05-28 22:03:20 +02003345 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346 case NFA_NOPEN:
3347 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003348 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003349 break;
3350
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003351 case NFA_MOPEN:
3352 case NFA_MOPEN1:
3353 case NFA_MOPEN2:
3354 case NFA_MOPEN3:
3355 case NFA_MOPEN4:
3356 case NFA_MOPEN5:
3357 case NFA_MOPEN6:
3358 case NFA_MOPEN7:
3359 case NFA_MOPEN8:
3360 case NFA_MOPEN9:
3361#ifdef FEAT_SYN_HL
3362 case NFA_ZOPEN:
3363 case NFA_ZOPEN1:
3364 case NFA_ZOPEN2:
3365 case NFA_ZOPEN3:
3366 case NFA_ZOPEN4:
3367 case NFA_ZOPEN5:
3368 case NFA_ZOPEN6:
3369 case NFA_ZOPEN7:
3370 case NFA_ZOPEN8:
3371 case NFA_ZOPEN9:
3372#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003373 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003374 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003375 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003377 sub = &subs->norm;
3378 }
3379#ifdef FEAT_SYN_HL
3380 else if (state->c >= NFA_ZOPEN)
3381 {
3382 subidx = state->c - NFA_ZOPEN;
3383 sub = &subs->synt;
3384 }
3385#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003386 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003387 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003388 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003389 sub = &subs->norm;
3390 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003391
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003392 /* Set the position (with "off") in the subexpression. Save and
3393 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003394 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003395 if (REG_MULTI)
3396 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003397 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003398 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003399 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003400 save_in_use = -1;
3401 }
3402 else
3403 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003404 save_in_use = sub->in_use;
3405 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003406 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003407 sub->list.multi[i].start.lnum = -1;
3408 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003409 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003410 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003411 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003412 if (off == -1)
3413 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003414 sub->list.multi[subidx].start.lnum = reglnum + 1;
3415 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003416 }
3417 else
3418 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003419 sub->list.multi[subidx].start.lnum = reglnum;
3420 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003421 (colnr_T)(reginput - regline + off);
3422 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003423 }
3424 else
3425 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003426 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003427 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003428 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003429 save_in_use = -1;
3430 }
3431 else
3432 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003433 save_in_use = sub->in_use;
3434 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003435 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003436 sub->list.line[i].start = NULL;
3437 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003438 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003439 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003440 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003441 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003442 }
3443
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003444 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003446 if (save_in_use == -1)
3447 {
3448 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003449 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003450 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003451 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003452 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003453 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003454 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 break;
3456
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003457 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003458 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003460 /* Do not overwrite the position set by \ze. If no \ze
3461 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003462 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003463 break;
3464 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003465 case NFA_MCLOSE1:
3466 case NFA_MCLOSE2:
3467 case NFA_MCLOSE3:
3468 case NFA_MCLOSE4:
3469 case NFA_MCLOSE5:
3470 case NFA_MCLOSE6:
3471 case NFA_MCLOSE7:
3472 case NFA_MCLOSE8:
3473 case NFA_MCLOSE9:
3474#ifdef FEAT_SYN_HL
3475 case NFA_ZCLOSE:
3476 case NFA_ZCLOSE1:
3477 case NFA_ZCLOSE2:
3478 case NFA_ZCLOSE3:
3479 case NFA_ZCLOSE4:
3480 case NFA_ZCLOSE5:
3481 case NFA_ZCLOSE6:
3482 case NFA_ZCLOSE7:
3483 case NFA_ZCLOSE8:
3484 case NFA_ZCLOSE9:
3485#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003486 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003488 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003490 sub = &subs->norm;
3491 }
3492#ifdef FEAT_SYN_HL
3493 else if (state->c >= NFA_ZCLOSE)
3494 {
3495 subidx = state->c - NFA_ZCLOSE;
3496 sub = &subs->synt;
3497 }
3498#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003499 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003500 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003501 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003502 sub = &subs->norm;
3503 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003505 /* We don't fill in gaps here, there must have been an MOPEN that
3506 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003507 save_in_use = sub->in_use;
3508 if (sub->in_use <= subidx)
3509 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 if (REG_MULTI)
3511 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003512 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003513 if (off == -1)
3514 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003515 sub->list.multi[subidx].end.lnum = reglnum + 1;
3516 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003517 }
3518 else
3519 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003520 sub->list.multi[subidx].end.lnum = reglnum;
3521 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003522 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003523 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524 }
3525 else
3526 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003527 save_ptr = sub->list.line[subidx].end;
3528 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529 }
3530
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003531 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003532
3533 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003534 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003535 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003536 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003537 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003538 break;
3539 }
3540}
3541
3542/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003543 * Like addstate(), but the new state(s) are put at position "*ip".
3544 * Used for zero-width matches, next state to use is the added one.
3545 * This makes sure the order of states to be tried does not change, which
3546 * matters for alternatives.
3547 */
3548 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003549addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003550 nfa_list_T *l; /* runtime state list */
3551 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003552 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003553 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003554 int *ip;
3555{
3556 int tlen = l->n;
3557 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003558 int listidx = *ip;
3559 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003560
3561 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003562 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003563
Bram Moolenaara2d95102013-06-04 14:23:05 +02003564 /* fill in the "pim" field in the new states */
3565 if (pim != NULL)
3566 for (i = tlen; i < l->n; ++i)
3567 l->t[i].pim = pim;
3568
Bram Moolenaar4b417062013-05-25 20:19:50 +02003569 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003570 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003571 return;
3572
3573 /* re-order to put the new state at the current position */
3574 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003575 if (count == 1)
3576 {
3577 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003578 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003579 }
3580 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003581 {
3582 /* make space for new states, then move them from the
3583 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003584 mch_memmove(&(l->t[listidx + count]),
3585 &(l->t[listidx + 1]),
3586 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3587 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003588 &(l->t[l->n - 1]),
3589 sizeof(nfa_thread_T) * count);
3590 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003591 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003592 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003593}
3594
3595/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596 * Check character class "class" against current character c.
3597 */
3598 static int
3599check_char_class(class, c)
3600 int class;
3601 int c;
3602{
3603 switch (class)
3604 {
3605 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003606 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003607 return OK;
3608 break;
3609 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003610 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003611 return OK;
3612 break;
3613 case NFA_CLASS_BLANK:
3614 if (c == ' ' || c == '\t')
3615 return OK;
3616 break;
3617 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003618 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 return OK;
3620 break;
3621 case NFA_CLASS_DIGIT:
3622 if (VIM_ISDIGIT(c))
3623 return OK;
3624 break;
3625 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003626 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003627 return OK;
3628 break;
3629 case NFA_CLASS_LOWER:
3630 if (MB_ISLOWER(c))
3631 return OK;
3632 break;
3633 case NFA_CLASS_PRINT:
3634 if (vim_isprintc(c))
3635 return OK;
3636 break;
3637 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003638 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639 return OK;
3640 break;
3641 case NFA_CLASS_SPACE:
3642 if ((c >=9 && c <= 13) || (c == ' '))
3643 return OK;
3644 break;
3645 case NFA_CLASS_UPPER:
3646 if (MB_ISUPPER(c))
3647 return OK;
3648 break;
3649 case NFA_CLASS_XDIGIT:
3650 if (vim_isxdigit(c))
3651 return OK;
3652 break;
3653 case NFA_CLASS_TAB:
3654 if (c == '\t')
3655 return OK;
3656 break;
3657 case NFA_CLASS_RETURN:
3658 if (c == '\r')
3659 return OK;
3660 break;
3661 case NFA_CLASS_BACKSPACE:
3662 if (c == '\b')
3663 return OK;
3664 break;
3665 case NFA_CLASS_ESCAPE:
3666 if (c == '\033')
3667 return OK;
3668 break;
3669
3670 default:
3671 /* should not be here :P */
3672 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3673 }
3674 return FAIL;
3675}
3676
Bram Moolenaar5714b802013-05-28 22:03:20 +02003677static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3678
3679/*
3680 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003681 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003682 */
3683 static int
3684match_backref(sub, subidx, bytelen)
3685 regsub_T *sub; /* pointers to subexpressions */
3686 int subidx;
3687 int *bytelen; /* out: length of match in bytes */
3688{
3689 int len;
3690
3691 if (sub->in_use <= subidx)
3692 {
3693retempty:
3694 /* backref was not set, match an empty string */
3695 *bytelen = 0;
3696 return TRUE;
3697 }
3698
3699 if (REG_MULTI)
3700 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003701 if (sub->list.multi[subidx].start.lnum < 0
3702 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003703 goto retempty;
3704 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003705 len = sub->list.multi[subidx].end.col
3706 - sub->list.multi[subidx].start.col;
3707 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003708 reginput, &len) == 0)
3709 {
3710 *bytelen = len;
3711 return TRUE;
3712 }
3713 }
3714 else
3715 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003716 if (sub->list.line[subidx].start == NULL
3717 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003718 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003719 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3720 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003721 {
3722 *bytelen = len;
3723 return TRUE;
3724 }
3725 }
3726 return FALSE;
3727}
3728
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003729#ifdef FEAT_SYN_HL
3730
3731static int match_zref __ARGS((int subidx, int *bytelen));
3732
3733/*
3734 * Check for a match with \z subexpression "subidx".
3735 * Return TRUE if it matches.
3736 */
3737 static int
3738match_zref(subidx, bytelen)
3739 int subidx;
3740 int *bytelen; /* out: length of match in bytes */
3741{
3742 int len;
3743
3744 cleanup_zsubexpr();
3745 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3746 {
3747 /* backref was not set, match an empty string */
3748 *bytelen = 0;
3749 return TRUE;
3750 }
3751
3752 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3753 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3754 {
3755 *bytelen = len;
3756 return TRUE;
3757 }
3758 return FALSE;
3759}
3760#endif
3761
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003762/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003763 * Save list IDs for all NFA states of "prog" into "list".
3764 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003765 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003766 */
3767 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003768nfa_save_listids(prog, list)
3769 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003770 int *list;
3771{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003772 int i;
3773 nfa_state_T *p;
3774
3775 /* Order in the list is reverse, it's a bit faster that way. */
3776 p = &prog->state[0];
3777 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003778 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003779 list[i] = p->lastlist[1];
3780 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003781 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003782 }
3783}
3784
3785/*
3786 * Restore list IDs from "list" to all NFA states.
3787 */
3788 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003789nfa_restore_listids(prog, list)
3790 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003791 int *list;
3792{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003793 int i;
3794 nfa_state_T *p;
3795
3796 p = &prog->state[0];
3797 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003798 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003799 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003800 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003801 }
3802}
3803
Bram Moolenaar423532e2013-05-29 21:14:42 +02003804 static int
3805nfa_re_num_cmp(val, op, pos)
3806 long_u val;
3807 int op;
3808 long_u pos;
3809{
3810 if (op == 1) return pos > val;
3811 if (op == 2) return pos < val;
3812 return val == pos;
3813}
3814
Bram Moolenaarf46da702013-06-02 22:37:42 +02003815static 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 +02003816static 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 +02003817
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003818/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003819 * Recursively call nfa_regmatch()
3820 */
3821 static int
3822recursive_regmatch(state, prog, submatch, m, listids)
3823 nfa_state_T *state;
3824 nfa_regprog_T *prog;
3825 regsubs_T *submatch;
3826 regsubs_T *m;
3827 int **listids;
3828{
3829 char_u *save_reginput = reginput;
3830 char_u *save_regline = regline;
3831 int save_reglnum = reglnum;
3832 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003833 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003834 save_se_T *save_nfa_endp = nfa_endp;
3835 save_se_T endpos;
3836 save_se_T *endposp = NULL;
3837 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003838 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003839
3840 if (state->c == NFA_START_INVISIBLE_BEFORE)
3841 {
3842 /* The recursive match must end at the current position. */
3843 endposp = &endpos;
3844 if (REG_MULTI)
3845 {
3846 endpos.se_u.pos.col = (int)(reginput - regline);
3847 endpos.se_u.pos.lnum = reglnum;
3848 }
3849 else
3850 endpos.se_u.ptr = reginput;
3851
3852 /* Go back the specified number of bytes, or as far as the
3853 * start of the previous line, to try matching "\@<=" or
3854 * not matching "\@<!".
3855 * TODO: This is very inefficient! Would be better to
3856 * first check for a match with what follows. */
3857 if (state->val <= 0)
3858 {
3859 if (REG_MULTI)
3860 {
3861 regline = reg_getline(--reglnum);
3862 if (regline == NULL)
3863 /* can't go before the first line */
3864 regline = reg_getline(++reglnum);
3865 }
3866 reginput = regline;
3867 }
3868 else
3869 {
3870 if (REG_MULTI && (int)(reginput - regline) < state->val)
3871 {
3872 /* Not enough bytes in this line, go to end of
3873 * previous line. */
3874 regline = reg_getline(--reglnum);
3875 if (regline == NULL)
3876 {
3877 /* can't go before the first line */
3878 regline = reg_getline(++reglnum);
3879 reginput = regline;
3880 }
3881 else
3882 reginput = regline + STRLEN(regline);
3883 }
3884 if ((int)(reginput - regline) >= state->val)
3885 {
3886 reginput -= state->val;
3887#ifdef FEAT_MBYTE
3888 if (has_mbyte)
3889 reginput -= mb_head_off(regline, reginput);
3890#endif
3891 }
3892 else
3893 reginput = regline;
3894 }
3895 }
3896
Bram Moolenaarf46da702013-06-02 22:37:42 +02003897#ifdef ENABLE_LOG
3898 if (log_fd != stderr)
3899 fclose(log_fd);
3900 log_fd = NULL;
3901#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003902 /* Have to clear the lastlist field of the NFA nodes, so that
3903 * nfa_regmatch() and addstate() can run properly after recursion. */
3904 if (nfa_ll_index == 1)
3905 {
3906 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3907 * values and clear them. */
3908 if (*listids == NULL)
3909 {
3910 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3911 if (*listids == NULL)
3912 {
3913 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3914 return 0;
3915 }
3916 }
3917 nfa_save_listids(prog, *listids);
3918 need_restore = TRUE;
3919 /* any value of nfa_listid will do */
3920 }
3921 else
3922 {
3923 /* First recursive nfa_regmatch() call, switch to the second lastlist
3924 * entry. Make sure nfa_listid is different from a previous recursive
3925 * call, because some states may still have this ID. */
3926 ++nfa_ll_index;
3927 if (nfa_listid <= nfa_alt_listid)
3928 nfa_listid = nfa_alt_listid;
3929 }
3930
3931 /* Call nfa_regmatch() to check if the current concat matches at this
3932 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003933 nfa_endp = endposp;
3934 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003935
3936 if (need_restore)
3937 nfa_restore_listids(prog, *listids);
3938 else
3939 {
3940 --nfa_ll_index;
3941 nfa_alt_listid = nfa_listid;
3942 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003943
3944 /* restore position in input text */
3945 reginput = save_reginput;
3946 regline = save_regline;
3947 reglnum = save_reglnum;
3948 nfa_match = save_nfa_match;
3949 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003950 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003951
3952#ifdef ENABLE_LOG
3953 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3954 if (log_fd != NULL)
3955 {
3956 fprintf(log_fd, "****************************\n");
3957 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3958 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3959 fprintf(log_fd, "****************************\n");
3960 }
3961 else
3962 {
3963 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3964 log_fd = stderr;
3965 }
3966#endif
3967
3968 return result;
3969}
3970
Bram Moolenaara2d95102013-06-04 14:23:05 +02003971static int failure_chance __ARGS((nfa_state_T *state, int depth));
3972
3973/*
3974 * Estimate the chance of a match with "state" failing.
3975 * NFA_ANY: 1
3976 * specific character: 99
3977 */
3978 static int
3979failure_chance(state, depth)
3980 nfa_state_T *state;
3981 int depth;
3982{
3983 int c = state->c;
3984 int l, r;
3985
3986 /* detect looping */
3987 if (depth > 4)
3988 return 1;
3989
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003990 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02003991 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003992 case NFA_SPLIT:
3993 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3994 /* avoid recursive stuff */
3995 return 1;
3996 /* two alternatives, use the lowest failure chance */
3997 l = failure_chance(state->out, depth + 1);
3998 r = failure_chance(state->out1, depth + 1);
3999 return l < r ? l : r;
4000
4001 case NFA_ANY:
4002 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004003 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004004 case NFA_MATCH:
4005 /* empty match works always */
4006 return 0;
4007
4008 case NFA_BOL:
4009 case NFA_EOL:
4010 case NFA_BOF:
4011 case NFA_EOF:
4012 case NFA_NEWL:
4013 return 99;
4014
4015 case NFA_BOW:
4016 case NFA_EOW:
4017 return 90;
4018
4019 case NFA_MOPEN:
4020 case NFA_MOPEN1:
4021 case NFA_MOPEN2:
4022 case NFA_MOPEN3:
4023 case NFA_MOPEN4:
4024 case NFA_MOPEN5:
4025 case NFA_MOPEN6:
4026 case NFA_MOPEN7:
4027 case NFA_MOPEN8:
4028 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004029#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004030 case NFA_ZOPEN:
4031 case NFA_ZOPEN1:
4032 case NFA_ZOPEN2:
4033 case NFA_ZOPEN3:
4034 case NFA_ZOPEN4:
4035 case NFA_ZOPEN5:
4036 case NFA_ZOPEN6:
4037 case NFA_ZOPEN7:
4038 case NFA_ZOPEN8:
4039 case NFA_ZOPEN9:
4040 case NFA_ZCLOSE:
4041 case NFA_ZCLOSE1:
4042 case NFA_ZCLOSE2:
4043 case NFA_ZCLOSE3:
4044 case NFA_ZCLOSE4:
4045 case NFA_ZCLOSE5:
4046 case NFA_ZCLOSE6:
4047 case NFA_ZCLOSE7:
4048 case NFA_ZCLOSE8:
4049 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004050#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004051 case NFA_NOPEN:
4052 case NFA_MCLOSE:
4053 case NFA_MCLOSE1:
4054 case NFA_MCLOSE2:
4055 case NFA_MCLOSE3:
4056 case NFA_MCLOSE4:
4057 case NFA_MCLOSE5:
4058 case NFA_MCLOSE6:
4059 case NFA_MCLOSE7:
4060 case NFA_MCLOSE8:
4061 case NFA_MCLOSE9:
4062 case NFA_NCLOSE:
4063 return failure_chance(state->out, depth + 1);
4064
4065 case NFA_BACKREF1:
4066 case NFA_BACKREF2:
4067 case NFA_BACKREF3:
4068 case NFA_BACKREF4:
4069 case NFA_BACKREF5:
4070 case NFA_BACKREF6:
4071 case NFA_BACKREF7:
4072 case NFA_BACKREF8:
4073 case NFA_BACKREF9:
4074#ifdef FEAT_SYN_HL
4075 case NFA_ZREF1:
4076 case NFA_ZREF2:
4077 case NFA_ZREF3:
4078 case NFA_ZREF4:
4079 case NFA_ZREF5:
4080 case NFA_ZREF6:
4081 case NFA_ZREF7:
4082 case NFA_ZREF8:
4083 case NFA_ZREF9:
4084#endif
4085 /* backreferences don't match in many places */
4086 return 94;
4087
4088 case NFA_LNUM_GT:
4089 case NFA_LNUM_LT:
4090 case NFA_COL_GT:
4091 case NFA_COL_LT:
4092 case NFA_VCOL_GT:
4093 case NFA_VCOL_LT:
4094 case NFA_MARK_GT:
4095 case NFA_MARK_LT:
4096#ifdef FEAT_VISUAL
4097 case NFA_VISUAL:
4098#endif
4099 /* before/after positions don't match very often */
4100 return 85;
4101
4102 case NFA_LNUM:
4103 return 90;
4104
4105 case NFA_CURSOR:
4106 case NFA_COL:
4107 case NFA_VCOL:
4108 case NFA_MARK:
4109 /* specific positions rarely match */
4110 return 98;
4111
4112 case NFA_COMPOSING:
4113 return 95;
4114
4115 default:
4116 if (c > 0)
4117 /* character match fails often */
4118 return 95;
4119 }
4120
4121 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004122 return 50;
4123}
4124
Bram Moolenaarf46da702013-06-02 22:37:42 +02004125/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004126 * Main matching routine.
4127 *
4128 * Run NFA to determine whether it matches reginput.
4129 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004130 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004131 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004132 * Return TRUE if there is a match, FALSE otherwise.
4133 * Note: Caller must ensure that: start != NULL.
4134 */
4135 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004136nfa_regmatch(prog, start, submatch, m)
4137 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004138 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004139 regsubs_T *submatch;
4140 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004142 int result;
4143 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004144 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004145 int go_to_nextline = FALSE;
4146 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004147 nfa_list_T list[3];
4148 nfa_list_T *listtbl[2][2];
4149 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004150 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004151 nfa_list_T *thislist;
4152 nfa_list_T *nextlist;
4153 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004154 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004155 nfa_state_T *add_state;
4156 int add_count;
4157 int add_off;
4158 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004159#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004160 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004161
4162 if (debug == NULL)
4163 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004164 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004165 return FALSE;
4166 }
4167#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004168 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004169 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004170
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004171 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004172 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004173 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4174 list[0].len = nstate + 1;
4175 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4176 list[1].len = nstate + 1;
4177 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4178 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004179 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
4180 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004181
4182#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004183 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004184 if (log_fd != NULL)
4185 {
4186 fprintf(log_fd, "**********************************\n");
4187 nfa_set_code(start->c);
4188 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4189 abs(start->id), code);
4190 fprintf(log_fd, "**********************************\n");
4191 }
4192 else
4193 {
4194 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4195 log_fd = stderr;
4196 }
4197#endif
4198
4199 thislist = &list[0];
4200 thislist->n = 0;
4201 nextlist = &list[1];
4202 nextlist->n = 0;
4203 neglist = &list[2];
4204 neglist->n = 0;
4205#ifdef ENABLE_LOG
4206 fprintf(log_fd, "(---) STARTSTATE\n");
4207#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004208 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004209 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004210
4211 /* There are two cases when the NFA advances: 1. input char matches the
4212 * NFA node and 2. input char does not match the NFA node, but the next
4213 * node is NFA_NOT. The following macro calls addstate() according to
4214 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4215 listtbl[0][0] = NULL;
4216 listtbl[0][1] = neglist;
4217 listtbl[1][0] = nextlist;
4218 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004219#define ADD_POS_NEG_STATE(state) \
4220 ll = listtbl[result ? 1 : 0][state->negated]; \
4221 if (ll != NULL) { \
4222 add_state = state->out; \
4223 add_off = clen; \
4224 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225
4226 /*
4227 * Run for each character.
4228 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004229 for (;;)
4230 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004231 int curc;
4232 int clen;
4233
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004234#ifdef FEAT_MBYTE
4235 if (has_mbyte)
4236 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004237 curc = (*mb_ptr2char)(reginput);
4238 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004239 }
4240 else
4241#endif
4242 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004243 curc = *reginput;
4244 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004245 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004246 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004247 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004248 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004249 go_to_nextline = FALSE;
4250 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004251
4252 /* swap lists */
4253 thislist = &list[flag];
4254 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004255 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004257 ++nfa_listid;
4258 thislist->id = nfa_listid;
4259 nextlist->id = nfa_listid + 1;
4260 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004261
Bram Moolenaara2d95102013-06-04 14:23:05 +02004262 pimlist.ga_len = 0;
4263
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004264#ifdef ENABLE_LOG
4265 fprintf(log_fd, "------------------------------------------\n");
4266 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004267 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004268 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004269 {
4270 int i;
4271
4272 for (i = 0; i < thislist->n; i++)
4273 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4274 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004275 fprintf(log_fd, "\n");
4276#endif
4277
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004278#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004279 fprintf(debug, "\n-------------------\n");
4280#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004281 /*
4282 * If the state lists are empty we can stop.
4283 */
4284 if (thislist->n == 0 && neglist->n == 0)
4285 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004286
4287 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004288 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004289 {
4290 if (neglist->n > 0)
4291 {
4292 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004293 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004294 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295 }
4296 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004297 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004299#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004300 nfa_set_code(t->state->c);
4301 fprintf(debug, "%s, ", code);
4302#endif
4303#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004304 {
4305 int col;
4306
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004307 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004308 col = -1;
4309 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004310 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004311 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004312 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004313 nfa_set_code(t->state->c);
4314 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4315 abs(t->state->id), (int)t->state->c, code, col);
4316 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004317#endif
4318
4319 /*
4320 * Handle the possible codes of the current state.
4321 * The most important is NFA_MATCH.
4322 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004323 add_state = NULL;
4324 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004325 switch (t->state->c)
4326 {
4327 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004328 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004329 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004330 copy_sub(&submatch->norm, &t->subs.norm);
4331#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004332 if (nfa_has_zsubexpr)
4333 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004334#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004335#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004336 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004337#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004338 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004339 * states at this position. When the list of states is going
4340 * to be empty quit without advancing, so that "reginput" is
4341 * correct. */
4342 if (nextlist->n == 0 && neglist->n == 0)
4343 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004344 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004345 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004346
4347 case NFA_END_INVISIBLE:
Bram Moolenaar87953742013-06-05 18:52:40 +02004348 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004349 /*
4350 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004351 * NFA_START_INVISIBLE_BEFORE node.
4352 * They surround a zero-width group, used with "\@=", "\&",
4353 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 * If we got here, it means that the current "invisible" group
4355 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004356 * nfa_regmatch(). For a look-behind match only when it ends
4357 * in the position in "nfa_endp".
4358 * Submatches are stored in *m, and used in the parent call.
4359 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004360#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004361 if (nfa_endp != NULL)
4362 {
4363 if (REG_MULTI)
4364 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4365 (int)reglnum,
4366 (int)nfa_endp->se_u.pos.lnum,
4367 (int)(reginput - regline),
4368 nfa_endp->se_u.pos.col);
4369 else
4370 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4371 (int)(reginput - regline),
4372 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004373 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004374#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004375 /* If "nfa_endp" is set it's only a match if it ends at
4376 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004377 if (nfa_endp != NULL && (REG_MULTI
4378 ? (reglnum != nfa_endp->se_u.pos.lnum
4379 || (int)(reginput - regline)
4380 != nfa_endp->se_u.pos.col)
4381 : reginput != nfa_endp->se_u.ptr))
4382 break;
4383
4384 /* do not set submatches for \@! */
4385 if (!t->state->negated)
4386 {
4387 copy_sub(&m->norm, &t->subs.norm);
4388#ifdef FEAT_SYN_HL
4389 if (nfa_has_zsubexpr)
4390 copy_sub(&m->synt, &t->subs.synt);
4391#endif
4392 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004393#ifdef ENABLE_LOG
4394 fprintf(log_fd, "Match found:\n");
4395 log_subsexpr(m);
4396#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004397 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004398 break;
4399
4400 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004401 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004402 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004403 nfa_pim_T *pim;
4404 int cout = t->state->out1->out->c;
4405
4406 /* Do it directly when what follows is possibly end of
4407 * match (closing paren).
4408 * Postpone when it is \@<= or \@<!, these are expensive.
4409 * TODO: remove the check for t->pim and check multiple
4410 * where it's used?
4411 * Otherwise first do the one that has the highest chance
4412 * of failing. */
4413 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004414#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004415 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004416#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004417 || cout == NFA_NCLOSE
4418 || t->pim != NULL
4419 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4420 && failure_chance(t->state->out1->out, 0)
4421 < failure_chance(t->state->out, 0)))
4422 {
4423 /*
4424 * First try matching the invisible match, then what
4425 * follows.
4426 */
4427 result = recursive_regmatch(t->state, prog,
4428 submatch, m, &listids);
4429
4430 /* for \@! it is a match when result is FALSE */
4431 if (result != t->state->negated)
4432 {
4433 /* Copy submatch info from the recursive call */
4434 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004435#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004436 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004437#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004438
Bram Moolenaara2d95102013-06-04 14:23:05 +02004439 /* t->state->out1 is the corresponding
4440 * END_INVISIBLE node; Add its out to the current
4441 * list (zero-width match). */
4442 addstate_here(thislist, t->state->out1->out,
4443 &t->subs, t->pim, &listidx);
4444 }
4445 }
4446 else
4447 {
4448 /*
4449 * First try matching what follows at the current
4450 * position. Only if a match is found, addstate() is
4451 * called, then verify the invisible match matches.
4452 * Add a nfa_pim_T to the following states, it
4453 * contains info about the invisible match.
4454 */
4455 if (ga_grow(&pimlist, 1) == FAIL)
4456 goto theend;
4457 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4458 ++pimlist.ga_len;
4459 pim->state = t->state;
4460 pim->pim = NULL;
4461 pim->result = NFA_PIM_TODO;
4462
4463 /* t->state->out1 is the corresponding END_INVISIBLE
4464 * node; Add its out to the current list (zero-width
4465 * match). */
4466 addstate_here(thislist, t->state->out1->out, &t->subs,
4467 pim, &listidx);
4468 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004470 break;
4471
Bram Moolenaar87953742013-06-05 18:52:40 +02004472 case NFA_START_PATTERN:
4473 /* First try matching the pattern. */
4474 result = recursive_regmatch(t->state, prog,
4475 submatch, m, &listids);
4476 if (result)
4477 {
4478 int bytelen;
4479
4480#ifdef ENABLE_LOG
4481 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4482 log_subsexpr(m);
4483#endif
4484 /* Copy submatch info from the recursive call */
4485 copy_sub_off(&t->subs.norm, &m->norm);
4486#ifdef FEAT_SYN_HL
4487 copy_sub_off(&t->subs.synt, &m->synt);
4488#endif
4489 /* Now we need to skip over the matched text and then
4490 * continue with what follows. */
4491 if (REG_MULTI)
4492 /* TODO: multi-line match */
4493 bytelen = m->norm.list.multi[0].end.col
4494 - (int)(reginput - regline);
4495 else
4496 bytelen = (int)(m->norm.list.line[0].end - reginput);
4497
4498#ifdef ENABLE_LOG
4499 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4500#endif
4501 if (bytelen == 0)
4502 {
4503 /* empty match, output of corresponding
4504 * NFA_END_PATTERN/NFA_SKIP to be used at current
4505 * position */
4506 addstate_here(thislist, t->state->out1->out->out,
4507 &t->subs, t->pim, &listidx);
4508 }
4509 else if (bytelen <= clen)
4510 {
4511 /* match current character, output of corresponding
4512 * NFA_END_PATTERN to be used at next position. */
4513 ll = nextlist;
4514 add_state = t->state->out1->out->out;
4515 add_off = clen;
4516 }
4517 else
4518 {
4519 /* skip over the matched characters, set character
4520 * count in NFA_SKIP */
4521 ll = nextlist;
4522 add_state = t->state->out1->out;
4523 add_off = bytelen;
4524 add_count = bytelen - clen;
4525 }
4526 }
4527 break;
4528
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529 case NFA_BOL:
4530 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004531 addstate_here(thislist, t->state->out, &t->subs,
4532 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004533 break;
4534
4535 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004536 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004537 addstate_here(thislist, t->state->out, &t->subs,
4538 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539 break;
4540
4541 case NFA_BOW:
4542 {
4543 int bow = TRUE;
4544
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004545 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004546 bow = FALSE;
4547#ifdef FEAT_MBYTE
4548 else if (has_mbyte)
4549 {
4550 int this_class;
4551
4552 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004553 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 if (this_class <= 1)
4555 bow = FALSE;
4556 else if (reg_prev_class() == this_class)
4557 bow = FALSE;
4558 }
4559#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004560 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004561 || (reginput > regline
4562 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 bow = FALSE;
4564 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004565 addstate_here(thislist, t->state->out, &t->subs,
4566 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004567 break;
4568 }
4569
4570 case NFA_EOW:
4571 {
4572 int eow = TRUE;
4573
4574 if (reginput == regline)
4575 eow = FALSE;
4576#ifdef FEAT_MBYTE
4577 else if (has_mbyte)
4578 {
4579 int this_class, prev_class;
4580
4581 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004582 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004583 prev_class = reg_prev_class();
4584 if (this_class == prev_class
4585 || prev_class == 0 || prev_class == 1)
4586 eow = FALSE;
4587 }
4588#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004589 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004590 || (reginput[0] != NUL
4591 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004592 eow = FALSE;
4593 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004594 addstate_here(thislist, t->state->out, &t->subs,
4595 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004596 break;
4597 }
4598
Bram Moolenaar4b780632013-05-31 22:14:52 +02004599 case NFA_BOF:
4600 if (reglnum == 0 && reginput == regline
4601 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004602 addstate_here(thislist, t->state->out, &t->subs,
4603 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004604 break;
4605
4606 case NFA_EOF:
4607 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004608 addstate_here(thislist, t->state->out, &t->subs,
4609 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004610 break;
4611
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004612#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004613 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004614 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004615 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004616 int len = 0;
4617 nfa_state_T *end;
4618 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004619 int cchars[MAX_MCO];
4620 int ccount = 0;
4621 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004622
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004623 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004624 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004625 if (utf_iscomposing(sta->c))
4626 {
4627 /* Only match composing character(s), ignore base
4628 * character. Used for ".{composing}" and "{composing}"
4629 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004630 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004631 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004632 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004633 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004634 /* If \Z was present, then ignore composing characters.
4635 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004636 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004637 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004638 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004639 else
4640 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004641 while (sta->c != NFA_END_COMPOSING)
4642 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004643 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004644
4645 /* Check base character matches first, unless ignored. */
4646 else if (len > 0 || mc == sta->c)
4647 {
4648 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004649 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004650 len += mb_char2len(mc);
4651 sta = sta->out;
4652 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004653
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004654 /* We don't care about the order of composing characters.
4655 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004656 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004657 {
4658 mc = mb_ptr2char(reginput + len);
4659 cchars[ccount++] = mc;
4660 len += mb_char2len(mc);
4661 if (ccount == MAX_MCO)
4662 break;
4663 }
4664
4665 /* Check that each composing char in the pattern matches a
4666 * composing char in the text. We do not check if all
4667 * composing chars are matched. */
4668 result = OK;
4669 while (sta->c != NFA_END_COMPOSING)
4670 {
4671 for (j = 0; j < ccount; ++j)
4672 if (cchars[j] == sta->c)
4673 break;
4674 if (j == ccount)
4675 {
4676 result = FAIL;
4677 break;
4678 }
4679 sta = sta->out;
4680 }
4681 }
4682 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004683 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004684
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004685 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686 ADD_POS_NEG_STATE(end);
4687 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004688 }
4689#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004690
4691 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004692 if (curc == NUL && !reg_line_lbr && REG_MULTI
4693 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004694 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004695 go_to_nextline = TRUE;
4696 /* Pass -1 for the offset, which means taking the position
4697 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004698 ll = nextlist;
4699 add_state = t->state->out;
4700 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004702 else if (curc == '\n' && reg_line_lbr)
4703 {
4704 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004705 ll = nextlist;
4706 add_state = t->state->out;
4707 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004708 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004709 break;
4710
4711 case NFA_CLASS_ALNUM:
4712 case NFA_CLASS_ALPHA:
4713 case NFA_CLASS_BLANK:
4714 case NFA_CLASS_CNTRL:
4715 case NFA_CLASS_DIGIT:
4716 case NFA_CLASS_GRAPH:
4717 case NFA_CLASS_LOWER:
4718 case NFA_CLASS_PRINT:
4719 case NFA_CLASS_PUNCT:
4720 case NFA_CLASS_SPACE:
4721 case NFA_CLASS_UPPER:
4722 case NFA_CLASS_XDIGIT:
4723 case NFA_CLASS_TAB:
4724 case NFA_CLASS_RETURN:
4725 case NFA_CLASS_BACKSPACE:
4726 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004727 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004728 ADD_POS_NEG_STATE(t->state);
4729 break;
4730
4731 case NFA_END_NEG_RANGE:
4732 /* This follows a series of negated nodes, like:
4733 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004734 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004735 {
4736 ll = nextlist;
4737 add_state = t->state->out;
4738 add_off = clen;
4739 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004740 break;
4741
4742 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004743 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004744 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004745 {
4746 ll = nextlist;
4747 add_state = t->state->out;
4748 add_off = clen;
4749 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004750 break;
4751
4752 /*
4753 * Character classes like \a for alpha, \d for digit etc.
4754 */
4755 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004756 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004757 ADD_POS_NEG_STATE(t->state);
4758 break;
4759
4760 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004761 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004762 ADD_POS_NEG_STATE(t->state);
4763 break;
4764
4765 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004766 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004767 ADD_POS_NEG_STATE(t->state);
4768 break;
4769
4770 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004771 result = !VIM_ISDIGIT(curc)
4772 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004773 ADD_POS_NEG_STATE(t->state);
4774 break;
4775
4776 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004777 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 ADD_POS_NEG_STATE(t->state);
4779 break;
4780
4781 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004782 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004783 ADD_POS_NEG_STATE(t->state);
4784 break;
4785
4786 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004787 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004788 ADD_POS_NEG_STATE(t->state);
4789 break;
4790
4791 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004792 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004793 ADD_POS_NEG_STATE(t->state);
4794 break;
4795
4796 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004797 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004798 ADD_POS_NEG_STATE(t->state);
4799 break;
4800
4801 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004802 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004803 ADD_POS_NEG_STATE(t->state);
4804 break;
4805
4806 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004807 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004808 ADD_POS_NEG_STATE(t->state);
4809 break;
4810
4811 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004812 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004813 ADD_POS_NEG_STATE(t->state);
4814 break;
4815
4816 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004817 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004818 ADD_POS_NEG_STATE(t->state);
4819 break;
4820
4821 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004822 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004823 ADD_POS_NEG_STATE(t->state);
4824 break;
4825
4826 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004827 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004828 ADD_POS_NEG_STATE(t->state);
4829 break;
4830
4831 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004832 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004833 ADD_POS_NEG_STATE(t->state);
4834 break;
4835
4836 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004837 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004838 ADD_POS_NEG_STATE(t->state);
4839 break;
4840
4841 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004842 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004843 ADD_POS_NEG_STATE(t->state);
4844 break;
4845
4846 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004847 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004848 ADD_POS_NEG_STATE(t->state);
4849 break;
4850
4851 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004852 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004853 ADD_POS_NEG_STATE(t->state);
4854 break;
4855
4856 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004857 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858 ADD_POS_NEG_STATE(t->state);
4859 break;
4860
4861 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004862 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004863 ADD_POS_NEG_STATE(t->state);
4864 break;
4865
4866 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004867 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004868 ADD_POS_NEG_STATE(t->state);
4869 break;
4870
4871 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004872 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004873 ADD_POS_NEG_STATE(t->state);
4874 break;
4875
4876 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004877 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878 ADD_POS_NEG_STATE(t->state);
4879 break;
4880
4881 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004882 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004883 ADD_POS_NEG_STATE(t->state);
4884 break;
4885
Bram Moolenaar5714b802013-05-28 22:03:20 +02004886 case NFA_BACKREF1:
4887 case NFA_BACKREF2:
4888 case NFA_BACKREF3:
4889 case NFA_BACKREF4:
4890 case NFA_BACKREF5:
4891 case NFA_BACKREF6:
4892 case NFA_BACKREF7:
4893 case NFA_BACKREF8:
4894 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004895#ifdef FEAT_SYN_HL
4896 case NFA_ZREF1:
4897 case NFA_ZREF2:
4898 case NFA_ZREF3:
4899 case NFA_ZREF4:
4900 case NFA_ZREF5:
4901 case NFA_ZREF6:
4902 case NFA_ZREF7:
4903 case NFA_ZREF8:
4904 case NFA_ZREF9:
4905#endif
4906 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004907 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004908 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004909 int bytelen;
4910
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004911 if (t->state->c <= NFA_BACKREF9)
4912 {
4913 subidx = t->state->c - NFA_BACKREF1 + 1;
4914 result = match_backref(&t->subs.norm, subidx, &bytelen);
4915 }
4916#ifdef FEAT_SYN_HL
4917 else
4918 {
4919 subidx = t->state->c - NFA_ZREF1 + 1;
4920 result = match_zref(subidx, &bytelen);
4921 }
4922#endif
4923
Bram Moolenaar5714b802013-05-28 22:03:20 +02004924 if (result)
4925 {
4926 if (bytelen == 0)
4927 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004928 /* empty match always works, output of NFA_SKIP to be
4929 * used next */
4930 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004931 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004932 }
4933 else if (bytelen <= clen)
4934 {
4935 /* match current character, jump ahead to out of
4936 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004937 ll = nextlist;
4938 add_state = t->state->out->out;
4939 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004940 }
4941 else
4942 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004943 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004944 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004945 ll = nextlist;
4946 add_state = t->state->out;
4947 add_off = bytelen;
4948 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004949 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004950 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004951 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004952 }
4953 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004954 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004955 if (t->count - clen <= 0)
4956 {
4957 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004958 ll = nextlist;
4959 add_state = t->state->out;
4960 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004961 }
4962 else
4963 {
4964 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004965 ll = nextlist;
4966 add_state = t->state;
4967 add_off = 0;
4968 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004969 }
4970 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004971
Bram Moolenaar423532e2013-05-29 21:14:42 +02004972 case NFA_LNUM:
4973 case NFA_LNUM_GT:
4974 case NFA_LNUM_LT:
4975 result = (REG_MULTI &&
4976 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4977 (long_u)(reglnum + reg_firstlnum)));
4978 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004979 addstate_here(thislist, t->state->out, &t->subs,
4980 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004981 break;
4982
4983 case NFA_COL:
4984 case NFA_COL_GT:
4985 case NFA_COL_LT:
4986 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4987 (long_u)(reginput - regline) + 1);
4988 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004989 addstate_here(thislist, t->state->out, &t->subs,
4990 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004991 break;
4992
4993 case NFA_VCOL:
4994 case NFA_VCOL_GT:
4995 case NFA_VCOL_LT:
4996 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4997 (long_u)win_linetabsize(
4998 reg_win == NULL ? curwin : reg_win,
4999 regline, (colnr_T)(reginput - regline)) + 1);
5000 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005001 addstate_here(thislist, t->state->out, &t->subs,
5002 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005003 break;
5004
Bram Moolenaar044aa292013-06-04 21:27:38 +02005005 case NFA_MARK:
5006 case NFA_MARK_GT:
5007 case NFA_MARK_LT:
5008 {
5009 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5010
5011 /* Compare the mark position to the match position. */
5012 result = (pos != NULL /* mark doesn't exist */
5013 && pos->lnum > 0 /* mark isn't set in reg_buf */
5014 && (pos->lnum == reglnum + reg_firstlnum
5015 ? (pos->col == (colnr_T)(reginput - regline)
5016 ? t->state->c == NFA_MARK
5017 : (pos->col < (colnr_T)(reginput - regline)
5018 ? t->state->c == NFA_MARK_GT
5019 : t->state->c == NFA_MARK_LT))
5020 : (pos->lnum < reglnum + reg_firstlnum
5021 ? t->state->c == NFA_MARK_GT
5022 : t->state->c == NFA_MARK_LT)));
5023 if (result)
5024 addstate_here(thislist, t->state->out, &t->subs,
5025 t->pim, &listidx);
5026 break;
5027 }
5028
Bram Moolenaar423532e2013-05-29 21:14:42 +02005029 case NFA_CURSOR:
5030 result = (reg_win != NULL
5031 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5032 && ((colnr_T)(reginput - regline)
5033 == reg_win->w_cursor.col));
5034 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005035 addstate_here(thislist, t->state->out, &t->subs,
5036 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005037 break;
5038
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005039#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005040 case NFA_VISUAL:
5041 result = reg_match_visual();
5042 if (result)
5043 addstate_here(thislist, t->state->out, &t->subs,
5044 t->pim, &listidx);
5045 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005046#endif
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005047
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005048 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005049 {
5050 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005051
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005052 /* TODO: put this in #ifdef later */
5053 if (c < -256)
5054 EMSGN("INTERNAL: Negative state char: %ld", c);
5055 if (is_Magic(c))
5056 c = un_Magic(c);
5057 result = (c == curc);
5058
5059 if (!result && ireg_ic)
5060 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005061#ifdef FEAT_MBYTE
5062 /* If there is a composing character which is not being
5063 * ignored there can be no match. Match with composing
5064 * character uses NFA_COMPOSING above. */
5065 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005066 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005067 result = FALSE;
5068#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005069 ADD_POS_NEG_STATE(t->state);
5070 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005071 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005072
5073 } /* switch (t->state->c) */
5074
5075 if (add_state != NULL)
5076 {
5077 if (t->pim != NULL)
5078 {
5079 /* postponed invisible match */
5080 /* TODO: also do t->pim->pim recursively? */
5081 if (t->pim->result == NFA_PIM_TODO)
5082 {
5083#ifdef ENABLE_LOG
5084 fprintf(log_fd, "\n");
5085 fprintf(log_fd, "==================================\n");
5086 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5087 fprintf(log_fd, "\n");
5088#endif
5089 result = recursive_regmatch(t->pim->state,
5090 prog, submatch, m, &listids);
5091 t->pim->result = result ? NFA_PIM_MATCH
5092 : NFA_PIM_NOMATCH;
5093 /* for \@! it is a match when result is FALSE */
5094 if (result != t->pim->state->negated)
5095 {
5096 /* Copy submatch info from the recursive call */
5097 copy_sub_off(&t->pim->subs.norm, &m->norm);
5098#ifdef FEAT_SYN_HL
5099 copy_sub_off(&t->pim->subs.synt, &m->synt);
5100#endif
5101 }
5102 }
5103 else
5104 {
5105 result = (t->pim->result == NFA_PIM_MATCH);
5106#ifdef ENABLE_LOG
5107 fprintf(log_fd, "\n");
5108 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5109 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5110 fprintf(log_fd, "\n");
5111#endif
5112 }
5113
5114 /* for \@! it is a match when result is FALSE */
5115 if (result != t->pim->state->negated)
5116 {
5117 /* Copy submatch info from the recursive call */
5118 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5119#ifdef FEAT_SYN_HL
5120 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
5121#endif
5122 }
5123 else
5124 /* look-behind match failed, don't add the state */
5125 continue;
5126 }
5127
5128 addstate(ll, add_state, &t->subs, add_off);
5129 if (add_count > 0)
5130 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005131 }
5132
5133 } /* for (thislist = thislist; thislist->state; thislist++) */
5134
Bram Moolenaare23febd2013-05-26 18:40:14 +02005135 /* Look for the start of a match in the current position by adding the
5136 * start state to the list of states.
5137 * The first found match is the leftmost one, thus the order of states
5138 * matters!
5139 * Do not add the start state in recursive calls of nfa_regmatch(),
5140 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005141 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005142 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005143 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005144 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005145 && reglnum == 0
5146 && clen != 0
5147 && (ireg_maxcol == 0
5148 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005149 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005150 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005151 ? (reglnum < nfa_endp->se_u.pos.lnum
5152 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005153 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005154 < nfa_endp->se_u.pos.col))
5155 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005156 {
5157#ifdef ENABLE_LOG
5158 fprintf(log_fd, "(---) STARTSTATE\n");
5159#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005160 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005161 }
5162
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005163#ifdef ENABLE_LOG
5164 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005165 {
5166 int i;
5167
5168 for (i = 0; i < thislist->n; i++)
5169 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5170 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005171 fprintf(log_fd, "\n");
5172#endif
5173
5174nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005175 /* Advance to the next character, or advance to the next line, or
5176 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005177 if (clen != 0)
5178 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005179 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5180 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005181 reg_nextline();
5182 else
5183 break;
5184 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005185
5186#ifdef ENABLE_LOG
5187 if (log_fd != stderr)
5188 fclose(log_fd);
5189 log_fd = NULL;
5190#endif
5191
5192theend:
5193 /* Free memory */
5194 vim_free(list[0].t);
5195 vim_free(list[1].t);
5196 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005197 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005198 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005199#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005200#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005201 fclose(debug);
5202#endif
5203
Bram Moolenaar963fee22013-05-26 21:47:28 +02005204 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005205}
5206
5207/*
5208 * Try match of "prog" with at regline["col"].
5209 * Returns 0 for failure, number of lines contained in the match otherwise.
5210 */
5211 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005212nfa_regtry(prog, col)
5213 nfa_regprog_T *prog;
5214 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005215{
5216 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005217 regsubs_T subs, m;
5218 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005219#ifdef ENABLE_LOG
5220 FILE *f;
5221#endif
5222
5223 reginput = regline + col;
5224 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005225#ifdef FEAT_SYN_HL
5226 /* Clear the external match subpointers if necessary. */
5227 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005228 {
5229 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005230 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005231 }
5232 else
5233 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005234#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005235
5236#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005237 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005238 if (f != NULL)
5239 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005240 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005241#ifdef DEBUG
5242 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5243#endif
5244 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005245 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005246 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005247 fprintf(f, "\n\n");
5248 fclose(f);
5249 }
5250 else
5251 EMSG(_("Could not open temporary log file for writing "));
5252#endif
5253
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005254 clear_sub(&subs.norm);
5255 clear_sub(&m.norm);
5256#ifdef FEAT_SYN_HL
5257 clear_sub(&subs.synt);
5258 clear_sub(&m.synt);
5259#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005260
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005261 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005262 return 0;
5263
5264 cleanup_subexpr();
5265 if (REG_MULTI)
5266 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005267 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005268 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005269 reg_startpos[i] = subs.norm.list.multi[i].start;
5270 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005271 }
5272
5273 if (reg_startpos[0].lnum < 0)
5274 {
5275 reg_startpos[0].lnum = 0;
5276 reg_startpos[0].col = col;
5277 }
5278 if (reg_endpos[0].lnum < 0)
5279 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005280 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005281 reg_endpos[0].lnum = reglnum;
5282 reg_endpos[0].col = (int)(reginput - regline);
5283 }
5284 else
5285 /* Use line number of "\ze". */
5286 reglnum = reg_endpos[0].lnum;
5287 }
5288 else
5289 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005290 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005291 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005292 reg_startp[i] = subs.norm.list.line[i].start;
5293 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005294 }
5295
5296 if (reg_startp[0] == NULL)
5297 reg_startp[0] = regline + col;
5298 if (reg_endp[0] == NULL)
5299 reg_endp[0] = reginput;
5300 }
5301
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005302#ifdef FEAT_SYN_HL
5303 /* Package any found \z(...\) matches for export. Default is none. */
5304 unref_extmatch(re_extmatch_out);
5305 re_extmatch_out = NULL;
5306
5307 if (prog->reghasz == REX_SET)
5308 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005309 cleanup_zsubexpr();
5310 re_extmatch_out = make_extmatch();
5311 for (i = 0; i < subs.synt.in_use; i++)
5312 {
5313 if (REG_MULTI)
5314 {
5315 struct multipos *mpos = &subs.synt.list.multi[i];
5316
5317 /* Only accept single line matches. */
5318 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5319 re_extmatch_out->matches[i] =
5320 vim_strnsave(reg_getline(mpos->start.lnum)
5321 + mpos->start.col,
5322 mpos->end.col - mpos->start.col);
5323 }
5324 else
5325 {
5326 struct linepos *lpos = &subs.synt.list.line[i];
5327
5328 if (lpos->start != NULL && lpos->end != NULL)
5329 re_extmatch_out->matches[i] =
5330 vim_strnsave(lpos->start,
5331 (int)(lpos->end - lpos->start));
5332 }
5333 }
5334 }
5335#endif
5336
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005337 return 1 + reglnum;
5338}
5339
5340/*
5341 * Match a regexp against a string ("line" points to the string) or multiple
5342 * lines ("line" is NULL, use reg_getline()).
5343 *
5344 * Returns 0 for failure, number of lines contained in the match otherwise.
5345 */
5346 static long
5347nfa_regexec_both(line, col)
5348 char_u *line;
5349 colnr_T col; /* column to start looking for match */
5350{
5351 nfa_regprog_T *prog;
5352 long retval = 0L;
5353 int i;
5354
5355 if (REG_MULTI)
5356 {
5357 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5358 line = reg_getline((linenr_T)0); /* relative to the cursor */
5359 reg_startpos = reg_mmatch->startpos;
5360 reg_endpos = reg_mmatch->endpos;
5361 }
5362 else
5363 {
5364 prog = (nfa_regprog_T *)reg_match->regprog;
5365 reg_startp = reg_match->startp;
5366 reg_endp = reg_match->endp;
5367 }
5368
5369 /* Be paranoid... */
5370 if (prog == NULL || line == NULL)
5371 {
5372 EMSG(_(e_null));
5373 goto theend;
5374 }
5375
5376 /* If the start column is past the maximum column: no need to try. */
5377 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5378 goto theend;
5379
5380 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5381 if (prog->regflags & RF_ICASE)
5382 ireg_ic = TRUE;
5383 else if (prog->regflags & RF_NOICASE)
5384 ireg_ic = FALSE;
5385
5386#ifdef FEAT_MBYTE
5387 /* If pattern contains "\Z" overrule value of ireg_icombine */
5388 if (prog->regflags & RF_ICOMBINE)
5389 ireg_icombine = TRUE;
5390#endif
5391
5392 regline = line;
5393 reglnum = 0; /* relative to line */
5394
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005395 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005396 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005397 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005398 nfa_listid = 1;
5399 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005400#ifdef DEBUG
5401 nfa_regengine.expr = prog->pattern;
5402#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005403
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005404 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005405 for (i = 0; i < nstate; ++i)
5406 {
5407 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005408 prog->state[i].lastlist[0] = 0;
5409 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 }
5411
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005412 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005413
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005414#ifdef DEBUG
5415 nfa_regengine.expr = NULL;
5416#endif
5417
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005418theend:
5419 return retval;
5420}
5421
5422/*
5423 * Compile a regular expression into internal code for the NFA matcher.
5424 * Returns the program in allocated space. Returns NULL for an error.
5425 */
5426 static regprog_T *
5427nfa_regcomp(expr, re_flags)
5428 char_u *expr;
5429 int re_flags;
5430{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005431 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005432 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005433 int *postfix;
5434
5435 if (expr == NULL)
5436 return NULL;
5437
5438#ifdef DEBUG
5439 nfa_regengine.expr = expr;
5440#endif
5441
5442 init_class_tab();
5443
5444 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5445 return NULL;
5446
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005447 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005448 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005449 postfix = re2post();
5450 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005451 {
5452 /* TODO: only give this error for debugging? */
5453 if (post_ptr >= post_end)
5454 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005455 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005456 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005457
5458 /*
5459 * In order to build the NFA, we parse the input regexp twice:
5460 * 1. first pass to count size (so we can allocate space)
5461 * 2. second to emit code
5462 */
5463#ifdef ENABLE_LOG
5464 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005465 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005466
5467 if (f != NULL)
5468 {
5469 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5470 fclose(f);
5471 }
5472 }
5473#endif
5474
5475 /*
5476 * PASS 1
5477 * Count number of NFA states in "nstate". Do not build the NFA.
5478 */
5479 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005480
5481 /* Space for compiled regexp */
5482 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5483 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5484 if (prog == NULL)
5485 goto fail;
5486 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005487 state_ptr = prog->state;
5488
5489 /*
5490 * PASS 2
5491 * Build the NFA
5492 */
5493 prog->start = post2nfa(postfix, post_ptr, FALSE);
5494 if (prog->start == NULL)
5495 goto fail;
5496
5497 prog->regflags = regflags;
5498 prog->engine = &nfa_regengine;
5499 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005500 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005501 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005502 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503#ifdef ENABLE_LOG
5504 nfa_postfix_dump(expr, OK);
5505 nfa_dump(prog);
5506#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005507#ifdef FEAT_SYN_HL
5508 /* Remember whether this pattern has any \z specials in it. */
5509 prog->reghasz = re_has_z;
5510#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005511#ifdef DEBUG
5512 prog->pattern = vim_strsave(expr); /* memory will leak */
5513 nfa_regengine.expr = NULL;
5514#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515
5516out:
5517 vim_free(post_start);
5518 post_start = post_ptr = post_end = NULL;
5519 state_ptr = NULL;
5520 return (regprog_T *)prog;
5521
5522fail:
5523 vim_free(prog);
5524 prog = NULL;
5525#ifdef ENABLE_LOG
5526 nfa_postfix_dump(expr, FAIL);
5527#endif
5528#ifdef DEBUG
5529 nfa_regengine.expr = NULL;
5530#endif
5531 goto out;
5532}
5533
5534
5535/*
5536 * Match a regexp against a string.
5537 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5538 * Uses curbuf for line count and 'iskeyword'.
5539 *
5540 * Return TRUE if there is a match, FALSE if not.
5541 */
5542 static int
5543nfa_regexec(rmp, line, col)
5544 regmatch_T *rmp;
5545 char_u *line; /* string to match against */
5546 colnr_T col; /* column to start looking for match */
5547{
5548 reg_match = rmp;
5549 reg_mmatch = NULL;
5550 reg_maxline = 0;
5551 reg_line_lbr = FALSE;
5552 reg_buf = curbuf;
5553 reg_win = NULL;
5554 ireg_ic = rmp->rm_ic;
5555#ifdef FEAT_MBYTE
5556 ireg_icombine = FALSE;
5557#endif
5558 ireg_maxcol = 0;
5559 return (nfa_regexec_both(line, col) != 0);
5560}
5561
5562#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5563 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5564
5565static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5566
5567/*
5568 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5569 */
5570 static int
5571nfa_regexec_nl(rmp, line, col)
5572 regmatch_T *rmp;
5573 char_u *line; /* string to match against */
5574 colnr_T col; /* column to start looking for match */
5575{
5576 reg_match = rmp;
5577 reg_mmatch = NULL;
5578 reg_maxline = 0;
5579 reg_line_lbr = TRUE;
5580 reg_buf = curbuf;
5581 reg_win = NULL;
5582 ireg_ic = rmp->rm_ic;
5583#ifdef FEAT_MBYTE
5584 ireg_icombine = FALSE;
5585#endif
5586 ireg_maxcol = 0;
5587 return (nfa_regexec_both(line, col) != 0);
5588}
5589#endif
5590
5591
5592/*
5593 * Match a regexp against multiple lines.
5594 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5595 * Uses curbuf for line count and 'iskeyword'.
5596 *
5597 * Return zero if there is no match. Return number of lines contained in the
5598 * match otherwise.
5599 *
5600 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5601 *
5602 * ! Also NOTE : match may actually be in another line. e.g.:
5603 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5604 *
5605 * +-------------------------+
5606 * |a |
5607 * |b |
5608 * |c |
5609 * | |
5610 * +-------------------------+
5611 *
5612 * then nfa_regexec_multi() returns 3. while the original
5613 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5614 *
5615 * FIXME if this behavior is not compatible.
5616 */
5617 static long
5618nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5619 regmmatch_T *rmp;
5620 win_T *win; /* window in which to search or NULL */
5621 buf_T *buf; /* buffer in which to search */
5622 linenr_T lnum; /* nr of line to start looking for match */
5623 colnr_T col; /* column to start looking for match */
5624 proftime_T *tm UNUSED; /* timeout limit or NULL */
5625{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005626 reg_match = NULL;
5627 reg_mmatch = rmp;
5628 reg_buf = buf;
5629 reg_win = win;
5630 reg_firstlnum = lnum;
5631 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5632 reg_line_lbr = FALSE;
5633 ireg_ic = rmp->rmm_ic;
5634#ifdef FEAT_MBYTE
5635 ireg_icombine = FALSE;
5636#endif
5637 ireg_maxcol = rmp->rmm_maxcol;
5638
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005639 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005640}
5641
5642#ifdef DEBUG
5643# undef ENABLE_LOG
5644#endif