blob: ea74a8d4cc7134edbfe5e5ce206b23729101c88f [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 Moolenaardacd7de2013-06-04 18:28:48 +0200187 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200188
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200189 NFA_FIRST_NL = NFA_ANY + ADD_NL,
190 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
191
192 /* Character classes [:alnum:] etc */
193 NFA_CLASS_ALNUM,
194 NFA_CLASS_ALPHA,
195 NFA_CLASS_BLANK,
196 NFA_CLASS_CNTRL,
197 NFA_CLASS_DIGIT,
198 NFA_CLASS_GRAPH,
199 NFA_CLASS_LOWER,
200 NFA_CLASS_PRINT,
201 NFA_CLASS_PUNCT,
202 NFA_CLASS_SPACE,
203 NFA_CLASS_UPPER,
204 NFA_CLASS_XDIGIT,
205 NFA_CLASS_TAB,
206 NFA_CLASS_RETURN,
207 NFA_CLASS_BACKSPACE,
208 NFA_CLASS_ESCAPE
209};
210
211/* Keep in sync with classchars. */
212static int nfa_classcodes[] = {
213 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
214 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
215 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
216 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
217 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
218 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
219 NFA_UPPER, NFA_NUPPER
220};
221
222static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
223
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200224/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200225static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200226
Bram Moolenaar428e9872013-05-30 17:05:39 +0200227/* NFA regexp \1 .. \9 encountered. */
228static int nfa_has_backref;
229
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200230#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200231/* NFA regexp has \z( ), set zsubexpr. */
232static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200233#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200234
Bram Moolenaar963fee22013-05-26 21:47:28 +0200235/* Number of sub expressions actually being used during execution. 1 if only
236 * the whole match (subexpr 0) is used. */
237static int nfa_nsubexpr;
238
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200239static int *post_start; /* holds the postfix form of r.e. */
240static int *post_end;
241static int *post_ptr;
242
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200243static int nstate; /* Number of states in the NFA. Also used when
244 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200245static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaar307aa162013-06-02 16:34:21 +0200247/* If not NULL match must end at this position */
248static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200250/* listid is global, so that it increases on recursive calls to
251 * nfa_regmatch(), which means we don't have to clear the lastlist field of
252 * all the states. */
253static int nfa_listid;
254static int nfa_alt_listid;
255
256/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
257static int nfa_ll_index = 0;
258
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200259static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
261static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262static int nfa_regatom __ARGS((void));
263static int nfa_regpiece __ARGS((void));
264static int nfa_regconcat __ARGS((void));
265static int nfa_regbranch __ARGS((void));
266static int nfa_reg __ARGS((int paren));
267#ifdef DEBUG
268static void nfa_set_code __ARGS((int c));
269static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200270static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
271static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272static void nfa_dump __ARGS((nfa_regprog_T *prog));
273#endif
274static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200275static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
277static int check_char_class __ARGS((int class, int c));
278static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200279static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
280static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200281static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200282static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200283static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
284static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
285static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
286static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
287
288/* helper functions used when doing re2post() ... regatom() parsing */
289#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200290 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200291 return FAIL; \
292 *post_ptr++ = c; \
293 } while (0)
294
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295/*
296 * Initialize internal variables before NFA compilation.
297 * Return OK on success, FAIL otherwise.
298 */
299 static int
300nfa_regcomp_start(expr, re_flags)
301 char_u *expr;
302 int re_flags; /* see vim_regcomp() */
303{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200304 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200305 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306
307 nstate = 0;
308 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200309 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200310 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200312 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200313 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200314 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200315
316 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200317 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200318
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200319 post_start = (int *)lalloc(postfix_size, TRUE);
320 if (post_start == NULL)
321 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200323 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200325 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200327 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328 regcomp_start(expr, re_flags);
329
330 return OK;
331}
332
333/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200334 * Allocate more space for post_start. Called when
335 * running above the estimated number of states.
336 */
337 static int
338realloc_post_list()
339{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200340 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200341 int new_max = nstate_max + 1000;
342 int *new_start;
343 int *old_start;
344
345 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
346 if (new_start == NULL)
347 return FAIL;
348 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200349 old_start = post_start;
350 post_start = new_start;
351 post_ptr = new_start + (post_ptr - old_start);
352 post_end = post_start + new_max;
353 vim_free(old_start);
354 return OK;
355}
356
357/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200358 * Search between "start" and "end" and try to recognize a
359 * character class in expanded form. For example [0-9].
360 * On success, return the id the character class to be emitted.
361 * On failure, return 0 (=FAIL)
362 * Start points to the first char of the range, while end should point
363 * to the closing brace.
364 */
365 static int
366nfa_recognize_char_class(start, end, extra_newl)
367 char_u *start;
368 char_u *end;
369 int extra_newl;
370{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200371# define CLASS_not 0x80
372# define CLASS_af 0x40
373# define CLASS_AF 0x20
374# define CLASS_az 0x10
375# define CLASS_AZ 0x08
376# define CLASS_o7 0x04
377# define CLASS_o9 0x02
378# define CLASS_underscore 0x01
379
380 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200381 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200382 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200383
384 if (extra_newl == TRUE)
385 newl = TRUE;
386
387 if (*end != ']')
388 return FAIL;
389 p = start;
390 if (*p == '^')
391 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200392 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200393 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200394 }
395
396 while (p < end)
397 {
398 if (p + 2 < end && *(p + 1) == '-')
399 {
400 switch (*p)
401 {
402 case '0':
403 if (*(p + 2) == '9')
404 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200405 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200406 break;
407 }
408 else
409 if (*(p + 2) == '7')
410 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200411 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200412 break;
413 }
414 case 'a':
415 if (*(p + 2) == 'z')
416 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200417 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200418 break;
419 }
420 else
421 if (*(p + 2) == 'f')
422 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200423 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200424 break;
425 }
426 case 'A':
427 if (*(p + 2) == 'Z')
428 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200429 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200430 break;
431 }
432 else
433 if (*(p + 2) == 'F')
434 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200435 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200436 break;
437 }
438 /* FALLTHROUGH */
439 default:
440 return FAIL;
441 }
442 p += 3;
443 }
444 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
445 {
446 newl = TRUE;
447 p += 2;
448 }
449 else if (*p == '_')
450 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200451 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200452 p ++;
453 }
454 else if (*p == '\n')
455 {
456 newl = TRUE;
457 p ++;
458 }
459 else
460 return FAIL;
461 } /* while (p < end) */
462
463 if (p != end)
464 return FAIL;
465
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200466 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200467 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200468
469 switch (config)
470 {
471 case CLASS_o9:
472 return extra_newl + NFA_DIGIT;
473 case CLASS_not | CLASS_o9:
474 return extra_newl + NFA_NDIGIT;
475 case CLASS_af | CLASS_AF | CLASS_o9:
476 return extra_newl + NFA_HEX;
477 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
478 return extra_newl + NFA_NHEX;
479 case CLASS_o7:
480 return extra_newl + NFA_OCTAL;
481 case CLASS_not | CLASS_o7:
482 return extra_newl + NFA_NOCTAL;
483 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
484 return extra_newl + NFA_WORD;
485 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
486 return extra_newl + NFA_NWORD;
487 case CLASS_az | CLASS_AZ | CLASS_underscore:
488 return extra_newl + NFA_HEAD;
489 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
490 return extra_newl + NFA_NHEAD;
491 case CLASS_az | CLASS_AZ:
492 return extra_newl + NFA_ALPHA;
493 case CLASS_not | CLASS_az | CLASS_AZ:
494 return extra_newl + NFA_NALPHA;
495 case CLASS_az:
496 return extra_newl + NFA_LOWER;
497 case CLASS_not | CLASS_az:
498 return extra_newl + NFA_NLOWER;
499 case CLASS_AZ:
500 return extra_newl + NFA_UPPER;
501 case CLASS_not | CLASS_AZ:
502 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200503 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200504 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200505}
506
507/*
508 * Produce the bytes for equivalence class "c".
509 * Currently only handles latin1, latin9 and utf-8.
510 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
511 * equivalent to 'a OR b OR c'
512 *
513 * NOTE! When changing this function, also update reg_equi_class()
514 */
515 static int
516nfa_emit_equi_class(c, neg)
517 int c;
518 int neg;
519{
520 int first = TRUE;
521 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
522#define EMIT2(c) \
523 EMIT(c); \
524 if (neg == TRUE) { \
525 EMIT(NFA_NOT); \
526 } \
527 if (first == FALSE) \
528 EMIT(glue); \
529 else \
530 first = FALSE; \
531
532#ifdef FEAT_MBYTE
533 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
534 || STRCMP(p_enc, "iso-8859-15") == 0)
535#endif
536 {
537 switch (c)
538 {
539 case 'A': case '\300': case '\301': case '\302':
540 case '\303': case '\304': case '\305':
541 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
542 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
543 EMIT2('\305');
544 return OK;
545
546 case 'C': case '\307':
547 EMIT2('C'); EMIT2('\307');
548 return OK;
549
550 case 'E': case '\310': case '\311': case '\312': case '\313':
551 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
552 EMIT2('\312'); EMIT2('\313');
553 return OK;
554
555 case 'I': case '\314': case '\315': case '\316': case '\317':
556 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
557 EMIT2('\316'); EMIT2('\317');
558 return OK;
559
560 case 'N': case '\321':
561 EMIT2('N'); EMIT2('\321');
562 return OK;
563
564 case 'O': case '\322': case '\323': case '\324': case '\325':
565 case '\326':
566 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
567 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
568 return OK;
569
570 case 'U': case '\331': case '\332': case '\333': case '\334':
571 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
572 EMIT2('\333'); EMIT2('\334');
573 return OK;
574
575 case 'Y': case '\335':
576 EMIT2('Y'); EMIT2('\335');
577 return OK;
578
579 case 'a': case '\340': case '\341': case '\342':
580 case '\343': case '\344': case '\345':
581 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
582 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
583 EMIT2('\345');
584 return OK;
585
586 case 'c': case '\347':
587 EMIT2('c'); EMIT2('\347');
588 return OK;
589
590 case 'e': case '\350': case '\351': case '\352': case '\353':
591 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
592 EMIT2('\352'); EMIT2('\353');
593 return OK;
594
595 case 'i': case '\354': case '\355': case '\356': case '\357':
596 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
597 EMIT2('\356'); EMIT2('\357');
598 return OK;
599
600 case 'n': case '\361':
601 EMIT2('n'); EMIT2('\361');
602 return OK;
603
604 case 'o': case '\362': case '\363': case '\364': case '\365':
605 case '\366':
606 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
607 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
608 return OK;
609
610 case 'u': case '\371': case '\372': case '\373': case '\374':
611 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
612 EMIT2('\373'); EMIT2('\374');
613 return OK;
614
615 case 'y': case '\375': case '\377':
616 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
617 return OK;
618
619 default:
620 return FAIL;
621 }
622 }
623
624 EMIT(c);
625 return OK;
626#undef EMIT2
627}
628
629/*
630 * Code to parse regular expression.
631 *
632 * We try to reuse parsing functions in regexp.c to
633 * minimize surprise and keep the syntax consistent.
634 */
635
636/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200637 * Parse the lowest level.
638 *
639 * An atom can be one of a long list of items. Many atoms match one character
640 * in the text. It is often an ordinary character or a character class.
641 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
642 * is only for syntax highlighting.
643 *
644 * atom ::= ordinary-atom
645 * or \( pattern \)
646 * or \%( pattern \)
647 * or \z( pattern \)
648 */
649 static int
650nfa_regatom()
651{
652 int c;
653 int charclass;
654 int equiclass;
655 int collclass;
656 int got_coll_char;
657 char_u *p;
658 char_u *endp;
659#ifdef FEAT_MBYTE
660 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200661#endif
662 int extra = 0;
663 int first;
664 int emit_range;
665 int negated;
666 int result;
667 int startc = -1;
668 int endc = -1;
669 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200670 int glue; /* ID that will "glue" nodes together */
671
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200672 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200673 switch (c)
674 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200675 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200676 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
677
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200678 case Magic('^'):
679 EMIT(NFA_BOL);
680 break;
681
682 case Magic('$'):
683 EMIT(NFA_EOL);
684#if defined(FEAT_SYN_HL) || defined(PROTO)
685 had_eol = TRUE;
686#endif
687 break;
688
689 case Magic('<'):
690 EMIT(NFA_BOW);
691 break;
692
693 case Magic('>'):
694 EMIT(NFA_EOW);
695 break;
696
697 case Magic('_'):
698 c = no_Magic(getchr());
699 if (c == '^') /* "\_^" is start-of-line */
700 {
701 EMIT(NFA_BOL);
702 break;
703 }
704 if (c == '$') /* "\_$" is end-of-line */
705 {
706 EMIT(NFA_EOL);
707#if defined(FEAT_SYN_HL) || defined(PROTO)
708 had_eol = TRUE;
709#endif
710 break;
711 }
712
713 extra = ADD_NL;
714
715 /* "\_[" is collection plus newline */
716 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200717 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200718
719 /* "\_x" is character class plus newline */
720 /*FALLTHROUGH*/
721
722 /*
723 * Character classes.
724 */
725 case Magic('.'):
726 case Magic('i'):
727 case Magic('I'):
728 case Magic('k'):
729 case Magic('K'):
730 case Magic('f'):
731 case Magic('F'):
732 case Magic('p'):
733 case Magic('P'):
734 case Magic('s'):
735 case Magic('S'):
736 case Magic('d'):
737 case Magic('D'):
738 case Magic('x'):
739 case Magic('X'):
740 case Magic('o'):
741 case Magic('O'):
742 case Magic('w'):
743 case Magic('W'):
744 case Magic('h'):
745 case Magic('H'):
746 case Magic('a'):
747 case Magic('A'):
748 case Magic('l'):
749 case Magic('L'):
750 case Magic('u'):
751 case Magic('U'):
752 p = vim_strchr(classchars, no_Magic(c));
753 if (p == NULL)
754 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200755 EMSGN("INTERNAL: Unknown character class char: %ld", c);
756 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200757 }
758#ifdef FEAT_MBYTE
759 /* When '.' is followed by a composing char ignore the dot, so that
760 * the composing char is matched here. */
761 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
762 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200763 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200764 c = getchr();
765 goto nfa_do_multibyte;
766 }
767#endif
768 EMIT(nfa_classcodes[p - classchars]);
769 if (extra == ADD_NL)
770 {
771 EMIT(NFA_NEWL);
772 EMIT(NFA_OR);
773 regflags |= RF_HASNL;
774 }
775 break;
776
777 case Magic('n'):
778 if (reg_string)
779 /* In a string "\n" matches a newline character. */
780 EMIT(NL);
781 else
782 {
783 /* In buffer text "\n" matches the end of a line. */
784 EMIT(NFA_NEWL);
785 regflags |= RF_HASNL;
786 }
787 break;
788
789 case Magic('('):
790 if (nfa_reg(REG_PAREN) == FAIL)
791 return FAIL; /* cascaded error */
792 break;
793
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200794 case Magic('|'):
795 case Magic('&'):
796 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200797 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200798 return FAIL;
799
800 case Magic('='):
801 case Magic('?'):
802 case Magic('+'):
803 case Magic('@'):
804 case Magic('*'):
805 case Magic('{'):
806 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +0200807 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 return FAIL;
809
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200810 case Magic('~'):
811 {
812 char_u *lp;
813
814 /* Previous substitute pattern.
815 * Generated as "\%(pattern\)". */
816 if (reg_prev_sub == NULL)
817 {
818 EMSG(_(e_nopresub));
819 return FAIL;
820 }
821 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
822 {
823 EMIT(PTR2CHAR(lp));
824 if (lp != reg_prev_sub)
825 EMIT(NFA_CONCAT);
826 }
827 EMIT(NFA_NOPEN);
828 break;
829 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200830
Bram Moolenaar428e9872013-05-30 17:05:39 +0200831 case Magic('1'):
832 case Magic('2'):
833 case Magic('3'):
834 case Magic('4'):
835 case Magic('5'):
836 case Magic('6'):
837 case Magic('7'):
838 case Magic('8'):
839 case Magic('9'):
840 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
841 nfa_has_backref = TRUE;
842 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200843
844 case Magic('z'):
845 c = no_Magic(getchr());
846 switch (c)
847 {
848 case 's':
849 EMIT(NFA_ZSTART);
850 break;
851 case 'e':
852 EMIT(NFA_ZEND);
853 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200854 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200855#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200856 case '1':
857 case '2':
858 case '3':
859 case '4':
860 case '5':
861 case '6':
862 case '7':
863 case '8':
864 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200865 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200866 if (reg_do_extmatch != REX_USE)
867 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200868 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
869 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +0200870 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200871 re_has_z = REX_USE;
872 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200873 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200874 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200875 if (reg_do_extmatch != REX_SET)
876 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200877 if (nfa_reg(REG_ZPAREN) == FAIL)
878 return FAIL; /* cascaded error */
879 re_has_z = REX_SET;
880 break;
881#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200882 default:
Bram Moolenaarba404472013-05-19 22:31:18 +0200883 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 no_Magic(c));
885 return FAIL;
886 }
887 break;
888
889 case Magic('%'):
890 c = no_Magic(getchr());
891 switch (c)
892 {
893 /* () without a back reference */
894 case '(':
895 if (nfa_reg(REG_NPAREN) == FAIL)
896 return FAIL;
897 EMIT(NFA_NOPEN);
898 break;
899
900 case 'd': /* %d123 decimal */
901 case 'o': /* %o123 octal */
902 case 'x': /* %xab hex 2 */
903 case 'u': /* %uabcd hex 4 */
904 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200905 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200906 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200907
Bram Moolenaar47196582013-05-25 22:04:23 +0200908 switch (c)
909 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200910 case 'd': nr = getdecchrs(); break;
911 case 'o': nr = getoctchrs(); break;
912 case 'x': nr = gethexchrs(2); break;
913 case 'u': nr = gethexchrs(4); break;
914 case 'U': nr = gethexchrs(8); break;
915 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200916 }
917
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200918 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200919 EMSG2_RET_FAIL(
920 _("E678: Invalid character after %s%%[dxouU]"),
921 reg_magic == MAGIC_ALL);
922 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200923 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200924 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200925 break;
926
927 /* Catch \%^ and \%$ regardless of where they appear in the
928 * pattern -- regardless of whether or not it makes sense. */
929 case '^':
930 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200931 break;
932
933 case '$':
934 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200935 break;
936
937 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200938 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200939 break;
940
941 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200942 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200943 break;
944
945 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200946 {
947 int n;
948
949 /* \%[abc] */
950 for (n = 0; (c = getchr()) != ']'; ++n)
951 {
952 if (c == NUL)
953 EMSG2_RET_FAIL(_(e_missing_sb),
954 reg_magic == MAGIC_ALL);
955 EMIT(c);
956 }
Bram Moolenaar2976c022013-06-05 21:30:37 +0200957 if (n == 0)
958 EMSG2_RET_FAIL(_(e_empty_sb),
959 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200960 EMIT(NFA_OPT_CHARS);
961 EMIT(n);
962 break;
963 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200964
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200966 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200967 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200968 int cmp = c;
969
970 if (c == '<' || c == '>')
971 c = getchr();
972 while (VIM_ISDIGIT(c))
973 {
974 n = n * 10 + (c - '0');
975 c = getchr();
976 }
977 if (c == 'l' || c == 'c' || c == 'v')
978 {
Bram Moolenaar423532e2013-05-29 21:14:42 +0200979 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200980 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200981 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200982 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200983 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200984 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200985 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200986 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200987 else
Bram Moolenaar044aa292013-06-04 21:27:38 +0200988 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200989 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200990 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200991 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200992 break;
993 }
Bram Moolenaar044aa292013-06-04 21:27:38 +0200994 else if (c == '\'' && n == 0)
995 {
996 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200997 EMIT(cmp == '<' ? NFA_MARK_LT :
998 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200999 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001000 break;
1001 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001002 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001003 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1004 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001005 return FAIL;
1006 }
1007 break;
1008
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001009 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001010collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001011 /*
1012 * Glue is emitted between several atoms from the [].
1013 * It is either NFA_OR, or NFA_CONCAT.
1014 *
1015 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1016 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1017 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1018 * notation)
1019 *
1020 */
1021
1022
1023/* Emit negation atoms, if needed.
1024 * The CONCAT below merges the NOT with the previous node. */
1025#define TRY_NEG() \
1026 if (negated == TRUE) \
1027 { \
1028 EMIT(NFA_NOT); \
1029 }
1030
1031/* Emit glue between important nodes : CONCAT or OR. */
1032#define EMIT_GLUE() \
1033 if (first == FALSE) \
1034 EMIT(glue); \
1035 else \
1036 first = FALSE;
1037
1038 p = regparse;
1039 endp = skip_anyof(p);
1040 if (*endp == ']')
1041 {
1042 /*
1043 * Try to reverse engineer character classes. For example,
1044 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1045 * and perform the necessary substitutions in the NFA.
1046 */
1047 result = nfa_recognize_char_class(regparse, endp,
1048 extra == ADD_NL);
1049 if (result != FAIL)
1050 {
1051 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1052 EMIT(result);
1053 else /* must be char class + newline */
1054 {
1055 EMIT(result - ADD_NL);
1056 EMIT(NFA_NEWL);
1057 EMIT(NFA_OR);
1058 }
1059 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001060 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001061 return OK;
1062 }
1063 /*
1064 * Failed to recognize a character class. Use the simple
1065 * version that turns [abc] into 'a' OR 'b' OR 'c'
1066 */
1067 startc = endc = oldstartc = -1;
1068 first = TRUE; /* Emitting first atom in this sequence? */
1069 negated = FALSE;
1070 glue = NFA_OR;
1071 if (*regparse == '^') /* negated range */
1072 {
1073 negated = TRUE;
1074 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001075 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001076 }
1077 if (*regparse == '-')
1078 {
1079 startc = '-';
1080 EMIT(startc);
1081 TRY_NEG();
1082 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001083 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001084 }
1085 /* Emit the OR branches for each character in the [] */
1086 emit_range = FALSE;
1087 while (regparse < endp)
1088 {
1089 oldstartc = startc;
1090 startc = -1;
1091 got_coll_char = FALSE;
1092 if (*regparse == '[')
1093 {
1094 /* Check for [: :], [= =], [. .] */
1095 equiclass = collclass = 0;
1096 charclass = get_char_class(&regparse);
1097 if (charclass == CLASS_NONE)
1098 {
1099 equiclass = get_equi_class(&regparse);
1100 if (equiclass == 0)
1101 collclass = get_coll_element(&regparse);
1102 }
1103
1104 /* Character class like [:alpha:] */
1105 if (charclass != CLASS_NONE)
1106 {
1107 switch (charclass)
1108 {
1109 case CLASS_ALNUM:
1110 EMIT(NFA_CLASS_ALNUM);
1111 break;
1112 case CLASS_ALPHA:
1113 EMIT(NFA_CLASS_ALPHA);
1114 break;
1115 case CLASS_BLANK:
1116 EMIT(NFA_CLASS_BLANK);
1117 break;
1118 case CLASS_CNTRL:
1119 EMIT(NFA_CLASS_CNTRL);
1120 break;
1121 case CLASS_DIGIT:
1122 EMIT(NFA_CLASS_DIGIT);
1123 break;
1124 case CLASS_GRAPH:
1125 EMIT(NFA_CLASS_GRAPH);
1126 break;
1127 case CLASS_LOWER:
1128 EMIT(NFA_CLASS_LOWER);
1129 break;
1130 case CLASS_PRINT:
1131 EMIT(NFA_CLASS_PRINT);
1132 break;
1133 case CLASS_PUNCT:
1134 EMIT(NFA_CLASS_PUNCT);
1135 break;
1136 case CLASS_SPACE:
1137 EMIT(NFA_CLASS_SPACE);
1138 break;
1139 case CLASS_UPPER:
1140 EMIT(NFA_CLASS_UPPER);
1141 break;
1142 case CLASS_XDIGIT:
1143 EMIT(NFA_CLASS_XDIGIT);
1144 break;
1145 case CLASS_TAB:
1146 EMIT(NFA_CLASS_TAB);
1147 break;
1148 case CLASS_RETURN:
1149 EMIT(NFA_CLASS_RETURN);
1150 break;
1151 case CLASS_BACKSPACE:
1152 EMIT(NFA_CLASS_BACKSPACE);
1153 break;
1154 case CLASS_ESCAPE:
1155 EMIT(NFA_CLASS_ESCAPE);
1156 break;
1157 }
1158 TRY_NEG();
1159 EMIT_GLUE();
1160 continue;
1161 }
1162 /* Try equivalence class [=a=] and the like */
1163 if (equiclass != 0)
1164 {
1165 result = nfa_emit_equi_class(equiclass, negated);
1166 if (result == FAIL)
1167 {
1168 /* should never happen */
1169 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1170 }
1171 EMIT_GLUE();
1172 continue;
1173 }
1174 /* Try collating class like [. .] */
1175 if (collclass != 0)
1176 {
1177 startc = collclass; /* allow [.a.]-x as a range */
1178 /* Will emit the proper atom at the end of the
1179 * while loop. */
1180 }
1181 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001182 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1183 * start character. */
1184 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001185 {
1186 emit_range = TRUE;
1187 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001188 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001189 continue; /* reading the end of the range */
1190 }
1191
1192 /* Now handle simple and escaped characters.
1193 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1194 * accepts "\t", "\e", etc., but only when the 'l' flag in
1195 * 'cpoptions' is not included.
1196 * Posix doesn't recognize backslash at all.
1197 */
1198 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001199 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001200 && regparse + 1 <= endp
1201 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001202 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 && vim_strchr(REGEXP_ABBR, regparse[1])
1204 != NULL)
1205 )
1206 )
1207 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001208 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001209
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001210 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001211 startc = reg_string ? NL : NFA_NEWL;
1212 else
1213 if (*regparse == 'd'
1214 || *regparse == 'o'
1215 || *regparse == 'x'
1216 || *regparse == 'u'
1217 || *regparse == 'U'
1218 )
1219 {
1220 /* TODO(RE) This needs more testing */
1221 startc = coll_get_char();
1222 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001223 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001224 }
1225 else
1226 {
1227 /* \r,\t,\e,\b */
1228 startc = backslash_trans(*regparse);
1229 }
1230 }
1231
1232 /* Normal printable char */
1233 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001234 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001235
1236 /* Previous char was '-', so this char is end of range. */
1237 if (emit_range)
1238 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001239 endc = startc;
1240 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 if (startc > endc)
1242 EMSG_RET_FAIL(_(e_invrange));
1243#ifdef FEAT_MBYTE
1244 if (has_mbyte && ((*mb_char2len)(startc) > 1
1245 || (*mb_char2len)(endc) > 1))
1246 {
1247 if (endc > startc + 256)
1248 EMSG_RET_FAIL(_(e_invrange));
1249 /* Emit the range. "startc" was already emitted, so
1250 * skip it. */
1251 for (c = startc + 1; c <= endc; c++)
1252 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001253 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254 TRY_NEG();
1255 EMIT_GLUE();
1256 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001257 }
1258 else
1259#endif
1260 {
1261#ifdef EBCDIC
1262 int alpha_only = FALSE;
1263
1264 /* for alphabetical range skip the gaps
1265 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1266 if (isalpha(startc) && isalpha(endc))
1267 alpha_only = TRUE;
1268#endif
1269 /* Emit the range. "startc" was already emitted, so
1270 * skip it. */
1271 for (c = startc + 1; c <= endc; c++)
1272#ifdef EBCDIC
1273 if (!alpha_only || isalpha(startc))
1274#endif
1275 {
1276 EMIT(c);
1277 TRY_NEG();
1278 EMIT_GLUE();
1279 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001280 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001281 emit_range = FALSE;
1282 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001283 }
1284 else
1285 {
1286 /*
1287 * This char (startc) is not part of a range. Just
1288 * emit it.
1289 *
1290 * Normally, simply emit startc. But if we get char
1291 * code=0 from a collating char, then replace it with
1292 * 0x0a.
1293 *
1294 * This is needed to completely mimic the behaviour of
1295 * the backtracking engine.
1296 */
1297 if (got_coll_char == TRUE && startc == 0)
1298 EMIT(0x0a);
1299 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001300 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001301 TRY_NEG();
1302 EMIT_GLUE();
1303 }
1304
Bram Moolenaar51a29832013-05-28 22:30:35 +02001305 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001306 } /* while (p < endp) */
1307
Bram Moolenaar51a29832013-05-28 22:30:35 +02001308 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001309 if (*regparse == '-') /* if last, '-' is just a char */
1310 {
1311 EMIT('-');
1312 TRY_NEG();
1313 EMIT_GLUE();
1314 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001315 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001316
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317 /* skip the trailing ] */
1318 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001319 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320 if (negated == TRUE)
1321 {
1322 /* Mark end of negated char range */
1323 EMIT(NFA_END_NEG_RANGE);
1324 EMIT(NFA_CONCAT);
1325 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001326
1327 /* \_[] also matches \n but it's not negated */
1328 if (extra == ADD_NL)
1329 {
1330 EMIT(reg_string ? NL : NFA_NEWL);
1331 EMIT(NFA_OR);
1332 }
1333
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001334 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001335 } /* if exists closing ] */
1336
1337 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001338 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001339 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001341 default:
1342 {
1343#ifdef FEAT_MBYTE
1344 int plen;
1345
1346nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001347 /* plen is length of current char with composing chars */
1348 if (enc_utf8 && ((*mb_char2len)(c)
1349 != (plen = (*mb_ptr2len)(old_regparse))
1350 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001351 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001352 int i = 0;
1353
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001354 /* A base character plus composing characters, or just one
1355 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001356 * This requires creating a separate atom as if enclosing
1357 * the characters in (), where NFA_COMPOSING is the ( and
1358 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359 * building the postfix form, not the NFA itself;
1360 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001361 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001362 for (;;)
1363 {
1364 EMIT(c);
1365 if (i > 0)
1366 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001367 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001368 break;
1369 c = utf_ptr2char(old_regparse + i);
1370 }
1371 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001372 regparse = old_regparse + plen;
1373 }
1374 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001375#endif
1376 {
1377 c = no_Magic(c);
1378 EMIT(c);
1379 }
1380 return OK;
1381 }
1382 }
1383
1384#undef TRY_NEG
1385#undef EMIT_GLUE
1386
1387 return OK;
1388}
1389
1390/*
1391 * Parse something followed by possible [*+=].
1392 *
1393 * A piece is an atom, possibly followed by a multi, an indication of how many
1394 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1395 * characters: "", "a", "aa", etc.
1396 *
1397 * piece ::= atom
1398 * or atom multi
1399 */
1400 static int
1401nfa_regpiece()
1402{
1403 int i;
1404 int op;
1405 int ret;
1406 long minval, maxval;
1407 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001408 parse_state_T old_state;
1409 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001410 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001411 int old_post_pos;
1412 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 int quest;
1414
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001415 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1416 * next. */
1417 save_parse_state(&old_state);
1418
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001419 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001420 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001421
1422 ret = nfa_regatom();
1423 if (ret == FAIL)
1424 return FAIL; /* cascaded error */
1425
1426 op = peekchr();
1427 if (re_multi_type(op) == NOT_MULTI)
1428 return OK;
1429
1430 skipchr();
1431 switch (op)
1432 {
1433 case Magic('*'):
1434 EMIT(NFA_STAR);
1435 break;
1436
1437 case Magic('+'):
1438 /*
1439 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1440 * first and only submatch would be "aaa". But the backtracking
1441 * engine interprets the plus as "try matching one more time", and
1442 * a* matches a second time at the end of the input, the empty
1443 * string.
1444 * The submatch will the empty string.
1445 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001446 * In order to be consistent with the old engine, we replace
1447 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001448 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001449 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 curchr = -1;
1451 if (nfa_regatom() == FAIL)
1452 return FAIL;
1453 EMIT(NFA_STAR);
1454 EMIT(NFA_CONCAT);
1455 skipchr(); /* skip the \+ */
1456 break;
1457
1458 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001459 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001460 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001461 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462 switch(op)
1463 {
1464 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001465 /* \@= */
1466 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001468 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001469 /* \@! */
1470 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001471 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001472 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001473 op = no_Magic(getchr());
1474 if (op == '=')
1475 /* \@<= */
1476 i = NFA_PREV_ATOM_JUST_BEFORE;
1477 else if (op == '!')
1478 /* \@<! */
1479 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1480 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001481 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001482 /* \@> */
1483 i = NFA_PREV_ATOM_LIKE_PATTERN;
1484 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001486 if (i == 0)
1487 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001488 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1489 return FAIL;
1490 }
1491 EMIT(i);
1492 if (i == NFA_PREV_ATOM_JUST_BEFORE
1493 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1494 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 break;
1496
1497 case Magic('?'):
1498 case Magic('='):
1499 EMIT(NFA_QUEST);
1500 break;
1501
1502 case Magic('{'):
1503 /* a{2,5} will expand to 'aaa?a?a?'
1504 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1505 * version of '?'
1506 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1507 * parenthesis have the same id
1508 */
1509
1510 greedy = TRUE;
1511 c2 = peekchr();
1512 if (c2 == '-' || c2 == Magic('-'))
1513 {
1514 skipchr();
1515 greedy = FALSE;
1516 }
1517 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001519
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1521 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001522 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001523 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001524 if (greedy)
1525 /* \{}, \{0,} */
1526 EMIT(NFA_STAR);
1527 else
1528 /* \{-}, \{-0,} */
1529 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 break;
1531 }
1532
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001533 /* Special case: x{0} or x{-0} */
1534 if (maxval == 0)
1535 {
1536 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001537 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001538 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1539 EMIT(NFA_SKIP_CHAR);
1540 return OK;
1541 }
1542
1543 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001544 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001545 /* Save parse state after the repeated atom and the \{} */
1546 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001547
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001548 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1549 for (i = 0; i < maxval; i++)
1550 {
1551 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001552 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001553 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001554 if (nfa_regatom() == FAIL)
1555 return FAIL;
1556 /* after "minval" times, atoms are optional */
1557 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001558 {
1559 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001560 {
1561 if (greedy)
1562 EMIT(NFA_STAR);
1563 else
1564 EMIT(NFA_STAR_NONGREEDY);
1565 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001566 else
1567 EMIT(quest);
1568 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001569 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001570 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001571 if (i + 1 > minval && maxval == MAX_LIMIT)
1572 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001573 }
1574
1575 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001576 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001577 curchr = -1;
1578
1579 break;
1580
1581
1582 default:
1583 break;
1584 } /* end switch */
1585
1586 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001587 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001588 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001589
1590 return OK;
1591}
1592
1593/*
1594 * Parse one or more pieces, concatenated. It matches a match for the
1595 * first piece, followed by a match for the second piece, etc. Example:
1596 * "f[0-9]b", first matches "f", then a digit and then "b".
1597 *
1598 * concat ::= piece
1599 * or piece piece
1600 * or piece piece piece
1601 * etc.
1602 */
1603 static int
1604nfa_regconcat()
1605{
1606 int cont = TRUE;
1607 int first = TRUE;
1608
1609 while (cont)
1610 {
1611 switch (peekchr())
1612 {
1613 case NUL:
1614 case Magic('|'):
1615 case Magic('&'):
1616 case Magic(')'):
1617 cont = FALSE;
1618 break;
1619
1620 case Magic('Z'):
1621#ifdef FEAT_MBYTE
1622 regflags |= RF_ICOMBINE;
1623#endif
1624 skipchr_keepstart();
1625 break;
1626 case Magic('c'):
1627 regflags |= RF_ICASE;
1628 skipchr_keepstart();
1629 break;
1630 case Magic('C'):
1631 regflags |= RF_NOICASE;
1632 skipchr_keepstart();
1633 break;
1634 case Magic('v'):
1635 reg_magic = MAGIC_ALL;
1636 skipchr_keepstart();
1637 curchr = -1;
1638 break;
1639 case Magic('m'):
1640 reg_magic = MAGIC_ON;
1641 skipchr_keepstart();
1642 curchr = -1;
1643 break;
1644 case Magic('M'):
1645 reg_magic = MAGIC_OFF;
1646 skipchr_keepstart();
1647 curchr = -1;
1648 break;
1649 case Magic('V'):
1650 reg_magic = MAGIC_NONE;
1651 skipchr_keepstart();
1652 curchr = -1;
1653 break;
1654
1655 default:
1656 if (nfa_regpiece() == FAIL)
1657 return FAIL;
1658 if (first == FALSE)
1659 EMIT(NFA_CONCAT);
1660 else
1661 first = FALSE;
1662 break;
1663 }
1664 }
1665
1666 return OK;
1667}
1668
1669/*
1670 * Parse a branch, one or more concats, separated by "\&". It matches the
1671 * last concat, but only if all the preceding concats also match at the same
1672 * position. Examples:
1673 * "foobeep\&..." matches "foo" in "foobeep".
1674 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1675 *
1676 * branch ::= concat
1677 * or concat \& concat
1678 * or concat \& concat \& concat
1679 * etc.
1680 */
1681 static int
1682nfa_regbranch()
1683{
1684 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001685 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001686
Bram Moolenaar16299b52013-05-30 18:45:23 +02001687 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001688
1689 /* First branch, possibly the only one */
1690 if (nfa_regconcat() == FAIL)
1691 return FAIL;
1692
1693 ch = peekchr();
1694 /* Try next concats */
1695 while (ch == Magic('&'))
1696 {
1697 skipchr();
1698 EMIT(NFA_NOPEN);
1699 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001700 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001701 if (nfa_regconcat() == FAIL)
1702 return FAIL;
1703 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001704 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001705 EMIT(NFA_SKIP_CHAR);
1706 EMIT(NFA_CONCAT);
1707 ch = peekchr();
1708 }
1709
1710 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001711 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001712 EMIT(NFA_SKIP_CHAR);
1713
1714 return OK;
1715}
1716
1717/*
1718 * Parse a pattern, one or more branches, separated by "\|". It matches
1719 * anything that matches one of the branches. Example: "foo\|beep" matches
1720 * "foo" and matches "beep". If more than one branch matches, the first one
1721 * is used.
1722 *
1723 * pattern ::= branch
1724 * or branch \| branch
1725 * or branch \| branch \| branch
1726 * etc.
1727 */
1728 static int
1729nfa_reg(paren)
1730 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1731{
1732 int parno = 0;
1733
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001734 if (paren == REG_PAREN)
1735 {
1736 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001737 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 parno = regnpar++;
1739 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001740#ifdef FEAT_SYN_HL
1741 else if (paren == REG_ZPAREN)
1742 {
1743 /* Make a ZOPEN node. */
1744 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001745 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001746 parno = regnzpar++;
1747 }
1748#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001749
1750 if (nfa_regbranch() == FAIL)
1751 return FAIL; /* cascaded error */
1752
1753 while (peekchr() == Magic('|'))
1754 {
1755 skipchr();
1756 if (nfa_regbranch() == FAIL)
1757 return FAIL; /* cascaded error */
1758 EMIT(NFA_OR);
1759 }
1760
1761 /* Check for proper termination. */
1762 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1763 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001764 if (paren == REG_NPAREN)
1765 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1766 else
1767 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1768 }
1769 else if (paren == REG_NOPAREN && peekchr() != NUL)
1770 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001771 if (peekchr() == Magic(')'))
1772 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1773 else
1774 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1775 }
1776 /*
1777 * Here we set the flag allowing back references to this set of
1778 * parentheses.
1779 */
1780 if (paren == REG_PAREN)
1781 {
1782 had_endbrace[parno] = TRUE; /* have seen the close paren */
1783 EMIT(NFA_MOPEN + parno);
1784 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001785#ifdef FEAT_SYN_HL
1786 else if (paren == REG_ZPAREN)
1787 EMIT(NFA_ZOPEN + parno);
1788#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789
1790 return OK;
1791}
1792
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001793#ifdef DEBUG
1794static char_u code[50];
1795
1796 static void
1797nfa_set_code(c)
1798 int c;
1799{
1800 int addnl = FALSE;
1801
1802 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1803 {
1804 addnl = TRUE;
1805 c -= ADD_NL;
1806 }
1807
1808 STRCPY(code, "");
1809 switch (c)
1810 {
1811 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1812 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1813 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1814 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1815 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1816 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1817
Bram Moolenaar5714b802013-05-28 22:03:20 +02001818 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1819 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1820 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1821 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1822 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1823 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1824 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1825 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1826 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001827#ifdef FEAT_SYN_HL
1828 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1829 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1830 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1831 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1832 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1833 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1834 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1835 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1836 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1837#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001838 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1839
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001840 case NFA_PREV_ATOM_NO_WIDTH:
1841 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001842 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1843 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001844 case NFA_PREV_ATOM_JUST_BEFORE:
1845 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1846 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1847 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001848 case NFA_PREV_ATOM_LIKE_PATTERN:
1849 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
1850
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001851 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1852 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001853 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001854 case NFA_START_INVISIBLE_BEFORE:
1855 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001856 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001857 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001858 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001860 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1861 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001862 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001863
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001864 case NFA_MOPEN:
1865 case NFA_MOPEN1:
1866 case NFA_MOPEN2:
1867 case NFA_MOPEN3:
1868 case NFA_MOPEN4:
1869 case NFA_MOPEN5:
1870 case NFA_MOPEN6:
1871 case NFA_MOPEN7:
1872 case NFA_MOPEN8:
1873 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 STRCPY(code, "NFA_MOPEN(x)");
1875 code[10] = c - NFA_MOPEN + '0';
1876 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001877 case NFA_MCLOSE:
1878 case NFA_MCLOSE1:
1879 case NFA_MCLOSE2:
1880 case NFA_MCLOSE3:
1881 case NFA_MCLOSE4:
1882 case NFA_MCLOSE5:
1883 case NFA_MCLOSE6:
1884 case NFA_MCLOSE7:
1885 case NFA_MCLOSE8:
1886 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887 STRCPY(code, "NFA_MCLOSE(x)");
1888 code[11] = c - NFA_MCLOSE + '0';
1889 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001890#ifdef FEAT_SYN_HL
1891 case NFA_ZOPEN:
1892 case NFA_ZOPEN1:
1893 case NFA_ZOPEN2:
1894 case NFA_ZOPEN3:
1895 case NFA_ZOPEN4:
1896 case NFA_ZOPEN5:
1897 case NFA_ZOPEN6:
1898 case NFA_ZOPEN7:
1899 case NFA_ZOPEN8:
1900 case NFA_ZOPEN9:
1901 STRCPY(code, "NFA_ZOPEN(x)");
1902 code[10] = c - NFA_ZOPEN + '0';
1903 break;
1904 case NFA_ZCLOSE:
1905 case NFA_ZCLOSE1:
1906 case NFA_ZCLOSE2:
1907 case NFA_ZCLOSE3:
1908 case NFA_ZCLOSE4:
1909 case NFA_ZCLOSE5:
1910 case NFA_ZCLOSE6:
1911 case NFA_ZCLOSE7:
1912 case NFA_ZCLOSE8:
1913 case NFA_ZCLOSE9:
1914 STRCPY(code, "NFA_ZCLOSE(x)");
1915 code[11] = c - NFA_ZCLOSE + '0';
1916 break;
1917#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001918 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1919 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1920 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1921 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001922 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1923 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001924 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1925 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1926 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1927 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1928 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1929 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1930 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1931 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1932 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1933 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1934 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1935 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1936 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
1937 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
1938
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001940 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1941 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1942 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1944 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1945 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001946 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1947 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1948 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1949 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1950 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1951 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1952 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1953 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1954 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1955 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1956 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1957 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1958 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1959 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1960 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1961 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1962 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1963
1964 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1965 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1966 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1967 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1968 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1969 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1970 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1971 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1972 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1973 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1974 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1975 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1976 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1977 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1978 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1979 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1980 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1981 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1982 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1983 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1984 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1985 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1986 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1987 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1988 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1989 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1990 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1991
1992 default:
1993 STRCPY(code, "CHAR(x)");
1994 code[5] = c;
1995 }
1996
1997 if (addnl == TRUE)
1998 STRCAT(code, " + NEWLINE ");
1999
2000}
2001
2002#ifdef ENABLE_LOG
2003static FILE *log_fd;
2004
2005/*
2006 * Print the postfix notation of the current regexp.
2007 */
2008 static void
2009nfa_postfix_dump(expr, retval)
2010 char_u *expr;
2011 int retval;
2012{
2013 int *p;
2014 FILE *f;
2015
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002016 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002017 if (f != NULL)
2018 {
2019 fprintf(f, "\n-------------------------\n");
2020 if (retval == FAIL)
2021 fprintf(f, ">>> NFA engine failed ... \n");
2022 else if (retval == OK)
2023 fprintf(f, ">>> NFA engine succeeded !\n");
2024 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002025 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002026 {
2027 nfa_set_code(*p);
2028 fprintf(f, "%s, ", code);
2029 }
2030 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002031 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002032 fprintf(f, "%d ", *p);
2033 fprintf(f, "\n\n");
2034 fclose(f);
2035 }
2036}
2037
2038/*
2039 * Print the NFA starting with a root node "state".
2040 */
2041 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002042nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 FILE *debugf;
2044 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002045{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002046 garray_T indent;
2047
2048 ga_init2(&indent, 1, 64);
2049 ga_append(&indent, '\0');
2050 nfa_print_state2(debugf, state, &indent);
2051 ga_clear(&indent);
2052}
2053
2054 static void
2055nfa_print_state2(debugf, state, indent)
2056 FILE *debugf;
2057 nfa_state_T *state;
2058 garray_T *indent;
2059{
2060 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061
2062 if (state == NULL)
2063 return;
2064
2065 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002066
2067 /* Output indent */
2068 p = (char_u *)indent->ga_data;
2069 if (indent->ga_len >= 3)
2070 {
2071 int last = indent->ga_len - 3;
2072 char_u save[2];
2073
2074 STRNCPY(save, &p[last], 2);
2075 STRNCPY(&p[last], "+-", 2);
2076 fprintf(debugf, " %s", p);
2077 STRNCPY(&p[last], save, 2);
2078 }
2079 else
2080 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081
2082 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002083 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2084 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085 if (state->id < 0)
2086 return;
2087
2088 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002089
2090 /* grow indent for state->out */
2091 indent->ga_len -= 1;
2092 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002093 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002094 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002095 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002096 ga_append(indent, '\0');
2097
2098 nfa_print_state2(debugf, state->out, indent);
2099
2100 /* replace last part of indent for state->out1 */
2101 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002102 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002103 ga_append(indent, '\0');
2104
2105 nfa_print_state2(debugf, state->out1, indent);
2106
2107 /* shrink indent */
2108 indent->ga_len -= 3;
2109 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002110}
2111
2112/*
2113 * Print the NFA state machine.
2114 */
2115 static void
2116nfa_dump(prog)
2117 nfa_regprog_T *prog;
2118{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002119 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002120
2121 if (debugf != NULL)
2122 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002123 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002124 fclose(debugf);
2125 }
2126}
2127#endif /* ENABLE_LOG */
2128#endif /* DEBUG */
2129
2130/*
2131 * Parse r.e. @expr and convert it into postfix form.
2132 * Return the postfix string on success, NULL otherwise.
2133 */
2134 static int *
2135re2post()
2136{
2137 if (nfa_reg(REG_NOPAREN) == FAIL)
2138 return NULL;
2139 EMIT(NFA_MOPEN);
2140 return post_start;
2141}
2142
2143/* NB. Some of the code below is inspired by Russ's. */
2144
2145/*
2146 * Represents an NFA state plus zero or one or two arrows exiting.
2147 * if c == MATCH, no arrows out; matching state.
2148 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2149 * If c < 256, labeled arrow with character c to out.
2150 */
2151
2152static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2153
2154/*
2155 * Allocate and initialize nfa_state_T.
2156 */
2157 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002158alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002159 int c;
2160 nfa_state_T *out;
2161 nfa_state_T *out1;
2162{
2163 nfa_state_T *s;
2164
2165 if (istate >= nstate)
2166 return NULL;
2167
2168 s = &state_ptr[istate++];
2169
2170 s->c = c;
2171 s->out = out;
2172 s->out1 = out1;
2173
2174 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002175 s->lastlist[0] = 0;
2176 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002177 s->negated = FALSE;
2178
2179 return s;
2180}
2181
2182/*
2183 * A partially built NFA without the matching state filled in.
2184 * Frag_T.start points at the start state.
2185 * Frag_T.out is a list of places that need to be set to the
2186 * next state for this fragment.
2187 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002188
2189/* Since the out pointers in the list are always
2190 * uninitialized, we use the pointers themselves
2191 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002192typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002193union Ptrlist
2194{
2195 Ptrlist *next;
2196 nfa_state_T *s;
2197};
2198
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002199struct Frag
2200{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002201 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002202 Ptrlist *out;
2203};
2204typedef struct Frag Frag_T;
2205
2206static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2207static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2208static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2209static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2210static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2211static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2212
2213/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002214 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002215 */
2216 static Frag_T
2217frag(start, out)
2218 nfa_state_T *start;
2219 Ptrlist *out;
2220{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002221 Frag_T n;
2222
2223 n.start = start;
2224 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002225 return n;
2226}
2227
2228/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229 * Create singleton list containing just outp.
2230 */
2231 static Ptrlist *
2232list1(outp)
2233 nfa_state_T **outp;
2234{
2235 Ptrlist *l;
2236
2237 l = (Ptrlist *)outp;
2238 l->next = NULL;
2239 return l;
2240}
2241
2242/*
2243 * Patch the list of states at out to point to start.
2244 */
2245 static void
2246patch(l, s)
2247 Ptrlist *l;
2248 nfa_state_T *s;
2249{
2250 Ptrlist *next;
2251
2252 for (; l; l = next)
2253 {
2254 next = l->next;
2255 l->s = s;
2256 }
2257}
2258
2259
2260/*
2261 * Join the two lists l1 and l2, returning the combination.
2262 */
2263 static Ptrlist *
2264append(l1, l2)
2265 Ptrlist *l1;
2266 Ptrlist *l2;
2267{
2268 Ptrlist *oldl1;
2269
2270 oldl1 = l1;
2271 while (l1->next)
2272 l1 = l1->next;
2273 l1->next = l2;
2274 return oldl1;
2275}
2276
2277/*
2278 * Stack used for transforming postfix form into NFA.
2279 */
2280static Frag_T empty;
2281
2282 static void
2283st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002284 int *postfix UNUSED;
2285 int *end UNUSED;
2286 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002288#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 FILE *df;
2290 int *p2;
2291
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002292 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293 if (df)
2294 {
2295 fprintf(df, "Error popping the stack!\n");
2296#ifdef DEBUG
2297 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2298#endif
2299 fprintf(df, "Postfix form is: ");
2300#ifdef DEBUG
2301 for (p2 = postfix; p2 < end; p2++)
2302 {
2303 nfa_set_code(*p2);
2304 fprintf(df, "%s, ", code);
2305 }
2306 nfa_set_code(*p);
2307 fprintf(df, "\nCurrent position is: ");
2308 for (p2 = postfix; p2 <= p; p2 ++)
2309 {
2310 nfa_set_code(*p2);
2311 fprintf(df, "%s, ", code);
2312 }
2313#else
2314 for (p2 = postfix; p2 < end; p2++)
2315 {
2316 fprintf(df, "%d, ", *p2);
2317 }
2318 fprintf(df, "\nCurrent position is: ");
2319 for (p2 = postfix; p2 <= p; p2 ++)
2320 {
2321 fprintf(df, "%d, ", *p2);
2322 }
2323#endif
2324 fprintf(df, "\n--------------------------\n");
2325 fclose(df);
2326 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002327#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328 EMSG(_("E874: (NFA) Could not pop the stack !"));
2329}
2330
2331/*
2332 * Push an item onto the stack.
2333 */
2334 static void
2335st_push(s, p, stack_end)
2336 Frag_T s;
2337 Frag_T **p;
2338 Frag_T *stack_end;
2339{
2340 Frag_T *stackp = *p;
2341
2342 if (stackp >= stack_end)
2343 return;
2344 *stackp = s;
2345 *p = *p + 1;
2346}
2347
2348/*
2349 * Pop an item from the stack.
2350 */
2351 static Frag_T
2352st_pop(p, stack)
2353 Frag_T **p;
2354 Frag_T *stack;
2355{
2356 Frag_T *stackp;
2357
2358 *p = *p - 1;
2359 stackp = *p;
2360 if (stackp < stack)
2361 return empty;
2362 return **p;
2363}
2364
2365/*
2366 * Convert a postfix form into its equivalent NFA.
2367 * Return the NFA start state on success, NULL otherwise.
2368 */
2369 static nfa_state_T *
2370post2nfa(postfix, end, nfa_calc_size)
2371 int *postfix;
2372 int *end;
2373 int nfa_calc_size;
2374{
2375 int *p;
2376 int mopen;
2377 int mclose;
2378 Frag_T *stack = NULL;
2379 Frag_T *stackp = NULL;
2380 Frag_T *stack_end = NULL;
2381 Frag_T e1;
2382 Frag_T e2;
2383 Frag_T e;
2384 nfa_state_T *s;
2385 nfa_state_T *s1;
2386 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002387 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002388
2389 if (postfix == NULL)
2390 return NULL;
2391
Bram Moolenaar053bb602013-05-20 13:55:21 +02002392#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002393#define POP() st_pop(&stackp, stack); \
2394 if (stackp < stack) \
2395 { \
2396 st_error(postfix, end, p); \
2397 return NULL; \
2398 }
2399
2400 if (nfa_calc_size == FALSE)
2401 {
2402 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002403 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002404 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002405 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002406 }
2407
2408 for (p = postfix; p < end; ++p)
2409 {
2410 switch (*p)
2411 {
2412 case NFA_CONCAT:
2413 /* Catenation.
2414 * Pay attention: this operator does not exist
2415 * in the r.e. itself (it is implicit, really).
2416 * It is added when r.e. is translated to postfix
2417 * form in re2post().
2418 *
2419 * No new state added here. */
2420 if (nfa_calc_size == TRUE)
2421 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002422 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002423 break;
2424 }
2425 e2 = POP();
2426 e1 = POP();
2427 patch(e1.out, e2.start);
2428 PUSH(frag(e1.start, e2.out));
2429 break;
2430
2431 case NFA_NOT:
2432 /* Negation of a character */
2433 if (nfa_calc_size == TRUE)
2434 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002435 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436 break;
2437 }
2438 e1 = POP();
2439 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002440#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002441 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002443#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 PUSH(e1);
2445 break;
2446
2447 case NFA_OR:
2448 /* Alternation */
2449 if (nfa_calc_size == TRUE)
2450 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002451 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 break;
2453 }
2454 e2 = POP();
2455 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002456 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002457 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002458 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 PUSH(frag(s, append(e1.out, e2.out)));
2460 break;
2461
2462 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002463 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002464 if (nfa_calc_size == TRUE)
2465 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002466 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002467 break;
2468 }
2469 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002470 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002472 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 patch(e.out, s);
2474 PUSH(frag(s, list1(&s->out1)));
2475 break;
2476
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002477 case NFA_STAR_NONGREEDY:
2478 /* Zero or more, prefer zero */
2479 if (nfa_calc_size == TRUE)
2480 {
2481 nstate++;
2482 break;
2483 }
2484 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002485 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002486 if (s == NULL)
2487 goto theend;
2488 patch(e.out, s);
2489 PUSH(frag(s, list1(&s->out)));
2490 break;
2491
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002492 case NFA_QUEST:
2493 /* one or zero atoms=> greedy match */
2494 if (nfa_calc_size == TRUE)
2495 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002496 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002497 break;
2498 }
2499 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002500 s = alloc_state(NFA_SPLIT, e.start, NULL);
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(e.out, list1(&s->out1))));
2504 break;
2505
2506 case NFA_QUEST_NONGREEDY:
2507 /* zero or one atoms => non-greedy match */
2508 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, NULL, e.start);
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 PUSH(frag(s, append(e.out, list1(&s->out))));
2518 break;
2519
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520 case NFA_SKIP_CHAR:
2521 /* Symbol of 0-length, Used in a repetition
2522 * with max/min count of 0 */
2523 if (nfa_calc_size == TRUE)
2524 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002525 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002526 break;
2527 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002528 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002529 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002530 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002531 PUSH(frag(s, list1(&s->out)));
2532 break;
2533
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002534 case NFA_OPT_CHARS:
2535 {
2536 int n;
2537
2538 /* \%[abc] */
2539 n = *++p; /* get number of characters */
2540 if (nfa_calc_size == TRUE)
2541 {
2542 nstate += n;
2543 break;
2544 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002545 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002546 e1.out = NULL; /* stores list with out1's */
2547 s1 = NULL; /* previous NFA_SPLIT to connect to */
2548 while (n-- > 0)
2549 {
2550 e = POP(); /* get character */
2551 s = alloc_state(NFA_SPLIT, e.start, NULL);
2552 if (s == NULL)
2553 goto theend;
2554 if (e1.out == NULL)
2555 e1 = e;
2556 patch(e.out, s1);
2557 append(e1.out, list1(&s->out1));
2558 s1 = s;
2559 }
2560 PUSH(frag(s, e1.out));
2561 break;
2562 }
2563
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002564 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002565 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002566 case NFA_PREV_ATOM_JUST_BEFORE:
2567 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002568 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002569 {
2570 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2571 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2572 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2573 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002574 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
2575 int start_state = NFA_START_INVISIBLE;
2576 int end_state = NFA_END_INVISIBLE;
2577 int n = 0;
2578 nfa_state_T *zend;
2579 nfa_state_T *skip;
2580
2581 if (before)
2582 start_state = NFA_START_INVISIBLE_BEFORE;
2583 else if (pattern)
2584 {
2585 start_state = NFA_START_PATTERN;
2586 end_state = NFA_END_PATTERN;
2587 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002588
2589 if (before)
2590 n = *++p; /* get the count */
2591
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002592 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002593 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002594 * The \@<= operator: match for the preceding atom.
2595 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002596 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002597 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002598
2599 if (nfa_calc_size == TRUE)
2600 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002601 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002602 break;
2603 }
2604 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002605 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002606 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002607 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608
Bram Moolenaar87953742013-06-05 18:52:40 +02002609 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002610 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002611 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002612 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002613 {
2614 s->negated = TRUE;
2615 s1->negated = TRUE;
2616 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002617 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002618 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002619 if (pattern)
2620 {
2621 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2622 skip = alloc_state(NFA_SKIP, NULL, NULL);
2623 zend = alloc_state(NFA_ZEND, s1, NULL);
2624 s1->out= skip;
2625 patch(e.out, zend);
2626 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002627 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002628 else
2629 {
2630 patch(e.out, s1);
2631 PUSH(frag(s, list1(&s1->out)));
2632 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002633 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002634 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002636#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002637 case NFA_COMPOSING: /* char with composing char */
2638#if 0
2639 /* TODO */
2640 if (regflags & RF_ICOMBINE)
2641 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002642 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002643 }
2644#endif
2645 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002646#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002647
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002648 case NFA_MOPEN: /* \( \) Submatch */
2649 case NFA_MOPEN1:
2650 case NFA_MOPEN2:
2651 case NFA_MOPEN3:
2652 case NFA_MOPEN4:
2653 case NFA_MOPEN5:
2654 case NFA_MOPEN6:
2655 case NFA_MOPEN7:
2656 case NFA_MOPEN8:
2657 case NFA_MOPEN9:
2658#ifdef FEAT_SYN_HL
2659 case NFA_ZOPEN: /* \z( \) Submatch */
2660 case NFA_ZOPEN1:
2661 case NFA_ZOPEN2:
2662 case NFA_ZOPEN3:
2663 case NFA_ZOPEN4:
2664 case NFA_ZOPEN5:
2665 case NFA_ZOPEN6:
2666 case NFA_ZOPEN7:
2667 case NFA_ZOPEN8:
2668 case NFA_ZOPEN9:
2669#endif
2670 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002671 if (nfa_calc_size == TRUE)
2672 {
2673 nstate += 2;
2674 break;
2675 }
2676
2677 mopen = *p;
2678 switch (*p)
2679 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002680 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2681#ifdef FEAT_SYN_HL
2682 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2683 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2684 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2685 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2686 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2687 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2688 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2689 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2690 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2691 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2692#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002693#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002694 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002695#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002697 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002698 mclose = *p + NSUBEXP;
2699 break;
2700 }
2701
2702 /* Allow "NFA_MOPEN" as a valid postfix representation for
2703 * the empty regexp "". In this case, the NFA will be
2704 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2705 * empty groups of parenthesis, and empty mbyte chars */
2706 if (stackp == stack)
2707 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002708 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002709 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002710 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002711 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002713 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714 patch(list1(&s->out), s1);
2715 PUSH(frag(s, list1(&s1->out)));
2716 break;
2717 }
2718
2719 /* At least one node was emitted before NFA_MOPEN, so
2720 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2721 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002722 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002723 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002724 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725
Bram Moolenaar525666f2013-06-02 16:40:55 +02002726 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002727 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002728 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002729 patch(e.out, s1);
2730
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002731#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002732 if (mopen == NFA_COMPOSING)
2733 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002734 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002735#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002736
2737 PUSH(frag(s, list1(&s1->out)));
2738 break;
2739
Bram Moolenaar5714b802013-05-28 22:03:20 +02002740 case NFA_BACKREF1:
2741 case NFA_BACKREF2:
2742 case NFA_BACKREF3:
2743 case NFA_BACKREF4:
2744 case NFA_BACKREF5:
2745 case NFA_BACKREF6:
2746 case NFA_BACKREF7:
2747 case NFA_BACKREF8:
2748 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002749#ifdef FEAT_SYN_HL
2750 case NFA_ZREF1:
2751 case NFA_ZREF2:
2752 case NFA_ZREF3:
2753 case NFA_ZREF4:
2754 case NFA_ZREF5:
2755 case NFA_ZREF6:
2756 case NFA_ZREF7:
2757 case NFA_ZREF8:
2758 case NFA_ZREF9:
2759#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002760 if (nfa_calc_size == TRUE)
2761 {
2762 nstate += 2;
2763 break;
2764 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002765 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002766 if (s == NULL)
2767 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002768 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002769 if (s1 == NULL)
2770 goto theend;
2771 patch(list1(&s->out), s1);
2772 PUSH(frag(s, list1(&s1->out)));
2773 break;
2774
Bram Moolenaar423532e2013-05-29 21:14:42 +02002775 case NFA_LNUM:
2776 case NFA_LNUM_GT:
2777 case NFA_LNUM_LT:
2778 case NFA_VCOL:
2779 case NFA_VCOL_GT:
2780 case NFA_VCOL_LT:
2781 case NFA_COL:
2782 case NFA_COL_GT:
2783 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002784 case NFA_MARK:
2785 case NFA_MARK_GT:
2786 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002787 {
2788 int n = *++p; /* lnum, col or mark name */
2789
Bram Moolenaar423532e2013-05-29 21:14:42 +02002790 if (nfa_calc_size == TRUE)
2791 {
2792 nstate += 1;
2793 break;
2794 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002795 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002796 if (s == NULL)
2797 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002798 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002799 PUSH(frag(s, list1(&s->out)));
2800 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002801 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002802
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002803 case NFA_ZSTART:
2804 case NFA_ZEND:
2805 default:
2806 /* Operands */
2807 if (nfa_calc_size == TRUE)
2808 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002809 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002810 break;
2811 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002812 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002813 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002814 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002815 PUSH(frag(s, list1(&s->out)));
2816 break;
2817
2818 } /* switch(*p) */
2819
2820 } /* for(p = postfix; *p; ++p) */
2821
2822 if (nfa_calc_size == TRUE)
2823 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002824 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002825 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826 }
2827
2828 e = POP();
2829 if (stackp != stack)
2830 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2831
2832 if (istate >= nstate)
2833 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2834
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835 matchstate = &state_ptr[istate++]; /* the match state */
2836 matchstate->c = NFA_MATCH;
2837 matchstate->out = matchstate->out1 = NULL;
2838
2839 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002840 ret = e.start;
2841
2842theend:
2843 vim_free(stack);
2844 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845
2846#undef POP1
2847#undef PUSH1
2848#undef POP2
2849#undef PUSH2
2850#undef POP
2851#undef PUSH
2852}
2853
2854/****************************************************************
2855 * NFA execution code.
2856 ****************************************************************/
2857
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002858typedef struct
2859{
2860 int in_use; /* number of subexpr with useful info */
2861
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002862 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002863 union
2864 {
2865 struct multipos
2866 {
2867 lpos_T start;
2868 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002869 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002870 struct linepos
2871 {
2872 char_u *start;
2873 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002874 } line[NSUBEXP];
2875 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002876} regsub_T;
2877
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002878typedef struct
2879{
2880 regsub_T norm; /* \( .. \) matches */
2881#ifdef FEAT_SYN_HL
2882 regsub_T synt; /* \z( .. \) matches */
2883#endif
2884} regsubs_T;
2885
Bram Moolenaara2d95102013-06-04 14:23:05 +02002886/* nfa_pim_T stores a Postponed Invisible Match. */
2887typedef struct nfa_pim_S nfa_pim_T;
2888struct nfa_pim_S
2889{
2890 nfa_state_T *state;
2891 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2892 nfa_pim_T *pim; /* another PIM at the same position */
2893 regsubs_T subs; /* submatch info, only party used */
2894};
2895
2896/* Values for done in nfa_pim_T. */
2897#define NFA_PIM_TODO 0
2898#define NFA_PIM_MATCH 1
2899#define NFA_PIM_NOMATCH -1
2900
2901
Bram Moolenaar963fee22013-05-26 21:47:28 +02002902/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002903typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904{
2905 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002906 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002907 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002908 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002909} nfa_thread_T;
2910
Bram Moolenaar963fee22013-05-26 21:47:28 +02002911/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002912typedef struct
2913{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002914 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002915 int n; /* nr of states currently in "t" */
2916 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002917 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002918} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002919
Bram Moolenaar5714b802013-05-28 22:03:20 +02002920#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002921static void log_subsexpr __ARGS((regsubs_T *subs));
2922static void log_subexpr __ARGS((regsub_T *sub));
2923
2924 static void
2925log_subsexpr(subs)
2926 regsubs_T *subs;
2927{
2928 log_subexpr(&subs->norm);
2929# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02002930 if (nfa_has_zsubexpr)
2931 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002932# endif
2933}
2934
Bram Moolenaar5714b802013-05-28 22:03:20 +02002935 static void
2936log_subexpr(sub)
2937 regsub_T *sub;
2938{
2939 int j;
2940
2941 for (j = 0; j < sub->in_use; j++)
2942 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02002943 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002944 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002945 sub->list.multi[j].start.col,
2946 (int)sub->list.multi[j].start.lnum,
2947 sub->list.multi[j].end.col,
2948 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002949 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002950 {
2951 char *s = (char *)sub->list.line[j].start;
2952 char *e = (char *)sub->list.line[j].end;
2953
Bram Moolenaar87953742013-06-05 18:52:40 +02002954 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002955 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002956 s == NULL ? "NULL" : s,
2957 e == NULL ? "NULL" : e);
2958 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002959}
2960#endif
2961
Bram Moolenaar963fee22013-05-26 21:47:28 +02002962/* Used during execution: whether a match has been found. */
2963static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002964
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002965static void clear_sub __ARGS((regsub_T *sub));
2966static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2967static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002968static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002969static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02002970static 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 +02002971
2972 static void
2973clear_sub(sub)
2974 regsub_T *sub;
2975{
2976 if (REG_MULTI)
2977 /* Use 0xff to set lnum to -1 */
2978 vim_memset(sub->list.multi, 0xff,
2979 sizeof(struct multipos) * nfa_nsubexpr);
2980 else
2981 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2982 sub->in_use = 0;
2983}
2984
2985/*
2986 * Copy the submatches from "from" to "to".
2987 */
2988 static void
2989copy_sub(to, from)
2990 regsub_T *to;
2991 regsub_T *from;
2992{
2993 to->in_use = from->in_use;
2994 if (from->in_use > 0)
2995 {
2996 /* Copy the match start and end positions. */
2997 if (REG_MULTI)
2998 mch_memmove(&to->list.multi[0],
2999 &from->list.multi[0],
3000 sizeof(struct multipos) * from->in_use);
3001 else
3002 mch_memmove(&to->list.line[0],
3003 &from->list.line[0],
3004 sizeof(struct linepos) * from->in_use);
3005 }
3006}
3007
3008/*
3009 * Like copy_sub() but exclude the main match.
3010 */
3011 static void
3012copy_sub_off(to, from)
3013 regsub_T *to;
3014 regsub_T *from;
3015{
3016 if (to->in_use < from->in_use)
3017 to->in_use = from->in_use;
3018 if (from->in_use > 1)
3019 {
3020 /* Copy the match start and end positions. */
3021 if (REG_MULTI)
3022 mch_memmove(&to->list.multi[1],
3023 &from->list.multi[1],
3024 sizeof(struct multipos) * (from->in_use - 1));
3025 else
3026 mch_memmove(&to->list.line[1],
3027 &from->list.line[1],
3028 sizeof(struct linepos) * (from->in_use - 1));
3029 }
3030}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003031
Bram Moolenaar428e9872013-05-30 17:05:39 +02003032/*
3033 * Return TRUE if "sub1" and "sub2" have the same positions.
3034 */
3035 static int
3036sub_equal(sub1, sub2)
3037 regsub_T *sub1;
3038 regsub_T *sub2;
3039{
3040 int i;
3041 int todo;
3042 linenr_T s1, e1;
3043 linenr_T s2, e2;
3044 char_u *sp1, *ep1;
3045 char_u *sp2, *ep2;
3046
3047 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3048 if (REG_MULTI)
3049 {
3050 for (i = 0; i < todo; ++i)
3051 {
3052 if (i < sub1->in_use)
3053 {
3054 s1 = sub1->list.multi[i].start.lnum;
3055 e1 = sub1->list.multi[i].end.lnum;
3056 }
3057 else
3058 {
3059 s1 = 0;
3060 e1 = 0;
3061 }
3062 if (i < sub2->in_use)
3063 {
3064 s2 = sub2->list.multi[i].start.lnum;
3065 e2 = sub2->list.multi[i].end.lnum;
3066 }
3067 else
3068 {
3069 s2 = 0;
3070 e2 = 0;
3071 }
3072 if (s1 != s2 || e1 != e2)
3073 return FALSE;
3074 if (s1 != 0 && sub1->list.multi[i].start.col
3075 != sub2->list.multi[i].start.col)
3076 return FALSE;
3077 if (e1 != 0 && sub1->list.multi[i].end.col
3078 != sub2->list.multi[i].end.col)
3079 return FALSE;
3080 }
3081 }
3082 else
3083 {
3084 for (i = 0; i < todo; ++i)
3085 {
3086 if (i < sub1->in_use)
3087 {
3088 sp1 = sub1->list.line[i].start;
3089 ep1 = sub1->list.line[i].end;
3090 }
3091 else
3092 {
3093 sp1 = NULL;
3094 ep1 = NULL;
3095 }
3096 if (i < sub2->in_use)
3097 {
3098 sp2 = sub2->list.line[i].start;
3099 ep2 = sub2->list.line[i].end;
3100 }
3101 else
3102 {
3103 sp2 = NULL;
3104 ep2 = NULL;
3105 }
3106 if (sp1 != sp2 || ep1 != ep2)
3107 return FALSE;
3108 }
3109 }
3110
3111 return TRUE;
3112}
3113
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003114#ifdef ENABLE_LOG
3115 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003116report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003117{
3118 int col;
3119
3120 if (sub->in_use <= 0)
3121 col = -1;
3122 else if (REG_MULTI)
3123 col = sub->list.multi[0].start.col;
3124 else
3125 col = (int)(sub->list.line[0].start - regline);
3126 nfa_set_code(state->c);
3127 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3128 action, abs(state->id), lid, state->c, code, col);
3129}
3130#endif
3131
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003132 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003133addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003134 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003135 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003136 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003137 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003138{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003139 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003140 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003141 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003142 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003143 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003144 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003145 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003146#ifdef ENABLE_LOG
3147 int did_print = FALSE;
3148#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003149
3150 if (l == NULL || state == NULL)
3151 return;
3152
3153 switch (state->c)
3154 {
3155 case NFA_SPLIT:
3156 case NFA_NOT:
3157 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003158 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003159 case NFA_NCLOSE:
3160 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003161 case NFA_MCLOSE1:
3162 case NFA_MCLOSE2:
3163 case NFA_MCLOSE3:
3164 case NFA_MCLOSE4:
3165 case NFA_MCLOSE5:
3166 case NFA_MCLOSE6:
3167 case NFA_MCLOSE7:
3168 case NFA_MCLOSE8:
3169 case NFA_MCLOSE9:
3170#ifdef FEAT_SYN_HL
3171 case NFA_ZCLOSE:
3172 case NFA_ZCLOSE1:
3173 case NFA_ZCLOSE2:
3174 case NFA_ZCLOSE3:
3175 case NFA_ZCLOSE4:
3176 case NFA_ZCLOSE5:
3177 case NFA_ZCLOSE6:
3178 case NFA_ZCLOSE7:
3179 case NFA_ZCLOSE8:
3180 case NFA_ZCLOSE9:
3181#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003182 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003183 /* These nodes are not added themselves but their "out" and/or
3184 * "out1" may be added below. */
3185 break;
3186
3187 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003188 case NFA_MOPEN1:
3189 case NFA_MOPEN2:
3190 case NFA_MOPEN3:
3191 case NFA_MOPEN4:
3192 case NFA_MOPEN5:
3193 case NFA_MOPEN6:
3194 case NFA_MOPEN7:
3195 case NFA_MOPEN8:
3196 case NFA_MOPEN9:
3197#ifdef FEAT_SYN_HL
3198 case NFA_ZOPEN:
3199 case NFA_ZOPEN1:
3200 case NFA_ZOPEN2:
3201 case NFA_ZOPEN3:
3202 case NFA_ZOPEN4:
3203 case NFA_ZOPEN5:
3204 case NFA_ZOPEN6:
3205 case NFA_ZOPEN7:
3206 case NFA_ZOPEN8:
3207 case NFA_ZOPEN9:
3208#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003209 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003210 /* These nodes do not need to be added, but we need to bail out
3211 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003212 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003213 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003214 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003215 break;
3216
Bram Moolenaar307aa162013-06-02 16:34:21 +02003217 case NFA_BOL:
3218 case NFA_BOF:
3219 /* "^" won't match past end-of-line, don't bother trying.
3220 * Except when we are going to the next line for a look-behind
3221 * match. */
3222 if (reginput > regline
3223 && (nfa_endp == NULL
3224 || !REG_MULTI
3225 || reglnum == nfa_endp->se_u.pos.lnum))
3226 goto skip_add;
3227 /* FALLTHROUGH */
3228
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003229 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003230 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003231 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003232 /* This state is already in the list, don't add it again,
3233 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003234 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003235 {
3236skip_add:
3237#ifdef ENABLE_LOG
3238 nfa_set_code(state->c);
3239 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3240 abs(state->id), l->id, state->c, code);
3241#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003242 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003243 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003244
3245 /* See if the same state is already in the list with the same
3246 * positions. */
3247 for (i = 0; i < l->n; ++i)
3248 {
3249 thread = &l->t[i];
3250 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003251 && sub_equal(&thread->subs.norm, &subs->norm)
3252#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003253 && (!nfa_has_zsubexpr ||
3254 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003255#endif
3256 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003257 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003258 }
3259 }
3260
Bram Moolenaara2d95102013-06-04 14:23:05 +02003261 /* when there are backreferences or look-behind matches the number
3262 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003263 if (nfa_has_backref && l->n == l->len)
3264 {
3265 int newlen = l->len * 3 / 2 + 50;
3266
3267 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3268 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003270
3271 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003272 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003273 thread = &l->t[l->n++];
3274 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003275 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003276 copy_sub(&thread->subs.norm, &subs->norm);
3277#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003278 if (nfa_has_zsubexpr)
3279 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003280#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003281#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003282 report_state("Adding", &thread->subs.norm, state, l->id);
3283 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003284#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003285 }
3286
3287#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003288 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003289 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003290#endif
3291 switch (state->c)
3292 {
3293 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003294 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295 break;
3296
3297 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003298 /* order matters here */
3299 addstate(l, state->out, subs, off);
3300 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 break;
3302
Bram Moolenaar5714b802013-05-28 22:03:20 +02003303 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304 case NFA_NOPEN:
3305 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003306 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307 break;
3308
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003309 case NFA_MOPEN:
3310 case NFA_MOPEN1:
3311 case NFA_MOPEN2:
3312 case NFA_MOPEN3:
3313 case NFA_MOPEN4:
3314 case NFA_MOPEN5:
3315 case NFA_MOPEN6:
3316 case NFA_MOPEN7:
3317 case NFA_MOPEN8:
3318 case NFA_MOPEN9:
3319#ifdef FEAT_SYN_HL
3320 case NFA_ZOPEN:
3321 case NFA_ZOPEN1:
3322 case NFA_ZOPEN2:
3323 case NFA_ZOPEN3:
3324 case NFA_ZOPEN4:
3325 case NFA_ZOPEN5:
3326 case NFA_ZOPEN6:
3327 case NFA_ZOPEN7:
3328 case NFA_ZOPEN8:
3329 case NFA_ZOPEN9:
3330#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003331 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003333 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003335 sub = &subs->norm;
3336 }
3337#ifdef FEAT_SYN_HL
3338 else if (state->c >= NFA_ZOPEN)
3339 {
3340 subidx = state->c - NFA_ZOPEN;
3341 sub = &subs->synt;
3342 }
3343#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003344 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003345 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003346 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003347 sub = &subs->norm;
3348 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003349
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003350 /* Set the position (with "off") in the subexpression. Save and
3351 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003352 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003353 if (REG_MULTI)
3354 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003355 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003356 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003357 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003358 save_in_use = -1;
3359 }
3360 else
3361 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003362 save_in_use = sub->in_use;
3363 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003364 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003365 sub->list.multi[i].start.lnum = -1;
3366 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003367 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003368 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003369 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003370 if (off == -1)
3371 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003372 sub->list.multi[subidx].start.lnum = reglnum + 1;
3373 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003374 }
3375 else
3376 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003377 sub->list.multi[subidx].start.lnum = reglnum;
3378 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003379 (colnr_T)(reginput - regline + off);
3380 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003381 }
3382 else
3383 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003384 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003385 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003386 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003387 save_in_use = -1;
3388 }
3389 else
3390 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003391 save_in_use = sub->in_use;
3392 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003393 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003394 sub->list.line[i].start = NULL;
3395 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003396 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003397 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003398 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003399 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400 }
3401
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003402 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003403
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003404 if (save_in_use == -1)
3405 {
3406 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003407 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003408 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003409 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003410 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003412 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 break;
3414
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003415 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003416 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003418 /* Do not overwrite the position set by \ze. If no \ze
3419 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003420 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003421 break;
3422 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003423 case NFA_MCLOSE1:
3424 case NFA_MCLOSE2:
3425 case NFA_MCLOSE3:
3426 case NFA_MCLOSE4:
3427 case NFA_MCLOSE5:
3428 case NFA_MCLOSE6:
3429 case NFA_MCLOSE7:
3430 case NFA_MCLOSE8:
3431 case NFA_MCLOSE9:
3432#ifdef FEAT_SYN_HL
3433 case NFA_ZCLOSE:
3434 case NFA_ZCLOSE1:
3435 case NFA_ZCLOSE2:
3436 case NFA_ZCLOSE3:
3437 case NFA_ZCLOSE4:
3438 case NFA_ZCLOSE5:
3439 case NFA_ZCLOSE6:
3440 case NFA_ZCLOSE7:
3441 case NFA_ZCLOSE8:
3442 case NFA_ZCLOSE9:
3443#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003446 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003447 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003448 sub = &subs->norm;
3449 }
3450#ifdef FEAT_SYN_HL
3451 else if (state->c >= NFA_ZCLOSE)
3452 {
3453 subidx = state->c - NFA_ZCLOSE;
3454 sub = &subs->synt;
3455 }
3456#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003457 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003458 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003459 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003460 sub = &subs->norm;
3461 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003463 /* We don't fill in gaps here, there must have been an MOPEN that
3464 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003465 save_in_use = sub->in_use;
3466 if (sub->in_use <= subidx)
3467 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003468 if (REG_MULTI)
3469 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003470 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003471 if (off == -1)
3472 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003473 sub->list.multi[subidx].end.lnum = reglnum + 1;
3474 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003475 }
3476 else
3477 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003478 sub->list.multi[subidx].end.lnum = reglnum;
3479 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003480 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003481 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 }
3483 else
3484 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003485 save_ptr = sub->list.line[subidx].end;
3486 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 }
3488
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003489 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003490
3491 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003492 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003494 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003495 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003496 break;
3497 }
3498}
3499
3500/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003501 * Like addstate(), but the new state(s) are put at position "*ip".
3502 * Used for zero-width matches, next state to use is the added one.
3503 * This makes sure the order of states to be tried does not change, which
3504 * matters for alternatives.
3505 */
3506 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003507addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003508 nfa_list_T *l; /* runtime state list */
3509 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003510 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003511 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003512 int *ip;
3513{
3514 int tlen = l->n;
3515 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003516 int listidx = *ip;
3517 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003518
3519 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003520 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003521
Bram Moolenaara2d95102013-06-04 14:23:05 +02003522 /* fill in the "pim" field in the new states */
3523 if (pim != NULL)
3524 for (i = tlen; i < l->n; ++i)
3525 l->t[i].pim = pim;
3526
Bram Moolenaar4b417062013-05-25 20:19:50 +02003527 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003528 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003529 return;
3530
3531 /* re-order to put the new state at the current position */
3532 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003533 if (count == 1)
3534 {
3535 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003536 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003537 }
3538 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003539 {
3540 /* make space for new states, then move them from the
3541 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003542 mch_memmove(&(l->t[listidx + count]),
3543 &(l->t[listidx + 1]),
3544 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3545 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003546 &(l->t[l->n - 1]),
3547 sizeof(nfa_thread_T) * count);
3548 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003549 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003550 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003551}
3552
3553/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003554 * Check character class "class" against current character c.
3555 */
3556 static int
3557check_char_class(class, c)
3558 int class;
3559 int c;
3560{
3561 switch (class)
3562 {
3563 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003564 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 return OK;
3566 break;
3567 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003568 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003569 return OK;
3570 break;
3571 case NFA_CLASS_BLANK:
3572 if (c == ' ' || c == '\t')
3573 return OK;
3574 break;
3575 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003576 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003577 return OK;
3578 break;
3579 case NFA_CLASS_DIGIT:
3580 if (VIM_ISDIGIT(c))
3581 return OK;
3582 break;
3583 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003584 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003585 return OK;
3586 break;
3587 case NFA_CLASS_LOWER:
3588 if (MB_ISLOWER(c))
3589 return OK;
3590 break;
3591 case NFA_CLASS_PRINT:
3592 if (vim_isprintc(c))
3593 return OK;
3594 break;
3595 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003596 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003597 return OK;
3598 break;
3599 case NFA_CLASS_SPACE:
3600 if ((c >=9 && c <= 13) || (c == ' '))
3601 return OK;
3602 break;
3603 case NFA_CLASS_UPPER:
3604 if (MB_ISUPPER(c))
3605 return OK;
3606 break;
3607 case NFA_CLASS_XDIGIT:
3608 if (vim_isxdigit(c))
3609 return OK;
3610 break;
3611 case NFA_CLASS_TAB:
3612 if (c == '\t')
3613 return OK;
3614 break;
3615 case NFA_CLASS_RETURN:
3616 if (c == '\r')
3617 return OK;
3618 break;
3619 case NFA_CLASS_BACKSPACE:
3620 if (c == '\b')
3621 return OK;
3622 break;
3623 case NFA_CLASS_ESCAPE:
3624 if (c == '\033')
3625 return OK;
3626 break;
3627
3628 default:
3629 /* should not be here :P */
3630 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3631 }
3632 return FAIL;
3633}
3634
Bram Moolenaar5714b802013-05-28 22:03:20 +02003635static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3636
3637/*
3638 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003639 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003640 */
3641 static int
3642match_backref(sub, subidx, bytelen)
3643 regsub_T *sub; /* pointers to subexpressions */
3644 int subidx;
3645 int *bytelen; /* out: length of match in bytes */
3646{
3647 int len;
3648
3649 if (sub->in_use <= subidx)
3650 {
3651retempty:
3652 /* backref was not set, match an empty string */
3653 *bytelen = 0;
3654 return TRUE;
3655 }
3656
3657 if (REG_MULTI)
3658 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003659 if (sub->list.multi[subidx].start.lnum < 0
3660 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003661 goto retempty;
3662 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003663 len = sub->list.multi[subidx].end.col
3664 - sub->list.multi[subidx].start.col;
3665 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003666 reginput, &len) == 0)
3667 {
3668 *bytelen = len;
3669 return TRUE;
3670 }
3671 }
3672 else
3673 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003674 if (sub->list.line[subidx].start == NULL
3675 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003676 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003677 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3678 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003679 {
3680 *bytelen = len;
3681 return TRUE;
3682 }
3683 }
3684 return FALSE;
3685}
3686
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003687#ifdef FEAT_SYN_HL
3688
3689static int match_zref __ARGS((int subidx, int *bytelen));
3690
3691/*
3692 * Check for a match with \z subexpression "subidx".
3693 * Return TRUE if it matches.
3694 */
3695 static int
3696match_zref(subidx, bytelen)
3697 int subidx;
3698 int *bytelen; /* out: length of match in bytes */
3699{
3700 int len;
3701
3702 cleanup_zsubexpr();
3703 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3704 {
3705 /* backref was not set, match an empty string */
3706 *bytelen = 0;
3707 return TRUE;
3708 }
3709
3710 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3711 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3712 {
3713 *bytelen = len;
3714 return TRUE;
3715 }
3716 return FALSE;
3717}
3718#endif
3719
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003721 * Save list IDs for all NFA states of "prog" into "list".
3722 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003723 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 */
3725 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003726nfa_save_listids(prog, list)
3727 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003728 int *list;
3729{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003730 int i;
3731 nfa_state_T *p;
3732
3733 /* Order in the list is reverse, it's a bit faster that way. */
3734 p = &prog->state[0];
3735 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003736 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003737 list[i] = p->lastlist[1];
3738 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003739 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003740 }
3741}
3742
3743/*
3744 * Restore list IDs from "list" to all NFA states.
3745 */
3746 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003747nfa_restore_listids(prog, list)
3748 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749 int *list;
3750{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003751 int i;
3752 nfa_state_T *p;
3753
3754 p = &prog->state[0];
3755 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003756 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003757 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003758 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759 }
3760}
3761
Bram Moolenaar423532e2013-05-29 21:14:42 +02003762 static int
3763nfa_re_num_cmp(val, op, pos)
3764 long_u val;
3765 int op;
3766 long_u pos;
3767{
3768 if (op == 1) return pos > val;
3769 if (op == 2) return pos < val;
3770 return val == pos;
3771}
3772
Bram Moolenaarf46da702013-06-02 22:37:42 +02003773static 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 +02003774static 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 +02003775
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003776/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003777 * Recursively call nfa_regmatch()
3778 */
3779 static int
3780recursive_regmatch(state, prog, submatch, m, listids)
3781 nfa_state_T *state;
3782 nfa_regprog_T *prog;
3783 regsubs_T *submatch;
3784 regsubs_T *m;
3785 int **listids;
3786{
3787 char_u *save_reginput = reginput;
3788 char_u *save_regline = regline;
3789 int save_reglnum = reglnum;
3790 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003791 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003792 save_se_T *save_nfa_endp = nfa_endp;
3793 save_se_T endpos;
3794 save_se_T *endposp = NULL;
3795 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003796 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003797
3798 if (state->c == NFA_START_INVISIBLE_BEFORE)
3799 {
3800 /* The recursive match must end at the current position. */
3801 endposp = &endpos;
3802 if (REG_MULTI)
3803 {
3804 endpos.se_u.pos.col = (int)(reginput - regline);
3805 endpos.se_u.pos.lnum = reglnum;
3806 }
3807 else
3808 endpos.se_u.ptr = reginput;
3809
3810 /* Go back the specified number of bytes, or as far as the
3811 * start of the previous line, to try matching "\@<=" or
3812 * not matching "\@<!".
3813 * TODO: This is very inefficient! Would be better to
3814 * first check for a match with what follows. */
3815 if (state->val <= 0)
3816 {
3817 if (REG_MULTI)
3818 {
3819 regline = reg_getline(--reglnum);
3820 if (regline == NULL)
3821 /* can't go before the first line */
3822 regline = reg_getline(++reglnum);
3823 }
3824 reginput = regline;
3825 }
3826 else
3827 {
3828 if (REG_MULTI && (int)(reginput - regline) < state->val)
3829 {
3830 /* Not enough bytes in this line, go to end of
3831 * previous line. */
3832 regline = reg_getline(--reglnum);
3833 if (regline == NULL)
3834 {
3835 /* can't go before the first line */
3836 regline = reg_getline(++reglnum);
3837 reginput = regline;
3838 }
3839 else
3840 reginput = regline + STRLEN(regline);
3841 }
3842 if ((int)(reginput - regline) >= state->val)
3843 {
3844 reginput -= state->val;
3845#ifdef FEAT_MBYTE
3846 if (has_mbyte)
3847 reginput -= mb_head_off(regline, reginput);
3848#endif
3849 }
3850 else
3851 reginput = regline;
3852 }
3853 }
3854
Bram Moolenaarf46da702013-06-02 22:37:42 +02003855#ifdef ENABLE_LOG
3856 if (log_fd != stderr)
3857 fclose(log_fd);
3858 log_fd = NULL;
3859#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003860 /* Have to clear the lastlist field of the NFA nodes, so that
3861 * nfa_regmatch() and addstate() can run properly after recursion. */
3862 if (nfa_ll_index == 1)
3863 {
3864 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3865 * values and clear them. */
3866 if (*listids == NULL)
3867 {
3868 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3869 if (*listids == NULL)
3870 {
3871 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3872 return 0;
3873 }
3874 }
3875 nfa_save_listids(prog, *listids);
3876 need_restore = TRUE;
3877 /* any value of nfa_listid will do */
3878 }
3879 else
3880 {
3881 /* First recursive nfa_regmatch() call, switch to the second lastlist
3882 * entry. Make sure nfa_listid is different from a previous recursive
3883 * call, because some states may still have this ID. */
3884 ++nfa_ll_index;
3885 if (nfa_listid <= nfa_alt_listid)
3886 nfa_listid = nfa_alt_listid;
3887 }
3888
3889 /* Call nfa_regmatch() to check if the current concat matches at this
3890 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003891 nfa_endp = endposp;
3892 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003893
3894 if (need_restore)
3895 nfa_restore_listids(prog, *listids);
3896 else
3897 {
3898 --nfa_ll_index;
3899 nfa_alt_listid = nfa_listid;
3900 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003901
3902 /* restore position in input text */
3903 reginput = save_reginput;
3904 regline = save_regline;
3905 reglnum = save_reglnum;
3906 nfa_match = save_nfa_match;
3907 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003908 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003909
3910#ifdef ENABLE_LOG
3911 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3912 if (log_fd != NULL)
3913 {
3914 fprintf(log_fd, "****************************\n");
3915 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3916 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3917 fprintf(log_fd, "****************************\n");
3918 }
3919 else
3920 {
3921 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3922 log_fd = stderr;
3923 }
3924#endif
3925
3926 return result;
3927}
3928
Bram Moolenaara2d95102013-06-04 14:23:05 +02003929static int failure_chance __ARGS((nfa_state_T *state, int depth));
3930
3931/*
3932 * Estimate the chance of a match with "state" failing.
3933 * NFA_ANY: 1
3934 * specific character: 99
3935 */
3936 static int
3937failure_chance(state, depth)
3938 nfa_state_T *state;
3939 int depth;
3940{
3941 int c = state->c;
3942 int l, r;
3943
3944 /* detect looping */
3945 if (depth > 4)
3946 return 1;
3947
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003948 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02003949 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003950 case NFA_SPLIT:
3951 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3952 /* avoid recursive stuff */
3953 return 1;
3954 /* two alternatives, use the lowest failure chance */
3955 l = failure_chance(state->out, depth + 1);
3956 r = failure_chance(state->out1, depth + 1);
3957 return l < r ? l : r;
3958
3959 case NFA_ANY:
3960 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003961 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003962 case NFA_MATCH:
3963 /* empty match works always */
3964 return 0;
3965
3966 case NFA_BOL:
3967 case NFA_EOL:
3968 case NFA_BOF:
3969 case NFA_EOF:
3970 case NFA_NEWL:
3971 return 99;
3972
3973 case NFA_BOW:
3974 case NFA_EOW:
3975 return 90;
3976
3977 case NFA_MOPEN:
3978 case NFA_MOPEN1:
3979 case NFA_MOPEN2:
3980 case NFA_MOPEN3:
3981 case NFA_MOPEN4:
3982 case NFA_MOPEN5:
3983 case NFA_MOPEN6:
3984 case NFA_MOPEN7:
3985 case NFA_MOPEN8:
3986 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02003987#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003988 case NFA_ZOPEN:
3989 case NFA_ZOPEN1:
3990 case NFA_ZOPEN2:
3991 case NFA_ZOPEN3:
3992 case NFA_ZOPEN4:
3993 case NFA_ZOPEN5:
3994 case NFA_ZOPEN6:
3995 case NFA_ZOPEN7:
3996 case NFA_ZOPEN8:
3997 case NFA_ZOPEN9:
3998 case NFA_ZCLOSE:
3999 case NFA_ZCLOSE1:
4000 case NFA_ZCLOSE2:
4001 case NFA_ZCLOSE3:
4002 case NFA_ZCLOSE4:
4003 case NFA_ZCLOSE5:
4004 case NFA_ZCLOSE6:
4005 case NFA_ZCLOSE7:
4006 case NFA_ZCLOSE8:
4007 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004008#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004009 case NFA_NOPEN:
4010 case NFA_MCLOSE:
4011 case NFA_MCLOSE1:
4012 case NFA_MCLOSE2:
4013 case NFA_MCLOSE3:
4014 case NFA_MCLOSE4:
4015 case NFA_MCLOSE5:
4016 case NFA_MCLOSE6:
4017 case NFA_MCLOSE7:
4018 case NFA_MCLOSE8:
4019 case NFA_MCLOSE9:
4020 case NFA_NCLOSE:
4021 return failure_chance(state->out, depth + 1);
4022
4023 case NFA_BACKREF1:
4024 case NFA_BACKREF2:
4025 case NFA_BACKREF3:
4026 case NFA_BACKREF4:
4027 case NFA_BACKREF5:
4028 case NFA_BACKREF6:
4029 case NFA_BACKREF7:
4030 case NFA_BACKREF8:
4031 case NFA_BACKREF9:
4032#ifdef FEAT_SYN_HL
4033 case NFA_ZREF1:
4034 case NFA_ZREF2:
4035 case NFA_ZREF3:
4036 case NFA_ZREF4:
4037 case NFA_ZREF5:
4038 case NFA_ZREF6:
4039 case NFA_ZREF7:
4040 case NFA_ZREF8:
4041 case NFA_ZREF9:
4042#endif
4043 /* backreferences don't match in many places */
4044 return 94;
4045
4046 case NFA_LNUM_GT:
4047 case NFA_LNUM_LT:
4048 case NFA_COL_GT:
4049 case NFA_COL_LT:
4050 case NFA_VCOL_GT:
4051 case NFA_VCOL_LT:
4052 case NFA_MARK_GT:
4053 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004054 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004055 /* before/after positions don't match very often */
4056 return 85;
4057
4058 case NFA_LNUM:
4059 return 90;
4060
4061 case NFA_CURSOR:
4062 case NFA_COL:
4063 case NFA_VCOL:
4064 case NFA_MARK:
4065 /* specific positions rarely match */
4066 return 98;
4067
4068 case NFA_COMPOSING:
4069 return 95;
4070
4071 default:
4072 if (c > 0)
4073 /* character match fails often */
4074 return 95;
4075 }
4076
4077 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004078 return 50;
4079}
4080
Bram Moolenaarf46da702013-06-02 22:37:42 +02004081/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004082 * Main matching routine.
4083 *
4084 * Run NFA to determine whether it matches reginput.
4085 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004086 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004087 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004088 * Return TRUE if there is a match, FALSE otherwise.
4089 * Note: Caller must ensure that: start != NULL.
4090 */
4091 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004092nfa_regmatch(prog, start, submatch, m)
4093 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004094 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004095 regsubs_T *submatch;
4096 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004097{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004098 int result;
4099 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004100 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004101 int go_to_nextline = FALSE;
4102 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004103 nfa_list_T list[3];
4104 nfa_list_T *listtbl[2][2];
4105 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004106 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004107 nfa_list_T *thislist;
4108 nfa_list_T *nextlist;
4109 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004110 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004111 nfa_state_T *add_state;
4112 int add_count;
4113 int add_off;
4114 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004115#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004116 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117
4118 if (debug == NULL)
4119 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004120 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004121 return FALSE;
4122 }
4123#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004124 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004125 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004126
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004127 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004128 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004129 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004130 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004131 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004132 list[1].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004133 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004134 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
4136 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004137
4138#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004139 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004140 if (log_fd != NULL)
4141 {
4142 fprintf(log_fd, "**********************************\n");
4143 nfa_set_code(start->c);
4144 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4145 abs(start->id), code);
4146 fprintf(log_fd, "**********************************\n");
4147 }
4148 else
4149 {
4150 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4151 log_fd = stderr;
4152 }
4153#endif
4154
4155 thislist = &list[0];
4156 thislist->n = 0;
4157 nextlist = &list[1];
4158 nextlist->n = 0;
4159 neglist = &list[2];
4160 neglist->n = 0;
4161#ifdef ENABLE_LOG
4162 fprintf(log_fd, "(---) STARTSTATE\n");
4163#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004164 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004165 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004166
4167 /* There are two cases when the NFA advances: 1. input char matches the
4168 * NFA node and 2. input char does not match the NFA node, but the next
4169 * node is NFA_NOT. The following macro calls addstate() according to
4170 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4171 listtbl[0][0] = NULL;
4172 listtbl[0][1] = neglist;
4173 listtbl[1][0] = nextlist;
4174 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004175#define ADD_POS_NEG_STATE(state) \
4176 ll = listtbl[result ? 1 : 0][state->negated]; \
4177 if (ll != NULL) { \
4178 add_state = state->out; \
4179 add_off = clen; \
4180 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004181
4182 /*
4183 * Run for each character.
4184 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004185 for (;;)
4186 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004187 int curc;
4188 int clen;
4189
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004190#ifdef FEAT_MBYTE
4191 if (has_mbyte)
4192 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004193 curc = (*mb_ptr2char)(reginput);
4194 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004195 }
4196 else
4197#endif
4198 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004199 curc = *reginput;
4200 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004201 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004202 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004203 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004204 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004205 go_to_nextline = FALSE;
4206 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004207
4208 /* swap lists */
4209 thislist = &list[flag];
4210 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004211 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004212 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004213 ++nfa_listid;
4214 thislist->id = nfa_listid;
4215 nextlist->id = nfa_listid + 1;
4216 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004217
Bram Moolenaara2d95102013-06-04 14:23:05 +02004218 pimlist.ga_len = 0;
4219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004220#ifdef ENABLE_LOG
4221 fprintf(log_fd, "------------------------------------------\n");
4222 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004223 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004224 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004225 {
4226 int i;
4227
4228 for (i = 0; i < thislist->n; i++)
4229 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4230 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004231 fprintf(log_fd, "\n");
4232#endif
4233
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004234#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235 fprintf(debug, "\n-------------------\n");
4236#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004237 /*
4238 * If the state lists are empty we can stop.
4239 */
4240 if (thislist->n == 0 && neglist->n == 0)
4241 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004242
4243 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004244 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004245 {
4246 if (neglist->n > 0)
4247 {
4248 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004249 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004250 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004251 }
4252 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004253 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004254
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004255#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 nfa_set_code(t->state->c);
4257 fprintf(debug, "%s, ", code);
4258#endif
4259#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004260 {
4261 int col;
4262
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004263 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004264 col = -1;
4265 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004266 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004267 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004268 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004269 nfa_set_code(t->state->c);
4270 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4271 abs(t->state->id), (int)t->state->c, code, col);
4272 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004273#endif
4274
4275 /*
4276 * Handle the possible codes of the current state.
4277 * The most important is NFA_MATCH.
4278 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004279 add_state = NULL;
4280 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004281 switch (t->state->c)
4282 {
4283 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004284 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004285 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004286 copy_sub(&submatch->norm, &t->subs.norm);
4287#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004288 if (nfa_has_zsubexpr)
4289 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004290#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004291#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004292 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004293#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004294 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004295 * states at this position. When the list of states is going
4296 * to be empty quit without advancing, so that "reginput" is
4297 * correct. */
4298 if (nextlist->n == 0 && neglist->n == 0)
4299 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004300 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004301 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004302
4303 case NFA_END_INVISIBLE:
Bram Moolenaar87953742013-06-05 18:52:40 +02004304 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004305 /*
4306 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004307 * NFA_START_INVISIBLE_BEFORE node.
4308 * They surround a zero-width group, used with "\@=", "\&",
4309 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004310 * If we got here, it means that the current "invisible" group
4311 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004312 * nfa_regmatch(). For a look-behind match only when it ends
4313 * in the position in "nfa_endp".
4314 * Submatches are stored in *m, and used in the parent call.
4315 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004316#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004317 if (nfa_endp != NULL)
4318 {
4319 if (REG_MULTI)
4320 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4321 (int)reglnum,
4322 (int)nfa_endp->se_u.pos.lnum,
4323 (int)(reginput - regline),
4324 nfa_endp->se_u.pos.col);
4325 else
4326 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4327 (int)(reginput - regline),
4328 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004329 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004330#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004331 /* If "nfa_endp" is set it's only a match if it ends at
4332 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004333 if (nfa_endp != NULL && (REG_MULTI
4334 ? (reglnum != nfa_endp->se_u.pos.lnum
4335 || (int)(reginput - regline)
4336 != nfa_endp->se_u.pos.col)
4337 : reginput != nfa_endp->se_u.ptr))
4338 break;
4339
4340 /* do not set submatches for \@! */
4341 if (!t->state->negated)
4342 {
4343 copy_sub(&m->norm, &t->subs.norm);
4344#ifdef FEAT_SYN_HL
4345 if (nfa_has_zsubexpr)
4346 copy_sub(&m->synt, &t->subs.synt);
4347#endif
4348 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004349#ifdef ENABLE_LOG
4350 fprintf(log_fd, "Match found:\n");
4351 log_subsexpr(m);
4352#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004353 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 break;
4355
4356 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004357 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004358 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004359 nfa_pim_T *pim;
4360 int cout = t->state->out1->out->c;
4361
4362 /* Do it directly when what follows is possibly end of
4363 * match (closing paren).
4364 * Postpone when it is \@<= or \@<!, these are expensive.
4365 * TODO: remove the check for t->pim and check multiple
4366 * where it's used?
4367 * Otherwise first do the one that has the highest chance
4368 * of failing. */
4369 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004370#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004371 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004372#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004373 || cout == NFA_NCLOSE
4374 || t->pim != NULL
4375 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4376 && failure_chance(t->state->out1->out, 0)
4377 < failure_chance(t->state->out, 0)))
4378 {
4379 /*
4380 * First try matching the invisible match, then what
4381 * follows.
4382 */
4383 result = recursive_regmatch(t->state, prog,
4384 submatch, m, &listids);
4385
4386 /* for \@! it is a match when result is FALSE */
4387 if (result != t->state->negated)
4388 {
4389 /* Copy submatch info from the recursive call */
4390 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004391#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004392 if (nfa_has_zsubexpr)
4393 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004394#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004395
Bram Moolenaara2d95102013-06-04 14:23:05 +02004396 /* t->state->out1 is the corresponding
4397 * END_INVISIBLE node; Add its out to the current
4398 * list (zero-width match). */
4399 addstate_here(thislist, t->state->out1->out,
4400 &t->subs, t->pim, &listidx);
4401 }
4402 }
4403 else
4404 {
4405 /*
4406 * First try matching what follows at the current
4407 * position. Only if a match is found, addstate() is
4408 * called, then verify the invisible match matches.
4409 * Add a nfa_pim_T to the following states, it
4410 * contains info about the invisible match.
4411 */
4412 if (ga_grow(&pimlist, 1) == FAIL)
4413 goto theend;
4414 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4415 ++pimlist.ga_len;
4416 pim->state = t->state;
4417 pim->pim = NULL;
4418 pim->result = NFA_PIM_TODO;
4419
4420 /* t->state->out1 is the corresponding END_INVISIBLE
4421 * node; Add its out to the current list (zero-width
4422 * match). */
4423 addstate_here(thislist, t->state->out1->out, &t->subs,
4424 pim, &listidx);
4425 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004426 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004427 break;
4428
Bram Moolenaar87953742013-06-05 18:52:40 +02004429 case NFA_START_PATTERN:
4430 /* First try matching the pattern. */
4431 result = recursive_regmatch(t->state, prog,
4432 submatch, m, &listids);
4433 if (result)
4434 {
4435 int bytelen;
4436
4437#ifdef ENABLE_LOG
4438 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4439 log_subsexpr(m);
4440#endif
4441 /* Copy submatch info from the recursive call */
4442 copy_sub_off(&t->subs.norm, &m->norm);
4443#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004444 if (nfa_has_zsubexpr)
4445 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02004446#endif
4447 /* Now we need to skip over the matched text and then
4448 * continue with what follows. */
4449 if (REG_MULTI)
4450 /* TODO: multi-line match */
4451 bytelen = m->norm.list.multi[0].end.col
4452 - (int)(reginput - regline);
4453 else
4454 bytelen = (int)(m->norm.list.line[0].end - reginput);
4455
4456#ifdef ENABLE_LOG
4457 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4458#endif
4459 if (bytelen == 0)
4460 {
4461 /* empty match, output of corresponding
4462 * NFA_END_PATTERN/NFA_SKIP to be used at current
4463 * position */
4464 addstate_here(thislist, t->state->out1->out->out,
4465 &t->subs, t->pim, &listidx);
4466 }
4467 else if (bytelen <= clen)
4468 {
4469 /* match current character, output of corresponding
4470 * NFA_END_PATTERN to be used at next position. */
4471 ll = nextlist;
4472 add_state = t->state->out1->out->out;
4473 add_off = clen;
4474 }
4475 else
4476 {
4477 /* skip over the matched characters, set character
4478 * count in NFA_SKIP */
4479 ll = nextlist;
4480 add_state = t->state->out1->out;
4481 add_off = bytelen;
4482 add_count = bytelen - clen;
4483 }
4484 }
4485 break;
4486
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004487 case NFA_BOL:
4488 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004489 addstate_here(thislist, t->state->out, &t->subs,
4490 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004491 break;
4492
4493 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004494 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004495 addstate_here(thislist, t->state->out, &t->subs,
4496 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004497 break;
4498
4499 case NFA_BOW:
4500 {
4501 int bow = TRUE;
4502
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004503 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004504 bow = FALSE;
4505#ifdef FEAT_MBYTE
4506 else if (has_mbyte)
4507 {
4508 int this_class;
4509
4510 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004511 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004512 if (this_class <= 1)
4513 bow = FALSE;
4514 else if (reg_prev_class() == this_class)
4515 bow = FALSE;
4516 }
4517#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004518 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004519 || (reginput > regline
4520 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004521 bow = FALSE;
4522 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004523 addstate_here(thislist, t->state->out, &t->subs,
4524 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 break;
4526 }
4527
4528 case NFA_EOW:
4529 {
4530 int eow = TRUE;
4531
4532 if (reginput == regline)
4533 eow = FALSE;
4534#ifdef FEAT_MBYTE
4535 else if (has_mbyte)
4536 {
4537 int this_class, prev_class;
4538
4539 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004540 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004541 prev_class = reg_prev_class();
4542 if (this_class == prev_class
4543 || prev_class == 0 || prev_class == 1)
4544 eow = FALSE;
4545 }
4546#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004547 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004548 || (reginput[0] != NUL
4549 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550 eow = FALSE;
4551 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004552 addstate_here(thislist, t->state->out, &t->subs,
4553 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 break;
4555 }
4556
Bram Moolenaar4b780632013-05-31 22:14:52 +02004557 case NFA_BOF:
4558 if (reglnum == 0 && reginput == regline
4559 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004560 addstate_here(thislist, t->state->out, &t->subs,
4561 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004562 break;
4563
4564 case NFA_EOF:
4565 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004566 addstate_here(thislist, t->state->out, &t->subs,
4567 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004568 break;
4569
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004570#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004571 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004572 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004573 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004574 int len = 0;
4575 nfa_state_T *end;
4576 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004577 int cchars[MAX_MCO];
4578 int ccount = 0;
4579 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004580
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004581 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004582 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004583 if (utf_iscomposing(sta->c))
4584 {
4585 /* Only match composing character(s), ignore base
4586 * character. Used for ".{composing}" and "{composing}"
4587 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004588 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004589 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004590 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004591 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004592 /* If \Z was present, then ignore composing characters.
4593 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004594 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004595 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004596 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004597 else
4598 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004599 while (sta->c != NFA_END_COMPOSING)
4600 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004601 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004602
4603 /* Check base character matches first, unless ignored. */
4604 else if (len > 0 || mc == sta->c)
4605 {
4606 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004607 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004608 len += mb_char2len(mc);
4609 sta = sta->out;
4610 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004612 /* We don't care about the order of composing characters.
4613 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004614 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004615 {
4616 mc = mb_ptr2char(reginput + len);
4617 cchars[ccount++] = mc;
4618 len += mb_char2len(mc);
4619 if (ccount == MAX_MCO)
4620 break;
4621 }
4622
4623 /* Check that each composing char in the pattern matches a
4624 * composing char in the text. We do not check if all
4625 * composing chars are matched. */
4626 result = OK;
4627 while (sta->c != NFA_END_COMPOSING)
4628 {
4629 for (j = 0; j < ccount; ++j)
4630 if (cchars[j] == sta->c)
4631 break;
4632 if (j == ccount)
4633 {
4634 result = FAIL;
4635 break;
4636 }
4637 sta = sta->out;
4638 }
4639 }
4640 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004641 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004642
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004643 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004644 ADD_POS_NEG_STATE(end);
4645 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004646 }
4647#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004648
4649 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004650 if (curc == NUL && !reg_line_lbr && REG_MULTI
4651 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004652 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004653 go_to_nextline = TRUE;
4654 /* Pass -1 for the offset, which means taking the position
4655 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004656 ll = nextlist;
4657 add_state = t->state->out;
4658 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004659 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004660 else if (curc == '\n' && reg_line_lbr)
4661 {
4662 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004663 ll = nextlist;
4664 add_state = t->state->out;
4665 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004666 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004667 break;
4668
4669 case NFA_CLASS_ALNUM:
4670 case NFA_CLASS_ALPHA:
4671 case NFA_CLASS_BLANK:
4672 case NFA_CLASS_CNTRL:
4673 case NFA_CLASS_DIGIT:
4674 case NFA_CLASS_GRAPH:
4675 case NFA_CLASS_LOWER:
4676 case NFA_CLASS_PRINT:
4677 case NFA_CLASS_PUNCT:
4678 case NFA_CLASS_SPACE:
4679 case NFA_CLASS_UPPER:
4680 case NFA_CLASS_XDIGIT:
4681 case NFA_CLASS_TAB:
4682 case NFA_CLASS_RETURN:
4683 case NFA_CLASS_BACKSPACE:
4684 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004685 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686 ADD_POS_NEG_STATE(t->state);
4687 break;
4688
4689 case NFA_END_NEG_RANGE:
4690 /* This follows a series of negated nodes, like:
4691 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004692 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004693 {
4694 ll = nextlist;
4695 add_state = t->state->out;
4696 add_off = clen;
4697 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 break;
4699
4700 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004701 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004702 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004703 {
4704 ll = nextlist;
4705 add_state = t->state->out;
4706 add_off = clen;
4707 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 break;
4709
4710 /*
4711 * Character classes like \a for alpha, \d for digit etc.
4712 */
4713 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004714 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004715 ADD_POS_NEG_STATE(t->state);
4716 break;
4717
4718 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004719 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004720 ADD_POS_NEG_STATE(t->state);
4721 break;
4722
4723 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004724 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004725 ADD_POS_NEG_STATE(t->state);
4726 break;
4727
4728 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004729 result = !VIM_ISDIGIT(curc)
4730 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004731 ADD_POS_NEG_STATE(t->state);
4732 break;
4733
4734 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004735 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736 ADD_POS_NEG_STATE(t->state);
4737 break;
4738
4739 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004740 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004741 ADD_POS_NEG_STATE(t->state);
4742 break;
4743
4744 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004745 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004746 ADD_POS_NEG_STATE(t->state);
4747 break;
4748
4749 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004750 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004751 ADD_POS_NEG_STATE(t->state);
4752 break;
4753
4754 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004755 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004756 ADD_POS_NEG_STATE(t->state);
4757 break;
4758
4759 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004760 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004761 ADD_POS_NEG_STATE(t->state);
4762 break;
4763
4764 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004765 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004766 ADD_POS_NEG_STATE(t->state);
4767 break;
4768
4769 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004770 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004771 ADD_POS_NEG_STATE(t->state);
4772 break;
4773
4774 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004775 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004776 ADD_POS_NEG_STATE(t->state);
4777 break;
4778
4779 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004780 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004781 ADD_POS_NEG_STATE(t->state);
4782 break;
4783
4784 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004785 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004786 ADD_POS_NEG_STATE(t->state);
4787 break;
4788
4789 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004790 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004791 ADD_POS_NEG_STATE(t->state);
4792 break;
4793
4794 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004795 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004796 ADD_POS_NEG_STATE(t->state);
4797 break;
4798
4799 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004800 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004801 ADD_POS_NEG_STATE(t->state);
4802 break;
4803
4804 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004805 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004806 ADD_POS_NEG_STATE(t->state);
4807 break;
4808
4809 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004810 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004811 ADD_POS_NEG_STATE(t->state);
4812 break;
4813
4814 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004815 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004816 ADD_POS_NEG_STATE(t->state);
4817 break;
4818
4819 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004820 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004821 ADD_POS_NEG_STATE(t->state);
4822 break;
4823
4824 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004825 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004826 ADD_POS_NEG_STATE(t->state);
4827 break;
4828
4829 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004830 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004831 ADD_POS_NEG_STATE(t->state);
4832 break;
4833
4834 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004835 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004836 ADD_POS_NEG_STATE(t->state);
4837 break;
4838
4839 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004840 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004841 ADD_POS_NEG_STATE(t->state);
4842 break;
4843
Bram Moolenaar5714b802013-05-28 22:03:20 +02004844 case NFA_BACKREF1:
4845 case NFA_BACKREF2:
4846 case NFA_BACKREF3:
4847 case NFA_BACKREF4:
4848 case NFA_BACKREF5:
4849 case NFA_BACKREF6:
4850 case NFA_BACKREF7:
4851 case NFA_BACKREF8:
4852 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004853#ifdef FEAT_SYN_HL
4854 case NFA_ZREF1:
4855 case NFA_ZREF2:
4856 case NFA_ZREF3:
4857 case NFA_ZREF4:
4858 case NFA_ZREF5:
4859 case NFA_ZREF6:
4860 case NFA_ZREF7:
4861 case NFA_ZREF8:
4862 case NFA_ZREF9:
4863#endif
4864 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004865 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004866 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004867 int bytelen;
4868
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004869 if (t->state->c <= NFA_BACKREF9)
4870 {
4871 subidx = t->state->c - NFA_BACKREF1 + 1;
4872 result = match_backref(&t->subs.norm, subidx, &bytelen);
4873 }
4874#ifdef FEAT_SYN_HL
4875 else
4876 {
4877 subidx = t->state->c - NFA_ZREF1 + 1;
4878 result = match_zref(subidx, &bytelen);
4879 }
4880#endif
4881
Bram Moolenaar5714b802013-05-28 22:03:20 +02004882 if (result)
4883 {
4884 if (bytelen == 0)
4885 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004886 /* empty match always works, output of NFA_SKIP to be
4887 * used next */
4888 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004889 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004890 }
4891 else if (bytelen <= clen)
4892 {
4893 /* match current character, jump ahead to out of
4894 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004895 ll = nextlist;
4896 add_state = t->state->out->out;
4897 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004898 }
4899 else
4900 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004901 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004902 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004903 ll = nextlist;
4904 add_state = t->state->out;
4905 add_off = bytelen;
4906 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004907 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004908 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004909 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004910 }
4911 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004912 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004913 if (t->count - clen <= 0)
4914 {
4915 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004916 ll = nextlist;
4917 add_state = t->state->out;
4918 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004919 }
4920 else
4921 {
4922 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004923 ll = nextlist;
4924 add_state = t->state;
4925 add_off = 0;
4926 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004927 }
4928 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004929
Bram Moolenaar423532e2013-05-29 21:14:42 +02004930 case NFA_LNUM:
4931 case NFA_LNUM_GT:
4932 case NFA_LNUM_LT:
4933 result = (REG_MULTI &&
4934 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4935 (long_u)(reglnum + reg_firstlnum)));
4936 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004937 addstate_here(thislist, t->state->out, &t->subs,
4938 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004939 break;
4940
4941 case NFA_COL:
4942 case NFA_COL_GT:
4943 case NFA_COL_LT:
4944 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4945 (long_u)(reginput - regline) + 1);
4946 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004947 addstate_here(thislist, t->state->out, &t->subs,
4948 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004949 break;
4950
4951 case NFA_VCOL:
4952 case NFA_VCOL_GT:
4953 case NFA_VCOL_LT:
4954 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4955 (long_u)win_linetabsize(
4956 reg_win == NULL ? curwin : reg_win,
4957 regline, (colnr_T)(reginput - regline)) + 1);
4958 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004959 addstate_here(thislist, t->state->out, &t->subs,
4960 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004961 break;
4962
Bram Moolenaar044aa292013-06-04 21:27:38 +02004963 case NFA_MARK:
4964 case NFA_MARK_GT:
4965 case NFA_MARK_LT:
4966 {
4967 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
4968
4969 /* Compare the mark position to the match position. */
4970 result = (pos != NULL /* mark doesn't exist */
4971 && pos->lnum > 0 /* mark isn't set in reg_buf */
4972 && (pos->lnum == reglnum + reg_firstlnum
4973 ? (pos->col == (colnr_T)(reginput - regline)
4974 ? t->state->c == NFA_MARK
4975 : (pos->col < (colnr_T)(reginput - regline)
4976 ? t->state->c == NFA_MARK_GT
4977 : t->state->c == NFA_MARK_LT))
4978 : (pos->lnum < reglnum + reg_firstlnum
4979 ? t->state->c == NFA_MARK_GT
4980 : t->state->c == NFA_MARK_LT)));
4981 if (result)
4982 addstate_here(thislist, t->state->out, &t->subs,
4983 t->pim, &listidx);
4984 break;
4985 }
4986
Bram Moolenaar423532e2013-05-29 21:14:42 +02004987 case NFA_CURSOR:
4988 result = (reg_win != NULL
4989 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4990 && ((colnr_T)(reginput - regline)
4991 == reg_win->w_cursor.col));
4992 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004993 addstate_here(thislist, t->state->out, &t->subs,
4994 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004995 break;
4996
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004997 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02004998#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004999 result = reg_match_visual();
5000 if (result)
5001 addstate_here(thislist, t->state->out, &t->subs,
5002 t->pim, &listidx);
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005003#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005004 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005005
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005006 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005007 {
5008 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005009
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005010 /* TODO: put this in #ifdef later */
5011 if (c < -256)
5012 EMSGN("INTERNAL: Negative state char: %ld", c);
5013 if (is_Magic(c))
5014 c = un_Magic(c);
5015 result = (c == curc);
5016
5017 if (!result && ireg_ic)
5018 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005019#ifdef FEAT_MBYTE
5020 /* If there is a composing character which is not being
5021 * ignored there can be no match. Match with composing
5022 * character uses NFA_COMPOSING above. */
5023 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005024 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005025 result = FALSE;
5026#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005027 ADD_POS_NEG_STATE(t->state);
5028 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005029 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005030
5031 } /* switch (t->state->c) */
5032
5033 if (add_state != NULL)
5034 {
5035 if (t->pim != NULL)
5036 {
5037 /* postponed invisible match */
5038 /* TODO: also do t->pim->pim recursively? */
5039 if (t->pim->result == NFA_PIM_TODO)
5040 {
5041#ifdef ENABLE_LOG
5042 fprintf(log_fd, "\n");
5043 fprintf(log_fd, "==================================\n");
5044 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5045 fprintf(log_fd, "\n");
5046#endif
5047 result = recursive_regmatch(t->pim->state,
5048 prog, submatch, m, &listids);
5049 t->pim->result = result ? NFA_PIM_MATCH
5050 : NFA_PIM_NOMATCH;
5051 /* for \@! it is a match when result is FALSE */
5052 if (result != t->pim->state->negated)
5053 {
5054 /* Copy submatch info from the recursive call */
5055 copy_sub_off(&t->pim->subs.norm, &m->norm);
5056#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005057 if (nfa_has_zsubexpr)
5058 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005059#endif
5060 }
5061 }
5062 else
5063 {
5064 result = (t->pim->result == NFA_PIM_MATCH);
5065#ifdef ENABLE_LOG
5066 fprintf(log_fd, "\n");
5067 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5068 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5069 fprintf(log_fd, "\n");
5070#endif
5071 }
5072
5073 /* for \@! it is a match when result is FALSE */
5074 if (result != t->pim->state->negated)
5075 {
5076 /* Copy submatch info from the recursive call */
5077 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5078#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005079 if (nfa_has_zsubexpr)
5080 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005081#endif
5082 }
5083 else
5084 /* look-behind match failed, don't add the state */
5085 continue;
5086 }
5087
5088 addstate(ll, add_state, &t->subs, add_off);
5089 if (add_count > 0)
5090 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005091 }
5092
5093 } /* for (thislist = thislist; thislist->state; thislist++) */
5094
Bram Moolenaare23febd2013-05-26 18:40:14 +02005095 /* Look for the start of a match in the current position by adding the
5096 * start state to the list of states.
5097 * The first found match is the leftmost one, thus the order of states
5098 * matters!
5099 * Do not add the start state in recursive calls of nfa_regmatch(),
5100 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005101 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005102 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005103 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005104 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005105 && reglnum == 0
5106 && clen != 0
5107 && (ireg_maxcol == 0
5108 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005109 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005110 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005111 ? (reglnum < nfa_endp->se_u.pos.lnum
5112 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005113 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005114 < nfa_endp->se_u.pos.col))
5115 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005116 {
5117#ifdef ENABLE_LOG
5118 fprintf(log_fd, "(---) STARTSTATE\n");
5119#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005120 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005121 }
5122
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005123#ifdef ENABLE_LOG
5124 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005125 {
5126 int i;
5127
5128 for (i = 0; i < thislist->n; i++)
5129 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5130 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005131 fprintf(log_fd, "\n");
5132#endif
5133
5134nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005135 /* Advance to the next character, or advance to the next line, or
5136 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005137 if (clen != 0)
5138 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005139 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5140 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005141 reg_nextline();
5142 else
5143 break;
5144 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005145
5146#ifdef ENABLE_LOG
5147 if (log_fd != stderr)
5148 fclose(log_fd);
5149 log_fd = NULL;
5150#endif
5151
5152theend:
5153 /* Free memory */
5154 vim_free(list[0].t);
5155 vim_free(list[1].t);
5156 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005157 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005158 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005159#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005160#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005161 fclose(debug);
5162#endif
5163
Bram Moolenaar963fee22013-05-26 21:47:28 +02005164 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005165}
5166
5167/*
5168 * Try match of "prog" with at regline["col"].
5169 * Returns 0 for failure, number of lines contained in the match otherwise.
5170 */
5171 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005172nfa_regtry(prog, col)
5173 nfa_regprog_T *prog;
5174 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005175{
5176 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005177 regsubs_T subs, m;
5178 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005179#ifdef ENABLE_LOG
5180 FILE *f;
5181#endif
5182
5183 reginput = regline + col;
5184 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005185#ifdef FEAT_SYN_HL
5186 /* Clear the external match subpointers if necessary. */
5187 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005188 {
5189 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005190 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005191 }
5192 else
5193 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005194#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005195
5196#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005197 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005198 if (f != NULL)
5199 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005200 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005201#ifdef DEBUG
5202 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5203#endif
5204 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005205 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005206 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005207 fprintf(f, "\n\n");
5208 fclose(f);
5209 }
5210 else
5211 EMSG(_("Could not open temporary log file for writing "));
5212#endif
5213
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005214 clear_sub(&subs.norm);
5215 clear_sub(&m.norm);
5216#ifdef FEAT_SYN_HL
5217 clear_sub(&subs.synt);
5218 clear_sub(&m.synt);
5219#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005220
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005221 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005222 return 0;
5223
5224 cleanup_subexpr();
5225 if (REG_MULTI)
5226 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005227 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005228 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005229 reg_startpos[i] = subs.norm.list.multi[i].start;
5230 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005231 }
5232
5233 if (reg_startpos[0].lnum < 0)
5234 {
5235 reg_startpos[0].lnum = 0;
5236 reg_startpos[0].col = col;
5237 }
5238 if (reg_endpos[0].lnum < 0)
5239 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005240 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005241 reg_endpos[0].lnum = reglnum;
5242 reg_endpos[0].col = (int)(reginput - regline);
5243 }
5244 else
5245 /* Use line number of "\ze". */
5246 reglnum = reg_endpos[0].lnum;
5247 }
5248 else
5249 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005250 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005251 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005252 reg_startp[i] = subs.norm.list.line[i].start;
5253 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005254 }
5255
5256 if (reg_startp[0] == NULL)
5257 reg_startp[0] = regline + col;
5258 if (reg_endp[0] == NULL)
5259 reg_endp[0] = reginput;
5260 }
5261
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005262#ifdef FEAT_SYN_HL
5263 /* Package any found \z(...\) matches for export. Default is none. */
5264 unref_extmatch(re_extmatch_out);
5265 re_extmatch_out = NULL;
5266
5267 if (prog->reghasz == REX_SET)
5268 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005269 cleanup_zsubexpr();
5270 re_extmatch_out = make_extmatch();
5271 for (i = 0; i < subs.synt.in_use; i++)
5272 {
5273 if (REG_MULTI)
5274 {
5275 struct multipos *mpos = &subs.synt.list.multi[i];
5276
5277 /* Only accept single line matches. */
5278 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5279 re_extmatch_out->matches[i] =
5280 vim_strnsave(reg_getline(mpos->start.lnum)
5281 + mpos->start.col,
5282 mpos->end.col - mpos->start.col);
5283 }
5284 else
5285 {
5286 struct linepos *lpos = &subs.synt.list.line[i];
5287
5288 if (lpos->start != NULL && lpos->end != NULL)
5289 re_extmatch_out->matches[i] =
5290 vim_strnsave(lpos->start,
5291 (int)(lpos->end - lpos->start));
5292 }
5293 }
5294 }
5295#endif
5296
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005297 return 1 + reglnum;
5298}
5299
5300/*
5301 * Match a regexp against a string ("line" points to the string) or multiple
5302 * lines ("line" is NULL, use reg_getline()).
5303 *
5304 * Returns 0 for failure, number of lines contained in the match otherwise.
5305 */
5306 static long
5307nfa_regexec_both(line, col)
5308 char_u *line;
5309 colnr_T col; /* column to start looking for match */
5310{
5311 nfa_regprog_T *prog;
5312 long retval = 0L;
5313 int i;
5314
5315 if (REG_MULTI)
5316 {
5317 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5318 line = reg_getline((linenr_T)0); /* relative to the cursor */
5319 reg_startpos = reg_mmatch->startpos;
5320 reg_endpos = reg_mmatch->endpos;
5321 }
5322 else
5323 {
5324 prog = (nfa_regprog_T *)reg_match->regprog;
5325 reg_startp = reg_match->startp;
5326 reg_endp = reg_match->endp;
5327 }
5328
5329 /* Be paranoid... */
5330 if (prog == NULL || line == NULL)
5331 {
5332 EMSG(_(e_null));
5333 goto theend;
5334 }
5335
5336 /* If the start column is past the maximum column: no need to try. */
5337 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5338 goto theend;
5339
5340 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5341 if (prog->regflags & RF_ICASE)
5342 ireg_ic = TRUE;
5343 else if (prog->regflags & RF_NOICASE)
5344 ireg_ic = FALSE;
5345
5346#ifdef FEAT_MBYTE
5347 /* If pattern contains "\Z" overrule value of ireg_icombine */
5348 if (prog->regflags & RF_ICOMBINE)
5349 ireg_icombine = TRUE;
5350#endif
5351
5352 regline = line;
5353 reglnum = 0; /* relative to line */
5354
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005355 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005356 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005357 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005358 nfa_listid = 1;
5359 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005360#ifdef DEBUG
5361 nfa_regengine.expr = prog->pattern;
5362#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005363
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005364 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005365 for (i = 0; i < nstate; ++i)
5366 {
5367 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005368 prog->state[i].lastlist[0] = 0;
5369 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005370 }
5371
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005372 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005373
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005374#ifdef DEBUG
5375 nfa_regengine.expr = NULL;
5376#endif
5377
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005378theend:
5379 return retval;
5380}
5381
5382/*
5383 * Compile a regular expression into internal code for the NFA matcher.
5384 * Returns the program in allocated space. Returns NULL for an error.
5385 */
5386 static regprog_T *
5387nfa_regcomp(expr, re_flags)
5388 char_u *expr;
5389 int re_flags;
5390{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005391 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005392 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005393 int *postfix;
5394
5395 if (expr == NULL)
5396 return NULL;
5397
5398#ifdef DEBUG
5399 nfa_regengine.expr = expr;
5400#endif
5401
5402 init_class_tab();
5403
5404 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5405 return NULL;
5406
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005407 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005408 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005409 postfix = re2post();
5410 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005411 {
5412 /* TODO: only give this error for debugging? */
5413 if (post_ptr >= post_end)
5414 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005415 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005416 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005417
5418 /*
5419 * In order to build the NFA, we parse the input regexp twice:
5420 * 1. first pass to count size (so we can allocate space)
5421 * 2. second to emit code
5422 */
5423#ifdef ENABLE_LOG
5424 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005425 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005426
5427 if (f != NULL)
5428 {
5429 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5430 fclose(f);
5431 }
5432 }
5433#endif
5434
5435 /*
5436 * PASS 1
5437 * Count number of NFA states in "nstate". Do not build the NFA.
5438 */
5439 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005440
5441 /* Space for compiled regexp */
5442 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5443 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5444 if (prog == NULL)
5445 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005446 state_ptr = prog->state;
5447
5448 /*
5449 * PASS 2
5450 * Build the NFA
5451 */
5452 prog->start = post2nfa(postfix, post_ptr, FALSE);
5453 if (prog->start == NULL)
5454 goto fail;
5455
5456 prog->regflags = regflags;
5457 prog->engine = &nfa_regengine;
5458 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005459 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005460 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005461 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005462#ifdef ENABLE_LOG
5463 nfa_postfix_dump(expr, OK);
5464 nfa_dump(prog);
5465#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005466#ifdef FEAT_SYN_HL
5467 /* Remember whether this pattern has any \z specials in it. */
5468 prog->reghasz = re_has_z;
5469#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005470#ifdef DEBUG
5471 prog->pattern = vim_strsave(expr); /* memory will leak */
5472 nfa_regengine.expr = NULL;
5473#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005474
5475out:
5476 vim_free(post_start);
5477 post_start = post_ptr = post_end = NULL;
5478 state_ptr = NULL;
5479 return (regprog_T *)prog;
5480
5481fail:
5482 vim_free(prog);
5483 prog = NULL;
5484#ifdef ENABLE_LOG
5485 nfa_postfix_dump(expr, FAIL);
5486#endif
5487#ifdef DEBUG
5488 nfa_regengine.expr = NULL;
5489#endif
5490 goto out;
5491}
5492
5493
5494/*
5495 * Match a regexp against a string.
5496 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5497 * Uses curbuf for line count and 'iskeyword'.
5498 *
5499 * Return TRUE if there is a match, FALSE if not.
5500 */
5501 static int
5502nfa_regexec(rmp, line, col)
5503 regmatch_T *rmp;
5504 char_u *line; /* string to match against */
5505 colnr_T col; /* column to start looking for match */
5506{
5507 reg_match = rmp;
5508 reg_mmatch = NULL;
5509 reg_maxline = 0;
5510 reg_line_lbr = FALSE;
5511 reg_buf = curbuf;
5512 reg_win = NULL;
5513 ireg_ic = rmp->rm_ic;
5514#ifdef FEAT_MBYTE
5515 ireg_icombine = FALSE;
5516#endif
5517 ireg_maxcol = 0;
5518 return (nfa_regexec_both(line, col) != 0);
5519}
5520
5521#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5522 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5523
5524static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5525
5526/*
5527 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5528 */
5529 static int
5530nfa_regexec_nl(rmp, line, col)
5531 regmatch_T *rmp;
5532 char_u *line; /* string to match against */
5533 colnr_T col; /* column to start looking for match */
5534{
5535 reg_match = rmp;
5536 reg_mmatch = NULL;
5537 reg_maxline = 0;
5538 reg_line_lbr = TRUE;
5539 reg_buf = curbuf;
5540 reg_win = NULL;
5541 ireg_ic = rmp->rm_ic;
5542#ifdef FEAT_MBYTE
5543 ireg_icombine = FALSE;
5544#endif
5545 ireg_maxcol = 0;
5546 return (nfa_regexec_both(line, col) != 0);
5547}
5548#endif
5549
5550
5551/*
5552 * Match a regexp against multiple lines.
5553 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5554 * Uses curbuf for line count and 'iskeyword'.
5555 *
5556 * Return zero if there is no match. Return number of lines contained in the
5557 * match otherwise.
5558 *
5559 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5560 *
5561 * ! Also NOTE : match may actually be in another line. e.g.:
5562 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5563 *
5564 * +-------------------------+
5565 * |a |
5566 * |b |
5567 * |c |
5568 * | |
5569 * +-------------------------+
5570 *
5571 * then nfa_regexec_multi() returns 3. while the original
5572 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5573 *
5574 * FIXME if this behavior is not compatible.
5575 */
5576 static long
5577nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5578 regmmatch_T *rmp;
5579 win_T *win; /* window in which to search or NULL */
5580 buf_T *buf; /* buffer in which to search */
5581 linenr_T lnum; /* nr of line to start looking for match */
5582 colnr_T col; /* column to start looking for match */
5583 proftime_T *tm UNUSED; /* timeout limit or NULL */
5584{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005585 reg_match = NULL;
5586 reg_mmatch = rmp;
5587 reg_buf = buf;
5588 reg_win = win;
5589 reg_firstlnum = lnum;
5590 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5591 reg_line_lbr = FALSE;
5592 ireg_ic = rmp->rmm_ic;
5593#ifdef FEAT_MBYTE
5594 ireg_icombine = FALSE;
5595#endif
5596 ireg_maxcol = rmp->rmm_maxcol;
5597
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005598 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599}
5600
5601#ifdef DEBUG
5602# undef ENABLE_LOG
5603#endif