blob: e843cd742c71381cab8a6b51035d81bade0a482a [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.
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003939 * Except when at the end of the line, or when we are going to the
3940 * next line for a look-behind match. */
Bram Moolenaar307aa162013-06-02 16:34:21 +02003941 if (reginput > regline
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003942 && *reginput != NUL
Bram Moolenaar307aa162013-06-02 16:34:21 +02003943 && (nfa_endp == NULL
3944 || !REG_MULTI
3945 || reglnum == nfa_endp->se_u.pos.lnum))
3946 goto skip_add;
3947 /* FALLTHROUGH */
3948
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003949 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003950 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003951 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003952 /* This state is already in the list, don't add it again,
3953 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003954 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003955 {
3956skip_add:
3957#ifdef ENABLE_LOG
3958 nfa_set_code(state->c);
3959 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3960 abs(state->id), l->id, state->c, code);
3961#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003962 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003963 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003964
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003965 /* Do not add the state again when it exists with the same
3966 * positions. */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003967 if (has_state_with_pos(l, state, subs))
3968 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003969 }
3970
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003971 /* When there are backreferences the number of states may be (a
3972 * lot) bigger than anticipated. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003973 if (nfa_has_backref && l->n == l->len)
3974 {
3975 int newlen = l->len * 3 / 2 + 50;
3976
3977 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3978 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003979 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003980
3981 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003982 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003983 thread = &l->t[l->n++];
3984 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003985 if (pim == NULL)
3986 thread->pim.result = NFA_PIM_UNUSED;
3987 else
3988 copy_pim(&thread->pim, pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003989 copy_sub(&thread->subs.norm, &subs->norm);
3990#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003991 if (nfa_has_zsubexpr)
3992 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003993#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003994#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003995 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003996 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003997#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003998 }
3999
4000#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004001 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004002 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004003#endif
4004 switch (state->c)
4005 {
4006 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004007 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004008 break;
4009
4010 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004011 /* order matters here */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004012 addstate(l, state->out, subs, pim, off);
4013 addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004014 break;
4015
Bram Moolenaar5714b802013-05-28 22:03:20 +02004016 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004017 case NFA_NOPEN:
4018 case NFA_NCLOSE:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004019 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004020 break;
4021
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004022 case NFA_MOPEN:
4023 case NFA_MOPEN1:
4024 case NFA_MOPEN2:
4025 case NFA_MOPEN3:
4026 case NFA_MOPEN4:
4027 case NFA_MOPEN5:
4028 case NFA_MOPEN6:
4029 case NFA_MOPEN7:
4030 case NFA_MOPEN8:
4031 case NFA_MOPEN9:
4032#ifdef FEAT_SYN_HL
4033 case NFA_ZOPEN:
4034 case NFA_ZOPEN1:
4035 case NFA_ZOPEN2:
4036 case NFA_ZOPEN3:
4037 case NFA_ZOPEN4:
4038 case NFA_ZOPEN5:
4039 case NFA_ZOPEN6:
4040 case NFA_ZOPEN7:
4041 case NFA_ZOPEN8:
4042 case NFA_ZOPEN9:
4043#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004044 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004045 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004046 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004047 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004048 sub = &subs->norm;
4049 }
4050#ifdef FEAT_SYN_HL
4051 else if (state->c >= NFA_ZOPEN)
4052 {
4053 subidx = state->c - NFA_ZOPEN;
4054 sub = &subs->synt;
4055 }
4056#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004057 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004058 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004059 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004060 sub = &subs->norm;
4061 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004062
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004063 /* Set the position (with "off" added) in the subexpression. Save
4064 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02004065 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004066 if (REG_MULTI)
4067 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004068 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004069 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004070 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004071 save_in_use = -1;
4072 }
4073 else
4074 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004075 save_in_use = sub->in_use;
4076 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004077 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004078 sub->list.multi[i].start.lnum = -1;
4079 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004080 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004081 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004082 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004083 if (off == -1)
4084 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004085 sub->list.multi[subidx].start.lnum = reglnum + 1;
4086 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004087 }
4088 else
4089 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004090 sub->list.multi[subidx].start.lnum = reglnum;
4091 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004092 (colnr_T)(reginput - regline + off);
4093 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004094 }
4095 else
4096 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004097 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004098 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004099 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004100 save_in_use = -1;
4101 }
4102 else
4103 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004104 save_in_use = sub->in_use;
4105 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004106 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004107 sub->list.line[i].start = NULL;
4108 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004109 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004110 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004111 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004112 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 }
4114
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004115 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004116
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004117 if (save_in_use == -1)
4118 {
4119 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004120 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004121 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004122 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004123 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004124 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004125 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004126 break;
4127
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004128 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004129 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004130 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004131 /* Do not overwrite the position set by \ze. If no \ze
4132 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004133 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004134 break;
4135 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004136 case NFA_MCLOSE1:
4137 case NFA_MCLOSE2:
4138 case NFA_MCLOSE3:
4139 case NFA_MCLOSE4:
4140 case NFA_MCLOSE5:
4141 case NFA_MCLOSE6:
4142 case NFA_MCLOSE7:
4143 case NFA_MCLOSE8:
4144 case NFA_MCLOSE9:
4145#ifdef FEAT_SYN_HL
4146 case NFA_ZCLOSE:
4147 case NFA_ZCLOSE1:
4148 case NFA_ZCLOSE2:
4149 case NFA_ZCLOSE3:
4150 case NFA_ZCLOSE4:
4151 case NFA_ZCLOSE5:
4152 case NFA_ZCLOSE6:
4153 case NFA_ZCLOSE7:
4154 case NFA_ZCLOSE8:
4155 case NFA_ZCLOSE9:
4156#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004157 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004158 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004159 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004160 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004161 sub = &subs->norm;
4162 }
4163#ifdef FEAT_SYN_HL
4164 else if (state->c >= NFA_ZCLOSE)
4165 {
4166 subidx = state->c - NFA_ZCLOSE;
4167 sub = &subs->synt;
4168 }
4169#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004170 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004171 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004172 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004173 sub = &subs->norm;
4174 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004175
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004176 /* We don't fill in gaps here, there must have been an MOPEN that
4177 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004178 save_in_use = sub->in_use;
4179 if (sub->in_use <= subidx)
4180 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004181 if (REG_MULTI)
4182 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004183 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004184 if (off == -1)
4185 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004186 sub->list.multi[subidx].end.lnum = reglnum + 1;
4187 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004188 }
4189 else
4190 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004191 sub->list.multi[subidx].end.lnum = reglnum;
4192 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004193 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004194 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004195 }
4196 else
4197 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004198 save_ptr = sub->list.line[subidx].end;
4199 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004200 }
4201
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004202 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004203
4204 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004205 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004206 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004207 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004208 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004209 break;
4210 }
4211}
4212
4213/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004214 * Like addstate(), but the new state(s) are put at position "*ip".
4215 * Used for zero-width matches, next state to use is the added one.
4216 * This makes sure the order of states to be tried does not change, which
4217 * matters for alternatives.
4218 */
4219 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004220addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004221 nfa_list_T *l; /* runtime state list */
4222 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004223 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004224 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004225 int *ip;
4226{
4227 int tlen = l->n;
4228 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004229 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004230
4231 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004232 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004233
Bram Moolenaar4b417062013-05-25 20:19:50 +02004234 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004235 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004236 return;
4237
4238 /* re-order to put the new state at the current position */
4239 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004240 if (count == 1)
4241 {
4242 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004243 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004244 }
4245 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004246 {
4247 /* make space for new states, then move them from the
4248 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004249 mch_memmove(&(l->t[listidx + count]),
4250 &(l->t[listidx + 1]),
4251 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4252 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02004253 &(l->t[l->n - 1]),
4254 sizeof(nfa_thread_T) * count);
4255 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004256 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004257 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004258}
4259
4260/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004261 * Check character class "class" against current character c.
4262 */
4263 static int
4264check_char_class(class, c)
4265 int class;
4266 int c;
4267{
4268 switch (class)
4269 {
4270 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004271 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004272 return OK;
4273 break;
4274 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004275 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004276 return OK;
4277 break;
4278 case NFA_CLASS_BLANK:
4279 if (c == ' ' || c == '\t')
4280 return OK;
4281 break;
4282 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004283 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004284 return OK;
4285 break;
4286 case NFA_CLASS_DIGIT:
4287 if (VIM_ISDIGIT(c))
4288 return OK;
4289 break;
4290 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004291 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004292 return OK;
4293 break;
4294 case NFA_CLASS_LOWER:
4295 if (MB_ISLOWER(c))
4296 return OK;
4297 break;
4298 case NFA_CLASS_PRINT:
4299 if (vim_isprintc(c))
4300 return OK;
4301 break;
4302 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004303 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304 return OK;
4305 break;
4306 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004307 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308 return OK;
4309 break;
4310 case NFA_CLASS_UPPER:
4311 if (MB_ISUPPER(c))
4312 return OK;
4313 break;
4314 case NFA_CLASS_XDIGIT:
4315 if (vim_isxdigit(c))
4316 return OK;
4317 break;
4318 case NFA_CLASS_TAB:
4319 if (c == '\t')
4320 return OK;
4321 break;
4322 case NFA_CLASS_RETURN:
4323 if (c == '\r')
4324 return OK;
4325 break;
4326 case NFA_CLASS_BACKSPACE:
4327 if (c == '\b')
4328 return OK;
4329 break;
4330 case NFA_CLASS_ESCAPE:
4331 if (c == '\033')
4332 return OK;
4333 break;
4334
4335 default:
4336 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004337 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4338 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004339 }
4340 return FAIL;
4341}
4342
Bram Moolenaar5714b802013-05-28 22:03:20 +02004343static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
4344
4345/*
4346 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004347 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004348 */
4349 static int
4350match_backref(sub, subidx, bytelen)
4351 regsub_T *sub; /* pointers to subexpressions */
4352 int subidx;
4353 int *bytelen; /* out: length of match in bytes */
4354{
4355 int len;
4356
4357 if (sub->in_use <= subidx)
4358 {
4359retempty:
4360 /* backref was not set, match an empty string */
4361 *bytelen = 0;
4362 return TRUE;
4363 }
4364
4365 if (REG_MULTI)
4366 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004367 if (sub->list.multi[subidx].start.lnum < 0
4368 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004369 goto retempty;
4370 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004371 len = sub->list.multi[subidx].end.col
4372 - sub->list.multi[subidx].start.col;
4373 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004374 reginput, &len) == 0)
4375 {
4376 *bytelen = len;
4377 return TRUE;
4378 }
4379 }
4380 else
4381 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004382 if (sub->list.line[subidx].start == NULL
4383 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004384 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004385 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4386 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004387 {
4388 *bytelen = len;
4389 return TRUE;
4390 }
4391 }
4392 return FALSE;
4393}
4394
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004395#ifdef FEAT_SYN_HL
4396
4397static int match_zref __ARGS((int subidx, int *bytelen));
4398
4399/*
4400 * Check for a match with \z subexpression "subidx".
4401 * Return TRUE if it matches.
4402 */
4403 static int
4404match_zref(subidx, bytelen)
4405 int subidx;
4406 int *bytelen; /* out: length of match in bytes */
4407{
4408 int len;
4409
4410 cleanup_zsubexpr();
4411 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4412 {
4413 /* backref was not set, match an empty string */
4414 *bytelen = 0;
4415 return TRUE;
4416 }
4417
4418 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4419 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4420 {
4421 *bytelen = len;
4422 return TRUE;
4423 }
4424 return FALSE;
4425}
4426#endif
4427
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004428/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004429 * Save list IDs for all NFA states of "prog" into "list".
4430 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004431 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004432 */
4433 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004434nfa_save_listids(prog, list)
4435 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004436 int *list;
4437{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004438 int i;
4439 nfa_state_T *p;
4440
4441 /* Order in the list is reverse, it's a bit faster that way. */
4442 p = &prog->state[0];
4443 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004444 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004445 list[i] = p->lastlist[1];
4446 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004447 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004448 }
4449}
4450
4451/*
4452 * Restore list IDs from "list" to all NFA states.
4453 */
4454 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004455nfa_restore_listids(prog, list)
4456 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004457 int *list;
4458{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004459 int i;
4460 nfa_state_T *p;
4461
4462 p = &prog->state[0];
4463 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004464 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004465 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004466 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004467 }
4468}
4469
Bram Moolenaar423532e2013-05-29 21:14:42 +02004470 static int
4471nfa_re_num_cmp(val, op, pos)
4472 long_u val;
4473 int op;
4474 long_u pos;
4475{
4476 if (op == 1) return pos > val;
4477 if (op == 2) return pos < val;
4478 return val == pos;
4479}
4480
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004481static 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 +02004482static 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 +02004483
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004484/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004485 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004486 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4487 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004488 */
4489 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004490recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004491 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004492 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004493 nfa_regprog_T *prog;
4494 regsubs_T *submatch;
4495 regsubs_T *m;
4496 int **listids;
4497{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004498 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004499 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 */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004635 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02004636 if (REG_MULTI)
4637 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004638 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004639 nfa_match = save_nfa_match;
4640 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004641 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004642
4643#ifdef ENABLE_LOG
4644 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4645 if (log_fd != NULL)
4646 {
4647 fprintf(log_fd, "****************************\n");
4648 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4649 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4650 fprintf(log_fd, "****************************\n");
4651 }
4652 else
4653 {
4654 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4655 log_fd = stderr;
4656 }
4657#endif
4658
4659 return result;
4660}
4661
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004662static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004663static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004664
4665/*
4666 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004667 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004668 * NFA_ANY: 1
4669 * specific character: 99
4670 */
4671 static int
4672failure_chance(state, depth)
4673 nfa_state_T *state;
4674 int depth;
4675{
4676 int c = state->c;
4677 int l, r;
4678
4679 /* detect looping */
4680 if (depth > 4)
4681 return 1;
4682
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004683 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004684 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004685 case NFA_SPLIT:
4686 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4687 /* avoid recursive stuff */
4688 return 1;
4689 /* two alternatives, use the lowest failure chance */
4690 l = failure_chance(state->out, depth + 1);
4691 r = failure_chance(state->out1, depth + 1);
4692 return l < r ? l : r;
4693
4694 case NFA_ANY:
4695 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004696 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004697
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004698 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004699 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004700 /* empty match works always */
4701 return 0;
4702
4703 case NFA_BOL:
4704 case NFA_EOL:
4705 case NFA_BOF:
4706 case NFA_EOF:
4707 case NFA_NEWL:
4708 return 99;
4709
4710 case NFA_BOW:
4711 case NFA_EOW:
4712 return 90;
4713
4714 case NFA_MOPEN:
4715 case NFA_MOPEN1:
4716 case NFA_MOPEN2:
4717 case NFA_MOPEN3:
4718 case NFA_MOPEN4:
4719 case NFA_MOPEN5:
4720 case NFA_MOPEN6:
4721 case NFA_MOPEN7:
4722 case NFA_MOPEN8:
4723 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004724#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004725 case NFA_ZOPEN:
4726 case NFA_ZOPEN1:
4727 case NFA_ZOPEN2:
4728 case NFA_ZOPEN3:
4729 case NFA_ZOPEN4:
4730 case NFA_ZOPEN5:
4731 case NFA_ZOPEN6:
4732 case NFA_ZOPEN7:
4733 case NFA_ZOPEN8:
4734 case NFA_ZOPEN9:
4735 case NFA_ZCLOSE:
4736 case NFA_ZCLOSE1:
4737 case NFA_ZCLOSE2:
4738 case NFA_ZCLOSE3:
4739 case NFA_ZCLOSE4:
4740 case NFA_ZCLOSE5:
4741 case NFA_ZCLOSE6:
4742 case NFA_ZCLOSE7:
4743 case NFA_ZCLOSE8:
4744 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004745#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004746 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004747 case NFA_MCLOSE1:
4748 case NFA_MCLOSE2:
4749 case NFA_MCLOSE3:
4750 case NFA_MCLOSE4:
4751 case NFA_MCLOSE5:
4752 case NFA_MCLOSE6:
4753 case NFA_MCLOSE7:
4754 case NFA_MCLOSE8:
4755 case NFA_MCLOSE9:
4756 case NFA_NCLOSE:
4757 return failure_chance(state->out, depth + 1);
4758
4759 case NFA_BACKREF1:
4760 case NFA_BACKREF2:
4761 case NFA_BACKREF3:
4762 case NFA_BACKREF4:
4763 case NFA_BACKREF5:
4764 case NFA_BACKREF6:
4765 case NFA_BACKREF7:
4766 case NFA_BACKREF8:
4767 case NFA_BACKREF9:
4768#ifdef FEAT_SYN_HL
4769 case NFA_ZREF1:
4770 case NFA_ZREF2:
4771 case NFA_ZREF3:
4772 case NFA_ZREF4:
4773 case NFA_ZREF5:
4774 case NFA_ZREF6:
4775 case NFA_ZREF7:
4776 case NFA_ZREF8:
4777 case NFA_ZREF9:
4778#endif
4779 /* backreferences don't match in many places */
4780 return 94;
4781
4782 case NFA_LNUM_GT:
4783 case NFA_LNUM_LT:
4784 case NFA_COL_GT:
4785 case NFA_COL_LT:
4786 case NFA_VCOL_GT:
4787 case NFA_VCOL_LT:
4788 case NFA_MARK_GT:
4789 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004790 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004791 /* before/after positions don't match very often */
4792 return 85;
4793
4794 case NFA_LNUM:
4795 return 90;
4796
4797 case NFA_CURSOR:
4798 case NFA_COL:
4799 case NFA_VCOL:
4800 case NFA_MARK:
4801 /* specific positions rarely match */
4802 return 98;
4803
4804 case NFA_COMPOSING:
4805 return 95;
4806
4807 default:
4808 if (c > 0)
4809 /* character match fails often */
4810 return 95;
4811 }
4812
4813 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004814 return 50;
4815}
4816
Bram Moolenaarf46da702013-06-02 22:37:42 +02004817/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004818 * Skip until the char "c" we know a match must start with.
4819 */
4820 static int
4821skip_to_start(c, colp)
4822 int c;
4823 colnr_T *colp;
4824{
4825 char_u *s;
4826
4827 /* Used often, do some work to avoid call overhead. */
4828 if (!ireg_ic
4829#ifdef FEAT_MBYTE
4830 && !has_mbyte
4831#endif
4832 )
4833 s = vim_strbyte(regline + *colp, c);
4834 else
4835 s = cstrchr(regline + *colp, c);
4836 if (s == NULL)
4837 return FAIL;
4838 *colp = (int)(s - regline);
4839 return OK;
4840}
4841
4842/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004843 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004844 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004845 * Returns zero for no match, 1 for a match.
4846 */
4847 static long
4848find_match_text(startcol, regstart, match_text)
4849 colnr_T startcol;
4850 int regstart;
4851 char_u *match_text;
4852{
4853 colnr_T col = startcol;
4854 int c1, c2;
4855 int len1, len2;
4856 int match;
4857
4858 for (;;)
4859 {
4860 match = TRUE;
4861 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4862 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4863 {
4864 c1 = PTR2CHAR(match_text + len1);
4865 c2 = PTR2CHAR(regline + col + len2);
4866 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4867 {
4868 match = FALSE;
4869 break;
4870 }
4871 len2 += MB_CHAR2LEN(c2);
4872 }
4873 if (match
4874#ifdef FEAT_MBYTE
4875 /* check that no composing char follows */
4876 && !(enc_utf8
4877 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4878#endif
4879 )
4880 {
4881 cleanup_subexpr();
4882 if (REG_MULTI)
4883 {
4884 reg_startpos[0].lnum = reglnum;
4885 reg_startpos[0].col = col;
4886 reg_endpos[0].lnum = reglnum;
4887 reg_endpos[0].col = col + len2;
4888 }
4889 else
4890 {
4891 reg_startp[0] = regline + col;
4892 reg_endp[0] = regline + col + len2;
4893 }
4894 return 1L;
4895 }
4896
4897 /* Try finding regstart after the current match. */
4898 col += MB_CHAR2LEN(regstart); /* skip regstart */
4899 if (skip_to_start(regstart, &col) == FAIL)
4900 break;
4901 }
4902 return 0L;
4903}
4904
4905/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004906 * Main matching routine.
4907 *
4908 * Run NFA to determine whether it matches reginput.
4909 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004910 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004911 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004912 * Return TRUE if there is a match, FALSE otherwise.
4913 * Note: Caller must ensure that: start != NULL.
4914 */
4915 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004916nfa_regmatch(prog, start, submatch, m)
4917 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004918 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004919 regsubs_T *submatch;
4920 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004921{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922 int result;
4923 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004924 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004925 int go_to_nextline = FALSE;
4926 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004927 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004928 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004929 nfa_list_T *thislist;
4930 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004931 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004932 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004933 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004934 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02004935 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004936 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004937#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004938 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004939
4940 if (debug == NULL)
4941 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004942 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004943 return FALSE;
4944 }
4945#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004946 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004947
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004948 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004949 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004950 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004951 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004952 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004953 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004954 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004955 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004956
4957#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004958 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004959 if (log_fd != NULL)
4960 {
4961 fprintf(log_fd, "**********************************\n");
4962 nfa_set_code(start->c);
4963 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4964 abs(start->id), code);
4965 fprintf(log_fd, "**********************************\n");
4966 }
4967 else
4968 {
4969 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4970 log_fd = stderr;
4971 }
4972#endif
4973
4974 thislist = &list[0];
4975 thislist->n = 0;
4976 nextlist = &list[1];
4977 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004978#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004979 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004980#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004981 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004982
4983 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4984 * it's the first MOPEN. */
4985 if (toplevel)
4986 {
4987 if (REG_MULTI)
4988 {
4989 m->norm.list.multi[0].start.lnum = reglnum;
4990 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4991 }
4992 else
4993 m->norm.list.line[0].start = reginput;
4994 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004995 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004996 }
4997 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004998 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004999
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005000#define ADD_STATE_IF_MATCH(state) \
5001 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005002 add_state = state->out; \
5003 add_off = clen; \
5004 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005005
5006 /*
5007 * Run for each character.
5008 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005009 for (;;)
5010 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005011 int curc;
5012 int clen;
5013
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005014#ifdef FEAT_MBYTE
5015 if (has_mbyte)
5016 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005017 curc = (*mb_ptr2char)(reginput);
5018 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005019 }
5020 else
5021#endif
5022 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005023 curc = *reginput;
5024 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005025 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005026 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005027 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005028 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005029 go_to_nextline = FALSE;
5030 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005031
5032 /* swap lists */
5033 thislist = &list[flag];
5034 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005035 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005036 ++nfa_listid;
5037 thislist->id = nfa_listid;
5038 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005039
5040#ifdef ENABLE_LOG
5041 fprintf(log_fd, "------------------------------------------\n");
5042 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005043 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005044 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005045 {
5046 int i;
5047
5048 for (i = 0; i < thislist->n; i++)
5049 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5050 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051 fprintf(log_fd, "\n");
5052#endif
5053
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005054#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055 fprintf(debug, "\n-------------------\n");
5056#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005057 /*
5058 * If the state lists are empty we can stop.
5059 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005060 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005061 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005062
5063 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005064 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005065 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005066 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005067
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005068#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005069 nfa_set_code(t->state->c);
5070 fprintf(debug, "%s, ", code);
5071#endif
5072#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005073 {
5074 int col;
5075
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005076 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005077 col = -1;
5078 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005079 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005080 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005081 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005082 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005083 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5084 abs(t->state->id), (int)t->state->c, code, col,
5085 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005086 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005087#endif
5088
5089 /*
5090 * Handle the possible codes of the current state.
5091 * The most important is NFA_MATCH.
5092 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005093 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005094 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005095 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005096 switch (t->state->c)
5097 {
5098 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005099 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005100 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005101 copy_sub(&submatch->norm, &t->subs.norm);
5102#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005103 if (nfa_has_zsubexpr)
5104 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005105#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005106#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005107 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005108#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005109 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005110 * states at this position. When the list of states is going
5111 * to be empty quit without advancing, so that "reginput" is
5112 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005113 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005114 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005115 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005116 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005117
5118 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005119 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005120 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005121 /*
5122 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005123 * NFA_START_INVISIBLE_BEFORE node.
5124 * They surround a zero-width group, used with "\@=", "\&",
5125 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005126 * If we got here, it means that the current "invisible" group
5127 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005128 * nfa_regmatch(). For a look-behind match only when it ends
5129 * in the position in "nfa_endp".
5130 * Submatches are stored in *m, and used in the parent call.
5131 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005132#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005133 if (nfa_endp != NULL)
5134 {
5135 if (REG_MULTI)
5136 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5137 (int)reglnum,
5138 (int)nfa_endp->se_u.pos.lnum,
5139 (int)(reginput - regline),
5140 nfa_endp->se_u.pos.col);
5141 else
5142 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5143 (int)(reginput - regline),
5144 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005145 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005146#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005147 /* If "nfa_endp" is set it's only a match if it ends at
5148 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005149 if (nfa_endp != NULL && (REG_MULTI
5150 ? (reglnum != nfa_endp->se_u.pos.lnum
5151 || (int)(reginput - regline)
5152 != nfa_endp->se_u.pos.col)
5153 : reginput != nfa_endp->se_u.ptr))
5154 break;
5155
5156 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005157 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005158 {
5159 copy_sub(&m->norm, &t->subs.norm);
5160#ifdef FEAT_SYN_HL
5161 if (nfa_has_zsubexpr)
5162 copy_sub(&m->synt, &t->subs.synt);
5163#endif
5164 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005165#ifdef ENABLE_LOG
5166 fprintf(log_fd, "Match found:\n");
5167 log_subsexpr(m);
5168#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005169 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005170 break;
5171
5172 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005173 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005174 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005175 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005176 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005177 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005178 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005179 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005180 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005181#ifdef ENABLE_LOG
5182 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5183 failure_chance(t->state->out, 0),
5184 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005185#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005186 /* Do it directly if there already is a PIM or when
5187 * nfa_postprocess() detected it will work better. */
5188 if (t->pim.result != NFA_PIM_UNUSED
5189 || t->state->c == NFA_START_INVISIBLE_FIRST
5190 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5191 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5192 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005193 {
5194 /*
5195 * First try matching the invisible match, then what
5196 * follows.
5197 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005198 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005199 submatch, m, &listids);
5200
Bram Moolenaardecd9542013-06-07 16:31:50 +02005201 /* for \@! and \@<! it is a match when the result is
5202 * FALSE */
5203 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005204 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5205 || t->state->c
5206 == NFA_START_INVISIBLE_BEFORE_NEG
5207 || t->state->c
5208 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005209 {
5210 /* Copy submatch info from the recursive call */
5211 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005212#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005213 if (nfa_has_zsubexpr)
5214 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005215#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005216
Bram Moolenaara2d95102013-06-04 14:23:05 +02005217 /* t->state->out1 is the corresponding
5218 * END_INVISIBLE node; Add its out to the current
5219 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005220 add_here = TRUE;
5221 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005222 }
5223 }
5224 else
5225 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005226 nfa_pim_T pim;
5227
Bram Moolenaara2d95102013-06-04 14:23:05 +02005228 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005229 * First try matching what follows. Only if a match
5230 * is found verify the invisible match matches. Add a
5231 * nfa_pim_T to the following states, it contains info
5232 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005233 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005234 pim.state = t->state;
5235 pim.result = NFA_PIM_TODO;
5236 pim.subs.norm.in_use = 0;
5237#ifdef FEAT_SYN_HL
5238 pim.subs.synt.in_use = 0;
5239#endif
5240 if (REG_MULTI)
5241 {
5242 pim.end.pos.col = (int)(reginput - regline);
5243 pim.end.pos.lnum = reglnum;
5244 }
5245 else
5246 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005247
5248 /* t->state->out1 is the corresponding END_INVISIBLE
5249 * node; Add its out to the current list (zero-width
5250 * match). */
5251 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005252 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005253 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005254 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005255 break;
5256
Bram Moolenaar87953742013-06-05 18:52:40 +02005257 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005258 {
5259 nfa_state_T *skip = NULL;
5260#ifdef ENABLE_LOG
5261 int skip_lid = 0;
5262#endif
5263
5264 /* There is no point in trying to match the pattern if the
5265 * output state is not going to be added to the list. */
5266 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5267 {
5268 skip = t->state->out1->out;
5269#ifdef ENABLE_LOG
5270 skip_lid = nextlist->id;
5271#endif
5272 }
5273 else if (state_in_list(nextlist,
5274 t->state->out1->out->out, &t->subs))
5275 {
5276 skip = t->state->out1->out->out;
5277#ifdef ENABLE_LOG
5278 skip_lid = nextlist->id;
5279#endif
5280 }
5281 else if(state_in_list(thislist,
5282 t->state->out1->out->out, &t->subs))
5283 {
5284 skip = t->state->out1->out->out;
5285#ifdef ENABLE_LOG
5286 skip_lid = thislist->id;
5287#endif
5288 }
5289 if (skip != NULL)
5290 {
5291#ifdef ENABLE_LOG
5292 nfa_set_code(skip->c);
5293 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5294 abs(skip->id), skip_lid, skip->c, code);
5295#endif
5296 break;
5297 }
5298
Bram Moolenaar87953742013-06-05 18:52:40 +02005299 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005300 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005301 submatch, m, &listids);
5302 if (result)
5303 {
5304 int bytelen;
5305
5306#ifdef ENABLE_LOG
5307 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5308 log_subsexpr(m);
5309#endif
5310 /* Copy submatch info from the recursive call */
5311 copy_sub_off(&t->subs.norm, &m->norm);
5312#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005313 if (nfa_has_zsubexpr)
5314 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005315#endif
5316 /* Now we need to skip over the matched text and then
5317 * continue with what follows. */
5318 if (REG_MULTI)
5319 /* TODO: multi-line match */
5320 bytelen = m->norm.list.multi[0].end.col
5321 - (int)(reginput - regline);
5322 else
5323 bytelen = (int)(m->norm.list.line[0].end - reginput);
5324
5325#ifdef ENABLE_LOG
5326 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5327#endif
5328 if (bytelen == 0)
5329 {
5330 /* empty match, output of corresponding
5331 * NFA_END_PATTERN/NFA_SKIP to be used at current
5332 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005333 add_here = TRUE;
5334 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005335 }
5336 else if (bytelen <= clen)
5337 {
5338 /* match current character, output of corresponding
5339 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005340 add_state = t->state->out1->out->out;
5341 add_off = clen;
5342 }
5343 else
5344 {
5345 /* skip over the matched characters, set character
5346 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005347 add_state = t->state->out1->out;
5348 add_off = bytelen;
5349 add_count = bytelen - clen;
5350 }
5351 }
5352 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005353 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005354
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005355 case NFA_BOL:
5356 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005357 {
5358 add_here = TRUE;
5359 add_state = t->state->out;
5360 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005361 break;
5362
5363 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005364 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005365 {
5366 add_here = TRUE;
5367 add_state = t->state->out;
5368 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005369 break;
5370
5371 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005372 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005373
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005374 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005375 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005376#ifdef FEAT_MBYTE
5377 else if (has_mbyte)
5378 {
5379 int this_class;
5380
5381 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005382 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005383 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005384 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005385 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005386 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005387 }
5388#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005389 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005390 || (reginput > regline
5391 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005392 result = FALSE;
5393 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005394 {
5395 add_here = TRUE;
5396 add_state = t->state->out;
5397 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005398 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005399
5400 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005401 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005402 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005403 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005404#ifdef FEAT_MBYTE
5405 else if (has_mbyte)
5406 {
5407 int this_class, prev_class;
5408
5409 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005410 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005411 prev_class = reg_prev_class();
5412 if (this_class == prev_class
5413 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005414 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005415 }
5416#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005417 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005418 || (reginput[0] != NUL
5419 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005420 result = FALSE;
5421 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005422 {
5423 add_here = TRUE;
5424 add_state = t->state->out;
5425 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005426 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005427
Bram Moolenaar4b780632013-05-31 22:14:52 +02005428 case NFA_BOF:
5429 if (reglnum == 0 && reginput == regline
5430 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005431 {
5432 add_here = TRUE;
5433 add_state = t->state->out;
5434 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005435 break;
5436
5437 case NFA_EOF:
5438 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005439 {
5440 add_here = TRUE;
5441 add_state = t->state->out;
5442 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005443 break;
5444
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005445#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005446 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005447 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005448 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005449 int len = 0;
5450 nfa_state_T *end;
5451 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005452 int cchars[MAX_MCO];
5453 int ccount = 0;
5454 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005456 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005457 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005458 if (utf_iscomposing(sta->c))
5459 {
5460 /* Only match composing character(s), ignore base
5461 * character. Used for ".{composing}" and "{composing}"
5462 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005463 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005464 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005465 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005466 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005467 /* If \Z was present, then ignore composing characters.
5468 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005469 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005470 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005471 else
5472 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005473 while (sta->c != NFA_END_COMPOSING)
5474 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005475 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005476
5477 /* Check base character matches first, unless ignored. */
5478 else if (len > 0 || mc == sta->c)
5479 {
5480 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005481 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005482 len += mb_char2len(mc);
5483 sta = sta->out;
5484 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005485
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005486 /* We don't care about the order of composing characters.
5487 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005488 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005489 {
5490 mc = mb_ptr2char(reginput + len);
5491 cchars[ccount++] = mc;
5492 len += mb_char2len(mc);
5493 if (ccount == MAX_MCO)
5494 break;
5495 }
5496
5497 /* Check that each composing char in the pattern matches a
5498 * composing char in the text. We do not check if all
5499 * composing chars are matched. */
5500 result = OK;
5501 while (sta->c != NFA_END_COMPOSING)
5502 {
5503 for (j = 0; j < ccount; ++j)
5504 if (cchars[j] == sta->c)
5505 break;
5506 if (j == ccount)
5507 {
5508 result = FAIL;
5509 break;
5510 }
5511 sta = sta->out;
5512 }
5513 }
5514 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005515 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005516
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005517 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005518 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005519 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005520 }
5521#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005522
5523 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005524 if (curc == NUL && !reg_line_lbr && REG_MULTI
5525 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005527 go_to_nextline = TRUE;
5528 /* Pass -1 for the offset, which means taking the position
5529 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005530 add_state = t->state->out;
5531 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005532 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005533 else if (curc == '\n' && reg_line_lbr)
5534 {
5535 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005536 add_state = t->state->out;
5537 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005538 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005539 break;
5540
Bram Moolenaar417bad22013-06-07 14:08:30 +02005541 case NFA_START_COLL:
5542 case NFA_START_NEG_COLL:
5543 {
5544 /* What follows is a list of characters, until NFA_END_COLL.
5545 * One of them must match or none of them must match. */
5546 nfa_state_T *state;
5547 int result_if_matched;
5548 int c1, c2;
5549
5550 /* Never match EOL. If it's part of the collection it is added
5551 * as a separate state with an OR. */
5552 if (curc == NUL)
5553 break;
5554
5555 state = t->state->out;
5556 result_if_matched = (t->state->c == NFA_START_COLL);
5557 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005558 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005559 if (state->c == NFA_END_COLL)
5560 {
5561 result = !result_if_matched;
5562 break;
5563 }
5564 if (state->c == NFA_RANGE_MIN)
5565 {
5566 c1 = state->val;
5567 state = state->out; /* advance to NFA_RANGE_MAX */
5568 c2 = state->val;
5569#ifdef ENABLE_LOG
5570 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5571 curc, c1, c2);
5572#endif
5573 if (curc >= c1 && curc <= c2)
5574 {
5575 result = result_if_matched;
5576 break;
5577 }
5578 if (ireg_ic)
5579 {
5580 int curc_low = MB_TOLOWER(curc);
5581 int done = FALSE;
5582
5583 for ( ; c1 <= c2; ++c1)
5584 if (MB_TOLOWER(c1) == curc_low)
5585 {
5586 result = result_if_matched;
5587 done = TRUE;
5588 break;
5589 }
5590 if (done)
5591 break;
5592 }
5593 }
5594 else if (state->c < 0 ? check_char_class(state->c, curc)
5595 : (curc == state->c
5596 || (ireg_ic && MB_TOLOWER(curc)
5597 == MB_TOLOWER(state->c))))
5598 {
5599 result = result_if_matched;
5600 break;
5601 }
5602 state = state->out;
5603 }
5604 if (result)
5605 {
5606 /* next state is in out of the NFA_END_COLL, out1 of
5607 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005608 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005609 add_off = clen;
5610 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005611 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005612 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005613
5614 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005615 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005616 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005617 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005618 add_state = t->state->out;
5619 add_off = clen;
5620 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005621 break;
5622
5623 /*
5624 * Character classes like \a for alpha, \d for digit etc.
5625 */
5626 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005627 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005628 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005629 break;
5630
5631 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005632 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005633 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005634 break;
5635
5636 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005637 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005638 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005639 break;
5640
5641 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005642 result = !VIM_ISDIGIT(curc)
5643 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005644 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005645 break;
5646
5647 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005648 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005649 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005650 break;
5651
5652 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005653 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005654 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005655 break;
5656
5657 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005658 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005659 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005660 break;
5661
5662 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005663 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005664 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005665 break;
5666
5667 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005668 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005669 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005670 break;
5671
5672 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005673 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005674 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005675 break;
5676
5677 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005678 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005679 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005680 break;
5681
5682 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005683 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005684 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005685 break;
5686
5687 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005688 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005689 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005690 break;
5691
5692 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005693 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005694 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005695 break;
5696
5697 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005698 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005699 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005700 break;
5701
5702 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005703 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005704 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005705 break;
5706
5707 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005708 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005709 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005710 break;
5711
5712 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005713 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005714 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005715 break;
5716
5717 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005718 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005719 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005720 break;
5721
5722 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005723 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005724 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005725 break;
5726
5727 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005728 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005729 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005730 break;
5731
5732 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005733 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005734 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005735 break;
5736
5737 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005738 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005739 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005740 break;
5741
5742 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005743 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005744 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005745 break;
5746
5747 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005748 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005749 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005750 break;
5751
5752 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005753 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005754 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005755 break;
5756
Bram Moolenaar5714b802013-05-28 22:03:20 +02005757 case NFA_BACKREF1:
5758 case NFA_BACKREF2:
5759 case NFA_BACKREF3:
5760 case NFA_BACKREF4:
5761 case NFA_BACKREF5:
5762 case NFA_BACKREF6:
5763 case NFA_BACKREF7:
5764 case NFA_BACKREF8:
5765 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005766#ifdef FEAT_SYN_HL
5767 case NFA_ZREF1:
5768 case NFA_ZREF2:
5769 case NFA_ZREF3:
5770 case NFA_ZREF4:
5771 case NFA_ZREF5:
5772 case NFA_ZREF6:
5773 case NFA_ZREF7:
5774 case NFA_ZREF8:
5775 case NFA_ZREF9:
5776#endif
5777 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005778 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005779 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005780 int bytelen;
5781
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005782 if (t->state->c <= NFA_BACKREF9)
5783 {
5784 subidx = t->state->c - NFA_BACKREF1 + 1;
5785 result = match_backref(&t->subs.norm, subidx, &bytelen);
5786 }
5787#ifdef FEAT_SYN_HL
5788 else
5789 {
5790 subidx = t->state->c - NFA_ZREF1 + 1;
5791 result = match_zref(subidx, &bytelen);
5792 }
5793#endif
5794
Bram Moolenaar5714b802013-05-28 22:03:20 +02005795 if (result)
5796 {
5797 if (bytelen == 0)
5798 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005799 /* empty match always works, output of NFA_SKIP to be
5800 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005801 add_here = TRUE;
5802 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005803 }
5804 else if (bytelen <= clen)
5805 {
5806 /* match current character, jump ahead to out of
5807 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005808 add_state = t->state->out->out;
5809 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005810 }
5811 else
5812 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005813 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005814 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005815 add_state = t->state->out;
5816 add_off = bytelen;
5817 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005818 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005819 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005820 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005821 }
5822 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005823 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005824 if (t->count - clen <= 0)
5825 {
5826 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005827 add_state = t->state->out;
5828 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005829 }
5830 else
5831 {
5832 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005833 add_state = t->state;
5834 add_off = 0;
5835 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005836 }
5837 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005838
Bram Moolenaar423532e2013-05-29 21:14:42 +02005839 case NFA_LNUM:
5840 case NFA_LNUM_GT:
5841 case NFA_LNUM_LT:
5842 result = (REG_MULTI &&
5843 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5844 (long_u)(reglnum + reg_firstlnum)));
5845 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005846 {
5847 add_here = TRUE;
5848 add_state = t->state->out;
5849 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005850 break;
5851
5852 case NFA_COL:
5853 case NFA_COL_GT:
5854 case NFA_COL_LT:
5855 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5856 (long_u)(reginput - regline) + 1);
5857 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005858 {
5859 add_here = TRUE;
5860 add_state = t->state->out;
5861 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005862 break;
5863
5864 case NFA_VCOL:
5865 case NFA_VCOL_GT:
5866 case NFA_VCOL_LT:
5867 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5868 (long_u)win_linetabsize(
5869 reg_win == NULL ? curwin : reg_win,
5870 regline, (colnr_T)(reginput - regline)) + 1);
5871 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005872 {
5873 add_here = TRUE;
5874 add_state = t->state->out;
5875 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005876 break;
5877
Bram Moolenaar044aa292013-06-04 21:27:38 +02005878 case NFA_MARK:
5879 case NFA_MARK_GT:
5880 case NFA_MARK_LT:
5881 {
5882 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5883
5884 /* Compare the mark position to the match position. */
5885 result = (pos != NULL /* mark doesn't exist */
5886 && pos->lnum > 0 /* mark isn't set in reg_buf */
5887 && (pos->lnum == reglnum + reg_firstlnum
5888 ? (pos->col == (colnr_T)(reginput - regline)
5889 ? t->state->c == NFA_MARK
5890 : (pos->col < (colnr_T)(reginput - regline)
5891 ? t->state->c == NFA_MARK_GT
5892 : t->state->c == NFA_MARK_LT))
5893 : (pos->lnum < reglnum + reg_firstlnum
5894 ? t->state->c == NFA_MARK_GT
5895 : t->state->c == NFA_MARK_LT)));
5896 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005897 {
5898 add_here = TRUE;
5899 add_state = t->state->out;
5900 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005901 break;
5902 }
5903
Bram Moolenaar423532e2013-05-29 21:14:42 +02005904 case NFA_CURSOR:
5905 result = (reg_win != NULL
5906 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5907 && ((colnr_T)(reginput - regline)
5908 == reg_win->w_cursor.col));
5909 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005910 {
5911 add_here = TRUE;
5912 add_state = t->state->out;
5913 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005914 break;
5915
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005916 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005917#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005918 result = reg_match_visual();
5919 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005920 {
5921 add_here = TRUE;
5922 add_state = t->state->out;
5923 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005924#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005925 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005926
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005927 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005928 {
5929 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005930
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005931 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005932 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005933 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005934 result = (c == curc);
5935
5936 if (!result && ireg_ic)
5937 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005938#ifdef FEAT_MBYTE
5939 /* If there is a composing character which is not being
5940 * ignored there can be no match. Match with composing
5941 * character uses NFA_COMPOSING above. */
5942 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005943 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005944 result = FALSE;
5945#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005946 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005947 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005948 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005949
5950 } /* switch (t->state->c) */
5951
5952 if (add_state != NULL)
5953 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005954 nfa_pim_T *pim;
5955
5956 if (t->pim.result == NFA_PIM_UNUSED)
5957 pim = NULL;
5958 else
5959 pim = &t->pim;
5960
5961 /* Handle the postponed invisible match if the match might end
5962 * without advancing and before the end of the line. */
5963 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005964 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005965 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005966 {
5967#ifdef ENABLE_LOG
5968 fprintf(log_fd, "\n");
5969 fprintf(log_fd, "==================================\n");
5970 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5971 fprintf(log_fd, "\n");
5972#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005973 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005974 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005975 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005976 /* for \@! and \@<! it is a match when the result is
5977 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005978 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005979 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
5980 || pim->state->c
5981 == NFA_START_INVISIBLE_BEFORE_NEG
5982 || pim->state->c
5983 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005984 {
5985 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005986 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005987#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005988 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005989 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005990#endif
5991 }
5992 }
5993 else
5994 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005995 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005996#ifdef ENABLE_LOG
5997 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005998 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005999 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6000 fprintf(log_fd, "\n");
6001#endif
6002 }
6003
Bram Moolenaardecd9542013-06-07 16:31:50 +02006004 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006005 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006006 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6007 || pim->state->c
6008 == NFA_START_INVISIBLE_BEFORE_NEG
6009 || pim->state->c
6010 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006011 {
6012 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006013 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006014#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006015 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006016 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006017#endif
6018 }
6019 else
6020 /* look-behind match failed, don't add the state */
6021 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006022
6023 /* Postponed invisible match was handled, don't add it to
6024 * following states. */
6025 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006026 }
6027
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006028 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006029 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006030 else
6031 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006032 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006033 if (add_count > 0)
6034 nextlist->t[nextlist->n - 1].count = add_count;
6035 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006036 }
6037
6038 } /* for (thislist = thislist; thislist->state; thislist++) */
6039
Bram Moolenaare23febd2013-05-26 18:40:14 +02006040 /* Look for the start of a match in the current position by adding the
6041 * start state to the list of states.
6042 * The first found match is the leftmost one, thus the order of states
6043 * matters!
6044 * Do not add the start state in recursive calls of nfa_regmatch(),
6045 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006046 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006047 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006048 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006049 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006050 && reglnum == 0
6051 && clen != 0
6052 && (ireg_maxcol == 0
6053 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006054 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006055 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006056 ? (reglnum < nfa_endp->se_u.pos.lnum
6057 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006058 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006059 < nfa_endp->se_u.pos.col))
6060 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006061 {
6062#ifdef ENABLE_LOG
6063 fprintf(log_fd, "(---) STARTSTATE\n");
6064#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006065 /* Inline optimized code for addstate() if we know the state is
6066 * the first MOPEN. */
6067 if (toplevel)
6068 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006069 int add = TRUE;
6070 int c;
6071
6072 if (prog->regstart != NUL && clen != 0)
6073 {
6074 if (nextlist->n == 0)
6075 {
6076 colnr_T col = (colnr_T)(reginput - regline) + clen;
6077
6078 /* Nextlist is empty, we can skip ahead to the
6079 * character that must appear at the start. */
6080 if (skip_to_start(prog->regstart, &col) == FAIL)
6081 break;
6082#ifdef ENABLE_LOG
6083 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6084 col - ((colnr_T)(reginput - regline) + clen));
6085#endif
6086 reginput = regline + col - clen;
6087 }
6088 else
6089 {
6090 /* Checking if the required start character matches is
6091 * cheaper than adding a state that won't match. */
6092 c = PTR2CHAR(reginput + clen);
6093 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6094 != MB_TOLOWER(prog->regstart)))
6095 {
6096#ifdef ENABLE_LOG
6097 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6098#endif
6099 add = FALSE;
6100 }
6101 }
6102 }
6103
6104 if (add)
6105 {
6106 if (REG_MULTI)
6107 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006108 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006109 else
6110 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006111 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006112 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006113 }
6114 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006115 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006116 }
6117
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006118#ifdef ENABLE_LOG
6119 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006120 {
6121 int i;
6122
6123 for (i = 0; i < thislist->n; i++)
6124 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6125 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006126 fprintf(log_fd, "\n");
6127#endif
6128
6129nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006130 /* Advance to the next character, or advance to the next line, or
6131 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006132 if (clen != 0)
6133 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006134 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6135 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006136 reg_nextline();
6137 else
6138 break;
6139 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006140
6141#ifdef ENABLE_LOG
6142 if (log_fd != stderr)
6143 fclose(log_fd);
6144 log_fd = NULL;
6145#endif
6146
6147theend:
6148 /* Free memory */
6149 vim_free(list[0].t);
6150 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006151 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006152#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006153#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006154 fclose(debug);
6155#endif
6156
Bram Moolenaar963fee22013-05-26 21:47:28 +02006157 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006158}
6159
6160/*
6161 * Try match of "prog" with at regline["col"].
6162 * Returns 0 for failure, number of lines contained in the match otherwise.
6163 */
6164 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006165nfa_regtry(prog, col)
6166 nfa_regprog_T *prog;
6167 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006168{
6169 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006170 regsubs_T subs, m;
6171 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006172#ifdef ENABLE_LOG
6173 FILE *f;
6174#endif
6175
6176 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006177
6178#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006179 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006180 if (f != NULL)
6181 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006182 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006183#ifdef DEBUG
6184 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6185#endif
6186 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006187 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006188 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006189 fprintf(f, "\n\n");
6190 fclose(f);
6191 }
6192 else
6193 EMSG(_("Could not open temporary log file for writing "));
6194#endif
6195
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006196 clear_sub(&subs.norm);
6197 clear_sub(&m.norm);
6198#ifdef FEAT_SYN_HL
6199 clear_sub(&subs.synt);
6200 clear_sub(&m.synt);
6201#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006202
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006203 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006204 return 0;
6205
6206 cleanup_subexpr();
6207 if (REG_MULTI)
6208 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006209 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006210 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006211 reg_startpos[i] = subs.norm.list.multi[i].start;
6212 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006213 }
6214
6215 if (reg_startpos[0].lnum < 0)
6216 {
6217 reg_startpos[0].lnum = 0;
6218 reg_startpos[0].col = col;
6219 }
6220 if (reg_endpos[0].lnum < 0)
6221 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006222 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006223 reg_endpos[0].lnum = reglnum;
6224 reg_endpos[0].col = (int)(reginput - regline);
6225 }
6226 else
6227 /* Use line number of "\ze". */
6228 reglnum = reg_endpos[0].lnum;
6229 }
6230 else
6231 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006232 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006233 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006234 reg_startp[i] = subs.norm.list.line[i].start;
6235 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006236 }
6237
6238 if (reg_startp[0] == NULL)
6239 reg_startp[0] = regline + col;
6240 if (reg_endp[0] == NULL)
6241 reg_endp[0] = reginput;
6242 }
6243
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006244#ifdef FEAT_SYN_HL
6245 /* Package any found \z(...\) matches for export. Default is none. */
6246 unref_extmatch(re_extmatch_out);
6247 re_extmatch_out = NULL;
6248
6249 if (prog->reghasz == REX_SET)
6250 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006251 cleanup_zsubexpr();
6252 re_extmatch_out = make_extmatch();
6253 for (i = 0; i < subs.synt.in_use; i++)
6254 {
6255 if (REG_MULTI)
6256 {
6257 struct multipos *mpos = &subs.synt.list.multi[i];
6258
6259 /* Only accept single line matches. */
6260 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6261 re_extmatch_out->matches[i] =
6262 vim_strnsave(reg_getline(mpos->start.lnum)
6263 + mpos->start.col,
6264 mpos->end.col - mpos->start.col);
6265 }
6266 else
6267 {
6268 struct linepos *lpos = &subs.synt.list.line[i];
6269
6270 if (lpos->start != NULL && lpos->end != NULL)
6271 re_extmatch_out->matches[i] =
6272 vim_strnsave(lpos->start,
6273 (int)(lpos->end - lpos->start));
6274 }
6275 }
6276 }
6277#endif
6278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006279 return 1 + reglnum;
6280}
6281
6282/*
6283 * Match a regexp against a string ("line" points to the string) or multiple
6284 * lines ("line" is NULL, use reg_getline()).
6285 *
6286 * Returns 0 for failure, number of lines contained in the match otherwise.
6287 */
6288 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006289nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006290 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006291 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006292{
6293 nfa_regprog_T *prog;
6294 long retval = 0L;
6295 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006296 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006297
6298 if (REG_MULTI)
6299 {
6300 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6301 line = reg_getline((linenr_T)0); /* relative to the cursor */
6302 reg_startpos = reg_mmatch->startpos;
6303 reg_endpos = reg_mmatch->endpos;
6304 }
6305 else
6306 {
6307 prog = (nfa_regprog_T *)reg_match->regprog;
6308 reg_startp = reg_match->startp;
6309 reg_endp = reg_match->endp;
6310 }
6311
6312 /* Be paranoid... */
6313 if (prog == NULL || line == NULL)
6314 {
6315 EMSG(_(e_null));
6316 goto theend;
6317 }
6318
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006319 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6320 if (prog->regflags & RF_ICASE)
6321 ireg_ic = TRUE;
6322 else if (prog->regflags & RF_NOICASE)
6323 ireg_ic = FALSE;
6324
6325#ifdef FEAT_MBYTE
6326 /* If pattern contains "\Z" overrule value of ireg_icombine */
6327 if (prog->regflags & RF_ICOMBINE)
6328 ireg_icombine = TRUE;
6329#endif
6330
6331 regline = line;
6332 reglnum = 0; /* relative to line */
6333
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006334 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006335 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006336 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006337 nfa_listid = 1;
6338 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006339#ifdef DEBUG
6340 nfa_regengine.expr = prog->pattern;
6341#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006342
Bram Moolenaard89616e2013-06-06 18:46:06 +02006343 if (prog->reganch && col > 0)
6344 return 0L;
6345
Bram Moolenaar473de612013-06-08 18:19:48 +02006346 need_clear_subexpr = TRUE;
6347#ifdef FEAT_SYN_HL
6348 /* Clear the external match subpointers if necessary. */
6349 if (prog->reghasz == REX_SET)
6350 {
6351 nfa_has_zsubexpr = TRUE;
6352 need_clear_zsubexpr = TRUE;
6353 }
6354 else
6355 nfa_has_zsubexpr = FALSE;
6356#endif
6357
Bram Moolenaard89616e2013-06-06 18:46:06 +02006358 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006359 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006360 /* Skip ahead until a character we know the match must start with.
6361 * When there is none there is no match. */
6362 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006363 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006364
Bram Moolenaar473de612013-06-08 18:19:48 +02006365 /* If match_text is set it contains the full text that must match.
6366 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006367 if (prog->match_text != NULL
6368#ifdef FEAT_MBYTE
6369 && !ireg_icombine
6370#endif
6371 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006372 return find_match_text(col, prog->regstart, prog->match_text);
6373 }
6374
Bram Moolenaard89616e2013-06-06 18:46:06 +02006375 /* If the start column is past the maximum column: no need to try. */
6376 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6377 goto theend;
6378
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006379 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006380 for (i = 0; i < nstate; ++i)
6381 {
6382 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006383 prog->state[i].lastlist[0] = 0;
6384 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006385 }
6386
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006387 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006388
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006389#ifdef DEBUG
6390 nfa_regengine.expr = NULL;
6391#endif
6392
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006393theend:
6394 return retval;
6395}
6396
6397/*
6398 * Compile a regular expression into internal code for the NFA matcher.
6399 * Returns the program in allocated space. Returns NULL for an error.
6400 */
6401 static regprog_T *
6402nfa_regcomp(expr, re_flags)
6403 char_u *expr;
6404 int re_flags;
6405{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006406 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006407 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006408 int *postfix;
6409
6410 if (expr == NULL)
6411 return NULL;
6412
6413#ifdef DEBUG
6414 nfa_regengine.expr = expr;
6415#endif
6416
6417 init_class_tab();
6418
6419 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6420 return NULL;
6421
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006422 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006423 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006424 postfix = re2post();
6425 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006426 {
6427 /* TODO: only give this error for debugging? */
6428 if (post_ptr >= post_end)
6429 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006430 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006431 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006432
6433 /*
6434 * In order to build the NFA, we parse the input regexp twice:
6435 * 1. first pass to count size (so we can allocate space)
6436 * 2. second to emit code
6437 */
6438#ifdef ENABLE_LOG
6439 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006440 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006441
6442 if (f != NULL)
6443 {
6444 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6445 fclose(f);
6446 }
6447 }
6448#endif
6449
6450 /*
6451 * PASS 1
6452 * Count number of NFA states in "nstate". Do not build the NFA.
6453 */
6454 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006455
Bram Moolenaar16619a22013-06-11 18:42:36 +02006456 /* allocate the regprog with space for the compiled regexp */
6457 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006458 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6459 if (prog == NULL)
6460 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006461 state_ptr = prog->state;
6462
6463 /*
6464 * PASS 2
6465 * Build the NFA
6466 */
6467 prog->start = post2nfa(postfix, post_ptr, FALSE);
6468 if (prog->start == NULL)
6469 goto fail;
6470
6471 prog->regflags = regflags;
6472 prog->engine = &nfa_regengine;
6473 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006474 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006475 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006476 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006477
Bram Moolenaara2947e22013-06-11 22:44:09 +02006478 nfa_postprocess(prog);
6479
Bram Moolenaard89616e2013-06-06 18:46:06 +02006480 prog->reganch = nfa_get_reganch(prog->start, 0);
6481 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006482 prog->match_text = nfa_get_match_text(prog->start);
6483
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006484#ifdef ENABLE_LOG
6485 nfa_postfix_dump(expr, OK);
6486 nfa_dump(prog);
6487#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006488#ifdef FEAT_SYN_HL
6489 /* Remember whether this pattern has any \z specials in it. */
6490 prog->reghasz = re_has_z;
6491#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006492#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006493 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006494 nfa_regengine.expr = NULL;
6495#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006496
6497out:
6498 vim_free(post_start);
6499 post_start = post_ptr = post_end = NULL;
6500 state_ptr = NULL;
6501 return (regprog_T *)prog;
6502
6503fail:
6504 vim_free(prog);
6505 prog = NULL;
6506#ifdef ENABLE_LOG
6507 nfa_postfix_dump(expr, FAIL);
6508#endif
6509#ifdef DEBUG
6510 nfa_regengine.expr = NULL;
6511#endif
6512 goto out;
6513}
6514
Bram Moolenaar473de612013-06-08 18:19:48 +02006515/*
6516 * Free a compiled regexp program, returned by nfa_regcomp().
6517 */
6518 static void
6519nfa_regfree(prog)
6520 regprog_T *prog;
6521{
6522 if (prog != NULL)
6523 {
6524 vim_free(((nfa_regprog_T *)prog)->match_text);
6525#ifdef DEBUG
6526 vim_free(((nfa_regprog_T *)prog)->pattern);
6527#endif
6528 vim_free(prog);
6529 }
6530}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006531
6532/*
6533 * Match a regexp against a string.
6534 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6535 * Uses curbuf for line count and 'iskeyword'.
6536 *
6537 * Return TRUE if there is a match, FALSE if not.
6538 */
6539 static int
6540nfa_regexec(rmp, line, col)
6541 regmatch_T *rmp;
6542 char_u *line; /* string to match against */
6543 colnr_T col; /* column to start looking for match */
6544{
6545 reg_match = rmp;
6546 reg_mmatch = NULL;
6547 reg_maxline = 0;
6548 reg_line_lbr = FALSE;
6549 reg_buf = curbuf;
6550 reg_win = NULL;
6551 ireg_ic = rmp->rm_ic;
6552#ifdef FEAT_MBYTE
6553 ireg_icombine = FALSE;
6554#endif
6555 ireg_maxcol = 0;
6556 return (nfa_regexec_both(line, col) != 0);
6557}
6558
6559#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6560 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6561
6562static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6563
6564/*
6565 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6566 */
6567 static int
6568nfa_regexec_nl(rmp, line, col)
6569 regmatch_T *rmp;
6570 char_u *line; /* string to match against */
6571 colnr_T col; /* column to start looking for match */
6572{
6573 reg_match = rmp;
6574 reg_mmatch = NULL;
6575 reg_maxline = 0;
6576 reg_line_lbr = TRUE;
6577 reg_buf = curbuf;
6578 reg_win = NULL;
6579 ireg_ic = rmp->rm_ic;
6580#ifdef FEAT_MBYTE
6581 ireg_icombine = FALSE;
6582#endif
6583 ireg_maxcol = 0;
6584 return (nfa_regexec_both(line, col) != 0);
6585}
6586#endif
6587
6588
6589/*
6590 * Match a regexp against multiple lines.
6591 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6592 * Uses curbuf for line count and 'iskeyword'.
6593 *
6594 * Return zero if there is no match. Return number of lines contained in the
6595 * match otherwise.
6596 *
6597 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6598 *
6599 * ! Also NOTE : match may actually be in another line. e.g.:
6600 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6601 *
6602 * +-------------------------+
6603 * |a |
6604 * |b |
6605 * |c |
6606 * | |
6607 * +-------------------------+
6608 *
6609 * then nfa_regexec_multi() returns 3. while the original
6610 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6611 *
6612 * FIXME if this behavior is not compatible.
6613 */
6614 static long
6615nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6616 regmmatch_T *rmp;
6617 win_T *win; /* window in which to search or NULL */
6618 buf_T *buf; /* buffer in which to search */
6619 linenr_T lnum; /* nr of line to start looking for match */
6620 colnr_T col; /* column to start looking for match */
6621 proftime_T *tm UNUSED; /* timeout limit or NULL */
6622{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006623 reg_match = NULL;
6624 reg_mmatch = rmp;
6625 reg_buf = buf;
6626 reg_win = win;
6627 reg_firstlnum = lnum;
6628 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6629 reg_line_lbr = FALSE;
6630 ireg_ic = rmp->rmm_ic;
6631#ifdef FEAT_MBYTE
6632 ireg_icombine = FALSE;
6633#endif
6634 ireg_maxcol = rmp->rmm_maxcol;
6635
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006636 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006637}
6638
6639#ifdef DEBUG
6640# undef ENABLE_LOG
6641#endif