blob: fc87e39fb34190480a7aa91f83126313daed8f3e [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 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020037
Bram Moolenaar417bad22013-06-07 14:08:30 +020038 NFA_START_COLL, /* [abc] start */
39 NFA_END_COLL, /* [abc] end */
40 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020041 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
42 NFA_RANGE, /* range of the two previous items
43 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020044 NFA_RANGE_MIN, /* low end of a range */
45 NFA_RANGE_MAX, /* high end of a range */
46
Bram Moolenaare7766ee2013-06-08 22:30:03 +020047 NFA_CONCAT, /* concatenate two previous items (postfix
48 * only) */
49 NFA_OR, /* \| (postfix only) */
50 NFA_STAR, /* greedy * (posfix only) */
51 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
52 NFA_QUEST, /* greedy \? (postfix only) */
53 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020054
55 NFA_BOL, /* ^ Begin line */
56 NFA_EOL, /* $ End line */
57 NFA_BOW, /* \< Begin word */
58 NFA_EOW, /* \> End word */
59 NFA_BOF, /* \%^ Begin file */
60 NFA_EOF, /* \%$ End file */
61 NFA_NEWL,
62 NFA_ZSTART, /* Used for \zs */
63 NFA_ZEND, /* Used for \ze */
64 NFA_NOPEN, /* Start of subexpression marked with \%( */
65 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
66 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020067 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020068 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020069 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020070 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020071 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020072 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020073 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020074 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020075 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020076 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_COMPOSING, /* Next nodes in NFA are part of the
79 composing multibyte char */
80 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020081 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020082
83 /* The following are used only in the postfix form, not in the NFA */
84 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
85 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
86 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
87 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
88 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
89
Bram Moolenaar5714b802013-05-28 22:03:20 +020090 NFA_BACKREF1, /* \1 */
91 NFA_BACKREF2, /* \2 */
92 NFA_BACKREF3, /* \3 */
93 NFA_BACKREF4, /* \4 */
94 NFA_BACKREF5, /* \5 */
95 NFA_BACKREF6, /* \6 */
96 NFA_BACKREF7, /* \7 */
97 NFA_BACKREF8, /* \8 */
98 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020099#ifdef FEAT_SYN_HL
100 NFA_ZREF1, /* \z1 */
101 NFA_ZREF2, /* \z2 */
102 NFA_ZREF3, /* \z3 */
103 NFA_ZREF4, /* \z4 */
104 NFA_ZREF5, /* \z5 */
105 NFA_ZREF6, /* \z6 */
106 NFA_ZREF7, /* \z7 */
107 NFA_ZREF8, /* \z8 */
108 NFA_ZREF9, /* \z9 */
109#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200110 NFA_SKIP, /* Skip characters */
111
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200112 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200113 NFA_MOPEN1,
114 NFA_MOPEN2,
115 NFA_MOPEN3,
116 NFA_MOPEN4,
117 NFA_MOPEN5,
118 NFA_MOPEN6,
119 NFA_MOPEN7,
120 NFA_MOPEN8,
121 NFA_MOPEN9,
122
123 NFA_MCLOSE,
124 NFA_MCLOSE1,
125 NFA_MCLOSE2,
126 NFA_MCLOSE3,
127 NFA_MCLOSE4,
128 NFA_MCLOSE5,
129 NFA_MCLOSE6,
130 NFA_MCLOSE7,
131 NFA_MCLOSE8,
132 NFA_MCLOSE9,
133
134#ifdef FEAT_SYN_HL
135 NFA_ZOPEN,
136 NFA_ZOPEN1,
137 NFA_ZOPEN2,
138 NFA_ZOPEN3,
139 NFA_ZOPEN4,
140 NFA_ZOPEN5,
141 NFA_ZOPEN6,
142 NFA_ZOPEN7,
143 NFA_ZOPEN8,
144 NFA_ZOPEN9,
145
146 NFA_ZCLOSE,
147 NFA_ZCLOSE1,
148 NFA_ZCLOSE2,
149 NFA_ZCLOSE3,
150 NFA_ZCLOSE4,
151 NFA_ZCLOSE5,
152 NFA_ZCLOSE6,
153 NFA_ZCLOSE7,
154 NFA_ZCLOSE8,
155 NFA_ZCLOSE9,
156#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200157
158 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200159 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200160 NFA_IDENT, /* Match identifier char */
161 NFA_SIDENT, /* Match identifier char but no digit */
162 NFA_KWORD, /* Match keyword char */
163 NFA_SKWORD, /* Match word char but no digit */
164 NFA_FNAME, /* Match file name char */
165 NFA_SFNAME, /* Match file name char but no digit */
166 NFA_PRINT, /* Match printable char */
167 NFA_SPRINT, /* Match printable char but no digit */
168 NFA_WHITE, /* Match whitespace char */
169 NFA_NWHITE, /* Match non-whitespace char */
170 NFA_DIGIT, /* Match digit char */
171 NFA_NDIGIT, /* Match non-digit char */
172 NFA_HEX, /* Match hex char */
173 NFA_NHEX, /* Match non-hex char */
174 NFA_OCTAL, /* Match octal char */
175 NFA_NOCTAL, /* Match non-octal char */
176 NFA_WORD, /* Match word char */
177 NFA_NWORD, /* Match non-word char */
178 NFA_HEAD, /* Match head char */
179 NFA_NHEAD, /* Match non-head char */
180 NFA_ALPHA, /* Match alpha char */
181 NFA_NALPHA, /* Match non-alpha char */
182 NFA_LOWER, /* Match lowercase char */
183 NFA_NLOWER, /* Match non-lowercase char */
184 NFA_UPPER, /* Match uppercase char */
185 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200186
187 NFA_CURSOR, /* Match cursor pos */
188 NFA_LNUM, /* Match line number */
189 NFA_LNUM_GT, /* Match > line number */
190 NFA_LNUM_LT, /* Match < line number */
191 NFA_COL, /* Match cursor column */
192 NFA_COL_GT, /* Match > cursor column */
193 NFA_COL_LT, /* Match < cursor column */
194 NFA_VCOL, /* Match cursor virtual column */
195 NFA_VCOL_GT, /* Match > cursor virtual column */
196 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200197 NFA_MARK, /* Match mark */
198 NFA_MARK_GT, /* Match > mark */
199 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200200 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200201
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200202 NFA_FIRST_NL = NFA_ANY + ADD_NL,
203 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
204
205 /* Character classes [:alnum:] etc */
206 NFA_CLASS_ALNUM,
207 NFA_CLASS_ALPHA,
208 NFA_CLASS_BLANK,
209 NFA_CLASS_CNTRL,
210 NFA_CLASS_DIGIT,
211 NFA_CLASS_GRAPH,
212 NFA_CLASS_LOWER,
213 NFA_CLASS_PRINT,
214 NFA_CLASS_PUNCT,
215 NFA_CLASS_SPACE,
216 NFA_CLASS_UPPER,
217 NFA_CLASS_XDIGIT,
218 NFA_CLASS_TAB,
219 NFA_CLASS_RETURN,
220 NFA_CLASS_BACKSPACE,
221 NFA_CLASS_ESCAPE
222};
223
224/* Keep in sync with classchars. */
225static int nfa_classcodes[] = {
226 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
227 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
228 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
229 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
230 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
231 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
232 NFA_UPPER, NFA_NUPPER
233};
234
235static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
236
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200238static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200239
Bram Moolenaar428e9872013-05-30 17:05:39 +0200240/* NFA regexp \1 .. \9 encountered. */
241static int nfa_has_backref;
242
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200243#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200244/* NFA regexp has \z( ), set zsubexpr. */
245static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200246#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200247
Bram Moolenaar963fee22013-05-26 21:47:28 +0200248/* Number of sub expressions actually being used during execution. 1 if only
249 * the whole match (subexpr 0) is used. */
250static int nfa_nsubexpr;
251
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252static int *post_start; /* holds the postfix form of r.e. */
253static int *post_end;
254static int *post_ptr;
255
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200256static int nstate; /* Number of states in the NFA. Also used when
257 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200258static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259
Bram Moolenaar307aa162013-06-02 16:34:21 +0200260/* If not NULL match must end at this position */
261static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200263/* listid is global, so that it increases on recursive calls to
264 * nfa_regmatch(), which means we don't have to clear the lastlist field of
265 * all the states. */
266static int nfa_listid;
267static int nfa_alt_listid;
268
269/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
270static int nfa_ll_index = 0;
271
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200272static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200273static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
274static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200275static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200276static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200277static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200278static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279static int nfa_regatom __ARGS((void));
280static int nfa_regpiece __ARGS((void));
281static int nfa_regconcat __ARGS((void));
282static int nfa_regbranch __ARGS((void));
283static int nfa_reg __ARGS((int paren));
284#ifdef DEBUG
285static void nfa_set_code __ARGS((int c));
286static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200287static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
288static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289static void nfa_dump __ARGS((nfa_regprog_T *prog));
290#endif
291static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200292static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200293static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200294static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295static int check_char_class __ARGS((int class, int c));
296static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200297static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
298static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200299static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200300static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200301static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
302static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200303static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200304static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
305static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200306static int match_follows __ARGS((nfa_state_T *startstate, int depth));
307static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308
309/* helper functions used when doing re2post() ... regatom() parsing */
310#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200311 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312 return FAIL; \
313 *post_ptr++ = c; \
314 } while (0)
315
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316/*
317 * Initialize internal variables before NFA compilation.
318 * Return OK on success, FAIL otherwise.
319 */
320 static int
321nfa_regcomp_start(expr, re_flags)
322 char_u *expr;
323 int re_flags; /* see vim_regcomp() */
324{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200325 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200326 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200327
328 nstate = 0;
329 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200330 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200331 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200332
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200333 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200334 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200335 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200336
337 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200338 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200339
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200340 post_start = (int *)lalloc(postfix_size, TRUE);
341 if (post_start == NULL)
342 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200343 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200344 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200345 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200346 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200347
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200348 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200349 regcomp_start(expr, re_flags);
350
351 return OK;
352}
353
354/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200355 * Figure out if the NFA state list starts with an anchor, must match at start
356 * of the line.
357 */
358 static int
359nfa_get_reganch(start, depth)
360 nfa_state_T *start;
361 int depth;
362{
363 nfa_state_T *p = start;
364
365 if (depth > 4)
366 return 0;
367
368 while (p != NULL)
369 {
370 switch (p->c)
371 {
372 case NFA_BOL:
373 case NFA_BOF:
374 return 1; /* yes! */
375
376 case NFA_ZSTART:
377 case NFA_ZEND:
378 case NFA_CURSOR:
379 case NFA_VISUAL:
380
381 case NFA_MOPEN:
382 case NFA_MOPEN1:
383 case NFA_MOPEN2:
384 case NFA_MOPEN3:
385 case NFA_MOPEN4:
386 case NFA_MOPEN5:
387 case NFA_MOPEN6:
388 case NFA_MOPEN7:
389 case NFA_MOPEN8:
390 case NFA_MOPEN9:
391 case NFA_NOPEN:
392#ifdef FEAT_SYN_HL
393 case NFA_ZOPEN:
394 case NFA_ZOPEN1:
395 case NFA_ZOPEN2:
396 case NFA_ZOPEN3:
397 case NFA_ZOPEN4:
398 case NFA_ZOPEN5:
399 case NFA_ZOPEN6:
400 case NFA_ZOPEN7:
401 case NFA_ZOPEN8:
402 case NFA_ZOPEN9:
403#endif
404 p = p->out;
405 break;
406
407 case NFA_SPLIT:
408 return nfa_get_reganch(p->out, depth + 1)
409 && nfa_get_reganch(p->out1, depth + 1);
410
411 default:
412 return 0; /* noooo */
413 }
414 }
415 return 0;
416}
417
418/*
419 * Figure out if the NFA state list starts with a character which must match
420 * at start of the match.
421 */
422 static int
423nfa_get_regstart(start, depth)
424 nfa_state_T *start;
425 int depth;
426{
427 nfa_state_T *p = start;
428
429 if (depth > 4)
430 return 0;
431
432 while (p != NULL)
433 {
434 switch (p->c)
435 {
436 /* all kinds of zero-width matches */
437 case NFA_BOL:
438 case NFA_BOF:
439 case NFA_BOW:
440 case NFA_EOW:
441 case NFA_ZSTART:
442 case NFA_ZEND:
443 case NFA_CURSOR:
444 case NFA_VISUAL:
445 case NFA_LNUM:
446 case NFA_LNUM_GT:
447 case NFA_LNUM_LT:
448 case NFA_COL:
449 case NFA_COL_GT:
450 case NFA_COL_LT:
451 case NFA_VCOL:
452 case NFA_VCOL_GT:
453 case NFA_VCOL_LT:
454 case NFA_MARK:
455 case NFA_MARK_GT:
456 case NFA_MARK_LT:
457
458 case NFA_MOPEN:
459 case NFA_MOPEN1:
460 case NFA_MOPEN2:
461 case NFA_MOPEN3:
462 case NFA_MOPEN4:
463 case NFA_MOPEN5:
464 case NFA_MOPEN6:
465 case NFA_MOPEN7:
466 case NFA_MOPEN8:
467 case NFA_MOPEN9:
468 case NFA_NOPEN:
469#ifdef FEAT_SYN_HL
470 case NFA_ZOPEN:
471 case NFA_ZOPEN1:
472 case NFA_ZOPEN2:
473 case NFA_ZOPEN3:
474 case NFA_ZOPEN4:
475 case NFA_ZOPEN5:
476 case NFA_ZOPEN6:
477 case NFA_ZOPEN7:
478 case NFA_ZOPEN8:
479 case NFA_ZOPEN9:
480#endif
481 p = p->out;
482 break;
483
484 case NFA_SPLIT:
485 {
486 int c1 = nfa_get_regstart(p->out, depth + 1);
487 int c2 = nfa_get_regstart(p->out1, depth + 1);
488
489 if (c1 == c2)
490 return c1; /* yes! */
491 return 0;
492 }
493
494 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200495 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200496 return p->c; /* yes! */
497 return 0;
498 }
499 }
500 return 0;
501}
502
503/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200504 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200505 * else. If so return a string in allocated memory with what must match after
506 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200507 */
508 static char_u *
509nfa_get_match_text(start)
510 nfa_state_T *start;
511{
512 nfa_state_T *p = start;
513 int len = 0;
514 char_u *ret;
515 char_u *s;
516
517 if (p->c != NFA_MOPEN)
518 return NULL; /* just in case */
519 p = p->out;
520 while (p->c > 0)
521 {
522 len += MB_CHAR2LEN(p->c);
523 p = p->out;
524 }
525 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
526 return NULL;
527
528 ret = alloc(len);
529 if (ret != NULL)
530 {
531 len = 0;
532 p = start->out->out; /* skip first char, it goes into regstart */
533 s = ret;
534 while (p->c > 0)
535 {
536#ifdef FEAT_MBYTE
537 if (has_mbyte)
538 s += (*mb_char2bytes)(p->c, s);
539 else
540#endif
541 *s++ = p->c;
542 p = p->out;
543 }
544 *s = NUL;
545 }
546 return ret;
547}
548
549/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200550 * Allocate more space for post_start. Called when
551 * running above the estimated number of states.
552 */
553 static int
554realloc_post_list()
555{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200556 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200557 int new_max = nstate_max + 1000;
558 int *new_start;
559 int *old_start;
560
561 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
562 if (new_start == NULL)
563 return FAIL;
564 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200565 old_start = post_start;
566 post_start = new_start;
567 post_ptr = new_start + (post_ptr - old_start);
568 post_end = post_start + new_max;
569 vim_free(old_start);
570 return OK;
571}
572
573/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200574 * Search between "start" and "end" and try to recognize a
575 * character class in expanded form. For example [0-9].
576 * On success, return the id the character class to be emitted.
577 * On failure, return 0 (=FAIL)
578 * Start points to the first char of the range, while end should point
579 * to the closing brace.
580 */
581 static int
582nfa_recognize_char_class(start, end, extra_newl)
583 char_u *start;
584 char_u *end;
585 int extra_newl;
586{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200587# define CLASS_not 0x80
588# define CLASS_af 0x40
589# define CLASS_AF 0x20
590# define CLASS_az 0x10
591# define CLASS_AZ 0x08
592# define CLASS_o7 0x04
593# define CLASS_o9 0x02
594# define CLASS_underscore 0x01
595
596 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200598 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200599
600 if (extra_newl == TRUE)
601 newl = TRUE;
602
603 if (*end != ']')
604 return FAIL;
605 p = start;
606 if (*p == '^')
607 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200608 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200609 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200610 }
611
612 while (p < end)
613 {
614 if (p + 2 < end && *(p + 1) == '-')
615 {
616 switch (*p)
617 {
618 case '0':
619 if (*(p + 2) == '9')
620 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200621 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200622 break;
623 }
624 else
625 if (*(p + 2) == '7')
626 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200627 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200628 break;
629 }
630 case 'a':
631 if (*(p + 2) == 'z')
632 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200633 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 break;
635 }
636 else
637 if (*(p + 2) == 'f')
638 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200639 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200640 break;
641 }
642 case 'A':
643 if (*(p + 2) == 'Z')
644 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200645 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200646 break;
647 }
648 else
649 if (*(p + 2) == 'F')
650 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200651 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200652 break;
653 }
654 /* FALLTHROUGH */
655 default:
656 return FAIL;
657 }
658 p += 3;
659 }
660 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
661 {
662 newl = TRUE;
663 p += 2;
664 }
665 else if (*p == '_')
666 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200667 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200668 p ++;
669 }
670 else if (*p == '\n')
671 {
672 newl = TRUE;
673 p ++;
674 }
675 else
676 return FAIL;
677 } /* while (p < end) */
678
679 if (p != end)
680 return FAIL;
681
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200682 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200683 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200684
685 switch (config)
686 {
687 case CLASS_o9:
688 return extra_newl + NFA_DIGIT;
689 case CLASS_not | CLASS_o9:
690 return extra_newl + NFA_NDIGIT;
691 case CLASS_af | CLASS_AF | CLASS_o9:
692 return extra_newl + NFA_HEX;
693 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
694 return extra_newl + NFA_NHEX;
695 case CLASS_o7:
696 return extra_newl + NFA_OCTAL;
697 case CLASS_not | CLASS_o7:
698 return extra_newl + NFA_NOCTAL;
699 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
700 return extra_newl + NFA_WORD;
701 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
702 return extra_newl + NFA_NWORD;
703 case CLASS_az | CLASS_AZ | CLASS_underscore:
704 return extra_newl + NFA_HEAD;
705 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
706 return extra_newl + NFA_NHEAD;
707 case CLASS_az | CLASS_AZ:
708 return extra_newl + NFA_ALPHA;
709 case CLASS_not | CLASS_az | CLASS_AZ:
710 return extra_newl + NFA_NALPHA;
711 case CLASS_az:
712 return extra_newl + NFA_LOWER;
713 case CLASS_not | CLASS_az:
714 return extra_newl + NFA_NLOWER;
715 case CLASS_AZ:
716 return extra_newl + NFA_UPPER;
717 case CLASS_not | CLASS_AZ:
718 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200719 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200721}
722
723/*
724 * Produce the bytes for equivalence class "c".
725 * Currently only handles latin1, latin9 and utf-8.
726 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
727 * equivalent to 'a OR b OR c'
728 *
729 * NOTE! When changing this function, also update reg_equi_class()
730 */
731 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200732nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200733 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200734{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200735#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200736
737#ifdef FEAT_MBYTE
738 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
739 || STRCMP(p_enc, "iso-8859-15") == 0)
740#endif
741 {
742 switch (c)
743 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200744 case 'A': case 0300: case 0301: case 0302:
745 case 0303: case 0304: case 0305:
746 EMIT2('A'); EMIT2(0300); EMIT2(0301);
747 EMIT2(0302); EMIT2(0303); EMIT2(0304);
748 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749 return OK;
750
Bram Moolenaar417bad22013-06-07 14:08:30 +0200751 case 'C': case 0307:
752 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200753 return OK;
754
Bram Moolenaar417bad22013-06-07 14:08:30 +0200755 case 'E': case 0310: case 0311: case 0312: case 0313:
756 EMIT2('E'); EMIT2(0310); EMIT2(0311);
757 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200758 return OK;
759
Bram Moolenaar417bad22013-06-07 14:08:30 +0200760 case 'I': case 0314: case 0315: case 0316: case 0317:
761 EMIT2('I'); EMIT2(0314); EMIT2(0315);
762 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200763 return OK;
764
Bram Moolenaar417bad22013-06-07 14:08:30 +0200765 case 'N': case 0321:
766 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200767 return OK;
768
Bram Moolenaar417bad22013-06-07 14:08:30 +0200769 case 'O': case 0322: case 0323: case 0324: case 0325:
770 case 0326:
771 EMIT2('O'); EMIT2(0322); EMIT2(0323);
772 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200773 return OK;
774
Bram Moolenaar417bad22013-06-07 14:08:30 +0200775 case 'U': case 0331: case 0332: case 0333: case 0334:
776 EMIT2('U'); EMIT2(0331); EMIT2(0332);
777 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200778 return OK;
779
Bram Moolenaar417bad22013-06-07 14:08:30 +0200780 case 'Y': case 0335:
781 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200782 return OK;
783
Bram Moolenaar417bad22013-06-07 14:08:30 +0200784 case 'a': case 0340: case 0341: case 0342:
785 case 0343: case 0344: case 0345:
786 EMIT2('a'); EMIT2(0340); EMIT2(0341);
787 EMIT2(0342); EMIT2(0343); EMIT2(0344);
788 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200789 return OK;
790
Bram Moolenaar417bad22013-06-07 14:08:30 +0200791 case 'c': case 0347:
792 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200793 return OK;
794
Bram Moolenaar417bad22013-06-07 14:08:30 +0200795 case 'e': case 0350: case 0351: case 0352: case 0353:
796 EMIT2('e'); EMIT2(0350); EMIT2(0351);
797 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200798 return OK;
799
Bram Moolenaar417bad22013-06-07 14:08:30 +0200800 case 'i': case 0354: case 0355: case 0356: case 0357:
801 EMIT2('i'); EMIT2(0354); EMIT2(0355);
802 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200803 return OK;
804
Bram Moolenaar417bad22013-06-07 14:08:30 +0200805 case 'n': case 0361:
806 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200807 return OK;
808
Bram Moolenaar417bad22013-06-07 14:08:30 +0200809 case 'o': case 0362: case 0363: case 0364: case 0365:
810 case 0366:
811 EMIT2('o'); EMIT2(0362); EMIT2(0363);
812 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200813 return OK;
814
Bram Moolenaar417bad22013-06-07 14:08:30 +0200815 case 'u': case 0371: case 0372: case 0373: case 0374:
816 EMIT2('u'); EMIT2(0371); EMIT2(0372);
817 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200818 return OK;
819
Bram Moolenaar417bad22013-06-07 14:08:30 +0200820 case 'y': case 0375: case 0377:
821 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200822 return OK;
823
824 default:
825 return FAIL;
826 }
827 }
828
829 EMIT(c);
830 return OK;
831#undef EMIT2
832}
833
834/*
835 * Code to parse regular expression.
836 *
837 * We try to reuse parsing functions in regexp.c to
838 * minimize surprise and keep the syntax consistent.
839 */
840
841/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200842 * Parse the lowest level.
843 *
844 * An atom can be one of a long list of items. Many atoms match one character
845 * in the text. It is often an ordinary character or a character class.
846 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
847 * is only for syntax highlighting.
848 *
849 * atom ::= ordinary-atom
850 * or \( pattern \)
851 * or \%( pattern \)
852 * or \z( pattern \)
853 */
854 static int
855nfa_regatom()
856{
857 int c;
858 int charclass;
859 int equiclass;
860 int collclass;
861 int got_coll_char;
862 char_u *p;
863 char_u *endp;
864#ifdef FEAT_MBYTE
865 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200866#endif
867 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 int emit_range;
869 int negated;
870 int result;
871 int startc = -1;
872 int endc = -1;
873 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200874
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200876 switch (c)
877 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200878 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200879 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
880
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200881 case Magic('^'):
882 EMIT(NFA_BOL);
883 break;
884
885 case Magic('$'):
886 EMIT(NFA_EOL);
887#if defined(FEAT_SYN_HL) || defined(PROTO)
888 had_eol = TRUE;
889#endif
890 break;
891
892 case Magic('<'):
893 EMIT(NFA_BOW);
894 break;
895
896 case Magic('>'):
897 EMIT(NFA_EOW);
898 break;
899
900 case Magic('_'):
901 c = no_Magic(getchr());
902 if (c == '^') /* "\_^" is start-of-line */
903 {
904 EMIT(NFA_BOL);
905 break;
906 }
907 if (c == '$') /* "\_$" is end-of-line */
908 {
909 EMIT(NFA_EOL);
910#if defined(FEAT_SYN_HL) || defined(PROTO)
911 had_eol = TRUE;
912#endif
913 break;
914 }
915
916 extra = ADD_NL;
917
918 /* "\_[" is collection plus newline */
919 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200920 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200921
922 /* "\_x" is character class plus newline */
923 /*FALLTHROUGH*/
924
925 /*
926 * Character classes.
927 */
928 case Magic('.'):
929 case Magic('i'):
930 case Magic('I'):
931 case Magic('k'):
932 case Magic('K'):
933 case Magic('f'):
934 case Magic('F'):
935 case Magic('p'):
936 case Magic('P'):
937 case Magic('s'):
938 case Magic('S'):
939 case Magic('d'):
940 case Magic('D'):
941 case Magic('x'):
942 case Magic('X'):
943 case Magic('o'):
944 case Magic('O'):
945 case Magic('w'):
946 case Magic('W'):
947 case Magic('h'):
948 case Magic('H'):
949 case Magic('a'):
950 case Magic('A'):
951 case Magic('l'):
952 case Magic('L'):
953 case Magic('u'):
954 case Magic('U'):
955 p = vim_strchr(classchars, no_Magic(c));
956 if (p == NULL)
957 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200958 EMSGN("INTERNAL: Unknown character class char: %ld", c);
959 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200960 }
961#ifdef FEAT_MBYTE
962 /* When '.' is followed by a composing char ignore the dot, so that
963 * the composing char is matched here. */
964 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
965 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200966 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200967 c = getchr();
968 goto nfa_do_multibyte;
969 }
970#endif
971 EMIT(nfa_classcodes[p - classchars]);
972 if (extra == ADD_NL)
973 {
974 EMIT(NFA_NEWL);
975 EMIT(NFA_OR);
976 regflags |= RF_HASNL;
977 }
978 break;
979
980 case Magic('n'):
981 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200982 /* In a string "\n" matches a newline character. */
983 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200984 else
985 {
986 /* In buffer text "\n" matches the end of a line. */
987 EMIT(NFA_NEWL);
988 regflags |= RF_HASNL;
989 }
990 break;
991
992 case Magic('('):
993 if (nfa_reg(REG_PAREN) == FAIL)
994 return FAIL; /* cascaded error */
995 break;
996
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200997 case Magic('|'):
998 case Magic('&'):
999 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001000 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001001 return FAIL;
1002
1003 case Magic('='):
1004 case Magic('?'):
1005 case Magic('+'):
1006 case Magic('@'):
1007 case Magic('*'):
1008 case Magic('{'):
1009 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001010 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001011 return FAIL;
1012
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001013 case Magic('~'):
1014 {
1015 char_u *lp;
1016
1017 /* Previous substitute pattern.
1018 * Generated as "\%(pattern\)". */
1019 if (reg_prev_sub == NULL)
1020 {
1021 EMSG(_(e_nopresub));
1022 return FAIL;
1023 }
1024 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1025 {
1026 EMIT(PTR2CHAR(lp));
1027 if (lp != reg_prev_sub)
1028 EMIT(NFA_CONCAT);
1029 }
1030 EMIT(NFA_NOPEN);
1031 break;
1032 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001033
Bram Moolenaar428e9872013-05-30 17:05:39 +02001034 case Magic('1'):
1035 case Magic('2'):
1036 case Magic('3'):
1037 case Magic('4'):
1038 case Magic('5'):
1039 case Magic('6'):
1040 case Magic('7'):
1041 case Magic('8'):
1042 case Magic('9'):
1043 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1044 nfa_has_backref = TRUE;
1045 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001046
1047 case Magic('z'):
1048 c = no_Magic(getchr());
1049 switch (c)
1050 {
1051 case 's':
1052 EMIT(NFA_ZSTART);
1053 break;
1054 case 'e':
1055 EMIT(NFA_ZEND);
1056 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001057 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001058#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001059 case '1':
1060 case '2':
1061 case '3':
1062 case '4':
1063 case '5':
1064 case '6':
1065 case '7':
1066 case '8':
1067 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001068 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001069 if (reg_do_extmatch != REX_USE)
1070 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001071 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1072 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001073 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001074 re_has_z = REX_USE;
1075 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001076 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001077 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001078 if (reg_do_extmatch != REX_SET)
1079 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001080 if (nfa_reg(REG_ZPAREN) == FAIL)
1081 return FAIL; /* cascaded error */
1082 re_has_z = REX_SET;
1083 break;
1084#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001086 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001087 no_Magic(c));
1088 return FAIL;
1089 }
1090 break;
1091
1092 case Magic('%'):
1093 c = no_Magic(getchr());
1094 switch (c)
1095 {
1096 /* () without a back reference */
1097 case '(':
1098 if (nfa_reg(REG_NPAREN) == FAIL)
1099 return FAIL;
1100 EMIT(NFA_NOPEN);
1101 break;
1102
1103 case 'd': /* %d123 decimal */
1104 case 'o': /* %o123 octal */
1105 case 'x': /* %xab hex 2 */
1106 case 'u': /* %uabcd hex 4 */
1107 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001108 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001109 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001110
Bram Moolenaar47196582013-05-25 22:04:23 +02001111 switch (c)
1112 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001113 case 'd': nr = getdecchrs(); break;
1114 case 'o': nr = getoctchrs(); break;
1115 case 'x': nr = gethexchrs(2); break;
1116 case 'u': nr = gethexchrs(4); break;
1117 case 'U': nr = gethexchrs(8); break;
1118 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001119 }
1120
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001121 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001122 EMSG2_RET_FAIL(
1123 _("E678: Invalid character after %s%%[dxouU]"),
1124 reg_magic == MAGIC_ALL);
1125 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001126 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001127 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001128 break;
1129
1130 /* Catch \%^ and \%$ regardless of where they appear in the
1131 * pattern -- regardless of whether or not it makes sense. */
1132 case '^':
1133 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001134 break;
1135
1136 case '$':
1137 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001138 break;
1139
1140 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001141 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001142 break;
1143
1144 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001145 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001146 break;
1147
1148 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001149 {
1150 int n;
1151
1152 /* \%[abc] */
1153 for (n = 0; (c = getchr()) != ']'; ++n)
1154 {
1155 if (c == NUL)
1156 EMSG2_RET_FAIL(_(e_missing_sb),
1157 reg_magic == MAGIC_ALL);
1158 EMIT(c);
1159 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001160 if (n == 0)
1161 EMSG2_RET_FAIL(_(e_empty_sb),
1162 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001163 EMIT(NFA_OPT_CHARS);
1164 EMIT(n);
1165 break;
1166 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001168 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001169 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001170 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001171 int cmp = c;
1172
1173 if (c == '<' || c == '>')
1174 c = getchr();
1175 while (VIM_ISDIGIT(c))
1176 {
1177 n = n * 10 + (c - '0');
1178 c = getchr();
1179 }
1180 if (c == 'l' || c == 'c' || c == 'v')
1181 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001182 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001183 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001184 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001185 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001186 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001187 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001188 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001189 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001190 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001191 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001192 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001193 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001194 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001195 break;
1196 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001197 else if (c == '\'' && n == 0)
1198 {
1199 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001200 EMIT(cmp == '<' ? NFA_MARK_LT :
1201 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001202 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001203 break;
1204 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001205 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001206 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1207 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 return FAIL;
1209 }
1210 break;
1211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001213collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001215 * [abc] uses NFA_START_COLL - NFA_END_COLL
1216 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1217 * Each character is produced as a regular state, using
1218 * NFA_CONCAT to bind them together.
1219 * Besides normal characters there can be:
1220 * - character classes NFA_CLASS_*
1221 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001222 */
1223
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001224 p = regparse;
1225 endp = skip_anyof(p);
1226 if (*endp == ']')
1227 {
1228 /*
1229 * Try to reverse engineer character classes. For example,
1230 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1231 * and perform the necessary substitutions in the NFA.
1232 */
1233 result = nfa_recognize_char_class(regparse, endp,
1234 extra == ADD_NL);
1235 if (result != FAIL)
1236 {
1237 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1238 EMIT(result);
1239 else /* must be char class + newline */
1240 {
1241 EMIT(result - ADD_NL);
1242 EMIT(NFA_NEWL);
1243 EMIT(NFA_OR);
1244 }
1245 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001246 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001247 return OK;
1248 }
1249 /*
1250 * Failed to recognize a character class. Use the simple
1251 * version that turns [abc] into 'a' OR 'b' OR 'c'
1252 */
1253 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 if (*regparse == '^') /* negated range */
1256 {
1257 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001258 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001259 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001261 else
1262 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263 if (*regparse == '-')
1264 {
1265 startc = '-';
1266 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001267 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001268 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269 }
1270 /* Emit the OR branches for each character in the [] */
1271 emit_range = FALSE;
1272 while (regparse < endp)
1273 {
1274 oldstartc = startc;
1275 startc = -1;
1276 got_coll_char = FALSE;
1277 if (*regparse == '[')
1278 {
1279 /* Check for [: :], [= =], [. .] */
1280 equiclass = collclass = 0;
1281 charclass = get_char_class(&regparse);
1282 if (charclass == CLASS_NONE)
1283 {
1284 equiclass = get_equi_class(&regparse);
1285 if (equiclass == 0)
1286 collclass = get_coll_element(&regparse);
1287 }
1288
1289 /* Character class like [:alpha:] */
1290 if (charclass != CLASS_NONE)
1291 {
1292 switch (charclass)
1293 {
1294 case CLASS_ALNUM:
1295 EMIT(NFA_CLASS_ALNUM);
1296 break;
1297 case CLASS_ALPHA:
1298 EMIT(NFA_CLASS_ALPHA);
1299 break;
1300 case CLASS_BLANK:
1301 EMIT(NFA_CLASS_BLANK);
1302 break;
1303 case CLASS_CNTRL:
1304 EMIT(NFA_CLASS_CNTRL);
1305 break;
1306 case CLASS_DIGIT:
1307 EMIT(NFA_CLASS_DIGIT);
1308 break;
1309 case CLASS_GRAPH:
1310 EMIT(NFA_CLASS_GRAPH);
1311 break;
1312 case CLASS_LOWER:
1313 EMIT(NFA_CLASS_LOWER);
1314 break;
1315 case CLASS_PRINT:
1316 EMIT(NFA_CLASS_PRINT);
1317 break;
1318 case CLASS_PUNCT:
1319 EMIT(NFA_CLASS_PUNCT);
1320 break;
1321 case CLASS_SPACE:
1322 EMIT(NFA_CLASS_SPACE);
1323 break;
1324 case CLASS_UPPER:
1325 EMIT(NFA_CLASS_UPPER);
1326 break;
1327 case CLASS_XDIGIT:
1328 EMIT(NFA_CLASS_XDIGIT);
1329 break;
1330 case CLASS_TAB:
1331 EMIT(NFA_CLASS_TAB);
1332 break;
1333 case CLASS_RETURN:
1334 EMIT(NFA_CLASS_RETURN);
1335 break;
1336 case CLASS_BACKSPACE:
1337 EMIT(NFA_CLASS_BACKSPACE);
1338 break;
1339 case CLASS_ESCAPE:
1340 EMIT(NFA_CLASS_ESCAPE);
1341 break;
1342 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001343 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 continue;
1345 }
1346 /* Try equivalence class [=a=] and the like */
1347 if (equiclass != 0)
1348 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001349 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001350 if (result == FAIL)
1351 {
1352 /* should never happen */
1353 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1354 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355 continue;
1356 }
1357 /* Try collating class like [. .] */
1358 if (collclass != 0)
1359 {
1360 startc = collclass; /* allow [.a.]-x as a range */
1361 /* Will emit the proper atom at the end of the
1362 * while loop. */
1363 }
1364 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001365 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1366 * start character. */
1367 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001368 {
1369 emit_range = TRUE;
1370 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001371 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001372 continue; /* reading the end of the range */
1373 }
1374
1375 /* Now handle simple and escaped characters.
1376 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1377 * accepts "\t", "\e", etc., but only when the 'l' flag in
1378 * 'cpoptions' is not included.
1379 * Posix doesn't recognize backslash at all.
1380 */
1381 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001382 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001383 && regparse + 1 <= endp
1384 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001385 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386 && vim_strchr(REGEXP_ABBR, regparse[1])
1387 != NULL)
1388 )
1389 )
1390 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001391 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001392
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001393 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001394 startc = reg_string ? NL : NFA_NEWL;
1395 else
1396 if (*regparse == 'd'
1397 || *regparse == 'o'
1398 || *regparse == 'x'
1399 || *regparse == 'u'
1400 || *regparse == 'U'
1401 )
1402 {
1403 /* TODO(RE) This needs more testing */
1404 startc = coll_get_char();
1405 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001406 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 }
1408 else
1409 {
1410 /* \r,\t,\e,\b */
1411 startc = backslash_trans(*regparse);
1412 }
1413 }
1414
1415 /* Normal printable char */
1416 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001417 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001418
1419 /* Previous char was '-', so this char is end of range. */
1420 if (emit_range)
1421 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001422 endc = startc;
1423 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001424 if (startc > endc)
1425 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001426
1427 if (endc > startc + 2)
1428 {
1429 /* Emit a range instead of the sequence of
1430 * individual characters. */
1431 if (startc == 0)
1432 /* \x00 is translated to \x0a, start at \x01. */
1433 EMIT(1);
1434 else
1435 --post_ptr; /* remove NFA_CONCAT */
1436 EMIT(endc);
1437 EMIT(NFA_RANGE);
1438 EMIT(NFA_CONCAT);
1439 }
1440 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001442 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001443 || (*mb_char2len)(endc) > 1))
1444 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001445 /* Emit the characters in the range.
1446 * "startc" was already emitted, so skip it.
1447 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001448 for (c = startc + 1; c <= endc; c++)
1449 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001450 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001451 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001453 }
1454 else
1455#endif
1456 {
1457#ifdef EBCDIC
1458 int alpha_only = FALSE;
1459
1460 /* for alphabetical range skip the gaps
1461 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1462 if (isalpha(startc) && isalpha(endc))
1463 alpha_only = TRUE;
1464#endif
1465 /* Emit the range. "startc" was already emitted, so
1466 * skip it. */
1467 for (c = startc + 1; c <= endc; c++)
1468#ifdef EBCDIC
1469 if (!alpha_only || isalpha(startc))
1470#endif
1471 {
1472 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001473 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001476 emit_range = FALSE;
1477 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001478 }
1479 else
1480 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001481 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001482 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483 * Normally, simply emit startc. But if we get char
1484 * code=0 from a collating char, then replace it with
1485 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001487 * the backtracking engine. */
1488 if (startc == NFA_NEWL)
1489 {
1490 /* Line break can't be matched as part of the
1491 * collection, add an OR below. But not for negated
1492 * range. */
1493 if (!negated)
1494 extra = ADD_NL;
1495 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001497 {
1498 if (got_coll_char == TRUE && startc == 0)
1499 EMIT(0x0a);
1500 else
1501 EMIT(startc);
1502 EMIT(NFA_CONCAT);
1503 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 }
1505
Bram Moolenaar51a29832013-05-28 22:30:35 +02001506 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001507 } /* while (p < endp) */
1508
Bram Moolenaar51a29832013-05-28 22:30:35 +02001509 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510 if (*regparse == '-') /* if last, '-' is just a char */
1511 {
1512 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001513 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001514 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001515 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001517 /* skip the trailing ] */
1518 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001519 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001520
1521 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001522 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001523 EMIT(NFA_END_NEG_COLL);
1524 else
1525 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001526
1527 /* \_[] also matches \n but it's not negated */
1528 if (extra == ADD_NL)
1529 {
1530 EMIT(reg_string ? NL : NFA_NEWL);
1531 EMIT(NFA_OR);
1532 }
1533
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001534 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001535 } /* if exists closing ] */
1536
1537 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001538 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001539 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001541 default:
1542 {
1543#ifdef FEAT_MBYTE
1544 int plen;
1545
1546nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001547 /* plen is length of current char with composing chars */
1548 if (enc_utf8 && ((*mb_char2len)(c)
1549 != (plen = (*mb_ptr2len)(old_regparse))
1550 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001552 int i = 0;
1553
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001554 /* A base character plus composing characters, or just one
1555 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001556 * This requires creating a separate atom as if enclosing
1557 * the characters in (), where NFA_COMPOSING is the ( and
1558 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 * building the postfix form, not the NFA itself;
1560 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001561 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001562 for (;;)
1563 {
1564 EMIT(c);
1565 if (i > 0)
1566 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001567 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001568 break;
1569 c = utf_ptr2char(old_regparse + i);
1570 }
1571 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001572 regparse = old_regparse + plen;
1573 }
1574 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001575#endif
1576 {
1577 c = no_Magic(c);
1578 EMIT(c);
1579 }
1580 return OK;
1581 }
1582 }
1583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001584 return OK;
1585}
1586
1587/*
1588 * Parse something followed by possible [*+=].
1589 *
1590 * A piece is an atom, possibly followed by a multi, an indication of how many
1591 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1592 * characters: "", "a", "aa", etc.
1593 *
1594 * piece ::= atom
1595 * or atom multi
1596 */
1597 static int
1598nfa_regpiece()
1599{
1600 int i;
1601 int op;
1602 int ret;
1603 long minval, maxval;
1604 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001605 parse_state_T old_state;
1606 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001607 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001608 int old_post_pos;
1609 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 int quest;
1611
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001612 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1613 * next. */
1614 save_parse_state(&old_state);
1615
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001616 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001617 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001618
1619 ret = nfa_regatom();
1620 if (ret == FAIL)
1621 return FAIL; /* cascaded error */
1622
1623 op = peekchr();
1624 if (re_multi_type(op) == NOT_MULTI)
1625 return OK;
1626
1627 skipchr();
1628 switch (op)
1629 {
1630 case Magic('*'):
1631 EMIT(NFA_STAR);
1632 break;
1633
1634 case Magic('+'):
1635 /*
1636 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1637 * first and only submatch would be "aaa". But the backtracking
1638 * engine interprets the plus as "try matching one more time", and
1639 * a* matches a second time at the end of the input, the empty
1640 * string.
1641 * The submatch will the empty string.
1642 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001643 * In order to be consistent with the old engine, we replace
1644 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001646 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001647 curchr = -1;
1648 if (nfa_regatom() == FAIL)
1649 return FAIL;
1650 EMIT(NFA_STAR);
1651 EMIT(NFA_CONCAT);
1652 skipchr(); /* skip the \+ */
1653 break;
1654
1655 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001656 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001658 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 switch(op)
1660 {
1661 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001662 /* \@= */
1663 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001664 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001665 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001666 /* \@! */
1667 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001668 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001670 op = no_Magic(getchr());
1671 if (op == '=')
1672 /* \@<= */
1673 i = NFA_PREV_ATOM_JUST_BEFORE;
1674 else if (op == '!')
1675 /* \@<! */
1676 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1677 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001679 /* \@> */
1680 i = NFA_PREV_ATOM_LIKE_PATTERN;
1681 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001682 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001683 if (i == 0)
1684 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001685 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1686 return FAIL;
1687 }
1688 EMIT(i);
1689 if (i == NFA_PREV_ATOM_JUST_BEFORE
1690 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1691 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001692 break;
1693
1694 case Magic('?'):
1695 case Magic('='):
1696 EMIT(NFA_QUEST);
1697 break;
1698
1699 case Magic('{'):
1700 /* a{2,5} will expand to 'aaa?a?a?'
1701 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1702 * version of '?'
1703 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1704 * parenthesis have the same id
1705 */
1706
1707 greedy = TRUE;
1708 c2 = peekchr();
1709 if (c2 == '-' || c2 == Magic('-'))
1710 {
1711 skipchr();
1712 greedy = FALSE;
1713 }
1714 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001715 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001716
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001717 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1718 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001719 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001721 if (greedy)
1722 /* \{}, \{0,} */
1723 EMIT(NFA_STAR);
1724 else
1725 /* \{-}, \{-0,} */
1726 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001727 break;
1728 }
1729
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001730 /* Special case: x{0} or x{-0} */
1731 if (maxval == 0)
1732 {
1733 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001734 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1736 EMIT(NFA_SKIP_CHAR);
1737 return OK;
1738 }
1739
1740 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001741 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001742 /* Save parse state after the repeated atom and the \{} */
1743 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1746 for (i = 0; i < maxval; i++)
1747 {
1748 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001749 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001750 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001751 if (nfa_regatom() == FAIL)
1752 return FAIL;
1753 /* after "minval" times, atoms are optional */
1754 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001755 {
1756 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001757 {
1758 if (greedy)
1759 EMIT(NFA_STAR);
1760 else
1761 EMIT(NFA_STAR_NONGREEDY);
1762 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001763 else
1764 EMIT(quest);
1765 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001766 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001767 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001768 if (i + 1 > minval && maxval == MAX_LIMIT)
1769 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001770 }
1771
1772 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001773 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001774 curchr = -1;
1775
1776 break;
1777
1778
1779 default:
1780 break;
1781 } /* end switch */
1782
1783 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001784 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001785 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786
1787 return OK;
1788}
1789
1790/*
1791 * Parse one or more pieces, concatenated. It matches a match for the
1792 * first piece, followed by a match for the second piece, etc. Example:
1793 * "f[0-9]b", first matches "f", then a digit and then "b".
1794 *
1795 * concat ::= piece
1796 * or piece piece
1797 * or piece piece piece
1798 * etc.
1799 */
1800 static int
1801nfa_regconcat()
1802{
1803 int cont = TRUE;
1804 int first = TRUE;
1805
1806 while (cont)
1807 {
1808 switch (peekchr())
1809 {
1810 case NUL:
1811 case Magic('|'):
1812 case Magic('&'):
1813 case Magic(')'):
1814 cont = FALSE;
1815 break;
1816
1817 case Magic('Z'):
1818#ifdef FEAT_MBYTE
1819 regflags |= RF_ICOMBINE;
1820#endif
1821 skipchr_keepstart();
1822 break;
1823 case Magic('c'):
1824 regflags |= RF_ICASE;
1825 skipchr_keepstart();
1826 break;
1827 case Magic('C'):
1828 regflags |= RF_NOICASE;
1829 skipchr_keepstart();
1830 break;
1831 case Magic('v'):
1832 reg_magic = MAGIC_ALL;
1833 skipchr_keepstart();
1834 curchr = -1;
1835 break;
1836 case Magic('m'):
1837 reg_magic = MAGIC_ON;
1838 skipchr_keepstart();
1839 curchr = -1;
1840 break;
1841 case Magic('M'):
1842 reg_magic = MAGIC_OFF;
1843 skipchr_keepstart();
1844 curchr = -1;
1845 break;
1846 case Magic('V'):
1847 reg_magic = MAGIC_NONE;
1848 skipchr_keepstart();
1849 curchr = -1;
1850 break;
1851
1852 default:
1853 if (nfa_regpiece() == FAIL)
1854 return FAIL;
1855 if (first == FALSE)
1856 EMIT(NFA_CONCAT);
1857 else
1858 first = FALSE;
1859 break;
1860 }
1861 }
1862
1863 return OK;
1864}
1865
1866/*
1867 * Parse a branch, one or more concats, separated by "\&". It matches the
1868 * last concat, but only if all the preceding concats also match at the same
1869 * position. Examples:
1870 * "foobeep\&..." matches "foo" in "foobeep".
1871 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1872 *
1873 * branch ::= concat
1874 * or concat \& concat
1875 * or concat \& concat \& concat
1876 * etc.
1877 */
1878 static int
1879nfa_regbranch()
1880{
1881 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001882 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001883
Bram Moolenaar16299b52013-05-30 18:45:23 +02001884 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885
1886 /* First branch, possibly the only one */
1887 if (nfa_regconcat() == FAIL)
1888 return FAIL;
1889
1890 ch = peekchr();
1891 /* Try next concats */
1892 while (ch == Magic('&'))
1893 {
1894 skipchr();
1895 EMIT(NFA_NOPEN);
1896 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001897 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 if (nfa_regconcat() == FAIL)
1899 return FAIL;
1900 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001901 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 EMIT(NFA_SKIP_CHAR);
1903 EMIT(NFA_CONCAT);
1904 ch = peekchr();
1905 }
1906
1907 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001908 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 EMIT(NFA_SKIP_CHAR);
1910
1911 return OK;
1912}
1913
1914/*
1915 * Parse a pattern, one or more branches, separated by "\|". It matches
1916 * anything that matches one of the branches. Example: "foo\|beep" matches
1917 * "foo" and matches "beep". If more than one branch matches, the first one
1918 * is used.
1919 *
1920 * pattern ::= branch
1921 * or branch \| branch
1922 * or branch \| branch \| branch
1923 * etc.
1924 */
1925 static int
1926nfa_reg(paren)
1927 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1928{
1929 int parno = 0;
1930
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 if (paren == REG_PAREN)
1932 {
1933 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 parno = regnpar++;
1936 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001937#ifdef FEAT_SYN_HL
1938 else if (paren == REG_ZPAREN)
1939 {
1940 /* Make a ZOPEN node. */
1941 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001942 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001943 parno = regnzpar++;
1944 }
1945#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001946
1947 if (nfa_regbranch() == FAIL)
1948 return FAIL; /* cascaded error */
1949
1950 while (peekchr() == Magic('|'))
1951 {
1952 skipchr();
1953 if (nfa_regbranch() == FAIL)
1954 return FAIL; /* cascaded error */
1955 EMIT(NFA_OR);
1956 }
1957
1958 /* Check for proper termination. */
1959 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1960 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001961 if (paren == REG_NPAREN)
1962 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1963 else
1964 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1965 }
1966 else if (paren == REG_NOPAREN && peekchr() != NUL)
1967 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968 if (peekchr() == Magic(')'))
1969 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1970 else
1971 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1972 }
1973 /*
1974 * Here we set the flag allowing back references to this set of
1975 * parentheses.
1976 */
1977 if (paren == REG_PAREN)
1978 {
1979 had_endbrace[parno] = TRUE; /* have seen the close paren */
1980 EMIT(NFA_MOPEN + parno);
1981 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001982#ifdef FEAT_SYN_HL
1983 else if (paren == REG_ZPAREN)
1984 EMIT(NFA_ZOPEN + parno);
1985#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001986
1987 return OK;
1988}
1989
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001990#ifdef DEBUG
1991static char_u code[50];
1992
1993 static void
1994nfa_set_code(c)
1995 int c;
1996{
1997 int addnl = FALSE;
1998
1999 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2000 {
2001 addnl = TRUE;
2002 c -= ADD_NL;
2003 }
2004
2005 STRCPY(code, "");
2006 switch (c)
2007 {
2008 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2009 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2010 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2011 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2012 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2013 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2014
Bram Moolenaar5714b802013-05-28 22:03:20 +02002015 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2016 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2017 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2018 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2019 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2020 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2021 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2022 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2023 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002024#ifdef FEAT_SYN_HL
2025 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2026 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2027 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2028 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2029 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2030 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2031 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2032 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2033 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2034#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002035 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2036
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037 case NFA_PREV_ATOM_NO_WIDTH:
2038 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002039 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2040 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002041 case NFA_PREV_ATOM_JUST_BEFORE:
2042 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2043 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2044 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002045 case NFA_PREV_ATOM_LIKE_PATTERN:
2046 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2047
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002048 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2049 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002050 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002051 case NFA_START_INVISIBLE_FIRST:
2052 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002053 case NFA_START_INVISIBLE_NEG:
2054 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002055 case NFA_START_INVISIBLE_NEG_FIRST:
2056 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002057 case NFA_START_INVISIBLE_BEFORE:
2058 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002059 case NFA_START_INVISIBLE_BEFORE_FIRST:
2060 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002061 case NFA_START_INVISIBLE_BEFORE_NEG:
2062 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002063 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2064 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002065 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002066 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002067 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002068 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2071 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002072 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002074 case NFA_MOPEN:
2075 case NFA_MOPEN1:
2076 case NFA_MOPEN2:
2077 case NFA_MOPEN3:
2078 case NFA_MOPEN4:
2079 case NFA_MOPEN5:
2080 case NFA_MOPEN6:
2081 case NFA_MOPEN7:
2082 case NFA_MOPEN8:
2083 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002084 STRCPY(code, "NFA_MOPEN(x)");
2085 code[10] = c - NFA_MOPEN + '0';
2086 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002087 case NFA_MCLOSE:
2088 case NFA_MCLOSE1:
2089 case NFA_MCLOSE2:
2090 case NFA_MCLOSE3:
2091 case NFA_MCLOSE4:
2092 case NFA_MCLOSE5:
2093 case NFA_MCLOSE6:
2094 case NFA_MCLOSE7:
2095 case NFA_MCLOSE8:
2096 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002097 STRCPY(code, "NFA_MCLOSE(x)");
2098 code[11] = c - NFA_MCLOSE + '0';
2099 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002100#ifdef FEAT_SYN_HL
2101 case NFA_ZOPEN:
2102 case NFA_ZOPEN1:
2103 case NFA_ZOPEN2:
2104 case NFA_ZOPEN3:
2105 case NFA_ZOPEN4:
2106 case NFA_ZOPEN5:
2107 case NFA_ZOPEN6:
2108 case NFA_ZOPEN7:
2109 case NFA_ZOPEN8:
2110 case NFA_ZOPEN9:
2111 STRCPY(code, "NFA_ZOPEN(x)");
2112 code[10] = c - NFA_ZOPEN + '0';
2113 break;
2114 case NFA_ZCLOSE:
2115 case NFA_ZCLOSE1:
2116 case NFA_ZCLOSE2:
2117 case NFA_ZCLOSE3:
2118 case NFA_ZCLOSE4:
2119 case NFA_ZCLOSE5:
2120 case NFA_ZCLOSE6:
2121 case NFA_ZCLOSE7:
2122 case NFA_ZCLOSE8:
2123 case NFA_ZCLOSE9:
2124 STRCPY(code, "NFA_ZCLOSE(x)");
2125 code[11] = c - NFA_ZCLOSE + '0';
2126 break;
2127#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002128 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2129 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2130 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2131 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002132 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2133 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002134 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2135 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2136 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2137 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2138 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2139 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2140 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2141 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2142 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2143 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2144 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2145 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2146 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2147 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2148
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002149 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002150 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2151 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2152 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002153 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2154 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002155
2156 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2157 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2158 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2159 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2160 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2161 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2162 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2163
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2165 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2166 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2167 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2168 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2169 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2170 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2171 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2172 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2173 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2174 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2175 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2176 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2177 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2178 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2179 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2180
2181 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2182 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2183 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2184 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2185 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2186 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2187 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2188 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2189 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2190 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2191 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2192 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2193 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2194 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2195 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2196 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2197 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2198 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2199 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2200 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2201 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2202 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2203 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2204 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2205 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2206 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2207 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2208
2209 default:
2210 STRCPY(code, "CHAR(x)");
2211 code[5] = c;
2212 }
2213
2214 if (addnl == TRUE)
2215 STRCAT(code, " + NEWLINE ");
2216
2217}
2218
2219#ifdef ENABLE_LOG
2220static FILE *log_fd;
2221
2222/*
2223 * Print the postfix notation of the current regexp.
2224 */
2225 static void
2226nfa_postfix_dump(expr, retval)
2227 char_u *expr;
2228 int retval;
2229{
2230 int *p;
2231 FILE *f;
2232
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002233 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234 if (f != NULL)
2235 {
2236 fprintf(f, "\n-------------------------\n");
2237 if (retval == FAIL)
2238 fprintf(f, ">>> NFA engine failed ... \n");
2239 else if (retval == OK)
2240 fprintf(f, ">>> NFA engine succeeded !\n");
2241 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002242 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 {
2244 nfa_set_code(*p);
2245 fprintf(f, "%s, ", code);
2246 }
2247 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002248 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002249 fprintf(f, "%d ", *p);
2250 fprintf(f, "\n\n");
2251 fclose(f);
2252 }
2253}
2254
2255/*
2256 * Print the NFA starting with a root node "state".
2257 */
2258 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002259nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002260 FILE *debugf;
2261 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002262{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002263 garray_T indent;
2264
2265 ga_init2(&indent, 1, 64);
2266 ga_append(&indent, '\0');
2267 nfa_print_state2(debugf, state, &indent);
2268 ga_clear(&indent);
2269}
2270
2271 static void
2272nfa_print_state2(debugf, state, indent)
2273 FILE *debugf;
2274 nfa_state_T *state;
2275 garray_T *indent;
2276{
2277 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002278
2279 if (state == NULL)
2280 return;
2281
2282 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002283
2284 /* Output indent */
2285 p = (char_u *)indent->ga_data;
2286 if (indent->ga_len >= 3)
2287 {
2288 int last = indent->ga_len - 3;
2289 char_u save[2];
2290
2291 STRNCPY(save, &p[last], 2);
2292 STRNCPY(&p[last], "+-", 2);
2293 fprintf(debugf, " %s", p);
2294 STRNCPY(&p[last], save, 2);
2295 }
2296 else
2297 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002298
2299 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002300 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002301 code,
2302 state->c,
2303 abs(state->id),
2304 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002305 if (state->id < 0)
2306 return;
2307
2308 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002309
2310 /* grow indent for state->out */
2311 indent->ga_len -= 1;
2312 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002313 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002314 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002315 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002316 ga_append(indent, '\0');
2317
2318 nfa_print_state2(debugf, state->out, indent);
2319
2320 /* replace last part of indent for state->out1 */
2321 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002322 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002323 ga_append(indent, '\0');
2324
2325 nfa_print_state2(debugf, state->out1, indent);
2326
2327 /* shrink indent */
2328 indent->ga_len -= 3;
2329 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330}
2331
2332/*
2333 * Print the NFA state machine.
2334 */
2335 static void
2336nfa_dump(prog)
2337 nfa_regprog_T *prog;
2338{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002339 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340
2341 if (debugf != NULL)
2342 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002343 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002344
Bram Moolenaar473de612013-06-08 18:19:48 +02002345 if (prog->reganch)
2346 fprintf(debugf, "reganch: %d\n", prog->reganch);
2347 if (prog->regstart != NUL)
2348 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2349 prog->regstart, prog->regstart);
2350 if (prog->match_text != NULL)
2351 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002352
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002353 fclose(debugf);
2354 }
2355}
2356#endif /* ENABLE_LOG */
2357#endif /* DEBUG */
2358
2359/*
2360 * Parse r.e. @expr and convert it into postfix form.
2361 * Return the postfix string on success, NULL otherwise.
2362 */
2363 static int *
2364re2post()
2365{
2366 if (nfa_reg(REG_NOPAREN) == FAIL)
2367 return NULL;
2368 EMIT(NFA_MOPEN);
2369 return post_start;
2370}
2371
2372/* NB. Some of the code below is inspired by Russ's. */
2373
2374/*
2375 * Represents an NFA state plus zero or one or two arrows exiting.
2376 * if c == MATCH, no arrows out; matching state.
2377 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2378 * If c < 256, labeled arrow with character c to out.
2379 */
2380
2381static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2382
2383/*
2384 * Allocate and initialize nfa_state_T.
2385 */
2386 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002387alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002388 int c;
2389 nfa_state_T *out;
2390 nfa_state_T *out1;
2391{
2392 nfa_state_T *s;
2393
2394 if (istate >= nstate)
2395 return NULL;
2396
2397 s = &state_ptr[istate++];
2398
2399 s->c = c;
2400 s->out = out;
2401 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002402 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002403
2404 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002405 s->lastlist[0] = 0;
2406 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002407
2408 return s;
2409}
2410
2411/*
2412 * A partially built NFA without the matching state filled in.
2413 * Frag_T.start points at the start state.
2414 * Frag_T.out is a list of places that need to be set to the
2415 * next state for this fragment.
2416 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002417
2418/* Since the out pointers in the list are always
2419 * uninitialized, we use the pointers themselves
2420 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002422union Ptrlist
2423{
2424 Ptrlist *next;
2425 nfa_state_T *s;
2426};
2427
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002428struct Frag
2429{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002430 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 Ptrlist *out;
2432};
2433typedef struct Frag Frag_T;
2434
2435static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2436static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2437static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2438static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2439static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2440static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2441
2442/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002443 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 */
2445 static Frag_T
2446frag(start, out)
2447 nfa_state_T *start;
2448 Ptrlist *out;
2449{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002450 Frag_T n;
2451
2452 n.start = start;
2453 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002454 return n;
2455}
2456
2457/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458 * Create singleton list containing just outp.
2459 */
2460 static Ptrlist *
2461list1(outp)
2462 nfa_state_T **outp;
2463{
2464 Ptrlist *l;
2465
2466 l = (Ptrlist *)outp;
2467 l->next = NULL;
2468 return l;
2469}
2470
2471/*
2472 * Patch the list of states at out to point to start.
2473 */
2474 static void
2475patch(l, s)
2476 Ptrlist *l;
2477 nfa_state_T *s;
2478{
2479 Ptrlist *next;
2480
2481 for (; l; l = next)
2482 {
2483 next = l->next;
2484 l->s = s;
2485 }
2486}
2487
2488
2489/*
2490 * Join the two lists l1 and l2, returning the combination.
2491 */
2492 static Ptrlist *
2493append(l1, l2)
2494 Ptrlist *l1;
2495 Ptrlist *l2;
2496{
2497 Ptrlist *oldl1;
2498
2499 oldl1 = l1;
2500 while (l1->next)
2501 l1 = l1->next;
2502 l1->next = l2;
2503 return oldl1;
2504}
2505
2506/*
2507 * Stack used for transforming postfix form into NFA.
2508 */
2509static Frag_T empty;
2510
2511 static void
2512st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002513 int *postfix UNUSED;
2514 int *end UNUSED;
2515 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002516{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002517#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002518 FILE *df;
2519 int *p2;
2520
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002521 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522 if (df)
2523 {
2524 fprintf(df, "Error popping the stack!\n");
2525#ifdef DEBUG
2526 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2527#endif
2528 fprintf(df, "Postfix form is: ");
2529#ifdef DEBUG
2530 for (p2 = postfix; p2 < end; p2++)
2531 {
2532 nfa_set_code(*p2);
2533 fprintf(df, "%s, ", code);
2534 }
2535 nfa_set_code(*p);
2536 fprintf(df, "\nCurrent position is: ");
2537 for (p2 = postfix; p2 <= p; p2 ++)
2538 {
2539 nfa_set_code(*p2);
2540 fprintf(df, "%s, ", code);
2541 }
2542#else
2543 for (p2 = postfix; p2 < end; p2++)
2544 {
2545 fprintf(df, "%d, ", *p2);
2546 }
2547 fprintf(df, "\nCurrent position is: ");
2548 for (p2 = postfix; p2 <= p; p2 ++)
2549 {
2550 fprintf(df, "%d, ", *p2);
2551 }
2552#endif
2553 fprintf(df, "\n--------------------------\n");
2554 fclose(df);
2555 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002556#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002557 EMSG(_("E874: (NFA) Could not pop the stack !"));
2558}
2559
2560/*
2561 * Push an item onto the stack.
2562 */
2563 static void
2564st_push(s, p, stack_end)
2565 Frag_T s;
2566 Frag_T **p;
2567 Frag_T *stack_end;
2568{
2569 Frag_T *stackp = *p;
2570
2571 if (stackp >= stack_end)
2572 return;
2573 *stackp = s;
2574 *p = *p + 1;
2575}
2576
2577/*
2578 * Pop an item from the stack.
2579 */
2580 static Frag_T
2581st_pop(p, stack)
2582 Frag_T **p;
2583 Frag_T *stack;
2584{
2585 Frag_T *stackp;
2586
2587 *p = *p - 1;
2588 stackp = *p;
2589 if (stackp < stack)
2590 return empty;
2591 return **p;
2592}
2593
2594/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002595 * Estimate the maximum byte length of anything matching "state".
2596 * When unknown or unlimited return -1.
2597 */
2598 static int
2599nfa_max_width(startstate, depth)
2600 nfa_state_T *startstate;
2601 int depth;
2602{
2603 int l, r;
2604 nfa_state_T *state = startstate;
2605 int len = 0;
2606
2607 /* detect looping in a NFA_SPLIT */
2608 if (depth > 4)
2609 return -1;
2610
2611 for (;;)
2612 {
2613 switch (state->c)
2614 {
2615 case NFA_END_INVISIBLE:
2616 case NFA_END_INVISIBLE_NEG:
2617 /* the end, return what we have */
2618 return len;
2619
2620 case NFA_SPLIT:
2621 /* two alternatives, use the maximum */
2622 l = nfa_max_width(state->out, depth + 1);
2623 r = nfa_max_width(state->out1, depth + 1);
2624 if (l < 0 || r < 0)
2625 return -1;
2626 return len + (l > r ? l : r);
2627
2628 case NFA_ANY:
2629 case NFA_START_COLL:
2630 case NFA_START_NEG_COLL:
2631 /* matches some character, including composing chars */
2632#ifdef FEAT_MBYTE
2633 if (enc_utf8)
2634 len += MB_MAXBYTES;
2635 else if (has_mbyte)
2636 len += 2;
2637 else
2638#endif
2639 ++len;
2640 if (state->c != NFA_ANY)
2641 {
2642 /* skip over the characters */
2643 state = state->out1->out;
2644 continue;
2645 }
2646 break;
2647
2648 case NFA_DIGIT:
2649 case NFA_WHITE:
2650 case NFA_HEX:
2651 case NFA_OCTAL:
2652 /* ascii */
2653 ++len;
2654 break;
2655
2656 case NFA_IDENT:
2657 case NFA_SIDENT:
2658 case NFA_KWORD:
2659 case NFA_SKWORD:
2660 case NFA_FNAME:
2661 case NFA_SFNAME:
2662 case NFA_PRINT:
2663 case NFA_SPRINT:
2664 case NFA_NWHITE:
2665 case NFA_NDIGIT:
2666 case NFA_NHEX:
2667 case NFA_NOCTAL:
2668 case NFA_WORD:
2669 case NFA_NWORD:
2670 case NFA_HEAD:
2671 case NFA_NHEAD:
2672 case NFA_ALPHA:
2673 case NFA_NALPHA:
2674 case NFA_LOWER:
2675 case NFA_NLOWER:
2676 case NFA_UPPER:
2677 case NFA_NUPPER:
2678 /* possibly non-ascii */
2679#ifdef FEAT_MBYTE
2680 if (has_mbyte)
2681 len += 3;
2682 else
2683#endif
2684 ++len;
2685 break;
2686
2687 case NFA_START_INVISIBLE:
2688 case NFA_START_INVISIBLE_NEG:
2689 case NFA_START_INVISIBLE_BEFORE:
2690 case NFA_START_INVISIBLE_BEFORE_NEG:
2691 /* zero-width, out1 points to the END state */
2692 state = state->out1->out;
2693 continue;
2694
2695 case NFA_BACKREF1:
2696 case NFA_BACKREF2:
2697 case NFA_BACKREF3:
2698 case NFA_BACKREF4:
2699 case NFA_BACKREF5:
2700 case NFA_BACKREF6:
2701 case NFA_BACKREF7:
2702 case NFA_BACKREF8:
2703 case NFA_BACKREF9:
2704#ifdef FEAT_SYN_HL
2705 case NFA_ZREF1:
2706 case NFA_ZREF2:
2707 case NFA_ZREF3:
2708 case NFA_ZREF4:
2709 case NFA_ZREF5:
2710 case NFA_ZREF6:
2711 case NFA_ZREF7:
2712 case NFA_ZREF8:
2713 case NFA_ZREF9:
2714#endif
2715 case NFA_NEWL:
2716 case NFA_SKIP:
2717 /* unknown width */
2718 return -1;
2719
2720 case NFA_BOL:
2721 case NFA_EOL:
2722 case NFA_BOF:
2723 case NFA_EOF:
2724 case NFA_BOW:
2725 case NFA_EOW:
2726 case NFA_MOPEN:
2727 case NFA_MOPEN1:
2728 case NFA_MOPEN2:
2729 case NFA_MOPEN3:
2730 case NFA_MOPEN4:
2731 case NFA_MOPEN5:
2732 case NFA_MOPEN6:
2733 case NFA_MOPEN7:
2734 case NFA_MOPEN8:
2735 case NFA_MOPEN9:
2736#ifdef FEAT_SYN_HL
2737 case NFA_ZOPEN:
2738 case NFA_ZOPEN1:
2739 case NFA_ZOPEN2:
2740 case NFA_ZOPEN3:
2741 case NFA_ZOPEN4:
2742 case NFA_ZOPEN5:
2743 case NFA_ZOPEN6:
2744 case NFA_ZOPEN7:
2745 case NFA_ZOPEN8:
2746 case NFA_ZOPEN9:
2747 case NFA_ZCLOSE:
2748 case NFA_ZCLOSE1:
2749 case NFA_ZCLOSE2:
2750 case NFA_ZCLOSE3:
2751 case NFA_ZCLOSE4:
2752 case NFA_ZCLOSE5:
2753 case NFA_ZCLOSE6:
2754 case NFA_ZCLOSE7:
2755 case NFA_ZCLOSE8:
2756 case NFA_ZCLOSE9:
2757#endif
2758 case NFA_MCLOSE:
2759 case NFA_MCLOSE1:
2760 case NFA_MCLOSE2:
2761 case NFA_MCLOSE3:
2762 case NFA_MCLOSE4:
2763 case NFA_MCLOSE5:
2764 case NFA_MCLOSE6:
2765 case NFA_MCLOSE7:
2766 case NFA_MCLOSE8:
2767 case NFA_MCLOSE9:
2768 case NFA_NOPEN:
2769 case NFA_NCLOSE:
2770
2771 case NFA_LNUM_GT:
2772 case NFA_LNUM_LT:
2773 case NFA_COL_GT:
2774 case NFA_COL_LT:
2775 case NFA_VCOL_GT:
2776 case NFA_VCOL_LT:
2777 case NFA_MARK_GT:
2778 case NFA_MARK_LT:
2779 case NFA_VISUAL:
2780 case NFA_LNUM:
2781 case NFA_CURSOR:
2782 case NFA_COL:
2783 case NFA_VCOL:
2784 case NFA_MARK:
2785
2786 case NFA_ZSTART:
2787 case NFA_ZEND:
2788 case NFA_OPT_CHARS:
2789 case NFA_SKIP_CHAR:
2790 case NFA_START_PATTERN:
2791 case NFA_END_PATTERN:
2792 case NFA_COMPOSING:
2793 case NFA_END_COMPOSING:
2794 /* zero-width */
2795 break;
2796
2797 default:
2798 if (state->c < 0)
2799 /* don't know what this is */
2800 return -1;
2801 /* normal character */
2802 len += MB_CHAR2LEN(state->c);
2803 break;
2804 }
2805
2806 /* normal way to continue */
2807 state = state->out;
2808 }
2809
2810 /* unrecognized */
2811 return -1;
2812}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02002813
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002814/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002815 * Convert a postfix form into its equivalent NFA.
2816 * Return the NFA start state on success, NULL otherwise.
2817 */
2818 static nfa_state_T *
2819post2nfa(postfix, end, nfa_calc_size)
2820 int *postfix;
2821 int *end;
2822 int nfa_calc_size;
2823{
2824 int *p;
2825 int mopen;
2826 int mclose;
2827 Frag_T *stack = NULL;
2828 Frag_T *stackp = NULL;
2829 Frag_T *stack_end = NULL;
2830 Frag_T e1;
2831 Frag_T e2;
2832 Frag_T e;
2833 nfa_state_T *s;
2834 nfa_state_T *s1;
2835 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002836 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002837
2838 if (postfix == NULL)
2839 return NULL;
2840
Bram Moolenaar053bb602013-05-20 13:55:21 +02002841#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842#define POP() st_pop(&stackp, stack); \
2843 if (stackp < stack) \
2844 { \
2845 st_error(postfix, end, p); \
2846 return NULL; \
2847 }
2848
2849 if (nfa_calc_size == FALSE)
2850 {
2851 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002852 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002853 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002854 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002855 }
2856
2857 for (p = postfix; p < end; ++p)
2858 {
2859 switch (*p)
2860 {
2861 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002862 /* Concatenation.
2863 * Pay attention: this operator does not exist in the r.e. itself
2864 * (it is implicit, really). It is added when r.e. is translated
2865 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002866 if (nfa_calc_size == TRUE)
2867 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002868 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869 break;
2870 }
2871 e2 = POP();
2872 e1 = POP();
2873 patch(e1.out, e2.start);
2874 PUSH(frag(e1.start, e2.out));
2875 break;
2876
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877 case NFA_OR:
2878 /* Alternation */
2879 if (nfa_calc_size == TRUE)
2880 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002881 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 break;
2883 }
2884 e2 = POP();
2885 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002886 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002887 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002888 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002889 PUSH(frag(s, append(e1.out, e2.out)));
2890 break;
2891
2892 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002893 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002894 if (nfa_calc_size == TRUE)
2895 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002896 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897 break;
2898 }
2899 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002900 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002902 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002903 patch(e.out, s);
2904 PUSH(frag(s, list1(&s->out1)));
2905 break;
2906
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002907 case NFA_STAR_NONGREEDY:
2908 /* Zero or more, prefer zero */
2909 if (nfa_calc_size == TRUE)
2910 {
2911 nstate++;
2912 break;
2913 }
2914 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002915 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002916 if (s == NULL)
2917 goto theend;
2918 patch(e.out, s);
2919 PUSH(frag(s, list1(&s->out)));
2920 break;
2921
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002922 case NFA_QUEST:
2923 /* one or zero atoms=> greedy match */
2924 if (nfa_calc_size == TRUE)
2925 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002926 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002927 break;
2928 }
2929 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002930 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002932 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002933 PUSH(frag(s, append(e.out, list1(&s->out1))));
2934 break;
2935
2936 case NFA_QUEST_NONGREEDY:
2937 /* zero or one atoms => non-greedy match */
2938 if (nfa_calc_size == TRUE)
2939 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002940 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002941 break;
2942 }
2943 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002944 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002946 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002947 PUSH(frag(s, append(e.out, list1(&s->out))));
2948 break;
2949
Bram Moolenaar417bad22013-06-07 14:08:30 +02002950 case NFA_END_COLL:
2951 case NFA_END_NEG_COLL:
2952 /* On the stack is the sequence starting with NFA_START_COLL or
2953 * NFA_START_NEG_COLL and all possible characters. Patch it to
2954 * add the output to the start. */
2955 if (nfa_calc_size == TRUE)
2956 {
2957 nstate++;
2958 break;
2959 }
2960 e = POP();
2961 s = alloc_state(NFA_END_COLL, NULL, NULL);
2962 if (s == NULL)
2963 goto theend;
2964 patch(e.out, s);
2965 e.start->out1 = s;
2966 PUSH(frag(e.start, list1(&s->out)));
2967 break;
2968
2969 case NFA_RANGE:
2970 /* Before this are two characters, the low and high end of a
2971 * range. Turn them into two states with MIN and MAX. */
2972 if (nfa_calc_size == TRUE)
2973 {
2974 /* nstate += 0; */
2975 break;
2976 }
2977 e2 = POP();
2978 e1 = POP();
2979 e2.start->val = e2.start->c;
2980 e2.start->c = NFA_RANGE_MAX;
2981 e1.start->val = e1.start->c;
2982 e1.start->c = NFA_RANGE_MIN;
2983 patch(e1.out, e2.start);
2984 PUSH(frag(e1.start, e2.out));
2985 break;
2986
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002987 case NFA_SKIP_CHAR:
2988 /* Symbol of 0-length, Used in a repetition
2989 * with max/min count of 0 */
2990 if (nfa_calc_size == TRUE)
2991 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002992 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002993 break;
2994 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002995 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002996 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002997 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002998 PUSH(frag(s, list1(&s->out)));
2999 break;
3000
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003001 case NFA_OPT_CHARS:
3002 {
3003 int n;
3004
3005 /* \%[abc] */
3006 n = *++p; /* get number of characters */
3007 if (nfa_calc_size == TRUE)
3008 {
3009 nstate += n;
3010 break;
3011 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003012 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003013 e1.out = NULL; /* stores list with out1's */
3014 s1 = NULL; /* previous NFA_SPLIT to connect to */
3015 while (n-- > 0)
3016 {
3017 e = POP(); /* get character */
3018 s = alloc_state(NFA_SPLIT, e.start, NULL);
3019 if (s == NULL)
3020 goto theend;
3021 if (e1.out == NULL)
3022 e1 = e;
3023 patch(e.out, s1);
3024 append(e1.out, list1(&s->out1));
3025 s1 = s;
3026 }
3027 PUSH(frag(s, e1.out));
3028 break;
3029 }
3030
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003031 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003032 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003033 case NFA_PREV_ATOM_JUST_BEFORE:
3034 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003035 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003036 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003037 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3038 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003039 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003040 int start_state;
3041 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003042 int n = 0;
3043 nfa_state_T *zend;
3044 nfa_state_T *skip;
3045
Bram Moolenaardecd9542013-06-07 16:31:50 +02003046 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003047 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003048 case NFA_PREV_ATOM_NO_WIDTH:
3049 start_state = NFA_START_INVISIBLE;
3050 end_state = NFA_END_INVISIBLE;
3051 break;
3052 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3053 start_state = NFA_START_INVISIBLE_NEG;
3054 end_state = NFA_END_INVISIBLE_NEG;
3055 break;
3056 case NFA_PREV_ATOM_JUST_BEFORE:
3057 start_state = NFA_START_INVISIBLE_BEFORE;
3058 end_state = NFA_END_INVISIBLE;
3059 break;
3060 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3061 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3062 end_state = NFA_END_INVISIBLE_NEG;
3063 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003064 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003065 start_state = NFA_START_PATTERN;
3066 end_state = NFA_END_PATTERN;
3067 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003068 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003069
3070 if (before)
3071 n = *++p; /* get the count */
3072
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003073 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003074 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003075 * The \@<= operator: match for the preceding atom.
3076 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003077 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003078 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003079
3080 if (nfa_calc_size == TRUE)
3081 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003082 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003083 break;
3084 }
3085 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003086 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003087 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003088 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003089
Bram Moolenaar87953742013-06-05 18:52:40 +02003090 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003091 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003092 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003093 if (pattern)
3094 {
3095 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3096 skip = alloc_state(NFA_SKIP, NULL, NULL);
3097 zend = alloc_state(NFA_ZEND, s1, NULL);
3098 s1->out= skip;
3099 patch(e.out, zend);
3100 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003101 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003102 else
3103 {
3104 patch(e.out, s1);
3105 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003106 if (before)
3107 {
3108 if (n <= 0)
3109 /* See if we can guess the maximum width, it avoids a
3110 * lot of pointless tries. */
3111 n = nfa_max_width(e.start, 0);
3112 s->val = n; /* store the count */
3113 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003114 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003115 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003116 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003117
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003118#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003119 case NFA_COMPOSING: /* char with composing char */
3120#if 0
3121 /* TODO */
3122 if (regflags & RF_ICOMBINE)
3123 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003124 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003125 }
3126#endif
3127 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003128#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003129
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003130 case NFA_MOPEN: /* \( \) Submatch */
3131 case NFA_MOPEN1:
3132 case NFA_MOPEN2:
3133 case NFA_MOPEN3:
3134 case NFA_MOPEN4:
3135 case NFA_MOPEN5:
3136 case NFA_MOPEN6:
3137 case NFA_MOPEN7:
3138 case NFA_MOPEN8:
3139 case NFA_MOPEN9:
3140#ifdef FEAT_SYN_HL
3141 case NFA_ZOPEN: /* \z( \) Submatch */
3142 case NFA_ZOPEN1:
3143 case NFA_ZOPEN2:
3144 case NFA_ZOPEN3:
3145 case NFA_ZOPEN4:
3146 case NFA_ZOPEN5:
3147 case NFA_ZOPEN6:
3148 case NFA_ZOPEN7:
3149 case NFA_ZOPEN8:
3150 case NFA_ZOPEN9:
3151#endif
3152 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003153 if (nfa_calc_size == TRUE)
3154 {
3155 nstate += 2;
3156 break;
3157 }
3158
3159 mopen = *p;
3160 switch (*p)
3161 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003162 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3163#ifdef FEAT_SYN_HL
3164 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3165 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3166 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3167 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3168 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3169 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3170 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3171 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3172 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3173 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3174#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003175#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003176 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003177#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003178 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003179 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180 mclose = *p + NSUBEXP;
3181 break;
3182 }
3183
3184 /* Allow "NFA_MOPEN" as a valid postfix representation for
3185 * the empty regexp "". In this case, the NFA will be
3186 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3187 * empty groups of parenthesis, and empty mbyte chars */
3188 if (stackp == stack)
3189 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003190 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003191 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003192 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003193 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003194 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003195 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003196 patch(list1(&s->out), s1);
3197 PUSH(frag(s, list1(&s1->out)));
3198 break;
3199 }
3200
3201 /* At least one node was emitted before NFA_MOPEN, so
3202 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3203 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003204 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003206 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207
Bram Moolenaar525666f2013-06-02 16:40:55 +02003208 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003210 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 patch(e.out, s1);
3212
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003213#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003214 if (mopen == NFA_COMPOSING)
3215 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003217#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218
3219 PUSH(frag(s, list1(&s1->out)));
3220 break;
3221
Bram Moolenaar5714b802013-05-28 22:03:20 +02003222 case NFA_BACKREF1:
3223 case NFA_BACKREF2:
3224 case NFA_BACKREF3:
3225 case NFA_BACKREF4:
3226 case NFA_BACKREF5:
3227 case NFA_BACKREF6:
3228 case NFA_BACKREF7:
3229 case NFA_BACKREF8:
3230 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003231#ifdef FEAT_SYN_HL
3232 case NFA_ZREF1:
3233 case NFA_ZREF2:
3234 case NFA_ZREF3:
3235 case NFA_ZREF4:
3236 case NFA_ZREF5:
3237 case NFA_ZREF6:
3238 case NFA_ZREF7:
3239 case NFA_ZREF8:
3240 case NFA_ZREF9:
3241#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003242 if (nfa_calc_size == TRUE)
3243 {
3244 nstate += 2;
3245 break;
3246 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003247 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003248 if (s == NULL)
3249 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003250 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003251 if (s1 == NULL)
3252 goto theend;
3253 patch(list1(&s->out), s1);
3254 PUSH(frag(s, list1(&s1->out)));
3255 break;
3256
Bram Moolenaar423532e2013-05-29 21:14:42 +02003257 case NFA_LNUM:
3258 case NFA_LNUM_GT:
3259 case NFA_LNUM_LT:
3260 case NFA_VCOL:
3261 case NFA_VCOL_GT:
3262 case NFA_VCOL_LT:
3263 case NFA_COL:
3264 case NFA_COL_GT:
3265 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003266 case NFA_MARK:
3267 case NFA_MARK_GT:
3268 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003269 {
3270 int n = *++p; /* lnum, col or mark name */
3271
Bram Moolenaar423532e2013-05-29 21:14:42 +02003272 if (nfa_calc_size == TRUE)
3273 {
3274 nstate += 1;
3275 break;
3276 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003277 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003278 if (s == NULL)
3279 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003280 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003281 PUSH(frag(s, list1(&s->out)));
3282 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003283 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003284
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003285 case NFA_ZSTART:
3286 case NFA_ZEND:
3287 default:
3288 /* Operands */
3289 if (nfa_calc_size == TRUE)
3290 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003291 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 break;
3293 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003294 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003296 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 PUSH(frag(s, list1(&s->out)));
3298 break;
3299
3300 } /* switch(*p) */
3301
3302 } /* for(p = postfix; *p; ++p) */
3303
3304 if (nfa_calc_size == TRUE)
3305 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003306 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003307 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 }
3309
3310 e = POP();
3311 if (stackp != stack)
3312 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3313
3314 if (istate >= nstate)
3315 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3316
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003317 matchstate = &state_ptr[istate++]; /* the match state */
3318 matchstate->c = NFA_MATCH;
3319 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003320 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003321
3322 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003323 ret = e.start;
3324
3325theend:
3326 vim_free(stack);
3327 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328
3329#undef POP1
3330#undef PUSH1
3331#undef POP2
3332#undef PUSH2
3333#undef POP
3334#undef PUSH
3335}
3336
Bram Moolenaara2947e22013-06-11 22:44:09 +02003337/*
3338 * After building the NFA program, inspect it to add optimization hints.
3339 */
3340 static void
3341nfa_postprocess(prog)
3342 nfa_regprog_T *prog;
3343{
3344 int i;
3345 int c;
3346
3347 for (i = 0; i < prog->nstate; ++i)
3348 {
3349 c = prog->state[i].c;
3350 if (c == NFA_START_INVISIBLE
3351 || c == NFA_START_INVISIBLE_NEG
3352 || c == NFA_START_INVISIBLE_BEFORE
3353 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3354 {
3355 int directly;
3356
3357 /* Do it directly when what follows is possibly the end of the
3358 * match. */
3359 if (match_follows(prog->state[i].out1->out, 0))
3360 directly = TRUE;
3361 else
3362 {
3363 int ch_invisible = failure_chance(prog->state[i].out, 0);
3364 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3365
3366 /* Postpone when the invisible match is expensive or has a
3367 * lower chance of failing. */
3368 if (c == NFA_START_INVISIBLE_BEFORE
3369 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3370 {
3371 /* "before" matches are very expensive when
3372 * unbounded, always prefer what follows then,
3373 * unless what follows will always match.
3374 * Otherwise strongly prefer what follows. */
3375 if (prog->state[i].val <= 0 && ch_follows > 0)
3376 directly = FALSE;
3377 else
3378 directly = ch_follows * 10 < ch_invisible;
3379 }
3380 else
3381 {
3382 /* normal invisible, first do the one with the
3383 * highest failure chance */
3384 directly = ch_follows < ch_invisible;
3385 }
3386 }
3387 if (directly)
3388 /* switch to the _FIRST state */
3389 ++prog->state[i].c;
3390 }
3391 }
3392}
3393
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003394/****************************************************************
3395 * NFA execution code.
3396 ****************************************************************/
3397
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003398typedef struct
3399{
3400 int in_use; /* number of subexpr with useful info */
3401
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003402 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003403 union
3404 {
3405 struct multipos
3406 {
3407 lpos_T start;
3408 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003409 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003410 struct linepos
3411 {
3412 char_u *start;
3413 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003414 } line[NSUBEXP];
3415 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003416} regsub_T;
3417
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003418typedef struct
3419{
3420 regsub_T norm; /* \( .. \) matches */
3421#ifdef FEAT_SYN_HL
3422 regsub_T synt; /* \z( .. \) matches */
3423#endif
3424} regsubs_T;
3425
Bram Moolenaara2d95102013-06-04 14:23:05 +02003426/* nfa_pim_T stores a Postponed Invisible Match. */
3427typedef struct nfa_pim_S nfa_pim_T;
3428struct nfa_pim_S
3429{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003430 int result; /* NFA_PIM_*, see below */
3431 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003432 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003433 union
3434 {
3435 lpos_T pos;
3436 char_u *ptr;
3437 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003438};
3439
3440/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003441#define NFA_PIM_UNUSED 0 /* pim not used */
3442#define NFA_PIM_TODO 1 /* pim not done yet */
3443#define NFA_PIM_MATCH 2 /* pim executed, matches */
3444#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003445
3446
Bram Moolenaar963fee22013-05-26 21:47:28 +02003447/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003448typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449{
3450 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003451 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003452 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3453 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003454 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003455} nfa_thread_T;
3456
Bram Moolenaar963fee22013-05-26 21:47:28 +02003457/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458typedef struct
3459{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003460 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003461 int n; /* nr of states currently in "t" */
3462 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003463 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003464} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465
Bram Moolenaar5714b802013-05-28 22:03:20 +02003466#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003467static void log_subsexpr __ARGS((regsubs_T *subs));
3468static void log_subexpr __ARGS((regsub_T *sub));
3469
3470 static void
3471log_subsexpr(subs)
3472 regsubs_T *subs;
3473{
3474 log_subexpr(&subs->norm);
3475# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003476 if (nfa_has_zsubexpr)
3477 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003478# endif
3479}
3480
Bram Moolenaar5714b802013-05-28 22:03:20 +02003481 static void
3482log_subexpr(sub)
3483 regsub_T *sub;
3484{
3485 int j;
3486
3487 for (j = 0; j < sub->in_use; j++)
3488 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003489 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003490 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003491 sub->list.multi[j].start.col,
3492 (int)sub->list.multi[j].start.lnum,
3493 sub->list.multi[j].end.col,
3494 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003495 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003496 {
3497 char *s = (char *)sub->list.line[j].start;
3498 char *e = (char *)sub->list.line[j].end;
3499
Bram Moolenaar87953742013-06-05 18:52:40 +02003500 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003501 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003502 s == NULL ? "NULL" : s,
3503 e == NULL ? "NULL" : e);
3504 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003505}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003506
3507 static char *
3508pim_info(nfa_pim_T *pim)
3509{
3510 static char buf[30];
3511
3512 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3513 buf[0] = NUL;
3514 else
3515 {
3516 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3517 : (int)(pim->end.ptr - reginput));
3518 }
3519 return buf;
3520}
3521
Bram Moolenaar5714b802013-05-28 22:03:20 +02003522#endif
3523
Bram Moolenaar963fee22013-05-26 21:47:28 +02003524/* Used during execution: whether a match has been found. */
3525static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003526
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003527static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003528static void clear_sub __ARGS((regsub_T *sub));
3529static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3530static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003531static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003532static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
3533static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003534static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003535static 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 +02003536
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003537/*
3538 * Copy postponed invisible match info from "from" to "to".
3539 */
3540 static void
3541copy_pim(to, from)
3542 nfa_pim_T *to;
3543 nfa_pim_T *from;
3544{
3545 to->result = from->result;
3546 to->state = from->state;
3547 copy_sub(&to->subs.norm, &from->subs.norm);
3548#ifdef FEAT_SYN_HL
3549 if (nfa_has_zsubexpr)
3550 copy_sub(&to->subs.synt, &from->subs.synt);
3551#endif
3552 to->end = from->end;
3553}
3554
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003555 static void
3556clear_sub(sub)
3557 regsub_T *sub;
3558{
3559 if (REG_MULTI)
3560 /* Use 0xff to set lnum to -1 */
3561 vim_memset(sub->list.multi, 0xff,
3562 sizeof(struct multipos) * nfa_nsubexpr);
3563 else
3564 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3565 sub->in_use = 0;
3566}
3567
3568/*
3569 * Copy the submatches from "from" to "to".
3570 */
3571 static void
3572copy_sub(to, from)
3573 regsub_T *to;
3574 regsub_T *from;
3575{
3576 to->in_use = from->in_use;
3577 if (from->in_use > 0)
3578 {
3579 /* Copy the match start and end positions. */
3580 if (REG_MULTI)
3581 mch_memmove(&to->list.multi[0],
3582 &from->list.multi[0],
3583 sizeof(struct multipos) * from->in_use);
3584 else
3585 mch_memmove(&to->list.line[0],
3586 &from->list.line[0],
3587 sizeof(struct linepos) * from->in_use);
3588 }
3589}
3590
3591/*
3592 * Like copy_sub() but exclude the main match.
3593 */
3594 static void
3595copy_sub_off(to, from)
3596 regsub_T *to;
3597 regsub_T *from;
3598{
3599 if (to->in_use < from->in_use)
3600 to->in_use = from->in_use;
3601 if (from->in_use > 1)
3602 {
3603 /* Copy the match start and end positions. */
3604 if (REG_MULTI)
3605 mch_memmove(&to->list.multi[1],
3606 &from->list.multi[1],
3607 sizeof(struct multipos) * (from->in_use - 1));
3608 else
3609 mch_memmove(&to->list.line[1],
3610 &from->list.line[1],
3611 sizeof(struct linepos) * (from->in_use - 1));
3612 }
3613}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003614
Bram Moolenaar428e9872013-05-30 17:05:39 +02003615/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003616 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003617 */
3618 static int
3619sub_equal(sub1, sub2)
3620 regsub_T *sub1;
3621 regsub_T *sub2;
3622{
3623 int i;
3624 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003625 linenr_T s1;
3626 linenr_T s2;
3627 char_u *sp1;
3628 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003629
3630 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3631 if (REG_MULTI)
3632 {
3633 for (i = 0; i < todo; ++i)
3634 {
3635 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003636 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003637 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003638 s1 = 0;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003639 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003640 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003641 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003642 s2 = 0;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003643 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003644 return FALSE;
3645 if (s1 != 0 && sub1->list.multi[i].start.col
3646 != sub2->list.multi[i].start.col)
3647 return FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003648 }
3649 }
3650 else
3651 {
3652 for (i = 0; i < todo; ++i)
3653 {
3654 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003655 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003656 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003657 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003658 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003659 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003660 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003661 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003662 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003663 return FALSE;
3664 }
3665 }
3666
3667 return TRUE;
3668}
3669
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003670#ifdef ENABLE_LOG
3671 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003672report_state(char *action,
3673 regsub_T *sub,
3674 nfa_state_T *state,
3675 int lid,
3676 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003677{
3678 int col;
3679
3680 if (sub->in_use <= 0)
3681 col = -1;
3682 else if (REG_MULTI)
3683 col = sub->list.multi[0].start.col;
3684 else
3685 col = (int)(sub->list.line[0].start - regline);
3686 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003687 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3688 action, abs(state->id), lid, state->c, code, col,
3689 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003690}
3691#endif
3692
Bram Moolenaar43e02982013-06-07 17:31:29 +02003693/*
3694 * Return TRUE if the same state is already in list "l" with the same
3695 * positions as "subs".
3696 */
3697 static int
3698has_state_with_pos(l, state, subs)
3699 nfa_list_T *l; /* runtime state list */
3700 nfa_state_T *state; /* state to update */
3701 regsubs_T *subs; /* pointers to subexpressions */
3702{
3703 nfa_thread_T *thread;
3704 int i;
3705
3706 for (i = 0; i < l->n; ++i)
3707 {
3708 thread = &l->t[i];
3709 if (thread->state->id == state->id
3710 && sub_equal(&thread->subs.norm, &subs->norm)
3711#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003712 && (!nfa_has_zsubexpr
3713 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003714#endif
3715 )
3716 return TRUE;
3717 }
3718 return FALSE;
3719}
3720
3721/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003722 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3723 */
3724 static int
3725match_follows(startstate, depth)
3726 nfa_state_T *startstate;
3727 int depth;
3728{
3729 nfa_state_T *state = startstate;
3730
3731 /* avoid too much recursion */
3732 if (depth > 10)
3733 return FALSE;
3734
3735 for (;;)
3736 {
3737 switch (state->c)
3738 {
3739 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003740 case NFA_MCLOSE:
3741 case NFA_END_INVISIBLE:
3742 case NFA_END_INVISIBLE_NEG:
3743 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003744 return TRUE;
3745
3746 case NFA_SPLIT:
3747 return match_follows(state->out, depth + 1)
3748 || match_follows(state->out1, depth + 1);
3749
3750 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003751 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003752 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003753 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003754 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003755 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003756 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003757 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003758 case NFA_COMPOSING:
3759 /* skip ahead to next state */
3760 state = state->out1->out;
3761 break;
3762
3763 case NFA_ANY:
3764 case NFA_IDENT:
3765 case NFA_SIDENT:
3766 case NFA_KWORD:
3767 case NFA_SKWORD:
3768 case NFA_FNAME:
3769 case NFA_SFNAME:
3770 case NFA_PRINT:
3771 case NFA_SPRINT:
3772 case NFA_WHITE:
3773 case NFA_NWHITE:
3774 case NFA_DIGIT:
3775 case NFA_NDIGIT:
3776 case NFA_HEX:
3777 case NFA_NHEX:
3778 case NFA_OCTAL:
3779 case NFA_NOCTAL:
3780 case NFA_WORD:
3781 case NFA_NWORD:
3782 case NFA_HEAD:
3783 case NFA_NHEAD:
3784 case NFA_ALPHA:
3785 case NFA_NALPHA:
3786 case NFA_LOWER:
3787 case NFA_NLOWER:
3788 case NFA_UPPER:
3789 case NFA_NUPPER:
3790 case NFA_START_COLL:
3791 case NFA_START_NEG_COLL:
3792 case NFA_NEWL:
3793 /* state will advance input */
3794 return FALSE;
3795
3796 default:
3797 if (state->c > 0)
3798 /* state will advance input */
3799 return FALSE;
3800
3801 /* Others: zero-width or possibly zero-width, might still find
3802 * a match at the same position, keep looking. */
3803 break;
3804 }
3805 state = state->out;
3806 }
3807 return FALSE;
3808}
3809
3810
3811/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003812 * Return TRUE if "state" is already in list "l".
3813 */
3814 static int
3815state_in_list(l, state, subs)
3816 nfa_list_T *l; /* runtime state list */
3817 nfa_state_T *state; /* state to update */
3818 regsubs_T *subs; /* pointers to subexpressions */
3819{
3820 if (state->lastlist[nfa_ll_index] == l->id)
3821 {
3822 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3823 return TRUE;
3824 }
3825 return FALSE;
3826}
3827
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003828 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003829addstate(l, state, subs, pim, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003830 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003832 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003833 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003834 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003835{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003836 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003837 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003838 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003839 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003840 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003841 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003842 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003843#ifdef ENABLE_LOG
3844 int did_print = FALSE;
3845#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003847 switch (state->c)
3848 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849 case NFA_NCLOSE:
3850 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003851 case NFA_MCLOSE1:
3852 case NFA_MCLOSE2:
3853 case NFA_MCLOSE3:
3854 case NFA_MCLOSE4:
3855 case NFA_MCLOSE5:
3856 case NFA_MCLOSE6:
3857 case NFA_MCLOSE7:
3858 case NFA_MCLOSE8:
3859 case NFA_MCLOSE9:
3860#ifdef FEAT_SYN_HL
3861 case NFA_ZCLOSE:
3862 case NFA_ZCLOSE1:
3863 case NFA_ZCLOSE2:
3864 case NFA_ZCLOSE3:
3865 case NFA_ZCLOSE4:
3866 case NFA_ZCLOSE5:
3867 case NFA_ZCLOSE6:
3868 case NFA_ZCLOSE7:
3869 case NFA_ZCLOSE8:
3870 case NFA_ZCLOSE9:
3871#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003872 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003873 case NFA_SPLIT:
3874 case NFA_NOPEN:
3875 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003876 /* These nodes are not added themselves but their "out" and/or
3877 * "out1" may be added below. */
3878 break;
3879
3880 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003881 case NFA_MOPEN1:
3882 case NFA_MOPEN2:
3883 case NFA_MOPEN3:
3884 case NFA_MOPEN4:
3885 case NFA_MOPEN5:
3886 case NFA_MOPEN6:
3887 case NFA_MOPEN7:
3888 case NFA_MOPEN8:
3889 case NFA_MOPEN9:
3890#ifdef FEAT_SYN_HL
3891 case NFA_ZOPEN:
3892 case NFA_ZOPEN1:
3893 case NFA_ZOPEN2:
3894 case NFA_ZOPEN3:
3895 case NFA_ZOPEN4:
3896 case NFA_ZOPEN5:
3897 case NFA_ZOPEN6:
3898 case NFA_ZOPEN7:
3899 case NFA_ZOPEN8:
3900 case NFA_ZOPEN9:
3901#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003902 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003903 /* These nodes do not need to be added, but we need to bail out
3904 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003905 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003906 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003907 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003908 break;
3909
Bram Moolenaar307aa162013-06-02 16:34:21 +02003910 case NFA_BOL:
3911 case NFA_BOF:
3912 /* "^" won't match past end-of-line, don't bother trying.
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003913 * Except when at the end of the line, or when we are going to the
3914 * next line for a look-behind match. */
Bram Moolenaar307aa162013-06-02 16:34:21 +02003915 if (reginput > regline
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003916 && *reginput != NUL
Bram Moolenaar307aa162013-06-02 16:34:21 +02003917 && (nfa_endp == NULL
3918 || !REG_MULTI
3919 || reglnum == nfa_endp->se_u.pos.lnum))
3920 goto skip_add;
3921 /* FALLTHROUGH */
3922
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003923 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003924 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003925 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003926 /* This state is already in the list, don't add it again,
3927 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003928 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003929 {
3930skip_add:
3931#ifdef ENABLE_LOG
3932 nfa_set_code(state->c);
3933 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3934 abs(state->id), l->id, state->c, code);
3935#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003936 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003937 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003938
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003939 /* Do not add the state again when it exists with the same
3940 * positions. */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003941 if (has_state_with_pos(l, state, subs))
3942 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003943 }
3944
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003945 /* When there are backreferences the number of states may be (a
3946 * lot) bigger than anticipated. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003947 if (nfa_has_backref && l->n == l->len)
3948 {
3949 int newlen = l->len * 3 / 2 + 50;
3950
3951 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3952 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003953 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003954
3955 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003956 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003957 thread = &l->t[l->n++];
3958 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003959 if (pim == NULL)
3960 thread->pim.result = NFA_PIM_UNUSED;
3961 else
3962 copy_pim(&thread->pim, pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003963 copy_sub(&thread->subs.norm, &subs->norm);
3964#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003965 if (nfa_has_zsubexpr)
3966 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003967#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003968#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003969 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003970 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003971#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003972 }
3973
3974#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003975 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003976 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003977#endif
3978 switch (state->c)
3979 {
3980 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003981 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003982 break;
3983
3984 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003985 /* order matters here */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003986 addstate(l, state->out, subs, pim, off);
3987 addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003988 break;
3989
Bram Moolenaar5714b802013-05-28 22:03:20 +02003990 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003991 case NFA_NOPEN:
3992 case NFA_NCLOSE:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003993 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003994 break;
3995
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003996 case NFA_MOPEN:
3997 case NFA_MOPEN1:
3998 case NFA_MOPEN2:
3999 case NFA_MOPEN3:
4000 case NFA_MOPEN4:
4001 case NFA_MOPEN5:
4002 case NFA_MOPEN6:
4003 case NFA_MOPEN7:
4004 case NFA_MOPEN8:
4005 case NFA_MOPEN9:
4006#ifdef FEAT_SYN_HL
4007 case NFA_ZOPEN:
4008 case NFA_ZOPEN1:
4009 case NFA_ZOPEN2:
4010 case NFA_ZOPEN3:
4011 case NFA_ZOPEN4:
4012 case NFA_ZOPEN5:
4013 case NFA_ZOPEN6:
4014 case NFA_ZOPEN7:
4015 case NFA_ZOPEN8:
4016 case NFA_ZOPEN9:
4017#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004018 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004019 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004020 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004021 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004022 sub = &subs->norm;
4023 }
4024#ifdef FEAT_SYN_HL
4025 else if (state->c >= NFA_ZOPEN)
4026 {
4027 subidx = state->c - NFA_ZOPEN;
4028 sub = &subs->synt;
4029 }
4030#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004031 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004032 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004033 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004034 sub = &subs->norm;
4035 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004036
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004037 /* Set the position (with "off" added) in the subexpression. Save
4038 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02004039 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004040 if (REG_MULTI)
4041 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004042 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004043 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004044 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004045 save_in_use = -1;
4046 }
4047 else
4048 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004049 save_in_use = sub->in_use;
4050 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004051 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004052 sub->list.multi[i].start.lnum = -1;
4053 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004054 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004055 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004056 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004057 if (off == -1)
4058 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004059 sub->list.multi[subidx].start.lnum = reglnum + 1;
4060 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004061 }
4062 else
4063 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004064 sub->list.multi[subidx].start.lnum = reglnum;
4065 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004066 (colnr_T)(reginput - regline + off);
4067 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004068 }
4069 else
4070 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004071 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004072 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004073 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004074 save_in_use = -1;
4075 }
4076 else
4077 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004078 save_in_use = sub->in_use;
4079 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004080 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004081 sub->list.line[i].start = NULL;
4082 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004083 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004084 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004085 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004086 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004087 }
4088
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004089 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004090
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004091 if (save_in_use == -1)
4092 {
4093 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004094 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004095 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004096 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004097 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004098 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004099 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004100 break;
4101
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004102 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004103 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004104 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004105 /* Do not overwrite the position set by \ze. If no \ze
4106 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004107 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004108 break;
4109 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004110 case NFA_MCLOSE1:
4111 case NFA_MCLOSE2:
4112 case NFA_MCLOSE3:
4113 case NFA_MCLOSE4:
4114 case NFA_MCLOSE5:
4115 case NFA_MCLOSE6:
4116 case NFA_MCLOSE7:
4117 case NFA_MCLOSE8:
4118 case NFA_MCLOSE9:
4119#ifdef FEAT_SYN_HL
4120 case NFA_ZCLOSE:
4121 case NFA_ZCLOSE1:
4122 case NFA_ZCLOSE2:
4123 case NFA_ZCLOSE3:
4124 case NFA_ZCLOSE4:
4125 case NFA_ZCLOSE5:
4126 case NFA_ZCLOSE6:
4127 case NFA_ZCLOSE7:
4128 case NFA_ZCLOSE8:
4129 case NFA_ZCLOSE9:
4130#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004132 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004133 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004134 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004135 sub = &subs->norm;
4136 }
4137#ifdef FEAT_SYN_HL
4138 else if (state->c >= NFA_ZCLOSE)
4139 {
4140 subidx = state->c - NFA_ZCLOSE;
4141 sub = &subs->synt;
4142 }
4143#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004144 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004145 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004146 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004147 sub = &subs->norm;
4148 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004149
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004150 /* We don't fill in gaps here, there must have been an MOPEN that
4151 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004152 save_in_use = sub->in_use;
4153 if (sub->in_use <= subidx)
4154 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004155 if (REG_MULTI)
4156 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004157 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004158 if (off == -1)
4159 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004160 sub->list.multi[subidx].end.lnum = reglnum + 1;
4161 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004162 }
4163 else
4164 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004165 sub->list.multi[subidx].end.lnum = reglnum;
4166 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004167 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004168 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004169 }
4170 else
4171 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004172 save_ptr = sub->list.line[subidx].end;
4173 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004174 }
4175
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004176 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004177
4178 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004179 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004180 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004181 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004182 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004183 break;
4184 }
4185}
4186
4187/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004188 * Like addstate(), but the new state(s) are put at position "*ip".
4189 * Used for zero-width matches, next state to use is the added one.
4190 * This makes sure the order of states to be tried does not change, which
4191 * matters for alternatives.
4192 */
4193 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004194addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004195 nfa_list_T *l; /* runtime state list */
4196 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004197 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004198 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004199 int *ip;
4200{
4201 int tlen = l->n;
4202 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004203 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004204
4205 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004206 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004207
Bram Moolenaar4b417062013-05-25 20:19:50 +02004208 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004209 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004210 return;
4211
4212 /* re-order to put the new state at the current position */
4213 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004214 if (count == 0)
4215 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004216 if (count == 1)
4217 {
4218 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004219 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004220 }
4221 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004222 {
4223 /* make space for new states, then move them from the
4224 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004225 mch_memmove(&(l->t[listidx + count]),
4226 &(l->t[listidx + 1]),
4227 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4228 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02004229 &(l->t[l->n - 1]),
4230 sizeof(nfa_thread_T) * count);
4231 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004232 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004233 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004234}
4235
4236/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004237 * Check character class "class" against current character c.
4238 */
4239 static int
4240check_char_class(class, c)
4241 int class;
4242 int c;
4243{
4244 switch (class)
4245 {
4246 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004247 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248 return OK;
4249 break;
4250 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004251 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004252 return OK;
4253 break;
4254 case NFA_CLASS_BLANK:
4255 if (c == ' ' || c == '\t')
4256 return OK;
4257 break;
4258 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004259 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260 return OK;
4261 break;
4262 case NFA_CLASS_DIGIT:
4263 if (VIM_ISDIGIT(c))
4264 return OK;
4265 break;
4266 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004267 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004268 return OK;
4269 break;
4270 case NFA_CLASS_LOWER:
4271 if (MB_ISLOWER(c))
4272 return OK;
4273 break;
4274 case NFA_CLASS_PRINT:
4275 if (vim_isprintc(c))
4276 return OK;
4277 break;
4278 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004279 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004280 return OK;
4281 break;
4282 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004283 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004284 return OK;
4285 break;
4286 case NFA_CLASS_UPPER:
4287 if (MB_ISUPPER(c))
4288 return OK;
4289 break;
4290 case NFA_CLASS_XDIGIT:
4291 if (vim_isxdigit(c))
4292 return OK;
4293 break;
4294 case NFA_CLASS_TAB:
4295 if (c == '\t')
4296 return OK;
4297 break;
4298 case NFA_CLASS_RETURN:
4299 if (c == '\r')
4300 return OK;
4301 break;
4302 case NFA_CLASS_BACKSPACE:
4303 if (c == '\b')
4304 return OK;
4305 break;
4306 case NFA_CLASS_ESCAPE:
4307 if (c == '\033')
4308 return OK;
4309 break;
4310
4311 default:
4312 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004313 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4314 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004315 }
4316 return FAIL;
4317}
4318
Bram Moolenaar5714b802013-05-28 22:03:20 +02004319static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
4320
4321/*
4322 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004323 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004324 */
4325 static int
4326match_backref(sub, subidx, bytelen)
4327 regsub_T *sub; /* pointers to subexpressions */
4328 int subidx;
4329 int *bytelen; /* out: length of match in bytes */
4330{
4331 int len;
4332
4333 if (sub->in_use <= subidx)
4334 {
4335retempty:
4336 /* backref was not set, match an empty string */
4337 *bytelen = 0;
4338 return TRUE;
4339 }
4340
4341 if (REG_MULTI)
4342 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004343 if (sub->list.multi[subidx].start.lnum < 0
4344 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004345 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004346 if (sub->list.multi[subidx].start.lnum == reglnum
4347 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004348 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004349 len = sub->list.multi[subidx].end.col
4350 - sub->list.multi[subidx].start.col;
4351 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4352 reginput, &len) == 0)
4353 {
4354 *bytelen = len;
4355 return TRUE;
4356 }
4357 }
4358 else
4359 {
4360 if (match_with_backref(
4361 sub->list.multi[subidx].start.lnum,
4362 sub->list.multi[subidx].start.col,
4363 sub->list.multi[subidx].end.lnum,
4364 sub->list.multi[subidx].end.col,
4365 bytelen) == RA_MATCH)
4366 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004367 }
4368 }
4369 else
4370 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004371 if (sub->list.line[subidx].start == NULL
4372 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004373 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004374 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4375 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004376 {
4377 *bytelen = len;
4378 return TRUE;
4379 }
4380 }
4381 return FALSE;
4382}
4383
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004384#ifdef FEAT_SYN_HL
4385
4386static int match_zref __ARGS((int subidx, int *bytelen));
4387
4388/*
4389 * Check for a match with \z subexpression "subidx".
4390 * Return TRUE if it matches.
4391 */
4392 static int
4393match_zref(subidx, bytelen)
4394 int subidx;
4395 int *bytelen; /* out: length of match in bytes */
4396{
4397 int len;
4398
4399 cleanup_zsubexpr();
4400 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4401 {
4402 /* backref was not set, match an empty string */
4403 *bytelen = 0;
4404 return TRUE;
4405 }
4406
4407 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4408 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4409 {
4410 *bytelen = len;
4411 return TRUE;
4412 }
4413 return FALSE;
4414}
4415#endif
4416
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004417/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004418 * Save list IDs for all NFA states of "prog" into "list".
4419 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004420 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004421 */
4422 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004423nfa_save_listids(prog, list)
4424 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004425 int *list;
4426{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004427 int i;
4428 nfa_state_T *p;
4429
4430 /* Order in the list is reverse, it's a bit faster that way. */
4431 p = &prog->state[0];
4432 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004433 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004434 list[i] = p->lastlist[1];
4435 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004436 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004437 }
4438}
4439
4440/*
4441 * Restore list IDs from "list" to all NFA states.
4442 */
4443 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004444nfa_restore_listids(prog, list)
4445 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004446 int *list;
4447{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004448 int i;
4449 nfa_state_T *p;
4450
4451 p = &prog->state[0];
4452 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004453 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004454 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004455 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004456 }
4457}
4458
Bram Moolenaar423532e2013-05-29 21:14:42 +02004459 static int
4460nfa_re_num_cmp(val, op, pos)
4461 long_u val;
4462 int op;
4463 long_u pos;
4464{
4465 if (op == 1) return pos > val;
4466 if (op == 2) return pos < val;
4467 return val == pos;
4468}
4469
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004470static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004471static 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 +02004472
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004473/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004474 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004475 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4476 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004477 */
4478 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004479recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004480 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004481 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004482 nfa_regprog_T *prog;
4483 regsubs_T *submatch;
4484 regsubs_T *m;
4485 int **listids;
4486{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004487 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004488 int save_reglnum = reglnum;
4489 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004490 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004491 save_se_T *save_nfa_endp = nfa_endp;
4492 save_se_T endpos;
4493 save_se_T *endposp = NULL;
4494 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004495 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004496
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004497 if (pim != NULL)
4498 {
4499 /* start at the position where the postponed match was */
4500 if (REG_MULTI)
4501 reginput = regline + pim->end.pos.col;
4502 else
4503 reginput = pim->end.ptr;
4504 }
4505
Bram Moolenaardecd9542013-06-07 16:31:50 +02004506 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004507 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4508 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4509 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004510 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004511 /* The recursive match must end at the current position. When "pim" is
4512 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004513 endposp = &endpos;
4514 if (REG_MULTI)
4515 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004516 if (pim == NULL)
4517 {
4518 endpos.se_u.pos.col = (int)(reginput - regline);
4519 endpos.se_u.pos.lnum = reglnum;
4520 }
4521 else
4522 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004523 }
4524 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004525 {
4526 if (pim == NULL)
4527 endpos.se_u.ptr = reginput;
4528 else
4529 endpos.se_u.ptr = pim->end.ptr;
4530 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004531
4532 /* Go back the specified number of bytes, or as far as the
4533 * start of the previous line, to try matching "\@<=" or
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004534 * not matching "\@<!". This is very ineffecient, limit the number of
4535 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004536 if (state->val <= 0)
4537 {
4538 if (REG_MULTI)
4539 {
4540 regline = reg_getline(--reglnum);
4541 if (regline == NULL)
4542 /* can't go before the first line */
4543 regline = reg_getline(++reglnum);
4544 }
4545 reginput = regline;
4546 }
4547 else
4548 {
4549 if (REG_MULTI && (int)(reginput - regline) < state->val)
4550 {
4551 /* Not enough bytes in this line, go to end of
4552 * previous line. */
4553 regline = reg_getline(--reglnum);
4554 if (regline == NULL)
4555 {
4556 /* can't go before the first line */
4557 regline = reg_getline(++reglnum);
4558 reginput = regline;
4559 }
4560 else
4561 reginput = regline + STRLEN(regline);
4562 }
4563 if ((int)(reginput - regline) >= state->val)
4564 {
4565 reginput -= state->val;
4566#ifdef FEAT_MBYTE
4567 if (has_mbyte)
4568 reginput -= mb_head_off(regline, reginput);
4569#endif
4570 }
4571 else
4572 reginput = regline;
4573 }
4574 }
4575
Bram Moolenaarf46da702013-06-02 22:37:42 +02004576#ifdef ENABLE_LOG
4577 if (log_fd != stderr)
4578 fclose(log_fd);
4579 log_fd = NULL;
4580#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004581 /* Have to clear the lastlist field of the NFA nodes, so that
4582 * nfa_regmatch() and addstate() can run properly after recursion. */
4583 if (nfa_ll_index == 1)
4584 {
4585 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4586 * values and clear them. */
4587 if (*listids == NULL)
4588 {
4589 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4590 if (*listids == NULL)
4591 {
4592 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4593 return 0;
4594 }
4595 }
4596 nfa_save_listids(prog, *listids);
4597 need_restore = TRUE;
4598 /* any value of nfa_listid will do */
4599 }
4600 else
4601 {
4602 /* First recursive nfa_regmatch() call, switch to the second lastlist
4603 * entry. Make sure nfa_listid is different from a previous recursive
4604 * call, because some states may still have this ID. */
4605 ++nfa_ll_index;
4606 if (nfa_listid <= nfa_alt_listid)
4607 nfa_listid = nfa_alt_listid;
4608 }
4609
4610 /* Call nfa_regmatch() to check if the current concat matches at this
4611 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004612 nfa_endp = endposp;
4613 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004614
4615 if (need_restore)
4616 nfa_restore_listids(prog, *listids);
4617 else
4618 {
4619 --nfa_ll_index;
4620 nfa_alt_listid = nfa_listid;
4621 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004622
4623 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004624 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02004625 if (REG_MULTI)
4626 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004627 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004628 nfa_match = save_nfa_match;
4629 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004630 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004631
4632#ifdef ENABLE_LOG
4633 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4634 if (log_fd != NULL)
4635 {
4636 fprintf(log_fd, "****************************\n");
4637 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4638 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4639 fprintf(log_fd, "****************************\n");
4640 }
4641 else
4642 {
4643 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4644 log_fd = stderr;
4645 }
4646#endif
4647
4648 return result;
4649}
4650
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004651static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004652static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004653
4654/*
4655 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004656 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004657 * NFA_ANY: 1
4658 * specific character: 99
4659 */
4660 static int
4661failure_chance(state, depth)
4662 nfa_state_T *state;
4663 int depth;
4664{
4665 int c = state->c;
4666 int l, r;
4667
4668 /* detect looping */
4669 if (depth > 4)
4670 return 1;
4671
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004672 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004673 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004674 case NFA_SPLIT:
4675 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4676 /* avoid recursive stuff */
4677 return 1;
4678 /* two alternatives, use the lowest failure chance */
4679 l = failure_chance(state->out, depth + 1);
4680 r = failure_chance(state->out1, depth + 1);
4681 return l < r ? l : r;
4682
4683 case NFA_ANY:
4684 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004685 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004686
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004687 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004688 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004689 /* empty match works always */
4690 return 0;
4691
Bram Moolenaar44c71db2013-06-14 22:33:51 +02004692 case NFA_START_INVISIBLE:
4693 case NFA_START_INVISIBLE_FIRST:
4694 case NFA_START_INVISIBLE_NEG:
4695 case NFA_START_INVISIBLE_NEG_FIRST:
4696 case NFA_START_INVISIBLE_BEFORE:
4697 case NFA_START_INVISIBLE_BEFORE_FIRST:
4698 case NFA_START_INVISIBLE_BEFORE_NEG:
4699 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
4700 case NFA_START_PATTERN:
4701 /* recursive regmatch is expensive, use low failure chance */
4702 return 5;
4703
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004704 case NFA_BOL:
4705 case NFA_EOL:
4706 case NFA_BOF:
4707 case NFA_EOF:
4708 case NFA_NEWL:
4709 return 99;
4710
4711 case NFA_BOW:
4712 case NFA_EOW:
4713 return 90;
4714
4715 case NFA_MOPEN:
4716 case NFA_MOPEN1:
4717 case NFA_MOPEN2:
4718 case NFA_MOPEN3:
4719 case NFA_MOPEN4:
4720 case NFA_MOPEN5:
4721 case NFA_MOPEN6:
4722 case NFA_MOPEN7:
4723 case NFA_MOPEN8:
4724 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004725#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004726 case NFA_ZOPEN:
4727 case NFA_ZOPEN1:
4728 case NFA_ZOPEN2:
4729 case NFA_ZOPEN3:
4730 case NFA_ZOPEN4:
4731 case NFA_ZOPEN5:
4732 case NFA_ZOPEN6:
4733 case NFA_ZOPEN7:
4734 case NFA_ZOPEN8:
4735 case NFA_ZOPEN9:
4736 case NFA_ZCLOSE:
4737 case NFA_ZCLOSE1:
4738 case NFA_ZCLOSE2:
4739 case NFA_ZCLOSE3:
4740 case NFA_ZCLOSE4:
4741 case NFA_ZCLOSE5:
4742 case NFA_ZCLOSE6:
4743 case NFA_ZCLOSE7:
4744 case NFA_ZCLOSE8:
4745 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004746#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004747 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004748 case NFA_MCLOSE1:
4749 case NFA_MCLOSE2:
4750 case NFA_MCLOSE3:
4751 case NFA_MCLOSE4:
4752 case NFA_MCLOSE5:
4753 case NFA_MCLOSE6:
4754 case NFA_MCLOSE7:
4755 case NFA_MCLOSE8:
4756 case NFA_MCLOSE9:
4757 case NFA_NCLOSE:
4758 return failure_chance(state->out, depth + 1);
4759
4760 case NFA_BACKREF1:
4761 case NFA_BACKREF2:
4762 case NFA_BACKREF3:
4763 case NFA_BACKREF4:
4764 case NFA_BACKREF5:
4765 case NFA_BACKREF6:
4766 case NFA_BACKREF7:
4767 case NFA_BACKREF8:
4768 case NFA_BACKREF9:
4769#ifdef FEAT_SYN_HL
4770 case NFA_ZREF1:
4771 case NFA_ZREF2:
4772 case NFA_ZREF3:
4773 case NFA_ZREF4:
4774 case NFA_ZREF5:
4775 case NFA_ZREF6:
4776 case NFA_ZREF7:
4777 case NFA_ZREF8:
4778 case NFA_ZREF9:
4779#endif
4780 /* backreferences don't match in many places */
4781 return 94;
4782
4783 case NFA_LNUM_GT:
4784 case NFA_LNUM_LT:
4785 case NFA_COL_GT:
4786 case NFA_COL_LT:
4787 case NFA_VCOL_GT:
4788 case NFA_VCOL_LT:
4789 case NFA_MARK_GT:
4790 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004791 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004792 /* before/after positions don't match very often */
4793 return 85;
4794
4795 case NFA_LNUM:
4796 return 90;
4797
4798 case NFA_CURSOR:
4799 case NFA_COL:
4800 case NFA_VCOL:
4801 case NFA_MARK:
4802 /* specific positions rarely match */
4803 return 98;
4804
4805 case NFA_COMPOSING:
4806 return 95;
4807
4808 default:
4809 if (c > 0)
4810 /* character match fails often */
4811 return 95;
4812 }
4813
4814 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004815 return 50;
4816}
4817
Bram Moolenaarf46da702013-06-02 22:37:42 +02004818/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004819 * Skip until the char "c" we know a match must start with.
4820 */
4821 static int
4822skip_to_start(c, colp)
4823 int c;
4824 colnr_T *colp;
4825{
4826 char_u *s;
4827
4828 /* Used often, do some work to avoid call overhead. */
4829 if (!ireg_ic
4830#ifdef FEAT_MBYTE
4831 && !has_mbyte
4832#endif
4833 )
4834 s = vim_strbyte(regline + *colp, c);
4835 else
4836 s = cstrchr(regline + *colp, c);
4837 if (s == NULL)
4838 return FAIL;
4839 *colp = (int)(s - regline);
4840 return OK;
4841}
4842
4843/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004844 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004845 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004846 * Returns zero for no match, 1 for a match.
4847 */
4848 static long
4849find_match_text(startcol, regstart, match_text)
4850 colnr_T startcol;
4851 int regstart;
4852 char_u *match_text;
4853{
4854 colnr_T col = startcol;
4855 int c1, c2;
4856 int len1, len2;
4857 int match;
4858
4859 for (;;)
4860 {
4861 match = TRUE;
4862 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4863 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4864 {
4865 c1 = PTR2CHAR(match_text + len1);
4866 c2 = PTR2CHAR(regline + col + len2);
4867 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4868 {
4869 match = FALSE;
4870 break;
4871 }
4872 len2 += MB_CHAR2LEN(c2);
4873 }
4874 if (match
4875#ifdef FEAT_MBYTE
4876 /* check that no composing char follows */
4877 && !(enc_utf8
4878 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4879#endif
4880 )
4881 {
4882 cleanup_subexpr();
4883 if (REG_MULTI)
4884 {
4885 reg_startpos[0].lnum = reglnum;
4886 reg_startpos[0].col = col;
4887 reg_endpos[0].lnum = reglnum;
4888 reg_endpos[0].col = col + len2;
4889 }
4890 else
4891 {
4892 reg_startp[0] = regline + col;
4893 reg_endp[0] = regline + col + len2;
4894 }
4895 return 1L;
4896 }
4897
4898 /* Try finding regstart after the current match. */
4899 col += MB_CHAR2LEN(regstart); /* skip regstart */
4900 if (skip_to_start(regstart, &col) == FAIL)
4901 break;
4902 }
4903 return 0L;
4904}
4905
4906/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004907 * Main matching routine.
4908 *
4909 * Run NFA to determine whether it matches reginput.
4910 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004911 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004912 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004913 * Return TRUE if there is a match, FALSE otherwise.
4914 * Note: Caller must ensure that: start != NULL.
4915 */
4916 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004917nfa_regmatch(prog, start, submatch, m)
4918 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004919 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004920 regsubs_T *submatch;
4921 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004923 int result;
4924 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004926 int go_to_nextline = FALSE;
4927 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004928 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004929 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004930 nfa_list_T *thislist;
4931 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004932 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004933 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004934 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004935 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02004936 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004937 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004938#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004939 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940
4941 if (debug == NULL)
4942 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004943 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004944 return FALSE;
4945 }
4946#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004947 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004948
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004949 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004950 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004951 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004952 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004953 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004954 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004955 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004956 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004957
4958#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004959 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004960 if (log_fd != NULL)
4961 {
4962 fprintf(log_fd, "**********************************\n");
4963 nfa_set_code(start->c);
4964 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4965 abs(start->id), code);
4966 fprintf(log_fd, "**********************************\n");
4967 }
4968 else
4969 {
4970 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4971 log_fd = stderr;
4972 }
4973#endif
4974
4975 thislist = &list[0];
4976 thislist->n = 0;
4977 nextlist = &list[1];
4978 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004979#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004980 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004981#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004982 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004983
4984 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4985 * it's the first MOPEN. */
4986 if (toplevel)
4987 {
4988 if (REG_MULTI)
4989 {
4990 m->norm.list.multi[0].start.lnum = reglnum;
4991 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4992 }
4993 else
4994 m->norm.list.line[0].start = reginput;
4995 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004996 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004997 }
4998 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004999 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005000
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005001#define ADD_STATE_IF_MATCH(state) \
5002 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005003 add_state = state->out; \
5004 add_off = clen; \
5005 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005006
5007 /*
5008 * Run for each character.
5009 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005010 for (;;)
5011 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005012 int curc;
5013 int clen;
5014
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005015#ifdef FEAT_MBYTE
5016 if (has_mbyte)
5017 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005018 curc = (*mb_ptr2char)(reginput);
5019 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005020 }
5021 else
5022#endif
5023 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005024 curc = *reginput;
5025 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005026 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005027 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005028 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005029 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005030 go_to_nextline = FALSE;
5031 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005032
5033 /* swap lists */
5034 thislist = &list[flag];
5035 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005036 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005037 ++nfa_listid;
5038 thislist->id = nfa_listid;
5039 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005040
5041#ifdef ENABLE_LOG
5042 fprintf(log_fd, "------------------------------------------\n");
5043 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005044 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005045 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005046 {
5047 int i;
5048
5049 for (i = 0; i < thislist->n; i++)
5050 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5051 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005052 fprintf(log_fd, "\n");
5053#endif
5054
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005055#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005056 fprintf(debug, "\n-------------------\n");
5057#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005058 /*
5059 * If the state lists are empty we can stop.
5060 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005061 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005062 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005063
5064 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005065 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005066 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005067 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005069#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005070 nfa_set_code(t->state->c);
5071 fprintf(debug, "%s, ", code);
5072#endif
5073#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005074 {
5075 int col;
5076
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005077 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005078 col = -1;
5079 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005080 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005081 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005082 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005083 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005084 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5085 abs(t->state->id), (int)t->state->c, code, col,
5086 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005087 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005088#endif
5089
5090 /*
5091 * Handle the possible codes of the current state.
5092 * The most important is NFA_MATCH.
5093 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005094 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005095 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005096 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005097 switch (t->state->c)
5098 {
5099 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005100 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005101 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005102 copy_sub(&submatch->norm, &t->subs.norm);
5103#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005104 if (nfa_has_zsubexpr)
5105 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005106#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005107#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005108 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005109#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005110 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005111 * states at this position. When the list of states is going
5112 * to be empty quit without advancing, so that "reginput" is
5113 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005114 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005115 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005116 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005117 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005118
5119 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005120 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005121 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005122 /*
5123 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005124 * NFA_START_INVISIBLE_BEFORE node.
5125 * They surround a zero-width group, used with "\@=", "\&",
5126 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005127 * If we got here, it means that the current "invisible" group
5128 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005129 * nfa_regmatch(). For a look-behind match only when it ends
5130 * in the position in "nfa_endp".
5131 * Submatches are stored in *m, and used in the parent call.
5132 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005133#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005134 if (nfa_endp != NULL)
5135 {
5136 if (REG_MULTI)
5137 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5138 (int)reglnum,
5139 (int)nfa_endp->se_u.pos.lnum,
5140 (int)(reginput - regline),
5141 nfa_endp->se_u.pos.col);
5142 else
5143 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5144 (int)(reginput - regline),
5145 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005146 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005147#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005148 /* If "nfa_endp" is set it's only a match if it ends at
5149 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005150 if (nfa_endp != NULL && (REG_MULTI
5151 ? (reglnum != nfa_endp->se_u.pos.lnum
5152 || (int)(reginput - regline)
5153 != nfa_endp->se_u.pos.col)
5154 : reginput != nfa_endp->se_u.ptr))
5155 break;
5156
5157 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005158 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005159 {
5160 copy_sub(&m->norm, &t->subs.norm);
5161#ifdef FEAT_SYN_HL
5162 if (nfa_has_zsubexpr)
5163 copy_sub(&m->synt, &t->subs.synt);
5164#endif
5165 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005166#ifdef ENABLE_LOG
5167 fprintf(log_fd, "Match found:\n");
5168 log_subsexpr(m);
5169#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005170 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005171 break;
5172
5173 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005174 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005175 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005176 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005177 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005178 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005179 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005180 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005181 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005182#ifdef ENABLE_LOG
5183 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5184 failure_chance(t->state->out, 0),
5185 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005186#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005187 /* Do it directly if there already is a PIM or when
5188 * nfa_postprocess() detected it will work better. */
5189 if (t->pim.result != NFA_PIM_UNUSED
5190 || t->state->c == NFA_START_INVISIBLE_FIRST
5191 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5192 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5193 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005194 {
5195 /*
5196 * First try matching the invisible match, then what
5197 * follows.
5198 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005199 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005200 submatch, m, &listids);
5201
Bram Moolenaardecd9542013-06-07 16:31:50 +02005202 /* for \@! and \@<! it is a match when the result is
5203 * FALSE */
5204 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005205 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5206 || t->state->c
5207 == NFA_START_INVISIBLE_BEFORE_NEG
5208 || t->state->c
5209 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005210 {
5211 /* Copy submatch info from the recursive call */
5212 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005213#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005214 if (nfa_has_zsubexpr)
5215 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005216#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005217
Bram Moolenaara2d95102013-06-04 14:23:05 +02005218 /* t->state->out1 is the corresponding
5219 * END_INVISIBLE node; Add its out to the current
5220 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005221 add_here = TRUE;
5222 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005223 }
5224 }
5225 else
5226 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005227 nfa_pim_T pim;
5228
Bram Moolenaara2d95102013-06-04 14:23:05 +02005229 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005230 * First try matching what follows. Only if a match
5231 * is found verify the invisible match matches. Add a
5232 * nfa_pim_T to the following states, it contains info
5233 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005234 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005235 pim.state = t->state;
5236 pim.result = NFA_PIM_TODO;
5237 pim.subs.norm.in_use = 0;
5238#ifdef FEAT_SYN_HL
5239 pim.subs.synt.in_use = 0;
5240#endif
5241 if (REG_MULTI)
5242 {
5243 pim.end.pos.col = (int)(reginput - regline);
5244 pim.end.pos.lnum = reglnum;
5245 }
5246 else
5247 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005248
5249 /* t->state->out1 is the corresponding END_INVISIBLE
5250 * node; Add its out to the current list (zero-width
5251 * match). */
5252 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005253 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005254 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005255 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005256 break;
5257
Bram Moolenaar87953742013-06-05 18:52:40 +02005258 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005259 {
5260 nfa_state_T *skip = NULL;
5261#ifdef ENABLE_LOG
5262 int skip_lid = 0;
5263#endif
5264
5265 /* There is no point in trying to match the pattern if the
5266 * output state is not going to be added to the list. */
5267 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5268 {
5269 skip = t->state->out1->out;
5270#ifdef ENABLE_LOG
5271 skip_lid = nextlist->id;
5272#endif
5273 }
5274 else if (state_in_list(nextlist,
5275 t->state->out1->out->out, &t->subs))
5276 {
5277 skip = t->state->out1->out->out;
5278#ifdef ENABLE_LOG
5279 skip_lid = nextlist->id;
5280#endif
5281 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005282 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005283 t->state->out1->out->out, &t->subs))
5284 {
5285 skip = t->state->out1->out->out;
5286#ifdef ENABLE_LOG
5287 skip_lid = thislist->id;
5288#endif
5289 }
5290 if (skip != NULL)
5291 {
5292#ifdef ENABLE_LOG
5293 nfa_set_code(skip->c);
5294 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5295 abs(skip->id), skip_lid, skip->c, code);
5296#endif
5297 break;
5298 }
5299
Bram Moolenaar87953742013-06-05 18:52:40 +02005300 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005301 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005302 submatch, m, &listids);
5303 if (result)
5304 {
5305 int bytelen;
5306
5307#ifdef ENABLE_LOG
5308 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5309 log_subsexpr(m);
5310#endif
5311 /* Copy submatch info from the recursive call */
5312 copy_sub_off(&t->subs.norm, &m->norm);
5313#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005314 if (nfa_has_zsubexpr)
5315 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005316#endif
5317 /* Now we need to skip over the matched text and then
5318 * continue with what follows. */
5319 if (REG_MULTI)
5320 /* TODO: multi-line match */
5321 bytelen = m->norm.list.multi[0].end.col
5322 - (int)(reginput - regline);
5323 else
5324 bytelen = (int)(m->norm.list.line[0].end - reginput);
5325
5326#ifdef ENABLE_LOG
5327 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5328#endif
5329 if (bytelen == 0)
5330 {
5331 /* empty match, output of corresponding
5332 * NFA_END_PATTERN/NFA_SKIP to be used at current
5333 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005334 add_here = TRUE;
5335 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005336 }
5337 else if (bytelen <= clen)
5338 {
5339 /* match current character, output of corresponding
5340 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005341 add_state = t->state->out1->out->out;
5342 add_off = clen;
5343 }
5344 else
5345 {
5346 /* skip over the matched characters, set character
5347 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005348 add_state = t->state->out1->out;
5349 add_off = bytelen;
5350 add_count = bytelen - clen;
5351 }
5352 }
5353 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005354 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005355
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005356 case NFA_BOL:
5357 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005358 {
5359 add_here = TRUE;
5360 add_state = t->state->out;
5361 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005362 break;
5363
5364 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005365 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005366 {
5367 add_here = TRUE;
5368 add_state = t->state->out;
5369 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005370 break;
5371
5372 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005373 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005374
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005375 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005376 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005377#ifdef FEAT_MBYTE
5378 else if (has_mbyte)
5379 {
5380 int this_class;
5381
5382 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005383 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005384 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005385 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005386 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005387 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005388 }
5389#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005390 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005391 || (reginput > regline
5392 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005393 result = FALSE;
5394 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005395 {
5396 add_here = TRUE;
5397 add_state = t->state->out;
5398 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005399 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005400
5401 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005402 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005403 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005404 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005405#ifdef FEAT_MBYTE
5406 else if (has_mbyte)
5407 {
5408 int this_class, prev_class;
5409
5410 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005411 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005412 prev_class = reg_prev_class();
5413 if (this_class == prev_class
5414 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005415 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005416 }
5417#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005418 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005419 || (reginput[0] != NUL
5420 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005421 result = FALSE;
5422 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005423 {
5424 add_here = TRUE;
5425 add_state = t->state->out;
5426 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005427 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005428
Bram Moolenaar4b780632013-05-31 22:14:52 +02005429 case NFA_BOF:
5430 if (reglnum == 0 && reginput == regline
5431 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005432 {
5433 add_here = TRUE;
5434 add_state = t->state->out;
5435 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005436 break;
5437
5438 case NFA_EOF:
5439 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005440 {
5441 add_here = TRUE;
5442 add_state = t->state->out;
5443 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005444 break;
5445
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005446#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005447 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005448 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005449 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005450 int len = 0;
5451 nfa_state_T *end;
5452 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005453 int cchars[MAX_MCO];
5454 int ccount = 0;
5455 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005456
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005457 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005458 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005459 if (utf_iscomposing(sta->c))
5460 {
5461 /* Only match composing character(s), ignore base
5462 * character. Used for ".{composing}" and "{composing}"
5463 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005464 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005465 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005466 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005467 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005468 /* If \Z was present, then ignore composing characters.
5469 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005470 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005471 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005472 else
5473 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005474 while (sta->c != NFA_END_COMPOSING)
5475 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005477
5478 /* Check base character matches first, unless ignored. */
5479 else if (len > 0 || mc == sta->c)
5480 {
5481 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005482 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005483 len += mb_char2len(mc);
5484 sta = sta->out;
5485 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005486
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005487 /* We don't care about the order of composing characters.
5488 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005489 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005490 {
5491 mc = mb_ptr2char(reginput + len);
5492 cchars[ccount++] = mc;
5493 len += mb_char2len(mc);
5494 if (ccount == MAX_MCO)
5495 break;
5496 }
5497
5498 /* Check that each composing char in the pattern matches a
5499 * composing char in the text. We do not check if all
5500 * composing chars are matched. */
5501 result = OK;
5502 while (sta->c != NFA_END_COMPOSING)
5503 {
5504 for (j = 0; j < ccount; ++j)
5505 if (cchars[j] == sta->c)
5506 break;
5507 if (j == ccount)
5508 {
5509 result = FAIL;
5510 break;
5511 }
5512 sta = sta->out;
5513 }
5514 }
5515 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005516 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005517
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005518 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005519 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005520 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005521 }
5522#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005523
5524 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005525 if (curc == NUL && !reg_line_lbr && REG_MULTI
5526 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005527 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005528 go_to_nextline = TRUE;
5529 /* Pass -1 for the offset, which means taking the position
5530 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005531 add_state = t->state->out;
5532 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005534 else if (curc == '\n' && reg_line_lbr)
5535 {
5536 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005537 add_state = t->state->out;
5538 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005539 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005540 break;
5541
Bram Moolenaar417bad22013-06-07 14:08:30 +02005542 case NFA_START_COLL:
5543 case NFA_START_NEG_COLL:
5544 {
5545 /* What follows is a list of characters, until NFA_END_COLL.
5546 * One of them must match or none of them must match. */
5547 nfa_state_T *state;
5548 int result_if_matched;
5549 int c1, c2;
5550
5551 /* Never match EOL. If it's part of the collection it is added
5552 * as a separate state with an OR. */
5553 if (curc == NUL)
5554 break;
5555
5556 state = t->state->out;
5557 result_if_matched = (t->state->c == NFA_START_COLL);
5558 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005559 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005560 if (state->c == NFA_END_COLL)
5561 {
5562 result = !result_if_matched;
5563 break;
5564 }
5565 if (state->c == NFA_RANGE_MIN)
5566 {
5567 c1 = state->val;
5568 state = state->out; /* advance to NFA_RANGE_MAX */
5569 c2 = state->val;
5570#ifdef ENABLE_LOG
5571 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5572 curc, c1, c2);
5573#endif
5574 if (curc >= c1 && curc <= c2)
5575 {
5576 result = result_if_matched;
5577 break;
5578 }
5579 if (ireg_ic)
5580 {
5581 int curc_low = MB_TOLOWER(curc);
5582 int done = FALSE;
5583
5584 for ( ; c1 <= c2; ++c1)
5585 if (MB_TOLOWER(c1) == curc_low)
5586 {
5587 result = result_if_matched;
5588 done = TRUE;
5589 break;
5590 }
5591 if (done)
5592 break;
5593 }
5594 }
5595 else if (state->c < 0 ? check_char_class(state->c, curc)
5596 : (curc == state->c
5597 || (ireg_ic && MB_TOLOWER(curc)
5598 == MB_TOLOWER(state->c))))
5599 {
5600 result = result_if_matched;
5601 break;
5602 }
5603 state = state->out;
5604 }
5605 if (result)
5606 {
5607 /* next state is in out of the NFA_END_COLL, out1 of
5608 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005609 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005610 add_off = clen;
5611 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005612 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005613 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614
5615 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005616 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005617 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005618 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005619 add_state = t->state->out;
5620 add_off = clen;
5621 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005622 break;
5623
5624 /*
5625 * Character classes like \a for alpha, \d for digit etc.
5626 */
5627 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005628 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005629 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005630 break;
5631
5632 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005633 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005634 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005635 break;
5636
5637 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005638 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005639 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005640 break;
5641
5642 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005643 result = !VIM_ISDIGIT(curc)
5644 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005645 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005646 break;
5647
5648 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005649 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005650 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005651 break;
5652
5653 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005654 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005655 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005656 break;
5657
5658 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005659 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005660 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005661 break;
5662
5663 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005664 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005665 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005666 break;
5667
5668 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005669 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005670 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005671 break;
5672
5673 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005674 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005675 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005676 break;
5677
5678 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005679 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005680 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005681 break;
5682
5683 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005684 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005685 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005686 break;
5687
5688 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005689 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005690 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005691 break;
5692
5693 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005694 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005695 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696 break;
5697
5698 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005699 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005700 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005701 break;
5702
5703 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005704 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005705 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005706 break;
5707
5708 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005709 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005710 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005711 break;
5712
5713 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005714 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005715 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005716 break;
5717
5718 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005719 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005720 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005721 break;
5722
5723 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005724 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005725 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005726 break;
5727
5728 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005729 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005730 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005731 break;
5732
5733 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005734 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005735 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005736 break;
5737
5738 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005739 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005740 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005741 break;
5742
5743 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005744 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005745 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005746 break;
5747
5748 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005749 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005750 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005751 break;
5752
5753 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005754 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005755 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005756 break;
5757
Bram Moolenaar5714b802013-05-28 22:03:20 +02005758 case NFA_BACKREF1:
5759 case NFA_BACKREF2:
5760 case NFA_BACKREF3:
5761 case NFA_BACKREF4:
5762 case NFA_BACKREF5:
5763 case NFA_BACKREF6:
5764 case NFA_BACKREF7:
5765 case NFA_BACKREF8:
5766 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005767#ifdef FEAT_SYN_HL
5768 case NFA_ZREF1:
5769 case NFA_ZREF2:
5770 case NFA_ZREF3:
5771 case NFA_ZREF4:
5772 case NFA_ZREF5:
5773 case NFA_ZREF6:
5774 case NFA_ZREF7:
5775 case NFA_ZREF8:
5776 case NFA_ZREF9:
5777#endif
5778 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005779 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005780 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005781 int bytelen;
5782
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005783 if (t->state->c <= NFA_BACKREF9)
5784 {
5785 subidx = t->state->c - NFA_BACKREF1 + 1;
5786 result = match_backref(&t->subs.norm, subidx, &bytelen);
5787 }
5788#ifdef FEAT_SYN_HL
5789 else
5790 {
5791 subidx = t->state->c - NFA_ZREF1 + 1;
5792 result = match_zref(subidx, &bytelen);
5793 }
5794#endif
5795
Bram Moolenaar5714b802013-05-28 22:03:20 +02005796 if (result)
5797 {
5798 if (bytelen == 0)
5799 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005800 /* empty match always works, output of NFA_SKIP to be
5801 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005802 add_here = TRUE;
5803 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005804 }
5805 else if (bytelen <= clen)
5806 {
5807 /* match current character, jump ahead to out of
5808 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005809 add_state = t->state->out->out;
5810 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005811 }
5812 else
5813 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005814 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005815 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005816 add_state = t->state->out;
5817 add_off = bytelen;
5818 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005819 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005820 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005821 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005822 }
5823 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005824 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005825 if (t->count - clen <= 0)
5826 {
5827 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005828 add_state = t->state->out;
5829 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005830 }
5831 else
5832 {
5833 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005834 add_state = t->state;
5835 add_off = 0;
5836 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005837 }
5838 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005839
Bram Moolenaar423532e2013-05-29 21:14:42 +02005840 case NFA_LNUM:
5841 case NFA_LNUM_GT:
5842 case NFA_LNUM_LT:
5843 result = (REG_MULTI &&
5844 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5845 (long_u)(reglnum + reg_firstlnum)));
5846 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005847 {
5848 add_here = TRUE;
5849 add_state = t->state->out;
5850 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005851 break;
5852
5853 case NFA_COL:
5854 case NFA_COL_GT:
5855 case NFA_COL_LT:
5856 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5857 (long_u)(reginput - regline) + 1);
5858 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005859 {
5860 add_here = TRUE;
5861 add_state = t->state->out;
5862 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005863 break;
5864
5865 case NFA_VCOL:
5866 case NFA_VCOL_GT:
5867 case NFA_VCOL_LT:
5868 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5869 (long_u)win_linetabsize(
5870 reg_win == NULL ? curwin : reg_win,
5871 regline, (colnr_T)(reginput - regline)) + 1);
5872 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005873 {
5874 add_here = TRUE;
5875 add_state = t->state->out;
5876 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005877 break;
5878
Bram Moolenaar044aa292013-06-04 21:27:38 +02005879 case NFA_MARK:
5880 case NFA_MARK_GT:
5881 case NFA_MARK_LT:
5882 {
5883 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5884
5885 /* Compare the mark position to the match position. */
5886 result = (pos != NULL /* mark doesn't exist */
5887 && pos->lnum > 0 /* mark isn't set in reg_buf */
5888 && (pos->lnum == reglnum + reg_firstlnum
5889 ? (pos->col == (colnr_T)(reginput - regline)
5890 ? t->state->c == NFA_MARK
5891 : (pos->col < (colnr_T)(reginput - regline)
5892 ? t->state->c == NFA_MARK_GT
5893 : t->state->c == NFA_MARK_LT))
5894 : (pos->lnum < reglnum + reg_firstlnum
5895 ? t->state->c == NFA_MARK_GT
5896 : t->state->c == NFA_MARK_LT)));
5897 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005898 {
5899 add_here = TRUE;
5900 add_state = t->state->out;
5901 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005902 break;
5903 }
5904
Bram Moolenaar423532e2013-05-29 21:14:42 +02005905 case NFA_CURSOR:
5906 result = (reg_win != NULL
5907 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5908 && ((colnr_T)(reginput - regline)
5909 == reg_win->w_cursor.col));
5910 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005911 {
5912 add_here = TRUE;
5913 add_state = t->state->out;
5914 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005915 break;
5916
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005917 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005918#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005919 result = reg_match_visual();
5920 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005921 {
5922 add_here = TRUE;
5923 add_state = t->state->out;
5924 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005925#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005926 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005927
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005928 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005929 {
5930 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005931
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005932 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005933 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005934 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005935 result = (c == curc);
5936
5937 if (!result && ireg_ic)
5938 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005939#ifdef FEAT_MBYTE
5940 /* If there is a composing character which is not being
5941 * ignored there can be no match. Match with composing
5942 * character uses NFA_COMPOSING above. */
5943 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005944 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005945 result = FALSE;
5946#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005947 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005948 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005949 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005950
5951 } /* switch (t->state->c) */
5952
5953 if (add_state != NULL)
5954 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005955 nfa_pim_T *pim;
5956
5957 if (t->pim.result == NFA_PIM_UNUSED)
5958 pim = NULL;
5959 else
5960 pim = &t->pim;
5961
5962 /* Handle the postponed invisible match if the match might end
5963 * without advancing and before the end of the line. */
5964 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005965 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005966 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005967 {
5968#ifdef ENABLE_LOG
5969 fprintf(log_fd, "\n");
5970 fprintf(log_fd, "==================================\n");
5971 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5972 fprintf(log_fd, "\n");
5973#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005974 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005975 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005976 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005977 /* for \@! and \@<! it is a match when the result is
5978 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005979 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005980 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
5981 || pim->state->c
5982 == NFA_START_INVISIBLE_BEFORE_NEG
5983 || pim->state->c
5984 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005985 {
5986 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005987 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005988#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005989 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005990 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005991#endif
5992 }
5993 }
5994 else
5995 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005996 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005997#ifdef ENABLE_LOG
5998 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005999 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006000 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6001 fprintf(log_fd, "\n");
6002#endif
6003 }
6004
Bram Moolenaardecd9542013-06-07 16:31:50 +02006005 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006006 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006007 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6008 || pim->state->c
6009 == NFA_START_INVISIBLE_BEFORE_NEG
6010 || pim->state->c
6011 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006012 {
6013 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006014 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006015#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006016 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006017 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006018#endif
6019 }
6020 else
6021 /* look-behind match failed, don't add the state */
6022 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006023
6024 /* Postponed invisible match was handled, don't add it to
6025 * following states. */
6026 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006027 }
6028
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006029 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006030 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006031 else
6032 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006033 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006034 if (add_count > 0)
6035 nextlist->t[nextlist->n - 1].count = add_count;
6036 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006037 }
6038
6039 } /* for (thislist = thislist; thislist->state; thislist++) */
6040
Bram Moolenaare23febd2013-05-26 18:40:14 +02006041 /* Look for the start of a match in the current position by adding the
6042 * start state to the list of states.
6043 * The first found match is the leftmost one, thus the order of states
6044 * matters!
6045 * Do not add the start state in recursive calls of nfa_regmatch(),
6046 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006047 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006048 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006049 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006050 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006051 && reglnum == 0
6052 && clen != 0
6053 && (ireg_maxcol == 0
6054 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006055 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006056 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006057 ? (reglnum < nfa_endp->se_u.pos.lnum
6058 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006059 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006060 < nfa_endp->se_u.pos.col))
6061 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006062 {
6063#ifdef ENABLE_LOG
6064 fprintf(log_fd, "(---) STARTSTATE\n");
6065#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006066 /* Inline optimized code for addstate() if we know the state is
6067 * the first MOPEN. */
6068 if (toplevel)
6069 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006070 int add = TRUE;
6071 int c;
6072
6073 if (prog->regstart != NUL && clen != 0)
6074 {
6075 if (nextlist->n == 0)
6076 {
6077 colnr_T col = (colnr_T)(reginput - regline) + clen;
6078
6079 /* Nextlist is empty, we can skip ahead to the
6080 * character that must appear at the start. */
6081 if (skip_to_start(prog->regstart, &col) == FAIL)
6082 break;
6083#ifdef ENABLE_LOG
6084 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6085 col - ((colnr_T)(reginput - regline) + clen));
6086#endif
6087 reginput = regline + col - clen;
6088 }
6089 else
6090 {
6091 /* Checking if the required start character matches is
6092 * cheaper than adding a state that won't match. */
6093 c = PTR2CHAR(reginput + clen);
6094 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6095 != MB_TOLOWER(prog->regstart)))
6096 {
6097#ifdef ENABLE_LOG
6098 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6099#endif
6100 add = FALSE;
6101 }
6102 }
6103 }
6104
6105 if (add)
6106 {
6107 if (REG_MULTI)
6108 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006109 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006110 else
6111 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006112 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006113 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006114 }
6115 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006116 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117 }
6118
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006119#ifdef ENABLE_LOG
6120 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006121 {
6122 int i;
6123
6124 for (i = 0; i < thislist->n; i++)
6125 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6126 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006127 fprintf(log_fd, "\n");
6128#endif
6129
6130nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006131 /* Advance to the next character, or advance to the next line, or
6132 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006133 if (clen != 0)
6134 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006135 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6136 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006137 reg_nextline();
6138 else
6139 break;
6140 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006141
6142#ifdef ENABLE_LOG
6143 if (log_fd != stderr)
6144 fclose(log_fd);
6145 log_fd = NULL;
6146#endif
6147
6148theend:
6149 /* Free memory */
6150 vim_free(list[0].t);
6151 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006152 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006153#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006154#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006155 fclose(debug);
6156#endif
6157
Bram Moolenaar963fee22013-05-26 21:47:28 +02006158 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006159}
6160
6161/*
6162 * Try match of "prog" with at regline["col"].
6163 * Returns 0 for failure, number of lines contained in the match otherwise.
6164 */
6165 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006166nfa_regtry(prog, col)
6167 nfa_regprog_T *prog;
6168 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006169{
6170 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006171 regsubs_T subs, m;
6172 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006173#ifdef ENABLE_LOG
6174 FILE *f;
6175#endif
6176
6177 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006178
6179#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006180 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006181 if (f != NULL)
6182 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006183 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006184#ifdef DEBUG
6185 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6186#endif
6187 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006188 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006189 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006190 fprintf(f, "\n\n");
6191 fclose(f);
6192 }
6193 else
6194 EMSG(_("Could not open temporary log file for writing "));
6195#endif
6196
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006197 clear_sub(&subs.norm);
6198 clear_sub(&m.norm);
6199#ifdef FEAT_SYN_HL
6200 clear_sub(&subs.synt);
6201 clear_sub(&m.synt);
6202#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006203
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006204 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006205 return 0;
6206
6207 cleanup_subexpr();
6208 if (REG_MULTI)
6209 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006210 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006211 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006212 reg_startpos[i] = subs.norm.list.multi[i].start;
6213 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006214 }
6215
6216 if (reg_startpos[0].lnum < 0)
6217 {
6218 reg_startpos[0].lnum = 0;
6219 reg_startpos[0].col = col;
6220 }
6221 if (reg_endpos[0].lnum < 0)
6222 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006223 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006224 reg_endpos[0].lnum = reglnum;
6225 reg_endpos[0].col = (int)(reginput - regline);
6226 }
6227 else
6228 /* Use line number of "\ze". */
6229 reglnum = reg_endpos[0].lnum;
6230 }
6231 else
6232 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006233 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006234 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006235 reg_startp[i] = subs.norm.list.line[i].start;
6236 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006237 }
6238
6239 if (reg_startp[0] == NULL)
6240 reg_startp[0] = regline + col;
6241 if (reg_endp[0] == NULL)
6242 reg_endp[0] = reginput;
6243 }
6244
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006245#ifdef FEAT_SYN_HL
6246 /* Package any found \z(...\) matches for export. Default is none. */
6247 unref_extmatch(re_extmatch_out);
6248 re_extmatch_out = NULL;
6249
6250 if (prog->reghasz == REX_SET)
6251 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006252 cleanup_zsubexpr();
6253 re_extmatch_out = make_extmatch();
6254 for (i = 0; i < subs.synt.in_use; i++)
6255 {
6256 if (REG_MULTI)
6257 {
6258 struct multipos *mpos = &subs.synt.list.multi[i];
6259
6260 /* Only accept single line matches. */
6261 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6262 re_extmatch_out->matches[i] =
6263 vim_strnsave(reg_getline(mpos->start.lnum)
6264 + mpos->start.col,
6265 mpos->end.col - mpos->start.col);
6266 }
6267 else
6268 {
6269 struct linepos *lpos = &subs.synt.list.line[i];
6270
6271 if (lpos->start != NULL && lpos->end != NULL)
6272 re_extmatch_out->matches[i] =
6273 vim_strnsave(lpos->start,
6274 (int)(lpos->end - lpos->start));
6275 }
6276 }
6277 }
6278#endif
6279
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006280 return 1 + reglnum;
6281}
6282
6283/*
6284 * Match a regexp against a string ("line" points to the string) or multiple
6285 * lines ("line" is NULL, use reg_getline()).
6286 *
6287 * Returns 0 for failure, number of lines contained in the match otherwise.
6288 */
6289 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006290nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006291 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006292 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006293{
6294 nfa_regprog_T *prog;
6295 long retval = 0L;
6296 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006297 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006298
6299 if (REG_MULTI)
6300 {
6301 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6302 line = reg_getline((linenr_T)0); /* relative to the cursor */
6303 reg_startpos = reg_mmatch->startpos;
6304 reg_endpos = reg_mmatch->endpos;
6305 }
6306 else
6307 {
6308 prog = (nfa_regprog_T *)reg_match->regprog;
6309 reg_startp = reg_match->startp;
6310 reg_endp = reg_match->endp;
6311 }
6312
6313 /* Be paranoid... */
6314 if (prog == NULL || line == NULL)
6315 {
6316 EMSG(_(e_null));
6317 goto theend;
6318 }
6319
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006320 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6321 if (prog->regflags & RF_ICASE)
6322 ireg_ic = TRUE;
6323 else if (prog->regflags & RF_NOICASE)
6324 ireg_ic = FALSE;
6325
6326#ifdef FEAT_MBYTE
6327 /* If pattern contains "\Z" overrule value of ireg_icombine */
6328 if (prog->regflags & RF_ICOMBINE)
6329 ireg_icombine = TRUE;
6330#endif
6331
6332 regline = line;
6333 reglnum = 0; /* relative to line */
6334
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006335 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006336 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006337 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006338 nfa_listid = 1;
6339 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006340#ifdef DEBUG
6341 nfa_regengine.expr = prog->pattern;
6342#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006343
Bram Moolenaard89616e2013-06-06 18:46:06 +02006344 if (prog->reganch && col > 0)
6345 return 0L;
6346
Bram Moolenaar473de612013-06-08 18:19:48 +02006347 need_clear_subexpr = TRUE;
6348#ifdef FEAT_SYN_HL
6349 /* Clear the external match subpointers if necessary. */
6350 if (prog->reghasz == REX_SET)
6351 {
6352 nfa_has_zsubexpr = TRUE;
6353 need_clear_zsubexpr = TRUE;
6354 }
6355 else
6356 nfa_has_zsubexpr = FALSE;
6357#endif
6358
Bram Moolenaard89616e2013-06-06 18:46:06 +02006359 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006360 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006361 /* Skip ahead until a character we know the match must start with.
6362 * When there is none there is no match. */
6363 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006364 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006365
Bram Moolenaar473de612013-06-08 18:19:48 +02006366 /* If match_text is set it contains the full text that must match.
6367 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006368 if (prog->match_text != NULL
6369#ifdef FEAT_MBYTE
6370 && !ireg_icombine
6371#endif
6372 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006373 return find_match_text(col, prog->regstart, prog->match_text);
6374 }
6375
Bram Moolenaard89616e2013-06-06 18:46:06 +02006376 /* If the start column is past the maximum column: no need to try. */
6377 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6378 goto theend;
6379
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006380 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006381 for (i = 0; i < nstate; ++i)
6382 {
6383 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006384 prog->state[i].lastlist[0] = 0;
6385 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386 }
6387
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006388 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006389
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006390#ifdef DEBUG
6391 nfa_regengine.expr = NULL;
6392#endif
6393
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006394theend:
6395 return retval;
6396}
6397
6398/*
6399 * Compile a regular expression into internal code for the NFA matcher.
6400 * Returns the program in allocated space. Returns NULL for an error.
6401 */
6402 static regprog_T *
6403nfa_regcomp(expr, re_flags)
6404 char_u *expr;
6405 int re_flags;
6406{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006407 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006408 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006409 int *postfix;
6410
6411 if (expr == NULL)
6412 return NULL;
6413
6414#ifdef DEBUG
6415 nfa_regengine.expr = expr;
6416#endif
6417
6418 init_class_tab();
6419
6420 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6421 return NULL;
6422
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006423 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006424 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006425 postfix = re2post();
6426 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006427 {
6428 /* TODO: only give this error for debugging? */
6429 if (post_ptr >= post_end)
6430 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006431 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006432 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006433
6434 /*
6435 * In order to build the NFA, we parse the input regexp twice:
6436 * 1. first pass to count size (so we can allocate space)
6437 * 2. second to emit code
6438 */
6439#ifdef ENABLE_LOG
6440 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006441 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006442
6443 if (f != NULL)
6444 {
6445 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6446 fclose(f);
6447 }
6448 }
6449#endif
6450
6451 /*
6452 * PASS 1
6453 * Count number of NFA states in "nstate". Do not build the NFA.
6454 */
6455 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006456
Bram Moolenaar16619a22013-06-11 18:42:36 +02006457 /* allocate the regprog with space for the compiled regexp */
6458 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006459 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6460 if (prog == NULL)
6461 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006462 state_ptr = prog->state;
6463
6464 /*
6465 * PASS 2
6466 * Build the NFA
6467 */
6468 prog->start = post2nfa(postfix, post_ptr, FALSE);
6469 if (prog->start == NULL)
6470 goto fail;
6471
6472 prog->regflags = regflags;
6473 prog->engine = &nfa_regengine;
6474 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006475 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006476 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006477 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006478
Bram Moolenaara2947e22013-06-11 22:44:09 +02006479 nfa_postprocess(prog);
6480
Bram Moolenaard89616e2013-06-06 18:46:06 +02006481 prog->reganch = nfa_get_reganch(prog->start, 0);
6482 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006483 prog->match_text = nfa_get_match_text(prog->start);
6484
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006485#ifdef ENABLE_LOG
6486 nfa_postfix_dump(expr, OK);
6487 nfa_dump(prog);
6488#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006489#ifdef FEAT_SYN_HL
6490 /* Remember whether this pattern has any \z specials in it. */
6491 prog->reghasz = re_has_z;
6492#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006493#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006494 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006495 nfa_regengine.expr = NULL;
6496#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006497
6498out:
6499 vim_free(post_start);
6500 post_start = post_ptr = post_end = NULL;
6501 state_ptr = NULL;
6502 return (regprog_T *)prog;
6503
6504fail:
6505 vim_free(prog);
6506 prog = NULL;
6507#ifdef ENABLE_LOG
6508 nfa_postfix_dump(expr, FAIL);
6509#endif
6510#ifdef DEBUG
6511 nfa_regengine.expr = NULL;
6512#endif
6513 goto out;
6514}
6515
Bram Moolenaar473de612013-06-08 18:19:48 +02006516/*
6517 * Free a compiled regexp program, returned by nfa_regcomp().
6518 */
6519 static void
6520nfa_regfree(prog)
6521 regprog_T *prog;
6522{
6523 if (prog != NULL)
6524 {
6525 vim_free(((nfa_regprog_T *)prog)->match_text);
6526#ifdef DEBUG
6527 vim_free(((nfa_regprog_T *)prog)->pattern);
6528#endif
6529 vim_free(prog);
6530 }
6531}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006532
6533/*
6534 * Match a regexp against a string.
6535 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6536 * Uses curbuf for line count and 'iskeyword'.
6537 *
6538 * Return TRUE if there is a match, FALSE if not.
6539 */
6540 static int
6541nfa_regexec(rmp, line, col)
6542 regmatch_T *rmp;
6543 char_u *line; /* string to match against */
6544 colnr_T col; /* column to start looking for match */
6545{
6546 reg_match = rmp;
6547 reg_mmatch = NULL;
6548 reg_maxline = 0;
6549 reg_line_lbr = FALSE;
6550 reg_buf = curbuf;
6551 reg_win = NULL;
6552 ireg_ic = rmp->rm_ic;
6553#ifdef FEAT_MBYTE
6554 ireg_icombine = FALSE;
6555#endif
6556 ireg_maxcol = 0;
6557 return (nfa_regexec_both(line, col) != 0);
6558}
6559
6560#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6561 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6562
6563static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6564
6565/*
6566 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6567 */
6568 static int
6569nfa_regexec_nl(rmp, line, col)
6570 regmatch_T *rmp;
6571 char_u *line; /* string to match against */
6572 colnr_T col; /* column to start looking for match */
6573{
6574 reg_match = rmp;
6575 reg_mmatch = NULL;
6576 reg_maxline = 0;
6577 reg_line_lbr = TRUE;
6578 reg_buf = curbuf;
6579 reg_win = NULL;
6580 ireg_ic = rmp->rm_ic;
6581#ifdef FEAT_MBYTE
6582 ireg_icombine = FALSE;
6583#endif
6584 ireg_maxcol = 0;
6585 return (nfa_regexec_both(line, col) != 0);
6586}
6587#endif
6588
6589
6590/*
6591 * Match a regexp against multiple lines.
6592 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6593 * Uses curbuf for line count and 'iskeyword'.
6594 *
6595 * Return zero if there is no match. Return number of lines contained in the
6596 * match otherwise.
6597 *
6598 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6599 *
6600 * ! Also NOTE : match may actually be in another line. e.g.:
6601 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6602 *
6603 * +-------------------------+
6604 * |a |
6605 * |b |
6606 * |c |
6607 * | |
6608 * +-------------------------+
6609 *
6610 * then nfa_regexec_multi() returns 3. while the original
6611 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6612 *
6613 * FIXME if this behavior is not compatible.
6614 */
6615 static long
6616nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6617 regmmatch_T *rmp;
6618 win_T *win; /* window in which to search or NULL */
6619 buf_T *buf; /* buffer in which to search */
6620 linenr_T lnum; /* nr of line to start looking for match */
6621 colnr_T col; /* column to start looking for match */
6622 proftime_T *tm UNUSED; /* timeout limit or NULL */
6623{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006624 reg_match = NULL;
6625 reg_mmatch = rmp;
6626 reg_buf = buf;
6627 reg_win = win;
6628 reg_firstlnum = lnum;
6629 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6630 reg_line_lbr = FALSE;
6631 ireg_ic = rmp->rmm_ic;
6632#ifdef FEAT_MBYTE
6633 ireg_icombine = FALSE;
6634#endif
6635 ireg_maxcol = rmp->rmm_maxcol;
6636
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006637 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006638}
6639
6640#ifdef DEBUG
6641# undef ENABLE_LOG
6642#endif