blob: 5ad7a07158ea55f5072a6384d842a101bfa151cf [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 Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200277static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200278static int nfa_regatom __ARGS((void));
279static int nfa_regpiece __ARGS((void));
280static int nfa_regconcat __ARGS((void));
281static int nfa_regbranch __ARGS((void));
282static int nfa_reg __ARGS((int paren));
283#ifdef DEBUG
284static void nfa_set_code __ARGS((int c));
285static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200286static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
287static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200288static void nfa_dump __ARGS((nfa_regprog_T *prog));
289#endif
290static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200291static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200293static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200294static int check_char_class __ARGS((int class, int c));
295static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200296static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
297static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200298static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200299static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
301static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200302static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
304static 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 +0200305static int match_follows __ARGS((nfa_state_T *startstate, int depth));
306static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307
308/* helper functions used when doing re2post() ... regatom() parsing */
309#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200310 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311 return FAIL; \
312 *post_ptr++ = c; \
313 } while (0)
314
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200315/*
316 * Initialize internal variables before NFA compilation.
317 * Return OK on success, FAIL otherwise.
318 */
319 static int
320nfa_regcomp_start(expr, re_flags)
321 char_u *expr;
322 int re_flags; /* see vim_regcomp() */
323{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200324 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200325 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326
327 nstate = 0;
328 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200329 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200330 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200331
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200332 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200333 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200334 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200335
336 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200337 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339 post_start = (int *)lalloc(postfix_size, TRUE);
340 if (post_start == NULL)
341 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200342 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200343 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200345 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200347 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200348 regcomp_start(expr, re_flags);
349
350 return OK;
351}
352
353/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200354 * Figure out if the NFA state list starts with an anchor, must match at start
355 * of the line.
356 */
357 static int
358nfa_get_reganch(start, depth)
359 nfa_state_T *start;
360 int depth;
361{
362 nfa_state_T *p = start;
363
364 if (depth > 4)
365 return 0;
366
367 while (p != NULL)
368 {
369 switch (p->c)
370 {
371 case NFA_BOL:
372 case NFA_BOF:
373 return 1; /* yes! */
374
375 case NFA_ZSTART:
376 case NFA_ZEND:
377 case NFA_CURSOR:
378 case NFA_VISUAL:
379
380 case NFA_MOPEN:
381 case NFA_MOPEN1:
382 case NFA_MOPEN2:
383 case NFA_MOPEN3:
384 case NFA_MOPEN4:
385 case NFA_MOPEN5:
386 case NFA_MOPEN6:
387 case NFA_MOPEN7:
388 case NFA_MOPEN8:
389 case NFA_MOPEN9:
390 case NFA_NOPEN:
391#ifdef FEAT_SYN_HL
392 case NFA_ZOPEN:
393 case NFA_ZOPEN1:
394 case NFA_ZOPEN2:
395 case NFA_ZOPEN3:
396 case NFA_ZOPEN4:
397 case NFA_ZOPEN5:
398 case NFA_ZOPEN6:
399 case NFA_ZOPEN7:
400 case NFA_ZOPEN8:
401 case NFA_ZOPEN9:
402#endif
403 p = p->out;
404 break;
405
406 case NFA_SPLIT:
407 return nfa_get_reganch(p->out, depth + 1)
408 && nfa_get_reganch(p->out1, depth + 1);
409
410 default:
411 return 0; /* noooo */
412 }
413 }
414 return 0;
415}
416
417/*
418 * Figure out if the NFA state list starts with a character which must match
419 * at start of the match.
420 */
421 static int
422nfa_get_regstart(start, depth)
423 nfa_state_T *start;
424 int depth;
425{
426 nfa_state_T *p = start;
427
428 if (depth > 4)
429 return 0;
430
431 while (p != NULL)
432 {
433 switch (p->c)
434 {
435 /* all kinds of zero-width matches */
436 case NFA_BOL:
437 case NFA_BOF:
438 case NFA_BOW:
439 case NFA_EOW:
440 case NFA_ZSTART:
441 case NFA_ZEND:
442 case NFA_CURSOR:
443 case NFA_VISUAL:
444 case NFA_LNUM:
445 case NFA_LNUM_GT:
446 case NFA_LNUM_LT:
447 case NFA_COL:
448 case NFA_COL_GT:
449 case NFA_COL_LT:
450 case NFA_VCOL:
451 case NFA_VCOL_GT:
452 case NFA_VCOL_LT:
453 case NFA_MARK:
454 case NFA_MARK_GT:
455 case NFA_MARK_LT:
456
457 case NFA_MOPEN:
458 case NFA_MOPEN1:
459 case NFA_MOPEN2:
460 case NFA_MOPEN3:
461 case NFA_MOPEN4:
462 case NFA_MOPEN5:
463 case NFA_MOPEN6:
464 case NFA_MOPEN7:
465 case NFA_MOPEN8:
466 case NFA_MOPEN9:
467 case NFA_NOPEN:
468#ifdef FEAT_SYN_HL
469 case NFA_ZOPEN:
470 case NFA_ZOPEN1:
471 case NFA_ZOPEN2:
472 case NFA_ZOPEN3:
473 case NFA_ZOPEN4:
474 case NFA_ZOPEN5:
475 case NFA_ZOPEN6:
476 case NFA_ZOPEN7:
477 case NFA_ZOPEN8:
478 case NFA_ZOPEN9:
479#endif
480 p = p->out;
481 break;
482
483 case NFA_SPLIT:
484 {
485 int c1 = nfa_get_regstart(p->out, depth + 1);
486 int c2 = nfa_get_regstart(p->out1, depth + 1);
487
488 if (c1 == c2)
489 return c1; /* yes! */
490 return 0;
491 }
492
493 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200494 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200495 return p->c; /* yes! */
496 return 0;
497 }
498 }
499 return 0;
500}
501
502/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200503 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200504 * else. If so return a string in allocated memory with what must match after
505 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200506 */
507 static char_u *
508nfa_get_match_text(start)
509 nfa_state_T *start;
510{
511 nfa_state_T *p = start;
512 int len = 0;
513 char_u *ret;
514 char_u *s;
515
516 if (p->c != NFA_MOPEN)
517 return NULL; /* just in case */
518 p = p->out;
519 while (p->c > 0)
520 {
521 len += MB_CHAR2LEN(p->c);
522 p = p->out;
523 }
524 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
525 return NULL;
526
527 ret = alloc(len);
528 if (ret != NULL)
529 {
530 len = 0;
531 p = start->out->out; /* skip first char, it goes into regstart */
532 s = ret;
533 while (p->c > 0)
534 {
535#ifdef FEAT_MBYTE
536 if (has_mbyte)
537 s += (*mb_char2bytes)(p->c, s);
538 else
539#endif
540 *s++ = p->c;
541 p = p->out;
542 }
543 *s = NUL;
544 }
545 return ret;
546}
547
548/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200549 * Allocate more space for post_start. Called when
550 * running above the estimated number of states.
551 */
552 static int
553realloc_post_list()
554{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200555 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200556 int new_max = nstate_max + 1000;
557 int *new_start;
558 int *old_start;
559
560 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
561 if (new_start == NULL)
562 return FAIL;
563 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200564 old_start = post_start;
565 post_start = new_start;
566 post_ptr = new_start + (post_ptr - old_start);
567 post_end = post_start + new_max;
568 vim_free(old_start);
569 return OK;
570}
571
572/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200573 * Search between "start" and "end" and try to recognize a
574 * character class in expanded form. For example [0-9].
575 * On success, return the id the character class to be emitted.
576 * On failure, return 0 (=FAIL)
577 * Start points to the first char of the range, while end should point
578 * to the closing brace.
579 */
580 static int
581nfa_recognize_char_class(start, end, extra_newl)
582 char_u *start;
583 char_u *end;
584 int extra_newl;
585{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200586# define CLASS_not 0x80
587# define CLASS_af 0x40
588# define CLASS_AF 0x20
589# define CLASS_az 0x10
590# define CLASS_AZ 0x08
591# define CLASS_o7 0x04
592# define CLASS_o9 0x02
593# define CLASS_underscore 0x01
594
595 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200596 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200597 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598
599 if (extra_newl == TRUE)
600 newl = TRUE;
601
602 if (*end != ']')
603 return FAIL;
604 p = start;
605 if (*p == '^')
606 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200607 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200608 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609 }
610
611 while (p < end)
612 {
613 if (p + 2 < end && *(p + 1) == '-')
614 {
615 switch (*p)
616 {
617 case '0':
618 if (*(p + 2) == '9')
619 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200620 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200621 break;
622 }
623 else
624 if (*(p + 2) == '7')
625 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200626 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200627 break;
628 }
629 case 'a':
630 if (*(p + 2) == 'z')
631 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200632 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200633 break;
634 }
635 else
636 if (*(p + 2) == 'f')
637 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200638 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200639 break;
640 }
641 case 'A':
642 if (*(p + 2) == 'Z')
643 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200644 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200645 break;
646 }
647 else
648 if (*(p + 2) == 'F')
649 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200650 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200651 break;
652 }
653 /* FALLTHROUGH */
654 default:
655 return FAIL;
656 }
657 p += 3;
658 }
659 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
660 {
661 newl = TRUE;
662 p += 2;
663 }
664 else if (*p == '_')
665 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200666 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200667 p ++;
668 }
669 else if (*p == '\n')
670 {
671 newl = TRUE;
672 p ++;
673 }
674 else
675 return FAIL;
676 } /* while (p < end) */
677
678 if (p != end)
679 return FAIL;
680
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200681 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200682 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200683
684 switch (config)
685 {
686 case CLASS_o9:
687 return extra_newl + NFA_DIGIT;
688 case CLASS_not | CLASS_o9:
689 return extra_newl + NFA_NDIGIT;
690 case CLASS_af | CLASS_AF | CLASS_o9:
691 return extra_newl + NFA_HEX;
692 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
693 return extra_newl + NFA_NHEX;
694 case CLASS_o7:
695 return extra_newl + NFA_OCTAL;
696 case CLASS_not | CLASS_o7:
697 return extra_newl + NFA_NOCTAL;
698 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
699 return extra_newl + NFA_WORD;
700 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
701 return extra_newl + NFA_NWORD;
702 case CLASS_az | CLASS_AZ | CLASS_underscore:
703 return extra_newl + NFA_HEAD;
704 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
705 return extra_newl + NFA_NHEAD;
706 case CLASS_az | CLASS_AZ:
707 return extra_newl + NFA_ALPHA;
708 case CLASS_not | CLASS_az | CLASS_AZ:
709 return extra_newl + NFA_NALPHA;
710 case CLASS_az:
711 return extra_newl + NFA_LOWER;
712 case CLASS_not | CLASS_az:
713 return extra_newl + NFA_NLOWER;
714 case CLASS_AZ:
715 return extra_newl + NFA_UPPER;
716 case CLASS_not | CLASS_AZ:
717 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200718 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200719 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720}
721
722/*
723 * Produce the bytes for equivalence class "c".
724 * Currently only handles latin1, latin9 and utf-8.
725 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
726 * equivalent to 'a OR b OR c'
727 *
728 * NOTE! When changing this function, also update reg_equi_class()
729 */
730 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200731nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200733{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200734#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735
736#ifdef FEAT_MBYTE
737 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
738 || STRCMP(p_enc, "iso-8859-15") == 0)
739#endif
740 {
741 switch (c)
742 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200743 case 'A': case 0300: case 0301: case 0302:
744 case 0303: case 0304: case 0305:
745 EMIT2('A'); EMIT2(0300); EMIT2(0301);
746 EMIT2(0302); EMIT2(0303); EMIT2(0304);
747 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200748 return OK;
749
Bram Moolenaar417bad22013-06-07 14:08:30 +0200750 case 'C': case 0307:
751 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200752 return OK;
753
Bram Moolenaar417bad22013-06-07 14:08:30 +0200754 case 'E': case 0310: case 0311: case 0312: case 0313:
755 EMIT2('E'); EMIT2(0310); EMIT2(0311);
756 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200757 return OK;
758
Bram Moolenaar417bad22013-06-07 14:08:30 +0200759 case 'I': case 0314: case 0315: case 0316: case 0317:
760 EMIT2('I'); EMIT2(0314); EMIT2(0315);
761 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200762 return OK;
763
Bram Moolenaar417bad22013-06-07 14:08:30 +0200764 case 'N': case 0321:
765 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200766 return OK;
767
Bram Moolenaar417bad22013-06-07 14:08:30 +0200768 case 'O': case 0322: case 0323: case 0324: case 0325:
769 case 0326:
770 EMIT2('O'); EMIT2(0322); EMIT2(0323);
771 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200772 return OK;
773
Bram Moolenaar417bad22013-06-07 14:08:30 +0200774 case 'U': case 0331: case 0332: case 0333: case 0334:
775 EMIT2('U'); EMIT2(0331); EMIT2(0332);
776 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200777 return OK;
778
Bram Moolenaar417bad22013-06-07 14:08:30 +0200779 case 'Y': case 0335:
780 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200781 return OK;
782
Bram Moolenaar417bad22013-06-07 14:08:30 +0200783 case 'a': case 0340: case 0341: case 0342:
784 case 0343: case 0344: case 0345:
785 EMIT2('a'); EMIT2(0340); EMIT2(0341);
786 EMIT2(0342); EMIT2(0343); EMIT2(0344);
787 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 return OK;
789
Bram Moolenaar417bad22013-06-07 14:08:30 +0200790 case 'c': case 0347:
791 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200792 return OK;
793
Bram Moolenaar417bad22013-06-07 14:08:30 +0200794 case 'e': case 0350: case 0351: case 0352: case 0353:
795 EMIT2('e'); EMIT2(0350); EMIT2(0351);
796 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200797 return OK;
798
Bram Moolenaar417bad22013-06-07 14:08:30 +0200799 case 'i': case 0354: case 0355: case 0356: case 0357:
800 EMIT2('i'); EMIT2(0354); EMIT2(0355);
801 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200802 return OK;
803
Bram Moolenaar417bad22013-06-07 14:08:30 +0200804 case 'n': case 0361:
805 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200806 return OK;
807
Bram Moolenaar417bad22013-06-07 14:08:30 +0200808 case 'o': case 0362: case 0363: case 0364: case 0365:
809 case 0366:
810 EMIT2('o'); EMIT2(0362); EMIT2(0363);
811 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 return OK;
813
Bram Moolenaar417bad22013-06-07 14:08:30 +0200814 case 'u': case 0371: case 0372: case 0373: case 0374:
815 EMIT2('u'); EMIT2(0371); EMIT2(0372);
816 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 return OK;
818
Bram Moolenaar417bad22013-06-07 14:08:30 +0200819 case 'y': case 0375: case 0377:
820 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 return OK;
822
823 default:
824 return FAIL;
825 }
826 }
827
828 EMIT(c);
829 return OK;
830#undef EMIT2
831}
832
833/*
834 * Code to parse regular expression.
835 *
836 * We try to reuse parsing functions in regexp.c to
837 * minimize surprise and keep the syntax consistent.
838 */
839
840/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200841 * Parse the lowest level.
842 *
843 * An atom can be one of a long list of items. Many atoms match one character
844 * in the text. It is often an ordinary character or a character class.
845 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
846 * is only for syntax highlighting.
847 *
848 * atom ::= ordinary-atom
849 * or \( pattern \)
850 * or \%( pattern \)
851 * or \z( pattern \)
852 */
853 static int
854nfa_regatom()
855{
856 int c;
857 int charclass;
858 int equiclass;
859 int collclass;
860 int got_coll_char;
861 char_u *p;
862 char_u *endp;
863#ifdef FEAT_MBYTE
864 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200865#endif
866 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867 int emit_range;
868 int negated;
869 int result;
870 int startc = -1;
871 int endc = -1;
872 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200873
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200874 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 switch (c)
876 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200877 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200878 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
879
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 case Magic('^'):
881 EMIT(NFA_BOL);
882 break;
883
884 case Magic('$'):
885 EMIT(NFA_EOL);
886#if defined(FEAT_SYN_HL) || defined(PROTO)
887 had_eol = TRUE;
888#endif
889 break;
890
891 case Magic('<'):
892 EMIT(NFA_BOW);
893 break;
894
895 case Magic('>'):
896 EMIT(NFA_EOW);
897 break;
898
899 case Magic('_'):
900 c = no_Magic(getchr());
901 if (c == '^') /* "\_^" is start-of-line */
902 {
903 EMIT(NFA_BOL);
904 break;
905 }
906 if (c == '$') /* "\_$" is end-of-line */
907 {
908 EMIT(NFA_EOL);
909#if defined(FEAT_SYN_HL) || defined(PROTO)
910 had_eol = TRUE;
911#endif
912 break;
913 }
914
915 extra = ADD_NL;
916
917 /* "\_[" is collection plus newline */
918 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200919 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200920
921 /* "\_x" is character class plus newline */
922 /*FALLTHROUGH*/
923
924 /*
925 * Character classes.
926 */
927 case Magic('.'):
928 case Magic('i'):
929 case Magic('I'):
930 case Magic('k'):
931 case Magic('K'):
932 case Magic('f'):
933 case Magic('F'):
934 case Magic('p'):
935 case Magic('P'):
936 case Magic('s'):
937 case Magic('S'):
938 case Magic('d'):
939 case Magic('D'):
940 case Magic('x'):
941 case Magic('X'):
942 case Magic('o'):
943 case Magic('O'):
944 case Magic('w'):
945 case Magic('W'):
946 case Magic('h'):
947 case Magic('H'):
948 case Magic('a'):
949 case Magic('A'):
950 case Magic('l'):
951 case Magic('L'):
952 case Magic('u'):
953 case Magic('U'):
954 p = vim_strchr(classchars, no_Magic(c));
955 if (p == NULL)
956 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200957 EMSGN("INTERNAL: Unknown character class char: %ld", c);
958 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200959 }
960#ifdef FEAT_MBYTE
961 /* When '.' is followed by a composing char ignore the dot, so that
962 * the composing char is matched here. */
963 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
964 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200965 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200966 c = getchr();
967 goto nfa_do_multibyte;
968 }
969#endif
970 EMIT(nfa_classcodes[p - classchars]);
971 if (extra == ADD_NL)
972 {
973 EMIT(NFA_NEWL);
974 EMIT(NFA_OR);
975 regflags |= RF_HASNL;
976 }
977 break;
978
979 case Magic('n'):
980 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200981 /* In a string "\n" matches a newline character. */
982 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200983 else
984 {
985 /* In buffer text "\n" matches the end of a line. */
986 EMIT(NFA_NEWL);
987 regflags |= RF_HASNL;
988 }
989 break;
990
991 case Magic('('):
992 if (nfa_reg(REG_PAREN) == FAIL)
993 return FAIL; /* cascaded error */
994 break;
995
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200996 case Magic('|'):
997 case Magic('&'):
998 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200999 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return FAIL;
1001
1002 case Magic('='):
1003 case Magic('?'):
1004 case Magic('+'):
1005 case Magic('@'):
1006 case Magic('*'):
1007 case Magic('{'):
1008 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001009 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001010 return FAIL;
1011
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001012 case Magic('~'):
1013 {
1014 char_u *lp;
1015
1016 /* Previous substitute pattern.
1017 * Generated as "\%(pattern\)". */
1018 if (reg_prev_sub == NULL)
1019 {
1020 EMSG(_(e_nopresub));
1021 return FAIL;
1022 }
1023 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1024 {
1025 EMIT(PTR2CHAR(lp));
1026 if (lp != reg_prev_sub)
1027 EMIT(NFA_CONCAT);
1028 }
1029 EMIT(NFA_NOPEN);
1030 break;
1031 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001032
Bram Moolenaar428e9872013-05-30 17:05:39 +02001033 case Magic('1'):
1034 case Magic('2'):
1035 case Magic('3'):
1036 case Magic('4'):
1037 case Magic('5'):
1038 case Magic('6'):
1039 case Magic('7'):
1040 case Magic('8'):
1041 case Magic('9'):
1042 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1043 nfa_has_backref = TRUE;
1044 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001045
1046 case Magic('z'):
1047 c = no_Magic(getchr());
1048 switch (c)
1049 {
1050 case 's':
1051 EMIT(NFA_ZSTART);
1052 break;
1053 case 'e':
1054 EMIT(NFA_ZEND);
1055 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001056 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001057#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001058 case '1':
1059 case '2':
1060 case '3':
1061 case '4':
1062 case '5':
1063 case '6':
1064 case '7':
1065 case '8':
1066 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001067 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001068 if (reg_do_extmatch != REX_USE)
1069 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001070 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1071 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001072 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001073 re_has_z = REX_USE;
1074 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001075 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001076 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001077 if (reg_do_extmatch != REX_SET)
1078 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001079 if (nfa_reg(REG_ZPAREN) == FAIL)
1080 return FAIL; /* cascaded error */
1081 re_has_z = REX_SET;
1082 break;
1083#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001084 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001085 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 no_Magic(c));
1087 return FAIL;
1088 }
1089 break;
1090
1091 case Magic('%'):
1092 c = no_Magic(getchr());
1093 switch (c)
1094 {
1095 /* () without a back reference */
1096 case '(':
1097 if (nfa_reg(REG_NPAREN) == FAIL)
1098 return FAIL;
1099 EMIT(NFA_NOPEN);
1100 break;
1101
1102 case 'd': /* %d123 decimal */
1103 case 'o': /* %o123 octal */
1104 case 'x': /* %xab hex 2 */
1105 case 'u': /* %uabcd hex 4 */
1106 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001107 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001108 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001109
Bram Moolenaar47196582013-05-25 22:04:23 +02001110 switch (c)
1111 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001112 case 'd': nr = getdecchrs(); break;
1113 case 'o': nr = getoctchrs(); break;
1114 case 'x': nr = gethexchrs(2); break;
1115 case 'u': nr = gethexchrs(4); break;
1116 case 'U': nr = gethexchrs(8); break;
1117 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001118 }
1119
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001120 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001121 EMSG2_RET_FAIL(
1122 _("E678: Invalid character after %s%%[dxouU]"),
1123 reg_magic == MAGIC_ALL);
1124 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001125 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001126 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001127 break;
1128
1129 /* Catch \%^ and \%$ regardless of where they appear in the
1130 * pattern -- regardless of whether or not it makes sense. */
1131 case '^':
1132 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001133 break;
1134
1135 case '$':
1136 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001137 break;
1138
1139 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001140 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 break;
1142
1143 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001144 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001145 break;
1146
1147 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001148 {
1149 int n;
1150
1151 /* \%[abc] */
1152 for (n = 0; (c = getchr()) != ']'; ++n)
1153 {
1154 if (c == NUL)
1155 EMSG2_RET_FAIL(_(e_missing_sb),
1156 reg_magic == MAGIC_ALL);
1157 EMIT(c);
1158 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001159 if (n == 0)
1160 EMSG2_RET_FAIL(_(e_empty_sb),
1161 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001162 EMIT(NFA_OPT_CHARS);
1163 EMIT(n);
1164 break;
1165 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001166
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001167 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001168 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001169 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001170 int cmp = c;
1171
1172 if (c == '<' || c == '>')
1173 c = getchr();
1174 while (VIM_ISDIGIT(c))
1175 {
1176 n = n * 10 + (c - '0');
1177 c = getchr();
1178 }
1179 if (c == 'l' || c == 'c' || c == 'v')
1180 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001181 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001182 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001183 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001184 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001185 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001186 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001187 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001188 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001189 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001190 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001191 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001192 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001193 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001194 break;
1195 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001196 else if (c == '\'' && n == 0)
1197 {
1198 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001199 EMIT(cmp == '<' ? NFA_MARK_LT :
1200 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001201 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001202 break;
1203 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001204 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001205 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1206 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001207 return FAIL;
1208 }
1209 break;
1210
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001211 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001212collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001214 * [abc] uses NFA_START_COLL - NFA_END_COLL
1215 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1216 * Each character is produced as a regular state, using
1217 * NFA_CONCAT to bind them together.
1218 * Besides normal characters there can be:
1219 * - character classes NFA_CLASS_*
1220 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 */
1222
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 p = regparse;
1224 endp = skip_anyof(p);
1225 if (*endp == ']')
1226 {
1227 /*
1228 * Try to reverse engineer character classes. For example,
1229 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1230 * and perform the necessary substitutions in the NFA.
1231 */
1232 result = nfa_recognize_char_class(regparse, endp,
1233 extra == ADD_NL);
1234 if (result != FAIL)
1235 {
1236 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1237 EMIT(result);
1238 else /* must be char class + newline */
1239 {
1240 EMIT(result - ADD_NL);
1241 EMIT(NFA_NEWL);
1242 EMIT(NFA_OR);
1243 }
1244 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001245 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246 return OK;
1247 }
1248 /*
1249 * Failed to recognize a character class. Use the simple
1250 * version that turns [abc] into 'a' OR 'b' OR 'c'
1251 */
1252 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254 if (*regparse == '^') /* negated range */
1255 {
1256 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001257 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001258 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001260 else
1261 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262 if (*regparse == '-')
1263 {
1264 startc = '-';
1265 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001266 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001267 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001268 }
1269 /* Emit the OR branches for each character in the [] */
1270 emit_range = FALSE;
1271 while (regparse < endp)
1272 {
1273 oldstartc = startc;
1274 startc = -1;
1275 got_coll_char = FALSE;
1276 if (*regparse == '[')
1277 {
1278 /* Check for [: :], [= =], [. .] */
1279 equiclass = collclass = 0;
1280 charclass = get_char_class(&regparse);
1281 if (charclass == CLASS_NONE)
1282 {
1283 equiclass = get_equi_class(&regparse);
1284 if (equiclass == 0)
1285 collclass = get_coll_element(&regparse);
1286 }
1287
1288 /* Character class like [:alpha:] */
1289 if (charclass != CLASS_NONE)
1290 {
1291 switch (charclass)
1292 {
1293 case CLASS_ALNUM:
1294 EMIT(NFA_CLASS_ALNUM);
1295 break;
1296 case CLASS_ALPHA:
1297 EMIT(NFA_CLASS_ALPHA);
1298 break;
1299 case CLASS_BLANK:
1300 EMIT(NFA_CLASS_BLANK);
1301 break;
1302 case CLASS_CNTRL:
1303 EMIT(NFA_CLASS_CNTRL);
1304 break;
1305 case CLASS_DIGIT:
1306 EMIT(NFA_CLASS_DIGIT);
1307 break;
1308 case CLASS_GRAPH:
1309 EMIT(NFA_CLASS_GRAPH);
1310 break;
1311 case CLASS_LOWER:
1312 EMIT(NFA_CLASS_LOWER);
1313 break;
1314 case CLASS_PRINT:
1315 EMIT(NFA_CLASS_PRINT);
1316 break;
1317 case CLASS_PUNCT:
1318 EMIT(NFA_CLASS_PUNCT);
1319 break;
1320 case CLASS_SPACE:
1321 EMIT(NFA_CLASS_SPACE);
1322 break;
1323 case CLASS_UPPER:
1324 EMIT(NFA_CLASS_UPPER);
1325 break;
1326 case CLASS_XDIGIT:
1327 EMIT(NFA_CLASS_XDIGIT);
1328 break;
1329 case CLASS_TAB:
1330 EMIT(NFA_CLASS_TAB);
1331 break;
1332 case CLASS_RETURN:
1333 EMIT(NFA_CLASS_RETURN);
1334 break;
1335 case CLASS_BACKSPACE:
1336 EMIT(NFA_CLASS_BACKSPACE);
1337 break;
1338 case CLASS_ESCAPE:
1339 EMIT(NFA_CLASS_ESCAPE);
1340 break;
1341 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001342 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 continue;
1344 }
1345 /* Try equivalence class [=a=] and the like */
1346 if (equiclass != 0)
1347 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001348 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001349 if (result == FAIL)
1350 {
1351 /* should never happen */
1352 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1353 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001354 continue;
1355 }
1356 /* Try collating class like [. .] */
1357 if (collclass != 0)
1358 {
1359 startc = collclass; /* allow [.a.]-x as a range */
1360 /* Will emit the proper atom at the end of the
1361 * while loop. */
1362 }
1363 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001364 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1365 * start character. */
1366 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001367 {
1368 emit_range = TRUE;
1369 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001370 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001371 continue; /* reading the end of the range */
1372 }
1373
1374 /* Now handle simple and escaped characters.
1375 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1376 * accepts "\t", "\e", etc., but only when the 'l' flag in
1377 * 'cpoptions' is not included.
1378 * Posix doesn't recognize backslash at all.
1379 */
1380 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001381 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001382 && regparse + 1 <= endp
1383 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001384 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385 && vim_strchr(REGEXP_ABBR, regparse[1])
1386 != NULL)
1387 )
1388 )
1389 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001390 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001391
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001392 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001393 startc = reg_string ? NL : NFA_NEWL;
1394 else
1395 if (*regparse == 'd'
1396 || *regparse == 'o'
1397 || *regparse == 'x'
1398 || *regparse == 'u'
1399 || *regparse == 'U'
1400 )
1401 {
1402 /* TODO(RE) This needs more testing */
1403 startc = coll_get_char();
1404 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001405 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 }
1407 else
1408 {
1409 /* \r,\t,\e,\b */
1410 startc = backslash_trans(*regparse);
1411 }
1412 }
1413
1414 /* Normal printable char */
1415 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001416 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417
1418 /* Previous char was '-', so this char is end of range. */
1419 if (emit_range)
1420 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001421 endc = startc;
1422 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423 if (startc > endc)
1424 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001425
1426 if (endc > startc + 2)
1427 {
1428 /* Emit a range instead of the sequence of
1429 * individual characters. */
1430 if (startc == 0)
1431 /* \x00 is translated to \x0a, start at \x01. */
1432 EMIT(1);
1433 else
1434 --post_ptr; /* remove NFA_CONCAT */
1435 EMIT(endc);
1436 EMIT(NFA_RANGE);
1437 EMIT(NFA_CONCAT);
1438 }
1439 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001441 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 || (*mb_char2len)(endc) > 1))
1443 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001444 /* Emit the characters in the range.
1445 * "startc" was already emitted, so skip it.
1446 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 for (c = startc + 1; c <= endc; c++)
1448 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001449 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001450 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001451 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 }
1453 else
1454#endif
1455 {
1456#ifdef EBCDIC
1457 int alpha_only = FALSE;
1458
1459 /* for alphabetical range skip the gaps
1460 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1461 if (isalpha(startc) && isalpha(endc))
1462 alpha_only = TRUE;
1463#endif
1464 /* Emit the range. "startc" was already emitted, so
1465 * skip it. */
1466 for (c = startc + 1; c <= endc; c++)
1467#ifdef EBCDIC
1468 if (!alpha_only || isalpha(startc))
1469#endif
1470 {
1471 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001472 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001473 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001475 emit_range = FALSE;
1476 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 }
1478 else
1479 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001480 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001481 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001482 * Normally, simply emit startc. But if we get char
1483 * code=0 from a collating char, then replace it with
1484 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001486 * the backtracking engine. */
1487 if (startc == NFA_NEWL)
1488 {
1489 /* Line break can't be matched as part of the
1490 * collection, add an OR below. But not for negated
1491 * range. */
1492 if (!negated)
1493 extra = ADD_NL;
1494 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001496 {
1497 if (got_coll_char == TRUE && startc == 0)
1498 EMIT(0x0a);
1499 else
1500 EMIT(startc);
1501 EMIT(NFA_CONCAT);
1502 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001503 }
1504
Bram Moolenaar51a29832013-05-28 22:30:35 +02001505 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001506 } /* while (p < endp) */
1507
Bram Moolenaar51a29832013-05-28 22:30:35 +02001508 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 if (*regparse == '-') /* if last, '-' is just a char */
1510 {
1511 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001512 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001513 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001514 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001515
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516 /* skip the trailing ] */
1517 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001518 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001519
1520 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001522 EMIT(NFA_END_NEG_COLL);
1523 else
1524 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001525
1526 /* \_[] also matches \n but it's not negated */
1527 if (extra == ADD_NL)
1528 {
1529 EMIT(reg_string ? NL : NFA_NEWL);
1530 EMIT(NFA_OR);
1531 }
1532
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001533 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001534 } /* if exists closing ] */
1535
1536 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001538 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540 default:
1541 {
1542#ifdef FEAT_MBYTE
1543 int plen;
1544
1545nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001546 /* plen is length of current char with composing chars */
1547 if (enc_utf8 && ((*mb_char2len)(c)
1548 != (plen = (*mb_ptr2len)(old_regparse))
1549 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001551 int i = 0;
1552
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001553 /* A base character plus composing characters, or just one
1554 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001555 * This requires creating a separate atom as if enclosing
1556 * the characters in (), where NFA_COMPOSING is the ( and
1557 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001558 * building the postfix form, not the NFA itself;
1559 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001560 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001561 for (;;)
1562 {
1563 EMIT(c);
1564 if (i > 0)
1565 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001566 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001567 break;
1568 c = utf_ptr2char(old_regparse + i);
1569 }
1570 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001571 regparse = old_regparse + plen;
1572 }
1573 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001574#endif
1575 {
1576 c = no_Magic(c);
1577 EMIT(c);
1578 }
1579 return OK;
1580 }
1581 }
1582
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001583 return OK;
1584}
1585
1586/*
1587 * Parse something followed by possible [*+=].
1588 *
1589 * A piece is an atom, possibly followed by a multi, an indication of how many
1590 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1591 * characters: "", "a", "aa", etc.
1592 *
1593 * piece ::= atom
1594 * or atom multi
1595 */
1596 static int
1597nfa_regpiece()
1598{
1599 int i;
1600 int op;
1601 int ret;
1602 long minval, maxval;
1603 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001604 parse_state_T old_state;
1605 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001606 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001607 int old_post_pos;
1608 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001609 int quest;
1610
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001611 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1612 * next. */
1613 save_parse_state(&old_state);
1614
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001615 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001616 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001617
1618 ret = nfa_regatom();
1619 if (ret == FAIL)
1620 return FAIL; /* cascaded error */
1621
1622 op = peekchr();
1623 if (re_multi_type(op) == NOT_MULTI)
1624 return OK;
1625
1626 skipchr();
1627 switch (op)
1628 {
1629 case Magic('*'):
1630 EMIT(NFA_STAR);
1631 break;
1632
1633 case Magic('+'):
1634 /*
1635 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1636 * first and only submatch would be "aaa". But the backtracking
1637 * engine interprets the plus as "try matching one more time", and
1638 * a* matches a second time at the end of the input, the empty
1639 * string.
1640 * The submatch will the empty string.
1641 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001642 * In order to be consistent with the old engine, we replace
1643 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001644 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001645 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001646 curchr = -1;
1647 if (nfa_regatom() == FAIL)
1648 return FAIL;
1649 EMIT(NFA_STAR);
1650 EMIT(NFA_CONCAT);
1651 skipchr(); /* skip the \+ */
1652 break;
1653
1654 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001655 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001656 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001657 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 switch(op)
1659 {
1660 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001661 /* \@= */
1662 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001664 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001665 /* \@! */
1666 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001667 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001669 op = no_Magic(getchr());
1670 if (op == '=')
1671 /* \@<= */
1672 i = NFA_PREV_ATOM_JUST_BEFORE;
1673 else if (op == '!')
1674 /* \@<! */
1675 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1676 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001678 /* \@> */
1679 i = NFA_PREV_ATOM_LIKE_PATTERN;
1680 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001682 if (i == 0)
1683 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001684 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1685 return FAIL;
1686 }
1687 EMIT(i);
1688 if (i == NFA_PREV_ATOM_JUST_BEFORE
1689 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1690 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 break;
1692
1693 case Magic('?'):
1694 case Magic('='):
1695 EMIT(NFA_QUEST);
1696 break;
1697
1698 case Magic('{'):
1699 /* a{2,5} will expand to 'aaa?a?a?'
1700 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1701 * version of '?'
1702 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1703 * parenthesis have the same id
1704 */
1705
1706 greedy = TRUE;
1707 c2 = peekchr();
1708 if (c2 == '-' || c2 == Magic('-'))
1709 {
1710 skipchr();
1711 greedy = FALSE;
1712 }
1713 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001714 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001715
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001716 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1717 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001718 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001719 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001720 if (greedy)
1721 /* \{}, \{0,} */
1722 EMIT(NFA_STAR);
1723 else
1724 /* \{-}, \{-0,} */
1725 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001726 break;
1727 }
1728
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001729 /* Special case: x{0} or x{-0} */
1730 if (maxval == 0)
1731 {
1732 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001733 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001734 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1735 EMIT(NFA_SKIP_CHAR);
1736 return OK;
1737 }
1738
1739 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001740 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001741 /* Save parse state after the repeated atom and the \{} */
1742 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1745 for (i = 0; i < maxval; i++)
1746 {
1747 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001748 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001749 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001750 if (nfa_regatom() == FAIL)
1751 return FAIL;
1752 /* after "minval" times, atoms are optional */
1753 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001754 {
1755 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001756 {
1757 if (greedy)
1758 EMIT(NFA_STAR);
1759 else
1760 EMIT(NFA_STAR_NONGREEDY);
1761 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001762 else
1763 EMIT(quest);
1764 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001765 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001767 if (i + 1 > minval && maxval == MAX_LIMIT)
1768 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 }
1770
1771 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001772 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 curchr = -1;
1774
1775 break;
1776
1777
1778 default:
1779 break;
1780 } /* end switch */
1781
1782 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001784 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001785
1786 return OK;
1787}
1788
1789/*
1790 * Parse one or more pieces, concatenated. It matches a match for the
1791 * first piece, followed by a match for the second piece, etc. Example:
1792 * "f[0-9]b", first matches "f", then a digit and then "b".
1793 *
1794 * concat ::= piece
1795 * or piece piece
1796 * or piece piece piece
1797 * etc.
1798 */
1799 static int
1800nfa_regconcat()
1801{
1802 int cont = TRUE;
1803 int first = TRUE;
1804
1805 while (cont)
1806 {
1807 switch (peekchr())
1808 {
1809 case NUL:
1810 case Magic('|'):
1811 case Magic('&'):
1812 case Magic(')'):
1813 cont = FALSE;
1814 break;
1815
1816 case Magic('Z'):
1817#ifdef FEAT_MBYTE
1818 regflags |= RF_ICOMBINE;
1819#endif
1820 skipchr_keepstart();
1821 break;
1822 case Magic('c'):
1823 regflags |= RF_ICASE;
1824 skipchr_keepstart();
1825 break;
1826 case Magic('C'):
1827 regflags |= RF_NOICASE;
1828 skipchr_keepstart();
1829 break;
1830 case Magic('v'):
1831 reg_magic = MAGIC_ALL;
1832 skipchr_keepstart();
1833 curchr = -1;
1834 break;
1835 case Magic('m'):
1836 reg_magic = MAGIC_ON;
1837 skipchr_keepstart();
1838 curchr = -1;
1839 break;
1840 case Magic('M'):
1841 reg_magic = MAGIC_OFF;
1842 skipchr_keepstart();
1843 curchr = -1;
1844 break;
1845 case Magic('V'):
1846 reg_magic = MAGIC_NONE;
1847 skipchr_keepstart();
1848 curchr = -1;
1849 break;
1850
1851 default:
1852 if (nfa_regpiece() == FAIL)
1853 return FAIL;
1854 if (first == FALSE)
1855 EMIT(NFA_CONCAT);
1856 else
1857 first = FALSE;
1858 break;
1859 }
1860 }
1861
1862 return OK;
1863}
1864
1865/*
1866 * Parse a branch, one or more concats, separated by "\&". It matches the
1867 * last concat, but only if all the preceding concats also match at the same
1868 * position. Examples:
1869 * "foobeep\&..." matches "foo" in "foobeep".
1870 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1871 *
1872 * branch ::= concat
1873 * or concat \& concat
1874 * or concat \& concat \& concat
1875 * etc.
1876 */
1877 static int
1878nfa_regbranch()
1879{
1880 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001881 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882
Bram Moolenaar16299b52013-05-30 18:45:23 +02001883 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884
1885 /* First branch, possibly the only one */
1886 if (nfa_regconcat() == FAIL)
1887 return FAIL;
1888
1889 ch = peekchr();
1890 /* Try next concats */
1891 while (ch == Magic('&'))
1892 {
1893 skipchr();
1894 EMIT(NFA_NOPEN);
1895 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001896 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 if (nfa_regconcat() == FAIL)
1898 return FAIL;
1899 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001900 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 EMIT(NFA_SKIP_CHAR);
1902 EMIT(NFA_CONCAT);
1903 ch = peekchr();
1904 }
1905
1906 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001907 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 EMIT(NFA_SKIP_CHAR);
1909
1910 return OK;
1911}
1912
1913/*
1914 * Parse a pattern, one or more branches, separated by "\|". It matches
1915 * anything that matches one of the branches. Example: "foo\|beep" matches
1916 * "foo" and matches "beep". If more than one branch matches, the first one
1917 * is used.
1918 *
1919 * pattern ::= branch
1920 * or branch \| branch
1921 * or branch \| branch \| branch
1922 * etc.
1923 */
1924 static int
1925nfa_reg(paren)
1926 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1927{
1928 int parno = 0;
1929
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 if (paren == REG_PAREN)
1931 {
1932 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 parno = regnpar++;
1935 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001936#ifdef FEAT_SYN_HL
1937 else if (paren == REG_ZPAREN)
1938 {
1939 /* Make a ZOPEN node. */
1940 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001941 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001942 parno = regnzpar++;
1943 }
1944#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945
1946 if (nfa_regbranch() == FAIL)
1947 return FAIL; /* cascaded error */
1948
1949 while (peekchr() == Magic('|'))
1950 {
1951 skipchr();
1952 if (nfa_regbranch() == FAIL)
1953 return FAIL; /* cascaded error */
1954 EMIT(NFA_OR);
1955 }
1956
1957 /* Check for proper termination. */
1958 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1959 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 if (paren == REG_NPAREN)
1961 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1962 else
1963 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1964 }
1965 else if (paren == REG_NOPAREN && peekchr() != NUL)
1966 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001967 if (peekchr() == Magic(')'))
1968 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1969 else
1970 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1971 }
1972 /*
1973 * Here we set the flag allowing back references to this set of
1974 * parentheses.
1975 */
1976 if (paren == REG_PAREN)
1977 {
1978 had_endbrace[parno] = TRUE; /* have seen the close paren */
1979 EMIT(NFA_MOPEN + parno);
1980 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001981#ifdef FEAT_SYN_HL
1982 else if (paren == REG_ZPAREN)
1983 EMIT(NFA_ZOPEN + parno);
1984#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001985
1986 return OK;
1987}
1988
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989#ifdef DEBUG
1990static char_u code[50];
1991
1992 static void
1993nfa_set_code(c)
1994 int c;
1995{
1996 int addnl = FALSE;
1997
1998 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1999 {
2000 addnl = TRUE;
2001 c -= ADD_NL;
2002 }
2003
2004 STRCPY(code, "");
2005 switch (c)
2006 {
2007 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2008 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2009 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2010 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2011 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2012 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2013
Bram Moolenaar5714b802013-05-28 22:03:20 +02002014 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2015 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2016 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2017 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2018 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2019 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2020 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2021 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2022 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002023#ifdef FEAT_SYN_HL
2024 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2025 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2026 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2027 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2028 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2029 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2030 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2031 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2032 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2033#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002034 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2035
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002036 case NFA_PREV_ATOM_NO_WIDTH:
2037 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002038 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2039 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002040 case NFA_PREV_ATOM_JUST_BEFORE:
2041 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2042 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2043 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002044 case NFA_PREV_ATOM_LIKE_PATTERN:
2045 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2046
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002047 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2048 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002050 case NFA_START_INVISIBLE_FIRST:
2051 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002052 case NFA_START_INVISIBLE_NEG:
2053 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002054 case NFA_START_INVISIBLE_NEG_FIRST:
2055 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002056 case NFA_START_INVISIBLE_BEFORE:
2057 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002058 case NFA_START_INVISIBLE_BEFORE_FIRST:
2059 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002060 case NFA_START_INVISIBLE_BEFORE_NEG:
2061 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002062 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2063 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002064 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002066 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002067 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2070 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002071 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002073 case NFA_MOPEN:
2074 case NFA_MOPEN1:
2075 case NFA_MOPEN2:
2076 case NFA_MOPEN3:
2077 case NFA_MOPEN4:
2078 case NFA_MOPEN5:
2079 case NFA_MOPEN6:
2080 case NFA_MOPEN7:
2081 case NFA_MOPEN8:
2082 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 STRCPY(code, "NFA_MOPEN(x)");
2084 code[10] = c - NFA_MOPEN + '0';
2085 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002086 case NFA_MCLOSE:
2087 case NFA_MCLOSE1:
2088 case NFA_MCLOSE2:
2089 case NFA_MCLOSE3:
2090 case NFA_MCLOSE4:
2091 case NFA_MCLOSE5:
2092 case NFA_MCLOSE6:
2093 case NFA_MCLOSE7:
2094 case NFA_MCLOSE8:
2095 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002096 STRCPY(code, "NFA_MCLOSE(x)");
2097 code[11] = c - NFA_MCLOSE + '0';
2098 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002099#ifdef FEAT_SYN_HL
2100 case NFA_ZOPEN:
2101 case NFA_ZOPEN1:
2102 case NFA_ZOPEN2:
2103 case NFA_ZOPEN3:
2104 case NFA_ZOPEN4:
2105 case NFA_ZOPEN5:
2106 case NFA_ZOPEN6:
2107 case NFA_ZOPEN7:
2108 case NFA_ZOPEN8:
2109 case NFA_ZOPEN9:
2110 STRCPY(code, "NFA_ZOPEN(x)");
2111 code[10] = c - NFA_ZOPEN + '0';
2112 break;
2113 case NFA_ZCLOSE:
2114 case NFA_ZCLOSE1:
2115 case NFA_ZCLOSE2:
2116 case NFA_ZCLOSE3:
2117 case NFA_ZCLOSE4:
2118 case NFA_ZCLOSE5:
2119 case NFA_ZCLOSE6:
2120 case NFA_ZCLOSE7:
2121 case NFA_ZCLOSE8:
2122 case NFA_ZCLOSE9:
2123 STRCPY(code, "NFA_ZCLOSE(x)");
2124 code[11] = c - NFA_ZCLOSE + '0';
2125 break;
2126#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002127 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2128 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2129 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2130 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002131 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2132 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002133 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2134 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2135 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2136 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2137 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2138 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2139 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2140 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2141 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2142 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2143 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2144 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2145 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2146 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2147
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002148 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002149 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2150 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2151 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002152 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2153 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002154
2155 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2156 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2157 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2158 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2159 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2160 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2161 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2162
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2164 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2165 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2166 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2167 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2168 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2169 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2170 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2171 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2172 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2173 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2174 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2175 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2176 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2177 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2178 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2179
2180 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2181 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2182 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2183 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2184 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2185 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2186 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2187 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2188 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2189 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2190 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2191 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2192 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2193 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2194 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2195 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2196 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2197 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2198 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2199 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2200 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2201 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2202 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2203 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2204 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2205 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2206 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2207
2208 default:
2209 STRCPY(code, "CHAR(x)");
2210 code[5] = c;
2211 }
2212
2213 if (addnl == TRUE)
2214 STRCAT(code, " + NEWLINE ");
2215
2216}
2217
2218#ifdef ENABLE_LOG
2219static FILE *log_fd;
2220
2221/*
2222 * Print the postfix notation of the current regexp.
2223 */
2224 static void
2225nfa_postfix_dump(expr, retval)
2226 char_u *expr;
2227 int retval;
2228{
2229 int *p;
2230 FILE *f;
2231
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002232 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233 if (f != NULL)
2234 {
2235 fprintf(f, "\n-------------------------\n");
2236 if (retval == FAIL)
2237 fprintf(f, ">>> NFA engine failed ... \n");
2238 else if (retval == OK)
2239 fprintf(f, ">>> NFA engine succeeded !\n");
2240 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002241 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002242 {
2243 nfa_set_code(*p);
2244 fprintf(f, "%s, ", code);
2245 }
2246 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002247 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002248 fprintf(f, "%d ", *p);
2249 fprintf(f, "\n\n");
2250 fclose(f);
2251 }
2252}
2253
2254/*
2255 * Print the NFA starting with a root node "state".
2256 */
2257 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002258nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259 FILE *debugf;
2260 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002262 garray_T indent;
2263
2264 ga_init2(&indent, 1, 64);
2265 ga_append(&indent, '\0');
2266 nfa_print_state2(debugf, state, &indent);
2267 ga_clear(&indent);
2268}
2269
2270 static void
2271nfa_print_state2(debugf, state, indent)
2272 FILE *debugf;
2273 nfa_state_T *state;
2274 garray_T *indent;
2275{
2276 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277
2278 if (state == NULL)
2279 return;
2280
2281 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002282
2283 /* Output indent */
2284 p = (char_u *)indent->ga_data;
2285 if (indent->ga_len >= 3)
2286 {
2287 int last = indent->ga_len - 3;
2288 char_u save[2];
2289
2290 STRNCPY(save, &p[last], 2);
2291 STRNCPY(&p[last], "+-", 2);
2292 fprintf(debugf, " %s", p);
2293 STRNCPY(&p[last], save, 2);
2294 }
2295 else
2296 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002297
2298 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002299 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002300 code,
2301 state->c,
2302 abs(state->id),
2303 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 if (state->id < 0)
2305 return;
2306
2307 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002308
2309 /* grow indent for state->out */
2310 indent->ga_len -= 1;
2311 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002312 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002313 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002314 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002315 ga_append(indent, '\0');
2316
2317 nfa_print_state2(debugf, state->out, indent);
2318
2319 /* replace last part of indent for state->out1 */
2320 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002321 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002322 ga_append(indent, '\0');
2323
2324 nfa_print_state2(debugf, state->out1, indent);
2325
2326 /* shrink indent */
2327 indent->ga_len -= 3;
2328 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329}
2330
2331/*
2332 * Print the NFA state machine.
2333 */
2334 static void
2335nfa_dump(prog)
2336 nfa_regprog_T *prog;
2337{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002338 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339
2340 if (debugf != NULL)
2341 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002342 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002343
Bram Moolenaar473de612013-06-08 18:19:48 +02002344 if (prog->reganch)
2345 fprintf(debugf, "reganch: %d\n", prog->reganch);
2346 if (prog->regstart != NUL)
2347 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2348 prog->regstart, prog->regstart);
2349 if (prog->match_text != NULL)
2350 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352 fclose(debugf);
2353 }
2354}
2355#endif /* ENABLE_LOG */
2356#endif /* DEBUG */
2357
2358/*
2359 * Parse r.e. @expr and convert it into postfix form.
2360 * Return the postfix string on success, NULL otherwise.
2361 */
2362 static int *
2363re2post()
2364{
2365 if (nfa_reg(REG_NOPAREN) == FAIL)
2366 return NULL;
2367 EMIT(NFA_MOPEN);
2368 return post_start;
2369}
2370
2371/* NB. Some of the code below is inspired by Russ's. */
2372
2373/*
2374 * Represents an NFA state plus zero or one or two arrows exiting.
2375 * if c == MATCH, no arrows out; matching state.
2376 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2377 * If c < 256, labeled arrow with character c to out.
2378 */
2379
2380static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2381
2382/*
2383 * Allocate and initialize nfa_state_T.
2384 */
2385 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002386alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387 int c;
2388 nfa_state_T *out;
2389 nfa_state_T *out1;
2390{
2391 nfa_state_T *s;
2392
2393 if (istate >= nstate)
2394 return NULL;
2395
2396 s = &state_ptr[istate++];
2397
2398 s->c = c;
2399 s->out = out;
2400 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002401 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002402
2403 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002404 s->lastlist[0] = 0;
2405 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002406
2407 return s;
2408}
2409
2410/*
2411 * A partially built NFA without the matching state filled in.
2412 * Frag_T.start points at the start state.
2413 * Frag_T.out is a list of places that need to be set to the
2414 * next state for this fragment.
2415 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002416
2417/* Since the out pointers in the list are always
2418 * uninitialized, we use the pointers themselves
2419 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002420typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002421union Ptrlist
2422{
2423 Ptrlist *next;
2424 nfa_state_T *s;
2425};
2426
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002427struct Frag
2428{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002429 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 Ptrlist *out;
2431};
2432typedef struct Frag Frag_T;
2433
2434static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2435static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2436static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2437static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2438static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2439static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2440
2441/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002442 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 */
2444 static Frag_T
2445frag(start, out)
2446 nfa_state_T *start;
2447 Ptrlist *out;
2448{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002449 Frag_T n;
2450
2451 n.start = start;
2452 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002453 return n;
2454}
2455
2456/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002457 * Create singleton list containing just outp.
2458 */
2459 static Ptrlist *
2460list1(outp)
2461 nfa_state_T **outp;
2462{
2463 Ptrlist *l;
2464
2465 l = (Ptrlist *)outp;
2466 l->next = NULL;
2467 return l;
2468}
2469
2470/*
2471 * Patch the list of states at out to point to start.
2472 */
2473 static void
2474patch(l, s)
2475 Ptrlist *l;
2476 nfa_state_T *s;
2477{
2478 Ptrlist *next;
2479
2480 for (; l; l = next)
2481 {
2482 next = l->next;
2483 l->s = s;
2484 }
2485}
2486
2487
2488/*
2489 * Join the two lists l1 and l2, returning the combination.
2490 */
2491 static Ptrlist *
2492append(l1, l2)
2493 Ptrlist *l1;
2494 Ptrlist *l2;
2495{
2496 Ptrlist *oldl1;
2497
2498 oldl1 = l1;
2499 while (l1->next)
2500 l1 = l1->next;
2501 l1->next = l2;
2502 return oldl1;
2503}
2504
2505/*
2506 * Stack used for transforming postfix form into NFA.
2507 */
2508static Frag_T empty;
2509
2510 static void
2511st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002512 int *postfix UNUSED;
2513 int *end UNUSED;
2514 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002516#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002517 FILE *df;
2518 int *p2;
2519
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002520 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521 if (df)
2522 {
2523 fprintf(df, "Error popping the stack!\n");
2524#ifdef DEBUG
2525 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2526#endif
2527 fprintf(df, "Postfix form is: ");
2528#ifdef DEBUG
2529 for (p2 = postfix; p2 < end; p2++)
2530 {
2531 nfa_set_code(*p2);
2532 fprintf(df, "%s, ", code);
2533 }
2534 nfa_set_code(*p);
2535 fprintf(df, "\nCurrent position is: ");
2536 for (p2 = postfix; p2 <= p; p2 ++)
2537 {
2538 nfa_set_code(*p2);
2539 fprintf(df, "%s, ", code);
2540 }
2541#else
2542 for (p2 = postfix; p2 < end; p2++)
2543 {
2544 fprintf(df, "%d, ", *p2);
2545 }
2546 fprintf(df, "\nCurrent position is: ");
2547 for (p2 = postfix; p2 <= p; p2 ++)
2548 {
2549 fprintf(df, "%d, ", *p2);
2550 }
2551#endif
2552 fprintf(df, "\n--------------------------\n");
2553 fclose(df);
2554 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002555#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002556 EMSG(_("E874: (NFA) Could not pop the stack !"));
2557}
2558
2559/*
2560 * Push an item onto the stack.
2561 */
2562 static void
2563st_push(s, p, stack_end)
2564 Frag_T s;
2565 Frag_T **p;
2566 Frag_T *stack_end;
2567{
2568 Frag_T *stackp = *p;
2569
2570 if (stackp >= stack_end)
2571 return;
2572 *stackp = s;
2573 *p = *p + 1;
2574}
2575
2576/*
2577 * Pop an item from the stack.
2578 */
2579 static Frag_T
2580st_pop(p, stack)
2581 Frag_T **p;
2582 Frag_T *stack;
2583{
2584 Frag_T *stackp;
2585
2586 *p = *p - 1;
2587 stackp = *p;
2588 if (stackp < stack)
2589 return empty;
2590 return **p;
2591}
2592
2593/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002594 * Estimate the maximum byte length of anything matching "state".
2595 * When unknown or unlimited return -1.
2596 */
2597 static int
2598nfa_max_width(startstate, depth)
2599 nfa_state_T *startstate;
2600 int depth;
2601{
2602 int l, r;
2603 nfa_state_T *state = startstate;
2604 int len = 0;
2605
2606 /* detect looping in a NFA_SPLIT */
2607 if (depth > 4)
2608 return -1;
2609
2610 for (;;)
2611 {
2612 switch (state->c)
2613 {
2614 case NFA_END_INVISIBLE:
2615 case NFA_END_INVISIBLE_NEG:
2616 /* the end, return what we have */
2617 return len;
2618
2619 case NFA_SPLIT:
2620 /* two alternatives, use the maximum */
2621 l = nfa_max_width(state->out, depth + 1);
2622 r = nfa_max_width(state->out1, depth + 1);
2623 if (l < 0 || r < 0)
2624 return -1;
2625 return len + (l > r ? l : r);
2626
2627 case NFA_ANY:
2628 case NFA_START_COLL:
2629 case NFA_START_NEG_COLL:
2630 /* matches some character, including composing chars */
2631#ifdef FEAT_MBYTE
2632 if (enc_utf8)
2633 len += MB_MAXBYTES;
2634 else if (has_mbyte)
2635 len += 2;
2636 else
2637#endif
2638 ++len;
2639 if (state->c != NFA_ANY)
2640 {
2641 /* skip over the characters */
2642 state = state->out1->out;
2643 continue;
2644 }
2645 break;
2646
2647 case NFA_DIGIT:
2648 case NFA_WHITE:
2649 case NFA_HEX:
2650 case NFA_OCTAL:
2651 /* ascii */
2652 ++len;
2653 break;
2654
2655 case NFA_IDENT:
2656 case NFA_SIDENT:
2657 case NFA_KWORD:
2658 case NFA_SKWORD:
2659 case NFA_FNAME:
2660 case NFA_SFNAME:
2661 case NFA_PRINT:
2662 case NFA_SPRINT:
2663 case NFA_NWHITE:
2664 case NFA_NDIGIT:
2665 case NFA_NHEX:
2666 case NFA_NOCTAL:
2667 case NFA_WORD:
2668 case NFA_NWORD:
2669 case NFA_HEAD:
2670 case NFA_NHEAD:
2671 case NFA_ALPHA:
2672 case NFA_NALPHA:
2673 case NFA_LOWER:
2674 case NFA_NLOWER:
2675 case NFA_UPPER:
2676 case NFA_NUPPER:
2677 /* possibly non-ascii */
2678#ifdef FEAT_MBYTE
2679 if (has_mbyte)
2680 len += 3;
2681 else
2682#endif
2683 ++len;
2684 break;
2685
2686 case NFA_START_INVISIBLE:
2687 case NFA_START_INVISIBLE_NEG:
2688 case NFA_START_INVISIBLE_BEFORE:
2689 case NFA_START_INVISIBLE_BEFORE_NEG:
2690 /* zero-width, out1 points to the END state */
2691 state = state->out1->out;
2692 continue;
2693
2694 case NFA_BACKREF1:
2695 case NFA_BACKREF2:
2696 case NFA_BACKREF3:
2697 case NFA_BACKREF4:
2698 case NFA_BACKREF5:
2699 case NFA_BACKREF6:
2700 case NFA_BACKREF7:
2701 case NFA_BACKREF8:
2702 case NFA_BACKREF9:
2703#ifdef FEAT_SYN_HL
2704 case NFA_ZREF1:
2705 case NFA_ZREF2:
2706 case NFA_ZREF3:
2707 case NFA_ZREF4:
2708 case NFA_ZREF5:
2709 case NFA_ZREF6:
2710 case NFA_ZREF7:
2711 case NFA_ZREF8:
2712 case NFA_ZREF9:
2713#endif
2714 case NFA_NEWL:
2715 case NFA_SKIP:
2716 /* unknown width */
2717 return -1;
2718
2719 case NFA_BOL:
2720 case NFA_EOL:
2721 case NFA_BOF:
2722 case NFA_EOF:
2723 case NFA_BOW:
2724 case NFA_EOW:
2725 case NFA_MOPEN:
2726 case NFA_MOPEN1:
2727 case NFA_MOPEN2:
2728 case NFA_MOPEN3:
2729 case NFA_MOPEN4:
2730 case NFA_MOPEN5:
2731 case NFA_MOPEN6:
2732 case NFA_MOPEN7:
2733 case NFA_MOPEN8:
2734 case NFA_MOPEN9:
2735#ifdef FEAT_SYN_HL
2736 case NFA_ZOPEN:
2737 case NFA_ZOPEN1:
2738 case NFA_ZOPEN2:
2739 case NFA_ZOPEN3:
2740 case NFA_ZOPEN4:
2741 case NFA_ZOPEN5:
2742 case NFA_ZOPEN6:
2743 case NFA_ZOPEN7:
2744 case NFA_ZOPEN8:
2745 case NFA_ZOPEN9:
2746 case NFA_ZCLOSE:
2747 case NFA_ZCLOSE1:
2748 case NFA_ZCLOSE2:
2749 case NFA_ZCLOSE3:
2750 case NFA_ZCLOSE4:
2751 case NFA_ZCLOSE5:
2752 case NFA_ZCLOSE6:
2753 case NFA_ZCLOSE7:
2754 case NFA_ZCLOSE8:
2755 case NFA_ZCLOSE9:
2756#endif
2757 case NFA_MCLOSE:
2758 case NFA_MCLOSE1:
2759 case NFA_MCLOSE2:
2760 case NFA_MCLOSE3:
2761 case NFA_MCLOSE4:
2762 case NFA_MCLOSE5:
2763 case NFA_MCLOSE6:
2764 case NFA_MCLOSE7:
2765 case NFA_MCLOSE8:
2766 case NFA_MCLOSE9:
2767 case NFA_NOPEN:
2768 case NFA_NCLOSE:
2769
2770 case NFA_LNUM_GT:
2771 case NFA_LNUM_LT:
2772 case NFA_COL_GT:
2773 case NFA_COL_LT:
2774 case NFA_VCOL_GT:
2775 case NFA_VCOL_LT:
2776 case NFA_MARK_GT:
2777 case NFA_MARK_LT:
2778 case NFA_VISUAL:
2779 case NFA_LNUM:
2780 case NFA_CURSOR:
2781 case NFA_COL:
2782 case NFA_VCOL:
2783 case NFA_MARK:
2784
2785 case NFA_ZSTART:
2786 case NFA_ZEND:
2787 case NFA_OPT_CHARS:
2788 case NFA_SKIP_CHAR:
2789 case NFA_START_PATTERN:
2790 case NFA_END_PATTERN:
2791 case NFA_COMPOSING:
2792 case NFA_END_COMPOSING:
2793 /* zero-width */
2794 break;
2795
2796 default:
2797 if (state->c < 0)
2798 /* don't know what this is */
2799 return -1;
2800 /* normal character */
2801 len += MB_CHAR2LEN(state->c);
2802 break;
2803 }
2804
2805 /* normal way to continue */
2806 state = state->out;
2807 }
2808
2809 /* unrecognized */
2810 return -1;
2811}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02002812
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002813/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002814 * Convert a postfix form into its equivalent NFA.
2815 * Return the NFA start state on success, NULL otherwise.
2816 */
2817 static nfa_state_T *
2818post2nfa(postfix, end, nfa_calc_size)
2819 int *postfix;
2820 int *end;
2821 int nfa_calc_size;
2822{
2823 int *p;
2824 int mopen;
2825 int mclose;
2826 Frag_T *stack = NULL;
2827 Frag_T *stackp = NULL;
2828 Frag_T *stack_end = NULL;
2829 Frag_T e1;
2830 Frag_T e2;
2831 Frag_T e;
2832 nfa_state_T *s;
2833 nfa_state_T *s1;
2834 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002835 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836
2837 if (postfix == NULL)
2838 return NULL;
2839
Bram Moolenaar053bb602013-05-20 13:55:21 +02002840#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002841#define POP() st_pop(&stackp, stack); \
2842 if (stackp < stack) \
2843 { \
2844 st_error(postfix, end, p); \
2845 return NULL; \
2846 }
2847
2848 if (nfa_calc_size == FALSE)
2849 {
2850 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002851 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002852 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002853 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854 }
2855
2856 for (p = postfix; p < end; ++p)
2857 {
2858 switch (*p)
2859 {
2860 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002861 /* Concatenation.
2862 * Pay attention: this operator does not exist in the r.e. itself
2863 * (it is implicit, really). It is added when r.e. is translated
2864 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002865 if (nfa_calc_size == TRUE)
2866 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002867 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002868 break;
2869 }
2870 e2 = POP();
2871 e1 = POP();
2872 patch(e1.out, e2.start);
2873 PUSH(frag(e1.start, e2.out));
2874 break;
2875
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876 case NFA_OR:
2877 /* Alternation */
2878 if (nfa_calc_size == TRUE)
2879 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002880 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881 break;
2882 }
2883 e2 = POP();
2884 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002885 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002887 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888 PUSH(frag(s, append(e1.out, e2.out)));
2889 break;
2890
2891 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002892 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 if (nfa_calc_size == TRUE)
2894 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002895 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 break;
2897 }
2898 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002899 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002900 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002901 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002902 patch(e.out, s);
2903 PUSH(frag(s, list1(&s->out1)));
2904 break;
2905
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002906 case NFA_STAR_NONGREEDY:
2907 /* Zero or more, prefer zero */
2908 if (nfa_calc_size == TRUE)
2909 {
2910 nstate++;
2911 break;
2912 }
2913 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002914 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002915 if (s == NULL)
2916 goto theend;
2917 patch(e.out, s);
2918 PUSH(frag(s, list1(&s->out)));
2919 break;
2920
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921 case NFA_QUEST:
2922 /* one or zero atoms=> greedy match */
2923 if (nfa_calc_size == TRUE)
2924 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002925 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 break;
2927 }
2928 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002929 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002930 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002931 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002932 PUSH(frag(s, append(e.out, list1(&s->out1))));
2933 break;
2934
2935 case NFA_QUEST_NONGREEDY:
2936 /* zero or one atoms => non-greedy match */
2937 if (nfa_calc_size == TRUE)
2938 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002939 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 break;
2941 }
2942 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002943 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002944 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002945 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002946 PUSH(frag(s, append(e.out, list1(&s->out))));
2947 break;
2948
Bram Moolenaar417bad22013-06-07 14:08:30 +02002949 case NFA_END_COLL:
2950 case NFA_END_NEG_COLL:
2951 /* On the stack is the sequence starting with NFA_START_COLL or
2952 * NFA_START_NEG_COLL and all possible characters. Patch it to
2953 * add the output to the start. */
2954 if (nfa_calc_size == TRUE)
2955 {
2956 nstate++;
2957 break;
2958 }
2959 e = POP();
2960 s = alloc_state(NFA_END_COLL, NULL, NULL);
2961 if (s == NULL)
2962 goto theend;
2963 patch(e.out, s);
2964 e.start->out1 = s;
2965 PUSH(frag(e.start, list1(&s->out)));
2966 break;
2967
2968 case NFA_RANGE:
2969 /* Before this are two characters, the low and high end of a
2970 * range. Turn them into two states with MIN and MAX. */
2971 if (nfa_calc_size == TRUE)
2972 {
2973 /* nstate += 0; */
2974 break;
2975 }
2976 e2 = POP();
2977 e1 = POP();
2978 e2.start->val = e2.start->c;
2979 e2.start->c = NFA_RANGE_MAX;
2980 e1.start->val = e1.start->c;
2981 e1.start->c = NFA_RANGE_MIN;
2982 patch(e1.out, e2.start);
2983 PUSH(frag(e1.start, e2.out));
2984 break;
2985
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002986 case NFA_SKIP_CHAR:
2987 /* Symbol of 0-length, Used in a repetition
2988 * with max/min count of 0 */
2989 if (nfa_calc_size == TRUE)
2990 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002991 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992 break;
2993 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002994 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002995 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002996 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002997 PUSH(frag(s, list1(&s->out)));
2998 break;
2999
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003000 case NFA_OPT_CHARS:
3001 {
3002 int n;
3003
3004 /* \%[abc] */
3005 n = *++p; /* get number of characters */
3006 if (nfa_calc_size == TRUE)
3007 {
3008 nstate += n;
3009 break;
3010 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003011 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003012 e1.out = NULL; /* stores list with out1's */
3013 s1 = NULL; /* previous NFA_SPLIT to connect to */
3014 while (n-- > 0)
3015 {
3016 e = POP(); /* get character */
3017 s = alloc_state(NFA_SPLIT, e.start, NULL);
3018 if (s == NULL)
3019 goto theend;
3020 if (e1.out == NULL)
3021 e1 = e;
3022 patch(e.out, s1);
3023 append(e1.out, list1(&s->out1));
3024 s1 = s;
3025 }
3026 PUSH(frag(s, e1.out));
3027 break;
3028 }
3029
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003030 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003031 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003032 case NFA_PREV_ATOM_JUST_BEFORE:
3033 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003034 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003035 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003036 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3037 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003038 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003039 int start_state;
3040 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003041 int n = 0;
3042 nfa_state_T *zend;
3043 nfa_state_T *skip;
3044
Bram Moolenaardecd9542013-06-07 16:31:50 +02003045 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003046 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003047 case NFA_PREV_ATOM_NO_WIDTH:
3048 start_state = NFA_START_INVISIBLE;
3049 end_state = NFA_END_INVISIBLE;
3050 break;
3051 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3052 start_state = NFA_START_INVISIBLE_NEG;
3053 end_state = NFA_END_INVISIBLE_NEG;
3054 break;
3055 case NFA_PREV_ATOM_JUST_BEFORE:
3056 start_state = NFA_START_INVISIBLE_BEFORE;
3057 end_state = NFA_END_INVISIBLE;
3058 break;
3059 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3060 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3061 end_state = NFA_END_INVISIBLE_NEG;
3062 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003063 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003064 start_state = NFA_START_PATTERN;
3065 end_state = NFA_END_PATTERN;
3066 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003067 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003068
3069 if (before)
3070 n = *++p; /* get the count */
3071
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003072 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003073 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003074 * The \@<= operator: match for the preceding atom.
3075 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003076 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003077 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003078
3079 if (nfa_calc_size == TRUE)
3080 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003081 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003082 break;
3083 }
3084 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003085 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003086 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003087 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003088
Bram Moolenaar87953742013-06-05 18:52:40 +02003089 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003090 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003091 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003092 if (pattern)
3093 {
3094 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3095 skip = alloc_state(NFA_SKIP, NULL, NULL);
3096 zend = alloc_state(NFA_ZEND, s1, NULL);
3097 s1->out= skip;
3098 patch(e.out, zend);
3099 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003100 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003101 else
3102 {
3103 patch(e.out, s1);
3104 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003105 if (before)
3106 {
3107 if (n <= 0)
3108 /* See if we can guess the maximum width, it avoids a
3109 * lot of pointless tries. */
3110 n = nfa_max_width(e.start, 0);
3111 s->val = n; /* store the count */
3112 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003113 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003115 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003116
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003117#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003118 case NFA_COMPOSING: /* char with composing char */
3119#if 0
3120 /* TODO */
3121 if (regflags & RF_ICOMBINE)
3122 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003123 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003124 }
3125#endif
3126 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003127#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003128
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003129 case NFA_MOPEN: /* \( \) Submatch */
3130 case NFA_MOPEN1:
3131 case NFA_MOPEN2:
3132 case NFA_MOPEN3:
3133 case NFA_MOPEN4:
3134 case NFA_MOPEN5:
3135 case NFA_MOPEN6:
3136 case NFA_MOPEN7:
3137 case NFA_MOPEN8:
3138 case NFA_MOPEN9:
3139#ifdef FEAT_SYN_HL
3140 case NFA_ZOPEN: /* \z( \) Submatch */
3141 case NFA_ZOPEN1:
3142 case NFA_ZOPEN2:
3143 case NFA_ZOPEN3:
3144 case NFA_ZOPEN4:
3145 case NFA_ZOPEN5:
3146 case NFA_ZOPEN6:
3147 case NFA_ZOPEN7:
3148 case NFA_ZOPEN8:
3149 case NFA_ZOPEN9:
3150#endif
3151 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003152 if (nfa_calc_size == TRUE)
3153 {
3154 nstate += 2;
3155 break;
3156 }
3157
3158 mopen = *p;
3159 switch (*p)
3160 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003161 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3162#ifdef FEAT_SYN_HL
3163 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3164 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3165 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3166 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3167 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3168 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3169 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3170 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3171 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3172 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3173#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003174#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003175 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003176#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003177 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003178 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 mclose = *p + NSUBEXP;
3180 break;
3181 }
3182
3183 /* Allow "NFA_MOPEN" as a valid postfix representation for
3184 * the empty regexp "". In this case, the NFA will be
3185 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3186 * empty groups of parenthesis, and empty mbyte chars */
3187 if (stackp == stack)
3188 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003189 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003190 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003191 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003192 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003194 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 patch(list1(&s->out), s1);
3196 PUSH(frag(s, list1(&s1->out)));
3197 break;
3198 }
3199
3200 /* At least one node was emitted before NFA_MOPEN, so
3201 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3202 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003203 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003205 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206
Bram Moolenaar525666f2013-06-02 16:40:55 +02003207 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003208 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003209 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003210 patch(e.out, s1);
3211
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003212#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003213 if (mopen == NFA_COMPOSING)
3214 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003215 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003216#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217
3218 PUSH(frag(s, list1(&s1->out)));
3219 break;
3220
Bram Moolenaar5714b802013-05-28 22:03:20 +02003221 case NFA_BACKREF1:
3222 case NFA_BACKREF2:
3223 case NFA_BACKREF3:
3224 case NFA_BACKREF4:
3225 case NFA_BACKREF5:
3226 case NFA_BACKREF6:
3227 case NFA_BACKREF7:
3228 case NFA_BACKREF8:
3229 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003230#ifdef FEAT_SYN_HL
3231 case NFA_ZREF1:
3232 case NFA_ZREF2:
3233 case NFA_ZREF3:
3234 case NFA_ZREF4:
3235 case NFA_ZREF5:
3236 case NFA_ZREF6:
3237 case NFA_ZREF7:
3238 case NFA_ZREF8:
3239 case NFA_ZREF9:
3240#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003241 if (nfa_calc_size == TRUE)
3242 {
3243 nstate += 2;
3244 break;
3245 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003246 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003247 if (s == NULL)
3248 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003249 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003250 if (s1 == NULL)
3251 goto theend;
3252 patch(list1(&s->out), s1);
3253 PUSH(frag(s, list1(&s1->out)));
3254 break;
3255
Bram Moolenaar423532e2013-05-29 21:14:42 +02003256 case NFA_LNUM:
3257 case NFA_LNUM_GT:
3258 case NFA_LNUM_LT:
3259 case NFA_VCOL:
3260 case NFA_VCOL_GT:
3261 case NFA_VCOL_LT:
3262 case NFA_COL:
3263 case NFA_COL_GT:
3264 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003265 case NFA_MARK:
3266 case NFA_MARK_GT:
3267 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003268 {
3269 int n = *++p; /* lnum, col or mark name */
3270
Bram Moolenaar423532e2013-05-29 21:14:42 +02003271 if (nfa_calc_size == TRUE)
3272 {
3273 nstate += 1;
3274 break;
3275 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003276 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003277 if (s == NULL)
3278 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003279 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003280 PUSH(frag(s, list1(&s->out)));
3281 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003282 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003283
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 case NFA_ZSTART:
3285 case NFA_ZEND:
3286 default:
3287 /* Operands */
3288 if (nfa_calc_size == TRUE)
3289 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003290 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 break;
3292 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003293 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003295 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 PUSH(frag(s, list1(&s->out)));
3297 break;
3298
3299 } /* switch(*p) */
3300
3301 } /* for(p = postfix; *p; ++p) */
3302
3303 if (nfa_calc_size == TRUE)
3304 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003305 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003306 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307 }
3308
3309 e = POP();
3310 if (stackp != stack)
3311 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3312
3313 if (istate >= nstate)
3314 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3315
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 matchstate = &state_ptr[istate++]; /* the match state */
3317 matchstate->c = NFA_MATCH;
3318 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003319 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320
3321 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003322 ret = e.start;
3323
3324theend:
3325 vim_free(stack);
3326 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003327
3328#undef POP1
3329#undef PUSH1
3330#undef POP2
3331#undef PUSH2
3332#undef POP
3333#undef PUSH
3334}
3335
Bram Moolenaara2947e22013-06-11 22:44:09 +02003336/*
3337 * After building the NFA program, inspect it to add optimization hints.
3338 */
3339 static void
3340nfa_postprocess(prog)
3341 nfa_regprog_T *prog;
3342{
3343 int i;
3344 int c;
3345
3346 for (i = 0; i < prog->nstate; ++i)
3347 {
3348 c = prog->state[i].c;
3349 if (c == NFA_START_INVISIBLE
3350 || c == NFA_START_INVISIBLE_NEG
3351 || c == NFA_START_INVISIBLE_BEFORE
3352 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3353 {
3354 int directly;
3355
3356 /* Do it directly when what follows is possibly the end of the
3357 * match. */
3358 if (match_follows(prog->state[i].out1->out, 0))
3359 directly = TRUE;
3360 else
3361 {
3362 int ch_invisible = failure_chance(prog->state[i].out, 0);
3363 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3364
3365 /* Postpone when the invisible match is expensive or has a
3366 * lower chance of failing. */
3367 if (c == NFA_START_INVISIBLE_BEFORE
3368 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3369 {
3370 /* "before" matches are very expensive when
3371 * unbounded, always prefer what follows then,
3372 * unless what follows will always match.
3373 * Otherwise strongly prefer what follows. */
3374 if (prog->state[i].val <= 0 && ch_follows > 0)
3375 directly = FALSE;
3376 else
3377 directly = ch_follows * 10 < ch_invisible;
3378 }
3379 else
3380 {
3381 /* normal invisible, first do the one with the
3382 * highest failure chance */
3383 directly = ch_follows < ch_invisible;
3384 }
3385 }
3386 if (directly)
3387 /* switch to the _FIRST state */
3388 ++prog->state[i].c;
3389 }
3390 }
3391}
3392
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003393/****************************************************************
3394 * NFA execution code.
3395 ****************************************************************/
3396
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003397typedef struct
3398{
3399 int in_use; /* number of subexpr with useful info */
3400
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003401 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003402 union
3403 {
3404 struct multipos
3405 {
3406 lpos_T start;
3407 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003408 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003409 struct linepos
3410 {
3411 char_u *start;
3412 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003413 } line[NSUBEXP];
3414 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003415} regsub_T;
3416
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003417typedef struct
3418{
3419 regsub_T norm; /* \( .. \) matches */
3420#ifdef FEAT_SYN_HL
3421 regsub_T synt; /* \z( .. \) matches */
3422#endif
3423} regsubs_T;
3424
Bram Moolenaara2d95102013-06-04 14:23:05 +02003425/* nfa_pim_T stores a Postponed Invisible Match. */
3426typedef struct nfa_pim_S nfa_pim_T;
3427struct nfa_pim_S
3428{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003429 int result; /* NFA_PIM_*, see below */
3430 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003431 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003432 union
3433 {
3434 lpos_T pos;
3435 char_u *ptr;
3436 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003437};
3438
3439/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003440#define NFA_PIM_UNUSED 0 /* pim not used */
3441#define NFA_PIM_TODO 1 /* pim not done yet */
3442#define NFA_PIM_MATCH 2 /* pim executed, matches */
3443#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003444
3445
Bram Moolenaar963fee22013-05-26 21:47:28 +02003446/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003447typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448{
3449 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003450 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003451 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3452 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003453 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003454} nfa_thread_T;
3455
Bram Moolenaar963fee22013-05-26 21:47:28 +02003456/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003457typedef struct
3458{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003459 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003460 int n; /* nr of states currently in "t" */
3461 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003462 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003463} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464
Bram Moolenaar5714b802013-05-28 22:03:20 +02003465#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003466static void log_subsexpr __ARGS((regsubs_T *subs));
3467static void log_subexpr __ARGS((regsub_T *sub));
3468
3469 static void
3470log_subsexpr(subs)
3471 regsubs_T *subs;
3472{
3473 log_subexpr(&subs->norm);
3474# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003475 if (nfa_has_zsubexpr)
3476 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003477# endif
3478}
3479
Bram Moolenaar5714b802013-05-28 22:03:20 +02003480 static void
3481log_subexpr(sub)
3482 regsub_T *sub;
3483{
3484 int j;
3485
3486 for (j = 0; j < sub->in_use; j++)
3487 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003488 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003489 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003490 sub->list.multi[j].start.col,
3491 (int)sub->list.multi[j].start.lnum,
3492 sub->list.multi[j].end.col,
3493 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003494 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003495 {
3496 char *s = (char *)sub->list.line[j].start;
3497 char *e = (char *)sub->list.line[j].end;
3498
Bram Moolenaar87953742013-06-05 18:52:40 +02003499 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003500 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003501 s == NULL ? "NULL" : s,
3502 e == NULL ? "NULL" : e);
3503 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003504}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003505
3506 static char *
3507pim_info(nfa_pim_T *pim)
3508{
3509 static char buf[30];
3510
3511 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3512 buf[0] = NUL;
3513 else
3514 {
3515 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3516 : (int)(pim->end.ptr - reginput));
3517 }
3518 return buf;
3519}
3520
Bram Moolenaar5714b802013-05-28 22:03:20 +02003521#endif
3522
Bram Moolenaar963fee22013-05-26 21:47:28 +02003523/* Used during execution: whether a match has been found. */
3524static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003525
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003526static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003527static void clear_sub __ARGS((regsub_T *sub));
3528static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3529static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003530static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003531static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
3532static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003533static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003534static 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 +02003535
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003536/*
3537 * Copy postponed invisible match info from "from" to "to".
3538 */
3539 static void
3540copy_pim(to, from)
3541 nfa_pim_T *to;
3542 nfa_pim_T *from;
3543{
3544 to->result = from->result;
3545 to->state = from->state;
3546 copy_sub(&to->subs.norm, &from->subs.norm);
3547#ifdef FEAT_SYN_HL
3548 if (nfa_has_zsubexpr)
3549 copy_sub(&to->subs.synt, &from->subs.synt);
3550#endif
3551 to->end = from->end;
3552}
3553
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003554 static void
3555clear_sub(sub)
3556 regsub_T *sub;
3557{
3558 if (REG_MULTI)
3559 /* Use 0xff to set lnum to -1 */
3560 vim_memset(sub->list.multi, 0xff,
3561 sizeof(struct multipos) * nfa_nsubexpr);
3562 else
3563 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3564 sub->in_use = 0;
3565}
3566
3567/*
3568 * Copy the submatches from "from" to "to".
3569 */
3570 static void
3571copy_sub(to, from)
3572 regsub_T *to;
3573 regsub_T *from;
3574{
3575 to->in_use = from->in_use;
3576 if (from->in_use > 0)
3577 {
3578 /* Copy the match start and end positions. */
3579 if (REG_MULTI)
3580 mch_memmove(&to->list.multi[0],
3581 &from->list.multi[0],
3582 sizeof(struct multipos) * from->in_use);
3583 else
3584 mch_memmove(&to->list.line[0],
3585 &from->list.line[0],
3586 sizeof(struct linepos) * from->in_use);
3587 }
3588}
3589
3590/*
3591 * Like copy_sub() but exclude the main match.
3592 */
3593 static void
3594copy_sub_off(to, from)
3595 regsub_T *to;
3596 regsub_T *from;
3597{
3598 if (to->in_use < from->in_use)
3599 to->in_use = from->in_use;
3600 if (from->in_use > 1)
3601 {
3602 /* Copy the match start and end positions. */
3603 if (REG_MULTI)
3604 mch_memmove(&to->list.multi[1],
3605 &from->list.multi[1],
3606 sizeof(struct multipos) * (from->in_use - 1));
3607 else
3608 mch_memmove(&to->list.line[1],
3609 &from->list.line[1],
3610 sizeof(struct linepos) * (from->in_use - 1));
3611 }
3612}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613
Bram Moolenaar428e9872013-05-30 17:05:39 +02003614/*
3615 * Return TRUE if "sub1" and "sub2" have the same positions.
3616 */
3617 static int
3618sub_equal(sub1, sub2)
3619 regsub_T *sub1;
3620 regsub_T *sub2;
3621{
3622 int i;
3623 int todo;
3624 linenr_T s1, e1;
3625 linenr_T s2, e2;
3626 char_u *sp1, *ep1;
3627 char_u *sp2, *ep2;
3628
3629 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3630 if (REG_MULTI)
3631 {
3632 for (i = 0; i < todo; ++i)
3633 {
3634 if (i < sub1->in_use)
3635 {
3636 s1 = sub1->list.multi[i].start.lnum;
3637 e1 = sub1->list.multi[i].end.lnum;
3638 }
3639 else
3640 {
3641 s1 = 0;
3642 e1 = 0;
3643 }
3644 if (i < sub2->in_use)
3645 {
3646 s2 = sub2->list.multi[i].start.lnum;
3647 e2 = sub2->list.multi[i].end.lnum;
3648 }
3649 else
3650 {
3651 s2 = 0;
3652 e2 = 0;
3653 }
3654 if (s1 != s2 || e1 != e2)
3655 return FALSE;
3656 if (s1 != 0 && sub1->list.multi[i].start.col
3657 != sub2->list.multi[i].start.col)
3658 return FALSE;
3659 if (e1 != 0 && sub1->list.multi[i].end.col
3660 != sub2->list.multi[i].end.col)
3661 return FALSE;
3662 }
3663 }
3664 else
3665 {
3666 for (i = 0; i < todo; ++i)
3667 {
3668 if (i < sub1->in_use)
3669 {
3670 sp1 = sub1->list.line[i].start;
3671 ep1 = sub1->list.line[i].end;
3672 }
3673 else
3674 {
3675 sp1 = NULL;
3676 ep1 = NULL;
3677 }
3678 if (i < sub2->in_use)
3679 {
3680 sp2 = sub2->list.line[i].start;
3681 ep2 = sub2->list.line[i].end;
3682 }
3683 else
3684 {
3685 sp2 = NULL;
3686 ep2 = NULL;
3687 }
3688 if (sp1 != sp2 || ep1 != ep2)
3689 return FALSE;
3690 }
3691 }
3692
3693 return TRUE;
3694}
3695
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003696#ifdef ENABLE_LOG
3697 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003698report_state(char *action,
3699 regsub_T *sub,
3700 nfa_state_T *state,
3701 int lid,
3702 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003703{
3704 int col;
3705
3706 if (sub->in_use <= 0)
3707 col = -1;
3708 else if (REG_MULTI)
3709 col = sub->list.multi[0].start.col;
3710 else
3711 col = (int)(sub->list.line[0].start - regline);
3712 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003713 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3714 action, abs(state->id), lid, state->c, code, col,
3715 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003716}
3717#endif
3718
Bram Moolenaar43e02982013-06-07 17:31:29 +02003719/*
3720 * Return TRUE if the same state is already in list "l" with the same
3721 * positions as "subs".
3722 */
3723 static int
3724has_state_with_pos(l, state, subs)
3725 nfa_list_T *l; /* runtime state list */
3726 nfa_state_T *state; /* state to update */
3727 regsubs_T *subs; /* pointers to subexpressions */
3728{
3729 nfa_thread_T *thread;
3730 int i;
3731
3732 for (i = 0; i < l->n; ++i)
3733 {
3734 thread = &l->t[i];
3735 if (thread->state->id == state->id
3736 && sub_equal(&thread->subs.norm, &subs->norm)
3737#ifdef FEAT_SYN_HL
3738 && (!nfa_has_zsubexpr ||
3739 sub_equal(&thread->subs.synt, &subs->synt))
3740#endif
3741 )
3742 return TRUE;
3743 }
3744 return FALSE;
3745}
3746
3747/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003748 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3749 */
3750 static int
3751match_follows(startstate, depth)
3752 nfa_state_T *startstate;
3753 int depth;
3754{
3755 nfa_state_T *state = startstate;
3756
3757 /* avoid too much recursion */
3758 if (depth > 10)
3759 return FALSE;
3760
3761 for (;;)
3762 {
3763 switch (state->c)
3764 {
3765 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003766 case NFA_MCLOSE:
3767 case NFA_END_INVISIBLE:
3768 case NFA_END_INVISIBLE_NEG:
3769 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003770 return TRUE;
3771
3772 case NFA_SPLIT:
3773 return match_follows(state->out, depth + 1)
3774 || match_follows(state->out1, depth + 1);
3775
3776 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003777 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003778 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003779 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003780 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003781 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003782 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003783 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003784 case NFA_COMPOSING:
3785 /* skip ahead to next state */
3786 state = state->out1->out;
3787 break;
3788
3789 case NFA_ANY:
3790 case NFA_IDENT:
3791 case NFA_SIDENT:
3792 case NFA_KWORD:
3793 case NFA_SKWORD:
3794 case NFA_FNAME:
3795 case NFA_SFNAME:
3796 case NFA_PRINT:
3797 case NFA_SPRINT:
3798 case NFA_WHITE:
3799 case NFA_NWHITE:
3800 case NFA_DIGIT:
3801 case NFA_NDIGIT:
3802 case NFA_HEX:
3803 case NFA_NHEX:
3804 case NFA_OCTAL:
3805 case NFA_NOCTAL:
3806 case NFA_WORD:
3807 case NFA_NWORD:
3808 case NFA_HEAD:
3809 case NFA_NHEAD:
3810 case NFA_ALPHA:
3811 case NFA_NALPHA:
3812 case NFA_LOWER:
3813 case NFA_NLOWER:
3814 case NFA_UPPER:
3815 case NFA_NUPPER:
3816 case NFA_START_COLL:
3817 case NFA_START_NEG_COLL:
3818 case NFA_NEWL:
3819 /* state will advance input */
3820 return FALSE;
3821
3822 default:
3823 if (state->c > 0)
3824 /* state will advance input */
3825 return FALSE;
3826
3827 /* Others: zero-width or possibly zero-width, might still find
3828 * a match at the same position, keep looking. */
3829 break;
3830 }
3831 state = state->out;
3832 }
3833 return FALSE;
3834}
3835
3836
3837/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003838 * Return TRUE if "state" is already in list "l".
3839 */
3840 static int
3841state_in_list(l, state, subs)
3842 nfa_list_T *l; /* runtime state list */
3843 nfa_state_T *state; /* state to update */
3844 regsubs_T *subs; /* pointers to subexpressions */
3845{
3846 if (state->lastlist[nfa_ll_index] == l->id)
3847 {
3848 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3849 return TRUE;
3850 }
3851 return FALSE;
3852}
3853
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003855addstate(l, state, subs, pim, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003856 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003857 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003858 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003859 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003860 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003861{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003862 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003863 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003864 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003865 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003866 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003867 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003868 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003869#ifdef ENABLE_LOG
3870 int did_print = FALSE;
3871#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003872
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003873 switch (state->c)
3874 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875 case NFA_NCLOSE:
3876 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003877 case NFA_MCLOSE1:
3878 case NFA_MCLOSE2:
3879 case NFA_MCLOSE3:
3880 case NFA_MCLOSE4:
3881 case NFA_MCLOSE5:
3882 case NFA_MCLOSE6:
3883 case NFA_MCLOSE7:
3884 case NFA_MCLOSE8:
3885 case NFA_MCLOSE9:
3886#ifdef FEAT_SYN_HL
3887 case NFA_ZCLOSE:
3888 case NFA_ZCLOSE1:
3889 case NFA_ZCLOSE2:
3890 case NFA_ZCLOSE3:
3891 case NFA_ZCLOSE4:
3892 case NFA_ZCLOSE5:
3893 case NFA_ZCLOSE6:
3894 case NFA_ZCLOSE7:
3895 case NFA_ZCLOSE8:
3896 case NFA_ZCLOSE9:
3897#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003898 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003899 case NFA_SPLIT:
3900 case NFA_NOPEN:
3901 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003902 /* These nodes are not added themselves but their "out" and/or
3903 * "out1" may be added below. */
3904 break;
3905
3906 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003907 case NFA_MOPEN1:
3908 case NFA_MOPEN2:
3909 case NFA_MOPEN3:
3910 case NFA_MOPEN4:
3911 case NFA_MOPEN5:
3912 case NFA_MOPEN6:
3913 case NFA_MOPEN7:
3914 case NFA_MOPEN8:
3915 case NFA_MOPEN9:
3916#ifdef FEAT_SYN_HL
3917 case NFA_ZOPEN:
3918 case NFA_ZOPEN1:
3919 case NFA_ZOPEN2:
3920 case NFA_ZOPEN3:
3921 case NFA_ZOPEN4:
3922 case NFA_ZOPEN5:
3923 case NFA_ZOPEN6:
3924 case NFA_ZOPEN7:
3925 case NFA_ZOPEN8:
3926 case NFA_ZOPEN9:
3927#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003928 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003929 /* These nodes do not need to be added, but we need to bail out
3930 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003931 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003932 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003933 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003934 break;
3935
Bram Moolenaar307aa162013-06-02 16:34:21 +02003936 case NFA_BOL:
3937 case NFA_BOF:
3938 /* "^" won't match past end-of-line, don't bother trying.
3939 * Except when we are going to the next line for a look-behind
3940 * match. */
3941 if (reginput > regline
3942 && (nfa_endp == NULL
3943 || !REG_MULTI
3944 || reglnum == nfa_endp->se_u.pos.lnum))
3945 goto skip_add;
3946 /* FALLTHROUGH */
3947
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003948 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003949 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003950 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003951 /* This state is already in the list, don't add it again,
3952 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003953 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003954 {
3955skip_add:
3956#ifdef ENABLE_LOG
3957 nfa_set_code(state->c);
3958 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3959 abs(state->id), l->id, state->c, code);
3960#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003961 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003962 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003963
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003964 /* Do not add the state again when it exists with the same
3965 * positions. */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003966 if (has_state_with_pos(l, state, subs))
3967 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003968 }
3969
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003970 /* When there are backreferences the number of states may be (a
3971 * lot) bigger than anticipated. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003972 if (nfa_has_backref && l->n == l->len)
3973 {
3974 int newlen = l->len * 3 / 2 + 50;
3975
3976 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3977 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003978 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003979
3980 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003981 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003982 thread = &l->t[l->n++];
3983 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003984 if (pim == NULL)
3985 thread->pim.result = NFA_PIM_UNUSED;
3986 else
3987 copy_pim(&thread->pim, pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003988 copy_sub(&thread->subs.norm, &subs->norm);
3989#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003990 if (nfa_has_zsubexpr)
3991 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003992#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003993#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003994 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003995 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003996#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003997 }
3998
3999#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004000 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004001 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002#endif
4003 switch (state->c)
4004 {
4005 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004006 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004007 break;
4008
4009 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004010 /* order matters here */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004011 addstate(l, state->out, subs, pim, off);
4012 addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004013 break;
4014
Bram Moolenaar5714b802013-05-28 22:03:20 +02004015 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004016 case NFA_NOPEN:
4017 case NFA_NCLOSE:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004018 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004019 break;
4020
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004021 case NFA_MOPEN:
4022 case NFA_MOPEN1:
4023 case NFA_MOPEN2:
4024 case NFA_MOPEN3:
4025 case NFA_MOPEN4:
4026 case NFA_MOPEN5:
4027 case NFA_MOPEN6:
4028 case NFA_MOPEN7:
4029 case NFA_MOPEN8:
4030 case NFA_MOPEN9:
4031#ifdef FEAT_SYN_HL
4032 case NFA_ZOPEN:
4033 case NFA_ZOPEN1:
4034 case NFA_ZOPEN2:
4035 case NFA_ZOPEN3:
4036 case NFA_ZOPEN4:
4037 case NFA_ZOPEN5:
4038 case NFA_ZOPEN6:
4039 case NFA_ZOPEN7:
4040 case NFA_ZOPEN8:
4041 case NFA_ZOPEN9:
4042#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004043 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004044 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004045 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004047 sub = &subs->norm;
4048 }
4049#ifdef FEAT_SYN_HL
4050 else if (state->c >= NFA_ZOPEN)
4051 {
4052 subidx = state->c - NFA_ZOPEN;
4053 sub = &subs->synt;
4054 }
4055#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004056 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004057 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004058 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004059 sub = &subs->norm;
4060 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004061
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004062 /* Set the position (with "off" added) in the subexpression. Save
4063 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02004064 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004065 if (REG_MULTI)
4066 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004067 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004068 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004069 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004070 save_in_use = -1;
4071 }
4072 else
4073 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004074 save_in_use = sub->in_use;
4075 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004076 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004077 sub->list.multi[i].start.lnum = -1;
4078 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004079 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004080 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004081 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004082 if (off == -1)
4083 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004084 sub->list.multi[subidx].start.lnum = reglnum + 1;
4085 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004086 }
4087 else
4088 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004089 sub->list.multi[subidx].start.lnum = reglnum;
4090 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004091 (colnr_T)(reginput - regline + off);
4092 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004093 }
4094 else
4095 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004096 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004097 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004098 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004099 save_in_use = -1;
4100 }
4101 else
4102 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004103 save_in_use = sub->in_use;
4104 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004105 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004106 sub->list.line[i].start = NULL;
4107 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004108 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004109 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004110 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004111 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004112 }
4113
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004114 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004115
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004116 if (save_in_use == -1)
4117 {
4118 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004119 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004120 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004121 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004122 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004123 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004124 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004125 break;
4126
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004127 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004128 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004129 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004130 /* Do not overwrite the position set by \ze. If no \ze
4131 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004132 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004133 break;
4134 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004135 case NFA_MCLOSE1:
4136 case NFA_MCLOSE2:
4137 case NFA_MCLOSE3:
4138 case NFA_MCLOSE4:
4139 case NFA_MCLOSE5:
4140 case NFA_MCLOSE6:
4141 case NFA_MCLOSE7:
4142 case NFA_MCLOSE8:
4143 case NFA_MCLOSE9:
4144#ifdef FEAT_SYN_HL
4145 case NFA_ZCLOSE:
4146 case NFA_ZCLOSE1:
4147 case NFA_ZCLOSE2:
4148 case NFA_ZCLOSE3:
4149 case NFA_ZCLOSE4:
4150 case NFA_ZCLOSE5:
4151 case NFA_ZCLOSE6:
4152 case NFA_ZCLOSE7:
4153 case NFA_ZCLOSE8:
4154 case NFA_ZCLOSE9:
4155#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004156 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004157 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004158 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004159 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004160 sub = &subs->norm;
4161 }
4162#ifdef FEAT_SYN_HL
4163 else if (state->c >= NFA_ZCLOSE)
4164 {
4165 subidx = state->c - NFA_ZCLOSE;
4166 sub = &subs->synt;
4167 }
4168#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004169 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004170 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004171 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004172 sub = &subs->norm;
4173 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004174
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004175 /* We don't fill in gaps here, there must have been an MOPEN that
4176 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004177 save_in_use = sub->in_use;
4178 if (sub->in_use <= subidx)
4179 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004180 if (REG_MULTI)
4181 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004182 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004183 if (off == -1)
4184 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004185 sub->list.multi[subidx].end.lnum = reglnum + 1;
4186 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004187 }
4188 else
4189 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004190 sub->list.multi[subidx].end.lnum = reglnum;
4191 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004192 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004193 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004194 }
4195 else
4196 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004197 save_ptr = sub->list.line[subidx].end;
4198 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004199 }
4200
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004201 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004202
4203 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004204 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004205 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004206 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004207 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208 break;
4209 }
4210}
4211
4212/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004213 * Like addstate(), but the new state(s) are put at position "*ip".
4214 * Used for zero-width matches, next state to use is the added one.
4215 * This makes sure the order of states to be tried does not change, which
4216 * matters for alternatives.
4217 */
4218 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004219addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004220 nfa_list_T *l; /* runtime state list */
4221 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004222 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004223 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004224 int *ip;
4225{
4226 int tlen = l->n;
4227 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004228 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004229
4230 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004231 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004232
Bram Moolenaar4b417062013-05-25 20:19:50 +02004233 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004234 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004235 return;
4236
4237 /* re-order to put the new state at the current position */
4238 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004239 if (count == 1)
4240 {
4241 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004242 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004243 }
4244 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004245 {
4246 /* make space for new states, then move them from the
4247 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004248 mch_memmove(&(l->t[listidx + count]),
4249 &(l->t[listidx + 1]),
4250 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4251 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02004252 &(l->t[l->n - 1]),
4253 sizeof(nfa_thread_T) * count);
4254 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004255 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004256 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004257}
4258
4259/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260 * Check character class "class" against current character c.
4261 */
4262 static int
4263check_char_class(class, c)
4264 int class;
4265 int c;
4266{
4267 switch (class)
4268 {
4269 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004270 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004271 return OK;
4272 break;
4273 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004274 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004275 return OK;
4276 break;
4277 case NFA_CLASS_BLANK:
4278 if (c == ' ' || c == '\t')
4279 return OK;
4280 break;
4281 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004282 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004283 return OK;
4284 break;
4285 case NFA_CLASS_DIGIT:
4286 if (VIM_ISDIGIT(c))
4287 return OK;
4288 break;
4289 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004290 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004291 return OK;
4292 break;
4293 case NFA_CLASS_LOWER:
4294 if (MB_ISLOWER(c))
4295 return OK;
4296 break;
4297 case NFA_CLASS_PRINT:
4298 if (vim_isprintc(c))
4299 return OK;
4300 break;
4301 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004302 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004303 return OK;
4304 break;
4305 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004306 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004307 return OK;
4308 break;
4309 case NFA_CLASS_UPPER:
4310 if (MB_ISUPPER(c))
4311 return OK;
4312 break;
4313 case NFA_CLASS_XDIGIT:
4314 if (vim_isxdigit(c))
4315 return OK;
4316 break;
4317 case NFA_CLASS_TAB:
4318 if (c == '\t')
4319 return OK;
4320 break;
4321 case NFA_CLASS_RETURN:
4322 if (c == '\r')
4323 return OK;
4324 break;
4325 case NFA_CLASS_BACKSPACE:
4326 if (c == '\b')
4327 return OK;
4328 break;
4329 case NFA_CLASS_ESCAPE:
4330 if (c == '\033')
4331 return OK;
4332 break;
4333
4334 default:
4335 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004336 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4337 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004338 }
4339 return FAIL;
4340}
4341
Bram Moolenaar5714b802013-05-28 22:03:20 +02004342static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
4343
4344/*
4345 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004346 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004347 */
4348 static int
4349match_backref(sub, subidx, bytelen)
4350 regsub_T *sub; /* pointers to subexpressions */
4351 int subidx;
4352 int *bytelen; /* out: length of match in bytes */
4353{
4354 int len;
4355
4356 if (sub->in_use <= subidx)
4357 {
4358retempty:
4359 /* backref was not set, match an empty string */
4360 *bytelen = 0;
4361 return TRUE;
4362 }
4363
4364 if (REG_MULTI)
4365 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004366 if (sub->list.multi[subidx].start.lnum < 0
4367 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004368 goto retempty;
4369 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004370 len = sub->list.multi[subidx].end.col
4371 - sub->list.multi[subidx].start.col;
4372 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004373 reginput, &len) == 0)
4374 {
4375 *bytelen = len;
4376 return TRUE;
4377 }
4378 }
4379 else
4380 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004381 if (sub->list.line[subidx].start == NULL
4382 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004383 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004384 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4385 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004386 {
4387 *bytelen = len;
4388 return TRUE;
4389 }
4390 }
4391 return FALSE;
4392}
4393
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004394#ifdef FEAT_SYN_HL
4395
4396static int match_zref __ARGS((int subidx, int *bytelen));
4397
4398/*
4399 * Check for a match with \z subexpression "subidx".
4400 * Return TRUE if it matches.
4401 */
4402 static int
4403match_zref(subidx, bytelen)
4404 int subidx;
4405 int *bytelen; /* out: length of match in bytes */
4406{
4407 int len;
4408
4409 cleanup_zsubexpr();
4410 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4411 {
4412 /* backref was not set, match an empty string */
4413 *bytelen = 0;
4414 return TRUE;
4415 }
4416
4417 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4418 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4419 {
4420 *bytelen = len;
4421 return TRUE;
4422 }
4423 return FALSE;
4424}
4425#endif
4426
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004427/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004428 * Save list IDs for all NFA states of "prog" into "list".
4429 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004430 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004431 */
4432 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004433nfa_save_listids(prog, list)
4434 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004435 int *list;
4436{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004437 int i;
4438 nfa_state_T *p;
4439
4440 /* Order in the list is reverse, it's a bit faster that way. */
4441 p = &prog->state[0];
4442 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004444 list[i] = p->lastlist[1];
4445 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004446 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004447 }
4448}
4449
4450/*
4451 * Restore list IDs from "list" to all NFA states.
4452 */
4453 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004454nfa_restore_listids(prog, list)
4455 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004456 int *list;
4457{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004458 int i;
4459 nfa_state_T *p;
4460
4461 p = &prog->state[0];
4462 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004463 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004464 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004465 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004466 }
4467}
4468
Bram Moolenaar423532e2013-05-29 21:14:42 +02004469 static int
4470nfa_re_num_cmp(val, op, pos)
4471 long_u val;
4472 int op;
4473 long_u pos;
4474{
4475 if (op == 1) return pos > val;
4476 if (op == 2) return pos < val;
4477 return val == pos;
4478}
4479
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004480static 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 +02004481static 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 +02004482
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004483/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004484 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004485 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4486 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004487 */
4488 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004489recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004490 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004491 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004492 nfa_regprog_T *prog;
4493 regsubs_T *submatch;
4494 regsubs_T *m;
4495 int **listids;
4496{
4497 char_u *save_reginput = reginput;
4498 char_u *save_regline = regline;
4499 int save_reglnum = reglnum;
4500 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004501 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004502 save_se_T *save_nfa_endp = nfa_endp;
4503 save_se_T endpos;
4504 save_se_T *endposp = NULL;
4505 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004506 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004507
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004508 if (pim != NULL)
4509 {
4510 /* start at the position where the postponed match was */
4511 if (REG_MULTI)
4512 reginput = regline + pim->end.pos.col;
4513 else
4514 reginput = pim->end.ptr;
4515 }
4516
Bram Moolenaardecd9542013-06-07 16:31:50 +02004517 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004518 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4519 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4520 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004521 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004522 /* The recursive match must end at the current position. When "pim" is
4523 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004524 endposp = &endpos;
4525 if (REG_MULTI)
4526 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004527 if (pim == NULL)
4528 {
4529 endpos.se_u.pos.col = (int)(reginput - regline);
4530 endpos.se_u.pos.lnum = reglnum;
4531 }
4532 else
4533 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004534 }
4535 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004536 {
4537 if (pim == NULL)
4538 endpos.se_u.ptr = reginput;
4539 else
4540 endpos.se_u.ptr = pim->end.ptr;
4541 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004542
4543 /* Go back the specified number of bytes, or as far as the
4544 * start of the previous line, to try matching "\@<=" or
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004545 * not matching "\@<!". This is very ineffecient, limit the number of
4546 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004547 if (state->val <= 0)
4548 {
4549 if (REG_MULTI)
4550 {
4551 regline = reg_getline(--reglnum);
4552 if (regline == NULL)
4553 /* can't go before the first line */
4554 regline = reg_getline(++reglnum);
4555 }
4556 reginput = regline;
4557 }
4558 else
4559 {
4560 if (REG_MULTI && (int)(reginput - regline) < state->val)
4561 {
4562 /* Not enough bytes in this line, go to end of
4563 * previous line. */
4564 regline = reg_getline(--reglnum);
4565 if (regline == NULL)
4566 {
4567 /* can't go before the first line */
4568 regline = reg_getline(++reglnum);
4569 reginput = regline;
4570 }
4571 else
4572 reginput = regline + STRLEN(regline);
4573 }
4574 if ((int)(reginput - regline) >= state->val)
4575 {
4576 reginput -= state->val;
4577#ifdef FEAT_MBYTE
4578 if (has_mbyte)
4579 reginput -= mb_head_off(regline, reginput);
4580#endif
4581 }
4582 else
4583 reginput = regline;
4584 }
4585 }
4586
Bram Moolenaarf46da702013-06-02 22:37:42 +02004587#ifdef ENABLE_LOG
4588 if (log_fd != stderr)
4589 fclose(log_fd);
4590 log_fd = NULL;
4591#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004592 /* Have to clear the lastlist field of the NFA nodes, so that
4593 * nfa_regmatch() and addstate() can run properly after recursion. */
4594 if (nfa_ll_index == 1)
4595 {
4596 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4597 * values and clear them. */
4598 if (*listids == NULL)
4599 {
4600 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4601 if (*listids == NULL)
4602 {
4603 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4604 return 0;
4605 }
4606 }
4607 nfa_save_listids(prog, *listids);
4608 need_restore = TRUE;
4609 /* any value of nfa_listid will do */
4610 }
4611 else
4612 {
4613 /* First recursive nfa_regmatch() call, switch to the second lastlist
4614 * entry. Make sure nfa_listid is different from a previous recursive
4615 * call, because some states may still have this ID. */
4616 ++nfa_ll_index;
4617 if (nfa_listid <= nfa_alt_listid)
4618 nfa_listid = nfa_alt_listid;
4619 }
4620
4621 /* Call nfa_regmatch() to check if the current concat matches at this
4622 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004623 nfa_endp = endposp;
4624 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004625
4626 if (need_restore)
4627 nfa_restore_listids(prog, *listids);
4628 else
4629 {
4630 --nfa_ll_index;
4631 nfa_alt_listid = nfa_listid;
4632 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004633
4634 /* restore position in input text */
4635 reginput = save_reginput;
4636 regline = save_regline;
4637 reglnum = save_reglnum;
4638 nfa_match = save_nfa_match;
4639 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004640 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004641
4642#ifdef ENABLE_LOG
4643 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4644 if (log_fd != NULL)
4645 {
4646 fprintf(log_fd, "****************************\n");
4647 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4648 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4649 fprintf(log_fd, "****************************\n");
4650 }
4651 else
4652 {
4653 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4654 log_fd = stderr;
4655 }
4656#endif
4657
4658 return result;
4659}
4660
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004661static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004662static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004663
4664/*
4665 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004666 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004667 * NFA_ANY: 1
4668 * specific character: 99
4669 */
4670 static int
4671failure_chance(state, depth)
4672 nfa_state_T *state;
4673 int depth;
4674{
4675 int c = state->c;
4676 int l, r;
4677
4678 /* detect looping */
4679 if (depth > 4)
4680 return 1;
4681
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004682 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004683 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004684 case NFA_SPLIT:
4685 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4686 /* avoid recursive stuff */
4687 return 1;
4688 /* two alternatives, use the lowest failure chance */
4689 l = failure_chance(state->out, depth + 1);
4690 r = failure_chance(state->out1, depth + 1);
4691 return l < r ? l : r;
4692
4693 case NFA_ANY:
4694 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004695 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004696
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004697 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004698 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004699 /* empty match works always */
4700 return 0;
4701
4702 case NFA_BOL:
4703 case NFA_EOL:
4704 case NFA_BOF:
4705 case NFA_EOF:
4706 case NFA_NEWL:
4707 return 99;
4708
4709 case NFA_BOW:
4710 case NFA_EOW:
4711 return 90;
4712
4713 case NFA_MOPEN:
4714 case NFA_MOPEN1:
4715 case NFA_MOPEN2:
4716 case NFA_MOPEN3:
4717 case NFA_MOPEN4:
4718 case NFA_MOPEN5:
4719 case NFA_MOPEN6:
4720 case NFA_MOPEN7:
4721 case NFA_MOPEN8:
4722 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004723#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004724 case NFA_ZOPEN:
4725 case NFA_ZOPEN1:
4726 case NFA_ZOPEN2:
4727 case NFA_ZOPEN3:
4728 case NFA_ZOPEN4:
4729 case NFA_ZOPEN5:
4730 case NFA_ZOPEN6:
4731 case NFA_ZOPEN7:
4732 case NFA_ZOPEN8:
4733 case NFA_ZOPEN9:
4734 case NFA_ZCLOSE:
4735 case NFA_ZCLOSE1:
4736 case NFA_ZCLOSE2:
4737 case NFA_ZCLOSE3:
4738 case NFA_ZCLOSE4:
4739 case NFA_ZCLOSE5:
4740 case NFA_ZCLOSE6:
4741 case NFA_ZCLOSE7:
4742 case NFA_ZCLOSE8:
4743 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004744#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004745 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004746 case NFA_MCLOSE1:
4747 case NFA_MCLOSE2:
4748 case NFA_MCLOSE3:
4749 case NFA_MCLOSE4:
4750 case NFA_MCLOSE5:
4751 case NFA_MCLOSE6:
4752 case NFA_MCLOSE7:
4753 case NFA_MCLOSE8:
4754 case NFA_MCLOSE9:
4755 case NFA_NCLOSE:
4756 return failure_chance(state->out, depth + 1);
4757
4758 case NFA_BACKREF1:
4759 case NFA_BACKREF2:
4760 case NFA_BACKREF3:
4761 case NFA_BACKREF4:
4762 case NFA_BACKREF5:
4763 case NFA_BACKREF6:
4764 case NFA_BACKREF7:
4765 case NFA_BACKREF8:
4766 case NFA_BACKREF9:
4767#ifdef FEAT_SYN_HL
4768 case NFA_ZREF1:
4769 case NFA_ZREF2:
4770 case NFA_ZREF3:
4771 case NFA_ZREF4:
4772 case NFA_ZREF5:
4773 case NFA_ZREF6:
4774 case NFA_ZREF7:
4775 case NFA_ZREF8:
4776 case NFA_ZREF9:
4777#endif
4778 /* backreferences don't match in many places */
4779 return 94;
4780
4781 case NFA_LNUM_GT:
4782 case NFA_LNUM_LT:
4783 case NFA_COL_GT:
4784 case NFA_COL_LT:
4785 case NFA_VCOL_GT:
4786 case NFA_VCOL_LT:
4787 case NFA_MARK_GT:
4788 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004789 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004790 /* before/after positions don't match very often */
4791 return 85;
4792
4793 case NFA_LNUM:
4794 return 90;
4795
4796 case NFA_CURSOR:
4797 case NFA_COL:
4798 case NFA_VCOL:
4799 case NFA_MARK:
4800 /* specific positions rarely match */
4801 return 98;
4802
4803 case NFA_COMPOSING:
4804 return 95;
4805
4806 default:
4807 if (c > 0)
4808 /* character match fails often */
4809 return 95;
4810 }
4811
4812 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004813 return 50;
4814}
4815
Bram Moolenaarf46da702013-06-02 22:37:42 +02004816/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004817 * Skip until the char "c" we know a match must start with.
4818 */
4819 static int
4820skip_to_start(c, colp)
4821 int c;
4822 colnr_T *colp;
4823{
4824 char_u *s;
4825
4826 /* Used often, do some work to avoid call overhead. */
4827 if (!ireg_ic
4828#ifdef FEAT_MBYTE
4829 && !has_mbyte
4830#endif
4831 )
4832 s = vim_strbyte(regline + *colp, c);
4833 else
4834 s = cstrchr(regline + *colp, c);
4835 if (s == NULL)
4836 return FAIL;
4837 *colp = (int)(s - regline);
4838 return OK;
4839}
4840
4841/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004842 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004843 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004844 * Returns zero for no match, 1 for a match.
4845 */
4846 static long
4847find_match_text(startcol, regstart, match_text)
4848 colnr_T startcol;
4849 int regstart;
4850 char_u *match_text;
4851{
4852 colnr_T col = startcol;
4853 int c1, c2;
4854 int len1, len2;
4855 int match;
4856
4857 for (;;)
4858 {
4859 match = TRUE;
4860 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4861 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4862 {
4863 c1 = PTR2CHAR(match_text + len1);
4864 c2 = PTR2CHAR(regline + col + len2);
4865 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4866 {
4867 match = FALSE;
4868 break;
4869 }
4870 len2 += MB_CHAR2LEN(c2);
4871 }
4872 if (match
4873#ifdef FEAT_MBYTE
4874 /* check that no composing char follows */
4875 && !(enc_utf8
4876 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4877#endif
4878 )
4879 {
4880 cleanup_subexpr();
4881 if (REG_MULTI)
4882 {
4883 reg_startpos[0].lnum = reglnum;
4884 reg_startpos[0].col = col;
4885 reg_endpos[0].lnum = reglnum;
4886 reg_endpos[0].col = col + len2;
4887 }
4888 else
4889 {
4890 reg_startp[0] = regline + col;
4891 reg_endp[0] = regline + col + len2;
4892 }
4893 return 1L;
4894 }
4895
4896 /* Try finding regstart after the current match. */
4897 col += MB_CHAR2LEN(regstart); /* skip regstart */
4898 if (skip_to_start(regstart, &col) == FAIL)
4899 break;
4900 }
4901 return 0L;
4902}
4903
4904/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004905 * Main matching routine.
4906 *
4907 * Run NFA to determine whether it matches reginput.
4908 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004909 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004910 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004911 * Return TRUE if there is a match, FALSE otherwise.
4912 * Note: Caller must ensure that: start != NULL.
4913 */
4914 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004915nfa_regmatch(prog, start, submatch, m)
4916 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004917 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004918 regsubs_T *submatch;
4919 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004921 int result;
4922 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004923 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004924 int go_to_nextline = FALSE;
4925 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004926 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004927 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004928 nfa_list_T *thislist;
4929 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004930 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004931 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004932 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004933 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02004934 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004935 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004936#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004937 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004938
4939 if (debug == NULL)
4940 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004941 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004942 return FALSE;
4943 }
4944#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004945 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004947 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004948 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004949 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004950 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004951 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004952 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004953 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004954 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004955
4956#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004957 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004958 if (log_fd != NULL)
4959 {
4960 fprintf(log_fd, "**********************************\n");
4961 nfa_set_code(start->c);
4962 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4963 abs(start->id), code);
4964 fprintf(log_fd, "**********************************\n");
4965 }
4966 else
4967 {
4968 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4969 log_fd = stderr;
4970 }
4971#endif
4972
4973 thislist = &list[0];
4974 thislist->n = 0;
4975 nextlist = &list[1];
4976 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004977#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004978 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004979#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004980 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004981
4982 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4983 * it's the first MOPEN. */
4984 if (toplevel)
4985 {
4986 if (REG_MULTI)
4987 {
4988 m->norm.list.multi[0].start.lnum = reglnum;
4989 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4990 }
4991 else
4992 m->norm.list.line[0].start = reginput;
4993 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004994 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004995 }
4996 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004997 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004998
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004999#define ADD_STATE_IF_MATCH(state) \
5000 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005001 add_state = state->out; \
5002 add_off = clen; \
5003 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005004
5005 /*
5006 * Run for each character.
5007 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005008 for (;;)
5009 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005010 int curc;
5011 int clen;
5012
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005013#ifdef FEAT_MBYTE
5014 if (has_mbyte)
5015 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005016 curc = (*mb_ptr2char)(reginput);
5017 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005018 }
5019 else
5020#endif
5021 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005022 curc = *reginput;
5023 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005024 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005025 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005026 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005027 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005028 go_to_nextline = FALSE;
5029 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030
5031 /* swap lists */
5032 thislist = &list[flag];
5033 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005034 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005035 ++nfa_listid;
5036 thislist->id = nfa_listid;
5037 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005038
5039#ifdef ENABLE_LOG
5040 fprintf(log_fd, "------------------------------------------\n");
5041 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005042 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005043 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005044 {
5045 int i;
5046
5047 for (i = 0; i < thislist->n; i++)
5048 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5049 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005050 fprintf(log_fd, "\n");
5051#endif
5052
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005053#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005054 fprintf(debug, "\n-------------------\n");
5055#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005056 /*
5057 * If the state lists are empty we can stop.
5058 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005059 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005060 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005061
5062 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005063 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005064 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005065 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005066
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005067#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068 nfa_set_code(t->state->c);
5069 fprintf(debug, "%s, ", code);
5070#endif
5071#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005072 {
5073 int col;
5074
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005075 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005076 col = -1;
5077 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005078 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005079 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005080 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005081 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005082 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5083 abs(t->state->id), (int)t->state->c, code, col,
5084 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005085 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005086#endif
5087
5088 /*
5089 * Handle the possible codes of the current state.
5090 * The most important is NFA_MATCH.
5091 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005092 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005093 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005094 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005095 switch (t->state->c)
5096 {
5097 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005098 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005099 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005100 copy_sub(&submatch->norm, &t->subs.norm);
5101#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005102 if (nfa_has_zsubexpr)
5103 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005104#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005105#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005106 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005107#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005108 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005109 * states at this position. When the list of states is going
5110 * to be empty quit without advancing, so that "reginput" is
5111 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005112 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005113 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005114 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005115 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005116
5117 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005118 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005119 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005120 /*
5121 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005122 * NFA_START_INVISIBLE_BEFORE node.
5123 * They surround a zero-width group, used with "\@=", "\&",
5124 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005125 * If we got here, it means that the current "invisible" group
5126 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005127 * nfa_regmatch(). For a look-behind match only when it ends
5128 * in the position in "nfa_endp".
5129 * Submatches are stored in *m, and used in the parent call.
5130 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005131#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005132 if (nfa_endp != NULL)
5133 {
5134 if (REG_MULTI)
5135 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5136 (int)reglnum,
5137 (int)nfa_endp->se_u.pos.lnum,
5138 (int)(reginput - regline),
5139 nfa_endp->se_u.pos.col);
5140 else
5141 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5142 (int)(reginput - regline),
5143 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005144 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005145#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005146 /* If "nfa_endp" is set it's only a match if it ends at
5147 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005148 if (nfa_endp != NULL && (REG_MULTI
5149 ? (reglnum != nfa_endp->se_u.pos.lnum
5150 || (int)(reginput - regline)
5151 != nfa_endp->se_u.pos.col)
5152 : reginput != nfa_endp->se_u.ptr))
5153 break;
5154
5155 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005156 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005157 {
5158 copy_sub(&m->norm, &t->subs.norm);
5159#ifdef FEAT_SYN_HL
5160 if (nfa_has_zsubexpr)
5161 copy_sub(&m->synt, &t->subs.synt);
5162#endif
5163 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005164#ifdef ENABLE_LOG
5165 fprintf(log_fd, "Match found:\n");
5166 log_subsexpr(m);
5167#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005168 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005169 break;
5170
5171 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005172 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005173 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005174 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005175 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005176 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005177 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005178 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005179 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005180#ifdef ENABLE_LOG
5181 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5182 failure_chance(t->state->out, 0),
5183 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005184#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005185 /* Do it directly if there already is a PIM or when
5186 * nfa_postprocess() detected it will work better. */
5187 if (t->pim.result != NFA_PIM_UNUSED
5188 || t->state->c == NFA_START_INVISIBLE_FIRST
5189 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5190 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5191 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005192 {
5193 /*
5194 * First try matching the invisible match, then what
5195 * follows.
5196 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005197 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005198 submatch, m, &listids);
5199
Bram Moolenaardecd9542013-06-07 16:31:50 +02005200 /* for \@! and \@<! it is a match when the result is
5201 * FALSE */
5202 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005203 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5204 || t->state->c
5205 == NFA_START_INVISIBLE_BEFORE_NEG
5206 || t->state->c
5207 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005208 {
5209 /* Copy submatch info from the recursive call */
5210 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005211#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005212 if (nfa_has_zsubexpr)
5213 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005214#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005215
Bram Moolenaara2d95102013-06-04 14:23:05 +02005216 /* t->state->out1 is the corresponding
5217 * END_INVISIBLE node; Add its out to the current
5218 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005219 add_here = TRUE;
5220 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005221 }
5222 }
5223 else
5224 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005225 nfa_pim_T pim;
5226
Bram Moolenaara2d95102013-06-04 14:23:05 +02005227 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005228 * First try matching what follows. Only if a match
5229 * is found verify the invisible match matches. Add a
5230 * nfa_pim_T to the following states, it contains info
5231 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005232 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005233 pim.state = t->state;
5234 pim.result = NFA_PIM_TODO;
5235 pim.subs.norm.in_use = 0;
5236#ifdef FEAT_SYN_HL
5237 pim.subs.synt.in_use = 0;
5238#endif
5239 if (REG_MULTI)
5240 {
5241 pim.end.pos.col = (int)(reginput - regline);
5242 pim.end.pos.lnum = reglnum;
5243 }
5244 else
5245 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005246
5247 /* t->state->out1 is the corresponding END_INVISIBLE
5248 * node; Add its out to the current list (zero-width
5249 * match). */
5250 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005251 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005252 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005253 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005254 break;
5255
Bram Moolenaar87953742013-06-05 18:52:40 +02005256 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005257 {
5258 nfa_state_T *skip = NULL;
5259#ifdef ENABLE_LOG
5260 int skip_lid = 0;
5261#endif
5262
5263 /* There is no point in trying to match the pattern if the
5264 * output state is not going to be added to the list. */
5265 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5266 {
5267 skip = t->state->out1->out;
5268#ifdef ENABLE_LOG
5269 skip_lid = nextlist->id;
5270#endif
5271 }
5272 else if (state_in_list(nextlist,
5273 t->state->out1->out->out, &t->subs))
5274 {
5275 skip = t->state->out1->out->out;
5276#ifdef ENABLE_LOG
5277 skip_lid = nextlist->id;
5278#endif
5279 }
5280 else if(state_in_list(thislist,
5281 t->state->out1->out->out, &t->subs))
5282 {
5283 skip = t->state->out1->out->out;
5284#ifdef ENABLE_LOG
5285 skip_lid = thislist->id;
5286#endif
5287 }
5288 if (skip != NULL)
5289 {
5290#ifdef ENABLE_LOG
5291 nfa_set_code(skip->c);
5292 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5293 abs(skip->id), skip_lid, skip->c, code);
5294#endif
5295 break;
5296 }
5297
Bram Moolenaar87953742013-06-05 18:52:40 +02005298 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005299 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005300 submatch, m, &listids);
5301 if (result)
5302 {
5303 int bytelen;
5304
5305#ifdef ENABLE_LOG
5306 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5307 log_subsexpr(m);
5308#endif
5309 /* Copy submatch info from the recursive call */
5310 copy_sub_off(&t->subs.norm, &m->norm);
5311#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005312 if (nfa_has_zsubexpr)
5313 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005314#endif
5315 /* Now we need to skip over the matched text and then
5316 * continue with what follows. */
5317 if (REG_MULTI)
5318 /* TODO: multi-line match */
5319 bytelen = m->norm.list.multi[0].end.col
5320 - (int)(reginput - regline);
5321 else
5322 bytelen = (int)(m->norm.list.line[0].end - reginput);
5323
5324#ifdef ENABLE_LOG
5325 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5326#endif
5327 if (bytelen == 0)
5328 {
5329 /* empty match, output of corresponding
5330 * NFA_END_PATTERN/NFA_SKIP to be used at current
5331 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005332 add_here = TRUE;
5333 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005334 }
5335 else if (bytelen <= clen)
5336 {
5337 /* match current character, output of corresponding
5338 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005339 add_state = t->state->out1->out->out;
5340 add_off = clen;
5341 }
5342 else
5343 {
5344 /* skip over the matched characters, set character
5345 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005346 add_state = t->state->out1->out;
5347 add_off = bytelen;
5348 add_count = bytelen - clen;
5349 }
5350 }
5351 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005352 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005354 case NFA_BOL:
5355 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005356 {
5357 add_here = TRUE;
5358 add_state = t->state->out;
5359 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005360 break;
5361
5362 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005363 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005364 {
5365 add_here = TRUE;
5366 add_state = t->state->out;
5367 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005368 break;
5369
5370 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005371 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005372
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005373 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005374 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005375#ifdef FEAT_MBYTE
5376 else if (has_mbyte)
5377 {
5378 int this_class;
5379
5380 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005381 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005382 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005383 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005384 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005385 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005386 }
5387#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005388 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005389 || (reginput > regline
5390 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005391 result = FALSE;
5392 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005393 {
5394 add_here = TRUE;
5395 add_state = t->state->out;
5396 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005397 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005398
5399 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005400 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005401 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005402 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005403#ifdef FEAT_MBYTE
5404 else if (has_mbyte)
5405 {
5406 int this_class, prev_class;
5407
5408 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005409 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 prev_class = reg_prev_class();
5411 if (this_class == prev_class
5412 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005413 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005414 }
5415#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005416 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005417 || (reginput[0] != NUL
5418 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005419 result = FALSE;
5420 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005421 {
5422 add_here = TRUE;
5423 add_state = t->state->out;
5424 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005425 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005426
Bram Moolenaar4b780632013-05-31 22:14:52 +02005427 case NFA_BOF:
5428 if (reglnum == 0 && reginput == regline
5429 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005430 {
5431 add_here = TRUE;
5432 add_state = t->state->out;
5433 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005434 break;
5435
5436 case NFA_EOF:
5437 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005438 {
5439 add_here = TRUE;
5440 add_state = t->state->out;
5441 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005442 break;
5443
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005444#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005445 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005446 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005447 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005448 int len = 0;
5449 nfa_state_T *end;
5450 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005451 int cchars[MAX_MCO];
5452 int ccount = 0;
5453 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005454
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005455 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005456 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005457 if (utf_iscomposing(sta->c))
5458 {
5459 /* Only match composing character(s), ignore base
5460 * character. Used for ".{composing}" and "{composing}"
5461 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005462 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005463 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005464 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005465 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005466 /* If \Z was present, then ignore composing characters.
5467 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005468 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005469 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005470 else
5471 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005472 while (sta->c != NFA_END_COMPOSING)
5473 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005474 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005475
5476 /* Check base character matches first, unless ignored. */
5477 else if (len > 0 || mc == sta->c)
5478 {
5479 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005480 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005481 len += mb_char2len(mc);
5482 sta = sta->out;
5483 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005484
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005485 /* We don't care about the order of composing characters.
5486 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005487 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005488 {
5489 mc = mb_ptr2char(reginput + len);
5490 cchars[ccount++] = mc;
5491 len += mb_char2len(mc);
5492 if (ccount == MAX_MCO)
5493 break;
5494 }
5495
5496 /* Check that each composing char in the pattern matches a
5497 * composing char in the text. We do not check if all
5498 * composing chars are matched. */
5499 result = OK;
5500 while (sta->c != NFA_END_COMPOSING)
5501 {
5502 for (j = 0; j < ccount; ++j)
5503 if (cchars[j] == sta->c)
5504 break;
5505 if (j == ccount)
5506 {
5507 result = FAIL;
5508 break;
5509 }
5510 sta = sta->out;
5511 }
5512 }
5513 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005514 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005515
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005516 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005517 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005519 }
5520#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005521
5522 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005523 if (curc == NUL && !reg_line_lbr && REG_MULTI
5524 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005525 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005526 go_to_nextline = TRUE;
5527 /* Pass -1 for the offset, which means taking the position
5528 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005529 add_state = t->state->out;
5530 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005532 else if (curc == '\n' && reg_line_lbr)
5533 {
5534 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005535 add_state = t->state->out;
5536 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005537 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538 break;
5539
Bram Moolenaar417bad22013-06-07 14:08:30 +02005540 case NFA_START_COLL:
5541 case NFA_START_NEG_COLL:
5542 {
5543 /* What follows is a list of characters, until NFA_END_COLL.
5544 * One of them must match or none of them must match. */
5545 nfa_state_T *state;
5546 int result_if_matched;
5547 int c1, c2;
5548
5549 /* Never match EOL. If it's part of the collection it is added
5550 * as a separate state with an OR. */
5551 if (curc == NUL)
5552 break;
5553
5554 state = t->state->out;
5555 result_if_matched = (t->state->c == NFA_START_COLL);
5556 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005557 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005558 if (state->c == NFA_END_COLL)
5559 {
5560 result = !result_if_matched;
5561 break;
5562 }
5563 if (state->c == NFA_RANGE_MIN)
5564 {
5565 c1 = state->val;
5566 state = state->out; /* advance to NFA_RANGE_MAX */
5567 c2 = state->val;
5568#ifdef ENABLE_LOG
5569 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5570 curc, c1, c2);
5571#endif
5572 if (curc >= c1 && curc <= c2)
5573 {
5574 result = result_if_matched;
5575 break;
5576 }
5577 if (ireg_ic)
5578 {
5579 int curc_low = MB_TOLOWER(curc);
5580 int done = FALSE;
5581
5582 for ( ; c1 <= c2; ++c1)
5583 if (MB_TOLOWER(c1) == curc_low)
5584 {
5585 result = result_if_matched;
5586 done = TRUE;
5587 break;
5588 }
5589 if (done)
5590 break;
5591 }
5592 }
5593 else if (state->c < 0 ? check_char_class(state->c, curc)
5594 : (curc == state->c
5595 || (ireg_ic && MB_TOLOWER(curc)
5596 == MB_TOLOWER(state->c))))
5597 {
5598 result = result_if_matched;
5599 break;
5600 }
5601 state = state->out;
5602 }
5603 if (result)
5604 {
5605 /* next state is in out of the NFA_END_COLL, out1 of
5606 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005607 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005608 add_off = clen;
5609 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005610 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005611 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005612
5613 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005614 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005615 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005616 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005617 add_state = t->state->out;
5618 add_off = clen;
5619 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005620 break;
5621
5622 /*
5623 * Character classes like \a for alpha, \d for digit etc.
5624 */
5625 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005626 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005627 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005628 break;
5629
5630 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005631 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005632 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005633 break;
5634
5635 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005636 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005637 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005638 break;
5639
5640 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005641 result = !VIM_ISDIGIT(curc)
5642 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005643 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005644 break;
5645
5646 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005647 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005648 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005649 break;
5650
5651 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005652 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005653 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005654 break;
5655
5656 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005657 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005658 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005659 break;
5660
5661 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005662 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005663 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005664 break;
5665
5666 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005667 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005668 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005669 break;
5670
5671 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005672 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005673 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005674 break;
5675
5676 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005677 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005678 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005679 break;
5680
5681 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005682 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005683 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005684 break;
5685
5686 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005687 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005688 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005689 break;
5690
5691 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005692 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005693 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005694 break;
5695
5696 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005697 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005698 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005699 break;
5700
5701 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005702 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005703 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005704 break;
5705
5706 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005707 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005708 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005709 break;
5710
5711 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005712 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005713 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005714 break;
5715
5716 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005717 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005718 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005719 break;
5720
5721 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005722 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005723 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005724 break;
5725
5726 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005727 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005728 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005729 break;
5730
5731 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005732 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005733 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005734 break;
5735
5736 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005737 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005738 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005739 break;
5740
5741 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005742 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005743 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005744 break;
5745
5746 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005747 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005748 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005749 break;
5750
5751 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005752 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005753 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005754 break;
5755
Bram Moolenaar5714b802013-05-28 22:03:20 +02005756 case NFA_BACKREF1:
5757 case NFA_BACKREF2:
5758 case NFA_BACKREF3:
5759 case NFA_BACKREF4:
5760 case NFA_BACKREF5:
5761 case NFA_BACKREF6:
5762 case NFA_BACKREF7:
5763 case NFA_BACKREF8:
5764 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005765#ifdef FEAT_SYN_HL
5766 case NFA_ZREF1:
5767 case NFA_ZREF2:
5768 case NFA_ZREF3:
5769 case NFA_ZREF4:
5770 case NFA_ZREF5:
5771 case NFA_ZREF6:
5772 case NFA_ZREF7:
5773 case NFA_ZREF8:
5774 case NFA_ZREF9:
5775#endif
5776 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005777 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005778 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005779 int bytelen;
5780
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005781 if (t->state->c <= NFA_BACKREF9)
5782 {
5783 subidx = t->state->c - NFA_BACKREF1 + 1;
5784 result = match_backref(&t->subs.norm, subidx, &bytelen);
5785 }
5786#ifdef FEAT_SYN_HL
5787 else
5788 {
5789 subidx = t->state->c - NFA_ZREF1 + 1;
5790 result = match_zref(subidx, &bytelen);
5791 }
5792#endif
5793
Bram Moolenaar5714b802013-05-28 22:03:20 +02005794 if (result)
5795 {
5796 if (bytelen == 0)
5797 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005798 /* empty match always works, output of NFA_SKIP to be
5799 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005800 add_here = TRUE;
5801 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005802 }
5803 else if (bytelen <= clen)
5804 {
5805 /* match current character, jump ahead to out of
5806 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005807 add_state = t->state->out->out;
5808 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005809 }
5810 else
5811 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005812 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005813 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005814 add_state = t->state->out;
5815 add_off = bytelen;
5816 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005817 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005818 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005819 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005820 }
5821 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005822 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005823 if (t->count - clen <= 0)
5824 {
5825 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005826 add_state = t->state->out;
5827 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005828 }
5829 else
5830 {
5831 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005832 add_state = t->state;
5833 add_off = 0;
5834 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005835 }
5836 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005837
Bram Moolenaar423532e2013-05-29 21:14:42 +02005838 case NFA_LNUM:
5839 case NFA_LNUM_GT:
5840 case NFA_LNUM_LT:
5841 result = (REG_MULTI &&
5842 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5843 (long_u)(reglnum + reg_firstlnum)));
5844 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005845 {
5846 add_here = TRUE;
5847 add_state = t->state->out;
5848 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005849 break;
5850
5851 case NFA_COL:
5852 case NFA_COL_GT:
5853 case NFA_COL_LT:
5854 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5855 (long_u)(reginput - regline) + 1);
5856 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005857 {
5858 add_here = TRUE;
5859 add_state = t->state->out;
5860 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005861 break;
5862
5863 case NFA_VCOL:
5864 case NFA_VCOL_GT:
5865 case NFA_VCOL_LT:
5866 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5867 (long_u)win_linetabsize(
5868 reg_win == NULL ? curwin : reg_win,
5869 regline, (colnr_T)(reginput - regline)) + 1);
5870 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005871 {
5872 add_here = TRUE;
5873 add_state = t->state->out;
5874 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005875 break;
5876
Bram Moolenaar044aa292013-06-04 21:27:38 +02005877 case NFA_MARK:
5878 case NFA_MARK_GT:
5879 case NFA_MARK_LT:
5880 {
5881 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5882
5883 /* Compare the mark position to the match position. */
5884 result = (pos != NULL /* mark doesn't exist */
5885 && pos->lnum > 0 /* mark isn't set in reg_buf */
5886 && (pos->lnum == reglnum + reg_firstlnum
5887 ? (pos->col == (colnr_T)(reginput - regline)
5888 ? t->state->c == NFA_MARK
5889 : (pos->col < (colnr_T)(reginput - regline)
5890 ? t->state->c == NFA_MARK_GT
5891 : t->state->c == NFA_MARK_LT))
5892 : (pos->lnum < reglnum + reg_firstlnum
5893 ? t->state->c == NFA_MARK_GT
5894 : t->state->c == NFA_MARK_LT)));
5895 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005896 {
5897 add_here = TRUE;
5898 add_state = t->state->out;
5899 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005900 break;
5901 }
5902
Bram Moolenaar423532e2013-05-29 21:14:42 +02005903 case NFA_CURSOR:
5904 result = (reg_win != NULL
5905 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5906 && ((colnr_T)(reginput - regline)
5907 == reg_win->w_cursor.col));
5908 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005909 {
5910 add_here = TRUE;
5911 add_state = t->state->out;
5912 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005913 break;
5914
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005915 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005916#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005917 result = reg_match_visual();
5918 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005919 {
5920 add_here = TRUE;
5921 add_state = t->state->out;
5922 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005923#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005924 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005925
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005926 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005927 {
5928 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005929
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005930 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005931 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005932 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005933 result = (c == curc);
5934
5935 if (!result && ireg_ic)
5936 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005937#ifdef FEAT_MBYTE
5938 /* If there is a composing character which is not being
5939 * ignored there can be no match. Match with composing
5940 * character uses NFA_COMPOSING above. */
5941 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005942 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005943 result = FALSE;
5944#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005945 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005946 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005947 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005948
5949 } /* switch (t->state->c) */
5950
5951 if (add_state != NULL)
5952 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005953 nfa_pim_T *pim;
5954
5955 if (t->pim.result == NFA_PIM_UNUSED)
5956 pim = NULL;
5957 else
5958 pim = &t->pim;
5959
5960 /* Handle the postponed invisible match if the match might end
5961 * without advancing and before the end of the line. */
5962 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005963 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005964 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005965 {
5966#ifdef ENABLE_LOG
5967 fprintf(log_fd, "\n");
5968 fprintf(log_fd, "==================================\n");
5969 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5970 fprintf(log_fd, "\n");
5971#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005972 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005973 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005974 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005975 /* for \@! and \@<! it is a match when the result is
5976 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005977 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005978 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
5979 || pim->state->c
5980 == NFA_START_INVISIBLE_BEFORE_NEG
5981 || pim->state->c
5982 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005983 {
5984 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005985 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005986#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005987 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005988 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005989#endif
5990 }
5991 }
5992 else
5993 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005994 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005995#ifdef ENABLE_LOG
5996 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005997 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005998 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5999 fprintf(log_fd, "\n");
6000#endif
6001 }
6002
Bram Moolenaardecd9542013-06-07 16:31:50 +02006003 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006004 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006005 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6006 || pim->state->c
6007 == NFA_START_INVISIBLE_BEFORE_NEG
6008 || pim->state->c
6009 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006010 {
6011 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006012 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006013#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006014 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006015 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006016#endif
6017 }
6018 else
6019 /* look-behind match failed, don't add the state */
6020 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006021
6022 /* Postponed invisible match was handled, don't add it to
6023 * following states. */
6024 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006025 }
6026
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006027 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006028 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006029 else
6030 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006031 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006032 if (add_count > 0)
6033 nextlist->t[nextlist->n - 1].count = add_count;
6034 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006035 }
6036
6037 } /* for (thislist = thislist; thislist->state; thislist++) */
6038
Bram Moolenaare23febd2013-05-26 18:40:14 +02006039 /* Look for the start of a match in the current position by adding the
6040 * start state to the list of states.
6041 * The first found match is the leftmost one, thus the order of states
6042 * matters!
6043 * Do not add the start state in recursive calls of nfa_regmatch(),
6044 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006045 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006046 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006047 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006048 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006049 && reglnum == 0
6050 && clen != 0
6051 && (ireg_maxcol == 0
6052 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006053 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006054 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006055 ? (reglnum < nfa_endp->se_u.pos.lnum
6056 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006057 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006058 < nfa_endp->se_u.pos.col))
6059 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006060 {
6061#ifdef ENABLE_LOG
6062 fprintf(log_fd, "(---) STARTSTATE\n");
6063#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006064 /* Inline optimized code for addstate() if we know the state is
6065 * the first MOPEN. */
6066 if (toplevel)
6067 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006068 int add = TRUE;
6069 int c;
6070
6071 if (prog->regstart != NUL && clen != 0)
6072 {
6073 if (nextlist->n == 0)
6074 {
6075 colnr_T col = (colnr_T)(reginput - regline) + clen;
6076
6077 /* Nextlist is empty, we can skip ahead to the
6078 * character that must appear at the start. */
6079 if (skip_to_start(prog->regstart, &col) == FAIL)
6080 break;
6081#ifdef ENABLE_LOG
6082 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6083 col - ((colnr_T)(reginput - regline) + clen));
6084#endif
6085 reginput = regline + col - clen;
6086 }
6087 else
6088 {
6089 /* Checking if the required start character matches is
6090 * cheaper than adding a state that won't match. */
6091 c = PTR2CHAR(reginput + clen);
6092 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6093 != MB_TOLOWER(prog->regstart)))
6094 {
6095#ifdef ENABLE_LOG
6096 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6097#endif
6098 add = FALSE;
6099 }
6100 }
6101 }
6102
6103 if (add)
6104 {
6105 if (REG_MULTI)
6106 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006107 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006108 else
6109 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006110 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006111 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006112 }
6113 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006114 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115 }
6116
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117#ifdef ENABLE_LOG
6118 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006119 {
6120 int i;
6121
6122 for (i = 0; i < thislist->n; i++)
6123 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6124 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006125 fprintf(log_fd, "\n");
6126#endif
6127
6128nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006129 /* Advance to the next character, or advance to the next line, or
6130 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006131 if (clen != 0)
6132 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006133 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6134 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006135 reg_nextline();
6136 else
6137 break;
6138 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006139
6140#ifdef ENABLE_LOG
6141 if (log_fd != stderr)
6142 fclose(log_fd);
6143 log_fd = NULL;
6144#endif
6145
6146theend:
6147 /* Free memory */
6148 vim_free(list[0].t);
6149 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006150 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006151#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006152#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006153 fclose(debug);
6154#endif
6155
Bram Moolenaar963fee22013-05-26 21:47:28 +02006156 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006157}
6158
6159/*
6160 * Try match of "prog" with at regline["col"].
6161 * Returns 0 for failure, number of lines contained in the match otherwise.
6162 */
6163 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006164nfa_regtry(prog, col)
6165 nfa_regprog_T *prog;
6166 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006167{
6168 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006169 regsubs_T subs, m;
6170 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171#ifdef ENABLE_LOG
6172 FILE *f;
6173#endif
6174
6175 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006176
6177#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006178 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006179 if (f != NULL)
6180 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006181 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006182#ifdef DEBUG
6183 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6184#endif
6185 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006186 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006187 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006188 fprintf(f, "\n\n");
6189 fclose(f);
6190 }
6191 else
6192 EMSG(_("Could not open temporary log file for writing "));
6193#endif
6194
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006195 clear_sub(&subs.norm);
6196 clear_sub(&m.norm);
6197#ifdef FEAT_SYN_HL
6198 clear_sub(&subs.synt);
6199 clear_sub(&m.synt);
6200#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006201
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006202 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006203 return 0;
6204
6205 cleanup_subexpr();
6206 if (REG_MULTI)
6207 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006208 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006209 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006210 reg_startpos[i] = subs.norm.list.multi[i].start;
6211 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006212 }
6213
6214 if (reg_startpos[0].lnum < 0)
6215 {
6216 reg_startpos[0].lnum = 0;
6217 reg_startpos[0].col = col;
6218 }
6219 if (reg_endpos[0].lnum < 0)
6220 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006221 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006222 reg_endpos[0].lnum = reglnum;
6223 reg_endpos[0].col = (int)(reginput - regline);
6224 }
6225 else
6226 /* Use line number of "\ze". */
6227 reglnum = reg_endpos[0].lnum;
6228 }
6229 else
6230 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006231 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006232 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006233 reg_startp[i] = subs.norm.list.line[i].start;
6234 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006235 }
6236
6237 if (reg_startp[0] == NULL)
6238 reg_startp[0] = regline + col;
6239 if (reg_endp[0] == NULL)
6240 reg_endp[0] = reginput;
6241 }
6242
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006243#ifdef FEAT_SYN_HL
6244 /* Package any found \z(...\) matches for export. Default is none. */
6245 unref_extmatch(re_extmatch_out);
6246 re_extmatch_out = NULL;
6247
6248 if (prog->reghasz == REX_SET)
6249 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006250 cleanup_zsubexpr();
6251 re_extmatch_out = make_extmatch();
6252 for (i = 0; i < subs.synt.in_use; i++)
6253 {
6254 if (REG_MULTI)
6255 {
6256 struct multipos *mpos = &subs.synt.list.multi[i];
6257
6258 /* Only accept single line matches. */
6259 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6260 re_extmatch_out->matches[i] =
6261 vim_strnsave(reg_getline(mpos->start.lnum)
6262 + mpos->start.col,
6263 mpos->end.col - mpos->start.col);
6264 }
6265 else
6266 {
6267 struct linepos *lpos = &subs.synt.list.line[i];
6268
6269 if (lpos->start != NULL && lpos->end != NULL)
6270 re_extmatch_out->matches[i] =
6271 vim_strnsave(lpos->start,
6272 (int)(lpos->end - lpos->start));
6273 }
6274 }
6275 }
6276#endif
6277
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006278 return 1 + reglnum;
6279}
6280
6281/*
6282 * Match a regexp against a string ("line" points to the string) or multiple
6283 * lines ("line" is NULL, use reg_getline()).
6284 *
6285 * Returns 0 for failure, number of lines contained in the match otherwise.
6286 */
6287 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006288nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006289 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006290 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006291{
6292 nfa_regprog_T *prog;
6293 long retval = 0L;
6294 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006295 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006296
6297 if (REG_MULTI)
6298 {
6299 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6300 line = reg_getline((linenr_T)0); /* relative to the cursor */
6301 reg_startpos = reg_mmatch->startpos;
6302 reg_endpos = reg_mmatch->endpos;
6303 }
6304 else
6305 {
6306 prog = (nfa_regprog_T *)reg_match->regprog;
6307 reg_startp = reg_match->startp;
6308 reg_endp = reg_match->endp;
6309 }
6310
6311 /* Be paranoid... */
6312 if (prog == NULL || line == NULL)
6313 {
6314 EMSG(_(e_null));
6315 goto theend;
6316 }
6317
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6319 if (prog->regflags & RF_ICASE)
6320 ireg_ic = TRUE;
6321 else if (prog->regflags & RF_NOICASE)
6322 ireg_ic = FALSE;
6323
6324#ifdef FEAT_MBYTE
6325 /* If pattern contains "\Z" overrule value of ireg_icombine */
6326 if (prog->regflags & RF_ICOMBINE)
6327 ireg_icombine = TRUE;
6328#endif
6329
6330 regline = line;
6331 reglnum = 0; /* relative to line */
6332
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006333 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006334 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006335 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006336 nfa_listid = 1;
6337 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006338#ifdef DEBUG
6339 nfa_regengine.expr = prog->pattern;
6340#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006341
Bram Moolenaard89616e2013-06-06 18:46:06 +02006342 if (prog->reganch && col > 0)
6343 return 0L;
6344
Bram Moolenaar473de612013-06-08 18:19:48 +02006345 need_clear_subexpr = TRUE;
6346#ifdef FEAT_SYN_HL
6347 /* Clear the external match subpointers if necessary. */
6348 if (prog->reghasz == REX_SET)
6349 {
6350 nfa_has_zsubexpr = TRUE;
6351 need_clear_zsubexpr = TRUE;
6352 }
6353 else
6354 nfa_has_zsubexpr = FALSE;
6355#endif
6356
Bram Moolenaard89616e2013-06-06 18:46:06 +02006357 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006358 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006359 /* Skip ahead until a character we know the match must start with.
6360 * When there is none there is no match. */
6361 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006362 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006363
Bram Moolenaar473de612013-06-08 18:19:48 +02006364 /* If match_text is set it contains the full text that must match.
6365 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006366 if (prog->match_text != NULL
6367#ifdef FEAT_MBYTE
6368 && !ireg_icombine
6369#endif
6370 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006371 return find_match_text(col, prog->regstart, prog->match_text);
6372 }
6373
Bram Moolenaard89616e2013-06-06 18:46:06 +02006374 /* If the start column is past the maximum column: no need to try. */
6375 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6376 goto theend;
6377
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006378 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379 for (i = 0; i < nstate; ++i)
6380 {
6381 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006382 prog->state[i].lastlist[0] = 0;
6383 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384 }
6385
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006386 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006387
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006388#ifdef DEBUG
6389 nfa_regengine.expr = NULL;
6390#endif
6391
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006392theend:
6393 return retval;
6394}
6395
6396/*
6397 * Compile a regular expression into internal code for the NFA matcher.
6398 * Returns the program in allocated space. Returns NULL for an error.
6399 */
6400 static regprog_T *
6401nfa_regcomp(expr, re_flags)
6402 char_u *expr;
6403 int re_flags;
6404{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006405 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006406 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006407 int *postfix;
6408
6409 if (expr == NULL)
6410 return NULL;
6411
6412#ifdef DEBUG
6413 nfa_regengine.expr = expr;
6414#endif
6415
6416 init_class_tab();
6417
6418 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6419 return NULL;
6420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006421 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006422 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006423 postfix = re2post();
6424 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006425 {
6426 /* TODO: only give this error for debugging? */
6427 if (post_ptr >= post_end)
6428 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006429 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006430 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006431
6432 /*
6433 * In order to build the NFA, we parse the input regexp twice:
6434 * 1. first pass to count size (so we can allocate space)
6435 * 2. second to emit code
6436 */
6437#ifdef ENABLE_LOG
6438 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006439 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006440
6441 if (f != NULL)
6442 {
6443 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6444 fclose(f);
6445 }
6446 }
6447#endif
6448
6449 /*
6450 * PASS 1
6451 * Count number of NFA states in "nstate". Do not build the NFA.
6452 */
6453 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006454
Bram Moolenaar16619a22013-06-11 18:42:36 +02006455 /* allocate the regprog with space for the compiled regexp */
6456 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006457 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6458 if (prog == NULL)
6459 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006460 state_ptr = prog->state;
6461
6462 /*
6463 * PASS 2
6464 * Build the NFA
6465 */
6466 prog->start = post2nfa(postfix, post_ptr, FALSE);
6467 if (prog->start == NULL)
6468 goto fail;
6469
6470 prog->regflags = regflags;
6471 prog->engine = &nfa_regengine;
6472 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006473 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006474 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006475 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006476
Bram Moolenaara2947e22013-06-11 22:44:09 +02006477 nfa_postprocess(prog);
6478
Bram Moolenaard89616e2013-06-06 18:46:06 +02006479 prog->reganch = nfa_get_reganch(prog->start, 0);
6480 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006481 prog->match_text = nfa_get_match_text(prog->start);
6482
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006483#ifdef ENABLE_LOG
6484 nfa_postfix_dump(expr, OK);
6485 nfa_dump(prog);
6486#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006487#ifdef FEAT_SYN_HL
6488 /* Remember whether this pattern has any \z specials in it. */
6489 prog->reghasz = re_has_z;
6490#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006491#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006492 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006493 nfa_regengine.expr = NULL;
6494#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006495
6496out:
6497 vim_free(post_start);
6498 post_start = post_ptr = post_end = NULL;
6499 state_ptr = NULL;
6500 return (regprog_T *)prog;
6501
6502fail:
6503 vim_free(prog);
6504 prog = NULL;
6505#ifdef ENABLE_LOG
6506 nfa_postfix_dump(expr, FAIL);
6507#endif
6508#ifdef DEBUG
6509 nfa_regengine.expr = NULL;
6510#endif
6511 goto out;
6512}
6513
Bram Moolenaar473de612013-06-08 18:19:48 +02006514/*
6515 * Free a compiled regexp program, returned by nfa_regcomp().
6516 */
6517 static void
6518nfa_regfree(prog)
6519 regprog_T *prog;
6520{
6521 if (prog != NULL)
6522 {
6523 vim_free(((nfa_regprog_T *)prog)->match_text);
6524#ifdef DEBUG
6525 vim_free(((nfa_regprog_T *)prog)->pattern);
6526#endif
6527 vim_free(prog);
6528 }
6529}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006530
6531/*
6532 * Match a regexp against a string.
6533 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6534 * Uses curbuf for line count and 'iskeyword'.
6535 *
6536 * Return TRUE if there is a match, FALSE if not.
6537 */
6538 static int
6539nfa_regexec(rmp, line, col)
6540 regmatch_T *rmp;
6541 char_u *line; /* string to match against */
6542 colnr_T col; /* column to start looking for match */
6543{
6544 reg_match = rmp;
6545 reg_mmatch = NULL;
6546 reg_maxline = 0;
6547 reg_line_lbr = FALSE;
6548 reg_buf = curbuf;
6549 reg_win = NULL;
6550 ireg_ic = rmp->rm_ic;
6551#ifdef FEAT_MBYTE
6552 ireg_icombine = FALSE;
6553#endif
6554 ireg_maxcol = 0;
6555 return (nfa_regexec_both(line, col) != 0);
6556}
6557
6558#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6559 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6560
6561static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6562
6563/*
6564 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6565 */
6566 static int
6567nfa_regexec_nl(rmp, line, col)
6568 regmatch_T *rmp;
6569 char_u *line; /* string to match against */
6570 colnr_T col; /* column to start looking for match */
6571{
6572 reg_match = rmp;
6573 reg_mmatch = NULL;
6574 reg_maxline = 0;
6575 reg_line_lbr = TRUE;
6576 reg_buf = curbuf;
6577 reg_win = NULL;
6578 ireg_ic = rmp->rm_ic;
6579#ifdef FEAT_MBYTE
6580 ireg_icombine = FALSE;
6581#endif
6582 ireg_maxcol = 0;
6583 return (nfa_regexec_both(line, col) != 0);
6584}
6585#endif
6586
6587
6588/*
6589 * Match a regexp against multiple lines.
6590 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6591 * Uses curbuf for line count and 'iskeyword'.
6592 *
6593 * Return zero if there is no match. Return number of lines contained in the
6594 * match otherwise.
6595 *
6596 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6597 *
6598 * ! Also NOTE : match may actually be in another line. e.g.:
6599 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6600 *
6601 * +-------------------------+
6602 * |a |
6603 * |b |
6604 * |c |
6605 * | |
6606 * +-------------------------+
6607 *
6608 * then nfa_regexec_multi() returns 3. while the original
6609 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6610 *
6611 * FIXME if this behavior is not compatible.
6612 */
6613 static long
6614nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6615 regmmatch_T *rmp;
6616 win_T *win; /* window in which to search or NULL */
6617 buf_T *buf; /* buffer in which to search */
6618 linenr_T lnum; /* nr of line to start looking for match */
6619 colnr_T col; /* column to start looking for match */
6620 proftime_T *tm UNUSED; /* timeout limit or NULL */
6621{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006622 reg_match = NULL;
6623 reg_mmatch = rmp;
6624 reg_buf = buf;
6625 reg_win = win;
6626 reg_firstlnum = lnum;
6627 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6628 reg_line_lbr = FALSE;
6629 ireg_ic = rmp->rmm_ic;
6630#ifdef FEAT_MBYTE
6631 ireg_icombine = FALSE;
6632#endif
6633 ireg_maxcol = rmp->rmm_maxcol;
6634
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006635 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006636}
6637
6638#ifdef DEBUG
6639# undef ENABLE_LOG
6640#endif