blob: 35b42ef9cf1c239b4fcf4a817525dce8ee619325 [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 Moolenaarf86c0b02013-06-26 12:42:44 +0200293static void st_error __ARGS((int *postfix, int *end, int *p));
294static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200296static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200298static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
299static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200300static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200301static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
303static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200304static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
306static 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 +0200307static int match_follows __ARGS((nfa_state_T *startstate, int depth));
308static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200309
310/* helper functions used when doing re2post() ... regatom() parsing */
311#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200312 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200313 return FAIL; \
314 *post_ptr++ = c; \
315 } while (0)
316
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200317/*
318 * Initialize internal variables before NFA compilation.
319 * Return OK on success, FAIL otherwise.
320 */
321 static int
322nfa_regcomp_start(expr, re_flags)
323 char_u *expr;
324 int re_flags; /* see vim_regcomp() */
325{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200326 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200327 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328
329 nstate = 0;
330 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200331 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200332 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200333
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200334 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200335 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200336 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200337
338 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200339 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341 post_start = (int *)lalloc(postfix_size, TRUE);
342 if (post_start == NULL)
343 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200345 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200347 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200348
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200349 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200350 regcomp_start(expr, re_flags);
351
352 return OK;
353}
354
355/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200356 * Figure out if the NFA state list starts with an anchor, must match at start
357 * of the line.
358 */
359 static int
360nfa_get_reganch(start, depth)
361 nfa_state_T *start;
362 int depth;
363{
364 nfa_state_T *p = start;
365
366 if (depth > 4)
367 return 0;
368
369 while (p != NULL)
370 {
371 switch (p->c)
372 {
373 case NFA_BOL:
374 case NFA_BOF:
375 return 1; /* yes! */
376
377 case NFA_ZSTART:
378 case NFA_ZEND:
379 case NFA_CURSOR:
380 case NFA_VISUAL:
381
382 case NFA_MOPEN:
383 case NFA_MOPEN1:
384 case NFA_MOPEN2:
385 case NFA_MOPEN3:
386 case NFA_MOPEN4:
387 case NFA_MOPEN5:
388 case NFA_MOPEN6:
389 case NFA_MOPEN7:
390 case NFA_MOPEN8:
391 case NFA_MOPEN9:
392 case NFA_NOPEN:
393#ifdef FEAT_SYN_HL
394 case NFA_ZOPEN:
395 case NFA_ZOPEN1:
396 case NFA_ZOPEN2:
397 case NFA_ZOPEN3:
398 case NFA_ZOPEN4:
399 case NFA_ZOPEN5:
400 case NFA_ZOPEN6:
401 case NFA_ZOPEN7:
402 case NFA_ZOPEN8:
403 case NFA_ZOPEN9:
404#endif
405 p = p->out;
406 break;
407
408 case NFA_SPLIT:
409 return nfa_get_reganch(p->out, depth + 1)
410 && nfa_get_reganch(p->out1, depth + 1);
411
412 default:
413 return 0; /* noooo */
414 }
415 }
416 return 0;
417}
418
419/*
420 * Figure out if the NFA state list starts with a character which must match
421 * at start of the match.
422 */
423 static int
424nfa_get_regstart(start, depth)
425 nfa_state_T *start;
426 int depth;
427{
428 nfa_state_T *p = start;
429
430 if (depth > 4)
431 return 0;
432
433 while (p != NULL)
434 {
435 switch (p->c)
436 {
437 /* all kinds of zero-width matches */
438 case NFA_BOL:
439 case NFA_BOF:
440 case NFA_BOW:
441 case NFA_EOW:
442 case NFA_ZSTART:
443 case NFA_ZEND:
444 case NFA_CURSOR:
445 case NFA_VISUAL:
446 case NFA_LNUM:
447 case NFA_LNUM_GT:
448 case NFA_LNUM_LT:
449 case NFA_COL:
450 case NFA_COL_GT:
451 case NFA_COL_LT:
452 case NFA_VCOL:
453 case NFA_VCOL_GT:
454 case NFA_VCOL_LT:
455 case NFA_MARK:
456 case NFA_MARK_GT:
457 case NFA_MARK_LT:
458
459 case NFA_MOPEN:
460 case NFA_MOPEN1:
461 case NFA_MOPEN2:
462 case NFA_MOPEN3:
463 case NFA_MOPEN4:
464 case NFA_MOPEN5:
465 case NFA_MOPEN6:
466 case NFA_MOPEN7:
467 case NFA_MOPEN8:
468 case NFA_MOPEN9:
469 case NFA_NOPEN:
470#ifdef FEAT_SYN_HL
471 case NFA_ZOPEN:
472 case NFA_ZOPEN1:
473 case NFA_ZOPEN2:
474 case NFA_ZOPEN3:
475 case NFA_ZOPEN4:
476 case NFA_ZOPEN5:
477 case NFA_ZOPEN6:
478 case NFA_ZOPEN7:
479 case NFA_ZOPEN8:
480 case NFA_ZOPEN9:
481#endif
482 p = p->out;
483 break;
484
485 case NFA_SPLIT:
486 {
487 int c1 = nfa_get_regstart(p->out, depth + 1);
488 int c2 = nfa_get_regstart(p->out1, depth + 1);
489
490 if (c1 == c2)
491 return c1; /* yes! */
492 return 0;
493 }
494
495 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200496 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200497 return p->c; /* yes! */
498 return 0;
499 }
500 }
501 return 0;
502}
503
504/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200505 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200506 * else. If so return a string in allocated memory with what must match after
507 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200508 */
509 static char_u *
510nfa_get_match_text(start)
511 nfa_state_T *start;
512{
513 nfa_state_T *p = start;
514 int len = 0;
515 char_u *ret;
516 char_u *s;
517
518 if (p->c != NFA_MOPEN)
519 return NULL; /* just in case */
520 p = p->out;
521 while (p->c > 0)
522 {
523 len += MB_CHAR2LEN(p->c);
524 p = p->out;
525 }
526 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
527 return NULL;
528
529 ret = alloc(len);
530 if (ret != NULL)
531 {
532 len = 0;
533 p = start->out->out; /* skip first char, it goes into regstart */
534 s = ret;
535 while (p->c > 0)
536 {
537#ifdef FEAT_MBYTE
538 if (has_mbyte)
539 s += (*mb_char2bytes)(p->c, s);
540 else
541#endif
542 *s++ = p->c;
543 p = p->out;
544 }
545 *s = NUL;
546 }
547 return ret;
548}
549
550/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200551 * Allocate more space for post_start. Called when
552 * running above the estimated number of states.
553 */
554 static int
555realloc_post_list()
556{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200557 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 int new_max = nstate_max + 1000;
559 int *new_start;
560 int *old_start;
561
562 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
563 if (new_start == NULL)
564 return FAIL;
565 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200566 old_start = post_start;
567 post_start = new_start;
568 post_ptr = new_start + (post_ptr - old_start);
569 post_end = post_start + new_max;
570 vim_free(old_start);
571 return OK;
572}
573
574/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200575 * Search between "start" and "end" and try to recognize a
576 * character class in expanded form. For example [0-9].
577 * On success, return the id the character class to be emitted.
578 * On failure, return 0 (=FAIL)
579 * Start points to the first char of the range, while end should point
580 * to the closing brace.
581 */
582 static int
583nfa_recognize_char_class(start, end, extra_newl)
584 char_u *start;
585 char_u *end;
586 int extra_newl;
587{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200588# define CLASS_not 0x80
589# define CLASS_af 0x40
590# define CLASS_AF 0x20
591# define CLASS_az 0x10
592# define CLASS_AZ 0x08
593# define CLASS_o7 0x04
594# define CLASS_o9 0x02
595# define CLASS_underscore 0x01
596
597 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200599 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200600
601 if (extra_newl == TRUE)
602 newl = TRUE;
603
604 if (*end != ']')
605 return FAIL;
606 p = start;
607 if (*p == '^')
608 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200609 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200610 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611 }
612
613 while (p < end)
614 {
615 if (p + 2 < end && *(p + 1) == '-')
616 {
617 switch (*p)
618 {
619 case '0':
620 if (*(p + 2) == '9')
621 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200622 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200623 break;
624 }
625 else
626 if (*(p + 2) == '7')
627 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200628 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 break;
630 }
631 case 'a':
632 if (*(p + 2) == 'z')
633 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200634 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 break;
636 }
637 else
638 if (*(p + 2) == 'f')
639 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200640 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 break;
642 }
643 case 'A':
644 if (*(p + 2) == 'Z')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
649 else
650 if (*(p + 2) == 'F')
651 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200652 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200653 break;
654 }
655 /* FALLTHROUGH */
656 default:
657 return FAIL;
658 }
659 p += 3;
660 }
661 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
662 {
663 newl = TRUE;
664 p += 2;
665 }
666 else if (*p == '_')
667 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200668 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200669 p ++;
670 }
671 else if (*p == '\n')
672 {
673 newl = TRUE;
674 p ++;
675 }
676 else
677 return FAIL;
678 } /* while (p < end) */
679
680 if (p != end)
681 return FAIL;
682
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200683 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200684 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200685
686 switch (config)
687 {
688 case CLASS_o9:
689 return extra_newl + NFA_DIGIT;
690 case CLASS_not | CLASS_o9:
691 return extra_newl + NFA_NDIGIT;
692 case CLASS_af | CLASS_AF | CLASS_o9:
693 return extra_newl + NFA_HEX;
694 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
695 return extra_newl + NFA_NHEX;
696 case CLASS_o7:
697 return extra_newl + NFA_OCTAL;
698 case CLASS_not | CLASS_o7:
699 return extra_newl + NFA_NOCTAL;
700 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
701 return extra_newl + NFA_WORD;
702 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
703 return extra_newl + NFA_NWORD;
704 case CLASS_az | CLASS_AZ | CLASS_underscore:
705 return extra_newl + NFA_HEAD;
706 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
707 return extra_newl + NFA_NHEAD;
708 case CLASS_az | CLASS_AZ:
709 return extra_newl + NFA_ALPHA;
710 case CLASS_not | CLASS_az | CLASS_AZ:
711 return extra_newl + NFA_NALPHA;
712 case CLASS_az:
713 return extra_newl + NFA_LOWER;
714 case CLASS_not | CLASS_az:
715 return extra_newl + NFA_NLOWER;
716 case CLASS_AZ:
717 return extra_newl + NFA_UPPER;
718 case CLASS_not | CLASS_AZ:
719 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200721 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200722}
723
724/*
725 * Produce the bytes for equivalence class "c".
726 * Currently only handles latin1, latin9 and utf-8.
727 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
728 * equivalent to 'a OR b OR c'
729 *
730 * NOTE! When changing this function, also update reg_equi_class()
731 */
732 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200733nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200734 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200736#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200737
738#ifdef FEAT_MBYTE
739 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
740 || STRCMP(p_enc, "iso-8859-15") == 0)
741#endif
742 {
743 switch (c)
744 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200745 case 'A': case 0300: case 0301: case 0302:
746 case 0303: case 0304: case 0305:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200747 EMIT2('A'); EMIT2(0300); EMIT2(0301);
748 EMIT2(0302); EMIT2(0303); EMIT2(0304);
Bram Moolenaar417bad22013-06-07 14:08:30 +0200749 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750 return OK;
751
Bram Moolenaar417bad22013-06-07 14:08:30 +0200752 case 'C': case 0307:
753 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200754 return OK;
755
Bram Moolenaar417bad22013-06-07 14:08:30 +0200756 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200757 EMIT2('E'); EMIT2(0310); EMIT2(0311);
758 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200759 return OK;
760
Bram Moolenaar417bad22013-06-07 14:08:30 +0200761 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200762 EMIT2('I'); EMIT2(0314); EMIT2(0315);
763 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200764 return OK;
765
Bram Moolenaar417bad22013-06-07 14:08:30 +0200766 case 'N': case 0321:
767 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200768 return OK;
769
Bram Moolenaar417bad22013-06-07 14:08:30 +0200770 case 'O': case 0322: case 0323: case 0324: case 0325:
771 case 0326:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200772 EMIT2('O'); EMIT2(0322); EMIT2(0323);
773 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200774 return OK;
775
Bram Moolenaar417bad22013-06-07 14:08:30 +0200776 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200777 EMIT2('U'); EMIT2(0331); EMIT2(0332);
778 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200779 return OK;
780
Bram Moolenaar417bad22013-06-07 14:08:30 +0200781 case 'Y': case 0335:
782 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200783 return OK;
784
Bram Moolenaar417bad22013-06-07 14:08:30 +0200785 case 'a': case 0340: case 0341: case 0342:
786 case 0343: case 0344: case 0345:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200787 EMIT2('a'); EMIT2(0340); EMIT2(0341);
788 EMIT2(0342); EMIT2(0343); EMIT2(0344);
Bram Moolenaar417bad22013-06-07 14:08:30 +0200789 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200790 return OK;
791
Bram Moolenaar417bad22013-06-07 14:08:30 +0200792 case 'c': case 0347:
793 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200794 return OK;
795
Bram Moolenaar417bad22013-06-07 14:08:30 +0200796 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200797 EMIT2('e'); EMIT2(0350); EMIT2(0351);
798 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200799 return OK;
800
Bram Moolenaar417bad22013-06-07 14:08:30 +0200801 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200802 EMIT2('i'); EMIT2(0354); EMIT2(0355);
803 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200804 return OK;
805
Bram Moolenaar417bad22013-06-07 14:08:30 +0200806 case 'n': case 0361:
807 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 return OK;
809
Bram Moolenaar417bad22013-06-07 14:08:30 +0200810 case 'o': case 0362: case 0363: case 0364: case 0365:
811 case 0366:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200812 EMIT2('o'); EMIT2(0362); EMIT2(0363);
813 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 return OK;
815
Bram Moolenaar417bad22013-06-07 14:08:30 +0200816 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200817 EMIT2('u'); EMIT2(0371); EMIT2(0372);
818 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200819 return OK;
820
Bram Moolenaar417bad22013-06-07 14:08:30 +0200821 case 'y': case 0375: case 0377:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200822 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200823 return OK;
824
825 default:
826 return FAIL;
827 }
828 }
829
830 EMIT(c);
831 return OK;
832#undef EMIT2
833}
834
835/*
836 * Code to parse regular expression.
837 *
838 * We try to reuse parsing functions in regexp.c to
839 * minimize surprise and keep the syntax consistent.
840 */
841
842/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200843 * Parse the lowest level.
844 *
845 * An atom can be one of a long list of items. Many atoms match one character
846 * in the text. It is often an ordinary character or a character class.
847 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
848 * is only for syntax highlighting.
849 *
850 * atom ::= ordinary-atom
851 * or \( pattern \)
852 * or \%( pattern \)
853 * or \z( pattern \)
854 */
855 static int
856nfa_regatom()
857{
858 int c;
859 int charclass;
860 int equiclass;
861 int collclass;
862 int got_coll_char;
863 char_u *p;
864 char_u *endp;
865#ifdef FEAT_MBYTE
866 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867#endif
868 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200869 int emit_range;
870 int negated;
871 int result;
872 int startc = -1;
873 int endc = -1;
874 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200876 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200877 switch (c)
878 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200879 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200880 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
881
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200882 case Magic('^'):
883 EMIT(NFA_BOL);
884 break;
885
886 case Magic('$'):
887 EMIT(NFA_EOL);
888#if defined(FEAT_SYN_HL) || defined(PROTO)
889 had_eol = TRUE;
890#endif
891 break;
892
893 case Magic('<'):
894 EMIT(NFA_BOW);
895 break;
896
897 case Magic('>'):
898 EMIT(NFA_EOW);
899 break;
900
901 case Magic('_'):
902 c = no_Magic(getchr());
903 if (c == '^') /* "\_^" is start-of-line */
904 {
905 EMIT(NFA_BOL);
906 break;
907 }
908 if (c == '$') /* "\_$" is end-of-line */
909 {
910 EMIT(NFA_EOL);
911#if defined(FEAT_SYN_HL) || defined(PROTO)
912 had_eol = TRUE;
913#endif
914 break;
915 }
916
917 extra = ADD_NL;
918
919 /* "\_[" is collection plus newline */
920 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200921 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200922
923 /* "\_x" is character class plus newline */
924 /*FALLTHROUGH*/
925
926 /*
927 * Character classes.
928 */
929 case Magic('.'):
930 case Magic('i'):
931 case Magic('I'):
932 case Magic('k'):
933 case Magic('K'):
934 case Magic('f'):
935 case Magic('F'):
936 case Magic('p'):
937 case Magic('P'):
938 case Magic('s'):
939 case Magic('S'):
940 case Magic('d'):
941 case Magic('D'):
942 case Magic('x'):
943 case Magic('X'):
944 case Magic('o'):
945 case Magic('O'):
946 case Magic('w'):
947 case Magic('W'):
948 case Magic('h'):
949 case Magic('H'):
950 case Magic('a'):
951 case Magic('A'):
952 case Magic('l'):
953 case Magic('L'):
954 case Magic('u'):
955 case Magic('U'):
956 p = vim_strchr(classchars, no_Magic(c));
957 if (p == NULL)
958 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200959 EMSGN("INTERNAL: Unknown character class char: %ld", c);
960 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200961 }
962#ifdef FEAT_MBYTE
963 /* When '.' is followed by a composing char ignore the dot, so that
964 * the composing char is matched here. */
965 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
966 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200967 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200968 c = getchr();
969 goto nfa_do_multibyte;
970 }
971#endif
972 EMIT(nfa_classcodes[p - classchars]);
973 if (extra == ADD_NL)
974 {
975 EMIT(NFA_NEWL);
976 EMIT(NFA_OR);
977 regflags |= RF_HASNL;
978 }
979 break;
980
981 case Magic('n'):
982 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200983 /* In a string "\n" matches a newline character. */
984 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200985 else
986 {
987 /* In buffer text "\n" matches the end of a line. */
988 EMIT(NFA_NEWL);
989 regflags |= RF_HASNL;
990 }
991 break;
992
993 case Magic('('):
994 if (nfa_reg(REG_PAREN) == FAIL)
995 return FAIL; /* cascaded error */
996 break;
997
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200998 case Magic('|'):
999 case Magic('&'):
1000 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001001 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001002 return FAIL;
1003
1004 case Magic('='):
1005 case Magic('?'):
1006 case Magic('+'):
1007 case Magic('@'):
1008 case Magic('*'):
1009 case Magic('{'):
1010 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001011 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001012 return FAIL;
1013
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001014 case Magic('~'):
1015 {
1016 char_u *lp;
1017
1018 /* Previous substitute pattern.
1019 * Generated as "\%(pattern\)". */
1020 if (reg_prev_sub == NULL)
1021 {
1022 EMSG(_(e_nopresub));
1023 return FAIL;
1024 }
1025 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1026 {
1027 EMIT(PTR2CHAR(lp));
1028 if (lp != reg_prev_sub)
1029 EMIT(NFA_CONCAT);
1030 }
1031 EMIT(NFA_NOPEN);
1032 break;
1033 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001034
Bram Moolenaar428e9872013-05-30 17:05:39 +02001035 case Magic('1'):
1036 case Magic('2'):
1037 case Magic('3'):
1038 case Magic('4'):
1039 case Magic('5'):
1040 case Magic('6'):
1041 case Magic('7'):
1042 case Magic('8'):
1043 case Magic('9'):
1044 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1045 nfa_has_backref = TRUE;
1046 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001047
1048 case Magic('z'):
1049 c = no_Magic(getchr());
1050 switch (c)
1051 {
1052 case 's':
1053 EMIT(NFA_ZSTART);
1054 break;
1055 case 'e':
1056 EMIT(NFA_ZEND);
1057 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001058 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001059#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001060 case '1':
1061 case '2':
1062 case '3':
1063 case '4':
1064 case '5':
1065 case '6':
1066 case '7':
1067 case '8':
1068 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001069 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001070 if (reg_do_extmatch != REX_USE)
1071 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001072 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1073 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001074 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001075 re_has_z = REX_USE;
1076 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001077 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001078 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001079 if (reg_do_extmatch != REX_SET)
1080 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001081 if (nfa_reg(REG_ZPAREN) == FAIL)
1082 return FAIL; /* cascaded error */
1083 re_has_z = REX_SET;
1084 break;
1085#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001087 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001088 no_Magic(c));
1089 return FAIL;
1090 }
1091 break;
1092
1093 case Magic('%'):
1094 c = no_Magic(getchr());
1095 switch (c)
1096 {
1097 /* () without a back reference */
1098 case '(':
1099 if (nfa_reg(REG_NPAREN) == FAIL)
1100 return FAIL;
1101 EMIT(NFA_NOPEN);
1102 break;
1103
1104 case 'd': /* %d123 decimal */
1105 case 'o': /* %o123 octal */
1106 case 'x': /* %xab hex 2 */
1107 case 'u': /* %uabcd hex 4 */
1108 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001109 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001110 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001111
Bram Moolenaar47196582013-05-25 22:04:23 +02001112 switch (c)
1113 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001114 case 'd': nr = getdecchrs(); break;
1115 case 'o': nr = getoctchrs(); break;
1116 case 'x': nr = gethexchrs(2); break;
1117 case 'u': nr = gethexchrs(4); break;
1118 case 'U': nr = gethexchrs(8); break;
1119 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001120 }
1121
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001122 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001123 EMSG2_RET_FAIL(
1124 _("E678: Invalid character after %s%%[dxouU]"),
1125 reg_magic == MAGIC_ALL);
1126 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001127 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001128 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001129 break;
1130
1131 /* Catch \%^ and \%$ regardless of where they appear in the
1132 * pattern -- regardless of whether or not it makes sense. */
1133 case '^':
1134 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001135 break;
1136
1137 case '$':
1138 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139 break;
1140
1141 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001142 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001143 break;
1144
1145 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001146 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001147 break;
1148
1149 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001150 {
1151 int n;
1152
1153 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001154 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001155 {
1156 if (c == NUL)
1157 EMSG2_RET_FAIL(_(e_missing_sb),
1158 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001159 /* recursive call! */
1160 if (nfa_regatom() == FAIL)
1161 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001162 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001163 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001164 if (n == 0)
1165 EMSG2_RET_FAIL(_(e_empty_sb),
1166 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001167 EMIT(NFA_OPT_CHARS);
1168 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001169
1170 /* Emit as "\%(\%[abc]\)" to be able to handle
1171 * "\%[abc]*" which would cause the empty string to be
1172 * matched an unlimited number of times. NFA_NOPEN is
1173 * added only once at a position, while NFA_SPLIT is
1174 * added multiple times. This is more efficient than
1175 * not allowsing NFA_SPLIT multiple times, it is used
1176 * a lot. */
1177 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001178 break;
1179 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001180
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001181 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001182 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001183 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001184 int cmp = c;
1185
1186 if (c == '<' || c == '>')
1187 c = getchr();
1188 while (VIM_ISDIGIT(c))
1189 {
1190 n = n * 10 + (c - '0');
1191 c = getchr();
1192 }
1193 if (c == 'l' || c == 'c' || c == 'v')
1194 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001195 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001196 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001197 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001198 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001199 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001200 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001201 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001202 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001203 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001204 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001205 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001206 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001207 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001208 break;
1209 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001210 else if (c == '\'' && n == 0)
1211 {
1212 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001213 EMIT(cmp == '<' ? NFA_MARK_LT :
1214 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001215 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001216 break;
1217 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001218 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001219 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1220 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 return FAIL;
1222 }
1223 break;
1224
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001226collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001228 * [abc] uses NFA_START_COLL - NFA_END_COLL
1229 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1230 * Each character is produced as a regular state, using
1231 * NFA_CONCAT to bind them together.
1232 * Besides normal characters there can be:
1233 * - character classes NFA_CLASS_*
1234 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001235 */
1236
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001237 p = regparse;
1238 endp = skip_anyof(p);
1239 if (*endp == ']')
1240 {
1241 /*
1242 * Try to reverse engineer character classes. For example,
1243 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1244 * and perform the necessary substitutions in the NFA.
1245 */
1246 result = nfa_recognize_char_class(regparse, endp,
1247 extra == ADD_NL);
1248 if (result != FAIL)
1249 {
1250 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1251 EMIT(result);
1252 else /* must be char class + newline */
1253 {
1254 EMIT(result - ADD_NL);
1255 EMIT(NFA_NEWL);
1256 EMIT(NFA_OR);
1257 }
1258 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001259 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260 return OK;
1261 }
1262 /*
1263 * Failed to recognize a character class. Use the simple
1264 * version that turns [abc] into 'a' OR 'b' OR 'c'
1265 */
1266 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001268 if (*regparse == '^') /* negated range */
1269 {
1270 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001271 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001272 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001274 else
1275 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001276 if (*regparse == '-')
1277 {
1278 startc = '-';
1279 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001280 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001281 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 }
1283 /* Emit the OR branches for each character in the [] */
1284 emit_range = FALSE;
1285 while (regparse < endp)
1286 {
1287 oldstartc = startc;
1288 startc = -1;
1289 got_coll_char = FALSE;
1290 if (*regparse == '[')
1291 {
1292 /* Check for [: :], [= =], [. .] */
1293 equiclass = collclass = 0;
1294 charclass = get_char_class(&regparse);
1295 if (charclass == CLASS_NONE)
1296 {
1297 equiclass = get_equi_class(&regparse);
1298 if (equiclass == 0)
1299 collclass = get_coll_element(&regparse);
1300 }
1301
1302 /* Character class like [:alpha:] */
1303 if (charclass != CLASS_NONE)
1304 {
1305 switch (charclass)
1306 {
1307 case CLASS_ALNUM:
1308 EMIT(NFA_CLASS_ALNUM);
1309 break;
1310 case CLASS_ALPHA:
1311 EMIT(NFA_CLASS_ALPHA);
1312 break;
1313 case CLASS_BLANK:
1314 EMIT(NFA_CLASS_BLANK);
1315 break;
1316 case CLASS_CNTRL:
1317 EMIT(NFA_CLASS_CNTRL);
1318 break;
1319 case CLASS_DIGIT:
1320 EMIT(NFA_CLASS_DIGIT);
1321 break;
1322 case CLASS_GRAPH:
1323 EMIT(NFA_CLASS_GRAPH);
1324 break;
1325 case CLASS_LOWER:
1326 EMIT(NFA_CLASS_LOWER);
1327 break;
1328 case CLASS_PRINT:
1329 EMIT(NFA_CLASS_PRINT);
1330 break;
1331 case CLASS_PUNCT:
1332 EMIT(NFA_CLASS_PUNCT);
1333 break;
1334 case CLASS_SPACE:
1335 EMIT(NFA_CLASS_SPACE);
1336 break;
1337 case CLASS_UPPER:
1338 EMIT(NFA_CLASS_UPPER);
1339 break;
1340 case CLASS_XDIGIT:
1341 EMIT(NFA_CLASS_XDIGIT);
1342 break;
1343 case CLASS_TAB:
1344 EMIT(NFA_CLASS_TAB);
1345 break;
1346 case CLASS_RETURN:
1347 EMIT(NFA_CLASS_RETURN);
1348 break;
1349 case CLASS_BACKSPACE:
1350 EMIT(NFA_CLASS_BACKSPACE);
1351 break;
1352 case CLASS_ESCAPE:
1353 EMIT(NFA_CLASS_ESCAPE);
1354 break;
1355 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001356 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001357 continue;
1358 }
1359 /* Try equivalence class [=a=] and the like */
1360 if (equiclass != 0)
1361 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001362 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001363 if (result == FAIL)
1364 {
1365 /* should never happen */
1366 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1367 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001368 continue;
1369 }
1370 /* Try collating class like [. .] */
1371 if (collclass != 0)
1372 {
1373 startc = collclass; /* allow [.a.]-x as a range */
1374 /* Will emit the proper atom at the end of the
1375 * while loop. */
1376 }
1377 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001378 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1379 * start character. */
1380 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001381 {
1382 emit_range = TRUE;
1383 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001384 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385 continue; /* reading the end of the range */
1386 }
1387
1388 /* Now handle simple and escaped characters.
1389 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1390 * accepts "\t", "\e", etc., but only when the 'l' flag in
1391 * 'cpoptions' is not included.
1392 * Posix doesn't recognize backslash at all.
1393 */
1394 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001395 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001396 && regparse + 1 <= endp
1397 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001398 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399 && vim_strchr(REGEXP_ABBR, regparse[1])
1400 != NULL)
1401 )
1402 )
1403 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001404 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001406 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 startc = reg_string ? NL : NFA_NEWL;
1408 else
1409 if (*regparse == 'd'
1410 || *regparse == 'o'
1411 || *regparse == 'x'
1412 || *regparse == 'u'
1413 || *regparse == 'U'
1414 )
1415 {
1416 /* TODO(RE) This needs more testing */
1417 startc = coll_get_char();
1418 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001419 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001420 }
1421 else
1422 {
1423 /* \r,\t,\e,\b */
1424 startc = backslash_trans(*regparse);
1425 }
1426 }
1427
1428 /* Normal printable char */
1429 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001430 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001431
1432 /* Previous char was '-', so this char is end of range. */
1433 if (emit_range)
1434 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001435 endc = startc;
1436 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 if (startc > endc)
1438 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001439
1440 if (endc > startc + 2)
1441 {
1442 /* Emit a range instead of the sequence of
1443 * individual characters. */
1444 if (startc == 0)
1445 /* \x00 is translated to \x0a, start at \x01. */
1446 EMIT(1);
1447 else
1448 --post_ptr; /* remove NFA_CONCAT */
1449 EMIT(endc);
1450 EMIT(NFA_RANGE);
1451 EMIT(NFA_CONCAT);
1452 }
1453 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001454#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001455 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001456 || (*mb_char2len)(endc) > 1))
1457 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001458 /* Emit the characters in the range.
1459 * "startc" was already emitted, so skip it.
1460 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461 for (c = startc + 1; c <= endc; c++)
1462 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001463 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001464 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001466 }
1467 else
1468#endif
1469 {
1470#ifdef EBCDIC
1471 int alpha_only = FALSE;
1472
1473 /* for alphabetical range skip the gaps
1474 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1475 if (isalpha(startc) && isalpha(endc))
1476 alpha_only = TRUE;
1477#endif
1478 /* Emit the range. "startc" was already emitted, so
1479 * skip it. */
1480 for (c = startc + 1; c <= endc; c++)
1481#ifdef EBCDIC
1482 if (!alpha_only || isalpha(startc))
1483#endif
1484 {
1485 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001486 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001488 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001489 emit_range = FALSE;
1490 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001491 }
1492 else
1493 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001494 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 * Normally, simply emit startc. But if we get char
1497 * code=0 from a collating char, then replace it with
1498 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001500 * the backtracking engine. */
1501 if (startc == NFA_NEWL)
1502 {
1503 /* Line break can't be matched as part of the
1504 * collection, add an OR below. But not for negated
1505 * range. */
1506 if (!negated)
1507 extra = ADD_NL;
1508 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001510 {
1511 if (got_coll_char == TRUE && startc == 0)
1512 EMIT(0x0a);
1513 else
1514 EMIT(startc);
1515 EMIT(NFA_CONCAT);
1516 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001517 }
1518
Bram Moolenaar51a29832013-05-28 22:30:35 +02001519 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520 } /* while (p < endp) */
1521
Bram Moolenaar51a29832013-05-28 22:30:35 +02001522 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001523 if (*regparse == '-') /* if last, '-' is just a char */
1524 {
1525 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001526 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001527 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001528
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001529 /* skip the trailing ] */
1530 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001531 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001532
1533 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001534 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001535 EMIT(NFA_END_NEG_COLL);
1536 else
1537 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001538
1539 /* \_[] also matches \n but it's not negated */
1540 if (extra == ADD_NL)
1541 {
1542 EMIT(reg_string ? NL : NFA_NEWL);
1543 EMIT(NFA_OR);
1544 }
1545
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001546 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001547 } /* if exists closing ] */
1548
1549 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001551 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001552
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 default:
1554 {
1555#ifdef FEAT_MBYTE
1556 int plen;
1557
1558nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001559 /* plen is length of current char with composing chars */
1560 if (enc_utf8 && ((*mb_char2len)(c)
1561 != (plen = (*mb_ptr2len)(old_regparse))
1562 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001564 int i = 0;
1565
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001566 /* A base character plus composing characters, or just one
1567 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001568 * This requires creating a separate atom as if enclosing
1569 * the characters in (), where NFA_COMPOSING is the ( and
1570 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001571 * building the postfix form, not the NFA itself;
1572 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001573 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001574 for (;;)
1575 {
1576 EMIT(c);
1577 if (i > 0)
1578 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001579 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001580 break;
1581 c = utf_ptr2char(old_regparse + i);
1582 }
1583 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001584 regparse = old_regparse + plen;
1585 }
1586 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001587#endif
1588 {
1589 c = no_Magic(c);
1590 EMIT(c);
1591 }
1592 return OK;
1593 }
1594 }
1595
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001596 return OK;
1597}
1598
1599/*
1600 * Parse something followed by possible [*+=].
1601 *
1602 * A piece is an atom, possibly followed by a multi, an indication of how many
1603 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1604 * characters: "", "a", "aa", etc.
1605 *
1606 * piece ::= atom
1607 * or atom multi
1608 */
1609 static int
1610nfa_regpiece()
1611{
1612 int i;
1613 int op;
1614 int ret;
1615 long minval, maxval;
1616 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001617 parse_state_T old_state;
1618 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001620 int old_post_pos;
1621 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001622 int quest;
1623
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001624 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1625 * next. */
1626 save_parse_state(&old_state);
1627
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001629 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001630
1631 ret = nfa_regatom();
1632 if (ret == FAIL)
1633 return FAIL; /* cascaded error */
1634
1635 op = peekchr();
1636 if (re_multi_type(op) == NOT_MULTI)
1637 return OK;
1638
1639 skipchr();
1640 switch (op)
1641 {
1642 case Magic('*'):
1643 EMIT(NFA_STAR);
1644 break;
1645
1646 case Magic('+'):
1647 /*
1648 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1649 * first and only submatch would be "aaa". But the backtracking
1650 * engine interprets the plus as "try matching one more time", and
1651 * a* matches a second time at the end of the input, the empty
1652 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001653 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001654 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001655 * In order to be consistent with the old engine, we replace
1656 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001658 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 curchr = -1;
1660 if (nfa_regatom() == FAIL)
1661 return FAIL;
1662 EMIT(NFA_STAR);
1663 EMIT(NFA_CONCAT);
1664 skipchr(); /* skip the \+ */
1665 break;
1666
1667 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001668 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001670 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001671 switch(op)
1672 {
1673 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001674 /* \@= */
1675 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001677 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001678 /* \@! */
1679 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001680 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001682 op = no_Magic(getchr());
1683 if (op == '=')
1684 /* \@<= */
1685 i = NFA_PREV_ATOM_JUST_BEFORE;
1686 else if (op == '!')
1687 /* \@<! */
1688 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1689 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001690 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001691 /* \@> */
1692 i = NFA_PREV_ATOM_LIKE_PATTERN;
1693 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001695 if (i == 0)
1696 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001697 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1698 return FAIL;
1699 }
1700 EMIT(i);
1701 if (i == NFA_PREV_ATOM_JUST_BEFORE
1702 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1703 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001704 break;
1705
1706 case Magic('?'):
1707 case Magic('='):
1708 EMIT(NFA_QUEST);
1709 break;
1710
1711 case Magic('{'):
1712 /* a{2,5} will expand to 'aaa?a?a?'
1713 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1714 * version of '?'
1715 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1716 * parenthesis have the same id
1717 */
1718
1719 greedy = TRUE;
1720 c2 = peekchr();
1721 if (c2 == '-' || c2 == Magic('-'))
1722 {
1723 skipchr();
1724 greedy = FALSE;
1725 }
1726 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001727 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001728
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001729 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1730 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001731 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001732 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001733 if (greedy)
1734 /* \{}, \{0,} */
1735 EMIT(NFA_STAR);
1736 else
1737 /* \{-}, \{-0,} */
1738 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 break;
1740 }
1741
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001742 /* Special case: x{0} or x{-0} */
1743 if (maxval == 0)
1744 {
1745 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001746 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1748 EMIT(NFA_SKIP_CHAR);
1749 return OK;
1750 }
1751
1752 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001753 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001754 /* Save parse state after the repeated atom and the \{} */
1755 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001756
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001757 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1758 for (i = 0; i < maxval; i++)
1759 {
1760 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001761 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001762 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001763 if (nfa_regatom() == FAIL)
1764 return FAIL;
1765 /* after "minval" times, atoms are optional */
1766 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001767 {
1768 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001769 {
1770 if (greedy)
1771 EMIT(NFA_STAR);
1772 else
1773 EMIT(NFA_STAR_NONGREEDY);
1774 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001775 else
1776 EMIT(quest);
1777 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001778 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001779 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001780 if (i + 1 > minval && maxval == MAX_LIMIT)
1781 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001782 }
1783
1784 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001785 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786 curchr = -1;
1787
1788 break;
1789
1790
1791 default:
1792 break;
1793 } /* end switch */
1794
1795 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001796 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001798
1799 return OK;
1800}
1801
1802/*
1803 * Parse one or more pieces, concatenated. It matches a match for the
1804 * first piece, followed by a match for the second piece, etc. Example:
1805 * "f[0-9]b", first matches "f", then a digit and then "b".
1806 *
1807 * concat ::= piece
1808 * or piece piece
1809 * or piece piece piece
1810 * etc.
1811 */
1812 static int
1813nfa_regconcat()
1814{
1815 int cont = TRUE;
1816 int first = TRUE;
1817
1818 while (cont)
1819 {
1820 switch (peekchr())
1821 {
1822 case NUL:
1823 case Magic('|'):
1824 case Magic('&'):
1825 case Magic(')'):
1826 cont = FALSE;
1827 break;
1828
1829 case Magic('Z'):
1830#ifdef FEAT_MBYTE
1831 regflags |= RF_ICOMBINE;
1832#endif
1833 skipchr_keepstart();
1834 break;
1835 case Magic('c'):
1836 regflags |= RF_ICASE;
1837 skipchr_keepstart();
1838 break;
1839 case Magic('C'):
1840 regflags |= RF_NOICASE;
1841 skipchr_keepstart();
1842 break;
1843 case Magic('v'):
1844 reg_magic = MAGIC_ALL;
1845 skipchr_keepstart();
1846 curchr = -1;
1847 break;
1848 case Magic('m'):
1849 reg_magic = MAGIC_ON;
1850 skipchr_keepstart();
1851 curchr = -1;
1852 break;
1853 case Magic('M'):
1854 reg_magic = MAGIC_OFF;
1855 skipchr_keepstart();
1856 curchr = -1;
1857 break;
1858 case Magic('V'):
1859 reg_magic = MAGIC_NONE;
1860 skipchr_keepstart();
1861 curchr = -1;
1862 break;
1863
1864 default:
1865 if (nfa_regpiece() == FAIL)
1866 return FAIL;
1867 if (first == FALSE)
1868 EMIT(NFA_CONCAT);
1869 else
1870 first = FALSE;
1871 break;
1872 }
1873 }
1874
1875 return OK;
1876}
1877
1878/*
1879 * Parse a branch, one or more concats, separated by "\&". It matches the
1880 * last concat, but only if all the preceding concats also match at the same
1881 * position. Examples:
1882 * "foobeep\&..." matches "foo" in "foobeep".
1883 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1884 *
1885 * branch ::= concat
1886 * or concat \& concat
1887 * or concat \& concat \& concat
1888 * etc.
1889 */
1890 static int
1891nfa_regbranch()
1892{
1893 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001894 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895
Bram Moolenaar16299b52013-05-30 18:45:23 +02001896 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897
1898 /* First branch, possibly the only one */
1899 if (nfa_regconcat() == FAIL)
1900 return FAIL;
1901
1902 ch = peekchr();
1903 /* Try next concats */
1904 while (ch == Magic('&'))
1905 {
1906 skipchr();
1907 EMIT(NFA_NOPEN);
1908 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001909 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 if (nfa_regconcat() == FAIL)
1911 return FAIL;
1912 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001913 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914 EMIT(NFA_SKIP_CHAR);
1915 EMIT(NFA_CONCAT);
1916 ch = peekchr();
1917 }
1918
1919 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001920 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001921 EMIT(NFA_SKIP_CHAR);
1922
1923 return OK;
1924}
1925
1926/*
1927 * Parse a pattern, one or more branches, separated by "\|". It matches
1928 * anything that matches one of the branches. Example: "foo\|beep" matches
1929 * "foo" and matches "beep". If more than one branch matches, the first one
1930 * is used.
1931 *
1932 * pattern ::= branch
1933 * or branch \| branch
1934 * or branch \| branch \| branch
1935 * etc.
1936 */
1937 static int
1938nfa_reg(paren)
1939 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1940{
1941 int parno = 0;
1942
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 if (paren == REG_PAREN)
1944 {
1945 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001946 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001947 parno = regnpar++;
1948 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001949#ifdef FEAT_SYN_HL
1950 else if (paren == REG_ZPAREN)
1951 {
1952 /* Make a ZOPEN node. */
1953 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001954 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001955 parno = regnzpar++;
1956 }
1957#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958
1959 if (nfa_regbranch() == FAIL)
1960 return FAIL; /* cascaded error */
1961
1962 while (peekchr() == Magic('|'))
1963 {
1964 skipchr();
1965 if (nfa_regbranch() == FAIL)
1966 return FAIL; /* cascaded error */
1967 EMIT(NFA_OR);
1968 }
1969
1970 /* Check for proper termination. */
1971 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1972 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001973 if (paren == REG_NPAREN)
1974 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1975 else
1976 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1977 }
1978 else if (paren == REG_NOPAREN && peekchr() != NUL)
1979 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001980 if (peekchr() == Magic(')'))
1981 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1982 else
1983 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1984 }
1985 /*
1986 * Here we set the flag allowing back references to this set of
1987 * parentheses.
1988 */
1989 if (paren == REG_PAREN)
1990 {
1991 had_endbrace[parno] = TRUE; /* have seen the close paren */
1992 EMIT(NFA_MOPEN + parno);
1993 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001994#ifdef FEAT_SYN_HL
1995 else if (paren == REG_ZPAREN)
1996 EMIT(NFA_ZOPEN + parno);
1997#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001998
1999 return OK;
2000}
2001
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002#ifdef DEBUG
2003static char_u code[50];
2004
2005 static void
2006nfa_set_code(c)
2007 int c;
2008{
2009 int addnl = FALSE;
2010
2011 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2012 {
2013 addnl = TRUE;
2014 c -= ADD_NL;
2015 }
2016
2017 STRCPY(code, "");
2018 switch (c)
2019 {
2020 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2021 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2022 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2023 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2024 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2025 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2026
Bram Moolenaar5714b802013-05-28 22:03:20 +02002027 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2028 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2029 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2030 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2031 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2032 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2033 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2034 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2035 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002036#ifdef FEAT_SYN_HL
2037 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2038 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2039 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2040 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2041 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2042 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2043 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2044 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2045 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2046#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002047 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2048
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 case NFA_PREV_ATOM_NO_WIDTH:
2050 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002051 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2052 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002053 case NFA_PREV_ATOM_JUST_BEFORE:
2054 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2055 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2056 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002057 case NFA_PREV_ATOM_LIKE_PATTERN:
2058 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2059
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002060 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2061 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002062 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002063 case NFA_START_INVISIBLE_FIRST:
2064 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002065 case NFA_START_INVISIBLE_NEG:
2066 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002067 case NFA_START_INVISIBLE_NEG_FIRST:
2068 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002069 case NFA_START_INVISIBLE_BEFORE:
2070 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002071 case NFA_START_INVISIBLE_BEFORE_FIRST:
2072 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002073 case NFA_START_INVISIBLE_BEFORE_NEG:
2074 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002075 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2076 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002077 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002078 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002079 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002080 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2083 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002084 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002086 case NFA_MOPEN:
2087 case NFA_MOPEN1:
2088 case NFA_MOPEN2:
2089 case NFA_MOPEN3:
2090 case NFA_MOPEN4:
2091 case NFA_MOPEN5:
2092 case NFA_MOPEN6:
2093 case NFA_MOPEN7:
2094 case NFA_MOPEN8:
2095 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002096 STRCPY(code, "NFA_MOPEN(x)");
2097 code[10] = c - NFA_MOPEN + '0';
2098 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002099 case NFA_MCLOSE:
2100 case NFA_MCLOSE1:
2101 case NFA_MCLOSE2:
2102 case NFA_MCLOSE3:
2103 case NFA_MCLOSE4:
2104 case NFA_MCLOSE5:
2105 case NFA_MCLOSE6:
2106 case NFA_MCLOSE7:
2107 case NFA_MCLOSE8:
2108 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002109 STRCPY(code, "NFA_MCLOSE(x)");
2110 code[11] = c - NFA_MCLOSE + '0';
2111 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002112#ifdef FEAT_SYN_HL
2113 case NFA_ZOPEN:
2114 case NFA_ZOPEN1:
2115 case NFA_ZOPEN2:
2116 case NFA_ZOPEN3:
2117 case NFA_ZOPEN4:
2118 case NFA_ZOPEN5:
2119 case NFA_ZOPEN6:
2120 case NFA_ZOPEN7:
2121 case NFA_ZOPEN8:
2122 case NFA_ZOPEN9:
2123 STRCPY(code, "NFA_ZOPEN(x)");
2124 code[10] = c - NFA_ZOPEN + '0';
2125 break;
2126 case NFA_ZCLOSE:
2127 case NFA_ZCLOSE1:
2128 case NFA_ZCLOSE2:
2129 case NFA_ZCLOSE3:
2130 case NFA_ZCLOSE4:
2131 case NFA_ZCLOSE5:
2132 case NFA_ZCLOSE6:
2133 case NFA_ZCLOSE7:
2134 case NFA_ZCLOSE8:
2135 case NFA_ZCLOSE9:
2136 STRCPY(code, "NFA_ZCLOSE(x)");
2137 code[11] = c - NFA_ZCLOSE + '0';
2138 break;
2139#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002140 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2141 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2142 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2143 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002144 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2145 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002146 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2147 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2148 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2149 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2150 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2151 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2152 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2153 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2154 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2155 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2156 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2157 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2158 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2159 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2160
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002161 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002162 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2163 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2164 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2166 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002167
2168 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2169 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2170 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2171 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2172 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2173 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2174 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2175
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002176 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2177 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2178 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2179 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2180 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2181 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2182 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2183 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2184 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2185 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2186 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2187 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2188 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2189 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2190 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2191 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2192
2193 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2194 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2195 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2196 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2197 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2198 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2199 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2200 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2201 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2202 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2203 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2204 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2205 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2206 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2207 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2208 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2209 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2210 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2211 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2212 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2213 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2214 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2215 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2216 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2217 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2218 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2219 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2220
2221 default:
2222 STRCPY(code, "CHAR(x)");
2223 code[5] = c;
2224 }
2225
2226 if (addnl == TRUE)
2227 STRCAT(code, " + NEWLINE ");
2228
2229}
2230
2231#ifdef ENABLE_LOG
2232static FILE *log_fd;
2233
2234/*
2235 * Print the postfix notation of the current regexp.
2236 */
2237 static void
2238nfa_postfix_dump(expr, retval)
2239 char_u *expr;
2240 int retval;
2241{
2242 int *p;
2243 FILE *f;
2244
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002245 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002246 if (f != NULL)
2247 {
2248 fprintf(f, "\n-------------------------\n");
2249 if (retval == FAIL)
2250 fprintf(f, ">>> NFA engine failed ... \n");
2251 else if (retval == OK)
2252 fprintf(f, ">>> NFA engine succeeded !\n");
2253 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002254 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002255 {
2256 nfa_set_code(*p);
2257 fprintf(f, "%s, ", code);
2258 }
2259 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002260 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261 fprintf(f, "%d ", *p);
2262 fprintf(f, "\n\n");
2263 fclose(f);
2264 }
2265}
2266
2267/*
2268 * Print the NFA starting with a root node "state".
2269 */
2270 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002271nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002272 FILE *debugf;
2273 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002275 garray_T indent;
2276
2277 ga_init2(&indent, 1, 64);
2278 ga_append(&indent, '\0');
2279 nfa_print_state2(debugf, state, &indent);
2280 ga_clear(&indent);
2281}
2282
2283 static void
2284nfa_print_state2(debugf, state, indent)
2285 FILE *debugf;
2286 nfa_state_T *state;
2287 garray_T *indent;
2288{
2289 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002290
2291 if (state == NULL)
2292 return;
2293
2294 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002295
2296 /* Output indent */
2297 p = (char_u *)indent->ga_data;
2298 if (indent->ga_len >= 3)
2299 {
2300 int last = indent->ga_len - 3;
2301 char_u save[2];
2302
2303 STRNCPY(save, &p[last], 2);
2304 STRNCPY(&p[last], "+-", 2);
2305 fprintf(debugf, " %s", p);
2306 STRNCPY(&p[last], save, 2);
2307 }
2308 else
2309 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002310
2311 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002312 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002313 code,
2314 state->c,
2315 abs(state->id),
2316 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317 if (state->id < 0)
2318 return;
2319
2320 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002321
2322 /* grow indent for state->out */
2323 indent->ga_len -= 1;
2324 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002325 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002326 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002327 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002328 ga_append(indent, '\0');
2329
2330 nfa_print_state2(debugf, state->out, indent);
2331
2332 /* replace last part of indent for state->out1 */
2333 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002334 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002335 ga_append(indent, '\0');
2336
2337 nfa_print_state2(debugf, state->out1, indent);
2338
2339 /* shrink indent */
2340 indent->ga_len -= 3;
2341 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002342}
2343
2344/*
2345 * Print the NFA state machine.
2346 */
2347 static void
2348nfa_dump(prog)
2349 nfa_regprog_T *prog;
2350{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002351 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352
2353 if (debugf != NULL)
2354 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002355 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002356
Bram Moolenaar473de612013-06-08 18:19:48 +02002357 if (prog->reganch)
2358 fprintf(debugf, "reganch: %d\n", prog->reganch);
2359 if (prog->regstart != NUL)
2360 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2361 prog->regstart, prog->regstart);
2362 if (prog->match_text != NULL)
2363 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002364
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365 fclose(debugf);
2366 }
2367}
2368#endif /* ENABLE_LOG */
2369#endif /* DEBUG */
2370
2371/*
2372 * Parse r.e. @expr and convert it into postfix form.
2373 * Return the postfix string on success, NULL otherwise.
2374 */
2375 static int *
2376re2post()
2377{
2378 if (nfa_reg(REG_NOPAREN) == FAIL)
2379 return NULL;
2380 EMIT(NFA_MOPEN);
2381 return post_start;
2382}
2383
2384/* NB. Some of the code below is inspired by Russ's. */
2385
2386/*
2387 * Represents an NFA state plus zero or one or two arrows exiting.
2388 * if c == MATCH, no arrows out; matching state.
2389 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2390 * If c < 256, labeled arrow with character c to out.
2391 */
2392
2393static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2394
2395/*
2396 * Allocate and initialize nfa_state_T.
2397 */
2398 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002399alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002400 int c;
2401 nfa_state_T *out;
2402 nfa_state_T *out1;
2403{
2404 nfa_state_T *s;
2405
2406 if (istate >= nstate)
2407 return NULL;
2408
2409 s = &state_ptr[istate++];
2410
2411 s->c = c;
2412 s->out = out;
2413 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002414 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415
2416 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002417 s->lastlist[0] = 0;
2418 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002419
2420 return s;
2421}
2422
2423/*
2424 * A partially built NFA without the matching state filled in.
2425 * Frag_T.start points at the start state.
2426 * Frag_T.out is a list of places that need to be set to the
2427 * next state for this fragment.
2428 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002429
2430/* Since the out pointers in the list are always
2431 * uninitialized, we use the pointers themselves
2432 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002434union Ptrlist
2435{
2436 Ptrlist *next;
2437 nfa_state_T *s;
2438};
2439
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440struct Frag
2441{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002442 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 Ptrlist *out;
2444};
2445typedef struct Frag Frag_T;
2446
2447static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2448static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2449static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2450static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2451static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2452static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2453
2454/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002455 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002456 */
2457 static Frag_T
2458frag(start, out)
2459 nfa_state_T *start;
2460 Ptrlist *out;
2461{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002462 Frag_T n;
2463
2464 n.start = start;
2465 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466 return n;
2467}
2468
2469/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002470 * Create singleton list containing just outp.
2471 */
2472 static Ptrlist *
2473list1(outp)
2474 nfa_state_T **outp;
2475{
2476 Ptrlist *l;
2477
2478 l = (Ptrlist *)outp;
2479 l->next = NULL;
2480 return l;
2481}
2482
2483/*
2484 * Patch the list of states at out to point to start.
2485 */
2486 static void
2487patch(l, s)
2488 Ptrlist *l;
2489 nfa_state_T *s;
2490{
2491 Ptrlist *next;
2492
2493 for (; l; l = next)
2494 {
2495 next = l->next;
2496 l->s = s;
2497 }
2498}
2499
2500
2501/*
2502 * Join the two lists l1 and l2, returning the combination.
2503 */
2504 static Ptrlist *
2505append(l1, l2)
2506 Ptrlist *l1;
2507 Ptrlist *l2;
2508{
2509 Ptrlist *oldl1;
2510
2511 oldl1 = l1;
2512 while (l1->next)
2513 l1 = l1->next;
2514 l1->next = l2;
2515 return oldl1;
2516}
2517
2518/*
2519 * Stack used for transforming postfix form into NFA.
2520 */
2521static Frag_T empty;
2522
2523 static void
2524st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002525 int *postfix UNUSED;
2526 int *end UNUSED;
2527 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002528{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002529#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002530 FILE *df;
2531 int *p2;
2532
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002533 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002534 if (df)
2535 {
2536 fprintf(df, "Error popping the stack!\n");
2537#ifdef DEBUG
2538 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2539#endif
2540 fprintf(df, "Postfix form is: ");
2541#ifdef DEBUG
2542 for (p2 = postfix; p2 < end; p2++)
2543 {
2544 nfa_set_code(*p2);
2545 fprintf(df, "%s, ", code);
2546 }
2547 nfa_set_code(*p);
2548 fprintf(df, "\nCurrent position is: ");
2549 for (p2 = postfix; p2 <= p; p2 ++)
2550 {
2551 nfa_set_code(*p2);
2552 fprintf(df, "%s, ", code);
2553 }
2554#else
2555 for (p2 = postfix; p2 < end; p2++)
2556 {
2557 fprintf(df, "%d, ", *p2);
2558 }
2559 fprintf(df, "\nCurrent position is: ");
2560 for (p2 = postfix; p2 <= p; p2 ++)
2561 {
2562 fprintf(df, "%d, ", *p2);
2563 }
2564#endif
2565 fprintf(df, "\n--------------------------\n");
2566 fclose(df);
2567 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002568#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002569 EMSG(_("E874: (NFA) Could not pop the stack !"));
2570}
2571
2572/*
2573 * Push an item onto the stack.
2574 */
2575 static void
2576st_push(s, p, stack_end)
2577 Frag_T s;
2578 Frag_T **p;
2579 Frag_T *stack_end;
2580{
2581 Frag_T *stackp = *p;
2582
2583 if (stackp >= stack_end)
2584 return;
2585 *stackp = s;
2586 *p = *p + 1;
2587}
2588
2589/*
2590 * Pop an item from the stack.
2591 */
2592 static Frag_T
2593st_pop(p, stack)
2594 Frag_T **p;
2595 Frag_T *stack;
2596{
2597 Frag_T *stackp;
2598
2599 *p = *p - 1;
2600 stackp = *p;
2601 if (stackp < stack)
2602 return empty;
2603 return **p;
2604}
2605
2606/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002607 * Estimate the maximum byte length of anything matching "state".
2608 * When unknown or unlimited return -1.
2609 */
2610 static int
2611nfa_max_width(startstate, depth)
2612 nfa_state_T *startstate;
2613 int depth;
2614{
2615 int l, r;
2616 nfa_state_T *state = startstate;
2617 int len = 0;
2618
2619 /* detect looping in a NFA_SPLIT */
2620 if (depth > 4)
2621 return -1;
2622
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002623 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002624 {
2625 switch (state->c)
2626 {
2627 case NFA_END_INVISIBLE:
2628 case NFA_END_INVISIBLE_NEG:
2629 /* the end, return what we have */
2630 return len;
2631
2632 case NFA_SPLIT:
2633 /* two alternatives, use the maximum */
2634 l = nfa_max_width(state->out, depth + 1);
2635 r = nfa_max_width(state->out1, depth + 1);
2636 if (l < 0 || r < 0)
2637 return -1;
2638 return len + (l > r ? l : r);
2639
2640 case NFA_ANY:
2641 case NFA_START_COLL:
2642 case NFA_START_NEG_COLL:
2643 /* matches some character, including composing chars */
2644#ifdef FEAT_MBYTE
2645 if (enc_utf8)
2646 len += MB_MAXBYTES;
2647 else if (has_mbyte)
2648 len += 2;
2649 else
2650#endif
2651 ++len;
2652 if (state->c != NFA_ANY)
2653 {
2654 /* skip over the characters */
2655 state = state->out1->out;
2656 continue;
2657 }
2658 break;
2659
2660 case NFA_DIGIT:
2661 case NFA_WHITE:
2662 case NFA_HEX:
2663 case NFA_OCTAL:
2664 /* ascii */
2665 ++len;
2666 break;
2667
2668 case NFA_IDENT:
2669 case NFA_SIDENT:
2670 case NFA_KWORD:
2671 case NFA_SKWORD:
2672 case NFA_FNAME:
2673 case NFA_SFNAME:
2674 case NFA_PRINT:
2675 case NFA_SPRINT:
2676 case NFA_NWHITE:
2677 case NFA_NDIGIT:
2678 case NFA_NHEX:
2679 case NFA_NOCTAL:
2680 case NFA_WORD:
2681 case NFA_NWORD:
2682 case NFA_HEAD:
2683 case NFA_NHEAD:
2684 case NFA_ALPHA:
2685 case NFA_NALPHA:
2686 case NFA_LOWER:
2687 case NFA_NLOWER:
2688 case NFA_UPPER:
2689 case NFA_NUPPER:
2690 /* possibly non-ascii */
2691#ifdef FEAT_MBYTE
2692 if (has_mbyte)
2693 len += 3;
2694 else
2695#endif
2696 ++len;
2697 break;
2698
2699 case NFA_START_INVISIBLE:
2700 case NFA_START_INVISIBLE_NEG:
2701 case NFA_START_INVISIBLE_BEFORE:
2702 case NFA_START_INVISIBLE_BEFORE_NEG:
2703 /* zero-width, out1 points to the END state */
2704 state = state->out1->out;
2705 continue;
2706
2707 case NFA_BACKREF1:
2708 case NFA_BACKREF2:
2709 case NFA_BACKREF3:
2710 case NFA_BACKREF4:
2711 case NFA_BACKREF5:
2712 case NFA_BACKREF6:
2713 case NFA_BACKREF7:
2714 case NFA_BACKREF8:
2715 case NFA_BACKREF9:
2716#ifdef FEAT_SYN_HL
2717 case NFA_ZREF1:
2718 case NFA_ZREF2:
2719 case NFA_ZREF3:
2720 case NFA_ZREF4:
2721 case NFA_ZREF5:
2722 case NFA_ZREF6:
2723 case NFA_ZREF7:
2724 case NFA_ZREF8:
2725 case NFA_ZREF9:
2726#endif
2727 case NFA_NEWL:
2728 case NFA_SKIP:
2729 /* unknown width */
2730 return -1;
2731
2732 case NFA_BOL:
2733 case NFA_EOL:
2734 case NFA_BOF:
2735 case NFA_EOF:
2736 case NFA_BOW:
2737 case NFA_EOW:
2738 case NFA_MOPEN:
2739 case NFA_MOPEN1:
2740 case NFA_MOPEN2:
2741 case NFA_MOPEN3:
2742 case NFA_MOPEN4:
2743 case NFA_MOPEN5:
2744 case NFA_MOPEN6:
2745 case NFA_MOPEN7:
2746 case NFA_MOPEN8:
2747 case NFA_MOPEN9:
2748#ifdef FEAT_SYN_HL
2749 case NFA_ZOPEN:
2750 case NFA_ZOPEN1:
2751 case NFA_ZOPEN2:
2752 case NFA_ZOPEN3:
2753 case NFA_ZOPEN4:
2754 case NFA_ZOPEN5:
2755 case NFA_ZOPEN6:
2756 case NFA_ZOPEN7:
2757 case NFA_ZOPEN8:
2758 case NFA_ZOPEN9:
2759 case NFA_ZCLOSE:
2760 case NFA_ZCLOSE1:
2761 case NFA_ZCLOSE2:
2762 case NFA_ZCLOSE3:
2763 case NFA_ZCLOSE4:
2764 case NFA_ZCLOSE5:
2765 case NFA_ZCLOSE6:
2766 case NFA_ZCLOSE7:
2767 case NFA_ZCLOSE8:
2768 case NFA_ZCLOSE9:
2769#endif
2770 case NFA_MCLOSE:
2771 case NFA_MCLOSE1:
2772 case NFA_MCLOSE2:
2773 case NFA_MCLOSE3:
2774 case NFA_MCLOSE4:
2775 case NFA_MCLOSE5:
2776 case NFA_MCLOSE6:
2777 case NFA_MCLOSE7:
2778 case NFA_MCLOSE8:
2779 case NFA_MCLOSE9:
2780 case NFA_NOPEN:
2781 case NFA_NCLOSE:
2782
2783 case NFA_LNUM_GT:
2784 case NFA_LNUM_LT:
2785 case NFA_COL_GT:
2786 case NFA_COL_LT:
2787 case NFA_VCOL_GT:
2788 case NFA_VCOL_LT:
2789 case NFA_MARK_GT:
2790 case NFA_MARK_LT:
2791 case NFA_VISUAL:
2792 case NFA_LNUM:
2793 case NFA_CURSOR:
2794 case NFA_COL:
2795 case NFA_VCOL:
2796 case NFA_MARK:
2797
2798 case NFA_ZSTART:
2799 case NFA_ZEND:
2800 case NFA_OPT_CHARS:
2801 case NFA_SKIP_CHAR:
2802 case NFA_START_PATTERN:
2803 case NFA_END_PATTERN:
2804 case NFA_COMPOSING:
2805 case NFA_END_COMPOSING:
2806 /* zero-width */
2807 break;
2808
2809 default:
2810 if (state->c < 0)
2811 /* don't know what this is */
2812 return -1;
2813 /* normal character */
2814 len += MB_CHAR2LEN(state->c);
2815 break;
2816 }
2817
2818 /* normal way to continue */
2819 state = state->out;
2820 }
2821
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002822 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002823 return -1;
2824}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02002825
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002826/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827 * Convert a postfix form into its equivalent NFA.
2828 * Return the NFA start state on success, NULL otherwise.
2829 */
2830 static nfa_state_T *
2831post2nfa(postfix, end, nfa_calc_size)
2832 int *postfix;
2833 int *end;
2834 int nfa_calc_size;
2835{
2836 int *p;
2837 int mopen;
2838 int mclose;
2839 Frag_T *stack = NULL;
2840 Frag_T *stackp = NULL;
2841 Frag_T *stack_end = NULL;
2842 Frag_T e1;
2843 Frag_T e2;
2844 Frag_T e;
2845 nfa_state_T *s;
2846 nfa_state_T *s1;
2847 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002848 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849
2850 if (postfix == NULL)
2851 return NULL;
2852
Bram Moolenaar053bb602013-05-20 13:55:21 +02002853#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854#define POP() st_pop(&stackp, stack); \
2855 if (stackp < stack) \
2856 { \
2857 st_error(postfix, end, p); \
2858 return NULL; \
2859 }
2860
2861 if (nfa_calc_size == FALSE)
2862 {
2863 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002864 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002865 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002866 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002867 }
2868
2869 for (p = postfix; p < end; ++p)
2870 {
2871 switch (*p)
2872 {
2873 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002874 /* Concatenation.
2875 * Pay attention: this operator does not exist in the r.e. itself
2876 * (it is implicit, really). It is added when r.e. is translated
2877 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002878 if (nfa_calc_size == TRUE)
2879 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002880 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881 break;
2882 }
2883 e2 = POP();
2884 e1 = POP();
2885 patch(e1.out, e2.start);
2886 PUSH(frag(e1.start, e2.out));
2887 break;
2888
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002889 case NFA_OR:
2890 /* Alternation */
2891 if (nfa_calc_size == TRUE)
2892 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002893 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002894 break;
2895 }
2896 e2 = POP();
2897 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002898 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002899 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002900 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 PUSH(frag(s, append(e1.out, e2.out)));
2902 break;
2903
2904 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002905 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002906 if (nfa_calc_size == TRUE)
2907 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002908 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002909 break;
2910 }
2911 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002912 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002913 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002914 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002915 patch(e.out, s);
2916 PUSH(frag(s, list1(&s->out1)));
2917 break;
2918
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002919 case NFA_STAR_NONGREEDY:
2920 /* Zero or more, prefer zero */
2921 if (nfa_calc_size == TRUE)
2922 {
2923 nstate++;
2924 break;
2925 }
2926 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002927 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002928 if (s == NULL)
2929 goto theend;
2930 patch(e.out, s);
2931 PUSH(frag(s, list1(&s->out)));
2932 break;
2933
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 case NFA_QUEST:
2935 /* one or zero atoms=> greedy match */
2936 if (nfa_calc_size == TRUE)
2937 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002938 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002939 break;
2940 }
2941 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002942 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002943 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002944 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945 PUSH(frag(s, append(e.out, list1(&s->out1))));
2946 break;
2947
2948 case NFA_QUEST_NONGREEDY:
2949 /* zero or one atoms => non-greedy match */
2950 if (nfa_calc_size == TRUE)
2951 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002952 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953 break;
2954 }
2955 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002956 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002957 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002958 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002959 PUSH(frag(s, append(e.out, list1(&s->out))));
2960 break;
2961
Bram Moolenaar417bad22013-06-07 14:08:30 +02002962 case NFA_END_COLL:
2963 case NFA_END_NEG_COLL:
2964 /* On the stack is the sequence starting with NFA_START_COLL or
2965 * NFA_START_NEG_COLL and all possible characters. Patch it to
2966 * add the output to the start. */
2967 if (nfa_calc_size == TRUE)
2968 {
2969 nstate++;
2970 break;
2971 }
2972 e = POP();
2973 s = alloc_state(NFA_END_COLL, NULL, NULL);
2974 if (s == NULL)
2975 goto theend;
2976 patch(e.out, s);
2977 e.start->out1 = s;
2978 PUSH(frag(e.start, list1(&s->out)));
2979 break;
2980
2981 case NFA_RANGE:
2982 /* Before this are two characters, the low and high end of a
2983 * range. Turn them into two states with MIN and MAX. */
2984 if (nfa_calc_size == TRUE)
2985 {
2986 /* nstate += 0; */
2987 break;
2988 }
2989 e2 = POP();
2990 e1 = POP();
2991 e2.start->val = e2.start->c;
2992 e2.start->c = NFA_RANGE_MAX;
2993 e1.start->val = e1.start->c;
2994 e1.start->c = NFA_RANGE_MIN;
2995 patch(e1.out, e2.start);
2996 PUSH(frag(e1.start, e2.out));
2997 break;
2998
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002999 case NFA_SKIP_CHAR:
3000 /* Symbol of 0-length, Used in a repetition
3001 * with max/min count of 0 */
3002 if (nfa_calc_size == TRUE)
3003 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003004 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003005 break;
3006 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003007 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003008 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003009 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003010 PUSH(frag(s, list1(&s->out)));
3011 break;
3012
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003013 case NFA_OPT_CHARS:
3014 {
3015 int n;
3016
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003017 /* \%[abc] implemented as:
3018 * NFA_SPLIT
3019 * +-CHAR(a)
3020 * | +-NFA_SPLIT
3021 * | +-CHAR(b)
3022 * | | +-NFA_SPLIT
3023 * | | +-CHAR(c)
3024 * | | | +-next
3025 * | | +- next
3026 * | +- next
3027 * +- next
3028 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003029 n = *++p; /* get number of characters */
3030 if (nfa_calc_size == TRUE)
3031 {
3032 nstate += n;
3033 break;
3034 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003035 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003036 e1.out = NULL; /* stores list with out1's */
3037 s1 = NULL; /* previous NFA_SPLIT to connect to */
3038 while (n-- > 0)
3039 {
3040 e = POP(); /* get character */
3041 s = alloc_state(NFA_SPLIT, e.start, NULL);
3042 if (s == NULL)
3043 goto theend;
3044 if (e1.out == NULL)
3045 e1 = e;
3046 patch(e.out, s1);
3047 append(e1.out, list1(&s->out1));
3048 s1 = s;
3049 }
3050 PUSH(frag(s, e1.out));
3051 break;
3052 }
3053
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003054 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003055 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003056 case NFA_PREV_ATOM_JUST_BEFORE:
3057 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003058 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003059 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003060 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3061 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003062 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003063 int start_state;
3064 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003065 int n = 0;
3066 nfa_state_T *zend;
3067 nfa_state_T *skip;
3068
Bram Moolenaardecd9542013-06-07 16:31:50 +02003069 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003070 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003071 case NFA_PREV_ATOM_NO_WIDTH:
3072 start_state = NFA_START_INVISIBLE;
3073 end_state = NFA_END_INVISIBLE;
3074 break;
3075 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3076 start_state = NFA_START_INVISIBLE_NEG;
3077 end_state = NFA_END_INVISIBLE_NEG;
3078 break;
3079 case NFA_PREV_ATOM_JUST_BEFORE:
3080 start_state = NFA_START_INVISIBLE_BEFORE;
3081 end_state = NFA_END_INVISIBLE;
3082 break;
3083 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3084 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3085 end_state = NFA_END_INVISIBLE_NEG;
3086 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003087 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003088 start_state = NFA_START_PATTERN;
3089 end_state = NFA_END_PATTERN;
3090 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003091 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003092
3093 if (before)
3094 n = *++p; /* get the count */
3095
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003096 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003097 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003098 * The \@<= operator: match for the preceding atom.
3099 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003100 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003101 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003102
3103 if (nfa_calc_size == TRUE)
3104 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003105 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003106 break;
3107 }
3108 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003109 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003110 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003111 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003112
Bram Moolenaar87953742013-06-05 18:52:40 +02003113 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003115 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003116 if (pattern)
3117 {
3118 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3119 skip = alloc_state(NFA_SKIP, NULL, NULL);
3120 zend = alloc_state(NFA_ZEND, s1, NULL);
3121 s1->out= skip;
3122 patch(e.out, zend);
3123 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003124 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003125 else
3126 {
3127 patch(e.out, s1);
3128 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003129 if (before)
3130 {
3131 if (n <= 0)
3132 /* See if we can guess the maximum width, it avoids a
3133 * lot of pointless tries. */
3134 n = nfa_max_width(e.start, 0);
3135 s->val = n; /* store the count */
3136 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003137 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003138 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003139 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003140
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003141#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003142 case NFA_COMPOSING: /* char with composing char */
3143#if 0
3144 /* TODO */
3145 if (regflags & RF_ICOMBINE)
3146 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003147 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003148 }
3149#endif
3150 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003151#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003152
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003153 case NFA_MOPEN: /* \( \) Submatch */
3154 case NFA_MOPEN1:
3155 case NFA_MOPEN2:
3156 case NFA_MOPEN3:
3157 case NFA_MOPEN4:
3158 case NFA_MOPEN5:
3159 case NFA_MOPEN6:
3160 case NFA_MOPEN7:
3161 case NFA_MOPEN8:
3162 case NFA_MOPEN9:
3163#ifdef FEAT_SYN_HL
3164 case NFA_ZOPEN: /* \z( \) Submatch */
3165 case NFA_ZOPEN1:
3166 case NFA_ZOPEN2:
3167 case NFA_ZOPEN3:
3168 case NFA_ZOPEN4:
3169 case NFA_ZOPEN5:
3170 case NFA_ZOPEN6:
3171 case NFA_ZOPEN7:
3172 case NFA_ZOPEN8:
3173 case NFA_ZOPEN9:
3174#endif
3175 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003176 if (nfa_calc_size == TRUE)
3177 {
3178 nstate += 2;
3179 break;
3180 }
3181
3182 mopen = *p;
3183 switch (*p)
3184 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003185 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3186#ifdef FEAT_SYN_HL
3187 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3188 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3189 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3190 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3191 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3192 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3193 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3194 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3195 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3196 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3197#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003198#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003199 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003200#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003201 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003202 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203 mclose = *p + NSUBEXP;
3204 break;
3205 }
3206
3207 /* Allow "NFA_MOPEN" as a valid postfix representation for
3208 * the empty regexp "". In this case, the NFA will be
3209 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3210 * empty groups of parenthesis, and empty mbyte chars */
3211 if (stackp == stack)
3212 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003213 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003214 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003215 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003216 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003218 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219 patch(list1(&s->out), s1);
3220 PUSH(frag(s, list1(&s1->out)));
3221 break;
3222 }
3223
3224 /* At least one node was emitted before NFA_MOPEN, so
3225 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3226 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003227 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003228 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003229 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003230
Bram Moolenaar525666f2013-06-02 16:40:55 +02003231 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003233 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 patch(e.out, s1);
3235
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003236#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003237 if (mopen == NFA_COMPOSING)
3238 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003240#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003241
3242 PUSH(frag(s, list1(&s1->out)));
3243 break;
3244
Bram Moolenaar5714b802013-05-28 22:03:20 +02003245 case NFA_BACKREF1:
3246 case NFA_BACKREF2:
3247 case NFA_BACKREF3:
3248 case NFA_BACKREF4:
3249 case NFA_BACKREF5:
3250 case NFA_BACKREF6:
3251 case NFA_BACKREF7:
3252 case NFA_BACKREF8:
3253 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003254#ifdef FEAT_SYN_HL
3255 case NFA_ZREF1:
3256 case NFA_ZREF2:
3257 case NFA_ZREF3:
3258 case NFA_ZREF4:
3259 case NFA_ZREF5:
3260 case NFA_ZREF6:
3261 case NFA_ZREF7:
3262 case NFA_ZREF8:
3263 case NFA_ZREF9:
3264#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003265 if (nfa_calc_size == TRUE)
3266 {
3267 nstate += 2;
3268 break;
3269 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003270 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003271 if (s == NULL)
3272 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003273 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003274 if (s1 == NULL)
3275 goto theend;
3276 patch(list1(&s->out), s1);
3277 PUSH(frag(s, list1(&s1->out)));
3278 break;
3279
Bram Moolenaar423532e2013-05-29 21:14:42 +02003280 case NFA_LNUM:
3281 case NFA_LNUM_GT:
3282 case NFA_LNUM_LT:
3283 case NFA_VCOL:
3284 case NFA_VCOL_GT:
3285 case NFA_VCOL_LT:
3286 case NFA_COL:
3287 case NFA_COL_GT:
3288 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003289 case NFA_MARK:
3290 case NFA_MARK_GT:
3291 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003292 {
3293 int n = *++p; /* lnum, col or mark name */
3294
Bram Moolenaar423532e2013-05-29 21:14:42 +02003295 if (nfa_calc_size == TRUE)
3296 {
3297 nstate += 1;
3298 break;
3299 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003300 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003301 if (s == NULL)
3302 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003303 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003304 PUSH(frag(s, list1(&s->out)));
3305 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003306 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003307
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 case NFA_ZSTART:
3309 case NFA_ZEND:
3310 default:
3311 /* Operands */
3312 if (nfa_calc_size == TRUE)
3313 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003314 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003315 break;
3316 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003317 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003319 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 PUSH(frag(s, list1(&s->out)));
3321 break;
3322
3323 } /* switch(*p) */
3324
3325 } /* for(p = postfix; *p; ++p) */
3326
3327 if (nfa_calc_size == TRUE)
3328 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003329 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003330 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003331 }
3332
3333 e = POP();
3334 if (stackp != stack)
3335 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3336
3337 if (istate >= nstate)
3338 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3339
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003340 matchstate = &state_ptr[istate++]; /* the match state */
3341 matchstate->c = NFA_MATCH;
3342 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003343 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003344
3345 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003346 ret = e.start;
3347
3348theend:
3349 vim_free(stack);
3350 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003351
3352#undef POP1
3353#undef PUSH1
3354#undef POP2
3355#undef PUSH2
3356#undef POP
3357#undef PUSH
3358}
3359
Bram Moolenaara2947e22013-06-11 22:44:09 +02003360/*
3361 * After building the NFA program, inspect it to add optimization hints.
3362 */
3363 static void
3364nfa_postprocess(prog)
3365 nfa_regprog_T *prog;
3366{
3367 int i;
3368 int c;
3369
3370 for (i = 0; i < prog->nstate; ++i)
3371 {
3372 c = prog->state[i].c;
3373 if (c == NFA_START_INVISIBLE
3374 || c == NFA_START_INVISIBLE_NEG
3375 || c == NFA_START_INVISIBLE_BEFORE
3376 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3377 {
3378 int directly;
3379
3380 /* Do it directly when what follows is possibly the end of the
3381 * match. */
3382 if (match_follows(prog->state[i].out1->out, 0))
3383 directly = TRUE;
3384 else
3385 {
3386 int ch_invisible = failure_chance(prog->state[i].out, 0);
3387 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3388
3389 /* Postpone when the invisible match is expensive or has a
3390 * lower chance of failing. */
3391 if (c == NFA_START_INVISIBLE_BEFORE
3392 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3393 {
3394 /* "before" matches are very expensive when
3395 * unbounded, always prefer what follows then,
3396 * unless what follows will always match.
3397 * Otherwise strongly prefer what follows. */
3398 if (prog->state[i].val <= 0 && ch_follows > 0)
3399 directly = FALSE;
3400 else
3401 directly = ch_follows * 10 < ch_invisible;
3402 }
3403 else
3404 {
3405 /* normal invisible, first do the one with the
3406 * highest failure chance */
3407 directly = ch_follows < ch_invisible;
3408 }
3409 }
3410 if (directly)
3411 /* switch to the _FIRST state */
3412 ++prog->state[i].c;
3413 }
3414 }
3415}
3416
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417/****************************************************************
3418 * NFA execution code.
3419 ****************************************************************/
3420
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003421typedef struct
3422{
3423 int in_use; /* number of subexpr with useful info */
3424
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003425 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003426 union
3427 {
3428 struct multipos
3429 {
3430 lpos_T start;
3431 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003432 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003433 struct linepos
3434 {
3435 char_u *start;
3436 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003437 } line[NSUBEXP];
3438 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003439} regsub_T;
3440
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003441typedef struct
3442{
3443 regsub_T norm; /* \( .. \) matches */
3444#ifdef FEAT_SYN_HL
3445 regsub_T synt; /* \z( .. \) matches */
3446#endif
3447} regsubs_T;
3448
Bram Moolenaara2d95102013-06-04 14:23:05 +02003449/* nfa_pim_T stores a Postponed Invisible Match. */
3450typedef struct nfa_pim_S nfa_pim_T;
3451struct nfa_pim_S
3452{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003453 int result; /* NFA_PIM_*, see below */
3454 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003455 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003456 union
3457 {
3458 lpos_T pos;
3459 char_u *ptr;
3460 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003461};
3462
3463/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003464#define NFA_PIM_UNUSED 0 /* pim not used */
3465#define NFA_PIM_TODO 1 /* pim not done yet */
3466#define NFA_PIM_MATCH 2 /* pim executed, matches */
3467#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003468
3469
Bram Moolenaar963fee22013-05-26 21:47:28 +02003470/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003471typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003472{
3473 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003474 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003475 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3476 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003477 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003478} nfa_thread_T;
3479
Bram Moolenaar963fee22013-05-26 21:47:28 +02003480/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003481typedef struct
3482{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003483 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003484 int n; /* nr of states currently in "t" */
3485 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003486 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003487 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003488} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489
Bram Moolenaar5714b802013-05-28 22:03:20 +02003490#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003491static void log_subsexpr __ARGS((regsubs_T *subs));
3492static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003493static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003494
3495 static void
3496log_subsexpr(subs)
3497 regsubs_T *subs;
3498{
3499 log_subexpr(&subs->norm);
3500# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003501 if (nfa_has_zsubexpr)
3502 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003503# endif
3504}
3505
Bram Moolenaar5714b802013-05-28 22:03:20 +02003506 static void
3507log_subexpr(sub)
3508 regsub_T *sub;
3509{
3510 int j;
3511
3512 for (j = 0; j < sub->in_use; j++)
3513 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003514 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003515 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003516 sub->list.multi[j].start.col,
3517 (int)sub->list.multi[j].start.lnum,
3518 sub->list.multi[j].end.col,
3519 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003520 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003521 {
3522 char *s = (char *)sub->list.line[j].start;
3523 char *e = (char *)sub->list.line[j].end;
3524
Bram Moolenaar87953742013-06-05 18:52:40 +02003525 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003526 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003527 s == NULL ? "NULL" : s,
3528 e == NULL ? "NULL" : e);
3529 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003530}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003531
3532 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003533pim_info(pim)
3534 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003535{
3536 static char buf[30];
3537
3538 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3539 buf[0] = NUL;
3540 else
3541 {
3542 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3543 : (int)(pim->end.ptr - reginput));
3544 }
3545 return buf;
3546}
3547
Bram Moolenaar5714b802013-05-28 22:03:20 +02003548#endif
3549
Bram Moolenaar963fee22013-05-26 21:47:28 +02003550/* Used during execution: whether a match has been found. */
3551static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003552
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003553static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003554static void clear_sub __ARGS((regsub_T *sub));
3555static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3556static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003557static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003558static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003559static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3560static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003561static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003562static regsubs_T *addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003563static 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 +02003564
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003565/*
3566 * Copy postponed invisible match info from "from" to "to".
3567 */
3568 static void
3569copy_pim(to, from)
3570 nfa_pim_T *to;
3571 nfa_pim_T *from;
3572{
3573 to->result = from->result;
3574 to->state = from->state;
3575 copy_sub(&to->subs.norm, &from->subs.norm);
3576#ifdef FEAT_SYN_HL
3577 if (nfa_has_zsubexpr)
3578 copy_sub(&to->subs.synt, &from->subs.synt);
3579#endif
3580 to->end = from->end;
3581}
3582
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003583 static void
3584clear_sub(sub)
3585 regsub_T *sub;
3586{
3587 if (REG_MULTI)
3588 /* Use 0xff to set lnum to -1 */
3589 vim_memset(sub->list.multi, 0xff,
3590 sizeof(struct multipos) * nfa_nsubexpr);
3591 else
3592 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3593 sub->in_use = 0;
3594}
3595
3596/*
3597 * Copy the submatches from "from" to "to".
3598 */
3599 static void
3600copy_sub(to, from)
3601 regsub_T *to;
3602 regsub_T *from;
3603{
3604 to->in_use = from->in_use;
3605 if (from->in_use > 0)
3606 {
3607 /* Copy the match start and end positions. */
3608 if (REG_MULTI)
3609 mch_memmove(&to->list.multi[0],
3610 &from->list.multi[0],
3611 sizeof(struct multipos) * from->in_use);
3612 else
3613 mch_memmove(&to->list.line[0],
3614 &from->list.line[0],
3615 sizeof(struct linepos) * from->in_use);
3616 }
3617}
3618
3619/*
3620 * Like copy_sub() but exclude the main match.
3621 */
3622 static void
3623copy_sub_off(to, from)
3624 regsub_T *to;
3625 regsub_T *from;
3626{
3627 if (to->in_use < from->in_use)
3628 to->in_use = from->in_use;
3629 if (from->in_use > 1)
3630 {
3631 /* Copy the match start and end positions. */
3632 if (REG_MULTI)
3633 mch_memmove(&to->list.multi[1],
3634 &from->list.multi[1],
3635 sizeof(struct multipos) * (from->in_use - 1));
3636 else
3637 mch_memmove(&to->list.line[1],
3638 &from->list.line[1],
3639 sizeof(struct linepos) * (from->in_use - 1));
3640 }
3641}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642
Bram Moolenaar428e9872013-05-30 17:05:39 +02003643/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003644 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003645 */
3646 static int
3647sub_equal(sub1, sub2)
3648 regsub_T *sub1;
3649 regsub_T *sub2;
3650{
3651 int i;
3652 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003653 linenr_T s1;
3654 linenr_T s2;
3655 char_u *sp1;
3656 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003657
3658 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3659 if (REG_MULTI)
3660 {
3661 for (i = 0; i < todo; ++i)
3662 {
3663 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003664 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003665 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003666 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003667 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003668 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003669 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003670 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003671 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003672 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02003673 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02003674 != sub2->list.multi[i].start.col)
3675 return FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003676 }
3677 }
3678 else
3679 {
3680 for (i = 0; i < todo; ++i)
3681 {
3682 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003683 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003684 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003685 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003686 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003687 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003688 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003689 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003690 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003691 return FALSE;
3692 }
3693 }
3694
3695 return TRUE;
3696}
3697
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003698#ifdef ENABLE_LOG
3699 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003700report_state(char *action,
3701 regsub_T *sub,
3702 nfa_state_T *state,
3703 int lid,
3704 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003705{
3706 int col;
3707
3708 if (sub->in_use <= 0)
3709 col = -1;
3710 else if (REG_MULTI)
3711 col = sub->list.multi[0].start.col;
3712 else
3713 col = (int)(sub->list.line[0].start - regline);
3714 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003715 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3716 action, abs(state->id), lid, state->c, code, col,
3717 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003718}
3719#endif
3720
Bram Moolenaar43e02982013-06-07 17:31:29 +02003721/*
3722 * Return TRUE if the same state is already in list "l" with the same
3723 * positions as "subs".
3724 */
3725 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02003726has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02003727 nfa_list_T *l; /* runtime state list */
3728 nfa_state_T *state; /* state to update */
3729 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02003730 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003731{
3732 nfa_thread_T *thread;
3733 int i;
3734
3735 for (i = 0; i < l->n; ++i)
3736 {
3737 thread = &l->t[i];
3738 if (thread->state->id == state->id
3739 && sub_equal(&thread->subs.norm, &subs->norm)
3740#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003741 && (!nfa_has_zsubexpr
3742 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003743#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02003744 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003745 return TRUE;
3746 }
3747 return FALSE;
3748}
3749
3750/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02003751 * Return TRUE if "one" and "two" are equal. That includes when both are not
3752 * set.
3753 */
3754 static int
3755pim_equal(one, two)
3756 nfa_pim_T *one;
3757 nfa_pim_T *two;
3758{
3759 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
3760 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
3761
3762 if (one_unused)
3763 /* one is unused: equal when two is also unused */
3764 return two_unused;
3765 if (two_unused)
3766 /* one is used and two is not: not equal */
3767 return FALSE;
3768 /* compare the position */
3769 if (REG_MULTI)
3770 return one->end.pos.lnum == two->end.pos.lnum
3771 && one->end.pos.col == two->end.pos.col;
3772 return one->end.ptr == two->end.ptr;
3773}
3774
3775/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003776 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3777 */
3778 static int
3779match_follows(startstate, depth)
3780 nfa_state_T *startstate;
3781 int depth;
3782{
3783 nfa_state_T *state = startstate;
3784
3785 /* avoid too much recursion */
3786 if (depth > 10)
3787 return FALSE;
3788
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02003789 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003790 {
3791 switch (state->c)
3792 {
3793 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003794 case NFA_MCLOSE:
3795 case NFA_END_INVISIBLE:
3796 case NFA_END_INVISIBLE_NEG:
3797 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003798 return TRUE;
3799
3800 case NFA_SPLIT:
3801 return match_follows(state->out, depth + 1)
3802 || match_follows(state->out1, depth + 1);
3803
3804 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003805 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003806 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003807 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003808 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003809 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003810 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003811 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003812 case NFA_COMPOSING:
3813 /* skip ahead to next state */
3814 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02003815 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003816
3817 case NFA_ANY:
3818 case NFA_IDENT:
3819 case NFA_SIDENT:
3820 case NFA_KWORD:
3821 case NFA_SKWORD:
3822 case NFA_FNAME:
3823 case NFA_SFNAME:
3824 case NFA_PRINT:
3825 case NFA_SPRINT:
3826 case NFA_WHITE:
3827 case NFA_NWHITE:
3828 case NFA_DIGIT:
3829 case NFA_NDIGIT:
3830 case NFA_HEX:
3831 case NFA_NHEX:
3832 case NFA_OCTAL:
3833 case NFA_NOCTAL:
3834 case NFA_WORD:
3835 case NFA_NWORD:
3836 case NFA_HEAD:
3837 case NFA_NHEAD:
3838 case NFA_ALPHA:
3839 case NFA_NALPHA:
3840 case NFA_LOWER:
3841 case NFA_NLOWER:
3842 case NFA_UPPER:
3843 case NFA_NUPPER:
3844 case NFA_START_COLL:
3845 case NFA_START_NEG_COLL:
3846 case NFA_NEWL:
3847 /* state will advance input */
3848 return FALSE;
3849
3850 default:
3851 if (state->c > 0)
3852 /* state will advance input */
3853 return FALSE;
3854
3855 /* Others: zero-width or possibly zero-width, might still find
3856 * a match at the same position, keep looking. */
3857 break;
3858 }
3859 state = state->out;
3860 }
3861 return FALSE;
3862}
3863
3864
3865/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003866 * Return TRUE if "state" is already in list "l".
3867 */
3868 static int
3869state_in_list(l, state, subs)
3870 nfa_list_T *l; /* runtime state list */
3871 nfa_state_T *state; /* state to update */
3872 regsubs_T *subs; /* pointers to subexpressions */
3873{
3874 if (state->lastlist[nfa_ll_index] == l->id)
3875 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02003876 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003877 return TRUE;
3878 }
3879 return FALSE;
3880}
3881
Bram Moolenaard05bf562013-06-30 23:24:08 +02003882/*
3883 * Add "state" and possibly what follows to state list ".".
3884 * Returns "subs_arg", possibly copied into temp_subs.
3885 */
3886
3887 static regsubs_T *
3888addstate(l, state, subs_arg, pim, off)
3889 nfa_list_T *l; /* runtime state list */
3890 nfa_state_T *state; /* state to update */
3891 regsubs_T *subs_arg; /* pointers to subexpressions */
3892 nfa_pim_T *pim; /* postponed look-behind match */
3893 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003894{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003895 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003896 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003897 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003898 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003899 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003900 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003901 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02003902 regsubs_T *subs = subs_arg;
3903 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003904#ifdef ENABLE_LOG
3905 int did_print = FALSE;
3906#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003907
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003908 switch (state->c)
3909 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910 case NFA_NCLOSE:
3911 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003912 case NFA_MCLOSE1:
3913 case NFA_MCLOSE2:
3914 case NFA_MCLOSE3:
3915 case NFA_MCLOSE4:
3916 case NFA_MCLOSE5:
3917 case NFA_MCLOSE6:
3918 case NFA_MCLOSE7:
3919 case NFA_MCLOSE8:
3920 case NFA_MCLOSE9:
3921#ifdef FEAT_SYN_HL
3922 case NFA_ZCLOSE:
3923 case NFA_ZCLOSE1:
3924 case NFA_ZCLOSE2:
3925 case NFA_ZCLOSE3:
3926 case NFA_ZCLOSE4:
3927 case NFA_ZCLOSE5:
3928 case NFA_ZCLOSE6:
3929 case NFA_ZCLOSE7:
3930 case NFA_ZCLOSE8:
3931 case NFA_ZCLOSE9:
3932#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003933 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003934 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003935 case NFA_SPLIT:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003936 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003937 /* These nodes are not added themselves but their "out" and/or
3938 * "out1" may be added below. */
3939 break;
3940
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003941 case NFA_BOL:
3942 case NFA_BOF:
3943 /* "^" won't match past end-of-line, don't bother trying.
3944 * Except when at the end of the line, or when we are going to the
3945 * next line for a look-behind match. */
3946 if (reginput > regline
3947 && *reginput != NUL
3948 && (nfa_endp == NULL
3949 || !REG_MULTI
3950 || reglnum == nfa_endp->se_u.pos.lnum))
3951 goto skip_add;
3952 /* FALLTHROUGH */
3953
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003954 case NFA_MOPEN1:
3955 case NFA_MOPEN2:
3956 case NFA_MOPEN3:
3957 case NFA_MOPEN4:
3958 case NFA_MOPEN5:
3959 case NFA_MOPEN6:
3960 case NFA_MOPEN7:
3961 case NFA_MOPEN8:
3962 case NFA_MOPEN9:
3963#ifdef FEAT_SYN_HL
3964 case NFA_ZOPEN:
3965 case NFA_ZOPEN1:
3966 case NFA_ZOPEN2:
3967 case NFA_ZOPEN3:
3968 case NFA_ZOPEN4:
3969 case NFA_ZOPEN5:
3970 case NFA_ZOPEN6:
3971 case NFA_ZOPEN7:
3972 case NFA_ZOPEN8:
3973 case NFA_ZOPEN9:
3974#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003975 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003976 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003977 /* These nodes need to be added so that we can bail out when it
3978 * was added to this list before at the same position to avoid an
3979 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02003980
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003981 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003982 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003983 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003984 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02003985 * unless it is an MOPEN that is used for a backreference or
3986 * when there is a PIM. */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003987 if (!nfa_has_backref && pim == NULL && !l->has_pim)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003988 {
3989skip_add:
3990#ifdef ENABLE_LOG
3991 nfa_set_code(state->c);
3992 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3993 abs(state->id), l->id, state->c, code);
3994#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02003995 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003996 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003997
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003998 /* Do not add the state again when it exists with the same
3999 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004000 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004001 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004002 }
4003
Bram Moolenaara0169122013-06-26 18:16:58 +02004004 /* When there are backreferences or PIMs the number of states may
4005 * be (a lot) bigger than anticipated. */
4006 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004007 {
4008 int newlen = l->len * 3 / 2 + 50;
4009
Bram Moolenaard05bf562013-06-30 23:24:08 +02004010 if (subs != &temp_subs)
4011 {
4012 /* "subs" may point into the current array, need to make a
4013 * copy before it becomes invalid. */
4014 copy_sub(&temp_subs.norm, &subs->norm);
4015#ifdef FEAT_SYN_HL
4016 if (nfa_has_zsubexpr)
4017 copy_sub(&temp_subs.synt, &subs->synt);
4018#endif
4019 subs = &temp_subs;
4020 }
4021
Bram Moolenaar428e9872013-05-30 17:05:39 +02004022 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4023 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004024 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004025
4026 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004027 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004028 thread = &l->t[l->n++];
4029 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004030 if (pim == NULL)
4031 thread->pim.result = NFA_PIM_UNUSED;
4032 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004033 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004034 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004035 l->has_pim = TRUE;
4036 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004037 copy_sub(&thread->subs.norm, &subs->norm);
4038#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004039 if (nfa_has_zsubexpr)
4040 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004041#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004042#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004043 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004044 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004045#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046 }
4047
4048#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004049 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004050 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004051#endif
4052 switch (state->c)
4053 {
4054 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004055 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004056 break;
4057
4058 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004059 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004060 subs = addstate(l, state->out, subs, pim, off);
4061 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004062 break;
4063
Bram Moolenaar5714b802013-05-28 22:03:20 +02004064 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004065 case NFA_NOPEN:
4066 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004067 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004068 break;
4069
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004070 case NFA_MOPEN:
4071 case NFA_MOPEN1:
4072 case NFA_MOPEN2:
4073 case NFA_MOPEN3:
4074 case NFA_MOPEN4:
4075 case NFA_MOPEN5:
4076 case NFA_MOPEN6:
4077 case NFA_MOPEN7:
4078 case NFA_MOPEN8:
4079 case NFA_MOPEN9:
4080#ifdef FEAT_SYN_HL
4081 case NFA_ZOPEN:
4082 case NFA_ZOPEN1:
4083 case NFA_ZOPEN2:
4084 case NFA_ZOPEN3:
4085 case NFA_ZOPEN4:
4086 case NFA_ZOPEN5:
4087 case NFA_ZOPEN6:
4088 case NFA_ZOPEN7:
4089 case NFA_ZOPEN8:
4090 case NFA_ZOPEN9:
4091#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004092 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004093 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004094 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004096 sub = &subs->norm;
4097 }
4098#ifdef FEAT_SYN_HL
4099 else if (state->c >= NFA_ZOPEN)
4100 {
4101 subidx = state->c - NFA_ZOPEN;
4102 sub = &subs->synt;
4103 }
4104#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004105 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004106 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004107 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004108 sub = &subs->norm;
4109 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004110
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004111 /* avoid compiler warnings */
4112 save_ptr = NULL;
4113 save_lpos.lnum = 0;
4114 save_lpos.col = 0;
4115
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004116 /* Set the position (with "off" added) in the subexpression. Save
4117 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004118 if (REG_MULTI)
4119 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004120 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004121 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004122 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004123 save_in_use = -1;
4124 }
4125 else
4126 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004127 save_in_use = sub->in_use;
4128 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004129 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004130 sub->list.multi[i].start.lnum = -1;
4131 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004132 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004133 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004134 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004135 if (off == -1)
4136 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004137 sub->list.multi[subidx].start.lnum = reglnum + 1;
4138 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004139 }
4140 else
4141 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004142 sub->list.multi[subidx].start.lnum = reglnum;
4143 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004144 (colnr_T)(reginput - regline + off);
4145 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004146 }
4147 else
4148 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004149 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004150 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004151 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004152 save_in_use = -1;
4153 }
4154 else
4155 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004156 save_in_use = sub->in_use;
4157 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004158 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004159 sub->list.line[i].start = NULL;
4160 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004161 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004162 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004163 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004164 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004165 }
4166
Bram Moolenaard05bf562013-06-30 23:24:08 +02004167 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004168
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004169 if (save_in_use == -1)
4170 {
4171 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004172 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004173 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004174 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004175 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004176 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004177 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004178 break;
4179
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004180 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004181 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004183 /* Do not overwrite the position set by \ze. If no \ze
4184 * encountered end will be set in nfa_regtry(). */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004185 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004186 break;
4187 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004188 case NFA_MCLOSE1:
4189 case NFA_MCLOSE2:
4190 case NFA_MCLOSE3:
4191 case NFA_MCLOSE4:
4192 case NFA_MCLOSE5:
4193 case NFA_MCLOSE6:
4194 case NFA_MCLOSE7:
4195 case NFA_MCLOSE8:
4196 case NFA_MCLOSE9:
4197#ifdef FEAT_SYN_HL
4198 case NFA_ZCLOSE:
4199 case NFA_ZCLOSE1:
4200 case NFA_ZCLOSE2:
4201 case NFA_ZCLOSE3:
4202 case NFA_ZCLOSE4:
4203 case NFA_ZCLOSE5:
4204 case NFA_ZCLOSE6:
4205 case NFA_ZCLOSE7:
4206 case NFA_ZCLOSE8:
4207 case NFA_ZCLOSE9:
4208#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004209 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004210 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004211 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004212 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004213 sub = &subs->norm;
4214 }
4215#ifdef FEAT_SYN_HL
4216 else if (state->c >= NFA_ZCLOSE)
4217 {
4218 subidx = state->c - NFA_ZCLOSE;
4219 sub = &subs->synt;
4220 }
4221#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004222 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004223 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004224 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004225 sub = &subs->norm;
4226 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004227
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004228 /* We don't fill in gaps here, there must have been an MOPEN that
4229 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004230 save_in_use = sub->in_use;
4231 if (sub->in_use <= subidx)
4232 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004233 if (REG_MULTI)
4234 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004235 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004236 if (off == -1)
4237 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004238 sub->list.multi[subidx].end.lnum = reglnum + 1;
4239 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004240 }
4241 else
4242 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004243 sub->list.multi[subidx].end.lnum = reglnum;
4244 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004245 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004246 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004247 /* avoid compiler warnings */
4248 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004249 }
4250 else
4251 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004252 save_ptr = sub->list.line[subidx].end;
4253 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004254 /* avoid compiler warnings */
4255 save_lpos.lnum = 0;
4256 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257 }
4258
Bram Moolenaard05bf562013-06-30 23:24:08 +02004259 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260
4261 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004262 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004263 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004264 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004265 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004266 break;
4267 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004268 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269}
4270
4271/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004272 * Like addstate(), but the new state(s) are put at position "*ip".
4273 * Used for zero-width matches, next state to use is the added one.
4274 * This makes sure the order of states to be tried does not change, which
4275 * matters for alternatives.
4276 */
4277 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004278addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004279 nfa_list_T *l; /* runtime state list */
4280 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004281 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004282 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004283 int *ip;
4284{
4285 int tlen = l->n;
4286 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004287 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004288
4289 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004290 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004291
Bram Moolenaar4b417062013-05-25 20:19:50 +02004292 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004293 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004294 return;
4295
4296 /* re-order to put the new state at the current position */
4297 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004298 if (count == 0)
4299 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004300 if (count == 1)
4301 {
4302 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004303 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004304 }
4305 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004306 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004307 if (l->n + count - 1 >= l->len)
4308 {
4309 /* not enough space to move the new states, reallocate the list
4310 * and move the states to the right position */
4311 nfa_thread_T *newl;
4312
4313 l->len = l->len * 3 / 2 + 50;
4314 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4315 if (newl == NULL)
4316 return;
4317 mch_memmove(&(newl[0]),
4318 &(l->t[0]),
4319 sizeof(nfa_thread_T) * listidx);
4320 mch_memmove(&(newl[listidx]),
4321 &(l->t[l->n - count]),
4322 sizeof(nfa_thread_T) * count);
4323 mch_memmove(&(newl[listidx + count]),
4324 &(l->t[listidx + 1]),
4325 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4326 vim_free(l->t);
4327 l->t = newl;
4328 }
4329 else
4330 {
4331 /* make space for new states, then move them from the
4332 * end to the current position */
4333 mch_memmove(&(l->t[listidx + count]),
4334 &(l->t[listidx + 1]),
4335 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4336 mch_memmove(&(l->t[listidx]),
4337 &(l->t[l->n - 1]),
4338 sizeof(nfa_thread_T) * count);
4339 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004340 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004341 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004342 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004343}
4344
4345/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004346 * Check character class "class" against current character c.
4347 */
4348 static int
4349check_char_class(class, c)
4350 int class;
4351 int c;
4352{
4353 switch (class)
4354 {
4355 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004356 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004357 return OK;
4358 break;
4359 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004360 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004361 return OK;
4362 break;
4363 case NFA_CLASS_BLANK:
4364 if (c == ' ' || c == '\t')
4365 return OK;
4366 break;
4367 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004368 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004369 return OK;
4370 break;
4371 case NFA_CLASS_DIGIT:
4372 if (VIM_ISDIGIT(c))
4373 return OK;
4374 break;
4375 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004376 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004377 return OK;
4378 break;
4379 case NFA_CLASS_LOWER:
4380 if (MB_ISLOWER(c))
4381 return OK;
4382 break;
4383 case NFA_CLASS_PRINT:
4384 if (vim_isprintc(c))
4385 return OK;
4386 break;
4387 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004388 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004389 return OK;
4390 break;
4391 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004392 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 return OK;
4394 break;
4395 case NFA_CLASS_UPPER:
4396 if (MB_ISUPPER(c))
4397 return OK;
4398 break;
4399 case NFA_CLASS_XDIGIT:
4400 if (vim_isxdigit(c))
4401 return OK;
4402 break;
4403 case NFA_CLASS_TAB:
4404 if (c == '\t')
4405 return OK;
4406 break;
4407 case NFA_CLASS_RETURN:
4408 if (c == '\r')
4409 return OK;
4410 break;
4411 case NFA_CLASS_BACKSPACE:
4412 if (c == '\b')
4413 return OK;
4414 break;
4415 case NFA_CLASS_ESCAPE:
4416 if (c == '\033')
4417 return OK;
4418 break;
4419
4420 default:
4421 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004422 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4423 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004424 }
4425 return FAIL;
4426}
4427
Bram Moolenaar5714b802013-05-28 22:03:20 +02004428/*
4429 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004430 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004431 */
4432 static int
4433match_backref(sub, subidx, bytelen)
4434 regsub_T *sub; /* pointers to subexpressions */
4435 int subidx;
4436 int *bytelen; /* out: length of match in bytes */
4437{
4438 int len;
4439
4440 if (sub->in_use <= subidx)
4441 {
4442retempty:
4443 /* backref was not set, match an empty string */
4444 *bytelen = 0;
4445 return TRUE;
4446 }
4447
4448 if (REG_MULTI)
4449 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004450 if (sub->list.multi[subidx].start.lnum < 0
4451 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004452 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004453 if (sub->list.multi[subidx].start.lnum == reglnum
4454 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004455 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004456 len = sub->list.multi[subidx].end.col
4457 - sub->list.multi[subidx].start.col;
4458 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4459 reginput, &len) == 0)
4460 {
4461 *bytelen = len;
4462 return TRUE;
4463 }
4464 }
4465 else
4466 {
4467 if (match_with_backref(
4468 sub->list.multi[subidx].start.lnum,
4469 sub->list.multi[subidx].start.col,
4470 sub->list.multi[subidx].end.lnum,
4471 sub->list.multi[subidx].end.col,
4472 bytelen) == RA_MATCH)
4473 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004474 }
4475 }
4476 else
4477 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004478 if (sub->list.line[subidx].start == NULL
4479 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004480 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004481 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4482 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004483 {
4484 *bytelen = len;
4485 return TRUE;
4486 }
4487 }
4488 return FALSE;
4489}
4490
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004491#ifdef FEAT_SYN_HL
4492
4493static int match_zref __ARGS((int subidx, int *bytelen));
4494
4495/*
4496 * Check for a match with \z subexpression "subidx".
4497 * Return TRUE if it matches.
4498 */
4499 static int
4500match_zref(subidx, bytelen)
4501 int subidx;
4502 int *bytelen; /* out: length of match in bytes */
4503{
4504 int len;
4505
4506 cleanup_zsubexpr();
4507 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4508 {
4509 /* backref was not set, match an empty string */
4510 *bytelen = 0;
4511 return TRUE;
4512 }
4513
4514 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4515 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4516 {
4517 *bytelen = len;
4518 return TRUE;
4519 }
4520 return FALSE;
4521}
4522#endif
4523
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004525 * Save list IDs for all NFA states of "prog" into "list".
4526 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004527 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004528 */
4529 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004530nfa_save_listids(prog, list)
4531 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004532 int *list;
4533{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004534 int i;
4535 nfa_state_T *p;
4536
4537 /* Order in the list is reverse, it's a bit faster that way. */
4538 p = &prog->state[0];
4539 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004540 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004541 list[i] = p->lastlist[1];
4542 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004543 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004544 }
4545}
4546
4547/*
4548 * Restore list IDs from "list" to all NFA states.
4549 */
4550 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004551nfa_restore_listids(prog, list)
4552 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 int *list;
4554{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004555 int i;
4556 nfa_state_T *p;
4557
4558 p = &prog->state[0];
4559 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004561 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004562 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 }
4564}
4565
Bram Moolenaar423532e2013-05-29 21:14:42 +02004566 static int
4567nfa_re_num_cmp(val, op, pos)
4568 long_u val;
4569 int op;
4570 long_u pos;
4571{
4572 if (op == 1) return pos > val;
4573 if (op == 2) return pos < val;
4574 return val == pos;
4575}
4576
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004577static 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 +02004578static 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 +02004579
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004581 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004582 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4583 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004584 */
4585 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004586recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004587 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004588 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004589 nfa_regprog_T *prog;
4590 regsubs_T *submatch;
4591 regsubs_T *m;
4592 int **listids;
4593{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004594 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004595 int save_reglnum = reglnum;
4596 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004597 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004598 save_se_T *save_nfa_endp = nfa_endp;
4599 save_se_T endpos;
4600 save_se_T *endposp = NULL;
4601 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004602 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004603
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004604 if (pim != NULL)
4605 {
4606 /* start at the position where the postponed match was */
4607 if (REG_MULTI)
4608 reginput = regline + pim->end.pos.col;
4609 else
4610 reginput = pim->end.ptr;
4611 }
4612
Bram Moolenaardecd9542013-06-07 16:31:50 +02004613 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004614 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4615 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4616 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004617 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004618 /* The recursive match must end at the current position. When "pim" is
4619 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004620 endposp = &endpos;
4621 if (REG_MULTI)
4622 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004623 if (pim == NULL)
4624 {
4625 endpos.se_u.pos.col = (int)(reginput - regline);
4626 endpos.se_u.pos.lnum = reglnum;
4627 }
4628 else
4629 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004630 }
4631 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004632 {
4633 if (pim == NULL)
4634 endpos.se_u.ptr = reginput;
4635 else
4636 endpos.se_u.ptr = pim->end.ptr;
4637 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004638
4639 /* Go back the specified number of bytes, or as far as the
4640 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02004641 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004642 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004643 if (state->val <= 0)
4644 {
4645 if (REG_MULTI)
4646 {
4647 regline = reg_getline(--reglnum);
4648 if (regline == NULL)
4649 /* can't go before the first line */
4650 regline = reg_getline(++reglnum);
4651 }
4652 reginput = regline;
4653 }
4654 else
4655 {
4656 if (REG_MULTI && (int)(reginput - regline) < state->val)
4657 {
4658 /* Not enough bytes in this line, go to end of
4659 * previous line. */
4660 regline = reg_getline(--reglnum);
4661 if (regline == NULL)
4662 {
4663 /* can't go before the first line */
4664 regline = reg_getline(++reglnum);
4665 reginput = regline;
4666 }
4667 else
4668 reginput = regline + STRLEN(regline);
4669 }
4670 if ((int)(reginput - regline) >= state->val)
4671 {
4672 reginput -= state->val;
4673#ifdef FEAT_MBYTE
4674 if (has_mbyte)
4675 reginput -= mb_head_off(regline, reginput);
4676#endif
4677 }
4678 else
4679 reginput = regline;
4680 }
4681 }
4682
Bram Moolenaarf46da702013-06-02 22:37:42 +02004683#ifdef ENABLE_LOG
4684 if (log_fd != stderr)
4685 fclose(log_fd);
4686 log_fd = NULL;
4687#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004688 /* Have to clear the lastlist field of the NFA nodes, so that
4689 * nfa_regmatch() and addstate() can run properly after recursion. */
4690 if (nfa_ll_index == 1)
4691 {
4692 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4693 * values and clear them. */
4694 if (*listids == NULL)
4695 {
4696 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4697 if (*listids == NULL)
4698 {
4699 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4700 return 0;
4701 }
4702 }
4703 nfa_save_listids(prog, *listids);
4704 need_restore = TRUE;
4705 /* any value of nfa_listid will do */
4706 }
4707 else
4708 {
4709 /* First recursive nfa_regmatch() call, switch to the second lastlist
4710 * entry. Make sure nfa_listid is different from a previous recursive
4711 * call, because some states may still have this ID. */
4712 ++nfa_ll_index;
4713 if (nfa_listid <= nfa_alt_listid)
4714 nfa_listid = nfa_alt_listid;
4715 }
4716
4717 /* Call nfa_regmatch() to check if the current concat matches at this
4718 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004719 nfa_endp = endposp;
4720 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004721
4722 if (need_restore)
4723 nfa_restore_listids(prog, *listids);
4724 else
4725 {
4726 --nfa_ll_index;
4727 nfa_alt_listid = nfa_listid;
4728 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004729
4730 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004731 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02004732 if (REG_MULTI)
4733 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004734 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004735 nfa_match = save_nfa_match;
4736 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004737 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004738
4739#ifdef ENABLE_LOG
4740 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4741 if (log_fd != NULL)
4742 {
4743 fprintf(log_fd, "****************************\n");
4744 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4745 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4746 fprintf(log_fd, "****************************\n");
4747 }
4748 else
4749 {
4750 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4751 log_fd = stderr;
4752 }
4753#endif
4754
4755 return result;
4756}
4757
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004758static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004759static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004760
4761/*
4762 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004763 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004764 * NFA_ANY: 1
4765 * specific character: 99
4766 */
4767 static int
4768failure_chance(state, depth)
4769 nfa_state_T *state;
4770 int depth;
4771{
4772 int c = state->c;
4773 int l, r;
4774
4775 /* detect looping */
4776 if (depth > 4)
4777 return 1;
4778
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004779 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004780 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004781 case NFA_SPLIT:
4782 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4783 /* avoid recursive stuff */
4784 return 1;
4785 /* two alternatives, use the lowest failure chance */
4786 l = failure_chance(state->out, depth + 1);
4787 r = failure_chance(state->out1, depth + 1);
4788 return l < r ? l : r;
4789
4790 case NFA_ANY:
4791 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004792 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004793
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004794 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004795 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004796 /* empty match works always */
4797 return 0;
4798
Bram Moolenaar44c71db2013-06-14 22:33:51 +02004799 case NFA_START_INVISIBLE:
4800 case NFA_START_INVISIBLE_FIRST:
4801 case NFA_START_INVISIBLE_NEG:
4802 case NFA_START_INVISIBLE_NEG_FIRST:
4803 case NFA_START_INVISIBLE_BEFORE:
4804 case NFA_START_INVISIBLE_BEFORE_FIRST:
4805 case NFA_START_INVISIBLE_BEFORE_NEG:
4806 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
4807 case NFA_START_PATTERN:
4808 /* recursive regmatch is expensive, use low failure chance */
4809 return 5;
4810
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004811 case NFA_BOL:
4812 case NFA_EOL:
4813 case NFA_BOF:
4814 case NFA_EOF:
4815 case NFA_NEWL:
4816 return 99;
4817
4818 case NFA_BOW:
4819 case NFA_EOW:
4820 return 90;
4821
4822 case NFA_MOPEN:
4823 case NFA_MOPEN1:
4824 case NFA_MOPEN2:
4825 case NFA_MOPEN3:
4826 case NFA_MOPEN4:
4827 case NFA_MOPEN5:
4828 case NFA_MOPEN6:
4829 case NFA_MOPEN7:
4830 case NFA_MOPEN8:
4831 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004832#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004833 case NFA_ZOPEN:
4834 case NFA_ZOPEN1:
4835 case NFA_ZOPEN2:
4836 case NFA_ZOPEN3:
4837 case NFA_ZOPEN4:
4838 case NFA_ZOPEN5:
4839 case NFA_ZOPEN6:
4840 case NFA_ZOPEN7:
4841 case NFA_ZOPEN8:
4842 case NFA_ZOPEN9:
4843 case NFA_ZCLOSE:
4844 case NFA_ZCLOSE1:
4845 case NFA_ZCLOSE2:
4846 case NFA_ZCLOSE3:
4847 case NFA_ZCLOSE4:
4848 case NFA_ZCLOSE5:
4849 case NFA_ZCLOSE6:
4850 case NFA_ZCLOSE7:
4851 case NFA_ZCLOSE8:
4852 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004853#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004854 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004855 case NFA_MCLOSE1:
4856 case NFA_MCLOSE2:
4857 case NFA_MCLOSE3:
4858 case NFA_MCLOSE4:
4859 case NFA_MCLOSE5:
4860 case NFA_MCLOSE6:
4861 case NFA_MCLOSE7:
4862 case NFA_MCLOSE8:
4863 case NFA_MCLOSE9:
4864 case NFA_NCLOSE:
4865 return failure_chance(state->out, depth + 1);
4866
4867 case NFA_BACKREF1:
4868 case NFA_BACKREF2:
4869 case NFA_BACKREF3:
4870 case NFA_BACKREF4:
4871 case NFA_BACKREF5:
4872 case NFA_BACKREF6:
4873 case NFA_BACKREF7:
4874 case NFA_BACKREF8:
4875 case NFA_BACKREF9:
4876#ifdef FEAT_SYN_HL
4877 case NFA_ZREF1:
4878 case NFA_ZREF2:
4879 case NFA_ZREF3:
4880 case NFA_ZREF4:
4881 case NFA_ZREF5:
4882 case NFA_ZREF6:
4883 case NFA_ZREF7:
4884 case NFA_ZREF8:
4885 case NFA_ZREF9:
4886#endif
4887 /* backreferences don't match in many places */
4888 return 94;
4889
4890 case NFA_LNUM_GT:
4891 case NFA_LNUM_LT:
4892 case NFA_COL_GT:
4893 case NFA_COL_LT:
4894 case NFA_VCOL_GT:
4895 case NFA_VCOL_LT:
4896 case NFA_MARK_GT:
4897 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004898 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004899 /* before/after positions don't match very often */
4900 return 85;
4901
4902 case NFA_LNUM:
4903 return 90;
4904
4905 case NFA_CURSOR:
4906 case NFA_COL:
4907 case NFA_VCOL:
4908 case NFA_MARK:
4909 /* specific positions rarely match */
4910 return 98;
4911
4912 case NFA_COMPOSING:
4913 return 95;
4914
4915 default:
4916 if (c > 0)
4917 /* character match fails often */
4918 return 95;
4919 }
4920
4921 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004922 return 50;
4923}
4924
Bram Moolenaarf46da702013-06-02 22:37:42 +02004925/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004926 * Skip until the char "c" we know a match must start with.
4927 */
4928 static int
4929skip_to_start(c, colp)
4930 int c;
4931 colnr_T *colp;
4932{
4933 char_u *s;
4934
4935 /* Used often, do some work to avoid call overhead. */
4936 if (!ireg_ic
4937#ifdef FEAT_MBYTE
4938 && !has_mbyte
4939#endif
4940 )
4941 s = vim_strbyte(regline + *colp, c);
4942 else
4943 s = cstrchr(regline + *colp, c);
4944 if (s == NULL)
4945 return FAIL;
4946 *colp = (int)(s - regline);
4947 return OK;
4948}
4949
4950/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004951 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004952 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004953 * Returns zero for no match, 1 for a match.
4954 */
4955 static long
4956find_match_text(startcol, regstart, match_text)
4957 colnr_T startcol;
4958 int regstart;
4959 char_u *match_text;
4960{
4961 colnr_T col = startcol;
4962 int c1, c2;
4963 int len1, len2;
4964 int match;
4965
4966 for (;;)
4967 {
4968 match = TRUE;
4969 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4970 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4971 {
4972 c1 = PTR2CHAR(match_text + len1);
4973 c2 = PTR2CHAR(regline + col + len2);
4974 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4975 {
4976 match = FALSE;
4977 break;
4978 }
4979 len2 += MB_CHAR2LEN(c2);
4980 }
4981 if (match
4982#ifdef FEAT_MBYTE
4983 /* check that no composing char follows */
4984 && !(enc_utf8
4985 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4986#endif
4987 )
4988 {
4989 cleanup_subexpr();
4990 if (REG_MULTI)
4991 {
4992 reg_startpos[0].lnum = reglnum;
4993 reg_startpos[0].col = col;
4994 reg_endpos[0].lnum = reglnum;
4995 reg_endpos[0].col = col + len2;
4996 }
4997 else
4998 {
4999 reg_startp[0] = regline + col;
5000 reg_endp[0] = regline + col + len2;
5001 }
5002 return 1L;
5003 }
5004
5005 /* Try finding regstart after the current match. */
5006 col += MB_CHAR2LEN(regstart); /* skip regstart */
5007 if (skip_to_start(regstart, &col) == FAIL)
5008 break;
5009 }
5010 return 0L;
5011}
5012
5013/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005014 * Main matching routine.
5015 *
5016 * Run NFA to determine whether it matches reginput.
5017 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005018 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005019 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005020 * Return TRUE if there is a match, FALSE otherwise.
5021 * Note: Caller must ensure that: start != NULL.
5022 */
5023 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005024nfa_regmatch(prog, start, submatch, m)
5025 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005026 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005027 regsubs_T *submatch;
5028 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030 int result;
5031 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005032 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005033 int go_to_nextline = FALSE;
5034 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005035 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005036 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005037 nfa_list_T *thislist;
5038 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005039 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005040 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005041 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005042 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005043 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005044 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005045#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005046 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005047
5048 if (debug == NULL)
5049 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005050 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051 return FALSE;
5052 }
5053#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005054 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005056 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005057 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005058 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005059 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005060 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005061 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005062 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005063 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005064
5065#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005066 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005067 if (log_fd != NULL)
5068 {
5069 fprintf(log_fd, "**********************************\n");
5070 nfa_set_code(start->c);
5071 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5072 abs(start->id), code);
5073 fprintf(log_fd, "**********************************\n");
5074 }
5075 else
5076 {
5077 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5078 log_fd = stderr;
5079 }
5080#endif
5081
5082 thislist = &list[0];
5083 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005084 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005085 nextlist = &list[1];
5086 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005087 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005088#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005089 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005090#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005091 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005092
5093 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5094 * it's the first MOPEN. */
5095 if (toplevel)
5096 {
5097 if (REG_MULTI)
5098 {
5099 m->norm.list.multi[0].start.lnum = reglnum;
5100 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5101 }
5102 else
5103 m->norm.list.line[0].start = reginput;
5104 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005105 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005106 }
5107 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005108 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005109
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005110#define ADD_STATE_IF_MATCH(state) \
5111 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005112 add_state = state->out; \
5113 add_off = clen; \
5114 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005115
5116 /*
5117 * Run for each character.
5118 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005119 for (;;)
5120 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005121 int curc;
5122 int clen;
5123
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005124#ifdef FEAT_MBYTE
5125 if (has_mbyte)
5126 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005127 curc = (*mb_ptr2char)(reginput);
5128 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005129 }
5130 else
5131#endif
5132 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005133 curc = *reginput;
5134 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005135 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005136 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005137 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005138 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005139 go_to_nextline = FALSE;
5140 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005141
5142 /* swap lists */
5143 thislist = &list[flag];
5144 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005145 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005146 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005147 ++nfa_listid;
5148 thislist->id = nfa_listid;
5149 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005150
5151#ifdef ENABLE_LOG
5152 fprintf(log_fd, "------------------------------------------\n");
5153 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005154 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005155 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005156 {
5157 int i;
5158
5159 for (i = 0; i < thislist->n; i++)
5160 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5161 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005162 fprintf(log_fd, "\n");
5163#endif
5164
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005165#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005166 fprintf(debug, "\n-------------------\n");
5167#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005168 /*
5169 * If the state lists are empty we can stop.
5170 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005171 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005172 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005173
5174 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005175 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005176 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005177 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005178
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005179#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005180 nfa_set_code(t->state->c);
5181 fprintf(debug, "%s, ", code);
5182#endif
5183#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005184 {
5185 int col;
5186
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005187 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005188 col = -1;
5189 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005190 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005191 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005192 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005193 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005194 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5195 abs(t->state->id), (int)t->state->c, code, col,
5196 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005197 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005198#endif
5199
5200 /*
5201 * Handle the possible codes of the current state.
5202 * The most important is NFA_MATCH.
5203 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005204 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005205 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005206 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005207 switch (t->state->c)
5208 {
5209 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005210 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005211 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005212 copy_sub(&submatch->norm, &t->subs.norm);
5213#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005214 if (nfa_has_zsubexpr)
5215 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005216#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005217#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005218 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005219#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005220 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005221 * states at this position. When the list of states is going
5222 * to be empty quit without advancing, so that "reginput" is
5223 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005224 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005225 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005226 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005227 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005228
5229 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005230 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005231 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005232 /*
5233 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005234 * NFA_START_INVISIBLE_BEFORE node.
5235 * They surround a zero-width group, used with "\@=", "\&",
5236 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005237 * If we got here, it means that the current "invisible" group
5238 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005239 * nfa_regmatch(). For a look-behind match only when it ends
5240 * in the position in "nfa_endp".
5241 * Submatches are stored in *m, and used in the parent call.
5242 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005243#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005244 if (nfa_endp != NULL)
5245 {
5246 if (REG_MULTI)
5247 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5248 (int)reglnum,
5249 (int)nfa_endp->se_u.pos.lnum,
5250 (int)(reginput - regline),
5251 nfa_endp->se_u.pos.col);
5252 else
5253 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5254 (int)(reginput - regline),
5255 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005256 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005257#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005258 /* If "nfa_endp" is set it's only a match if it ends at
5259 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005260 if (nfa_endp != NULL && (REG_MULTI
5261 ? (reglnum != nfa_endp->se_u.pos.lnum
5262 || (int)(reginput - regline)
5263 != nfa_endp->se_u.pos.col)
5264 : reginput != nfa_endp->se_u.ptr))
5265 break;
5266
5267 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005268 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005269 {
5270 copy_sub(&m->norm, &t->subs.norm);
5271#ifdef FEAT_SYN_HL
5272 if (nfa_has_zsubexpr)
5273 copy_sub(&m->synt, &t->subs.synt);
5274#endif
5275 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005276#ifdef ENABLE_LOG
5277 fprintf(log_fd, "Match found:\n");
5278 log_subsexpr(m);
5279#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005280 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005281 break;
5282
5283 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005284 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005285 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005286 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005287 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005288 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005289 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005290 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005291 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005292#ifdef ENABLE_LOG
5293 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5294 failure_chance(t->state->out, 0),
5295 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005296#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005297 /* Do it directly if there already is a PIM or when
5298 * nfa_postprocess() detected it will work better. */
5299 if (t->pim.result != NFA_PIM_UNUSED
5300 || t->state->c == NFA_START_INVISIBLE_FIRST
5301 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5302 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5303 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005304 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005305 int in_use = m->norm.in_use;
5306
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005307 /* Copy submatch info for the recursive call, so that
5308 * \1 can be matched. */
5309 copy_sub_off(&m->norm, &t->subs.norm);
5310
Bram Moolenaara2d95102013-06-04 14:23:05 +02005311 /*
5312 * First try matching the invisible match, then what
5313 * follows.
5314 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005315 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005316 submatch, m, &listids);
5317
Bram Moolenaardecd9542013-06-07 16:31:50 +02005318 /* for \@! and \@<! it is a match when the result is
5319 * FALSE */
5320 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005321 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5322 || t->state->c
5323 == NFA_START_INVISIBLE_BEFORE_NEG
5324 || t->state->c
5325 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005326 {
5327 /* Copy submatch info from the recursive call */
5328 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005329#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005330 if (nfa_has_zsubexpr)
5331 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005332#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005333
Bram Moolenaara2d95102013-06-04 14:23:05 +02005334 /* t->state->out1 is the corresponding
5335 * END_INVISIBLE node; Add its out to the current
5336 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005337 add_here = TRUE;
5338 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005339 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005340 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005341 }
5342 else
5343 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005344 nfa_pim_T pim;
5345
Bram Moolenaara2d95102013-06-04 14:23:05 +02005346 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005347 * First try matching what follows. Only if a match
5348 * is found verify the invisible match matches. Add a
5349 * nfa_pim_T to the following states, it contains info
5350 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005351 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005352 pim.state = t->state;
5353 pim.result = NFA_PIM_TODO;
5354 pim.subs.norm.in_use = 0;
5355#ifdef FEAT_SYN_HL
5356 pim.subs.synt.in_use = 0;
5357#endif
5358 if (REG_MULTI)
5359 {
5360 pim.end.pos.col = (int)(reginput - regline);
5361 pim.end.pos.lnum = reglnum;
5362 }
5363 else
5364 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005365
5366 /* t->state->out1 is the corresponding END_INVISIBLE
5367 * node; Add its out to the current list (zero-width
5368 * match). */
5369 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005370 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005371 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005372 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005373 break;
5374
Bram Moolenaar87953742013-06-05 18:52:40 +02005375 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005376 {
5377 nfa_state_T *skip = NULL;
5378#ifdef ENABLE_LOG
5379 int skip_lid = 0;
5380#endif
5381
5382 /* There is no point in trying to match the pattern if the
5383 * output state is not going to be added to the list. */
5384 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5385 {
5386 skip = t->state->out1->out;
5387#ifdef ENABLE_LOG
5388 skip_lid = nextlist->id;
5389#endif
5390 }
5391 else if (state_in_list(nextlist,
5392 t->state->out1->out->out, &t->subs))
5393 {
5394 skip = t->state->out1->out->out;
5395#ifdef ENABLE_LOG
5396 skip_lid = nextlist->id;
5397#endif
5398 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005399 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005400 t->state->out1->out->out, &t->subs))
5401 {
5402 skip = t->state->out1->out->out;
5403#ifdef ENABLE_LOG
5404 skip_lid = thislist->id;
5405#endif
5406 }
5407 if (skip != NULL)
5408 {
5409#ifdef ENABLE_LOG
5410 nfa_set_code(skip->c);
5411 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5412 abs(skip->id), skip_lid, skip->c, code);
5413#endif
5414 break;
5415 }
5416
Bram Moolenaar87953742013-06-05 18:52:40 +02005417 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005418 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005419 submatch, m, &listids);
5420 if (result)
5421 {
5422 int bytelen;
5423
5424#ifdef ENABLE_LOG
5425 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5426 log_subsexpr(m);
5427#endif
5428 /* Copy submatch info from the recursive call */
5429 copy_sub_off(&t->subs.norm, &m->norm);
5430#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005431 if (nfa_has_zsubexpr)
5432 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005433#endif
5434 /* Now we need to skip over the matched text and then
5435 * continue with what follows. */
5436 if (REG_MULTI)
5437 /* TODO: multi-line match */
5438 bytelen = m->norm.list.multi[0].end.col
5439 - (int)(reginput - regline);
5440 else
5441 bytelen = (int)(m->norm.list.line[0].end - reginput);
5442
5443#ifdef ENABLE_LOG
5444 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5445#endif
5446 if (bytelen == 0)
5447 {
5448 /* empty match, output of corresponding
5449 * NFA_END_PATTERN/NFA_SKIP to be used at current
5450 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005451 add_here = TRUE;
5452 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005453 }
5454 else if (bytelen <= clen)
5455 {
5456 /* match current character, output of corresponding
5457 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005458 add_state = t->state->out1->out->out;
5459 add_off = clen;
5460 }
5461 else
5462 {
5463 /* skip over the matched characters, set character
5464 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005465 add_state = t->state->out1->out;
5466 add_off = bytelen;
5467 add_count = bytelen - clen;
5468 }
5469 }
5470 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005471 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005472
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005473 case NFA_BOL:
5474 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005475 {
5476 add_here = TRUE;
5477 add_state = t->state->out;
5478 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005479 break;
5480
5481 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005482 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005483 {
5484 add_here = TRUE;
5485 add_state = t->state->out;
5486 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005487 break;
5488
5489 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005490 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005491
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005492 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005493 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005494#ifdef FEAT_MBYTE
5495 else if (has_mbyte)
5496 {
5497 int this_class;
5498
5499 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005500 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005501 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005502 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005504 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005505 }
5506#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005507 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005508 || (reginput > regline
5509 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005510 result = FALSE;
5511 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005512 {
5513 add_here = TRUE;
5514 add_state = t->state->out;
5515 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005516 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005517
5518 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005519 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005520 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005521 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005522#ifdef FEAT_MBYTE
5523 else if (has_mbyte)
5524 {
5525 int this_class, prev_class;
5526
5527 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005528 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529 prev_class = reg_prev_class();
5530 if (this_class == prev_class
5531 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005532 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533 }
5534#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005535 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005536 || (reginput[0] != NUL
5537 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005538 result = FALSE;
5539 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005540 {
5541 add_here = TRUE;
5542 add_state = t->state->out;
5543 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005544 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005545
Bram Moolenaar4b780632013-05-31 22:14:52 +02005546 case NFA_BOF:
5547 if (reglnum == 0 && reginput == regline
5548 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005549 {
5550 add_here = TRUE;
5551 add_state = t->state->out;
5552 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005553 break;
5554
5555 case NFA_EOF:
5556 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005557 {
5558 add_here = TRUE;
5559 add_state = t->state->out;
5560 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005561 break;
5562
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005563#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005565 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005566 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005567 int len = 0;
5568 nfa_state_T *end;
5569 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005570 int cchars[MAX_MCO];
5571 int ccount = 0;
5572 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005573
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005574 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005575 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005576 if (utf_iscomposing(sta->c))
5577 {
5578 /* Only match composing character(s), ignore base
5579 * character. Used for ".{composing}" and "{composing}"
5580 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005581 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005582 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005583 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005584 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005585 /* If \Z was present, then ignore composing characters.
5586 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005587 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005588 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005589 else
5590 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005591 while (sta->c != NFA_END_COMPOSING)
5592 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005593 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005594
5595 /* Check base character matches first, unless ignored. */
5596 else if (len > 0 || mc == sta->c)
5597 {
5598 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005599 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005600 len += mb_char2len(mc);
5601 sta = sta->out;
5602 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005603
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005604 /* We don't care about the order of composing characters.
5605 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005606 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005607 {
5608 mc = mb_ptr2char(reginput + len);
5609 cchars[ccount++] = mc;
5610 len += mb_char2len(mc);
5611 if (ccount == MAX_MCO)
5612 break;
5613 }
5614
5615 /* Check that each composing char in the pattern matches a
5616 * composing char in the text. We do not check if all
5617 * composing chars are matched. */
5618 result = OK;
5619 while (sta->c != NFA_END_COMPOSING)
5620 {
5621 for (j = 0; j < ccount; ++j)
5622 if (cchars[j] == sta->c)
5623 break;
5624 if (j == ccount)
5625 {
5626 result = FAIL;
5627 break;
5628 }
5629 sta = sta->out;
5630 }
5631 }
5632 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005633 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005634
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005635 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005636 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005638 }
5639#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005640
5641 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005642 if (curc == NUL && !reg_line_lbr && REG_MULTI
5643 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005644 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005645 go_to_nextline = TRUE;
5646 /* Pass -1 for the offset, which means taking the position
5647 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005648 add_state = t->state->out;
5649 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005650 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005651 else if (curc == '\n' && reg_line_lbr)
5652 {
5653 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005654 add_state = t->state->out;
5655 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005656 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005657 break;
5658
Bram Moolenaar417bad22013-06-07 14:08:30 +02005659 case NFA_START_COLL:
5660 case NFA_START_NEG_COLL:
5661 {
5662 /* What follows is a list of characters, until NFA_END_COLL.
5663 * One of them must match or none of them must match. */
5664 nfa_state_T *state;
5665 int result_if_matched;
5666 int c1, c2;
5667
5668 /* Never match EOL. If it's part of the collection it is added
5669 * as a separate state with an OR. */
5670 if (curc == NUL)
5671 break;
5672
5673 state = t->state->out;
5674 result_if_matched = (t->state->c == NFA_START_COLL);
5675 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005676 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005677 if (state->c == NFA_END_COLL)
5678 {
5679 result = !result_if_matched;
5680 break;
5681 }
5682 if (state->c == NFA_RANGE_MIN)
5683 {
5684 c1 = state->val;
5685 state = state->out; /* advance to NFA_RANGE_MAX */
5686 c2 = state->val;
5687#ifdef ENABLE_LOG
5688 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5689 curc, c1, c2);
5690#endif
5691 if (curc >= c1 && curc <= c2)
5692 {
5693 result = result_if_matched;
5694 break;
5695 }
5696 if (ireg_ic)
5697 {
5698 int curc_low = MB_TOLOWER(curc);
5699 int done = FALSE;
5700
5701 for ( ; c1 <= c2; ++c1)
5702 if (MB_TOLOWER(c1) == curc_low)
5703 {
5704 result = result_if_matched;
5705 done = TRUE;
5706 break;
5707 }
5708 if (done)
5709 break;
5710 }
5711 }
5712 else if (state->c < 0 ? check_char_class(state->c, curc)
5713 : (curc == state->c
5714 || (ireg_ic && MB_TOLOWER(curc)
5715 == MB_TOLOWER(state->c))))
5716 {
5717 result = result_if_matched;
5718 break;
5719 }
5720 state = state->out;
5721 }
5722 if (result)
5723 {
5724 /* next state is in out of the NFA_END_COLL, out1 of
5725 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005726 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005727 add_off = clen;
5728 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005729 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005730 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005731
5732 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005733 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005734 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005735 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005736 add_state = t->state->out;
5737 add_off = clen;
5738 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005739 break;
5740
5741 /*
5742 * Character classes like \a for alpha, \d for digit etc.
5743 */
5744 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005745 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005746 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005747 break;
5748
5749 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005750 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005751 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005752 break;
5753
5754 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005755 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005756 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005757 break;
5758
5759 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005760 result = !VIM_ISDIGIT(curc)
5761 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005762 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005763 break;
5764
5765 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005766 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005767 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005768 break;
5769
5770 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005771 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005772 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005773 break;
5774
5775 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005776 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005777 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005778 break;
5779
5780 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005781 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005782 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005783 break;
5784
5785 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005786 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005787 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005788 break;
5789
5790 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005791 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005792 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005793 break;
5794
5795 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005796 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005797 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005798 break;
5799
5800 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005801 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005802 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005803 break;
5804
5805 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005806 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005807 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005808 break;
5809
5810 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005811 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005812 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005813 break;
5814
5815 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005816 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005817 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005818 break;
5819
5820 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005821 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005822 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005823 break;
5824
5825 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005826 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005827 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005828 break;
5829
5830 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005831 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005832 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005833 break;
5834
5835 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005836 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005837 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005838 break;
5839
5840 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005841 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005842 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005843 break;
5844
5845 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005846 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005847 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005848 break;
5849
5850 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005851 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005852 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005853 break;
5854
5855 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005856 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005857 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005858 break;
5859
5860 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005861 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005862 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005863 break;
5864
5865 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005866 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005867 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005868 break;
5869
5870 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005871 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005872 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005873 break;
5874
Bram Moolenaar5714b802013-05-28 22:03:20 +02005875 case NFA_BACKREF1:
5876 case NFA_BACKREF2:
5877 case NFA_BACKREF3:
5878 case NFA_BACKREF4:
5879 case NFA_BACKREF5:
5880 case NFA_BACKREF6:
5881 case NFA_BACKREF7:
5882 case NFA_BACKREF8:
5883 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005884#ifdef FEAT_SYN_HL
5885 case NFA_ZREF1:
5886 case NFA_ZREF2:
5887 case NFA_ZREF3:
5888 case NFA_ZREF4:
5889 case NFA_ZREF5:
5890 case NFA_ZREF6:
5891 case NFA_ZREF7:
5892 case NFA_ZREF8:
5893 case NFA_ZREF9:
5894#endif
5895 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005896 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005897 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005898 int bytelen;
5899
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005900 if (t->state->c <= NFA_BACKREF9)
5901 {
5902 subidx = t->state->c - NFA_BACKREF1 + 1;
5903 result = match_backref(&t->subs.norm, subidx, &bytelen);
5904 }
5905#ifdef FEAT_SYN_HL
5906 else
5907 {
5908 subidx = t->state->c - NFA_ZREF1 + 1;
5909 result = match_zref(subidx, &bytelen);
5910 }
5911#endif
5912
Bram Moolenaar5714b802013-05-28 22:03:20 +02005913 if (result)
5914 {
5915 if (bytelen == 0)
5916 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005917 /* empty match always works, output of NFA_SKIP to be
5918 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005919 add_here = TRUE;
5920 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005921 }
5922 else if (bytelen <= clen)
5923 {
5924 /* match current character, jump ahead to out of
5925 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005926 add_state = t->state->out->out;
5927 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005928 }
5929 else
5930 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005931 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005932 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005933 add_state = t->state->out;
5934 add_off = bytelen;
5935 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005936 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005937 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005938 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005939 }
5940 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005941 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005942 if (t->count - clen <= 0)
5943 {
5944 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005945 add_state = t->state->out;
5946 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005947 }
5948 else
5949 {
5950 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005951 add_state = t->state;
5952 add_off = 0;
5953 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005954 }
5955 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005956
Bram Moolenaar423532e2013-05-29 21:14:42 +02005957 case NFA_LNUM:
5958 case NFA_LNUM_GT:
5959 case NFA_LNUM_LT:
5960 result = (REG_MULTI &&
5961 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5962 (long_u)(reglnum + reg_firstlnum)));
5963 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005964 {
5965 add_here = TRUE;
5966 add_state = t->state->out;
5967 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005968 break;
5969
5970 case NFA_COL:
5971 case NFA_COL_GT:
5972 case NFA_COL_LT:
5973 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5974 (long_u)(reginput - regline) + 1);
5975 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005976 {
5977 add_here = TRUE;
5978 add_state = t->state->out;
5979 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005980 break;
5981
5982 case NFA_VCOL:
5983 case NFA_VCOL_GT:
5984 case NFA_VCOL_LT:
5985 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5986 (long_u)win_linetabsize(
5987 reg_win == NULL ? curwin : reg_win,
5988 regline, (colnr_T)(reginput - regline)) + 1);
5989 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005990 {
5991 add_here = TRUE;
5992 add_state = t->state->out;
5993 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005994 break;
5995
Bram Moolenaar044aa292013-06-04 21:27:38 +02005996 case NFA_MARK:
5997 case NFA_MARK_GT:
5998 case NFA_MARK_LT:
5999 {
6000 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6001
6002 /* Compare the mark position to the match position. */
6003 result = (pos != NULL /* mark doesn't exist */
6004 && pos->lnum > 0 /* mark isn't set in reg_buf */
6005 && (pos->lnum == reglnum + reg_firstlnum
6006 ? (pos->col == (colnr_T)(reginput - regline)
6007 ? t->state->c == NFA_MARK
6008 : (pos->col < (colnr_T)(reginput - regline)
6009 ? t->state->c == NFA_MARK_GT
6010 : t->state->c == NFA_MARK_LT))
6011 : (pos->lnum < reglnum + reg_firstlnum
6012 ? t->state->c == NFA_MARK_GT
6013 : t->state->c == NFA_MARK_LT)));
6014 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006015 {
6016 add_here = TRUE;
6017 add_state = t->state->out;
6018 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006019 break;
6020 }
6021
Bram Moolenaar423532e2013-05-29 21:14:42 +02006022 case NFA_CURSOR:
6023 result = (reg_win != NULL
6024 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6025 && ((colnr_T)(reginput - regline)
6026 == reg_win->w_cursor.col));
6027 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006028 {
6029 add_here = TRUE;
6030 add_state = t->state->out;
6031 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006032 break;
6033
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006034 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02006035#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006036 result = reg_match_visual();
6037 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006038 {
6039 add_here = TRUE;
6040 add_state = t->state->out;
6041 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02006042#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02006043 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006044
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006045 case NFA_MOPEN1:
6046 case NFA_MOPEN2:
6047 case NFA_MOPEN3:
6048 case NFA_MOPEN4:
6049 case NFA_MOPEN5:
6050 case NFA_MOPEN6:
6051 case NFA_MOPEN7:
6052 case NFA_MOPEN8:
6053 case NFA_MOPEN9:
6054#ifdef FEAT_SYN_HL
6055 case NFA_ZOPEN:
6056 case NFA_ZOPEN1:
6057 case NFA_ZOPEN2:
6058 case NFA_ZOPEN3:
6059 case NFA_ZOPEN4:
6060 case NFA_ZOPEN5:
6061 case NFA_ZOPEN6:
6062 case NFA_ZOPEN7:
6063 case NFA_ZOPEN8:
6064 case NFA_ZOPEN9:
6065#endif
6066 case NFA_NOPEN:
6067 case NFA_ZSTART:
6068 /* These states are only added to be able to bail out when
6069 * they are added again, nothing is to be done. */
6070 break;
6071
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006072 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006073 {
6074 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006075
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006076#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006077 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006078 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006079#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006080 result = (c == curc);
6081
6082 if (!result && ireg_ic)
6083 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006084#ifdef FEAT_MBYTE
6085 /* If there is a composing character which is not being
6086 * ignored there can be no match. Match with composing
6087 * character uses NFA_COMPOSING above. */
6088 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006089 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006090 result = FALSE;
6091#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006092 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006094 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006095
6096 } /* switch (t->state->c) */
6097
6098 if (add_state != NULL)
6099 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006100 nfa_pim_T *pim;
6101
6102 if (t->pim.result == NFA_PIM_UNUSED)
6103 pim = NULL;
6104 else
6105 pim = &t->pim;
6106
6107 /* Handle the postponed invisible match if the match might end
6108 * without advancing and before the end of the line. */
6109 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006110 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006111 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006112 {
6113#ifdef ENABLE_LOG
6114 fprintf(log_fd, "\n");
6115 fprintf(log_fd, "==================================\n");
6116 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6117 fprintf(log_fd, "\n");
6118#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006119 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006120 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006121 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006122 /* for \@! and \@<! it is a match when the result is
6123 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006124 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006125 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6126 || pim->state->c
6127 == NFA_START_INVISIBLE_BEFORE_NEG
6128 || pim->state->c
6129 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006130 {
6131 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006132 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006133#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006134 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006135 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006136#endif
6137 }
6138 }
6139 else
6140 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006141 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006142#ifdef ENABLE_LOG
6143 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006144 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006145 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6146 fprintf(log_fd, "\n");
6147#endif
6148 }
6149
Bram Moolenaardecd9542013-06-07 16:31:50 +02006150 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006151 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006152 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6153 || pim->state->c
6154 == NFA_START_INVISIBLE_BEFORE_NEG
6155 || pim->state->c
6156 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006157 {
6158 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006159 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006160#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006161 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006162 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006163#endif
6164 }
6165 else
6166 /* look-behind match failed, don't add the state */
6167 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006168
6169 /* Postponed invisible match was handled, don't add it to
6170 * following states. */
6171 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006172 }
6173
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006174 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006175 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006176 else
6177 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006178 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006179 if (add_count > 0)
6180 nextlist->t[nextlist->n - 1].count = add_count;
6181 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006182 }
6183
6184 } /* for (thislist = thislist; thislist->state; thislist++) */
6185
Bram Moolenaare23febd2013-05-26 18:40:14 +02006186 /* Look for the start of a match in the current position by adding the
6187 * start state to the list of states.
6188 * The first found match is the leftmost one, thus the order of states
6189 * matters!
6190 * Do not add the start state in recursive calls of nfa_regmatch(),
6191 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006192 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006193 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006194 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006195 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006196 && reglnum == 0
6197 && clen != 0
6198 && (ireg_maxcol == 0
6199 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006200 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006201 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006202 ? (reglnum < nfa_endp->se_u.pos.lnum
6203 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006204 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006205 < nfa_endp->se_u.pos.col))
6206 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006207 {
6208#ifdef ENABLE_LOG
6209 fprintf(log_fd, "(---) STARTSTATE\n");
6210#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006211 /* Inline optimized code for addstate() if we know the state is
6212 * the first MOPEN. */
6213 if (toplevel)
6214 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006215 int add = TRUE;
6216 int c;
6217
6218 if (prog->regstart != NUL && clen != 0)
6219 {
6220 if (nextlist->n == 0)
6221 {
6222 colnr_T col = (colnr_T)(reginput - regline) + clen;
6223
6224 /* Nextlist is empty, we can skip ahead to the
6225 * character that must appear at the start. */
6226 if (skip_to_start(prog->regstart, &col) == FAIL)
6227 break;
6228#ifdef ENABLE_LOG
6229 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6230 col - ((colnr_T)(reginput - regline) + clen));
6231#endif
6232 reginput = regline + col - clen;
6233 }
6234 else
6235 {
6236 /* Checking if the required start character matches is
6237 * cheaper than adding a state that won't match. */
6238 c = PTR2CHAR(reginput + clen);
6239 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6240 != MB_TOLOWER(prog->regstart)))
6241 {
6242#ifdef ENABLE_LOG
6243 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6244#endif
6245 add = FALSE;
6246 }
6247 }
6248 }
6249
6250 if (add)
6251 {
6252 if (REG_MULTI)
6253 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006254 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006255 else
6256 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006257 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006258 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006259 }
6260 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006261 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006262 }
6263
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006264#ifdef ENABLE_LOG
6265 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006266 {
6267 int i;
6268
6269 for (i = 0; i < thislist->n; i++)
6270 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6271 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006272 fprintf(log_fd, "\n");
6273#endif
6274
6275nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006276 /* Advance to the next character, or advance to the next line, or
6277 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006278 if (clen != 0)
6279 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006280 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6281 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006282 reg_nextline();
6283 else
6284 break;
6285 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006286
6287#ifdef ENABLE_LOG
6288 if (log_fd != stderr)
6289 fclose(log_fd);
6290 log_fd = NULL;
6291#endif
6292
6293theend:
6294 /* Free memory */
6295 vim_free(list[0].t);
6296 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006297 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006298#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006299#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006300 fclose(debug);
6301#endif
6302
Bram Moolenaar963fee22013-05-26 21:47:28 +02006303 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006304}
6305
6306/*
6307 * Try match of "prog" with at regline["col"].
6308 * Returns 0 for failure, number of lines contained in the match otherwise.
6309 */
6310 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006311nfa_regtry(prog, col)
6312 nfa_regprog_T *prog;
6313 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006314{
6315 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006316 regsubs_T subs, m;
6317 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318#ifdef ENABLE_LOG
6319 FILE *f;
6320#endif
6321
6322 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323
6324#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006325 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006326 if (f != NULL)
6327 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006328 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006329#ifdef DEBUG
6330 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6331#endif
6332 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006333 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006334 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006335 fprintf(f, "\n\n");
6336 fclose(f);
6337 }
6338 else
6339 EMSG(_("Could not open temporary log file for writing "));
6340#endif
6341
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006342 clear_sub(&subs.norm);
6343 clear_sub(&m.norm);
6344#ifdef FEAT_SYN_HL
6345 clear_sub(&subs.synt);
6346 clear_sub(&m.synt);
6347#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006348
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006349 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006350 return 0;
6351
6352 cleanup_subexpr();
6353 if (REG_MULTI)
6354 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006355 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006356 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006357 reg_startpos[i] = subs.norm.list.multi[i].start;
6358 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006359 }
6360
6361 if (reg_startpos[0].lnum < 0)
6362 {
6363 reg_startpos[0].lnum = 0;
6364 reg_startpos[0].col = col;
6365 }
6366 if (reg_endpos[0].lnum < 0)
6367 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006368 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006369 reg_endpos[0].lnum = reglnum;
6370 reg_endpos[0].col = (int)(reginput - regline);
6371 }
6372 else
6373 /* Use line number of "\ze". */
6374 reglnum = reg_endpos[0].lnum;
6375 }
6376 else
6377 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006378 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006380 reg_startp[i] = subs.norm.list.line[i].start;
6381 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006382 }
6383
6384 if (reg_startp[0] == NULL)
6385 reg_startp[0] = regline + col;
6386 if (reg_endp[0] == NULL)
6387 reg_endp[0] = reginput;
6388 }
6389
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006390#ifdef FEAT_SYN_HL
6391 /* Package any found \z(...\) matches for export. Default is none. */
6392 unref_extmatch(re_extmatch_out);
6393 re_extmatch_out = NULL;
6394
6395 if (prog->reghasz == REX_SET)
6396 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006397 cleanup_zsubexpr();
6398 re_extmatch_out = make_extmatch();
6399 for (i = 0; i < subs.synt.in_use; i++)
6400 {
6401 if (REG_MULTI)
6402 {
6403 struct multipos *mpos = &subs.synt.list.multi[i];
6404
6405 /* Only accept single line matches. */
6406 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6407 re_extmatch_out->matches[i] =
6408 vim_strnsave(reg_getline(mpos->start.lnum)
6409 + mpos->start.col,
6410 mpos->end.col - mpos->start.col);
6411 }
6412 else
6413 {
6414 struct linepos *lpos = &subs.synt.list.line[i];
6415
6416 if (lpos->start != NULL && lpos->end != NULL)
6417 re_extmatch_out->matches[i] =
6418 vim_strnsave(lpos->start,
6419 (int)(lpos->end - lpos->start));
6420 }
6421 }
6422 }
6423#endif
6424
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006425 return 1 + reglnum;
6426}
6427
6428/*
6429 * Match a regexp against a string ("line" points to the string) or multiple
6430 * lines ("line" is NULL, use reg_getline()).
6431 *
6432 * Returns 0 for failure, number of lines contained in the match otherwise.
6433 */
6434 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006435nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006436 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006437 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006438{
6439 nfa_regprog_T *prog;
6440 long retval = 0L;
6441 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006442 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006443
6444 if (REG_MULTI)
6445 {
6446 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6447 line = reg_getline((linenr_T)0); /* relative to the cursor */
6448 reg_startpos = reg_mmatch->startpos;
6449 reg_endpos = reg_mmatch->endpos;
6450 }
6451 else
6452 {
6453 prog = (nfa_regprog_T *)reg_match->regprog;
6454 reg_startp = reg_match->startp;
6455 reg_endp = reg_match->endp;
6456 }
6457
6458 /* Be paranoid... */
6459 if (prog == NULL || line == NULL)
6460 {
6461 EMSG(_(e_null));
6462 goto theend;
6463 }
6464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006465 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6466 if (prog->regflags & RF_ICASE)
6467 ireg_ic = TRUE;
6468 else if (prog->regflags & RF_NOICASE)
6469 ireg_ic = FALSE;
6470
6471#ifdef FEAT_MBYTE
6472 /* If pattern contains "\Z" overrule value of ireg_icombine */
6473 if (prog->regflags & RF_ICOMBINE)
6474 ireg_icombine = TRUE;
6475#endif
6476
6477 regline = line;
6478 reglnum = 0; /* relative to line */
6479
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006480 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006481 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006482 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006483 nfa_listid = 1;
6484 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006485#ifdef DEBUG
6486 nfa_regengine.expr = prog->pattern;
6487#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006488
Bram Moolenaard89616e2013-06-06 18:46:06 +02006489 if (prog->reganch && col > 0)
6490 return 0L;
6491
Bram Moolenaar473de612013-06-08 18:19:48 +02006492 need_clear_subexpr = TRUE;
6493#ifdef FEAT_SYN_HL
6494 /* Clear the external match subpointers if necessary. */
6495 if (prog->reghasz == REX_SET)
6496 {
6497 nfa_has_zsubexpr = TRUE;
6498 need_clear_zsubexpr = TRUE;
6499 }
6500 else
6501 nfa_has_zsubexpr = FALSE;
6502#endif
6503
Bram Moolenaard89616e2013-06-06 18:46:06 +02006504 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006505 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006506 /* Skip ahead until a character we know the match must start with.
6507 * When there is none there is no match. */
6508 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006509 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006510
Bram Moolenaar473de612013-06-08 18:19:48 +02006511 /* If match_text is set it contains the full text that must match.
6512 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006513 if (prog->match_text != NULL
6514#ifdef FEAT_MBYTE
6515 && !ireg_icombine
6516#endif
6517 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006518 return find_match_text(col, prog->regstart, prog->match_text);
6519 }
6520
Bram Moolenaard89616e2013-06-06 18:46:06 +02006521 /* If the start column is past the maximum column: no need to try. */
6522 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6523 goto theend;
6524
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006525 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006526 for (i = 0; i < nstate; ++i)
6527 {
6528 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006529 prog->state[i].lastlist[0] = 0;
6530 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006531 }
6532
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006533 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006534
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006535#ifdef DEBUG
6536 nfa_regengine.expr = NULL;
6537#endif
6538
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006539theend:
6540 return retval;
6541}
6542
6543/*
6544 * Compile a regular expression into internal code for the NFA matcher.
6545 * Returns the program in allocated space. Returns NULL for an error.
6546 */
6547 static regprog_T *
6548nfa_regcomp(expr, re_flags)
6549 char_u *expr;
6550 int re_flags;
6551{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006552 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006553 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006554 int *postfix;
6555
6556 if (expr == NULL)
6557 return NULL;
6558
6559#ifdef DEBUG
6560 nfa_regengine.expr = expr;
6561#endif
6562
6563 init_class_tab();
6564
6565 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6566 return NULL;
6567
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006568 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006569 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006570 postfix = re2post();
6571 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006572 {
6573 /* TODO: only give this error for debugging? */
6574 if (post_ptr >= post_end)
6575 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006576 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006578
6579 /*
6580 * In order to build the NFA, we parse the input regexp twice:
6581 * 1. first pass to count size (so we can allocate space)
6582 * 2. second to emit code
6583 */
6584#ifdef ENABLE_LOG
6585 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006586 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006587
6588 if (f != NULL)
6589 {
6590 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6591 fclose(f);
6592 }
6593 }
6594#endif
6595
6596 /*
6597 * PASS 1
6598 * Count number of NFA states in "nstate". Do not build the NFA.
6599 */
6600 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006601
Bram Moolenaar16619a22013-06-11 18:42:36 +02006602 /* allocate the regprog with space for the compiled regexp */
6603 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006604 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6605 if (prog == NULL)
6606 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006607 state_ptr = prog->state;
6608
6609 /*
6610 * PASS 2
6611 * Build the NFA
6612 */
6613 prog->start = post2nfa(postfix, post_ptr, FALSE);
6614 if (prog->start == NULL)
6615 goto fail;
6616
6617 prog->regflags = regflags;
6618 prog->engine = &nfa_regengine;
6619 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006620 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006621 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006622 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006623
Bram Moolenaara2947e22013-06-11 22:44:09 +02006624 nfa_postprocess(prog);
6625
Bram Moolenaard89616e2013-06-06 18:46:06 +02006626 prog->reganch = nfa_get_reganch(prog->start, 0);
6627 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006628 prog->match_text = nfa_get_match_text(prog->start);
6629
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006630#ifdef ENABLE_LOG
6631 nfa_postfix_dump(expr, OK);
6632 nfa_dump(prog);
6633#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006634#ifdef FEAT_SYN_HL
6635 /* Remember whether this pattern has any \z specials in it. */
6636 prog->reghasz = re_has_z;
6637#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006638#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006639 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006640 nfa_regengine.expr = NULL;
6641#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006642
6643out:
6644 vim_free(post_start);
6645 post_start = post_ptr = post_end = NULL;
6646 state_ptr = NULL;
6647 return (regprog_T *)prog;
6648
6649fail:
6650 vim_free(prog);
6651 prog = NULL;
6652#ifdef ENABLE_LOG
6653 nfa_postfix_dump(expr, FAIL);
6654#endif
6655#ifdef DEBUG
6656 nfa_regengine.expr = NULL;
6657#endif
6658 goto out;
6659}
6660
Bram Moolenaar473de612013-06-08 18:19:48 +02006661/*
6662 * Free a compiled regexp program, returned by nfa_regcomp().
6663 */
6664 static void
6665nfa_regfree(prog)
6666 regprog_T *prog;
6667{
6668 if (prog != NULL)
6669 {
6670 vim_free(((nfa_regprog_T *)prog)->match_text);
6671#ifdef DEBUG
6672 vim_free(((nfa_regprog_T *)prog)->pattern);
6673#endif
6674 vim_free(prog);
6675 }
6676}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006677
6678/*
6679 * Match a regexp against a string.
6680 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6681 * Uses curbuf for line count and 'iskeyword'.
6682 *
6683 * Return TRUE if there is a match, FALSE if not.
6684 */
6685 static int
6686nfa_regexec(rmp, line, col)
6687 regmatch_T *rmp;
6688 char_u *line; /* string to match against */
6689 colnr_T col; /* column to start looking for match */
6690{
6691 reg_match = rmp;
6692 reg_mmatch = NULL;
6693 reg_maxline = 0;
6694 reg_line_lbr = FALSE;
6695 reg_buf = curbuf;
6696 reg_win = NULL;
6697 ireg_ic = rmp->rm_ic;
6698#ifdef FEAT_MBYTE
6699 ireg_icombine = FALSE;
6700#endif
6701 ireg_maxcol = 0;
6702 return (nfa_regexec_both(line, col) != 0);
6703}
6704
6705#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6706 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6707
6708static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6709
6710/*
6711 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6712 */
6713 static int
6714nfa_regexec_nl(rmp, line, col)
6715 regmatch_T *rmp;
6716 char_u *line; /* string to match against */
6717 colnr_T col; /* column to start looking for match */
6718{
6719 reg_match = rmp;
6720 reg_mmatch = NULL;
6721 reg_maxline = 0;
6722 reg_line_lbr = TRUE;
6723 reg_buf = curbuf;
6724 reg_win = NULL;
6725 ireg_ic = rmp->rm_ic;
6726#ifdef FEAT_MBYTE
6727 ireg_icombine = FALSE;
6728#endif
6729 ireg_maxcol = 0;
6730 return (nfa_regexec_both(line, col) != 0);
6731}
6732#endif
6733
6734
6735/*
6736 * Match a regexp against multiple lines.
6737 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6738 * Uses curbuf for line count and 'iskeyword'.
6739 *
6740 * Return zero if there is no match. Return number of lines contained in the
6741 * match otherwise.
6742 *
6743 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6744 *
6745 * ! Also NOTE : match may actually be in another line. e.g.:
6746 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6747 *
6748 * +-------------------------+
6749 * |a |
6750 * |b |
6751 * |c |
6752 * | |
6753 * +-------------------------+
6754 *
6755 * then nfa_regexec_multi() returns 3. while the original
6756 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6757 *
6758 * FIXME if this behavior is not compatible.
6759 */
6760 static long
6761nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6762 regmmatch_T *rmp;
6763 win_T *win; /* window in which to search or NULL */
6764 buf_T *buf; /* buffer in which to search */
6765 linenr_T lnum; /* nr of line to start looking for match */
6766 colnr_T col; /* column to start looking for match */
6767 proftime_T *tm UNUSED; /* timeout limit or NULL */
6768{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006769 reg_match = NULL;
6770 reg_mmatch = rmp;
6771 reg_buf = buf;
6772 reg_win = win;
6773 reg_firstlnum = lnum;
6774 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6775 reg_line_lbr = FALSE;
6776 ireg_ic = rmp->rmm_ic;
6777#ifdef FEAT_MBYTE
6778 ireg_icombine = FALSE;
6779#endif
6780 ireg_maxcol = rmp->rmm_maxcol;
6781
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006782 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006783}
6784
6785#ifdef DEBUG
6786# undef ENABLE_LOG
6787#endif