blob: 712743c26b9cc4bd7c2a68f3733549098217e4db [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 Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
260static 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;
322 vim_memset(post_start, 0, postfix_size);
323 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200324 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200325 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200326 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200327
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200328 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200329 regcomp_start(expr, re_flags);
330
331 return OK;
332}
333
334/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200335 * Allocate more space for post_start. Called when
336 * running above the estimated number of states.
337 */
338 static int
339realloc_post_list()
340{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200341 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200342 int new_max = nstate_max + 1000;
343 int *new_start;
344 int *old_start;
345
346 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
347 if (new_start == NULL)
348 return FAIL;
349 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
350 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
351 old_start = post_start;
352 post_start = new_start;
353 post_ptr = new_start + (post_ptr - old_start);
354 post_end = post_start + new_max;
355 vim_free(old_start);
356 return OK;
357}
358
359/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200360 * Search between "start" and "end" and try to recognize a
361 * character class in expanded form. For example [0-9].
362 * On success, return the id the character class to be emitted.
363 * On failure, return 0 (=FAIL)
364 * Start points to the first char of the range, while end should point
365 * to the closing brace.
366 */
367 static int
368nfa_recognize_char_class(start, end, extra_newl)
369 char_u *start;
370 char_u *end;
371 int extra_newl;
372{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200373# define CLASS_not 0x80
374# define CLASS_af 0x40
375# define CLASS_AF 0x20
376# define CLASS_az 0x10
377# define CLASS_AZ 0x08
378# define CLASS_o7 0x04
379# define CLASS_o9 0x02
380# define CLASS_underscore 0x01
381
382 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200383 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200384 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200385
386 if (extra_newl == TRUE)
387 newl = TRUE;
388
389 if (*end != ']')
390 return FAIL;
391 p = start;
392 if (*p == '^')
393 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200394 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200395 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200396 }
397
398 while (p < end)
399 {
400 if (p + 2 < end && *(p + 1) == '-')
401 {
402 switch (*p)
403 {
404 case '0':
405 if (*(p + 2) == '9')
406 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200407 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200408 break;
409 }
410 else
411 if (*(p + 2) == '7')
412 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200413 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200414 break;
415 }
416 case 'a':
417 if (*(p + 2) == 'z')
418 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200419 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200420 break;
421 }
422 else
423 if (*(p + 2) == 'f')
424 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200425 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200426 break;
427 }
428 case 'A':
429 if (*(p + 2) == 'Z')
430 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200431 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200432 break;
433 }
434 else
435 if (*(p + 2) == 'F')
436 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200437 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200438 break;
439 }
440 /* FALLTHROUGH */
441 default:
442 return FAIL;
443 }
444 p += 3;
445 }
446 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
447 {
448 newl = TRUE;
449 p += 2;
450 }
451 else if (*p == '_')
452 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200453 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200454 p ++;
455 }
456 else if (*p == '\n')
457 {
458 newl = TRUE;
459 p ++;
460 }
461 else
462 return FAIL;
463 } /* while (p < end) */
464
465 if (p != end)
466 return FAIL;
467
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200468 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200469 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200470
471 switch (config)
472 {
473 case CLASS_o9:
474 return extra_newl + NFA_DIGIT;
475 case CLASS_not | CLASS_o9:
476 return extra_newl + NFA_NDIGIT;
477 case CLASS_af | CLASS_AF | CLASS_o9:
478 return extra_newl + NFA_HEX;
479 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
480 return extra_newl + NFA_NHEX;
481 case CLASS_o7:
482 return extra_newl + NFA_OCTAL;
483 case CLASS_not | CLASS_o7:
484 return extra_newl + NFA_NOCTAL;
485 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
486 return extra_newl + NFA_WORD;
487 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
488 return extra_newl + NFA_NWORD;
489 case CLASS_az | CLASS_AZ | CLASS_underscore:
490 return extra_newl + NFA_HEAD;
491 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
492 return extra_newl + NFA_NHEAD;
493 case CLASS_az | CLASS_AZ:
494 return extra_newl + NFA_ALPHA;
495 case CLASS_not | CLASS_az | CLASS_AZ:
496 return extra_newl + NFA_NALPHA;
497 case CLASS_az:
498 return extra_newl + NFA_LOWER;
499 case CLASS_not | CLASS_az:
500 return extra_newl + NFA_NLOWER;
501 case CLASS_AZ:
502 return extra_newl + NFA_UPPER;
503 case CLASS_not | CLASS_AZ:
504 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200505 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200506 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200507}
508
509/*
510 * Produce the bytes for equivalence class "c".
511 * Currently only handles latin1, latin9 and utf-8.
512 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
513 * equivalent to 'a OR b OR c'
514 *
515 * NOTE! When changing this function, also update reg_equi_class()
516 */
517 static int
518nfa_emit_equi_class(c, neg)
519 int c;
520 int neg;
521{
522 int first = TRUE;
523 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
524#define EMIT2(c) \
525 EMIT(c); \
526 if (neg == TRUE) { \
527 EMIT(NFA_NOT); \
528 } \
529 if (first == FALSE) \
530 EMIT(glue); \
531 else \
532 first = FALSE; \
533
534#ifdef FEAT_MBYTE
535 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
536 || STRCMP(p_enc, "iso-8859-15") == 0)
537#endif
538 {
539 switch (c)
540 {
541 case 'A': case '\300': case '\301': case '\302':
542 case '\303': case '\304': case '\305':
543 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
544 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
545 EMIT2('\305');
546 return OK;
547
548 case 'C': case '\307':
549 EMIT2('C'); EMIT2('\307');
550 return OK;
551
552 case 'E': case '\310': case '\311': case '\312': case '\313':
553 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
554 EMIT2('\312'); EMIT2('\313');
555 return OK;
556
557 case 'I': case '\314': case '\315': case '\316': case '\317':
558 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
559 EMIT2('\316'); EMIT2('\317');
560 return OK;
561
562 case 'N': case '\321':
563 EMIT2('N'); EMIT2('\321');
564 return OK;
565
566 case 'O': case '\322': case '\323': case '\324': case '\325':
567 case '\326':
568 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
569 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
570 return OK;
571
572 case 'U': case '\331': case '\332': case '\333': case '\334':
573 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
574 EMIT2('\333'); EMIT2('\334');
575 return OK;
576
577 case 'Y': case '\335':
578 EMIT2('Y'); EMIT2('\335');
579 return OK;
580
581 case 'a': case '\340': case '\341': case '\342':
582 case '\343': case '\344': case '\345':
583 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
584 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
585 EMIT2('\345');
586 return OK;
587
588 case 'c': case '\347':
589 EMIT2('c'); EMIT2('\347');
590 return OK;
591
592 case 'e': case '\350': case '\351': case '\352': case '\353':
593 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
594 EMIT2('\352'); EMIT2('\353');
595 return OK;
596
597 case 'i': case '\354': case '\355': case '\356': case '\357':
598 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
599 EMIT2('\356'); EMIT2('\357');
600 return OK;
601
602 case 'n': case '\361':
603 EMIT2('n'); EMIT2('\361');
604 return OK;
605
606 case 'o': case '\362': case '\363': case '\364': case '\365':
607 case '\366':
608 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
609 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
610 return OK;
611
612 case 'u': case '\371': case '\372': case '\373': case '\374':
613 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
614 EMIT2('\373'); EMIT2('\374');
615 return OK;
616
617 case 'y': case '\375': case '\377':
618 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
619 return OK;
620
621 default:
622 return FAIL;
623 }
624 }
625
626 EMIT(c);
627 return OK;
628#undef EMIT2
629}
630
631/*
632 * Code to parse regular expression.
633 *
634 * We try to reuse parsing functions in regexp.c to
635 * minimize surprise and keep the syntax consistent.
636 */
637
638/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200639 * Parse the lowest level.
640 *
641 * An atom can be one of a long list of items. Many atoms match one character
642 * in the text. It is often an ordinary character or a character class.
643 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
644 * is only for syntax highlighting.
645 *
646 * atom ::= ordinary-atom
647 * or \( pattern \)
648 * or \%( pattern \)
649 * or \z( pattern \)
650 */
651 static int
652nfa_regatom()
653{
654 int c;
655 int charclass;
656 int equiclass;
657 int collclass;
658 int got_coll_char;
659 char_u *p;
660 char_u *endp;
661#ifdef FEAT_MBYTE
662 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200663#endif
664 int extra = 0;
665 int first;
666 int emit_range;
667 int negated;
668 int result;
669 int startc = -1;
670 int endc = -1;
671 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200672 int glue; /* ID that will "glue" nodes together */
673
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200674 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675 switch (c)
676 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200677 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200678 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
679
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680 case Magic('^'):
681 EMIT(NFA_BOL);
682 break;
683
684 case Magic('$'):
685 EMIT(NFA_EOL);
686#if defined(FEAT_SYN_HL) || defined(PROTO)
687 had_eol = TRUE;
688#endif
689 break;
690
691 case Magic('<'):
692 EMIT(NFA_BOW);
693 break;
694
695 case Magic('>'):
696 EMIT(NFA_EOW);
697 break;
698
699 case Magic('_'):
700 c = no_Magic(getchr());
701 if (c == '^') /* "\_^" is start-of-line */
702 {
703 EMIT(NFA_BOL);
704 break;
705 }
706 if (c == '$') /* "\_$" is end-of-line */
707 {
708 EMIT(NFA_EOL);
709#if defined(FEAT_SYN_HL) || defined(PROTO)
710 had_eol = TRUE;
711#endif
712 break;
713 }
714
715 extra = ADD_NL;
716
717 /* "\_[" is collection plus newline */
718 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200719 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720
721 /* "\_x" is character class plus newline */
722 /*FALLTHROUGH*/
723
724 /*
725 * Character classes.
726 */
727 case Magic('.'):
728 case Magic('i'):
729 case Magic('I'):
730 case Magic('k'):
731 case Magic('K'):
732 case Magic('f'):
733 case Magic('F'):
734 case Magic('p'):
735 case Magic('P'):
736 case Magic('s'):
737 case Magic('S'):
738 case Magic('d'):
739 case Magic('D'):
740 case Magic('x'):
741 case Magic('X'):
742 case Magic('o'):
743 case Magic('O'):
744 case Magic('w'):
745 case Magic('W'):
746 case Magic('h'):
747 case Magic('H'):
748 case Magic('a'):
749 case Magic('A'):
750 case Magic('l'):
751 case Magic('L'):
752 case Magic('u'):
753 case Magic('U'):
754 p = vim_strchr(classchars, no_Magic(c));
755 if (p == NULL)
756 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200757 EMSGN("INTERNAL: Unknown character class char: %ld", c);
758 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200759 }
760#ifdef FEAT_MBYTE
761 /* When '.' is followed by a composing char ignore the dot, so that
762 * the composing char is matched here. */
763 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
764 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200765 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200766 c = getchr();
767 goto nfa_do_multibyte;
768 }
769#endif
770 EMIT(nfa_classcodes[p - classchars]);
771 if (extra == ADD_NL)
772 {
773 EMIT(NFA_NEWL);
774 EMIT(NFA_OR);
775 regflags |= RF_HASNL;
776 }
777 break;
778
779 case Magic('n'):
780 if (reg_string)
781 /* In a string "\n" matches a newline character. */
782 EMIT(NL);
783 else
784 {
785 /* In buffer text "\n" matches the end of a line. */
786 EMIT(NFA_NEWL);
787 regflags |= RF_HASNL;
788 }
789 break;
790
791 case Magic('('):
792 if (nfa_reg(REG_PAREN) == FAIL)
793 return FAIL; /* cascaded error */
794 break;
795
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200796 case Magic('|'):
797 case Magic('&'):
798 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200799 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200800 return FAIL;
801
802 case Magic('='):
803 case Magic('?'):
804 case Magic('+'):
805 case Magic('@'):
806 case Magic('*'):
807 case Magic('{'):
808 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +0200809 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200810 return FAIL;
811
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200812 case Magic('~'):
813 {
814 char_u *lp;
815
816 /* Previous substitute pattern.
817 * Generated as "\%(pattern\)". */
818 if (reg_prev_sub == NULL)
819 {
820 EMSG(_(e_nopresub));
821 return FAIL;
822 }
823 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
824 {
825 EMIT(PTR2CHAR(lp));
826 if (lp != reg_prev_sub)
827 EMIT(NFA_CONCAT);
828 }
829 EMIT(NFA_NOPEN);
830 break;
831 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200832
Bram Moolenaar428e9872013-05-30 17:05:39 +0200833 case Magic('1'):
834 case Magic('2'):
835 case Magic('3'):
836 case Magic('4'):
837 case Magic('5'):
838 case Magic('6'):
839 case Magic('7'):
840 case Magic('8'):
841 case Magic('9'):
842 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
843 nfa_has_backref = TRUE;
844 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200845
846 case Magic('z'):
847 c = no_Magic(getchr());
848 switch (c)
849 {
850 case 's':
851 EMIT(NFA_ZSTART);
852 break;
853 case 'e':
854 EMIT(NFA_ZEND);
855 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200856 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200857#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200858 case '1':
859 case '2':
860 case '3':
861 case '4':
862 case '5':
863 case '6':
864 case '7':
865 case '8':
866 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200867 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200868 if (reg_do_extmatch != REX_USE)
869 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200870 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
871 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +0200872 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200873 re_has_z = REX_USE;
874 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200876 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200877 if (reg_do_extmatch != REX_SET)
878 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200879 if (nfa_reg(REG_ZPAREN) == FAIL)
880 return FAIL; /* cascaded error */
881 re_has_z = REX_SET;
882 break;
883#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 default:
Bram Moolenaarba404472013-05-19 22:31:18 +0200885 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200886 no_Magic(c));
887 return FAIL;
888 }
889 break;
890
891 case Magic('%'):
892 c = no_Magic(getchr());
893 switch (c)
894 {
895 /* () without a back reference */
896 case '(':
897 if (nfa_reg(REG_NPAREN) == FAIL)
898 return FAIL;
899 EMIT(NFA_NOPEN);
900 break;
901
902 case 'd': /* %d123 decimal */
903 case 'o': /* %o123 octal */
904 case 'x': /* %xab hex 2 */
905 case 'u': /* %uabcd hex 4 */
906 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200907 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200908 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200909
Bram Moolenaar47196582013-05-25 22:04:23 +0200910 switch (c)
911 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200912 case 'd': nr = getdecchrs(); break;
913 case 'o': nr = getoctchrs(); break;
914 case 'x': nr = gethexchrs(2); break;
915 case 'u': nr = gethexchrs(4); break;
916 case 'U': nr = gethexchrs(8); break;
917 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200918 }
919
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200920 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200921 EMSG2_RET_FAIL(
922 _("E678: Invalid character after %s%%[dxouU]"),
923 reg_magic == MAGIC_ALL);
924 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200925 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200926 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200927 break;
928
929 /* Catch \%^ and \%$ regardless of where they appear in the
930 * pattern -- regardless of whether or not it makes sense. */
931 case '^':
932 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200933 break;
934
935 case '$':
936 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200937 break;
938
939 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200940 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200941 break;
942
943 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200944 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200945 break;
946
947 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200948 {
949 int n;
950
951 /* \%[abc] */
952 for (n = 0; (c = getchr()) != ']'; ++n)
953 {
954 if (c == NUL)
955 EMSG2_RET_FAIL(_(e_missing_sb),
956 reg_magic == MAGIC_ALL);
957 EMIT(c);
958 }
Bram Moolenaar2976c022013-06-05 21:30:37 +0200959 if (n == 0)
960 EMSG2_RET_FAIL(_(e_empty_sb),
961 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200962 EMIT(NFA_OPT_CHARS);
963 EMIT(n);
964 break;
965 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200966
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200967 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200968 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200969 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200970 int cmp = c;
971
972 if (c == '<' || c == '>')
973 c = getchr();
974 while (VIM_ISDIGIT(c))
975 {
976 n = n * 10 + (c - '0');
977 c = getchr();
978 }
979 if (c == 'l' || c == 'c' || c == 'v')
980 {
Bram Moolenaar423532e2013-05-29 21:14:42 +0200981 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200982 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200983 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200984 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200985 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200986 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200987 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200988 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200989 else
Bram Moolenaar044aa292013-06-04 21:27:38 +0200990 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200991 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200992 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +0200993 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200994 break;
995 }
Bram Moolenaar044aa292013-06-04 21:27:38 +0200996 else if (c == '\'' && n == 0)
997 {
998 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200999 EMIT(cmp == '<' ? NFA_MARK_LT :
1000 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001001 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001002 break;
1003 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001004 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001005 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1006 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001007 return FAIL;
1008 }
1009 break;
1010
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001011 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001012collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001013 /*
1014 * Glue is emitted between several atoms from the [].
1015 * It is either NFA_OR, or NFA_CONCAT.
1016 *
1017 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1018 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1019 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1020 * notation)
1021 *
1022 */
1023
1024
1025/* Emit negation atoms, if needed.
1026 * The CONCAT below merges the NOT with the previous node. */
1027#define TRY_NEG() \
1028 if (negated == TRUE) \
1029 { \
1030 EMIT(NFA_NOT); \
1031 }
1032
1033/* Emit glue between important nodes : CONCAT or OR. */
1034#define EMIT_GLUE() \
1035 if (first == FALSE) \
1036 EMIT(glue); \
1037 else \
1038 first = FALSE;
1039
1040 p = regparse;
1041 endp = skip_anyof(p);
1042 if (*endp == ']')
1043 {
1044 /*
1045 * Try to reverse engineer character classes. For example,
1046 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1047 * and perform the necessary substitutions in the NFA.
1048 */
1049 result = nfa_recognize_char_class(regparse, endp,
1050 extra == ADD_NL);
1051 if (result != FAIL)
1052 {
1053 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1054 EMIT(result);
1055 else /* must be char class + newline */
1056 {
1057 EMIT(result - ADD_NL);
1058 EMIT(NFA_NEWL);
1059 EMIT(NFA_OR);
1060 }
1061 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001062 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001063 return OK;
1064 }
1065 /*
1066 * Failed to recognize a character class. Use the simple
1067 * version that turns [abc] into 'a' OR 'b' OR 'c'
1068 */
1069 startc = endc = oldstartc = -1;
1070 first = TRUE; /* Emitting first atom in this sequence? */
1071 negated = FALSE;
1072 glue = NFA_OR;
1073 if (*regparse == '^') /* negated range */
1074 {
1075 negated = TRUE;
1076 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001077 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001078 }
1079 if (*regparse == '-')
1080 {
1081 startc = '-';
1082 EMIT(startc);
1083 TRY_NEG();
1084 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001085 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 }
1087 /* Emit the OR branches for each character in the [] */
1088 emit_range = FALSE;
1089 while (regparse < endp)
1090 {
1091 oldstartc = startc;
1092 startc = -1;
1093 got_coll_char = FALSE;
1094 if (*regparse == '[')
1095 {
1096 /* Check for [: :], [= =], [. .] */
1097 equiclass = collclass = 0;
1098 charclass = get_char_class(&regparse);
1099 if (charclass == CLASS_NONE)
1100 {
1101 equiclass = get_equi_class(&regparse);
1102 if (equiclass == 0)
1103 collclass = get_coll_element(&regparse);
1104 }
1105
1106 /* Character class like [:alpha:] */
1107 if (charclass != CLASS_NONE)
1108 {
1109 switch (charclass)
1110 {
1111 case CLASS_ALNUM:
1112 EMIT(NFA_CLASS_ALNUM);
1113 break;
1114 case CLASS_ALPHA:
1115 EMIT(NFA_CLASS_ALPHA);
1116 break;
1117 case CLASS_BLANK:
1118 EMIT(NFA_CLASS_BLANK);
1119 break;
1120 case CLASS_CNTRL:
1121 EMIT(NFA_CLASS_CNTRL);
1122 break;
1123 case CLASS_DIGIT:
1124 EMIT(NFA_CLASS_DIGIT);
1125 break;
1126 case CLASS_GRAPH:
1127 EMIT(NFA_CLASS_GRAPH);
1128 break;
1129 case CLASS_LOWER:
1130 EMIT(NFA_CLASS_LOWER);
1131 break;
1132 case CLASS_PRINT:
1133 EMIT(NFA_CLASS_PRINT);
1134 break;
1135 case CLASS_PUNCT:
1136 EMIT(NFA_CLASS_PUNCT);
1137 break;
1138 case CLASS_SPACE:
1139 EMIT(NFA_CLASS_SPACE);
1140 break;
1141 case CLASS_UPPER:
1142 EMIT(NFA_CLASS_UPPER);
1143 break;
1144 case CLASS_XDIGIT:
1145 EMIT(NFA_CLASS_XDIGIT);
1146 break;
1147 case CLASS_TAB:
1148 EMIT(NFA_CLASS_TAB);
1149 break;
1150 case CLASS_RETURN:
1151 EMIT(NFA_CLASS_RETURN);
1152 break;
1153 case CLASS_BACKSPACE:
1154 EMIT(NFA_CLASS_BACKSPACE);
1155 break;
1156 case CLASS_ESCAPE:
1157 EMIT(NFA_CLASS_ESCAPE);
1158 break;
1159 }
1160 TRY_NEG();
1161 EMIT_GLUE();
1162 continue;
1163 }
1164 /* Try equivalence class [=a=] and the like */
1165 if (equiclass != 0)
1166 {
1167 result = nfa_emit_equi_class(equiclass, negated);
1168 if (result == FAIL)
1169 {
1170 /* should never happen */
1171 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1172 }
1173 EMIT_GLUE();
1174 continue;
1175 }
1176 /* Try collating class like [. .] */
1177 if (collclass != 0)
1178 {
1179 startc = collclass; /* allow [.a.]-x as a range */
1180 /* Will emit the proper atom at the end of the
1181 * while loop. */
1182 }
1183 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001184 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1185 * start character. */
1186 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001187 {
1188 emit_range = TRUE;
1189 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001190 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001191 continue; /* reading the end of the range */
1192 }
1193
1194 /* Now handle simple and escaped characters.
1195 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1196 * accepts "\t", "\e", etc., but only when the 'l' flag in
1197 * 'cpoptions' is not included.
1198 * Posix doesn't recognize backslash at all.
1199 */
1200 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001201 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001202 && regparse + 1 <= endp
1203 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001204 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205 && vim_strchr(REGEXP_ABBR, regparse[1])
1206 != NULL)
1207 )
1208 )
1209 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001210 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001211
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001212 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 startc = reg_string ? NL : NFA_NEWL;
1214 else
1215 if (*regparse == 'd'
1216 || *regparse == 'o'
1217 || *regparse == 'x'
1218 || *regparse == 'u'
1219 || *regparse == 'U'
1220 )
1221 {
1222 /* TODO(RE) This needs more testing */
1223 startc = coll_get_char();
1224 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001225 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226 }
1227 else
1228 {
1229 /* \r,\t,\e,\b */
1230 startc = backslash_trans(*regparse);
1231 }
1232 }
1233
1234 /* Normal printable char */
1235 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001236 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001237
1238 /* Previous char was '-', so this char is end of range. */
1239 if (emit_range)
1240 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001241 endc = startc;
1242 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243 if (startc > endc)
1244 EMSG_RET_FAIL(_(e_invrange));
1245#ifdef FEAT_MBYTE
1246 if (has_mbyte && ((*mb_char2len)(startc) > 1
1247 || (*mb_char2len)(endc) > 1))
1248 {
1249 if (endc > startc + 256)
1250 EMSG_RET_FAIL(_(e_invrange));
1251 /* Emit the range. "startc" was already emitted, so
1252 * skip it. */
1253 for (c = startc + 1; c <= endc; c++)
1254 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001255 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256 TRY_NEG();
1257 EMIT_GLUE();
1258 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259 }
1260 else
1261#endif
1262 {
1263#ifdef EBCDIC
1264 int alpha_only = FALSE;
1265
1266 /* for alphabetical range skip the gaps
1267 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1268 if (isalpha(startc) && isalpha(endc))
1269 alpha_only = TRUE;
1270#endif
1271 /* Emit the range. "startc" was already emitted, so
1272 * skip it. */
1273 for (c = startc + 1; c <= endc; c++)
1274#ifdef EBCDIC
1275 if (!alpha_only || isalpha(startc))
1276#endif
1277 {
1278 EMIT(c);
1279 TRY_NEG();
1280 EMIT_GLUE();
1281 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001283 emit_range = FALSE;
1284 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001285 }
1286 else
1287 {
1288 /*
1289 * This char (startc) is not part of a range. Just
1290 * emit it.
1291 *
1292 * Normally, simply emit startc. But if we get char
1293 * code=0 from a collating char, then replace it with
1294 * 0x0a.
1295 *
1296 * This is needed to completely mimic the behaviour of
1297 * the backtracking engine.
1298 */
1299 if (got_coll_char == TRUE && startc == 0)
1300 EMIT(0x0a);
1301 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001302 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001303 TRY_NEG();
1304 EMIT_GLUE();
1305 }
1306
Bram Moolenaar51a29832013-05-28 22:30:35 +02001307 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001308 } /* while (p < endp) */
1309
Bram Moolenaar51a29832013-05-28 22:30:35 +02001310 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001311 if (*regparse == '-') /* if last, '-' is just a char */
1312 {
1313 EMIT('-');
1314 TRY_NEG();
1315 EMIT_GLUE();
1316 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001317 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 /* skip the trailing ] */
1320 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001321 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001322 if (negated == TRUE)
1323 {
1324 /* Mark end of negated char range */
1325 EMIT(NFA_END_NEG_RANGE);
1326 EMIT(NFA_CONCAT);
1327 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001328
1329 /* \_[] also matches \n but it's not negated */
1330 if (extra == ADD_NL)
1331 {
1332 EMIT(reg_string ? NL : NFA_NEWL);
1333 EMIT(NFA_OR);
1334 }
1335
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001337 } /* if exists closing ] */
1338
1339 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001340 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001341 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 default:
1344 {
1345#ifdef FEAT_MBYTE
1346 int plen;
1347
1348nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001349 /* plen is length of current char with composing chars */
1350 if (enc_utf8 && ((*mb_char2len)(c)
1351 != (plen = (*mb_ptr2len)(old_regparse))
1352 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001353 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001354 int i = 0;
1355
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001356 /* A base character plus composing characters, or just one
1357 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001358 * This requires creating a separate atom as if enclosing
1359 * the characters in (), where NFA_COMPOSING is the ( and
1360 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001361 * building the postfix form, not the NFA itself;
1362 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001363 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001364 for (;;)
1365 {
1366 EMIT(c);
1367 if (i > 0)
1368 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001369 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001370 break;
1371 c = utf_ptr2char(old_regparse + i);
1372 }
1373 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001374 regparse = old_regparse + plen;
1375 }
1376 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377#endif
1378 {
1379 c = no_Magic(c);
1380 EMIT(c);
1381 }
1382 return OK;
1383 }
1384 }
1385
1386#undef TRY_NEG
1387#undef EMIT_GLUE
1388
1389 return OK;
1390}
1391
1392/*
1393 * Parse something followed by possible [*+=].
1394 *
1395 * A piece is an atom, possibly followed by a multi, an indication of how many
1396 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1397 * characters: "", "a", "aa", etc.
1398 *
1399 * piece ::= atom
1400 * or atom multi
1401 */
1402 static int
1403nfa_regpiece()
1404{
1405 int i;
1406 int op;
1407 int ret;
1408 long minval, maxval;
1409 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001410 parse_state_T old_state;
1411 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001412 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001413 int old_post_pos;
1414 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001415 int quest;
1416
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001417 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1418 * next. */
1419 save_parse_state(&old_state);
1420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001421 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001422 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423
1424 ret = nfa_regatom();
1425 if (ret == FAIL)
1426 return FAIL; /* cascaded error */
1427
1428 op = peekchr();
1429 if (re_multi_type(op) == NOT_MULTI)
1430 return OK;
1431
1432 skipchr();
1433 switch (op)
1434 {
1435 case Magic('*'):
1436 EMIT(NFA_STAR);
1437 break;
1438
1439 case Magic('+'):
1440 /*
1441 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1442 * first and only submatch would be "aaa". But the backtracking
1443 * engine interprets the plus as "try matching one more time", and
1444 * a* matches a second time at the end of the input, the empty
1445 * string.
1446 * The submatch will the empty string.
1447 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001448 * In order to be consistent with the old engine, we replace
1449 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001451 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 curchr = -1;
1453 if (nfa_regatom() == FAIL)
1454 return FAIL;
1455 EMIT(NFA_STAR);
1456 EMIT(NFA_CONCAT);
1457 skipchr(); /* skip the \+ */
1458 break;
1459
1460 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001461 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001463 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001464 switch(op)
1465 {
1466 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001467 /* \@= */
1468 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001469 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001470 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001471 /* \@! */
1472 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001473 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001475 op = no_Magic(getchr());
1476 if (op == '=')
1477 /* \@<= */
1478 i = NFA_PREV_ATOM_JUST_BEFORE;
1479 else if (op == '!')
1480 /* \@<! */
1481 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1482 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001484 /* \@> */
1485 i = NFA_PREV_ATOM_LIKE_PATTERN;
1486 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001488 if (i == 0)
1489 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001490 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1491 return FAIL;
1492 }
1493 EMIT(i);
1494 if (i == NFA_PREV_ATOM_JUST_BEFORE
1495 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1496 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 break;
1498
1499 case Magic('?'):
1500 case Magic('='):
1501 EMIT(NFA_QUEST);
1502 break;
1503
1504 case Magic('{'):
1505 /* a{2,5} will expand to 'aaa?a?a?'
1506 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1507 * version of '?'
1508 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1509 * parenthesis have the same id
1510 */
1511
1512 greedy = TRUE;
1513 c2 = peekchr();
1514 if (c2 == '-' || c2 == Magic('-'))
1515 {
1516 skipchr();
1517 greedy = FALSE;
1518 }
1519 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001521
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001522 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1523 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001524 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001525 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001526 if (greedy)
1527 /* \{}, \{0,} */
1528 EMIT(NFA_STAR);
1529 else
1530 /* \{-}, \{-0,} */
1531 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 break;
1533 }
1534
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001535 /* Special case: x{0} or x{-0} */
1536 if (maxval == 0)
1537 {
1538 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001539 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1541 EMIT(NFA_SKIP_CHAR);
1542 return OK;
1543 }
1544
1545 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001546 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001547 /* Save parse state after the repeated atom and the \{} */
1548 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1551 for (i = 0; i < maxval; i++)
1552 {
1553 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001554 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001555 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001556 if (nfa_regatom() == FAIL)
1557 return FAIL;
1558 /* after "minval" times, atoms are optional */
1559 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001560 {
1561 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001562 {
1563 if (greedy)
1564 EMIT(NFA_STAR);
1565 else
1566 EMIT(NFA_STAR_NONGREEDY);
1567 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001568 else
1569 EMIT(quest);
1570 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001571 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001572 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001573 if (i + 1 > minval && maxval == MAX_LIMIT)
1574 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001575 }
1576
1577 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001578 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001579 curchr = -1;
1580
1581 break;
1582
1583
1584 default:
1585 break;
1586 } /* end switch */
1587
1588 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001589 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001590 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001591
1592 return OK;
1593}
1594
1595/*
1596 * Parse one or more pieces, concatenated. It matches a match for the
1597 * first piece, followed by a match for the second piece, etc. Example:
1598 * "f[0-9]b", first matches "f", then a digit and then "b".
1599 *
1600 * concat ::= piece
1601 * or piece piece
1602 * or piece piece piece
1603 * etc.
1604 */
1605 static int
1606nfa_regconcat()
1607{
1608 int cont = TRUE;
1609 int first = TRUE;
1610
1611 while (cont)
1612 {
1613 switch (peekchr())
1614 {
1615 case NUL:
1616 case Magic('|'):
1617 case Magic('&'):
1618 case Magic(')'):
1619 cont = FALSE;
1620 break;
1621
1622 case Magic('Z'):
1623#ifdef FEAT_MBYTE
1624 regflags |= RF_ICOMBINE;
1625#endif
1626 skipchr_keepstart();
1627 break;
1628 case Magic('c'):
1629 regflags |= RF_ICASE;
1630 skipchr_keepstart();
1631 break;
1632 case Magic('C'):
1633 regflags |= RF_NOICASE;
1634 skipchr_keepstart();
1635 break;
1636 case Magic('v'):
1637 reg_magic = MAGIC_ALL;
1638 skipchr_keepstart();
1639 curchr = -1;
1640 break;
1641 case Magic('m'):
1642 reg_magic = MAGIC_ON;
1643 skipchr_keepstart();
1644 curchr = -1;
1645 break;
1646 case Magic('M'):
1647 reg_magic = MAGIC_OFF;
1648 skipchr_keepstart();
1649 curchr = -1;
1650 break;
1651 case Magic('V'):
1652 reg_magic = MAGIC_NONE;
1653 skipchr_keepstart();
1654 curchr = -1;
1655 break;
1656
1657 default:
1658 if (nfa_regpiece() == FAIL)
1659 return FAIL;
1660 if (first == FALSE)
1661 EMIT(NFA_CONCAT);
1662 else
1663 first = FALSE;
1664 break;
1665 }
1666 }
1667
1668 return OK;
1669}
1670
1671/*
1672 * Parse a branch, one or more concats, separated by "\&". It matches the
1673 * last concat, but only if all the preceding concats also match at the same
1674 * position. Examples:
1675 * "foobeep\&..." matches "foo" in "foobeep".
1676 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1677 *
1678 * branch ::= concat
1679 * or concat \& concat
1680 * or concat \& concat \& concat
1681 * etc.
1682 */
1683 static int
1684nfa_regbranch()
1685{
1686 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001687 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001688
Bram Moolenaar16299b52013-05-30 18:45:23 +02001689 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001690
1691 /* First branch, possibly the only one */
1692 if (nfa_regconcat() == FAIL)
1693 return FAIL;
1694
1695 ch = peekchr();
1696 /* Try next concats */
1697 while (ch == Magic('&'))
1698 {
1699 skipchr();
1700 EMIT(NFA_NOPEN);
1701 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001702 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001703 if (nfa_regconcat() == FAIL)
1704 return FAIL;
1705 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001706 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707 EMIT(NFA_SKIP_CHAR);
1708 EMIT(NFA_CONCAT);
1709 ch = peekchr();
1710 }
1711
1712 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001713 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001714 EMIT(NFA_SKIP_CHAR);
1715
1716 return OK;
1717}
1718
1719/*
1720 * Parse a pattern, one or more branches, separated by "\|". It matches
1721 * anything that matches one of the branches. Example: "foo\|beep" matches
1722 * "foo" and matches "beep". If more than one branch matches, the first one
1723 * is used.
1724 *
1725 * pattern ::= branch
1726 * or branch \| branch
1727 * or branch \| branch \| branch
1728 * etc.
1729 */
1730 static int
1731nfa_reg(paren)
1732 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1733{
1734 int parno = 0;
1735
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001736 if (paren == REG_PAREN)
1737 {
1738 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740 parno = regnpar++;
1741 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001742#ifdef FEAT_SYN_HL
1743 else if (paren == REG_ZPAREN)
1744 {
1745 /* Make a ZOPEN node. */
1746 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001747 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001748 parno = regnzpar++;
1749 }
1750#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001751
1752 if (nfa_regbranch() == FAIL)
1753 return FAIL; /* cascaded error */
1754
1755 while (peekchr() == Magic('|'))
1756 {
1757 skipchr();
1758 if (nfa_regbranch() == FAIL)
1759 return FAIL; /* cascaded error */
1760 EMIT(NFA_OR);
1761 }
1762
1763 /* Check for proper termination. */
1764 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1765 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 if (paren == REG_NPAREN)
1767 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1768 else
1769 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1770 }
1771 else if (paren == REG_NOPAREN && peekchr() != NUL)
1772 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 if (peekchr() == Magic(')'))
1774 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1775 else
1776 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1777 }
1778 /*
1779 * Here we set the flag allowing back references to this set of
1780 * parentheses.
1781 */
1782 if (paren == REG_PAREN)
1783 {
1784 had_endbrace[parno] = TRUE; /* have seen the close paren */
1785 EMIT(NFA_MOPEN + parno);
1786 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001787#ifdef FEAT_SYN_HL
1788 else if (paren == REG_ZPAREN)
1789 EMIT(NFA_ZOPEN + parno);
1790#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001791
1792 return OK;
1793}
1794
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001795#ifdef DEBUG
1796static char_u code[50];
1797
1798 static void
1799nfa_set_code(c)
1800 int c;
1801{
1802 int addnl = FALSE;
1803
1804 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1805 {
1806 addnl = TRUE;
1807 c -= ADD_NL;
1808 }
1809
1810 STRCPY(code, "");
1811 switch (c)
1812 {
1813 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1814 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1815 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1816 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1817 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1818 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1819
Bram Moolenaar5714b802013-05-28 22:03:20 +02001820 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1821 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1822 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1823 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1824 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1825 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1826 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1827 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1828 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001829#ifdef FEAT_SYN_HL
1830 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1831 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1832 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1833 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1834 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1835 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1836 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1837 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1838 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1839#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001840 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1841
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001842 case NFA_PREV_ATOM_NO_WIDTH:
1843 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001844 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1845 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001846 case NFA_PREV_ATOM_JUST_BEFORE:
1847 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1848 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1849 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001850 case NFA_PREV_ATOM_LIKE_PATTERN:
1851 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
1852
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001853 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1854 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001856 case NFA_START_INVISIBLE_BEFORE:
1857 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001858 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001860 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001861
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1863 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001864 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001865
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001866 case NFA_MOPEN:
1867 case NFA_MOPEN1:
1868 case NFA_MOPEN2:
1869 case NFA_MOPEN3:
1870 case NFA_MOPEN4:
1871 case NFA_MOPEN5:
1872 case NFA_MOPEN6:
1873 case NFA_MOPEN7:
1874 case NFA_MOPEN8:
1875 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001876 STRCPY(code, "NFA_MOPEN(x)");
1877 code[10] = c - NFA_MOPEN + '0';
1878 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001879 case NFA_MCLOSE:
1880 case NFA_MCLOSE1:
1881 case NFA_MCLOSE2:
1882 case NFA_MCLOSE3:
1883 case NFA_MCLOSE4:
1884 case NFA_MCLOSE5:
1885 case NFA_MCLOSE6:
1886 case NFA_MCLOSE7:
1887 case NFA_MCLOSE8:
1888 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889 STRCPY(code, "NFA_MCLOSE(x)");
1890 code[11] = c - NFA_MCLOSE + '0';
1891 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001892#ifdef FEAT_SYN_HL
1893 case NFA_ZOPEN:
1894 case NFA_ZOPEN1:
1895 case NFA_ZOPEN2:
1896 case NFA_ZOPEN3:
1897 case NFA_ZOPEN4:
1898 case NFA_ZOPEN5:
1899 case NFA_ZOPEN6:
1900 case NFA_ZOPEN7:
1901 case NFA_ZOPEN8:
1902 case NFA_ZOPEN9:
1903 STRCPY(code, "NFA_ZOPEN(x)");
1904 code[10] = c - NFA_ZOPEN + '0';
1905 break;
1906 case NFA_ZCLOSE:
1907 case NFA_ZCLOSE1:
1908 case NFA_ZCLOSE2:
1909 case NFA_ZCLOSE3:
1910 case NFA_ZCLOSE4:
1911 case NFA_ZCLOSE5:
1912 case NFA_ZCLOSE6:
1913 case NFA_ZCLOSE7:
1914 case NFA_ZCLOSE8:
1915 case NFA_ZCLOSE9:
1916 STRCPY(code, "NFA_ZCLOSE(x)");
1917 code[11] = c - NFA_ZCLOSE + '0';
1918 break;
1919#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001920 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1921 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1922 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1923 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001924 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1925 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001926 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1927 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1928 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1929 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1930 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1931 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1932 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1933 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1934 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1935 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1936 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1937 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1938 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
1939 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
1940
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001942 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1943 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1944 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1946 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1947 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1949 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1950 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1951 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1952 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1953 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1954 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1955 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1956 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1957 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1958 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1959 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1960 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1961 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1962 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1963 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1964 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1965
1966 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1967 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1968 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1969 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1970 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1971 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1972 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1973 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1974 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1975 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1976 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1977 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1978 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1979 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1980 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1981 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1982 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1983 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1984 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1985 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1986 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1987 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1988 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1989 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1990 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1991 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1992 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1993
1994 default:
1995 STRCPY(code, "CHAR(x)");
1996 code[5] = c;
1997 }
1998
1999 if (addnl == TRUE)
2000 STRCAT(code, " + NEWLINE ");
2001
2002}
2003
2004#ifdef ENABLE_LOG
2005static FILE *log_fd;
2006
2007/*
2008 * Print the postfix notation of the current regexp.
2009 */
2010 static void
2011nfa_postfix_dump(expr, retval)
2012 char_u *expr;
2013 int retval;
2014{
2015 int *p;
2016 FILE *f;
2017
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002018 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002019 if (f != NULL)
2020 {
2021 fprintf(f, "\n-------------------------\n");
2022 if (retval == FAIL)
2023 fprintf(f, ">>> NFA engine failed ... \n");
2024 else if (retval == OK)
2025 fprintf(f, ">>> NFA engine succeeded !\n");
2026 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002027 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002028 {
2029 nfa_set_code(*p);
2030 fprintf(f, "%s, ", code);
2031 }
2032 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002033 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034 fprintf(f, "%d ", *p);
2035 fprintf(f, "\n\n");
2036 fclose(f);
2037 }
2038}
2039
2040/*
2041 * Print the NFA starting with a root node "state".
2042 */
2043 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002044nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002045 FILE *debugf;
2046 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002047{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002048 garray_T indent;
2049
2050 ga_init2(&indent, 1, 64);
2051 ga_append(&indent, '\0');
2052 nfa_print_state2(debugf, state, &indent);
2053 ga_clear(&indent);
2054}
2055
2056 static void
2057nfa_print_state2(debugf, state, indent)
2058 FILE *debugf;
2059 nfa_state_T *state;
2060 garray_T *indent;
2061{
2062 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063
2064 if (state == NULL)
2065 return;
2066
2067 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002068
2069 /* Output indent */
2070 p = (char_u *)indent->ga_data;
2071 if (indent->ga_len >= 3)
2072 {
2073 int last = indent->ga_len - 3;
2074 char_u save[2];
2075
2076 STRNCPY(save, &p[last], 2);
2077 STRNCPY(&p[last], "+-", 2);
2078 fprintf(debugf, " %s", p);
2079 STRNCPY(&p[last], save, 2);
2080 }
2081 else
2082 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083
2084 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002085 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2086 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 if (state->id < 0)
2088 return;
2089
2090 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002091
2092 /* grow indent for state->out */
2093 indent->ga_len -= 1;
2094 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002095 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002096 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002097 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002098 ga_append(indent, '\0');
2099
2100 nfa_print_state2(debugf, state->out, indent);
2101
2102 /* replace last part of indent for state->out1 */
2103 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002104 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002105 ga_append(indent, '\0');
2106
2107 nfa_print_state2(debugf, state->out1, indent);
2108
2109 /* shrink indent */
2110 indent->ga_len -= 3;
2111 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112}
2113
2114/*
2115 * Print the NFA state machine.
2116 */
2117 static void
2118nfa_dump(prog)
2119 nfa_regprog_T *prog;
2120{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002121 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122
2123 if (debugf != NULL)
2124 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002125 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002126 fclose(debugf);
2127 }
2128}
2129#endif /* ENABLE_LOG */
2130#endif /* DEBUG */
2131
2132/*
2133 * Parse r.e. @expr and convert it into postfix form.
2134 * Return the postfix string on success, NULL otherwise.
2135 */
2136 static int *
2137re2post()
2138{
2139 if (nfa_reg(REG_NOPAREN) == FAIL)
2140 return NULL;
2141 EMIT(NFA_MOPEN);
2142 return post_start;
2143}
2144
2145/* NB. Some of the code below is inspired by Russ's. */
2146
2147/*
2148 * Represents an NFA state plus zero or one or two arrows exiting.
2149 * if c == MATCH, no arrows out; matching state.
2150 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2151 * If c < 256, labeled arrow with character c to out.
2152 */
2153
2154static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2155
2156/*
2157 * Allocate and initialize nfa_state_T.
2158 */
2159 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002160alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002161 int c;
2162 nfa_state_T *out;
2163 nfa_state_T *out1;
2164{
2165 nfa_state_T *s;
2166
2167 if (istate >= nstate)
2168 return NULL;
2169
2170 s = &state_ptr[istate++];
2171
2172 s->c = c;
2173 s->out = out;
2174 s->out1 = out1;
2175
2176 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002177 s->lastlist[0] = 0;
2178 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179 s->negated = FALSE;
2180
2181 return s;
2182}
2183
2184/*
2185 * A partially built NFA without the matching state filled in.
2186 * Frag_T.start points at the start state.
2187 * Frag_T.out is a list of places that need to be set to the
2188 * next state for this fragment.
2189 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002190
2191/* Since the out pointers in the list are always
2192 * uninitialized, we use the pointers themselves
2193 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002195union Ptrlist
2196{
2197 Ptrlist *next;
2198 nfa_state_T *s;
2199};
2200
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201struct Frag
2202{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002203 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002204 Ptrlist *out;
2205};
2206typedef struct Frag Frag_T;
2207
2208static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2209static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2210static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2211static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2212static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2213static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2214
2215/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002216 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 */
2218 static Frag_T
2219frag(start, out)
2220 nfa_state_T *start;
2221 Ptrlist *out;
2222{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002223 Frag_T n;
2224
2225 n.start = start;
2226 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002227 return n;
2228}
2229
2230/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002231 * Create singleton list containing just outp.
2232 */
2233 static Ptrlist *
2234list1(outp)
2235 nfa_state_T **outp;
2236{
2237 Ptrlist *l;
2238
2239 l = (Ptrlist *)outp;
2240 l->next = NULL;
2241 return l;
2242}
2243
2244/*
2245 * Patch the list of states at out to point to start.
2246 */
2247 static void
2248patch(l, s)
2249 Ptrlist *l;
2250 nfa_state_T *s;
2251{
2252 Ptrlist *next;
2253
2254 for (; l; l = next)
2255 {
2256 next = l->next;
2257 l->s = s;
2258 }
2259}
2260
2261
2262/*
2263 * Join the two lists l1 and l2, returning the combination.
2264 */
2265 static Ptrlist *
2266append(l1, l2)
2267 Ptrlist *l1;
2268 Ptrlist *l2;
2269{
2270 Ptrlist *oldl1;
2271
2272 oldl1 = l1;
2273 while (l1->next)
2274 l1 = l1->next;
2275 l1->next = l2;
2276 return oldl1;
2277}
2278
2279/*
2280 * Stack used for transforming postfix form into NFA.
2281 */
2282static Frag_T empty;
2283
2284 static void
2285st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002286 int *postfix UNUSED;
2287 int *end UNUSED;
2288 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002290#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002291 FILE *df;
2292 int *p2;
2293
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002294 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 if (df)
2296 {
2297 fprintf(df, "Error popping the stack!\n");
2298#ifdef DEBUG
2299 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2300#endif
2301 fprintf(df, "Postfix form is: ");
2302#ifdef DEBUG
2303 for (p2 = postfix; p2 < end; p2++)
2304 {
2305 nfa_set_code(*p2);
2306 fprintf(df, "%s, ", code);
2307 }
2308 nfa_set_code(*p);
2309 fprintf(df, "\nCurrent position is: ");
2310 for (p2 = postfix; p2 <= p; p2 ++)
2311 {
2312 nfa_set_code(*p2);
2313 fprintf(df, "%s, ", code);
2314 }
2315#else
2316 for (p2 = postfix; p2 < end; p2++)
2317 {
2318 fprintf(df, "%d, ", *p2);
2319 }
2320 fprintf(df, "\nCurrent position is: ");
2321 for (p2 = postfix; p2 <= p; p2 ++)
2322 {
2323 fprintf(df, "%d, ", *p2);
2324 }
2325#endif
2326 fprintf(df, "\n--------------------------\n");
2327 fclose(df);
2328 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002329#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330 EMSG(_("E874: (NFA) Could not pop the stack !"));
2331}
2332
2333/*
2334 * Push an item onto the stack.
2335 */
2336 static void
2337st_push(s, p, stack_end)
2338 Frag_T s;
2339 Frag_T **p;
2340 Frag_T *stack_end;
2341{
2342 Frag_T *stackp = *p;
2343
2344 if (stackp >= stack_end)
2345 return;
2346 *stackp = s;
2347 *p = *p + 1;
2348}
2349
2350/*
2351 * Pop an item from the stack.
2352 */
2353 static Frag_T
2354st_pop(p, stack)
2355 Frag_T **p;
2356 Frag_T *stack;
2357{
2358 Frag_T *stackp;
2359
2360 *p = *p - 1;
2361 stackp = *p;
2362 if (stackp < stack)
2363 return empty;
2364 return **p;
2365}
2366
2367/*
2368 * Convert a postfix form into its equivalent NFA.
2369 * Return the NFA start state on success, NULL otherwise.
2370 */
2371 static nfa_state_T *
2372post2nfa(postfix, end, nfa_calc_size)
2373 int *postfix;
2374 int *end;
2375 int nfa_calc_size;
2376{
2377 int *p;
2378 int mopen;
2379 int mclose;
2380 Frag_T *stack = NULL;
2381 Frag_T *stackp = NULL;
2382 Frag_T *stack_end = NULL;
2383 Frag_T e1;
2384 Frag_T e2;
2385 Frag_T e;
2386 nfa_state_T *s;
2387 nfa_state_T *s1;
2388 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002389 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002390
2391 if (postfix == NULL)
2392 return NULL;
2393
Bram Moolenaar053bb602013-05-20 13:55:21 +02002394#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002395#define POP() st_pop(&stackp, stack); \
2396 if (stackp < stack) \
2397 { \
2398 st_error(postfix, end, p); \
2399 return NULL; \
2400 }
2401
2402 if (nfa_calc_size == FALSE)
2403 {
2404 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002405 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002406 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002407 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002408 }
2409
2410 for (p = postfix; p < end; ++p)
2411 {
2412 switch (*p)
2413 {
2414 case NFA_CONCAT:
2415 /* Catenation.
2416 * Pay attention: this operator does not exist
2417 * in the r.e. itself (it is implicit, really).
2418 * It is added when r.e. is translated to postfix
2419 * form in re2post().
2420 *
2421 * No new state added here. */
2422 if (nfa_calc_size == TRUE)
2423 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002424 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425 break;
2426 }
2427 e2 = POP();
2428 e1 = POP();
2429 patch(e1.out, e2.start);
2430 PUSH(frag(e1.start, e2.out));
2431 break;
2432
2433 case NFA_NOT:
2434 /* Negation of a character */
2435 if (nfa_calc_size == TRUE)
2436 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002437 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002438 break;
2439 }
2440 e1 = POP();
2441 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002442#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002443 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002445#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002446 PUSH(e1);
2447 break;
2448
2449 case NFA_OR:
2450 /* Alternation */
2451 if (nfa_calc_size == TRUE)
2452 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002453 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002454 break;
2455 }
2456 e2 = POP();
2457 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002458 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002460 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461 PUSH(frag(s, append(e1.out, e2.out)));
2462 break;
2463
2464 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002465 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466 if (nfa_calc_size == TRUE)
2467 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002468 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 break;
2470 }
2471 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002472 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002474 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002475 patch(e.out, s);
2476 PUSH(frag(s, list1(&s->out1)));
2477 break;
2478
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002479 case NFA_STAR_NONGREEDY:
2480 /* Zero or more, prefer zero */
2481 if (nfa_calc_size == TRUE)
2482 {
2483 nstate++;
2484 break;
2485 }
2486 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002487 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002488 if (s == NULL)
2489 goto theend;
2490 patch(e.out, s);
2491 PUSH(frag(s, list1(&s->out)));
2492 break;
2493
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494 case NFA_QUEST:
2495 /* one or zero atoms=> greedy match */
2496 if (nfa_calc_size == TRUE)
2497 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002498 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499 break;
2500 }
2501 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002502 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002504 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505 PUSH(frag(s, append(e.out, list1(&s->out1))));
2506 break;
2507
2508 case NFA_QUEST_NONGREEDY:
2509 /* zero or one atoms => non-greedy match */
2510 if (nfa_calc_size == TRUE)
2511 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002512 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002513 break;
2514 }
2515 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002516 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002517 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002518 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 PUSH(frag(s, append(e.out, list1(&s->out))));
2520 break;
2521
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522 case NFA_SKIP_CHAR:
2523 /* Symbol of 0-length, Used in a repetition
2524 * with max/min count of 0 */
2525 if (nfa_calc_size == TRUE)
2526 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002527 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002528 break;
2529 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002530 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002531 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002532 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002533 PUSH(frag(s, list1(&s->out)));
2534 break;
2535
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002536 case NFA_OPT_CHARS:
2537 {
2538 int n;
2539
2540 /* \%[abc] */
2541 n = *++p; /* get number of characters */
2542 if (nfa_calc_size == TRUE)
2543 {
2544 nstate += n;
2545 break;
2546 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002547 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002548 e1.out = NULL; /* stores list with out1's */
2549 s1 = NULL; /* previous NFA_SPLIT to connect to */
2550 while (n-- > 0)
2551 {
2552 e = POP(); /* get character */
2553 s = alloc_state(NFA_SPLIT, e.start, NULL);
2554 if (s == NULL)
2555 goto theend;
2556 if (e1.out == NULL)
2557 e1 = e;
2558 patch(e.out, s1);
2559 append(e1.out, list1(&s->out1));
2560 s1 = s;
2561 }
2562 PUSH(frag(s, e1.out));
2563 break;
2564 }
2565
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002566 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002567 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002568 case NFA_PREV_ATOM_JUST_BEFORE:
2569 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002570 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002571 {
2572 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2573 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2574 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2575 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002576 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
2577 int start_state = NFA_START_INVISIBLE;
2578 int end_state = NFA_END_INVISIBLE;
2579 int n = 0;
2580 nfa_state_T *zend;
2581 nfa_state_T *skip;
2582
2583 if (before)
2584 start_state = NFA_START_INVISIBLE_BEFORE;
2585 else if (pattern)
2586 {
2587 start_state = NFA_START_PATTERN;
2588 end_state = NFA_END_PATTERN;
2589 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002590
2591 if (before)
2592 n = *++p; /* get the count */
2593
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002594 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002595 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002596 * The \@<= operator: match for the preceding atom.
2597 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002598 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002599 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002600
2601 if (nfa_calc_size == TRUE)
2602 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002603 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002604 break;
2605 }
2606 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002607 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002609 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002610
Bram Moolenaar87953742013-06-05 18:52:40 +02002611 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002612 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002613 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002614 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002615 {
2616 s->negated = TRUE;
2617 s1->negated = TRUE;
2618 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002619 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002620 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002621 if (pattern)
2622 {
2623 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2624 skip = alloc_state(NFA_SKIP, NULL, NULL);
2625 zend = alloc_state(NFA_ZEND, s1, NULL);
2626 s1->out= skip;
2627 patch(e.out, zend);
2628 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002629 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002630 else
2631 {
2632 patch(e.out, s1);
2633 PUSH(frag(s, list1(&s1->out)));
2634 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002637
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002638#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002639 case NFA_COMPOSING: /* char with composing char */
2640#if 0
2641 /* TODO */
2642 if (regflags & RF_ICOMBINE)
2643 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002644 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002645 }
2646#endif
2647 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002648#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002649
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002650 case NFA_MOPEN: /* \( \) Submatch */
2651 case NFA_MOPEN1:
2652 case NFA_MOPEN2:
2653 case NFA_MOPEN3:
2654 case NFA_MOPEN4:
2655 case NFA_MOPEN5:
2656 case NFA_MOPEN6:
2657 case NFA_MOPEN7:
2658 case NFA_MOPEN8:
2659 case NFA_MOPEN9:
2660#ifdef FEAT_SYN_HL
2661 case NFA_ZOPEN: /* \z( \) Submatch */
2662 case NFA_ZOPEN1:
2663 case NFA_ZOPEN2:
2664 case NFA_ZOPEN3:
2665 case NFA_ZOPEN4:
2666 case NFA_ZOPEN5:
2667 case NFA_ZOPEN6:
2668 case NFA_ZOPEN7:
2669 case NFA_ZOPEN8:
2670 case NFA_ZOPEN9:
2671#endif
2672 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002673 if (nfa_calc_size == TRUE)
2674 {
2675 nstate += 2;
2676 break;
2677 }
2678
2679 mopen = *p;
2680 switch (*p)
2681 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002682 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2683#ifdef FEAT_SYN_HL
2684 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2685 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2686 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2687 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2688 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2689 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2690 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2691 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2692 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2693 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2694#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002695#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002696 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002697#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002698 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002699 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002700 mclose = *p + NSUBEXP;
2701 break;
2702 }
2703
2704 /* Allow "NFA_MOPEN" as a valid postfix representation for
2705 * the empty regexp "". In this case, the NFA will be
2706 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2707 * empty groups of parenthesis, and empty mbyte chars */
2708 if (stackp == stack)
2709 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002710 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002711 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002712 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002713 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002715 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002716 patch(list1(&s->out), s1);
2717 PUSH(frag(s, list1(&s1->out)));
2718 break;
2719 }
2720
2721 /* At least one node was emitted before NFA_MOPEN, so
2722 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2723 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002724 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002726 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002727
Bram Moolenaar525666f2013-06-02 16:40:55 +02002728 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002729 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002730 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731 patch(e.out, s1);
2732
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002733#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002734 if (mopen == NFA_COMPOSING)
2735 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002736 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002737#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738
2739 PUSH(frag(s, list1(&s1->out)));
2740 break;
2741
Bram Moolenaar5714b802013-05-28 22:03:20 +02002742 case NFA_BACKREF1:
2743 case NFA_BACKREF2:
2744 case NFA_BACKREF3:
2745 case NFA_BACKREF4:
2746 case NFA_BACKREF5:
2747 case NFA_BACKREF6:
2748 case NFA_BACKREF7:
2749 case NFA_BACKREF8:
2750 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002751#ifdef FEAT_SYN_HL
2752 case NFA_ZREF1:
2753 case NFA_ZREF2:
2754 case NFA_ZREF3:
2755 case NFA_ZREF4:
2756 case NFA_ZREF5:
2757 case NFA_ZREF6:
2758 case NFA_ZREF7:
2759 case NFA_ZREF8:
2760 case NFA_ZREF9:
2761#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002762 if (nfa_calc_size == TRUE)
2763 {
2764 nstate += 2;
2765 break;
2766 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002767 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002768 if (s == NULL)
2769 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002770 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002771 if (s1 == NULL)
2772 goto theend;
2773 patch(list1(&s->out), s1);
2774 PUSH(frag(s, list1(&s1->out)));
2775 break;
2776
Bram Moolenaar423532e2013-05-29 21:14:42 +02002777 case NFA_LNUM:
2778 case NFA_LNUM_GT:
2779 case NFA_LNUM_LT:
2780 case NFA_VCOL:
2781 case NFA_VCOL_GT:
2782 case NFA_VCOL_LT:
2783 case NFA_COL:
2784 case NFA_COL_GT:
2785 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002786 case NFA_MARK:
2787 case NFA_MARK_GT:
2788 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002789 {
2790 int n = *++p; /* lnum, col or mark name */
2791
Bram Moolenaar423532e2013-05-29 21:14:42 +02002792 if (nfa_calc_size == TRUE)
2793 {
2794 nstate += 1;
2795 break;
2796 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002797 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002798 if (s == NULL)
2799 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002800 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002801 PUSH(frag(s, list1(&s->out)));
2802 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002803 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002804
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002805 case NFA_ZSTART:
2806 case NFA_ZEND:
2807 default:
2808 /* Operands */
2809 if (nfa_calc_size == TRUE)
2810 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002811 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002812 break;
2813 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002814 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002815 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002816 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002817 PUSH(frag(s, list1(&s->out)));
2818 break;
2819
2820 } /* switch(*p) */
2821
2822 } /* for(p = postfix; *p; ++p) */
2823
2824 if (nfa_calc_size == TRUE)
2825 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002826 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002827 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 }
2829
2830 e = POP();
2831 if (stackp != stack)
2832 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2833
2834 if (istate >= nstate)
2835 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2836
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002837 matchstate = &state_ptr[istate++]; /* the match state */
2838 matchstate->c = NFA_MATCH;
2839 matchstate->out = matchstate->out1 = NULL;
2840
2841 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002842 ret = e.start;
2843
2844theend:
2845 vim_free(stack);
2846 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002847
2848#undef POP1
2849#undef PUSH1
2850#undef POP2
2851#undef PUSH2
2852#undef POP
2853#undef PUSH
2854}
2855
2856/****************************************************************
2857 * NFA execution code.
2858 ****************************************************************/
2859
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002860typedef struct
2861{
2862 int in_use; /* number of subexpr with useful info */
2863
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002864 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002865 union
2866 {
2867 struct multipos
2868 {
2869 lpos_T start;
2870 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002871 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002872 struct linepos
2873 {
2874 char_u *start;
2875 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002876 } line[NSUBEXP];
2877 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002878} regsub_T;
2879
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002880typedef struct
2881{
2882 regsub_T norm; /* \( .. \) matches */
2883#ifdef FEAT_SYN_HL
2884 regsub_T synt; /* \z( .. \) matches */
2885#endif
2886} regsubs_T;
2887
Bram Moolenaara2d95102013-06-04 14:23:05 +02002888/* nfa_pim_T stores a Postponed Invisible Match. */
2889typedef struct nfa_pim_S nfa_pim_T;
2890struct nfa_pim_S
2891{
2892 nfa_state_T *state;
2893 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2894 nfa_pim_T *pim; /* another PIM at the same position */
2895 regsubs_T subs; /* submatch info, only party used */
2896};
2897
2898/* Values for done in nfa_pim_T. */
2899#define NFA_PIM_TODO 0
2900#define NFA_PIM_MATCH 1
2901#define NFA_PIM_NOMATCH -1
2902
2903
Bram Moolenaar963fee22013-05-26 21:47:28 +02002904/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002905typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002906{
2907 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002908 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002909 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002910 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002911} nfa_thread_T;
2912
Bram Moolenaar963fee22013-05-26 21:47:28 +02002913/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914typedef struct
2915{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002916 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002917 int n; /* nr of states currently in "t" */
2918 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002919 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002920} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921
Bram Moolenaar5714b802013-05-28 22:03:20 +02002922#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002923static void log_subsexpr __ARGS((regsubs_T *subs));
2924static void log_subexpr __ARGS((regsub_T *sub));
2925
2926 static void
2927log_subsexpr(subs)
2928 regsubs_T *subs;
2929{
2930 log_subexpr(&subs->norm);
2931# ifdef FEAT_SYN_HL
2932 log_subexpr(&subs->synt);
2933# endif
2934}
2935
Bram Moolenaar5714b802013-05-28 22:03:20 +02002936 static void
2937log_subexpr(sub)
2938 regsub_T *sub;
2939{
2940 int j;
2941
2942 for (j = 0; j < sub->in_use; j++)
2943 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02002944 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002945 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002946 sub->list.multi[j].start.col,
2947 (int)sub->list.multi[j].start.lnum,
2948 sub->list.multi[j].end.col,
2949 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002950 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002951 {
2952 char *s = (char *)sub->list.line[j].start;
2953 char *e = (char *)sub->list.line[j].end;
2954
Bram Moolenaar87953742013-06-05 18:52:40 +02002955 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02002956 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02002957 s == NULL ? "NULL" : s,
2958 e == NULL ? "NULL" : e);
2959 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002960}
2961#endif
2962
Bram Moolenaar963fee22013-05-26 21:47:28 +02002963/* Used during execution: whether a match has been found. */
2964static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002965
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002966static void clear_sub __ARGS((regsub_T *sub));
2967static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2968static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002969static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002970static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02002971static 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 +02002972
2973 static void
2974clear_sub(sub)
2975 regsub_T *sub;
2976{
2977 if (REG_MULTI)
2978 /* Use 0xff to set lnum to -1 */
2979 vim_memset(sub->list.multi, 0xff,
2980 sizeof(struct multipos) * nfa_nsubexpr);
2981 else
2982 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2983 sub->in_use = 0;
2984}
2985
2986/*
2987 * Copy the submatches from "from" to "to".
2988 */
2989 static void
2990copy_sub(to, from)
2991 regsub_T *to;
2992 regsub_T *from;
2993{
2994 to->in_use = from->in_use;
2995 if (from->in_use > 0)
2996 {
2997 /* Copy the match start and end positions. */
2998 if (REG_MULTI)
2999 mch_memmove(&to->list.multi[0],
3000 &from->list.multi[0],
3001 sizeof(struct multipos) * from->in_use);
3002 else
3003 mch_memmove(&to->list.line[0],
3004 &from->list.line[0],
3005 sizeof(struct linepos) * from->in_use);
3006 }
3007}
3008
3009/*
3010 * Like copy_sub() but exclude the main match.
3011 */
3012 static void
3013copy_sub_off(to, from)
3014 regsub_T *to;
3015 regsub_T *from;
3016{
3017 if (to->in_use < from->in_use)
3018 to->in_use = from->in_use;
3019 if (from->in_use > 1)
3020 {
3021 /* Copy the match start and end positions. */
3022 if (REG_MULTI)
3023 mch_memmove(&to->list.multi[1],
3024 &from->list.multi[1],
3025 sizeof(struct multipos) * (from->in_use - 1));
3026 else
3027 mch_memmove(&to->list.line[1],
3028 &from->list.line[1],
3029 sizeof(struct linepos) * (from->in_use - 1));
3030 }
3031}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003032
Bram Moolenaar428e9872013-05-30 17:05:39 +02003033/*
3034 * Return TRUE if "sub1" and "sub2" have the same positions.
3035 */
3036 static int
3037sub_equal(sub1, sub2)
3038 regsub_T *sub1;
3039 regsub_T *sub2;
3040{
3041 int i;
3042 int todo;
3043 linenr_T s1, e1;
3044 linenr_T s2, e2;
3045 char_u *sp1, *ep1;
3046 char_u *sp2, *ep2;
3047
3048 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3049 if (REG_MULTI)
3050 {
3051 for (i = 0; i < todo; ++i)
3052 {
3053 if (i < sub1->in_use)
3054 {
3055 s1 = sub1->list.multi[i].start.lnum;
3056 e1 = sub1->list.multi[i].end.lnum;
3057 }
3058 else
3059 {
3060 s1 = 0;
3061 e1 = 0;
3062 }
3063 if (i < sub2->in_use)
3064 {
3065 s2 = sub2->list.multi[i].start.lnum;
3066 e2 = sub2->list.multi[i].end.lnum;
3067 }
3068 else
3069 {
3070 s2 = 0;
3071 e2 = 0;
3072 }
3073 if (s1 != s2 || e1 != e2)
3074 return FALSE;
3075 if (s1 != 0 && sub1->list.multi[i].start.col
3076 != sub2->list.multi[i].start.col)
3077 return FALSE;
3078 if (e1 != 0 && sub1->list.multi[i].end.col
3079 != sub2->list.multi[i].end.col)
3080 return FALSE;
3081 }
3082 }
3083 else
3084 {
3085 for (i = 0; i < todo; ++i)
3086 {
3087 if (i < sub1->in_use)
3088 {
3089 sp1 = sub1->list.line[i].start;
3090 ep1 = sub1->list.line[i].end;
3091 }
3092 else
3093 {
3094 sp1 = NULL;
3095 ep1 = NULL;
3096 }
3097 if (i < sub2->in_use)
3098 {
3099 sp2 = sub2->list.line[i].start;
3100 ep2 = sub2->list.line[i].end;
3101 }
3102 else
3103 {
3104 sp2 = NULL;
3105 ep2 = NULL;
3106 }
3107 if (sp1 != sp2 || ep1 != ep2)
3108 return FALSE;
3109 }
3110 }
3111
3112 return TRUE;
3113}
3114
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003115#ifdef ENABLE_LOG
3116 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003117report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003118{
3119 int col;
3120
3121 if (sub->in_use <= 0)
3122 col = -1;
3123 else if (REG_MULTI)
3124 col = sub->list.multi[0].start.col;
3125 else
3126 col = (int)(sub->list.line[0].start - regline);
3127 nfa_set_code(state->c);
3128 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3129 action, abs(state->id), lid, state->c, code, col);
3130}
3131#endif
3132
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003133 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003134addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003135 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003136 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003137 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003138 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003139{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003140 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003141 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003142 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003143 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003144 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003145 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003146 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003147#ifdef ENABLE_LOG
3148 int did_print = FALSE;
3149#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003150
3151 if (l == NULL || state == NULL)
3152 return;
3153
3154 switch (state->c)
3155 {
3156 case NFA_SPLIT:
3157 case NFA_NOT:
3158 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003159 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003160 case NFA_NCLOSE:
3161 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003162 case NFA_MCLOSE1:
3163 case NFA_MCLOSE2:
3164 case NFA_MCLOSE3:
3165 case NFA_MCLOSE4:
3166 case NFA_MCLOSE5:
3167 case NFA_MCLOSE6:
3168 case NFA_MCLOSE7:
3169 case NFA_MCLOSE8:
3170 case NFA_MCLOSE9:
3171#ifdef FEAT_SYN_HL
3172 case NFA_ZCLOSE:
3173 case NFA_ZCLOSE1:
3174 case NFA_ZCLOSE2:
3175 case NFA_ZCLOSE3:
3176 case NFA_ZCLOSE4:
3177 case NFA_ZCLOSE5:
3178 case NFA_ZCLOSE6:
3179 case NFA_ZCLOSE7:
3180 case NFA_ZCLOSE8:
3181 case NFA_ZCLOSE9:
3182#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003183 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003184 /* These nodes are not added themselves but their "out" and/or
3185 * "out1" may be added below. */
3186 break;
3187
3188 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003189 case NFA_MOPEN1:
3190 case NFA_MOPEN2:
3191 case NFA_MOPEN3:
3192 case NFA_MOPEN4:
3193 case NFA_MOPEN5:
3194 case NFA_MOPEN6:
3195 case NFA_MOPEN7:
3196 case NFA_MOPEN8:
3197 case NFA_MOPEN9:
3198#ifdef FEAT_SYN_HL
3199 case NFA_ZOPEN:
3200 case NFA_ZOPEN1:
3201 case NFA_ZOPEN2:
3202 case NFA_ZOPEN3:
3203 case NFA_ZOPEN4:
3204 case NFA_ZOPEN5:
3205 case NFA_ZOPEN6:
3206 case NFA_ZOPEN7:
3207 case NFA_ZOPEN8:
3208 case NFA_ZOPEN9:
3209#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003210 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003211 /* These nodes do not need to be added, but we need to bail out
3212 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003213 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003214 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003215 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216 break;
3217
Bram Moolenaar307aa162013-06-02 16:34:21 +02003218 case NFA_BOL:
3219 case NFA_BOF:
3220 /* "^" won't match past end-of-line, don't bother trying.
3221 * Except when we are going to the next line for a look-behind
3222 * match. */
3223 if (reginput > regline
3224 && (nfa_endp == NULL
3225 || !REG_MULTI
3226 || reglnum == nfa_endp->se_u.pos.lnum))
3227 goto skip_add;
3228 /* FALLTHROUGH */
3229
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003230 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003231 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003233 /* This state is already in the list, don't add it again,
3234 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003235 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003236 {
3237skip_add:
3238#ifdef ENABLE_LOG
3239 nfa_set_code(state->c);
3240 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3241 abs(state->id), l->id, state->c, code);
3242#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003243 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003244 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003245
3246 /* See if the same state is already in the list with the same
3247 * positions. */
3248 for (i = 0; i < l->n; ++i)
3249 {
3250 thread = &l->t[i];
3251 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003252 && sub_equal(&thread->subs.norm, &subs->norm)
3253#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003254 && (!nfa_has_zsubexpr ||
3255 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003256#endif
3257 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003258 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003259 }
3260 }
3261
Bram Moolenaara2d95102013-06-04 14:23:05 +02003262 /* when there are backreferences or look-behind matches the number
3263 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003264 if (nfa_has_backref && l->n == l->len)
3265 {
3266 int newlen = l->len * 3 / 2 + 50;
3267
3268 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3269 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003271
3272 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003273 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003274 thread = &l->t[l->n++];
3275 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003276 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003277 copy_sub(&thread->subs.norm, &subs->norm);
3278#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003279 if (nfa_has_zsubexpr)
3280 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003281#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003282#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003283 report_state("Adding", &thread->subs.norm, state, l->id);
3284 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003285#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 }
3287
3288#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003289 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003290 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291#endif
3292 switch (state->c)
3293 {
3294 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003295 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 break;
3297
3298 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003299 /* order matters here */
3300 addstate(l, state->out, subs, off);
3301 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 break;
3303
Bram Moolenaar5714b802013-05-28 22:03:20 +02003304 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 case NFA_NOPEN:
3306 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003307 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 break;
3309
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003310 case NFA_MOPEN:
3311 case NFA_MOPEN1:
3312 case NFA_MOPEN2:
3313 case NFA_MOPEN3:
3314 case NFA_MOPEN4:
3315 case NFA_MOPEN5:
3316 case NFA_MOPEN6:
3317 case NFA_MOPEN7:
3318 case NFA_MOPEN8:
3319 case NFA_MOPEN9:
3320#ifdef FEAT_SYN_HL
3321 case NFA_ZOPEN:
3322 case NFA_ZOPEN1:
3323 case NFA_ZOPEN2:
3324 case NFA_ZOPEN3:
3325 case NFA_ZOPEN4:
3326 case NFA_ZOPEN5:
3327 case NFA_ZOPEN6:
3328 case NFA_ZOPEN7:
3329 case NFA_ZOPEN8:
3330 case NFA_ZOPEN9:
3331#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003334 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003335 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003336 sub = &subs->norm;
3337 }
3338#ifdef FEAT_SYN_HL
3339 else if (state->c >= NFA_ZOPEN)
3340 {
3341 subidx = state->c - NFA_ZOPEN;
3342 sub = &subs->synt;
3343 }
3344#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003345 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003346 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003347 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003348 sub = &subs->norm;
3349 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003351 /* Set the position (with "off") in the subexpression. Save and
3352 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003353 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 if (REG_MULTI)
3355 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003356 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003357 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003358 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003359 save_in_use = -1;
3360 }
3361 else
3362 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003363 save_in_use = sub->in_use;
3364 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003365 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003366 sub->list.multi[i].start.lnum = -1;
3367 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003368 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003369 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003370 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003371 if (off == -1)
3372 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003373 sub->list.multi[subidx].start.lnum = reglnum + 1;
3374 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003375 }
3376 else
3377 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003378 sub->list.multi[subidx].start.lnum = reglnum;
3379 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003380 (colnr_T)(reginput - regline + off);
3381 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003382 }
3383 else
3384 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003385 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003386 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003387 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003388 save_in_use = -1;
3389 }
3390 else
3391 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003392 save_in_use = sub->in_use;
3393 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003394 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003395 sub->list.line[i].start = NULL;
3396 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003397 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003398 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003399 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003400 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003401 }
3402
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003403 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003405 if (save_in_use == -1)
3406 {
3407 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003408 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003409 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003410 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003411 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003413 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003414 break;
3415
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003416 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003417 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003419 /* Do not overwrite the position set by \ze. If no \ze
3420 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003421 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 break;
3423 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003424 case NFA_MCLOSE1:
3425 case NFA_MCLOSE2:
3426 case NFA_MCLOSE3:
3427 case NFA_MCLOSE4:
3428 case NFA_MCLOSE5:
3429 case NFA_MCLOSE6:
3430 case NFA_MCLOSE7:
3431 case NFA_MCLOSE8:
3432 case NFA_MCLOSE9:
3433#ifdef FEAT_SYN_HL
3434 case NFA_ZCLOSE:
3435 case NFA_ZCLOSE1:
3436 case NFA_ZCLOSE2:
3437 case NFA_ZCLOSE3:
3438 case NFA_ZCLOSE4:
3439 case NFA_ZCLOSE5:
3440 case NFA_ZCLOSE6:
3441 case NFA_ZCLOSE7:
3442 case NFA_ZCLOSE8:
3443 case NFA_ZCLOSE9:
3444#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003446 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003447 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003449 sub = &subs->norm;
3450 }
3451#ifdef FEAT_SYN_HL
3452 else if (state->c >= NFA_ZCLOSE)
3453 {
3454 subidx = state->c - NFA_ZCLOSE;
3455 sub = &subs->synt;
3456 }
3457#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003458 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003459 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003460 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003461 sub = &subs->norm;
3462 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003463
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003464 /* We don't fill in gaps here, there must have been an MOPEN that
3465 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003466 save_in_use = sub->in_use;
3467 if (sub->in_use <= subidx)
3468 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 if (REG_MULTI)
3470 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003471 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003472 if (off == -1)
3473 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003474 sub->list.multi[subidx].end.lnum = reglnum + 1;
3475 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003476 }
3477 else
3478 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003479 sub->list.multi[subidx].end.lnum = reglnum;
3480 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003481 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003482 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003483 }
3484 else
3485 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003486 save_ptr = sub->list.line[subidx].end;
3487 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003488 }
3489
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003490 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003491
3492 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003493 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003495 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003496 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003497 break;
3498 }
3499}
3500
3501/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003502 * Like addstate(), but the new state(s) are put at position "*ip".
3503 * Used for zero-width matches, next state to use is the added one.
3504 * This makes sure the order of states to be tried does not change, which
3505 * matters for alternatives.
3506 */
3507 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003508addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003509 nfa_list_T *l; /* runtime state list */
3510 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003511 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003512 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003513 int *ip;
3514{
3515 int tlen = l->n;
3516 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003517 int listidx = *ip;
3518 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003519
3520 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003521 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003522
Bram Moolenaara2d95102013-06-04 14:23:05 +02003523 /* fill in the "pim" field in the new states */
3524 if (pim != NULL)
3525 for (i = tlen; i < l->n; ++i)
3526 l->t[i].pim = pim;
3527
Bram Moolenaar4b417062013-05-25 20:19:50 +02003528 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003529 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003530 return;
3531
3532 /* re-order to put the new state at the current position */
3533 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003534 if (count == 1)
3535 {
3536 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003537 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003538 }
3539 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003540 {
3541 /* make space for new states, then move them from the
3542 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003543 mch_memmove(&(l->t[listidx + count]),
3544 &(l->t[listidx + 1]),
3545 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3546 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003547 &(l->t[l->n - 1]),
3548 sizeof(nfa_thread_T) * count);
3549 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003550 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003551 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003552}
3553
3554/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 * Check character class "class" against current character c.
3556 */
3557 static int
3558check_char_class(class, c)
3559 int class;
3560 int c;
3561{
3562 switch (class)
3563 {
3564 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003565 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003566 return OK;
3567 break;
3568 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003569 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003570 return OK;
3571 break;
3572 case NFA_CLASS_BLANK:
3573 if (c == ' ' || c == '\t')
3574 return OK;
3575 break;
3576 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003577 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578 return OK;
3579 break;
3580 case NFA_CLASS_DIGIT:
3581 if (VIM_ISDIGIT(c))
3582 return OK;
3583 break;
3584 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003585 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 return OK;
3587 break;
3588 case NFA_CLASS_LOWER:
3589 if (MB_ISLOWER(c))
3590 return OK;
3591 break;
3592 case NFA_CLASS_PRINT:
3593 if (vim_isprintc(c))
3594 return OK;
3595 break;
3596 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003597 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003598 return OK;
3599 break;
3600 case NFA_CLASS_SPACE:
3601 if ((c >=9 && c <= 13) || (c == ' '))
3602 return OK;
3603 break;
3604 case NFA_CLASS_UPPER:
3605 if (MB_ISUPPER(c))
3606 return OK;
3607 break;
3608 case NFA_CLASS_XDIGIT:
3609 if (vim_isxdigit(c))
3610 return OK;
3611 break;
3612 case NFA_CLASS_TAB:
3613 if (c == '\t')
3614 return OK;
3615 break;
3616 case NFA_CLASS_RETURN:
3617 if (c == '\r')
3618 return OK;
3619 break;
3620 case NFA_CLASS_BACKSPACE:
3621 if (c == '\b')
3622 return OK;
3623 break;
3624 case NFA_CLASS_ESCAPE:
3625 if (c == '\033')
3626 return OK;
3627 break;
3628
3629 default:
3630 /* should not be here :P */
3631 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3632 }
3633 return FAIL;
3634}
3635
Bram Moolenaar5714b802013-05-28 22:03:20 +02003636static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3637
3638/*
3639 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003640 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003641 */
3642 static int
3643match_backref(sub, subidx, bytelen)
3644 regsub_T *sub; /* pointers to subexpressions */
3645 int subidx;
3646 int *bytelen; /* out: length of match in bytes */
3647{
3648 int len;
3649
3650 if (sub->in_use <= subidx)
3651 {
3652retempty:
3653 /* backref was not set, match an empty string */
3654 *bytelen = 0;
3655 return TRUE;
3656 }
3657
3658 if (REG_MULTI)
3659 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003660 if (sub->list.multi[subidx].start.lnum < 0
3661 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003662 goto retempty;
3663 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003664 len = sub->list.multi[subidx].end.col
3665 - sub->list.multi[subidx].start.col;
3666 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003667 reginput, &len) == 0)
3668 {
3669 *bytelen = len;
3670 return TRUE;
3671 }
3672 }
3673 else
3674 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003675 if (sub->list.line[subidx].start == NULL
3676 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003677 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003678 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3679 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003680 {
3681 *bytelen = len;
3682 return TRUE;
3683 }
3684 }
3685 return FALSE;
3686}
3687
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003688#ifdef FEAT_SYN_HL
3689
3690static int match_zref __ARGS((int subidx, int *bytelen));
3691
3692/*
3693 * Check for a match with \z subexpression "subidx".
3694 * Return TRUE if it matches.
3695 */
3696 static int
3697match_zref(subidx, bytelen)
3698 int subidx;
3699 int *bytelen; /* out: length of match in bytes */
3700{
3701 int len;
3702
3703 cleanup_zsubexpr();
3704 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3705 {
3706 /* backref was not set, match an empty string */
3707 *bytelen = 0;
3708 return TRUE;
3709 }
3710
3711 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3712 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3713 {
3714 *bytelen = len;
3715 return TRUE;
3716 }
3717 return FALSE;
3718}
3719#endif
3720
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003721/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003722 * Save list IDs for all NFA states of "prog" into "list".
3723 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003724 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003725 */
3726 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003727nfa_save_listids(prog, list)
3728 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 int *list;
3730{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003731 int i;
3732 nfa_state_T *p;
3733
3734 /* Order in the list is reverse, it's a bit faster that way. */
3735 p = &prog->state[0];
3736 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003737 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003738 list[i] = p->lastlist[1];
3739 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003740 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741 }
3742}
3743
3744/*
3745 * Restore list IDs from "list" to all NFA states.
3746 */
3747 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003748nfa_restore_listids(prog, list)
3749 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750 int *list;
3751{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003752 int i;
3753 nfa_state_T *p;
3754
3755 p = &prog->state[0];
3756 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003757 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003758 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003759 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760 }
3761}
3762
Bram Moolenaar423532e2013-05-29 21:14:42 +02003763 static int
3764nfa_re_num_cmp(val, op, pos)
3765 long_u val;
3766 int op;
3767 long_u pos;
3768{
3769 if (op == 1) return pos > val;
3770 if (op == 2) return pos < val;
3771 return val == pos;
3772}
3773
Bram Moolenaarf46da702013-06-02 22:37:42 +02003774static 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 +02003775static 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 +02003776
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003777/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003778 * Recursively call nfa_regmatch()
3779 */
3780 static int
3781recursive_regmatch(state, prog, submatch, m, listids)
3782 nfa_state_T *state;
3783 nfa_regprog_T *prog;
3784 regsubs_T *submatch;
3785 regsubs_T *m;
3786 int **listids;
3787{
3788 char_u *save_reginput = reginput;
3789 char_u *save_regline = regline;
3790 int save_reglnum = reglnum;
3791 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003792 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003793 save_se_T *save_nfa_endp = nfa_endp;
3794 save_se_T endpos;
3795 save_se_T *endposp = NULL;
3796 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003797 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003798
3799 if (state->c == NFA_START_INVISIBLE_BEFORE)
3800 {
3801 /* The recursive match must end at the current position. */
3802 endposp = &endpos;
3803 if (REG_MULTI)
3804 {
3805 endpos.se_u.pos.col = (int)(reginput - regline);
3806 endpos.se_u.pos.lnum = reglnum;
3807 }
3808 else
3809 endpos.se_u.ptr = reginput;
3810
3811 /* Go back the specified number of bytes, or as far as the
3812 * start of the previous line, to try matching "\@<=" or
3813 * not matching "\@<!".
3814 * TODO: This is very inefficient! Would be better to
3815 * first check for a match with what follows. */
3816 if (state->val <= 0)
3817 {
3818 if (REG_MULTI)
3819 {
3820 regline = reg_getline(--reglnum);
3821 if (regline == NULL)
3822 /* can't go before the first line */
3823 regline = reg_getline(++reglnum);
3824 }
3825 reginput = regline;
3826 }
3827 else
3828 {
3829 if (REG_MULTI && (int)(reginput - regline) < state->val)
3830 {
3831 /* Not enough bytes in this line, go to end of
3832 * previous line. */
3833 regline = reg_getline(--reglnum);
3834 if (regline == NULL)
3835 {
3836 /* can't go before the first line */
3837 regline = reg_getline(++reglnum);
3838 reginput = regline;
3839 }
3840 else
3841 reginput = regline + STRLEN(regline);
3842 }
3843 if ((int)(reginput - regline) >= state->val)
3844 {
3845 reginput -= state->val;
3846#ifdef FEAT_MBYTE
3847 if (has_mbyte)
3848 reginput -= mb_head_off(regline, reginput);
3849#endif
3850 }
3851 else
3852 reginput = regline;
3853 }
3854 }
3855
Bram Moolenaarf46da702013-06-02 22:37:42 +02003856#ifdef ENABLE_LOG
3857 if (log_fd != stderr)
3858 fclose(log_fd);
3859 log_fd = NULL;
3860#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003861 /* Have to clear the lastlist field of the NFA nodes, so that
3862 * nfa_regmatch() and addstate() can run properly after recursion. */
3863 if (nfa_ll_index == 1)
3864 {
3865 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3866 * values and clear them. */
3867 if (*listids == NULL)
3868 {
3869 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3870 if (*listids == NULL)
3871 {
3872 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3873 return 0;
3874 }
3875 }
3876 nfa_save_listids(prog, *listids);
3877 need_restore = TRUE;
3878 /* any value of nfa_listid will do */
3879 }
3880 else
3881 {
3882 /* First recursive nfa_regmatch() call, switch to the second lastlist
3883 * entry. Make sure nfa_listid is different from a previous recursive
3884 * call, because some states may still have this ID. */
3885 ++nfa_ll_index;
3886 if (nfa_listid <= nfa_alt_listid)
3887 nfa_listid = nfa_alt_listid;
3888 }
3889
3890 /* Call nfa_regmatch() to check if the current concat matches at this
3891 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003892 nfa_endp = endposp;
3893 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003894
3895 if (need_restore)
3896 nfa_restore_listids(prog, *listids);
3897 else
3898 {
3899 --nfa_ll_index;
3900 nfa_alt_listid = nfa_listid;
3901 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003902
3903 /* restore position in input text */
3904 reginput = save_reginput;
3905 regline = save_regline;
3906 reglnum = save_reglnum;
3907 nfa_match = save_nfa_match;
3908 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003909 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003910
3911#ifdef ENABLE_LOG
3912 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3913 if (log_fd != NULL)
3914 {
3915 fprintf(log_fd, "****************************\n");
3916 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3917 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3918 fprintf(log_fd, "****************************\n");
3919 }
3920 else
3921 {
3922 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3923 log_fd = stderr;
3924 }
3925#endif
3926
3927 return result;
3928}
3929
Bram Moolenaara2d95102013-06-04 14:23:05 +02003930static int failure_chance __ARGS((nfa_state_T *state, int depth));
3931
3932/*
3933 * Estimate the chance of a match with "state" failing.
3934 * NFA_ANY: 1
3935 * specific character: 99
3936 */
3937 static int
3938failure_chance(state, depth)
3939 nfa_state_T *state;
3940 int depth;
3941{
3942 int c = state->c;
3943 int l, r;
3944
3945 /* detect looping */
3946 if (depth > 4)
3947 return 1;
3948
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003949 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02003950 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003951 case NFA_SPLIT:
3952 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3953 /* avoid recursive stuff */
3954 return 1;
3955 /* two alternatives, use the lowest failure chance */
3956 l = failure_chance(state->out, depth + 1);
3957 r = failure_chance(state->out1, depth + 1);
3958 return l < r ? l : r;
3959
3960 case NFA_ANY:
3961 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003962 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003963 case NFA_MATCH:
3964 /* empty match works always */
3965 return 0;
3966
3967 case NFA_BOL:
3968 case NFA_EOL:
3969 case NFA_BOF:
3970 case NFA_EOF:
3971 case NFA_NEWL:
3972 return 99;
3973
3974 case NFA_BOW:
3975 case NFA_EOW:
3976 return 90;
3977
3978 case NFA_MOPEN:
3979 case NFA_MOPEN1:
3980 case NFA_MOPEN2:
3981 case NFA_MOPEN3:
3982 case NFA_MOPEN4:
3983 case NFA_MOPEN5:
3984 case NFA_MOPEN6:
3985 case NFA_MOPEN7:
3986 case NFA_MOPEN8:
3987 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02003988#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02003989 case NFA_ZOPEN:
3990 case NFA_ZOPEN1:
3991 case NFA_ZOPEN2:
3992 case NFA_ZOPEN3:
3993 case NFA_ZOPEN4:
3994 case NFA_ZOPEN5:
3995 case NFA_ZOPEN6:
3996 case NFA_ZOPEN7:
3997 case NFA_ZOPEN8:
3998 case NFA_ZOPEN9:
3999 case NFA_ZCLOSE:
4000 case NFA_ZCLOSE1:
4001 case NFA_ZCLOSE2:
4002 case NFA_ZCLOSE3:
4003 case NFA_ZCLOSE4:
4004 case NFA_ZCLOSE5:
4005 case NFA_ZCLOSE6:
4006 case NFA_ZCLOSE7:
4007 case NFA_ZCLOSE8:
4008 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004009#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004010 case NFA_NOPEN:
4011 case NFA_MCLOSE:
4012 case NFA_MCLOSE1:
4013 case NFA_MCLOSE2:
4014 case NFA_MCLOSE3:
4015 case NFA_MCLOSE4:
4016 case NFA_MCLOSE5:
4017 case NFA_MCLOSE6:
4018 case NFA_MCLOSE7:
4019 case NFA_MCLOSE8:
4020 case NFA_MCLOSE9:
4021 case NFA_NCLOSE:
4022 return failure_chance(state->out, depth + 1);
4023
4024 case NFA_BACKREF1:
4025 case NFA_BACKREF2:
4026 case NFA_BACKREF3:
4027 case NFA_BACKREF4:
4028 case NFA_BACKREF5:
4029 case NFA_BACKREF6:
4030 case NFA_BACKREF7:
4031 case NFA_BACKREF8:
4032 case NFA_BACKREF9:
4033#ifdef FEAT_SYN_HL
4034 case NFA_ZREF1:
4035 case NFA_ZREF2:
4036 case NFA_ZREF3:
4037 case NFA_ZREF4:
4038 case NFA_ZREF5:
4039 case NFA_ZREF6:
4040 case NFA_ZREF7:
4041 case NFA_ZREF8:
4042 case NFA_ZREF9:
4043#endif
4044 /* backreferences don't match in many places */
4045 return 94;
4046
4047 case NFA_LNUM_GT:
4048 case NFA_LNUM_LT:
4049 case NFA_COL_GT:
4050 case NFA_COL_LT:
4051 case NFA_VCOL_GT:
4052 case NFA_VCOL_LT:
4053 case NFA_MARK_GT:
4054 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004055 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004056 /* before/after positions don't match very often */
4057 return 85;
4058
4059 case NFA_LNUM:
4060 return 90;
4061
4062 case NFA_CURSOR:
4063 case NFA_COL:
4064 case NFA_VCOL:
4065 case NFA_MARK:
4066 /* specific positions rarely match */
4067 return 98;
4068
4069 case NFA_COMPOSING:
4070 return 95;
4071
4072 default:
4073 if (c > 0)
4074 /* character match fails often */
4075 return 95;
4076 }
4077
4078 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004079 return 50;
4080}
4081
Bram Moolenaarf46da702013-06-02 22:37:42 +02004082/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004083 * Main matching routine.
4084 *
4085 * Run NFA to determine whether it matches reginput.
4086 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004087 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004088 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089 * Return TRUE if there is a match, FALSE otherwise.
4090 * Note: Caller must ensure that: start != NULL.
4091 */
4092 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004093nfa_regmatch(prog, start, submatch, m)
4094 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004096 regsubs_T *submatch;
4097 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004098{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004099 int result;
4100 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004101 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004102 int go_to_nextline = FALSE;
4103 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004104 nfa_list_T list[3];
4105 nfa_list_T *listtbl[2][2];
4106 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004107 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004108 nfa_list_T *thislist;
4109 nfa_list_T *nextlist;
4110 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004111 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004112 nfa_state_T *add_state;
4113 int add_count;
4114 int add_off;
4115 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004116#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004117 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004118
4119 if (debug == NULL)
4120 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004121 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004122 return FALSE;
4123 }
4124#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004125 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004126 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004127
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004128 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004129 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004130 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4131 list[0].len = nstate + 1;
4132 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4133 list[1].len = nstate + 1;
4134 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
4135 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004136 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
4137 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004138
4139#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004140 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004141 if (log_fd != NULL)
4142 {
4143 fprintf(log_fd, "**********************************\n");
4144 nfa_set_code(start->c);
4145 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4146 abs(start->id), code);
4147 fprintf(log_fd, "**********************************\n");
4148 }
4149 else
4150 {
4151 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4152 log_fd = stderr;
4153 }
4154#endif
4155
4156 thislist = &list[0];
4157 thislist->n = 0;
4158 nextlist = &list[1];
4159 nextlist->n = 0;
4160 neglist = &list[2];
4161 neglist->n = 0;
4162#ifdef ENABLE_LOG
4163 fprintf(log_fd, "(---) STARTSTATE\n");
4164#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004165 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004166 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004167
4168 /* There are two cases when the NFA advances: 1. input char matches the
4169 * NFA node and 2. input char does not match the NFA node, but the next
4170 * node is NFA_NOT. The following macro calls addstate() according to
4171 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4172 listtbl[0][0] = NULL;
4173 listtbl[0][1] = neglist;
4174 listtbl[1][0] = nextlist;
4175 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004176#define ADD_POS_NEG_STATE(state) \
4177 ll = listtbl[result ? 1 : 0][state->negated]; \
4178 if (ll != NULL) { \
4179 add_state = state->out; \
4180 add_off = clen; \
4181 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182
4183 /*
4184 * Run for each character.
4185 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004186 for (;;)
4187 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004188 int curc;
4189 int clen;
4190
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004191#ifdef FEAT_MBYTE
4192 if (has_mbyte)
4193 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004194 curc = (*mb_ptr2char)(reginput);
4195 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004196 }
4197 else
4198#endif
4199 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004200 curc = *reginput;
4201 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004202 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004203 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004204 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004205 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004206 go_to_nextline = FALSE;
4207 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208
4209 /* swap lists */
4210 thislist = &list[flag];
4211 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004212 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004213 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004214 ++nfa_listid;
4215 thislist->id = nfa_listid;
4216 nextlist->id = nfa_listid + 1;
4217 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004218
Bram Moolenaara2d95102013-06-04 14:23:05 +02004219 pimlist.ga_len = 0;
4220
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004221#ifdef ENABLE_LOG
4222 fprintf(log_fd, "------------------------------------------\n");
4223 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004224 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004226 {
4227 int i;
4228
4229 for (i = 0; i < thislist->n; i++)
4230 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4231 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004232 fprintf(log_fd, "\n");
4233#endif
4234
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004235#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004236 fprintf(debug, "\n-------------------\n");
4237#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004238 /*
4239 * If the state lists are empty we can stop.
4240 */
4241 if (thislist->n == 0 && neglist->n == 0)
4242 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004243
4244 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004245 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004246 {
4247 if (neglist->n > 0)
4248 {
4249 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004250 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004251 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004252 }
4253 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004254 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004255
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004256#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257 nfa_set_code(t->state->c);
4258 fprintf(debug, "%s, ", code);
4259#endif
4260#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004261 {
4262 int col;
4263
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004264 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004265 col = -1;
4266 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004267 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004268 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004269 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004270 nfa_set_code(t->state->c);
4271 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4272 abs(t->state->id), (int)t->state->c, code, col);
4273 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004274#endif
4275
4276 /*
4277 * Handle the possible codes of the current state.
4278 * The most important is NFA_MATCH.
4279 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004280 add_state = NULL;
4281 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004282 switch (t->state->c)
4283 {
4284 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004285 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004286 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004287 copy_sub(&submatch->norm, &t->subs.norm);
4288#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004289 if (nfa_has_zsubexpr)
4290 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004291#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004292#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004293 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004294#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004295 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004296 * states at this position. When the list of states is going
4297 * to be empty quit without advancing, so that "reginput" is
4298 * correct. */
4299 if (nextlist->n == 0 && neglist->n == 0)
4300 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004301 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004302 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004303
4304 case NFA_END_INVISIBLE:
Bram Moolenaar87953742013-06-05 18:52:40 +02004305 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004306 /*
4307 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004308 * NFA_START_INVISIBLE_BEFORE node.
4309 * They surround a zero-width group, used with "\@=", "\&",
4310 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004311 * If we got here, it means that the current "invisible" group
4312 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004313 * nfa_regmatch(). For a look-behind match only when it ends
4314 * in the position in "nfa_endp".
4315 * Submatches are stored in *m, and used in the parent call.
4316 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004317#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004318 if (nfa_endp != NULL)
4319 {
4320 if (REG_MULTI)
4321 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4322 (int)reglnum,
4323 (int)nfa_endp->se_u.pos.lnum,
4324 (int)(reginput - regline),
4325 nfa_endp->se_u.pos.col);
4326 else
4327 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4328 (int)(reginput - regline),
4329 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004331#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004332 /* If "nfa_endp" is set it's only a match if it ends at
4333 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004334 if (nfa_endp != NULL && (REG_MULTI
4335 ? (reglnum != nfa_endp->se_u.pos.lnum
4336 || (int)(reginput - regline)
4337 != nfa_endp->se_u.pos.col)
4338 : reginput != nfa_endp->se_u.ptr))
4339 break;
4340
4341 /* do not set submatches for \@! */
4342 if (!t->state->negated)
4343 {
4344 copy_sub(&m->norm, &t->subs.norm);
4345#ifdef FEAT_SYN_HL
4346 if (nfa_has_zsubexpr)
4347 copy_sub(&m->synt, &t->subs.synt);
4348#endif
4349 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004350#ifdef ENABLE_LOG
4351 fprintf(log_fd, "Match found:\n");
4352 log_subsexpr(m);
4353#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004354 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004355 break;
4356
4357 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004358 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004359 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004360 nfa_pim_T *pim;
4361 int cout = t->state->out1->out->c;
4362
4363 /* Do it directly when what follows is possibly end of
4364 * match (closing paren).
4365 * Postpone when it is \@<= or \@<!, these are expensive.
4366 * TODO: remove the check for t->pim and check multiple
4367 * where it's used?
4368 * Otherwise first do the one that has the highest chance
4369 * of failing. */
4370 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004371#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004372 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004373#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004374 || cout == NFA_NCLOSE
4375 || t->pim != NULL
4376 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4377 && failure_chance(t->state->out1->out, 0)
4378 < failure_chance(t->state->out, 0)))
4379 {
4380 /*
4381 * First try matching the invisible match, then what
4382 * follows.
4383 */
4384 result = recursive_regmatch(t->state, prog,
4385 submatch, m, &listids);
4386
4387 /* for \@! it is a match when result is FALSE */
4388 if (result != t->state->negated)
4389 {
4390 /* Copy submatch info from the recursive call */
4391 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004392#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004393 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
4444 copy_sub_off(&t->subs.synt, &m->synt);
4445#endif
4446 /* Now we need to skip over the matched text and then
4447 * continue with what follows. */
4448 if (REG_MULTI)
4449 /* TODO: multi-line match */
4450 bytelen = m->norm.list.multi[0].end.col
4451 - (int)(reginput - regline);
4452 else
4453 bytelen = (int)(m->norm.list.line[0].end - reginput);
4454
4455#ifdef ENABLE_LOG
4456 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4457#endif
4458 if (bytelen == 0)
4459 {
4460 /* empty match, output of corresponding
4461 * NFA_END_PATTERN/NFA_SKIP to be used at current
4462 * position */
4463 addstate_here(thislist, t->state->out1->out->out,
4464 &t->subs, t->pim, &listidx);
4465 }
4466 else if (bytelen <= clen)
4467 {
4468 /* match current character, output of corresponding
4469 * NFA_END_PATTERN to be used at next position. */
4470 ll = nextlist;
4471 add_state = t->state->out1->out->out;
4472 add_off = clen;
4473 }
4474 else
4475 {
4476 /* skip over the matched characters, set character
4477 * count in NFA_SKIP */
4478 ll = nextlist;
4479 add_state = t->state->out1->out;
4480 add_off = bytelen;
4481 add_count = bytelen - clen;
4482 }
4483 }
4484 break;
4485
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004486 case NFA_BOL:
4487 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004488 addstate_here(thislist, t->state->out, &t->subs,
4489 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004490 break;
4491
4492 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004493 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004494 addstate_here(thislist, t->state->out, &t->subs,
4495 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004496 break;
4497
4498 case NFA_BOW:
4499 {
4500 int bow = TRUE;
4501
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004502 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004503 bow = FALSE;
4504#ifdef FEAT_MBYTE
4505 else if (has_mbyte)
4506 {
4507 int this_class;
4508
4509 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004510 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004511 if (this_class <= 1)
4512 bow = FALSE;
4513 else if (reg_prev_class() == this_class)
4514 bow = FALSE;
4515 }
4516#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004517 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004518 || (reginput > regline
4519 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004520 bow = FALSE;
4521 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004522 addstate_here(thislist, t->state->out, &t->subs,
4523 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 break;
4525 }
4526
4527 case NFA_EOW:
4528 {
4529 int eow = TRUE;
4530
4531 if (reginput == regline)
4532 eow = FALSE;
4533#ifdef FEAT_MBYTE
4534 else if (has_mbyte)
4535 {
4536 int this_class, prev_class;
4537
4538 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004539 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004540 prev_class = reg_prev_class();
4541 if (this_class == prev_class
4542 || prev_class == 0 || prev_class == 1)
4543 eow = FALSE;
4544 }
4545#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004546 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004547 || (reginput[0] != NUL
4548 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004549 eow = FALSE;
4550 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004551 addstate_here(thislist, t->state->out, &t->subs,
4552 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 break;
4554 }
4555
Bram Moolenaar4b780632013-05-31 22:14:52 +02004556 case NFA_BOF:
4557 if (reglnum == 0 && reginput == regline
4558 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004559 addstate_here(thislist, t->state->out, &t->subs,
4560 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004561 break;
4562
4563 case NFA_EOF:
4564 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004565 addstate_here(thislist, t->state->out, &t->subs,
4566 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004567 break;
4568
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004569#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004570 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004571 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004572 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004573 int len = 0;
4574 nfa_state_T *end;
4575 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004576 int cchars[MAX_MCO];
4577 int ccount = 0;
4578 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004579
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004581 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004582 if (utf_iscomposing(sta->c))
4583 {
4584 /* Only match composing character(s), ignore base
4585 * character. Used for ".{composing}" and "{composing}"
4586 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004587 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004588 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004589 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004590 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004591 /* If \Z was present, then ignore composing characters.
4592 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004593 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004594 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004595 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004596 else
4597 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004598 while (sta->c != NFA_END_COMPOSING)
4599 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004601
4602 /* Check base character matches first, unless ignored. */
4603 else if (len > 0 || mc == sta->c)
4604 {
4605 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004606 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004607 len += mb_char2len(mc);
4608 sta = sta->out;
4609 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004610
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004611 /* We don't care about the order of composing characters.
4612 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004613 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004614 {
4615 mc = mb_ptr2char(reginput + len);
4616 cchars[ccount++] = mc;
4617 len += mb_char2len(mc);
4618 if (ccount == MAX_MCO)
4619 break;
4620 }
4621
4622 /* Check that each composing char in the pattern matches a
4623 * composing char in the text. We do not check if all
4624 * composing chars are matched. */
4625 result = OK;
4626 while (sta->c != NFA_END_COMPOSING)
4627 {
4628 for (j = 0; j < ccount; ++j)
4629 if (cchars[j] == sta->c)
4630 break;
4631 if (j == ccount)
4632 {
4633 result = FAIL;
4634 break;
4635 }
4636 sta = sta->out;
4637 }
4638 }
4639 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004640 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004641
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004642 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004643 ADD_POS_NEG_STATE(end);
4644 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004645 }
4646#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004647
4648 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004649 if (curc == NUL && !reg_line_lbr && REG_MULTI
4650 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004652 go_to_nextline = TRUE;
4653 /* Pass -1 for the offset, which means taking the position
4654 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004655 ll = nextlist;
4656 add_state = t->state->out;
4657 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004658 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004659 else if (curc == '\n' && reg_line_lbr)
4660 {
4661 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004662 ll = nextlist;
4663 add_state = t->state->out;
4664 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004665 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004666 break;
4667
4668 case NFA_CLASS_ALNUM:
4669 case NFA_CLASS_ALPHA:
4670 case NFA_CLASS_BLANK:
4671 case NFA_CLASS_CNTRL:
4672 case NFA_CLASS_DIGIT:
4673 case NFA_CLASS_GRAPH:
4674 case NFA_CLASS_LOWER:
4675 case NFA_CLASS_PRINT:
4676 case NFA_CLASS_PUNCT:
4677 case NFA_CLASS_SPACE:
4678 case NFA_CLASS_UPPER:
4679 case NFA_CLASS_XDIGIT:
4680 case NFA_CLASS_TAB:
4681 case NFA_CLASS_RETURN:
4682 case NFA_CLASS_BACKSPACE:
4683 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004684 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 ADD_POS_NEG_STATE(t->state);
4686 break;
4687
4688 case NFA_END_NEG_RANGE:
4689 /* This follows a series of negated nodes, like:
4690 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004691 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004692 {
4693 ll = nextlist;
4694 add_state = t->state->out;
4695 add_off = clen;
4696 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004697 break;
4698
4699 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004700 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004701 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004702 {
4703 ll = nextlist;
4704 add_state = t->state->out;
4705 add_off = clen;
4706 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004707 break;
4708
4709 /*
4710 * Character classes like \a for alpha, \d for digit etc.
4711 */
4712 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004713 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004714 ADD_POS_NEG_STATE(t->state);
4715 break;
4716
4717 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004718 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004719 ADD_POS_NEG_STATE(t->state);
4720 break;
4721
4722 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004723 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 ADD_POS_NEG_STATE(t->state);
4725 break;
4726
4727 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004728 result = !VIM_ISDIGIT(curc)
4729 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730 ADD_POS_NEG_STATE(t->state);
4731 break;
4732
4733 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004734 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004735 ADD_POS_NEG_STATE(t->state);
4736 break;
4737
4738 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004739 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004740 ADD_POS_NEG_STATE(t->state);
4741 break;
4742
4743 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004744 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004745 ADD_POS_NEG_STATE(t->state);
4746 break;
4747
4748 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004749 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004750 ADD_POS_NEG_STATE(t->state);
4751 break;
4752
4753 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004754 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004755 ADD_POS_NEG_STATE(t->state);
4756 break;
4757
4758 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004759 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004760 ADD_POS_NEG_STATE(t->state);
4761 break;
4762
4763 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004764 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004765 ADD_POS_NEG_STATE(t->state);
4766 break;
4767
4768 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004769 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004770 ADD_POS_NEG_STATE(t->state);
4771 break;
4772
4773 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004774 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004775 ADD_POS_NEG_STATE(t->state);
4776 break;
4777
4778 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004779 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004780 ADD_POS_NEG_STATE(t->state);
4781 break;
4782
4783 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004784 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004785 ADD_POS_NEG_STATE(t->state);
4786 break;
4787
4788 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004789 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004790 ADD_POS_NEG_STATE(t->state);
4791 break;
4792
4793 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004794 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004795 ADD_POS_NEG_STATE(t->state);
4796 break;
4797
4798 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004799 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004800 ADD_POS_NEG_STATE(t->state);
4801 break;
4802
4803 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004804 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004805 ADD_POS_NEG_STATE(t->state);
4806 break;
4807
4808 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004809 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004810 ADD_POS_NEG_STATE(t->state);
4811 break;
4812
4813 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004814 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004815 ADD_POS_NEG_STATE(t->state);
4816 break;
4817
4818 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004819 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004820 ADD_POS_NEG_STATE(t->state);
4821 break;
4822
4823 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004824 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004825 ADD_POS_NEG_STATE(t->state);
4826 break;
4827
4828 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004829 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004830 ADD_POS_NEG_STATE(t->state);
4831 break;
4832
4833 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004834 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004835 ADD_POS_NEG_STATE(t->state);
4836 break;
4837
4838 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004839 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004840 ADD_POS_NEG_STATE(t->state);
4841 break;
4842
Bram Moolenaar5714b802013-05-28 22:03:20 +02004843 case NFA_BACKREF1:
4844 case NFA_BACKREF2:
4845 case NFA_BACKREF3:
4846 case NFA_BACKREF4:
4847 case NFA_BACKREF5:
4848 case NFA_BACKREF6:
4849 case NFA_BACKREF7:
4850 case NFA_BACKREF8:
4851 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004852#ifdef FEAT_SYN_HL
4853 case NFA_ZREF1:
4854 case NFA_ZREF2:
4855 case NFA_ZREF3:
4856 case NFA_ZREF4:
4857 case NFA_ZREF5:
4858 case NFA_ZREF6:
4859 case NFA_ZREF7:
4860 case NFA_ZREF8:
4861 case NFA_ZREF9:
4862#endif
4863 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004864 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004865 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004866 int bytelen;
4867
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004868 if (t->state->c <= NFA_BACKREF9)
4869 {
4870 subidx = t->state->c - NFA_BACKREF1 + 1;
4871 result = match_backref(&t->subs.norm, subidx, &bytelen);
4872 }
4873#ifdef FEAT_SYN_HL
4874 else
4875 {
4876 subidx = t->state->c - NFA_ZREF1 + 1;
4877 result = match_zref(subidx, &bytelen);
4878 }
4879#endif
4880
Bram Moolenaar5714b802013-05-28 22:03:20 +02004881 if (result)
4882 {
4883 if (bytelen == 0)
4884 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004885 /* empty match always works, output of NFA_SKIP to be
4886 * used next */
4887 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004888 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004889 }
4890 else if (bytelen <= clen)
4891 {
4892 /* match current character, jump ahead to out of
4893 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004894 ll = nextlist;
4895 add_state = t->state->out->out;
4896 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004897 }
4898 else
4899 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004900 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004901 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004902 ll = nextlist;
4903 add_state = t->state->out;
4904 add_off = bytelen;
4905 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004906 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004907 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004908 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004909 }
4910 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004911 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004912 if (t->count - clen <= 0)
4913 {
4914 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004915 ll = nextlist;
4916 add_state = t->state->out;
4917 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004918 }
4919 else
4920 {
4921 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004922 ll = nextlist;
4923 add_state = t->state;
4924 add_off = 0;
4925 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004926 }
4927 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004928
Bram Moolenaar423532e2013-05-29 21:14:42 +02004929 case NFA_LNUM:
4930 case NFA_LNUM_GT:
4931 case NFA_LNUM_LT:
4932 result = (REG_MULTI &&
4933 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4934 (long_u)(reglnum + reg_firstlnum)));
4935 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004936 addstate_here(thislist, t->state->out, &t->subs,
4937 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004938 break;
4939
4940 case NFA_COL:
4941 case NFA_COL_GT:
4942 case NFA_COL_LT:
4943 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4944 (long_u)(reginput - regline) + 1);
4945 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004946 addstate_here(thislist, t->state->out, &t->subs,
4947 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004948 break;
4949
4950 case NFA_VCOL:
4951 case NFA_VCOL_GT:
4952 case NFA_VCOL_LT:
4953 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4954 (long_u)win_linetabsize(
4955 reg_win == NULL ? curwin : reg_win,
4956 regline, (colnr_T)(reginput - regline)) + 1);
4957 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004958 addstate_here(thislist, t->state->out, &t->subs,
4959 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004960 break;
4961
Bram Moolenaar044aa292013-06-04 21:27:38 +02004962 case NFA_MARK:
4963 case NFA_MARK_GT:
4964 case NFA_MARK_LT:
4965 {
4966 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
4967
4968 /* Compare the mark position to the match position. */
4969 result = (pos != NULL /* mark doesn't exist */
4970 && pos->lnum > 0 /* mark isn't set in reg_buf */
4971 && (pos->lnum == reglnum + reg_firstlnum
4972 ? (pos->col == (colnr_T)(reginput - regline)
4973 ? t->state->c == NFA_MARK
4974 : (pos->col < (colnr_T)(reginput - regline)
4975 ? t->state->c == NFA_MARK_GT
4976 : t->state->c == NFA_MARK_LT))
4977 : (pos->lnum < reglnum + reg_firstlnum
4978 ? t->state->c == NFA_MARK_GT
4979 : t->state->c == NFA_MARK_LT)));
4980 if (result)
4981 addstate_here(thislist, t->state->out, &t->subs,
4982 t->pim, &listidx);
4983 break;
4984 }
4985
Bram Moolenaar423532e2013-05-29 21:14:42 +02004986 case NFA_CURSOR:
4987 result = (reg_win != NULL
4988 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4989 && ((colnr_T)(reginput - regline)
4990 == reg_win->w_cursor.col));
4991 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004992 addstate_here(thislist, t->state->out, &t->subs,
4993 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004994 break;
4995
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004996 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02004997#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004998 result = reg_match_visual();
4999 if (result)
5000 addstate_here(thislist, t->state->out, &t->subs,
5001 t->pim, &listidx);
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005002#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005003 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005004
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005005 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005006 {
5007 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005008
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005009 /* TODO: put this in #ifdef later */
5010 if (c < -256)
5011 EMSGN("INTERNAL: Negative state char: %ld", c);
5012 if (is_Magic(c))
5013 c = un_Magic(c);
5014 result = (c == curc);
5015
5016 if (!result && ireg_ic)
5017 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005018#ifdef FEAT_MBYTE
5019 /* If there is a composing character which is not being
5020 * ignored there can be no match. Match with composing
5021 * character uses NFA_COMPOSING above. */
5022 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005023 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005024 result = FALSE;
5025#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005026 ADD_POS_NEG_STATE(t->state);
5027 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005028 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005029
5030 } /* switch (t->state->c) */
5031
5032 if (add_state != NULL)
5033 {
5034 if (t->pim != NULL)
5035 {
5036 /* postponed invisible match */
5037 /* TODO: also do t->pim->pim recursively? */
5038 if (t->pim->result == NFA_PIM_TODO)
5039 {
5040#ifdef ENABLE_LOG
5041 fprintf(log_fd, "\n");
5042 fprintf(log_fd, "==================================\n");
5043 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5044 fprintf(log_fd, "\n");
5045#endif
5046 result = recursive_regmatch(t->pim->state,
5047 prog, submatch, m, &listids);
5048 t->pim->result = result ? NFA_PIM_MATCH
5049 : NFA_PIM_NOMATCH;
5050 /* for \@! it is a match when result is FALSE */
5051 if (result != t->pim->state->negated)
5052 {
5053 /* Copy submatch info from the recursive call */
5054 copy_sub_off(&t->pim->subs.norm, &m->norm);
5055#ifdef FEAT_SYN_HL
5056 copy_sub_off(&t->pim->subs.synt, &m->synt);
5057#endif
5058 }
5059 }
5060 else
5061 {
5062 result = (t->pim->result == NFA_PIM_MATCH);
5063#ifdef ENABLE_LOG
5064 fprintf(log_fd, "\n");
5065 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5066 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5067 fprintf(log_fd, "\n");
5068#endif
5069 }
5070
5071 /* for \@! it is a match when result is FALSE */
5072 if (result != t->pim->state->negated)
5073 {
5074 /* Copy submatch info from the recursive call */
5075 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5076#ifdef FEAT_SYN_HL
5077 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
5078#endif
5079 }
5080 else
5081 /* look-behind match failed, don't add the state */
5082 continue;
5083 }
5084
5085 addstate(ll, add_state, &t->subs, add_off);
5086 if (add_count > 0)
5087 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005088 }
5089
5090 } /* for (thislist = thislist; thislist->state; thislist++) */
5091
Bram Moolenaare23febd2013-05-26 18:40:14 +02005092 /* Look for the start of a match in the current position by adding the
5093 * start state to the list of states.
5094 * The first found match is the leftmost one, thus the order of states
5095 * matters!
5096 * Do not add the start state in recursive calls of nfa_regmatch(),
5097 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005098 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005099 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005100 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005101 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005102 && reglnum == 0
5103 && clen != 0
5104 && (ireg_maxcol == 0
5105 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005106 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005107 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005108 ? (reglnum < nfa_endp->se_u.pos.lnum
5109 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005110 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005111 < nfa_endp->se_u.pos.col))
5112 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005113 {
5114#ifdef ENABLE_LOG
5115 fprintf(log_fd, "(---) STARTSTATE\n");
5116#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005117 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005118 }
5119
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005120#ifdef ENABLE_LOG
5121 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005122 {
5123 int i;
5124
5125 for (i = 0; i < thislist->n; i++)
5126 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5127 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005128 fprintf(log_fd, "\n");
5129#endif
5130
5131nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005132 /* Advance to the next character, or advance to the next line, or
5133 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005134 if (clen != 0)
5135 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005136 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5137 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005138 reg_nextline();
5139 else
5140 break;
5141 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005142
5143#ifdef ENABLE_LOG
5144 if (log_fd != stderr)
5145 fclose(log_fd);
5146 log_fd = NULL;
5147#endif
5148
5149theend:
5150 /* Free memory */
5151 vim_free(list[0].t);
5152 vim_free(list[1].t);
5153 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005154 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005155 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005156#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005157#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005158 fclose(debug);
5159#endif
5160
Bram Moolenaar963fee22013-05-26 21:47:28 +02005161 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005162}
5163
5164/*
5165 * Try match of "prog" with at regline["col"].
5166 * Returns 0 for failure, number of lines contained in the match otherwise.
5167 */
5168 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005169nfa_regtry(prog, col)
5170 nfa_regprog_T *prog;
5171 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005172{
5173 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005174 regsubs_T subs, m;
5175 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005176#ifdef ENABLE_LOG
5177 FILE *f;
5178#endif
5179
5180 reginput = regline + col;
5181 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005182#ifdef FEAT_SYN_HL
5183 /* Clear the external match subpointers if necessary. */
5184 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005185 {
5186 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005187 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005188 }
5189 else
5190 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005191#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005192
5193#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005194 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005195 if (f != NULL)
5196 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005197 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005198#ifdef DEBUG
5199 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5200#endif
5201 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005202 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005203 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005204 fprintf(f, "\n\n");
5205 fclose(f);
5206 }
5207 else
5208 EMSG(_("Could not open temporary log file for writing "));
5209#endif
5210
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005211 clear_sub(&subs.norm);
5212 clear_sub(&m.norm);
5213#ifdef FEAT_SYN_HL
5214 clear_sub(&subs.synt);
5215 clear_sub(&m.synt);
5216#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005217
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005218 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005219 return 0;
5220
5221 cleanup_subexpr();
5222 if (REG_MULTI)
5223 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005224 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005225 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005226 reg_startpos[i] = subs.norm.list.multi[i].start;
5227 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005228 }
5229
5230 if (reg_startpos[0].lnum < 0)
5231 {
5232 reg_startpos[0].lnum = 0;
5233 reg_startpos[0].col = col;
5234 }
5235 if (reg_endpos[0].lnum < 0)
5236 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005237 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005238 reg_endpos[0].lnum = reglnum;
5239 reg_endpos[0].col = (int)(reginput - regline);
5240 }
5241 else
5242 /* Use line number of "\ze". */
5243 reglnum = reg_endpos[0].lnum;
5244 }
5245 else
5246 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005247 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005248 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005249 reg_startp[i] = subs.norm.list.line[i].start;
5250 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005251 }
5252
5253 if (reg_startp[0] == NULL)
5254 reg_startp[0] = regline + col;
5255 if (reg_endp[0] == NULL)
5256 reg_endp[0] = reginput;
5257 }
5258
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005259#ifdef FEAT_SYN_HL
5260 /* Package any found \z(...\) matches for export. Default is none. */
5261 unref_extmatch(re_extmatch_out);
5262 re_extmatch_out = NULL;
5263
5264 if (prog->reghasz == REX_SET)
5265 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005266 cleanup_zsubexpr();
5267 re_extmatch_out = make_extmatch();
5268 for (i = 0; i < subs.synt.in_use; i++)
5269 {
5270 if (REG_MULTI)
5271 {
5272 struct multipos *mpos = &subs.synt.list.multi[i];
5273
5274 /* Only accept single line matches. */
5275 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5276 re_extmatch_out->matches[i] =
5277 vim_strnsave(reg_getline(mpos->start.lnum)
5278 + mpos->start.col,
5279 mpos->end.col - mpos->start.col);
5280 }
5281 else
5282 {
5283 struct linepos *lpos = &subs.synt.list.line[i];
5284
5285 if (lpos->start != NULL && lpos->end != NULL)
5286 re_extmatch_out->matches[i] =
5287 vim_strnsave(lpos->start,
5288 (int)(lpos->end - lpos->start));
5289 }
5290 }
5291 }
5292#endif
5293
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005294 return 1 + reglnum;
5295}
5296
5297/*
5298 * Match a regexp against a string ("line" points to the string) or multiple
5299 * lines ("line" is NULL, use reg_getline()).
5300 *
5301 * Returns 0 for failure, number of lines contained in the match otherwise.
5302 */
5303 static long
5304nfa_regexec_both(line, col)
5305 char_u *line;
5306 colnr_T col; /* column to start looking for match */
5307{
5308 nfa_regprog_T *prog;
5309 long retval = 0L;
5310 int i;
5311
5312 if (REG_MULTI)
5313 {
5314 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5315 line = reg_getline((linenr_T)0); /* relative to the cursor */
5316 reg_startpos = reg_mmatch->startpos;
5317 reg_endpos = reg_mmatch->endpos;
5318 }
5319 else
5320 {
5321 prog = (nfa_regprog_T *)reg_match->regprog;
5322 reg_startp = reg_match->startp;
5323 reg_endp = reg_match->endp;
5324 }
5325
5326 /* Be paranoid... */
5327 if (prog == NULL || line == NULL)
5328 {
5329 EMSG(_(e_null));
5330 goto theend;
5331 }
5332
5333 /* If the start column is past the maximum column: no need to try. */
5334 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5335 goto theend;
5336
5337 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5338 if (prog->regflags & RF_ICASE)
5339 ireg_ic = TRUE;
5340 else if (prog->regflags & RF_NOICASE)
5341 ireg_ic = FALSE;
5342
5343#ifdef FEAT_MBYTE
5344 /* If pattern contains "\Z" overrule value of ireg_icombine */
5345 if (prog->regflags & RF_ICOMBINE)
5346 ireg_icombine = TRUE;
5347#endif
5348
5349 regline = line;
5350 reglnum = 0; /* relative to line */
5351
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005352 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005353 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005354 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005355 nfa_listid = 1;
5356 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005357#ifdef DEBUG
5358 nfa_regengine.expr = prog->pattern;
5359#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005360
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005361 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005362 for (i = 0; i < nstate; ++i)
5363 {
5364 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005365 prog->state[i].lastlist[0] = 0;
5366 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005367 }
5368
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005369 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005370
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005371#ifdef DEBUG
5372 nfa_regengine.expr = NULL;
5373#endif
5374
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005375theend:
5376 return retval;
5377}
5378
5379/*
5380 * Compile a regular expression into internal code for the NFA matcher.
5381 * Returns the program in allocated space. Returns NULL for an error.
5382 */
5383 static regprog_T *
5384nfa_regcomp(expr, re_flags)
5385 char_u *expr;
5386 int re_flags;
5387{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005388 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005389 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005390 int *postfix;
5391
5392 if (expr == NULL)
5393 return NULL;
5394
5395#ifdef DEBUG
5396 nfa_regengine.expr = expr;
5397#endif
5398
5399 init_class_tab();
5400
5401 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5402 return NULL;
5403
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005404 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005405 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005406 postfix = re2post();
5407 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005408 {
5409 /* TODO: only give this error for debugging? */
5410 if (post_ptr >= post_end)
5411 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005412 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005413 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005414
5415 /*
5416 * In order to build the NFA, we parse the input regexp twice:
5417 * 1. first pass to count size (so we can allocate space)
5418 * 2. second to emit code
5419 */
5420#ifdef ENABLE_LOG
5421 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005422 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005423
5424 if (f != NULL)
5425 {
5426 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5427 fclose(f);
5428 }
5429 }
5430#endif
5431
5432 /*
5433 * PASS 1
5434 * Count number of NFA states in "nstate". Do not build the NFA.
5435 */
5436 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005437
5438 /* Space for compiled regexp */
5439 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5440 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5441 if (prog == NULL)
5442 goto fail;
5443 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444 state_ptr = prog->state;
5445
5446 /*
5447 * PASS 2
5448 * Build the NFA
5449 */
5450 prog->start = post2nfa(postfix, post_ptr, FALSE);
5451 if (prog->start == NULL)
5452 goto fail;
5453
5454 prog->regflags = regflags;
5455 prog->engine = &nfa_regengine;
5456 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005457 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005458 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005459 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005460#ifdef ENABLE_LOG
5461 nfa_postfix_dump(expr, OK);
5462 nfa_dump(prog);
5463#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005464#ifdef FEAT_SYN_HL
5465 /* Remember whether this pattern has any \z specials in it. */
5466 prog->reghasz = re_has_z;
5467#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005468#ifdef DEBUG
5469 prog->pattern = vim_strsave(expr); /* memory will leak */
5470 nfa_regengine.expr = NULL;
5471#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005472
5473out:
5474 vim_free(post_start);
5475 post_start = post_ptr = post_end = NULL;
5476 state_ptr = NULL;
5477 return (regprog_T *)prog;
5478
5479fail:
5480 vim_free(prog);
5481 prog = NULL;
5482#ifdef ENABLE_LOG
5483 nfa_postfix_dump(expr, FAIL);
5484#endif
5485#ifdef DEBUG
5486 nfa_regengine.expr = NULL;
5487#endif
5488 goto out;
5489}
5490
5491
5492/*
5493 * Match a regexp against a string.
5494 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5495 * Uses curbuf for line count and 'iskeyword'.
5496 *
5497 * Return TRUE if there is a match, FALSE if not.
5498 */
5499 static int
5500nfa_regexec(rmp, line, col)
5501 regmatch_T *rmp;
5502 char_u *line; /* string to match against */
5503 colnr_T col; /* column to start looking for match */
5504{
5505 reg_match = rmp;
5506 reg_mmatch = NULL;
5507 reg_maxline = 0;
5508 reg_line_lbr = FALSE;
5509 reg_buf = curbuf;
5510 reg_win = NULL;
5511 ireg_ic = rmp->rm_ic;
5512#ifdef FEAT_MBYTE
5513 ireg_icombine = FALSE;
5514#endif
5515 ireg_maxcol = 0;
5516 return (nfa_regexec_both(line, col) != 0);
5517}
5518
5519#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5520 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5521
5522static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5523
5524/*
5525 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5526 */
5527 static int
5528nfa_regexec_nl(rmp, line, col)
5529 regmatch_T *rmp;
5530 char_u *line; /* string to match against */
5531 colnr_T col; /* column to start looking for match */
5532{
5533 reg_match = rmp;
5534 reg_mmatch = NULL;
5535 reg_maxline = 0;
5536 reg_line_lbr = TRUE;
5537 reg_buf = curbuf;
5538 reg_win = NULL;
5539 ireg_ic = rmp->rm_ic;
5540#ifdef FEAT_MBYTE
5541 ireg_icombine = FALSE;
5542#endif
5543 ireg_maxcol = 0;
5544 return (nfa_regexec_both(line, col) != 0);
5545}
5546#endif
5547
5548
5549/*
5550 * Match a regexp against multiple lines.
5551 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5552 * Uses curbuf for line count and 'iskeyword'.
5553 *
5554 * Return zero if there is no match. Return number of lines contained in the
5555 * match otherwise.
5556 *
5557 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5558 *
5559 * ! Also NOTE : match may actually be in another line. e.g.:
5560 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5561 *
5562 * +-------------------------+
5563 * |a |
5564 * |b |
5565 * |c |
5566 * | |
5567 * +-------------------------+
5568 *
5569 * then nfa_regexec_multi() returns 3. while the original
5570 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5571 *
5572 * FIXME if this behavior is not compatible.
5573 */
5574 static long
5575nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5576 regmmatch_T *rmp;
5577 win_T *win; /* window in which to search or NULL */
5578 buf_T *buf; /* buffer in which to search */
5579 linenr_T lnum; /* nr of line to start looking for match */
5580 colnr_T col; /* column to start looking for match */
5581 proftime_T *tm UNUSED; /* timeout limit or NULL */
5582{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005583 reg_match = NULL;
5584 reg_mmatch = rmp;
5585 reg_buf = buf;
5586 reg_win = win;
5587 reg_firstlnum = lnum;
5588 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5589 reg_line_lbr = FALSE;
5590 ireg_ic = rmp->rmm_ic;
5591#ifdef FEAT_MBYTE
5592 ireg_icombine = FALSE;
5593#endif
5594 ireg_maxcol = rmp->rmm_maxcol;
5595
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005596 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597}
5598
5599#ifdef DEBUG
5600# undef ENABLE_LOG
5601#endif