blob: d8df13cb079a16c8da122542dc08eacc3eefca62 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020032enum
33{
34 NFA_SPLIT = -1024,
35 NFA_MATCH,
36 NFA_SKIP_CHAR, /* matches a 0-length char */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020037
Bram Moolenaar417bad22013-06-07 14:08:30 +020038 NFA_START_COLL, /* [abc] start */
39 NFA_END_COLL, /* [abc] end */
40 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020041 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
42 NFA_RANGE, /* range of the two previous items
43 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020044 NFA_RANGE_MIN, /* low end of a range */
45 NFA_RANGE_MAX, /* high end of a range */
46
Bram Moolenaare7766ee2013-06-08 22:30:03 +020047 NFA_CONCAT, /* concatenate two previous items (postfix
48 * only) */
49 NFA_OR, /* \| (postfix only) */
50 NFA_STAR, /* greedy * (posfix only) */
51 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
52 NFA_QUEST, /* greedy \? (postfix only) */
53 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020054
55 NFA_BOL, /* ^ Begin line */
56 NFA_EOL, /* $ End line */
57 NFA_BOW, /* \< Begin word */
58 NFA_EOW, /* \> End word */
59 NFA_BOF, /* \%^ Begin file */
60 NFA_EOF, /* \%$ End file */
61 NFA_NEWL,
62 NFA_ZSTART, /* Used for \zs */
63 NFA_ZEND, /* Used for \ze */
64 NFA_NOPEN, /* Start of subexpression marked with \%( */
65 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
66 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020067 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020068 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020069 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020070 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020071 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020072 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020073 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020074 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020075 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020076 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_COMPOSING, /* Next nodes in NFA are part of the
79 composing multibyte char */
80 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020081 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020082
83 /* The following are used only in the postfix form, not in the NFA */
84 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
85 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
86 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
87 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
88 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
89
Bram Moolenaar5714b802013-05-28 22:03:20 +020090 NFA_BACKREF1, /* \1 */
91 NFA_BACKREF2, /* \2 */
92 NFA_BACKREF3, /* \3 */
93 NFA_BACKREF4, /* \4 */
94 NFA_BACKREF5, /* \5 */
95 NFA_BACKREF6, /* \6 */
96 NFA_BACKREF7, /* \7 */
97 NFA_BACKREF8, /* \8 */
98 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020099#ifdef FEAT_SYN_HL
100 NFA_ZREF1, /* \z1 */
101 NFA_ZREF2, /* \z2 */
102 NFA_ZREF3, /* \z3 */
103 NFA_ZREF4, /* \z4 */
104 NFA_ZREF5, /* \z5 */
105 NFA_ZREF6, /* \z6 */
106 NFA_ZREF7, /* \z7 */
107 NFA_ZREF8, /* \z8 */
108 NFA_ZREF9, /* \z9 */
109#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200110 NFA_SKIP, /* Skip characters */
111
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200112 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200113 NFA_MOPEN1,
114 NFA_MOPEN2,
115 NFA_MOPEN3,
116 NFA_MOPEN4,
117 NFA_MOPEN5,
118 NFA_MOPEN6,
119 NFA_MOPEN7,
120 NFA_MOPEN8,
121 NFA_MOPEN9,
122
123 NFA_MCLOSE,
124 NFA_MCLOSE1,
125 NFA_MCLOSE2,
126 NFA_MCLOSE3,
127 NFA_MCLOSE4,
128 NFA_MCLOSE5,
129 NFA_MCLOSE6,
130 NFA_MCLOSE7,
131 NFA_MCLOSE8,
132 NFA_MCLOSE9,
133
134#ifdef FEAT_SYN_HL
135 NFA_ZOPEN,
136 NFA_ZOPEN1,
137 NFA_ZOPEN2,
138 NFA_ZOPEN3,
139 NFA_ZOPEN4,
140 NFA_ZOPEN5,
141 NFA_ZOPEN6,
142 NFA_ZOPEN7,
143 NFA_ZOPEN8,
144 NFA_ZOPEN9,
145
146 NFA_ZCLOSE,
147 NFA_ZCLOSE1,
148 NFA_ZCLOSE2,
149 NFA_ZCLOSE3,
150 NFA_ZCLOSE4,
151 NFA_ZCLOSE5,
152 NFA_ZCLOSE6,
153 NFA_ZCLOSE7,
154 NFA_ZCLOSE8,
155 NFA_ZCLOSE9,
156#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200157
158 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200159 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200160 NFA_IDENT, /* Match identifier char */
161 NFA_SIDENT, /* Match identifier char but no digit */
162 NFA_KWORD, /* Match keyword char */
163 NFA_SKWORD, /* Match word char but no digit */
164 NFA_FNAME, /* Match file name char */
165 NFA_SFNAME, /* Match file name char but no digit */
166 NFA_PRINT, /* Match printable char */
167 NFA_SPRINT, /* Match printable char but no digit */
168 NFA_WHITE, /* Match whitespace char */
169 NFA_NWHITE, /* Match non-whitespace char */
170 NFA_DIGIT, /* Match digit char */
171 NFA_NDIGIT, /* Match non-digit char */
172 NFA_HEX, /* Match hex char */
173 NFA_NHEX, /* Match non-hex char */
174 NFA_OCTAL, /* Match octal char */
175 NFA_NOCTAL, /* Match non-octal char */
176 NFA_WORD, /* Match word char */
177 NFA_NWORD, /* Match non-word char */
178 NFA_HEAD, /* Match head char */
179 NFA_NHEAD, /* Match non-head char */
180 NFA_ALPHA, /* Match alpha char */
181 NFA_NALPHA, /* Match non-alpha char */
182 NFA_LOWER, /* Match lowercase char */
183 NFA_NLOWER, /* Match non-lowercase char */
184 NFA_UPPER, /* Match uppercase char */
185 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200186
187 NFA_CURSOR, /* Match cursor pos */
188 NFA_LNUM, /* Match line number */
189 NFA_LNUM_GT, /* Match > line number */
190 NFA_LNUM_LT, /* Match < line number */
191 NFA_COL, /* Match cursor column */
192 NFA_COL_GT, /* Match > cursor column */
193 NFA_COL_LT, /* Match < cursor column */
194 NFA_VCOL, /* Match cursor virtual column */
195 NFA_VCOL_GT, /* Match > cursor virtual column */
196 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200197 NFA_MARK, /* Match mark */
198 NFA_MARK_GT, /* Match > mark */
199 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200200 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200201
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200202 NFA_FIRST_NL = NFA_ANY + ADD_NL,
203 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
204
205 /* Character classes [:alnum:] etc */
206 NFA_CLASS_ALNUM,
207 NFA_CLASS_ALPHA,
208 NFA_CLASS_BLANK,
209 NFA_CLASS_CNTRL,
210 NFA_CLASS_DIGIT,
211 NFA_CLASS_GRAPH,
212 NFA_CLASS_LOWER,
213 NFA_CLASS_PRINT,
214 NFA_CLASS_PUNCT,
215 NFA_CLASS_SPACE,
216 NFA_CLASS_UPPER,
217 NFA_CLASS_XDIGIT,
218 NFA_CLASS_TAB,
219 NFA_CLASS_RETURN,
220 NFA_CLASS_BACKSPACE,
221 NFA_CLASS_ESCAPE
222};
223
224/* Keep in sync with classchars. */
225static int nfa_classcodes[] = {
226 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
227 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
228 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
229 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
230 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
231 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
232 NFA_UPPER, NFA_NUPPER
233};
234
235static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
236
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200238static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200239
Bram Moolenaar428e9872013-05-30 17:05:39 +0200240/* NFA regexp \1 .. \9 encountered. */
241static int nfa_has_backref;
242
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200243#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200244/* NFA regexp has \z( ), set zsubexpr. */
245static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200246#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200247
Bram Moolenaar963fee22013-05-26 21:47:28 +0200248/* Number of sub expressions actually being used during execution. 1 if only
249 * the whole match (subexpr 0) is used. */
250static int nfa_nsubexpr;
251
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252static int *post_start; /* holds the postfix form of r.e. */
253static int *post_end;
254static int *post_ptr;
255
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200256static int nstate; /* Number of states in the NFA. Also used when
257 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200258static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259
Bram Moolenaar307aa162013-06-02 16:34:21 +0200260/* If not NULL match must end at this position */
261static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200263/* listid is global, so that it increases on recursive calls to
264 * nfa_regmatch(), which means we don't have to clear the lastlist field of
265 * all the states. */
266static int nfa_listid;
267static int nfa_alt_listid;
268
269/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
270static int nfa_ll_index = 0;
271
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200272static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200273static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
274static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200275static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200276static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200277static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200278static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279static int nfa_regatom __ARGS((void));
280static int nfa_regpiece __ARGS((void));
281static int nfa_regconcat __ARGS((void));
282static int nfa_regbranch __ARGS((void));
283static int nfa_reg __ARGS((int paren));
284#ifdef DEBUG
285static void nfa_set_code __ARGS((int c));
286static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200287static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
288static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289static void nfa_dump __ARGS((nfa_regprog_T *prog));
290#endif
291static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200292static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +0200293static void st_error __ARGS((int *postfix, int *end, int *p));
294static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200296static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200298static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
299static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200300static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200301static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
303static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200304static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
306static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200307static int match_follows __ARGS((nfa_state_T *startstate, int depth));
308static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200309
310/* helper functions used when doing re2post() ... regatom() parsing */
311#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200312 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200313 return FAIL; \
314 *post_ptr++ = c; \
315 } while (0)
316
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200317/*
318 * Initialize internal variables before NFA compilation.
319 * Return OK on success, FAIL otherwise.
320 */
321 static int
322nfa_regcomp_start(expr, re_flags)
323 char_u *expr;
324 int re_flags; /* see vim_regcomp() */
325{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200326 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200327 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328
329 nstate = 0;
330 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200331 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200332 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200333
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200334 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200335 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200336 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200337
338 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200339 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341 post_start = (int *)lalloc(postfix_size, TRUE);
342 if (post_start == NULL)
343 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200345 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200347 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200348
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200349 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200350 regcomp_start(expr, re_flags);
351
352 return OK;
353}
354
355/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200356 * Figure out if the NFA state list starts with an anchor, must match at start
357 * of the line.
358 */
359 static int
360nfa_get_reganch(start, depth)
361 nfa_state_T *start;
362 int depth;
363{
364 nfa_state_T *p = start;
365
366 if (depth > 4)
367 return 0;
368
369 while (p != NULL)
370 {
371 switch (p->c)
372 {
373 case NFA_BOL:
374 case NFA_BOF:
375 return 1; /* yes! */
376
377 case NFA_ZSTART:
378 case NFA_ZEND:
379 case NFA_CURSOR:
380 case NFA_VISUAL:
381
382 case NFA_MOPEN:
383 case NFA_MOPEN1:
384 case NFA_MOPEN2:
385 case NFA_MOPEN3:
386 case NFA_MOPEN4:
387 case NFA_MOPEN5:
388 case NFA_MOPEN6:
389 case NFA_MOPEN7:
390 case NFA_MOPEN8:
391 case NFA_MOPEN9:
392 case NFA_NOPEN:
393#ifdef FEAT_SYN_HL
394 case NFA_ZOPEN:
395 case NFA_ZOPEN1:
396 case NFA_ZOPEN2:
397 case NFA_ZOPEN3:
398 case NFA_ZOPEN4:
399 case NFA_ZOPEN5:
400 case NFA_ZOPEN6:
401 case NFA_ZOPEN7:
402 case NFA_ZOPEN8:
403 case NFA_ZOPEN9:
404#endif
405 p = p->out;
406 break;
407
408 case NFA_SPLIT:
409 return nfa_get_reganch(p->out, depth + 1)
410 && nfa_get_reganch(p->out1, depth + 1);
411
412 default:
413 return 0; /* noooo */
414 }
415 }
416 return 0;
417}
418
419/*
420 * Figure out if the NFA state list starts with a character which must match
421 * at start of the match.
422 */
423 static int
424nfa_get_regstart(start, depth)
425 nfa_state_T *start;
426 int depth;
427{
428 nfa_state_T *p = start;
429
430 if (depth > 4)
431 return 0;
432
433 while (p != NULL)
434 {
435 switch (p->c)
436 {
437 /* all kinds of zero-width matches */
438 case NFA_BOL:
439 case NFA_BOF:
440 case NFA_BOW:
441 case NFA_EOW:
442 case NFA_ZSTART:
443 case NFA_ZEND:
444 case NFA_CURSOR:
445 case NFA_VISUAL:
446 case NFA_LNUM:
447 case NFA_LNUM_GT:
448 case NFA_LNUM_LT:
449 case NFA_COL:
450 case NFA_COL_GT:
451 case NFA_COL_LT:
452 case NFA_VCOL:
453 case NFA_VCOL_GT:
454 case NFA_VCOL_LT:
455 case NFA_MARK:
456 case NFA_MARK_GT:
457 case NFA_MARK_LT:
458
459 case NFA_MOPEN:
460 case NFA_MOPEN1:
461 case NFA_MOPEN2:
462 case NFA_MOPEN3:
463 case NFA_MOPEN4:
464 case NFA_MOPEN5:
465 case NFA_MOPEN6:
466 case NFA_MOPEN7:
467 case NFA_MOPEN8:
468 case NFA_MOPEN9:
469 case NFA_NOPEN:
470#ifdef FEAT_SYN_HL
471 case NFA_ZOPEN:
472 case NFA_ZOPEN1:
473 case NFA_ZOPEN2:
474 case NFA_ZOPEN3:
475 case NFA_ZOPEN4:
476 case NFA_ZOPEN5:
477 case NFA_ZOPEN6:
478 case NFA_ZOPEN7:
479 case NFA_ZOPEN8:
480 case NFA_ZOPEN9:
481#endif
482 p = p->out;
483 break;
484
485 case NFA_SPLIT:
486 {
487 int c1 = nfa_get_regstart(p->out, depth + 1);
488 int c2 = nfa_get_regstart(p->out1, depth + 1);
489
490 if (c1 == c2)
491 return c1; /* yes! */
492 return 0;
493 }
494
495 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200496 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200497 return p->c; /* yes! */
498 return 0;
499 }
500 }
501 return 0;
502}
503
504/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200505 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200506 * else. If so return a string in allocated memory with what must match after
507 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200508 */
509 static char_u *
510nfa_get_match_text(start)
511 nfa_state_T *start;
512{
513 nfa_state_T *p = start;
514 int len = 0;
515 char_u *ret;
516 char_u *s;
517
518 if (p->c != NFA_MOPEN)
519 return NULL; /* just in case */
520 p = p->out;
521 while (p->c > 0)
522 {
523 len += MB_CHAR2LEN(p->c);
524 p = p->out;
525 }
526 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
527 return NULL;
528
529 ret = alloc(len);
530 if (ret != NULL)
531 {
532 len = 0;
533 p = start->out->out; /* skip first char, it goes into regstart */
534 s = ret;
535 while (p->c > 0)
536 {
537#ifdef FEAT_MBYTE
538 if (has_mbyte)
539 s += (*mb_char2bytes)(p->c, s);
540 else
541#endif
542 *s++ = p->c;
543 p = p->out;
544 }
545 *s = NUL;
546 }
547 return ret;
548}
549
550/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200551 * Allocate more space for post_start. Called when
552 * running above the estimated number of states.
553 */
554 static int
555realloc_post_list()
556{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200557 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 int new_max = nstate_max + 1000;
559 int *new_start;
560 int *old_start;
561
562 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
563 if (new_start == NULL)
564 return FAIL;
565 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200566 old_start = post_start;
567 post_start = new_start;
568 post_ptr = new_start + (post_ptr - old_start);
569 post_end = post_start + new_max;
570 vim_free(old_start);
571 return OK;
572}
573
574/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200575 * Search between "start" and "end" and try to recognize a
576 * character class in expanded form. For example [0-9].
577 * On success, return the id the character class to be emitted.
578 * On failure, return 0 (=FAIL)
579 * Start points to the first char of the range, while end should point
580 * to the closing brace.
581 */
582 static int
583nfa_recognize_char_class(start, end, extra_newl)
584 char_u *start;
585 char_u *end;
586 int extra_newl;
587{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200588# define CLASS_not 0x80
589# define CLASS_af 0x40
590# define CLASS_AF 0x20
591# define CLASS_az 0x10
592# define CLASS_AZ 0x08
593# define CLASS_o7 0x04
594# define CLASS_o9 0x02
595# define CLASS_underscore 0x01
596
597 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200599 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200600
601 if (extra_newl == TRUE)
602 newl = TRUE;
603
604 if (*end != ']')
605 return FAIL;
606 p = start;
607 if (*p == '^')
608 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200609 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200610 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611 }
612
613 while (p < end)
614 {
615 if (p + 2 < end && *(p + 1) == '-')
616 {
617 switch (*p)
618 {
619 case '0':
620 if (*(p + 2) == '9')
621 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200622 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200623 break;
624 }
625 else
626 if (*(p + 2) == '7')
627 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200628 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 break;
630 }
631 case 'a':
632 if (*(p + 2) == 'z')
633 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200634 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 break;
636 }
637 else
638 if (*(p + 2) == 'f')
639 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200640 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 break;
642 }
643 case 'A':
644 if (*(p + 2) == 'Z')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
649 else
650 if (*(p + 2) == 'F')
651 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200652 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200653 break;
654 }
655 /* FALLTHROUGH */
656 default:
657 return FAIL;
658 }
659 p += 3;
660 }
661 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
662 {
663 newl = TRUE;
664 p += 2;
665 }
666 else if (*p == '_')
667 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200668 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200669 p ++;
670 }
671 else if (*p == '\n')
672 {
673 newl = TRUE;
674 p ++;
675 }
676 else
677 return FAIL;
678 } /* while (p < end) */
679
680 if (p != end)
681 return FAIL;
682
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200683 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200684 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200685
686 switch (config)
687 {
688 case CLASS_o9:
689 return extra_newl + NFA_DIGIT;
690 case CLASS_not | CLASS_o9:
691 return extra_newl + NFA_NDIGIT;
692 case CLASS_af | CLASS_AF | CLASS_o9:
693 return extra_newl + NFA_HEX;
694 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
695 return extra_newl + NFA_NHEX;
696 case CLASS_o7:
697 return extra_newl + NFA_OCTAL;
698 case CLASS_not | CLASS_o7:
699 return extra_newl + NFA_NOCTAL;
700 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
701 return extra_newl + NFA_WORD;
702 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
703 return extra_newl + NFA_NWORD;
704 case CLASS_az | CLASS_AZ | CLASS_underscore:
705 return extra_newl + NFA_HEAD;
706 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
707 return extra_newl + NFA_NHEAD;
708 case CLASS_az | CLASS_AZ:
709 return extra_newl + NFA_ALPHA;
710 case CLASS_not | CLASS_az | CLASS_AZ:
711 return extra_newl + NFA_NALPHA;
712 case CLASS_az:
713 return extra_newl + NFA_LOWER;
714 case CLASS_not | CLASS_az:
715 return extra_newl + NFA_NLOWER;
716 case CLASS_AZ:
717 return extra_newl + NFA_UPPER;
718 case CLASS_not | CLASS_AZ:
719 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200721 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200722}
723
724/*
725 * Produce the bytes for equivalence class "c".
726 * Currently only handles latin1, latin9 and utf-8.
727 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
728 * equivalent to 'a OR b OR c'
729 *
730 * NOTE! When changing this function, also update reg_equi_class()
731 */
732 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200733nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200734 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200736#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200737
738#ifdef FEAT_MBYTE
739 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
740 || STRCMP(p_enc, "iso-8859-15") == 0)
741#endif
742 {
743 switch (c)
744 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200745 case 'A': case 0300: case 0301: case 0302:
746 case 0303: case 0304: case 0305:
747 EMIT2('A'); EMIT2(0300); EMIT2(0301);
748 EMIT2(0302); EMIT2(0303); EMIT2(0304);
749 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750 return OK;
751
Bram Moolenaar417bad22013-06-07 14:08:30 +0200752 case 'C': case 0307:
753 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200754 return OK;
755
Bram Moolenaar417bad22013-06-07 14:08:30 +0200756 case 'E': case 0310: case 0311: case 0312: case 0313:
757 EMIT2('E'); EMIT2(0310); EMIT2(0311);
758 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200759 return OK;
760
Bram Moolenaar417bad22013-06-07 14:08:30 +0200761 case 'I': case 0314: case 0315: case 0316: case 0317:
762 EMIT2('I'); EMIT2(0314); EMIT2(0315);
763 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200764 return OK;
765
Bram Moolenaar417bad22013-06-07 14:08:30 +0200766 case 'N': case 0321:
767 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200768 return OK;
769
Bram Moolenaar417bad22013-06-07 14:08:30 +0200770 case 'O': case 0322: case 0323: case 0324: case 0325:
771 case 0326:
772 EMIT2('O'); EMIT2(0322); EMIT2(0323);
773 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200774 return OK;
775
Bram Moolenaar417bad22013-06-07 14:08:30 +0200776 case 'U': case 0331: case 0332: case 0333: case 0334:
777 EMIT2('U'); EMIT2(0331); EMIT2(0332);
778 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200779 return OK;
780
Bram Moolenaar417bad22013-06-07 14:08:30 +0200781 case 'Y': case 0335:
782 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200783 return OK;
784
Bram Moolenaar417bad22013-06-07 14:08:30 +0200785 case 'a': case 0340: case 0341: case 0342:
786 case 0343: case 0344: case 0345:
787 EMIT2('a'); EMIT2(0340); EMIT2(0341);
788 EMIT2(0342); EMIT2(0343); EMIT2(0344);
789 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200790 return OK;
791
Bram Moolenaar417bad22013-06-07 14:08:30 +0200792 case 'c': case 0347:
793 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200794 return OK;
795
Bram Moolenaar417bad22013-06-07 14:08:30 +0200796 case 'e': case 0350: case 0351: case 0352: case 0353:
797 EMIT2('e'); EMIT2(0350); EMIT2(0351);
798 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200799 return OK;
800
Bram Moolenaar417bad22013-06-07 14:08:30 +0200801 case 'i': case 0354: case 0355: case 0356: case 0357:
802 EMIT2('i'); EMIT2(0354); EMIT2(0355);
803 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200804 return OK;
805
Bram Moolenaar417bad22013-06-07 14:08:30 +0200806 case 'n': case 0361:
807 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 return OK;
809
Bram Moolenaar417bad22013-06-07 14:08:30 +0200810 case 'o': case 0362: case 0363: case 0364: case 0365:
811 case 0366:
812 EMIT2('o'); EMIT2(0362); EMIT2(0363);
813 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 return OK;
815
Bram Moolenaar417bad22013-06-07 14:08:30 +0200816 case 'u': case 0371: case 0372: case 0373: case 0374:
817 EMIT2('u'); EMIT2(0371); EMIT2(0372);
818 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200819 return OK;
820
Bram Moolenaar417bad22013-06-07 14:08:30 +0200821 case 'y': case 0375: case 0377:
822 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200823 return OK;
824
825 default:
826 return FAIL;
827 }
828 }
829
830 EMIT(c);
831 return OK;
832#undef EMIT2
833}
834
835/*
836 * Code to parse regular expression.
837 *
838 * We try to reuse parsing functions in regexp.c to
839 * minimize surprise and keep the syntax consistent.
840 */
841
842/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200843 * Parse the lowest level.
844 *
845 * An atom can be one of a long list of items. Many atoms match one character
846 * in the text. It is often an ordinary character or a character class.
847 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
848 * is only for syntax highlighting.
849 *
850 * atom ::= ordinary-atom
851 * or \( pattern \)
852 * or \%( pattern \)
853 * or \z( pattern \)
854 */
855 static int
856nfa_regatom()
857{
858 int c;
859 int charclass;
860 int equiclass;
861 int collclass;
862 int got_coll_char;
863 char_u *p;
864 char_u *endp;
865#ifdef FEAT_MBYTE
866 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867#endif
868 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200869 int emit_range;
870 int negated;
871 int result;
872 int startc = -1;
873 int endc = -1;
874 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200876 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200877 switch (c)
878 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200879 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200880 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
881
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200882 case Magic('^'):
883 EMIT(NFA_BOL);
884 break;
885
886 case Magic('$'):
887 EMIT(NFA_EOL);
888#if defined(FEAT_SYN_HL) || defined(PROTO)
889 had_eol = TRUE;
890#endif
891 break;
892
893 case Magic('<'):
894 EMIT(NFA_BOW);
895 break;
896
897 case Magic('>'):
898 EMIT(NFA_EOW);
899 break;
900
901 case Magic('_'):
902 c = no_Magic(getchr());
903 if (c == '^') /* "\_^" is start-of-line */
904 {
905 EMIT(NFA_BOL);
906 break;
907 }
908 if (c == '$') /* "\_$" is end-of-line */
909 {
910 EMIT(NFA_EOL);
911#if defined(FEAT_SYN_HL) || defined(PROTO)
912 had_eol = TRUE;
913#endif
914 break;
915 }
916
917 extra = ADD_NL;
918
919 /* "\_[" is collection plus newline */
920 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200921 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200922
923 /* "\_x" is character class plus newline */
924 /*FALLTHROUGH*/
925
926 /*
927 * Character classes.
928 */
929 case Magic('.'):
930 case Magic('i'):
931 case Magic('I'):
932 case Magic('k'):
933 case Magic('K'):
934 case Magic('f'):
935 case Magic('F'):
936 case Magic('p'):
937 case Magic('P'):
938 case Magic('s'):
939 case Magic('S'):
940 case Magic('d'):
941 case Magic('D'):
942 case Magic('x'):
943 case Magic('X'):
944 case Magic('o'):
945 case Magic('O'):
946 case Magic('w'):
947 case Magic('W'):
948 case Magic('h'):
949 case Magic('H'):
950 case Magic('a'):
951 case Magic('A'):
952 case Magic('l'):
953 case Magic('L'):
954 case Magic('u'):
955 case Magic('U'):
956 p = vim_strchr(classchars, no_Magic(c));
957 if (p == NULL)
958 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200959 EMSGN("INTERNAL: Unknown character class char: %ld", c);
960 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200961 }
962#ifdef FEAT_MBYTE
963 /* When '.' is followed by a composing char ignore the dot, so that
964 * the composing char is matched here. */
965 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
966 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200967 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200968 c = getchr();
969 goto nfa_do_multibyte;
970 }
971#endif
972 EMIT(nfa_classcodes[p - classchars]);
973 if (extra == ADD_NL)
974 {
975 EMIT(NFA_NEWL);
976 EMIT(NFA_OR);
977 regflags |= RF_HASNL;
978 }
979 break;
980
981 case Magic('n'):
982 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200983 /* In a string "\n" matches a newline character. */
984 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200985 else
986 {
987 /* In buffer text "\n" matches the end of a line. */
988 EMIT(NFA_NEWL);
989 regflags |= RF_HASNL;
990 }
991 break;
992
993 case Magic('('):
994 if (nfa_reg(REG_PAREN) == FAIL)
995 return FAIL; /* cascaded error */
996 break;
997
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200998 case Magic('|'):
999 case Magic('&'):
1000 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001001 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001002 return FAIL;
1003
1004 case Magic('='):
1005 case Magic('?'):
1006 case Magic('+'):
1007 case Magic('@'):
1008 case Magic('*'):
1009 case Magic('{'):
1010 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001011 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001012 return FAIL;
1013
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001014 case Magic('~'):
1015 {
1016 char_u *lp;
1017
1018 /* Previous substitute pattern.
1019 * Generated as "\%(pattern\)". */
1020 if (reg_prev_sub == NULL)
1021 {
1022 EMSG(_(e_nopresub));
1023 return FAIL;
1024 }
1025 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1026 {
1027 EMIT(PTR2CHAR(lp));
1028 if (lp != reg_prev_sub)
1029 EMIT(NFA_CONCAT);
1030 }
1031 EMIT(NFA_NOPEN);
1032 break;
1033 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001034
Bram Moolenaar428e9872013-05-30 17:05:39 +02001035 case Magic('1'):
1036 case Magic('2'):
1037 case Magic('3'):
1038 case Magic('4'):
1039 case Magic('5'):
1040 case Magic('6'):
1041 case Magic('7'):
1042 case Magic('8'):
1043 case Magic('9'):
1044 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1045 nfa_has_backref = TRUE;
1046 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001047
1048 case Magic('z'):
1049 c = no_Magic(getchr());
1050 switch (c)
1051 {
1052 case 's':
1053 EMIT(NFA_ZSTART);
1054 break;
1055 case 'e':
1056 EMIT(NFA_ZEND);
1057 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001058 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001059#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001060 case '1':
1061 case '2':
1062 case '3':
1063 case '4':
1064 case '5':
1065 case '6':
1066 case '7':
1067 case '8':
1068 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001069 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001070 if (reg_do_extmatch != REX_USE)
1071 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001072 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1073 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001074 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001075 re_has_z = REX_USE;
1076 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001077 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001078 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001079 if (reg_do_extmatch != REX_SET)
1080 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001081 if (nfa_reg(REG_ZPAREN) == FAIL)
1082 return FAIL; /* cascaded error */
1083 re_has_z = REX_SET;
1084 break;
1085#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001087 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001088 no_Magic(c));
1089 return FAIL;
1090 }
1091 break;
1092
1093 case Magic('%'):
1094 c = no_Magic(getchr());
1095 switch (c)
1096 {
1097 /* () without a back reference */
1098 case '(':
1099 if (nfa_reg(REG_NPAREN) == FAIL)
1100 return FAIL;
1101 EMIT(NFA_NOPEN);
1102 break;
1103
1104 case 'd': /* %d123 decimal */
1105 case 'o': /* %o123 octal */
1106 case 'x': /* %xab hex 2 */
1107 case 'u': /* %uabcd hex 4 */
1108 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001109 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001110 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001111
Bram Moolenaar47196582013-05-25 22:04:23 +02001112 switch (c)
1113 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001114 case 'd': nr = getdecchrs(); break;
1115 case 'o': nr = getoctchrs(); break;
1116 case 'x': nr = gethexchrs(2); break;
1117 case 'u': nr = gethexchrs(4); break;
1118 case 'U': nr = gethexchrs(8); break;
1119 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001120 }
1121
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001122 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001123 EMSG2_RET_FAIL(
1124 _("E678: Invalid character after %s%%[dxouU]"),
1125 reg_magic == MAGIC_ALL);
1126 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001127 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001128 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001129 break;
1130
1131 /* Catch \%^ and \%$ regardless of where they appear in the
1132 * pattern -- regardless of whether or not it makes sense. */
1133 case '^':
1134 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001135 break;
1136
1137 case '$':
1138 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139 break;
1140
1141 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001142 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001143 break;
1144
1145 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001146 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001147 break;
1148
1149 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001150 {
1151 int n;
1152
1153 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001154 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001155 {
1156 if (c == NUL)
1157 EMSG2_RET_FAIL(_(e_missing_sb),
1158 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001159 /* recursive call! */
1160 if (nfa_regatom() == FAIL)
1161 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001162 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001163 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001164 if (n == 0)
1165 EMSG2_RET_FAIL(_(e_empty_sb),
1166 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001167 EMIT(NFA_OPT_CHARS);
1168 EMIT(n);
1169 break;
1170 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001171
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001172 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001173 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001174 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001175 int cmp = c;
1176
1177 if (c == '<' || c == '>')
1178 c = getchr();
1179 while (VIM_ISDIGIT(c))
1180 {
1181 n = n * 10 + (c - '0');
1182 c = getchr();
1183 }
1184 if (c == 'l' || c == 'c' || c == 'v')
1185 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001186 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001187 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001188 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001189 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001190 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001191 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001192 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001193 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001194 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001195 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001196 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001197 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001198 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001199 break;
1200 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001201 else if (c == '\'' && n == 0)
1202 {
1203 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001204 EMIT(cmp == '<' ? NFA_MARK_LT :
1205 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001206 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001207 break;
1208 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001209 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001210 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1211 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 return FAIL;
1213 }
1214 break;
1215
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001216 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001217collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001218 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001219 * [abc] uses NFA_START_COLL - NFA_END_COLL
1220 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1221 * Each character is produced as a regular state, using
1222 * NFA_CONCAT to bind them together.
1223 * Besides normal characters there can be:
1224 * - character classes NFA_CLASS_*
1225 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226 */
1227
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228 p = regparse;
1229 endp = skip_anyof(p);
1230 if (*endp == ']')
1231 {
1232 /*
1233 * Try to reverse engineer character classes. For example,
1234 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1235 * and perform the necessary substitutions in the NFA.
1236 */
1237 result = nfa_recognize_char_class(regparse, endp,
1238 extra == ADD_NL);
1239 if (result != FAIL)
1240 {
1241 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1242 EMIT(result);
1243 else /* must be char class + newline */
1244 {
1245 EMIT(result - ADD_NL);
1246 EMIT(NFA_NEWL);
1247 EMIT(NFA_OR);
1248 }
1249 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001250 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 return OK;
1252 }
1253 /*
1254 * Failed to recognize a character class. Use the simple
1255 * version that turns [abc] into 'a' OR 'b' OR 'c'
1256 */
1257 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001258 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259 if (*regparse == '^') /* negated range */
1260 {
1261 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001262 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001263 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001265 else
1266 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267 if (*regparse == '-')
1268 {
1269 startc = '-';
1270 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001271 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001272 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 }
1274 /* Emit the OR branches for each character in the [] */
1275 emit_range = FALSE;
1276 while (regparse < endp)
1277 {
1278 oldstartc = startc;
1279 startc = -1;
1280 got_coll_char = FALSE;
1281 if (*regparse == '[')
1282 {
1283 /* Check for [: :], [= =], [. .] */
1284 equiclass = collclass = 0;
1285 charclass = get_char_class(&regparse);
1286 if (charclass == CLASS_NONE)
1287 {
1288 equiclass = get_equi_class(&regparse);
1289 if (equiclass == 0)
1290 collclass = get_coll_element(&regparse);
1291 }
1292
1293 /* Character class like [:alpha:] */
1294 if (charclass != CLASS_NONE)
1295 {
1296 switch (charclass)
1297 {
1298 case CLASS_ALNUM:
1299 EMIT(NFA_CLASS_ALNUM);
1300 break;
1301 case CLASS_ALPHA:
1302 EMIT(NFA_CLASS_ALPHA);
1303 break;
1304 case CLASS_BLANK:
1305 EMIT(NFA_CLASS_BLANK);
1306 break;
1307 case CLASS_CNTRL:
1308 EMIT(NFA_CLASS_CNTRL);
1309 break;
1310 case CLASS_DIGIT:
1311 EMIT(NFA_CLASS_DIGIT);
1312 break;
1313 case CLASS_GRAPH:
1314 EMIT(NFA_CLASS_GRAPH);
1315 break;
1316 case CLASS_LOWER:
1317 EMIT(NFA_CLASS_LOWER);
1318 break;
1319 case CLASS_PRINT:
1320 EMIT(NFA_CLASS_PRINT);
1321 break;
1322 case CLASS_PUNCT:
1323 EMIT(NFA_CLASS_PUNCT);
1324 break;
1325 case CLASS_SPACE:
1326 EMIT(NFA_CLASS_SPACE);
1327 break;
1328 case CLASS_UPPER:
1329 EMIT(NFA_CLASS_UPPER);
1330 break;
1331 case CLASS_XDIGIT:
1332 EMIT(NFA_CLASS_XDIGIT);
1333 break;
1334 case CLASS_TAB:
1335 EMIT(NFA_CLASS_TAB);
1336 break;
1337 case CLASS_RETURN:
1338 EMIT(NFA_CLASS_RETURN);
1339 break;
1340 case CLASS_BACKSPACE:
1341 EMIT(NFA_CLASS_BACKSPACE);
1342 break;
1343 case CLASS_ESCAPE:
1344 EMIT(NFA_CLASS_ESCAPE);
1345 break;
1346 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001347 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 continue;
1349 }
1350 /* Try equivalence class [=a=] and the like */
1351 if (equiclass != 0)
1352 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001353 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001354 if (result == FAIL)
1355 {
1356 /* should never happen */
1357 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1358 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359 continue;
1360 }
1361 /* Try collating class like [. .] */
1362 if (collclass != 0)
1363 {
1364 startc = collclass; /* allow [.a.]-x as a range */
1365 /* Will emit the proper atom at the end of the
1366 * while loop. */
1367 }
1368 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001369 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1370 * start character. */
1371 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001372 {
1373 emit_range = TRUE;
1374 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001375 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001376 continue; /* reading the end of the range */
1377 }
1378
1379 /* Now handle simple and escaped characters.
1380 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1381 * accepts "\t", "\e", etc., but only when the 'l' flag in
1382 * 'cpoptions' is not included.
1383 * Posix doesn't recognize backslash at all.
1384 */
1385 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001386 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001387 && regparse + 1 <= endp
1388 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001389 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 && vim_strchr(REGEXP_ABBR, regparse[1])
1391 != NULL)
1392 )
1393 )
1394 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001395 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001396
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001397 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001398 startc = reg_string ? NL : NFA_NEWL;
1399 else
1400 if (*regparse == 'd'
1401 || *regparse == 'o'
1402 || *regparse == 'x'
1403 || *regparse == 'u'
1404 || *regparse == 'U'
1405 )
1406 {
1407 /* TODO(RE) This needs more testing */
1408 startc = coll_get_char();
1409 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001410 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001411 }
1412 else
1413 {
1414 /* \r,\t,\e,\b */
1415 startc = backslash_trans(*regparse);
1416 }
1417 }
1418
1419 /* Normal printable char */
1420 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001421 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001422
1423 /* Previous char was '-', so this char is end of range. */
1424 if (emit_range)
1425 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001426 endc = startc;
1427 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001428 if (startc > endc)
1429 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001430
1431 if (endc > startc + 2)
1432 {
1433 /* Emit a range instead of the sequence of
1434 * individual characters. */
1435 if (startc == 0)
1436 /* \x00 is translated to \x0a, start at \x01. */
1437 EMIT(1);
1438 else
1439 --post_ptr; /* remove NFA_CONCAT */
1440 EMIT(endc);
1441 EMIT(NFA_RANGE);
1442 EMIT(NFA_CONCAT);
1443 }
1444 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001446 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 || (*mb_char2len)(endc) > 1))
1448 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001449 /* Emit the characters in the range.
1450 * "startc" was already emitted, so skip it.
1451 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 for (c = startc + 1; c <= endc; c++)
1453 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001454 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001455 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001456 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001457 }
1458 else
1459#endif
1460 {
1461#ifdef EBCDIC
1462 int alpha_only = FALSE;
1463
1464 /* for alphabetical range skip the gaps
1465 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1466 if (isalpha(startc) && isalpha(endc))
1467 alpha_only = TRUE;
1468#endif
1469 /* Emit the range. "startc" was already emitted, so
1470 * skip it. */
1471 for (c = startc + 1; c <= endc; c++)
1472#ifdef EBCDIC
1473 if (!alpha_only || isalpha(startc))
1474#endif
1475 {
1476 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001477 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001478 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001479 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001480 emit_range = FALSE;
1481 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001482 }
1483 else
1484 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001485 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 * Normally, simply emit startc. But if we get char
1488 * code=0 from a collating char, then replace it with
1489 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001490 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001491 * the backtracking engine. */
1492 if (startc == NFA_NEWL)
1493 {
1494 /* Line break can't be matched as part of the
1495 * collection, add an OR below. But not for negated
1496 * range. */
1497 if (!negated)
1498 extra = ADD_NL;
1499 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001501 {
1502 if (got_coll_char == TRUE && startc == 0)
1503 EMIT(0x0a);
1504 else
1505 EMIT(startc);
1506 EMIT(NFA_CONCAT);
1507 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508 }
1509
Bram Moolenaar51a29832013-05-28 22:30:35 +02001510 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 } /* while (p < endp) */
1512
Bram Moolenaar51a29832013-05-28 22:30:35 +02001513 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001514 if (*regparse == '-') /* if last, '-' is just a char */
1515 {
1516 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001517 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001519 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521 /* skip the trailing ] */
1522 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001523 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001524
1525 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001526 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001527 EMIT(NFA_END_NEG_COLL);
1528 else
1529 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001530
1531 /* \_[] also matches \n but it's not negated */
1532 if (extra == ADD_NL)
1533 {
1534 EMIT(reg_string ? NL : NFA_NEWL);
1535 EMIT(NFA_OR);
1536 }
1537
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001538 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001539 } /* if exists closing ] */
1540
1541 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001543 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001544
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 default:
1546 {
1547#ifdef FEAT_MBYTE
1548 int plen;
1549
1550nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001551 /* plen is length of current char with composing chars */
1552 if (enc_utf8 && ((*mb_char2len)(c)
1553 != (plen = (*mb_ptr2len)(old_regparse))
1554 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001556 int i = 0;
1557
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001558 /* A base character plus composing characters, or just one
1559 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001560 * This requires creating a separate atom as if enclosing
1561 * the characters in (), where NFA_COMPOSING is the ( and
1562 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 * building the postfix form, not the NFA itself;
1564 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001565 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001566 for (;;)
1567 {
1568 EMIT(c);
1569 if (i > 0)
1570 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001571 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001572 break;
1573 c = utf_ptr2char(old_regparse + i);
1574 }
1575 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001576 regparse = old_regparse + plen;
1577 }
1578 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001579#endif
1580 {
1581 c = no_Magic(c);
1582 EMIT(c);
1583 }
1584 return OK;
1585 }
1586 }
1587
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001588 return OK;
1589}
1590
1591/*
1592 * Parse something followed by possible [*+=].
1593 *
1594 * A piece is an atom, possibly followed by a multi, an indication of how many
1595 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1596 * characters: "", "a", "aa", etc.
1597 *
1598 * piece ::= atom
1599 * or atom multi
1600 */
1601 static int
1602nfa_regpiece()
1603{
1604 int i;
1605 int op;
1606 int ret;
1607 long minval, maxval;
1608 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001609 parse_state_T old_state;
1610 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001611 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001612 int old_post_pos;
1613 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001614 int quest;
1615
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001616 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1617 * next. */
1618 save_parse_state(&old_state);
1619
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001620 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001621 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001622
1623 ret = nfa_regatom();
1624 if (ret == FAIL)
1625 return FAIL; /* cascaded error */
1626
1627 op = peekchr();
1628 if (re_multi_type(op) == NOT_MULTI)
1629 return OK;
1630
1631 skipchr();
1632 switch (op)
1633 {
1634 case Magic('*'):
1635 EMIT(NFA_STAR);
1636 break;
1637
1638 case Magic('+'):
1639 /*
1640 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1641 * first and only submatch would be "aaa". But the backtracking
1642 * engine interprets the plus as "try matching one more time", and
1643 * a* matches a second time at the end of the input, the empty
1644 * string.
1645 * The submatch will the empty string.
1646 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001647 * In order to be consistent with the old engine, we replace
1648 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001649 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001650 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 curchr = -1;
1652 if (nfa_regatom() == FAIL)
1653 return FAIL;
1654 EMIT(NFA_STAR);
1655 EMIT(NFA_CONCAT);
1656 skipchr(); /* skip the \+ */
1657 break;
1658
1659 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001660 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001661 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001662 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 switch(op)
1664 {
1665 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001666 /* \@= */
1667 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001669 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001670 /* \@! */
1671 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001672 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001673 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001674 op = no_Magic(getchr());
1675 if (op == '=')
1676 /* \@<= */
1677 i = NFA_PREV_ATOM_JUST_BEFORE;
1678 else if (op == '!')
1679 /* \@<! */
1680 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1681 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001682 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001683 /* \@> */
1684 i = NFA_PREV_ATOM_LIKE_PATTERN;
1685 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001686 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001687 if (i == 0)
1688 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001689 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1690 return FAIL;
1691 }
1692 EMIT(i);
1693 if (i == NFA_PREV_ATOM_JUST_BEFORE
1694 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1695 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001696 break;
1697
1698 case Magic('?'):
1699 case Magic('='):
1700 EMIT(NFA_QUEST);
1701 break;
1702
1703 case Magic('{'):
1704 /* a{2,5} will expand to 'aaa?a?a?'
1705 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1706 * version of '?'
1707 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1708 * parenthesis have the same id
1709 */
1710
1711 greedy = TRUE;
1712 c2 = peekchr();
1713 if (c2 == '-' || c2 == Magic('-'))
1714 {
1715 skipchr();
1716 greedy = FALSE;
1717 }
1718 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001719 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001720
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001721 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1722 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001723 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001724 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001725 if (greedy)
1726 /* \{}, \{0,} */
1727 EMIT(NFA_STAR);
1728 else
1729 /* \{-}, \{-0,} */
1730 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001731 break;
1732 }
1733
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001734 /* Special case: x{0} or x{-0} */
1735 if (maxval == 0)
1736 {
1737 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001738 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1740 EMIT(NFA_SKIP_CHAR);
1741 return OK;
1742 }
1743
1744 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001745 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001746 /* Save parse state after the repeated atom and the \{} */
1747 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001748
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001749 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1750 for (i = 0; i < maxval; i++)
1751 {
1752 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001753 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001754 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001755 if (nfa_regatom() == FAIL)
1756 return FAIL;
1757 /* after "minval" times, atoms are optional */
1758 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001759 {
1760 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001761 {
1762 if (greedy)
1763 EMIT(NFA_STAR);
1764 else
1765 EMIT(NFA_STAR_NONGREEDY);
1766 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001767 else
1768 EMIT(quest);
1769 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001770 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001771 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001772 if (i + 1 > minval && maxval == MAX_LIMIT)
1773 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001774 }
1775
1776 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001777 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778 curchr = -1;
1779
1780 break;
1781
1782
1783 default:
1784 break;
1785 } /* end switch */
1786
1787 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001790
1791 return OK;
1792}
1793
1794/*
1795 * Parse one or more pieces, concatenated. It matches a match for the
1796 * first piece, followed by a match for the second piece, etc. Example:
1797 * "f[0-9]b", first matches "f", then a digit and then "b".
1798 *
1799 * concat ::= piece
1800 * or piece piece
1801 * or piece piece piece
1802 * etc.
1803 */
1804 static int
1805nfa_regconcat()
1806{
1807 int cont = TRUE;
1808 int first = TRUE;
1809
1810 while (cont)
1811 {
1812 switch (peekchr())
1813 {
1814 case NUL:
1815 case Magic('|'):
1816 case Magic('&'):
1817 case Magic(')'):
1818 cont = FALSE;
1819 break;
1820
1821 case Magic('Z'):
1822#ifdef FEAT_MBYTE
1823 regflags |= RF_ICOMBINE;
1824#endif
1825 skipchr_keepstart();
1826 break;
1827 case Magic('c'):
1828 regflags |= RF_ICASE;
1829 skipchr_keepstart();
1830 break;
1831 case Magic('C'):
1832 regflags |= RF_NOICASE;
1833 skipchr_keepstart();
1834 break;
1835 case Magic('v'):
1836 reg_magic = MAGIC_ALL;
1837 skipchr_keepstart();
1838 curchr = -1;
1839 break;
1840 case Magic('m'):
1841 reg_magic = MAGIC_ON;
1842 skipchr_keepstart();
1843 curchr = -1;
1844 break;
1845 case Magic('M'):
1846 reg_magic = MAGIC_OFF;
1847 skipchr_keepstart();
1848 curchr = -1;
1849 break;
1850 case Magic('V'):
1851 reg_magic = MAGIC_NONE;
1852 skipchr_keepstart();
1853 curchr = -1;
1854 break;
1855
1856 default:
1857 if (nfa_regpiece() == FAIL)
1858 return FAIL;
1859 if (first == FALSE)
1860 EMIT(NFA_CONCAT);
1861 else
1862 first = FALSE;
1863 break;
1864 }
1865 }
1866
1867 return OK;
1868}
1869
1870/*
1871 * Parse a branch, one or more concats, separated by "\&". It matches the
1872 * last concat, but only if all the preceding concats also match at the same
1873 * position. Examples:
1874 * "foobeep\&..." matches "foo" in "foobeep".
1875 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1876 *
1877 * branch ::= concat
1878 * or concat \& concat
1879 * or concat \& concat \& concat
1880 * etc.
1881 */
1882 static int
1883nfa_regbranch()
1884{
1885 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001886 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887
Bram Moolenaar16299b52013-05-30 18:45:23 +02001888 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889
1890 /* First branch, possibly the only one */
1891 if (nfa_regconcat() == FAIL)
1892 return FAIL;
1893
1894 ch = peekchr();
1895 /* Try next concats */
1896 while (ch == Magic('&'))
1897 {
1898 skipchr();
1899 EMIT(NFA_NOPEN);
1900 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001901 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 if (nfa_regconcat() == FAIL)
1903 return FAIL;
1904 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001905 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 EMIT(NFA_SKIP_CHAR);
1907 EMIT(NFA_CONCAT);
1908 ch = peekchr();
1909 }
1910
1911 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001912 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001913 EMIT(NFA_SKIP_CHAR);
1914
1915 return OK;
1916}
1917
1918/*
1919 * Parse a pattern, one or more branches, separated by "\|". It matches
1920 * anything that matches one of the branches. Example: "foo\|beep" matches
1921 * "foo" and matches "beep". If more than one branch matches, the first one
1922 * is used.
1923 *
1924 * pattern ::= branch
1925 * or branch \| branch
1926 * or branch \| branch \| branch
1927 * etc.
1928 */
1929 static int
1930nfa_reg(paren)
1931 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1932{
1933 int parno = 0;
1934
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 if (paren == REG_PAREN)
1936 {
1937 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 parno = regnpar++;
1940 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001941#ifdef FEAT_SYN_HL
1942 else if (paren == REG_ZPAREN)
1943 {
1944 /* Make a ZOPEN node. */
1945 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001946 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001947 parno = regnzpar++;
1948 }
1949#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950
1951 if (nfa_regbranch() == FAIL)
1952 return FAIL; /* cascaded error */
1953
1954 while (peekchr() == Magic('|'))
1955 {
1956 skipchr();
1957 if (nfa_regbranch() == FAIL)
1958 return FAIL; /* cascaded error */
1959 EMIT(NFA_OR);
1960 }
1961
1962 /* Check for proper termination. */
1963 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1964 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001965 if (paren == REG_NPAREN)
1966 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1967 else
1968 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1969 }
1970 else if (paren == REG_NOPAREN && peekchr() != NUL)
1971 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001972 if (peekchr() == Magic(')'))
1973 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1974 else
1975 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1976 }
1977 /*
1978 * Here we set the flag allowing back references to this set of
1979 * parentheses.
1980 */
1981 if (paren == REG_PAREN)
1982 {
1983 had_endbrace[parno] = TRUE; /* have seen the close paren */
1984 EMIT(NFA_MOPEN + parno);
1985 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001986#ifdef FEAT_SYN_HL
1987 else if (paren == REG_ZPAREN)
1988 EMIT(NFA_ZOPEN + parno);
1989#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001990
1991 return OK;
1992}
1993
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001994#ifdef DEBUG
1995static char_u code[50];
1996
1997 static void
1998nfa_set_code(c)
1999 int c;
2000{
2001 int addnl = FALSE;
2002
2003 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2004 {
2005 addnl = TRUE;
2006 c -= ADD_NL;
2007 }
2008
2009 STRCPY(code, "");
2010 switch (c)
2011 {
2012 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2013 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2014 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2015 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2016 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2017 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2018
Bram Moolenaar5714b802013-05-28 22:03:20 +02002019 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2020 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2021 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2022 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2023 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2024 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2025 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2026 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2027 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002028#ifdef FEAT_SYN_HL
2029 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2030 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2031 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2032 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2033 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2034 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2035 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2036 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2037 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2038#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002039 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2040
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002041 case NFA_PREV_ATOM_NO_WIDTH:
2042 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002043 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2044 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002045 case NFA_PREV_ATOM_JUST_BEFORE:
2046 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2047 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2048 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002049 case NFA_PREV_ATOM_LIKE_PATTERN:
2050 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2051
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002052 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2053 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002055 case NFA_START_INVISIBLE_FIRST:
2056 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002057 case NFA_START_INVISIBLE_NEG:
2058 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002059 case NFA_START_INVISIBLE_NEG_FIRST:
2060 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002061 case NFA_START_INVISIBLE_BEFORE:
2062 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002063 case NFA_START_INVISIBLE_BEFORE_FIRST:
2064 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002065 case NFA_START_INVISIBLE_BEFORE_NEG:
2066 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002067 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2068 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002069 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002071 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002072 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002074 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2075 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002076 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002078 case NFA_MOPEN:
2079 case NFA_MOPEN1:
2080 case NFA_MOPEN2:
2081 case NFA_MOPEN3:
2082 case NFA_MOPEN4:
2083 case NFA_MOPEN5:
2084 case NFA_MOPEN6:
2085 case NFA_MOPEN7:
2086 case NFA_MOPEN8:
2087 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002088 STRCPY(code, "NFA_MOPEN(x)");
2089 code[10] = c - NFA_MOPEN + '0';
2090 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002091 case NFA_MCLOSE:
2092 case NFA_MCLOSE1:
2093 case NFA_MCLOSE2:
2094 case NFA_MCLOSE3:
2095 case NFA_MCLOSE4:
2096 case NFA_MCLOSE5:
2097 case NFA_MCLOSE6:
2098 case NFA_MCLOSE7:
2099 case NFA_MCLOSE8:
2100 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002101 STRCPY(code, "NFA_MCLOSE(x)");
2102 code[11] = c - NFA_MCLOSE + '0';
2103 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002104#ifdef FEAT_SYN_HL
2105 case NFA_ZOPEN:
2106 case NFA_ZOPEN1:
2107 case NFA_ZOPEN2:
2108 case NFA_ZOPEN3:
2109 case NFA_ZOPEN4:
2110 case NFA_ZOPEN5:
2111 case NFA_ZOPEN6:
2112 case NFA_ZOPEN7:
2113 case NFA_ZOPEN8:
2114 case NFA_ZOPEN9:
2115 STRCPY(code, "NFA_ZOPEN(x)");
2116 code[10] = c - NFA_ZOPEN + '0';
2117 break;
2118 case NFA_ZCLOSE:
2119 case NFA_ZCLOSE1:
2120 case NFA_ZCLOSE2:
2121 case NFA_ZCLOSE3:
2122 case NFA_ZCLOSE4:
2123 case NFA_ZCLOSE5:
2124 case NFA_ZCLOSE6:
2125 case NFA_ZCLOSE7:
2126 case NFA_ZCLOSE8:
2127 case NFA_ZCLOSE9:
2128 STRCPY(code, "NFA_ZCLOSE(x)");
2129 code[11] = c - NFA_ZCLOSE + '0';
2130 break;
2131#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002132 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2133 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2134 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2135 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002136 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2137 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002138 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2139 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2140 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2141 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2142 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2143 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2144 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2145 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2146 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2147 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2148 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2149 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2150 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2151 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2152
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002153 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002154 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2155 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2156 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002157 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2158 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002159
2160 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2161 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2162 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2163 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2164 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2165 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2166 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002168 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2169 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2170 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2171 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2172 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2173 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2174 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2175 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2176 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2177 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2178 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2179 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2180 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2181 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2182 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2183 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2184
2185 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2186 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2187 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2188 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2189 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2190 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2191 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2192 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2193 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2194 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2195 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2196 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2197 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2198 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2199 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2200 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2201 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2202 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2203 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2204 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2205 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2206 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2207 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2208 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2209 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2210 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2211 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2212
2213 default:
2214 STRCPY(code, "CHAR(x)");
2215 code[5] = c;
2216 }
2217
2218 if (addnl == TRUE)
2219 STRCAT(code, " + NEWLINE ");
2220
2221}
2222
2223#ifdef ENABLE_LOG
2224static FILE *log_fd;
2225
2226/*
2227 * Print the postfix notation of the current regexp.
2228 */
2229 static void
2230nfa_postfix_dump(expr, retval)
2231 char_u *expr;
2232 int retval;
2233{
2234 int *p;
2235 FILE *f;
2236
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002237 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002238 if (f != NULL)
2239 {
2240 fprintf(f, "\n-------------------------\n");
2241 if (retval == FAIL)
2242 fprintf(f, ">>> NFA engine failed ... \n");
2243 else if (retval == OK)
2244 fprintf(f, ">>> NFA engine succeeded !\n");
2245 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002246 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002247 {
2248 nfa_set_code(*p);
2249 fprintf(f, "%s, ", code);
2250 }
2251 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002252 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002253 fprintf(f, "%d ", *p);
2254 fprintf(f, "\n\n");
2255 fclose(f);
2256 }
2257}
2258
2259/*
2260 * Print the NFA starting with a root node "state".
2261 */
2262 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002263nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002264 FILE *debugf;
2265 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002267 garray_T indent;
2268
2269 ga_init2(&indent, 1, 64);
2270 ga_append(&indent, '\0');
2271 nfa_print_state2(debugf, state, &indent);
2272 ga_clear(&indent);
2273}
2274
2275 static void
2276nfa_print_state2(debugf, state, indent)
2277 FILE *debugf;
2278 nfa_state_T *state;
2279 garray_T *indent;
2280{
2281 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282
2283 if (state == NULL)
2284 return;
2285
2286 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002287
2288 /* Output indent */
2289 p = (char_u *)indent->ga_data;
2290 if (indent->ga_len >= 3)
2291 {
2292 int last = indent->ga_len - 3;
2293 char_u save[2];
2294
2295 STRNCPY(save, &p[last], 2);
2296 STRNCPY(&p[last], "+-", 2);
2297 fprintf(debugf, " %s", p);
2298 STRNCPY(&p[last], save, 2);
2299 }
2300 else
2301 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302
2303 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002304 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002305 code,
2306 state->c,
2307 abs(state->id),
2308 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 if (state->id < 0)
2310 return;
2311
2312 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002313
2314 /* grow indent for state->out */
2315 indent->ga_len -= 1;
2316 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002317 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002318 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002319 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002320 ga_append(indent, '\0');
2321
2322 nfa_print_state2(debugf, state->out, indent);
2323
2324 /* replace last part of indent for state->out1 */
2325 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002326 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002327 ga_append(indent, '\0');
2328
2329 nfa_print_state2(debugf, state->out1, indent);
2330
2331 /* shrink indent */
2332 indent->ga_len -= 3;
2333 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334}
2335
2336/*
2337 * Print the NFA state machine.
2338 */
2339 static void
2340nfa_dump(prog)
2341 nfa_regprog_T *prog;
2342{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002343 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002344
2345 if (debugf != NULL)
2346 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002347 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002348
Bram Moolenaar473de612013-06-08 18:19:48 +02002349 if (prog->reganch)
2350 fprintf(debugf, "reganch: %d\n", prog->reganch);
2351 if (prog->regstart != NUL)
2352 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2353 prog->regstart, prog->regstart);
2354 if (prog->match_text != NULL)
2355 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002356
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357 fclose(debugf);
2358 }
2359}
2360#endif /* ENABLE_LOG */
2361#endif /* DEBUG */
2362
2363/*
2364 * Parse r.e. @expr and convert it into postfix form.
2365 * Return the postfix string on success, NULL otherwise.
2366 */
2367 static int *
2368re2post()
2369{
2370 if (nfa_reg(REG_NOPAREN) == FAIL)
2371 return NULL;
2372 EMIT(NFA_MOPEN);
2373 return post_start;
2374}
2375
2376/* NB. Some of the code below is inspired by Russ's. */
2377
2378/*
2379 * Represents an NFA state plus zero or one or two arrows exiting.
2380 * if c == MATCH, no arrows out; matching state.
2381 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2382 * If c < 256, labeled arrow with character c to out.
2383 */
2384
2385static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2386
2387/*
2388 * Allocate and initialize nfa_state_T.
2389 */
2390 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002391alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002392 int c;
2393 nfa_state_T *out;
2394 nfa_state_T *out1;
2395{
2396 nfa_state_T *s;
2397
2398 if (istate >= nstate)
2399 return NULL;
2400
2401 s = &state_ptr[istate++];
2402
2403 s->c = c;
2404 s->out = out;
2405 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002406 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002407
2408 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002409 s->lastlist[0] = 0;
2410 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002411
2412 return s;
2413}
2414
2415/*
2416 * A partially built NFA without the matching state filled in.
2417 * Frag_T.start points at the start state.
2418 * Frag_T.out is a list of places that need to be set to the
2419 * next state for this fragment.
2420 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002421
2422/* Since the out pointers in the list are always
2423 * uninitialized, we use the pointers themselves
2424 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002426union Ptrlist
2427{
2428 Ptrlist *next;
2429 nfa_state_T *s;
2430};
2431
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432struct Frag
2433{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002434 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002435 Ptrlist *out;
2436};
2437typedef struct Frag Frag_T;
2438
2439static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2440static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2441static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2442static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2443static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2444static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2445
2446/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002447 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 */
2449 static Frag_T
2450frag(start, out)
2451 nfa_state_T *start;
2452 Ptrlist *out;
2453{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002454 Frag_T n;
2455
2456 n.start = start;
2457 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458 return n;
2459}
2460
2461/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462 * Create singleton list containing just outp.
2463 */
2464 static Ptrlist *
2465list1(outp)
2466 nfa_state_T **outp;
2467{
2468 Ptrlist *l;
2469
2470 l = (Ptrlist *)outp;
2471 l->next = NULL;
2472 return l;
2473}
2474
2475/*
2476 * Patch the list of states at out to point to start.
2477 */
2478 static void
2479patch(l, s)
2480 Ptrlist *l;
2481 nfa_state_T *s;
2482{
2483 Ptrlist *next;
2484
2485 for (; l; l = next)
2486 {
2487 next = l->next;
2488 l->s = s;
2489 }
2490}
2491
2492
2493/*
2494 * Join the two lists l1 and l2, returning the combination.
2495 */
2496 static Ptrlist *
2497append(l1, l2)
2498 Ptrlist *l1;
2499 Ptrlist *l2;
2500{
2501 Ptrlist *oldl1;
2502
2503 oldl1 = l1;
2504 while (l1->next)
2505 l1 = l1->next;
2506 l1->next = l2;
2507 return oldl1;
2508}
2509
2510/*
2511 * Stack used for transforming postfix form into NFA.
2512 */
2513static Frag_T empty;
2514
2515 static void
2516st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002517 int *postfix UNUSED;
2518 int *end UNUSED;
2519 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002521#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522 FILE *df;
2523 int *p2;
2524
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002525 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002526 if (df)
2527 {
2528 fprintf(df, "Error popping the stack!\n");
2529#ifdef DEBUG
2530 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2531#endif
2532 fprintf(df, "Postfix form is: ");
2533#ifdef DEBUG
2534 for (p2 = postfix; p2 < end; p2++)
2535 {
2536 nfa_set_code(*p2);
2537 fprintf(df, "%s, ", code);
2538 }
2539 nfa_set_code(*p);
2540 fprintf(df, "\nCurrent position is: ");
2541 for (p2 = postfix; p2 <= p; p2 ++)
2542 {
2543 nfa_set_code(*p2);
2544 fprintf(df, "%s, ", code);
2545 }
2546#else
2547 for (p2 = postfix; p2 < end; p2++)
2548 {
2549 fprintf(df, "%d, ", *p2);
2550 }
2551 fprintf(df, "\nCurrent position is: ");
2552 for (p2 = postfix; p2 <= p; p2 ++)
2553 {
2554 fprintf(df, "%d, ", *p2);
2555 }
2556#endif
2557 fprintf(df, "\n--------------------------\n");
2558 fclose(df);
2559 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002560#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002561 EMSG(_("E874: (NFA) Could not pop the stack !"));
2562}
2563
2564/*
2565 * Push an item onto the stack.
2566 */
2567 static void
2568st_push(s, p, stack_end)
2569 Frag_T s;
2570 Frag_T **p;
2571 Frag_T *stack_end;
2572{
2573 Frag_T *stackp = *p;
2574
2575 if (stackp >= stack_end)
2576 return;
2577 *stackp = s;
2578 *p = *p + 1;
2579}
2580
2581/*
2582 * Pop an item from the stack.
2583 */
2584 static Frag_T
2585st_pop(p, stack)
2586 Frag_T **p;
2587 Frag_T *stack;
2588{
2589 Frag_T *stackp;
2590
2591 *p = *p - 1;
2592 stackp = *p;
2593 if (stackp < stack)
2594 return empty;
2595 return **p;
2596}
2597
2598/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002599 * Estimate the maximum byte length of anything matching "state".
2600 * When unknown or unlimited return -1.
2601 */
2602 static int
2603nfa_max_width(startstate, depth)
2604 nfa_state_T *startstate;
2605 int depth;
2606{
2607 int l, r;
2608 nfa_state_T *state = startstate;
2609 int len = 0;
2610
2611 /* detect looping in a NFA_SPLIT */
2612 if (depth > 4)
2613 return -1;
2614
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002615 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002616 {
2617 switch (state->c)
2618 {
2619 case NFA_END_INVISIBLE:
2620 case NFA_END_INVISIBLE_NEG:
2621 /* the end, return what we have */
2622 return len;
2623
2624 case NFA_SPLIT:
2625 /* two alternatives, use the maximum */
2626 l = nfa_max_width(state->out, depth + 1);
2627 r = nfa_max_width(state->out1, depth + 1);
2628 if (l < 0 || r < 0)
2629 return -1;
2630 return len + (l > r ? l : r);
2631
2632 case NFA_ANY:
2633 case NFA_START_COLL:
2634 case NFA_START_NEG_COLL:
2635 /* matches some character, including composing chars */
2636#ifdef FEAT_MBYTE
2637 if (enc_utf8)
2638 len += MB_MAXBYTES;
2639 else if (has_mbyte)
2640 len += 2;
2641 else
2642#endif
2643 ++len;
2644 if (state->c != NFA_ANY)
2645 {
2646 /* skip over the characters */
2647 state = state->out1->out;
2648 continue;
2649 }
2650 break;
2651
2652 case NFA_DIGIT:
2653 case NFA_WHITE:
2654 case NFA_HEX:
2655 case NFA_OCTAL:
2656 /* ascii */
2657 ++len;
2658 break;
2659
2660 case NFA_IDENT:
2661 case NFA_SIDENT:
2662 case NFA_KWORD:
2663 case NFA_SKWORD:
2664 case NFA_FNAME:
2665 case NFA_SFNAME:
2666 case NFA_PRINT:
2667 case NFA_SPRINT:
2668 case NFA_NWHITE:
2669 case NFA_NDIGIT:
2670 case NFA_NHEX:
2671 case NFA_NOCTAL:
2672 case NFA_WORD:
2673 case NFA_NWORD:
2674 case NFA_HEAD:
2675 case NFA_NHEAD:
2676 case NFA_ALPHA:
2677 case NFA_NALPHA:
2678 case NFA_LOWER:
2679 case NFA_NLOWER:
2680 case NFA_UPPER:
2681 case NFA_NUPPER:
2682 /* possibly non-ascii */
2683#ifdef FEAT_MBYTE
2684 if (has_mbyte)
2685 len += 3;
2686 else
2687#endif
2688 ++len;
2689 break;
2690
2691 case NFA_START_INVISIBLE:
2692 case NFA_START_INVISIBLE_NEG:
2693 case NFA_START_INVISIBLE_BEFORE:
2694 case NFA_START_INVISIBLE_BEFORE_NEG:
2695 /* zero-width, out1 points to the END state */
2696 state = state->out1->out;
2697 continue;
2698
2699 case NFA_BACKREF1:
2700 case NFA_BACKREF2:
2701 case NFA_BACKREF3:
2702 case NFA_BACKREF4:
2703 case NFA_BACKREF5:
2704 case NFA_BACKREF6:
2705 case NFA_BACKREF7:
2706 case NFA_BACKREF8:
2707 case NFA_BACKREF9:
2708#ifdef FEAT_SYN_HL
2709 case NFA_ZREF1:
2710 case NFA_ZREF2:
2711 case NFA_ZREF3:
2712 case NFA_ZREF4:
2713 case NFA_ZREF5:
2714 case NFA_ZREF6:
2715 case NFA_ZREF7:
2716 case NFA_ZREF8:
2717 case NFA_ZREF9:
2718#endif
2719 case NFA_NEWL:
2720 case NFA_SKIP:
2721 /* unknown width */
2722 return -1;
2723
2724 case NFA_BOL:
2725 case NFA_EOL:
2726 case NFA_BOF:
2727 case NFA_EOF:
2728 case NFA_BOW:
2729 case NFA_EOW:
2730 case NFA_MOPEN:
2731 case NFA_MOPEN1:
2732 case NFA_MOPEN2:
2733 case NFA_MOPEN3:
2734 case NFA_MOPEN4:
2735 case NFA_MOPEN5:
2736 case NFA_MOPEN6:
2737 case NFA_MOPEN7:
2738 case NFA_MOPEN8:
2739 case NFA_MOPEN9:
2740#ifdef FEAT_SYN_HL
2741 case NFA_ZOPEN:
2742 case NFA_ZOPEN1:
2743 case NFA_ZOPEN2:
2744 case NFA_ZOPEN3:
2745 case NFA_ZOPEN4:
2746 case NFA_ZOPEN5:
2747 case NFA_ZOPEN6:
2748 case NFA_ZOPEN7:
2749 case NFA_ZOPEN8:
2750 case NFA_ZOPEN9:
2751 case NFA_ZCLOSE:
2752 case NFA_ZCLOSE1:
2753 case NFA_ZCLOSE2:
2754 case NFA_ZCLOSE3:
2755 case NFA_ZCLOSE4:
2756 case NFA_ZCLOSE5:
2757 case NFA_ZCLOSE6:
2758 case NFA_ZCLOSE7:
2759 case NFA_ZCLOSE8:
2760 case NFA_ZCLOSE9:
2761#endif
2762 case NFA_MCLOSE:
2763 case NFA_MCLOSE1:
2764 case NFA_MCLOSE2:
2765 case NFA_MCLOSE3:
2766 case NFA_MCLOSE4:
2767 case NFA_MCLOSE5:
2768 case NFA_MCLOSE6:
2769 case NFA_MCLOSE7:
2770 case NFA_MCLOSE8:
2771 case NFA_MCLOSE9:
2772 case NFA_NOPEN:
2773 case NFA_NCLOSE:
2774
2775 case NFA_LNUM_GT:
2776 case NFA_LNUM_LT:
2777 case NFA_COL_GT:
2778 case NFA_COL_LT:
2779 case NFA_VCOL_GT:
2780 case NFA_VCOL_LT:
2781 case NFA_MARK_GT:
2782 case NFA_MARK_LT:
2783 case NFA_VISUAL:
2784 case NFA_LNUM:
2785 case NFA_CURSOR:
2786 case NFA_COL:
2787 case NFA_VCOL:
2788 case NFA_MARK:
2789
2790 case NFA_ZSTART:
2791 case NFA_ZEND:
2792 case NFA_OPT_CHARS:
2793 case NFA_SKIP_CHAR:
2794 case NFA_START_PATTERN:
2795 case NFA_END_PATTERN:
2796 case NFA_COMPOSING:
2797 case NFA_END_COMPOSING:
2798 /* zero-width */
2799 break;
2800
2801 default:
2802 if (state->c < 0)
2803 /* don't know what this is */
2804 return -1;
2805 /* normal character */
2806 len += MB_CHAR2LEN(state->c);
2807 break;
2808 }
2809
2810 /* normal way to continue */
2811 state = state->out;
2812 }
2813
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002814 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002815 return -1;
2816}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02002817
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002818/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002819 * Convert a postfix form into its equivalent NFA.
2820 * Return the NFA start state on success, NULL otherwise.
2821 */
2822 static nfa_state_T *
2823post2nfa(postfix, end, nfa_calc_size)
2824 int *postfix;
2825 int *end;
2826 int nfa_calc_size;
2827{
2828 int *p;
2829 int mopen;
2830 int mclose;
2831 Frag_T *stack = NULL;
2832 Frag_T *stackp = NULL;
2833 Frag_T *stack_end = NULL;
2834 Frag_T e1;
2835 Frag_T e2;
2836 Frag_T e;
2837 nfa_state_T *s;
2838 nfa_state_T *s1;
2839 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002840 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002841
2842 if (postfix == NULL)
2843 return NULL;
2844
Bram Moolenaar053bb602013-05-20 13:55:21 +02002845#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002846#define POP() st_pop(&stackp, stack); \
2847 if (stackp < stack) \
2848 { \
2849 st_error(postfix, end, p); \
2850 return NULL; \
2851 }
2852
2853 if (nfa_calc_size == FALSE)
2854 {
2855 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002856 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002857 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002858 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002859 }
2860
2861 for (p = postfix; p < end; ++p)
2862 {
2863 switch (*p)
2864 {
2865 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002866 /* Concatenation.
2867 * Pay attention: this operator does not exist in the r.e. itself
2868 * (it is implicit, really). It is added when r.e. is translated
2869 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002870 if (nfa_calc_size == TRUE)
2871 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002872 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873 break;
2874 }
2875 e2 = POP();
2876 e1 = POP();
2877 patch(e1.out, e2.start);
2878 PUSH(frag(e1.start, e2.out));
2879 break;
2880
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881 case NFA_OR:
2882 /* Alternation */
2883 if (nfa_calc_size == TRUE)
2884 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002885 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 break;
2887 }
2888 e2 = POP();
2889 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002890 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002892 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 PUSH(frag(s, append(e1.out, e2.out)));
2894 break;
2895
2896 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002897 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002898 if (nfa_calc_size == TRUE)
2899 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002900 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 break;
2902 }
2903 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002904 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002905 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002906 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002907 patch(e.out, s);
2908 PUSH(frag(s, list1(&s->out1)));
2909 break;
2910
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002911 case NFA_STAR_NONGREEDY:
2912 /* Zero or more, prefer zero */
2913 if (nfa_calc_size == TRUE)
2914 {
2915 nstate++;
2916 break;
2917 }
2918 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002919 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002920 if (s == NULL)
2921 goto theend;
2922 patch(e.out, s);
2923 PUSH(frag(s, list1(&s->out)));
2924 break;
2925
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 case NFA_QUEST:
2927 /* one or zero atoms=> greedy match */
2928 if (nfa_calc_size == TRUE)
2929 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002930 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931 break;
2932 }
2933 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002934 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002935 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002936 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002937 PUSH(frag(s, append(e.out, list1(&s->out1))));
2938 break;
2939
2940 case NFA_QUEST_NONGREEDY:
2941 /* zero or one atoms => non-greedy match */
2942 if (nfa_calc_size == TRUE)
2943 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002944 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945 break;
2946 }
2947 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002948 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002949 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002950 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002951 PUSH(frag(s, append(e.out, list1(&s->out))));
2952 break;
2953
Bram Moolenaar417bad22013-06-07 14:08:30 +02002954 case NFA_END_COLL:
2955 case NFA_END_NEG_COLL:
2956 /* On the stack is the sequence starting with NFA_START_COLL or
2957 * NFA_START_NEG_COLL and all possible characters. Patch it to
2958 * add the output to the start. */
2959 if (nfa_calc_size == TRUE)
2960 {
2961 nstate++;
2962 break;
2963 }
2964 e = POP();
2965 s = alloc_state(NFA_END_COLL, NULL, NULL);
2966 if (s == NULL)
2967 goto theend;
2968 patch(e.out, s);
2969 e.start->out1 = s;
2970 PUSH(frag(e.start, list1(&s->out)));
2971 break;
2972
2973 case NFA_RANGE:
2974 /* Before this are two characters, the low and high end of a
2975 * range. Turn them into two states with MIN and MAX. */
2976 if (nfa_calc_size == TRUE)
2977 {
2978 /* nstate += 0; */
2979 break;
2980 }
2981 e2 = POP();
2982 e1 = POP();
2983 e2.start->val = e2.start->c;
2984 e2.start->c = NFA_RANGE_MAX;
2985 e1.start->val = e1.start->c;
2986 e1.start->c = NFA_RANGE_MIN;
2987 patch(e1.out, e2.start);
2988 PUSH(frag(e1.start, e2.out));
2989 break;
2990
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002991 case NFA_SKIP_CHAR:
2992 /* Symbol of 0-length, Used in a repetition
2993 * with max/min count of 0 */
2994 if (nfa_calc_size == TRUE)
2995 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002996 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002997 break;
2998 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002999 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003001 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003002 PUSH(frag(s, list1(&s->out)));
3003 break;
3004
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003005 case NFA_OPT_CHARS:
3006 {
3007 int n;
3008
3009 /* \%[abc] */
3010 n = *++p; /* get number of characters */
3011 if (nfa_calc_size == TRUE)
3012 {
3013 nstate += n;
3014 break;
3015 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003016 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003017 e1.out = NULL; /* stores list with out1's */
3018 s1 = NULL; /* previous NFA_SPLIT to connect to */
3019 while (n-- > 0)
3020 {
3021 e = POP(); /* get character */
3022 s = alloc_state(NFA_SPLIT, e.start, NULL);
3023 if (s == NULL)
3024 goto theend;
3025 if (e1.out == NULL)
3026 e1 = e;
3027 patch(e.out, s1);
3028 append(e1.out, list1(&s->out1));
3029 s1 = s;
3030 }
3031 PUSH(frag(s, e1.out));
3032 break;
3033 }
3034
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003035 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003036 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003037 case NFA_PREV_ATOM_JUST_BEFORE:
3038 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003039 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003040 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003041 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3042 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003043 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003044 int start_state;
3045 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003046 int n = 0;
3047 nfa_state_T *zend;
3048 nfa_state_T *skip;
3049
Bram Moolenaardecd9542013-06-07 16:31:50 +02003050 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003051 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003052 case NFA_PREV_ATOM_NO_WIDTH:
3053 start_state = NFA_START_INVISIBLE;
3054 end_state = NFA_END_INVISIBLE;
3055 break;
3056 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3057 start_state = NFA_START_INVISIBLE_NEG;
3058 end_state = NFA_END_INVISIBLE_NEG;
3059 break;
3060 case NFA_PREV_ATOM_JUST_BEFORE:
3061 start_state = NFA_START_INVISIBLE_BEFORE;
3062 end_state = NFA_END_INVISIBLE;
3063 break;
3064 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3065 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3066 end_state = NFA_END_INVISIBLE_NEG;
3067 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003068 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003069 start_state = NFA_START_PATTERN;
3070 end_state = NFA_END_PATTERN;
3071 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003072 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003073
3074 if (before)
3075 n = *++p; /* get the count */
3076
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003077 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003078 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003079 * The \@<= operator: match for the preceding atom.
3080 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003081 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003082 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003083
3084 if (nfa_calc_size == TRUE)
3085 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003086 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003087 break;
3088 }
3089 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003090 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003091 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003092 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003093
Bram Moolenaar87953742013-06-05 18:52:40 +02003094 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003095 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003096 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003097 if (pattern)
3098 {
3099 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3100 skip = alloc_state(NFA_SKIP, NULL, NULL);
3101 zend = alloc_state(NFA_ZEND, s1, NULL);
3102 s1->out= skip;
3103 patch(e.out, zend);
3104 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003105 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003106 else
3107 {
3108 patch(e.out, s1);
3109 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003110 if (before)
3111 {
3112 if (n <= 0)
3113 /* See if we can guess the maximum width, it avoids a
3114 * lot of pointless tries. */
3115 n = nfa_max_width(e.start, 0);
3116 s->val = n; /* store the count */
3117 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003118 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003119 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003120 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003121
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003122#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003123 case NFA_COMPOSING: /* char with composing char */
3124#if 0
3125 /* TODO */
3126 if (regflags & RF_ICOMBINE)
3127 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003128 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003129 }
3130#endif
3131 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003132#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003133
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003134 case NFA_MOPEN: /* \( \) Submatch */
3135 case NFA_MOPEN1:
3136 case NFA_MOPEN2:
3137 case NFA_MOPEN3:
3138 case NFA_MOPEN4:
3139 case NFA_MOPEN5:
3140 case NFA_MOPEN6:
3141 case NFA_MOPEN7:
3142 case NFA_MOPEN8:
3143 case NFA_MOPEN9:
3144#ifdef FEAT_SYN_HL
3145 case NFA_ZOPEN: /* \z( \) Submatch */
3146 case NFA_ZOPEN1:
3147 case NFA_ZOPEN2:
3148 case NFA_ZOPEN3:
3149 case NFA_ZOPEN4:
3150 case NFA_ZOPEN5:
3151 case NFA_ZOPEN6:
3152 case NFA_ZOPEN7:
3153 case NFA_ZOPEN8:
3154 case NFA_ZOPEN9:
3155#endif
3156 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003157 if (nfa_calc_size == TRUE)
3158 {
3159 nstate += 2;
3160 break;
3161 }
3162
3163 mopen = *p;
3164 switch (*p)
3165 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003166 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3167#ifdef FEAT_SYN_HL
3168 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3169 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3170 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3171 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3172 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3173 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3174 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3175 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3176 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3177 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3178#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003179#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003180 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003181#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003182 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003183 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003184 mclose = *p + NSUBEXP;
3185 break;
3186 }
3187
3188 /* Allow "NFA_MOPEN" as a valid postfix representation for
3189 * the empty regexp "". In this case, the NFA will be
3190 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3191 * empty groups of parenthesis, and empty mbyte chars */
3192 if (stackp == stack)
3193 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003194 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003196 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003197 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003198 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003199 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 patch(list1(&s->out), s1);
3201 PUSH(frag(s, list1(&s1->out)));
3202 break;
3203 }
3204
3205 /* At least one node was emitted before NFA_MOPEN, so
3206 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3207 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003208 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003210 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211
Bram Moolenaar525666f2013-06-02 16:40:55 +02003212 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003214 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003215 patch(e.out, s1);
3216
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003217#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003218 if (mopen == NFA_COMPOSING)
3219 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003221#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003222
3223 PUSH(frag(s, list1(&s1->out)));
3224 break;
3225
Bram Moolenaar5714b802013-05-28 22:03:20 +02003226 case NFA_BACKREF1:
3227 case NFA_BACKREF2:
3228 case NFA_BACKREF3:
3229 case NFA_BACKREF4:
3230 case NFA_BACKREF5:
3231 case NFA_BACKREF6:
3232 case NFA_BACKREF7:
3233 case NFA_BACKREF8:
3234 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003235#ifdef FEAT_SYN_HL
3236 case NFA_ZREF1:
3237 case NFA_ZREF2:
3238 case NFA_ZREF3:
3239 case NFA_ZREF4:
3240 case NFA_ZREF5:
3241 case NFA_ZREF6:
3242 case NFA_ZREF7:
3243 case NFA_ZREF8:
3244 case NFA_ZREF9:
3245#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003246 if (nfa_calc_size == TRUE)
3247 {
3248 nstate += 2;
3249 break;
3250 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003251 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003252 if (s == NULL)
3253 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003254 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003255 if (s1 == NULL)
3256 goto theend;
3257 patch(list1(&s->out), s1);
3258 PUSH(frag(s, list1(&s1->out)));
3259 break;
3260
Bram Moolenaar423532e2013-05-29 21:14:42 +02003261 case NFA_LNUM:
3262 case NFA_LNUM_GT:
3263 case NFA_LNUM_LT:
3264 case NFA_VCOL:
3265 case NFA_VCOL_GT:
3266 case NFA_VCOL_LT:
3267 case NFA_COL:
3268 case NFA_COL_GT:
3269 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003270 case NFA_MARK:
3271 case NFA_MARK_GT:
3272 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003273 {
3274 int n = *++p; /* lnum, col or mark name */
3275
Bram Moolenaar423532e2013-05-29 21:14:42 +02003276 if (nfa_calc_size == TRUE)
3277 {
3278 nstate += 1;
3279 break;
3280 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003281 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003282 if (s == NULL)
3283 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003284 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003285 PUSH(frag(s, list1(&s->out)));
3286 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003287 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003288
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289 case NFA_ZSTART:
3290 case NFA_ZEND:
3291 default:
3292 /* Operands */
3293 if (nfa_calc_size == TRUE)
3294 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003295 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 break;
3297 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003298 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003300 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 PUSH(frag(s, list1(&s->out)));
3302 break;
3303
3304 } /* switch(*p) */
3305
3306 } /* for(p = postfix; *p; ++p) */
3307
3308 if (nfa_calc_size == TRUE)
3309 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003310 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003311 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 }
3313
3314 e = POP();
3315 if (stackp != stack)
3316 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3317
3318 if (istate >= nstate)
3319 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3320
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003321 matchstate = &state_ptr[istate++]; /* the match state */
3322 matchstate->c = NFA_MATCH;
3323 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003324 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003325
3326 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003327 ret = e.start;
3328
3329theend:
3330 vim_free(stack);
3331 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332
3333#undef POP1
3334#undef PUSH1
3335#undef POP2
3336#undef PUSH2
3337#undef POP
3338#undef PUSH
3339}
3340
Bram Moolenaara2947e22013-06-11 22:44:09 +02003341/*
3342 * After building the NFA program, inspect it to add optimization hints.
3343 */
3344 static void
3345nfa_postprocess(prog)
3346 nfa_regprog_T *prog;
3347{
3348 int i;
3349 int c;
3350
3351 for (i = 0; i < prog->nstate; ++i)
3352 {
3353 c = prog->state[i].c;
3354 if (c == NFA_START_INVISIBLE
3355 || c == NFA_START_INVISIBLE_NEG
3356 || c == NFA_START_INVISIBLE_BEFORE
3357 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3358 {
3359 int directly;
3360
3361 /* Do it directly when what follows is possibly the end of the
3362 * match. */
3363 if (match_follows(prog->state[i].out1->out, 0))
3364 directly = TRUE;
3365 else
3366 {
3367 int ch_invisible = failure_chance(prog->state[i].out, 0);
3368 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3369
3370 /* Postpone when the invisible match is expensive or has a
3371 * lower chance of failing. */
3372 if (c == NFA_START_INVISIBLE_BEFORE
3373 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3374 {
3375 /* "before" matches are very expensive when
3376 * unbounded, always prefer what follows then,
3377 * unless what follows will always match.
3378 * Otherwise strongly prefer what follows. */
3379 if (prog->state[i].val <= 0 && ch_follows > 0)
3380 directly = FALSE;
3381 else
3382 directly = ch_follows * 10 < ch_invisible;
3383 }
3384 else
3385 {
3386 /* normal invisible, first do the one with the
3387 * highest failure chance */
3388 directly = ch_follows < ch_invisible;
3389 }
3390 }
3391 if (directly)
3392 /* switch to the _FIRST state */
3393 ++prog->state[i].c;
3394 }
3395 }
3396}
3397
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003398/****************************************************************
3399 * NFA execution code.
3400 ****************************************************************/
3401
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003402typedef struct
3403{
3404 int in_use; /* number of subexpr with useful info */
3405
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003406 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003407 union
3408 {
3409 struct multipos
3410 {
3411 lpos_T start;
3412 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003413 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003414 struct linepos
3415 {
3416 char_u *start;
3417 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003418 } line[NSUBEXP];
3419 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003420} regsub_T;
3421
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003422typedef struct
3423{
3424 regsub_T norm; /* \( .. \) matches */
3425#ifdef FEAT_SYN_HL
3426 regsub_T synt; /* \z( .. \) matches */
3427#endif
3428} regsubs_T;
3429
Bram Moolenaara2d95102013-06-04 14:23:05 +02003430/* nfa_pim_T stores a Postponed Invisible Match. */
3431typedef struct nfa_pim_S nfa_pim_T;
3432struct nfa_pim_S
3433{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003434 int result; /* NFA_PIM_*, see below */
3435 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003436 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003437 union
3438 {
3439 lpos_T pos;
3440 char_u *ptr;
3441 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003442};
3443
3444/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003445#define NFA_PIM_UNUSED 0 /* pim not used */
3446#define NFA_PIM_TODO 1 /* pim not done yet */
3447#define NFA_PIM_MATCH 2 /* pim executed, matches */
3448#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003449
3450
Bram Moolenaar963fee22013-05-26 21:47:28 +02003451/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003452typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003453{
3454 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003455 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003456 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3457 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003458 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003459} nfa_thread_T;
3460
Bram Moolenaar963fee22013-05-26 21:47:28 +02003461/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462typedef struct
3463{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003464 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003465 int n; /* nr of states currently in "t" */
3466 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003467 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003468} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469
Bram Moolenaar5714b802013-05-28 22:03:20 +02003470#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003471static void log_subsexpr __ARGS((regsubs_T *subs));
3472static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003473static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003474
3475 static void
3476log_subsexpr(subs)
3477 regsubs_T *subs;
3478{
3479 log_subexpr(&subs->norm);
3480# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003481 if (nfa_has_zsubexpr)
3482 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003483# endif
3484}
3485
Bram Moolenaar5714b802013-05-28 22:03:20 +02003486 static void
3487log_subexpr(sub)
3488 regsub_T *sub;
3489{
3490 int j;
3491
3492 for (j = 0; j < sub->in_use; j++)
3493 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003494 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003495 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003496 sub->list.multi[j].start.col,
3497 (int)sub->list.multi[j].start.lnum,
3498 sub->list.multi[j].end.col,
3499 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003500 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003501 {
3502 char *s = (char *)sub->list.line[j].start;
3503 char *e = (char *)sub->list.line[j].end;
3504
Bram Moolenaar87953742013-06-05 18:52:40 +02003505 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003506 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003507 s == NULL ? "NULL" : s,
3508 e == NULL ? "NULL" : e);
3509 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003510}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003511
3512 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003513pim_info(pim)
3514 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003515{
3516 static char buf[30];
3517
3518 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3519 buf[0] = NUL;
3520 else
3521 {
3522 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3523 : (int)(pim->end.ptr - reginput));
3524 }
3525 return buf;
3526}
3527
Bram Moolenaar5714b802013-05-28 22:03:20 +02003528#endif
3529
Bram Moolenaar963fee22013-05-26 21:47:28 +02003530/* Used during execution: whether a match has been found. */
3531static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003532
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003533static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003534static void clear_sub __ARGS((regsub_T *sub));
3535static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3536static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003537static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003538static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003539static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
3540static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003541static regsubs_T *addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003542static 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 +02003543
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003544/*
3545 * Copy postponed invisible match info from "from" to "to".
3546 */
3547 static void
3548copy_pim(to, from)
3549 nfa_pim_T *to;
3550 nfa_pim_T *from;
3551{
3552 to->result = from->result;
3553 to->state = from->state;
3554 copy_sub(&to->subs.norm, &from->subs.norm);
3555#ifdef FEAT_SYN_HL
3556 if (nfa_has_zsubexpr)
3557 copy_sub(&to->subs.synt, &from->subs.synt);
3558#endif
3559 to->end = from->end;
3560}
3561
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003562 static void
3563clear_sub(sub)
3564 regsub_T *sub;
3565{
3566 if (REG_MULTI)
3567 /* Use 0xff to set lnum to -1 */
3568 vim_memset(sub->list.multi, 0xff,
3569 sizeof(struct multipos) * nfa_nsubexpr);
3570 else
3571 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3572 sub->in_use = 0;
3573}
3574
3575/*
3576 * Copy the submatches from "from" to "to".
3577 */
3578 static void
3579copy_sub(to, from)
3580 regsub_T *to;
3581 regsub_T *from;
3582{
3583 to->in_use = from->in_use;
3584 if (from->in_use > 0)
3585 {
3586 /* Copy the match start and end positions. */
3587 if (REG_MULTI)
3588 mch_memmove(&to->list.multi[0],
3589 &from->list.multi[0],
3590 sizeof(struct multipos) * from->in_use);
3591 else
3592 mch_memmove(&to->list.line[0],
3593 &from->list.line[0],
3594 sizeof(struct linepos) * from->in_use);
3595 }
3596}
3597
3598/*
3599 * Like copy_sub() but exclude the main match.
3600 */
3601 static void
3602copy_sub_off(to, from)
3603 regsub_T *to;
3604 regsub_T *from;
3605{
3606 if (to->in_use < from->in_use)
3607 to->in_use = from->in_use;
3608 if (from->in_use > 1)
3609 {
3610 /* Copy the match start and end positions. */
3611 if (REG_MULTI)
3612 mch_memmove(&to->list.multi[1],
3613 &from->list.multi[1],
3614 sizeof(struct multipos) * (from->in_use - 1));
3615 else
3616 mch_memmove(&to->list.line[1],
3617 &from->list.line[1],
3618 sizeof(struct linepos) * (from->in_use - 1));
3619 }
3620}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621
Bram Moolenaar428e9872013-05-30 17:05:39 +02003622/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003623 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003624 */
3625 static int
3626sub_equal(sub1, sub2)
3627 regsub_T *sub1;
3628 regsub_T *sub2;
3629{
3630 int i;
3631 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003632 linenr_T s1;
3633 linenr_T s2;
3634 char_u *sp1;
3635 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003636
3637 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3638 if (REG_MULTI)
3639 {
3640 for (i = 0; i < todo; ++i)
3641 {
3642 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003643 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003644 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003645 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003646 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003647 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003648 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003649 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003650 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003651 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02003652 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02003653 != sub2->list.multi[i].start.col)
3654 return FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003655 }
3656 }
3657 else
3658 {
3659 for (i = 0; i < todo; ++i)
3660 {
3661 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003662 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003663 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003664 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003665 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003666 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003667 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003668 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003669 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003670 return FALSE;
3671 }
3672 }
3673
3674 return TRUE;
3675}
3676
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003677#ifdef ENABLE_LOG
3678 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003679report_state(char *action,
3680 regsub_T *sub,
3681 nfa_state_T *state,
3682 int lid,
3683 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003684{
3685 int col;
3686
3687 if (sub->in_use <= 0)
3688 col = -1;
3689 else if (REG_MULTI)
3690 col = sub->list.multi[0].start.col;
3691 else
3692 col = (int)(sub->list.line[0].start - regline);
3693 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003694 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3695 action, abs(state->id), lid, state->c, code, col,
3696 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003697}
3698#endif
3699
Bram Moolenaar43e02982013-06-07 17:31:29 +02003700/*
3701 * Return TRUE if the same state is already in list "l" with the same
3702 * positions as "subs".
3703 */
3704 static int
3705has_state_with_pos(l, state, subs)
3706 nfa_list_T *l; /* runtime state list */
3707 nfa_state_T *state; /* state to update */
3708 regsubs_T *subs; /* pointers to subexpressions */
3709{
3710 nfa_thread_T *thread;
3711 int i;
3712
3713 for (i = 0; i < l->n; ++i)
3714 {
3715 thread = &l->t[i];
3716 if (thread->state->id == state->id
3717 && sub_equal(&thread->subs.norm, &subs->norm)
3718#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003719 && (!nfa_has_zsubexpr
3720 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003721#endif
3722 )
3723 return TRUE;
3724 }
3725 return FALSE;
3726}
3727
3728/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003729 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3730 */
3731 static int
3732match_follows(startstate, depth)
3733 nfa_state_T *startstate;
3734 int depth;
3735{
3736 nfa_state_T *state = startstate;
3737
3738 /* avoid too much recursion */
3739 if (depth > 10)
3740 return FALSE;
3741
3742 for (;;)
3743 {
3744 switch (state->c)
3745 {
3746 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003747 case NFA_MCLOSE:
3748 case NFA_END_INVISIBLE:
3749 case NFA_END_INVISIBLE_NEG:
3750 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003751 return TRUE;
3752
3753 case NFA_SPLIT:
3754 return match_follows(state->out, depth + 1)
3755 || match_follows(state->out1, depth + 1);
3756
3757 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003758 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003759 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003760 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003761 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003762 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003763 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003764 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003765 case NFA_COMPOSING:
3766 /* skip ahead to next state */
3767 state = state->out1->out;
3768 break;
3769
3770 case NFA_ANY:
3771 case NFA_IDENT:
3772 case NFA_SIDENT:
3773 case NFA_KWORD:
3774 case NFA_SKWORD:
3775 case NFA_FNAME:
3776 case NFA_SFNAME:
3777 case NFA_PRINT:
3778 case NFA_SPRINT:
3779 case NFA_WHITE:
3780 case NFA_NWHITE:
3781 case NFA_DIGIT:
3782 case NFA_NDIGIT:
3783 case NFA_HEX:
3784 case NFA_NHEX:
3785 case NFA_OCTAL:
3786 case NFA_NOCTAL:
3787 case NFA_WORD:
3788 case NFA_NWORD:
3789 case NFA_HEAD:
3790 case NFA_NHEAD:
3791 case NFA_ALPHA:
3792 case NFA_NALPHA:
3793 case NFA_LOWER:
3794 case NFA_NLOWER:
3795 case NFA_UPPER:
3796 case NFA_NUPPER:
3797 case NFA_START_COLL:
3798 case NFA_START_NEG_COLL:
3799 case NFA_NEWL:
3800 /* state will advance input */
3801 return FALSE;
3802
3803 default:
3804 if (state->c > 0)
3805 /* state will advance input */
3806 return FALSE;
3807
3808 /* Others: zero-width or possibly zero-width, might still find
3809 * a match at the same position, keep looking. */
3810 break;
3811 }
3812 state = state->out;
3813 }
3814 return FALSE;
3815}
3816
3817
3818/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003819 * Return TRUE if "state" is already in list "l".
3820 */
3821 static int
3822state_in_list(l, state, subs)
3823 nfa_list_T *l; /* runtime state list */
3824 nfa_state_T *state; /* state to update */
3825 regsubs_T *subs; /* pointers to subexpressions */
3826{
3827 if (state->lastlist[nfa_ll_index] == l->id)
3828 {
3829 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3830 return TRUE;
3831 }
3832 return FALSE;
3833}
3834
Bram Moolenaard05bf562013-06-30 23:24:08 +02003835/*
3836 * Add "state" and possibly what follows to state list ".".
3837 * Returns "subs_arg", possibly copied into temp_subs.
3838 */
3839
3840 static regsubs_T *
3841addstate(l, state, subs_arg, pim, off)
3842 nfa_list_T *l; /* runtime state list */
3843 nfa_state_T *state; /* state to update */
3844 regsubs_T *subs_arg; /* pointers to subexpressions */
3845 nfa_pim_T *pim; /* postponed look-behind match */
3846 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003847{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003848 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003849 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003850 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003851 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003852 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003853 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003854 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02003855 regsubs_T *subs = subs_arg;
3856 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003857#ifdef ENABLE_LOG
3858 int did_print = FALSE;
3859#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003860
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003861 switch (state->c)
3862 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003863 case NFA_NCLOSE:
3864 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003865 case NFA_MCLOSE1:
3866 case NFA_MCLOSE2:
3867 case NFA_MCLOSE3:
3868 case NFA_MCLOSE4:
3869 case NFA_MCLOSE5:
3870 case NFA_MCLOSE6:
3871 case NFA_MCLOSE7:
3872 case NFA_MCLOSE8:
3873 case NFA_MCLOSE9:
3874#ifdef FEAT_SYN_HL
3875 case NFA_ZCLOSE:
3876 case NFA_ZCLOSE1:
3877 case NFA_ZCLOSE2:
3878 case NFA_ZCLOSE3:
3879 case NFA_ZCLOSE4:
3880 case NFA_ZCLOSE5:
3881 case NFA_ZCLOSE6:
3882 case NFA_ZCLOSE7:
3883 case NFA_ZCLOSE8:
3884 case NFA_ZCLOSE9:
3885#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003886 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003887 case NFA_SPLIT:
3888 case NFA_NOPEN:
3889 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003890 /* These nodes are not added themselves but their "out" and/or
3891 * "out1" may be added below. */
3892 break;
3893
3894 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003895 case NFA_MOPEN1:
3896 case NFA_MOPEN2:
3897 case NFA_MOPEN3:
3898 case NFA_MOPEN4:
3899 case NFA_MOPEN5:
3900 case NFA_MOPEN6:
3901 case NFA_MOPEN7:
3902 case NFA_MOPEN8:
3903 case NFA_MOPEN9:
3904#ifdef FEAT_SYN_HL
3905 case NFA_ZOPEN:
3906 case NFA_ZOPEN1:
3907 case NFA_ZOPEN2:
3908 case NFA_ZOPEN3:
3909 case NFA_ZOPEN4:
3910 case NFA_ZOPEN5:
3911 case NFA_ZOPEN6:
3912 case NFA_ZOPEN7:
3913 case NFA_ZOPEN8:
3914 case NFA_ZOPEN9:
3915#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003916 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003917 /* These nodes do not need to be added, but we need to bail out
3918 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003919 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003920 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003921 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922 break;
3923
Bram Moolenaar307aa162013-06-02 16:34:21 +02003924 case NFA_BOL:
3925 case NFA_BOF:
3926 /* "^" won't match past end-of-line, don't bother trying.
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003927 * Except when at the end of the line, or when we are going to the
3928 * next line for a look-behind match. */
Bram Moolenaar307aa162013-06-02 16:34:21 +02003929 if (reginput > regline
Bram Moolenaarb62bcd12013-06-13 20:19:40 +02003930 && *reginput != NUL
Bram Moolenaar307aa162013-06-02 16:34:21 +02003931 && (nfa_endp == NULL
3932 || !REG_MULTI
3933 || reglnum == nfa_endp->se_u.pos.lnum))
3934 goto skip_add;
3935 /* FALLTHROUGH */
3936
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003937 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003938 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003939 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003940 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02003941 * unless it is an MOPEN that is used for a backreference or
3942 * when there is a PIM. */
3943 if (!nfa_has_backref && pim == NULL)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003944 {
3945skip_add:
3946#ifdef ENABLE_LOG
3947 nfa_set_code(state->c);
3948 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3949 abs(state->id), l->id, state->c, code);
3950#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02003951 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003952 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003953
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003954 /* Do not add the state again when it exists with the same
3955 * positions. */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003956 if (has_state_with_pos(l, state, subs))
3957 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003958 }
3959
Bram Moolenaara0169122013-06-26 18:16:58 +02003960 /* When there are backreferences or PIMs the number of states may
3961 * be (a lot) bigger than anticipated. */
3962 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003963 {
3964 int newlen = l->len * 3 / 2 + 50;
3965
Bram Moolenaard05bf562013-06-30 23:24:08 +02003966 if (subs != &temp_subs)
3967 {
3968 /* "subs" may point into the current array, need to make a
3969 * copy before it becomes invalid. */
3970 copy_sub(&temp_subs.norm, &subs->norm);
3971#ifdef FEAT_SYN_HL
3972 if (nfa_has_zsubexpr)
3973 copy_sub(&temp_subs.synt, &subs->synt);
3974#endif
3975 subs = &temp_subs;
3976 }
3977
Bram Moolenaar428e9872013-05-30 17:05:39 +02003978 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3979 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003980 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003981
3982 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003983 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003984 thread = &l->t[l->n++];
3985 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003986 if (pim == NULL)
3987 thread->pim.result = NFA_PIM_UNUSED;
3988 else
3989 copy_pim(&thread->pim, pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003990 copy_sub(&thread->subs.norm, &subs->norm);
3991#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003992 if (nfa_has_zsubexpr)
3993 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003994#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003995#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003996 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003997 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003998#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003999 }
4000
4001#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004002 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004003 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004004#endif
4005 switch (state->c)
4006 {
4007 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004008 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004009 break;
4010
4011 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004012 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004013 subs = addstate(l, state->out, subs, pim, off);
4014 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004015 break;
4016
Bram Moolenaar5714b802013-05-28 22:03:20 +02004017 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004018 case NFA_NOPEN:
4019 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004020 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004021 break;
4022
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004023 case NFA_MOPEN:
4024 case NFA_MOPEN1:
4025 case NFA_MOPEN2:
4026 case NFA_MOPEN3:
4027 case NFA_MOPEN4:
4028 case NFA_MOPEN5:
4029 case NFA_MOPEN6:
4030 case NFA_MOPEN7:
4031 case NFA_MOPEN8:
4032 case NFA_MOPEN9:
4033#ifdef FEAT_SYN_HL
4034 case NFA_ZOPEN:
4035 case NFA_ZOPEN1:
4036 case NFA_ZOPEN2:
4037 case NFA_ZOPEN3:
4038 case NFA_ZOPEN4:
4039 case NFA_ZOPEN5:
4040 case NFA_ZOPEN6:
4041 case NFA_ZOPEN7:
4042 case NFA_ZOPEN8:
4043 case NFA_ZOPEN9:
4044#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004045 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004047 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004048 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004049 sub = &subs->norm;
4050 }
4051#ifdef FEAT_SYN_HL
4052 else if (state->c >= NFA_ZOPEN)
4053 {
4054 subidx = state->c - NFA_ZOPEN;
4055 sub = &subs->synt;
4056 }
4057#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004058 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004059 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004060 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004061 sub = &subs->norm;
4062 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004063
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004064 /* Set the position (with "off" added) in the subexpression. Save
4065 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02004066 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067 if (REG_MULTI)
4068 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004069 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004070 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004071 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004072 save_in_use = -1;
4073 }
4074 else
4075 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004076 save_in_use = sub->in_use;
4077 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004078 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004079 sub->list.multi[i].start.lnum = -1;
4080 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004081 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004082 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004083 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004084 if (off == -1)
4085 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004086 sub->list.multi[subidx].start.lnum = reglnum + 1;
4087 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004088 }
4089 else
4090 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004091 sub->list.multi[subidx].start.lnum = reglnum;
4092 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004093 (colnr_T)(reginput - regline + off);
4094 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 }
4096 else
4097 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004098 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004099 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004100 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004101 save_in_use = -1;
4102 }
4103 else
4104 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004105 save_in_use = sub->in_use;
4106 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004107 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004108 sub->list.line[i].start = NULL;
4109 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004110 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004111 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004112 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004113 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004114 }
4115
Bram Moolenaard05bf562013-06-30 23:24:08 +02004116 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004118 if (save_in_use == -1)
4119 {
4120 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004121 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004122 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004123 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004124 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004125 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004126 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004127 break;
4128
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004129 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004130 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004132 /* Do not overwrite the position set by \ze. If no \ze
4133 * encountered end will be set in nfa_regtry(). */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004134 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135 break;
4136 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004137 case NFA_MCLOSE1:
4138 case NFA_MCLOSE2:
4139 case NFA_MCLOSE3:
4140 case NFA_MCLOSE4:
4141 case NFA_MCLOSE5:
4142 case NFA_MCLOSE6:
4143 case NFA_MCLOSE7:
4144 case NFA_MCLOSE8:
4145 case NFA_MCLOSE9:
4146#ifdef FEAT_SYN_HL
4147 case NFA_ZCLOSE:
4148 case NFA_ZCLOSE1:
4149 case NFA_ZCLOSE2:
4150 case NFA_ZCLOSE3:
4151 case NFA_ZCLOSE4:
4152 case NFA_ZCLOSE5:
4153 case NFA_ZCLOSE6:
4154 case NFA_ZCLOSE7:
4155 case NFA_ZCLOSE8:
4156 case NFA_ZCLOSE9:
4157#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004158 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004159 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004160 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004161 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004162 sub = &subs->norm;
4163 }
4164#ifdef FEAT_SYN_HL
4165 else if (state->c >= NFA_ZCLOSE)
4166 {
4167 subidx = state->c - NFA_ZCLOSE;
4168 sub = &subs->synt;
4169 }
4170#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004171 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004172 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004173 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004174 sub = &subs->norm;
4175 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004176
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004177 /* We don't fill in gaps here, there must have been an MOPEN that
4178 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004179 save_in_use = sub->in_use;
4180 if (sub->in_use <= subidx)
4181 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182 if (REG_MULTI)
4183 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004184 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004185 if (off == -1)
4186 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004187 sub->list.multi[subidx].end.lnum = reglnum + 1;
4188 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004189 }
4190 else
4191 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004192 sub->list.multi[subidx].end.lnum = reglnum;
4193 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004194 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004195 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004196 }
4197 else
4198 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004199 save_ptr = sub->list.line[subidx].end;
4200 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004201 }
4202
Bram Moolenaard05bf562013-06-30 23:24:08 +02004203 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004204
4205 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004206 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004207 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004208 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004209 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004210 break;
4211 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004212 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004213}
4214
4215/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004216 * Like addstate(), but the new state(s) are put at position "*ip".
4217 * Used for zero-width matches, next state to use is the added one.
4218 * This makes sure the order of states to be tried does not change, which
4219 * matters for alternatives.
4220 */
4221 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004222addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004223 nfa_list_T *l; /* runtime state list */
4224 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004225 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004226 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004227 int *ip;
4228{
4229 int tlen = l->n;
4230 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004231 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004232
4233 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004234 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004235
Bram Moolenaar4b417062013-05-25 20:19:50 +02004236 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004237 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004238 return;
4239
4240 /* re-order to put the new state at the current position */
4241 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004242 if (count == 0)
4243 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004244 if (count == 1)
4245 {
4246 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004247 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004248 }
4249 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004250 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004251 if (l->n + count - 1 >= l->len)
4252 {
4253 /* not enough space to move the new states, reallocate the list
4254 * and move the states to the right position */
4255 nfa_thread_T *newl;
4256
4257 l->len = l->len * 3 / 2 + 50;
4258 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4259 if (newl == NULL)
4260 return;
4261 mch_memmove(&(newl[0]),
4262 &(l->t[0]),
4263 sizeof(nfa_thread_T) * listidx);
4264 mch_memmove(&(newl[listidx]),
4265 &(l->t[l->n - count]),
4266 sizeof(nfa_thread_T) * count);
4267 mch_memmove(&(newl[listidx + count]),
4268 &(l->t[listidx + 1]),
4269 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4270 vim_free(l->t);
4271 l->t = newl;
4272 }
4273 else
4274 {
4275 /* make space for new states, then move them from the
4276 * end to the current position */
4277 mch_memmove(&(l->t[listidx + count]),
4278 &(l->t[listidx + 1]),
4279 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4280 mch_memmove(&(l->t[listidx]),
4281 &(l->t[l->n - 1]),
4282 sizeof(nfa_thread_T) * count);
4283 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004284 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004285 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004286 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004287}
4288
4289/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004290 * Check character class "class" against current character c.
4291 */
4292 static int
4293check_char_class(class, c)
4294 int class;
4295 int c;
4296{
4297 switch (class)
4298 {
4299 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004300 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004301 return OK;
4302 break;
4303 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004304 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004305 return OK;
4306 break;
4307 case NFA_CLASS_BLANK:
4308 if (c == ' ' || c == '\t')
4309 return OK;
4310 break;
4311 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004312 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004313 return OK;
4314 break;
4315 case NFA_CLASS_DIGIT:
4316 if (VIM_ISDIGIT(c))
4317 return OK;
4318 break;
4319 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004320 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 return OK;
4322 break;
4323 case NFA_CLASS_LOWER:
4324 if (MB_ISLOWER(c))
4325 return OK;
4326 break;
4327 case NFA_CLASS_PRINT:
4328 if (vim_isprintc(c))
4329 return OK;
4330 break;
4331 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004332 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004333 return OK;
4334 break;
4335 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004336 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004337 return OK;
4338 break;
4339 case NFA_CLASS_UPPER:
4340 if (MB_ISUPPER(c))
4341 return OK;
4342 break;
4343 case NFA_CLASS_XDIGIT:
4344 if (vim_isxdigit(c))
4345 return OK;
4346 break;
4347 case NFA_CLASS_TAB:
4348 if (c == '\t')
4349 return OK;
4350 break;
4351 case NFA_CLASS_RETURN:
4352 if (c == '\r')
4353 return OK;
4354 break;
4355 case NFA_CLASS_BACKSPACE:
4356 if (c == '\b')
4357 return OK;
4358 break;
4359 case NFA_CLASS_ESCAPE:
4360 if (c == '\033')
4361 return OK;
4362 break;
4363
4364 default:
4365 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004366 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4367 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004368 }
4369 return FAIL;
4370}
4371
Bram Moolenaar5714b802013-05-28 22:03:20 +02004372/*
4373 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004374 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004375 */
4376 static int
4377match_backref(sub, subidx, bytelen)
4378 regsub_T *sub; /* pointers to subexpressions */
4379 int subidx;
4380 int *bytelen; /* out: length of match in bytes */
4381{
4382 int len;
4383
4384 if (sub->in_use <= subidx)
4385 {
4386retempty:
4387 /* backref was not set, match an empty string */
4388 *bytelen = 0;
4389 return TRUE;
4390 }
4391
4392 if (REG_MULTI)
4393 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004394 if (sub->list.multi[subidx].start.lnum < 0
4395 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004396 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004397 if (sub->list.multi[subidx].start.lnum == reglnum
4398 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004399 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004400 len = sub->list.multi[subidx].end.col
4401 - sub->list.multi[subidx].start.col;
4402 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4403 reginput, &len) == 0)
4404 {
4405 *bytelen = len;
4406 return TRUE;
4407 }
4408 }
4409 else
4410 {
4411 if (match_with_backref(
4412 sub->list.multi[subidx].start.lnum,
4413 sub->list.multi[subidx].start.col,
4414 sub->list.multi[subidx].end.lnum,
4415 sub->list.multi[subidx].end.col,
4416 bytelen) == RA_MATCH)
4417 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004418 }
4419 }
4420 else
4421 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004422 if (sub->list.line[subidx].start == NULL
4423 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004424 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004425 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4426 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004427 {
4428 *bytelen = len;
4429 return TRUE;
4430 }
4431 }
4432 return FALSE;
4433}
4434
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004435#ifdef FEAT_SYN_HL
4436
4437static int match_zref __ARGS((int subidx, int *bytelen));
4438
4439/*
4440 * Check for a match with \z subexpression "subidx".
4441 * Return TRUE if it matches.
4442 */
4443 static int
4444match_zref(subidx, bytelen)
4445 int subidx;
4446 int *bytelen; /* out: length of match in bytes */
4447{
4448 int len;
4449
4450 cleanup_zsubexpr();
4451 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4452 {
4453 /* backref was not set, match an empty string */
4454 *bytelen = 0;
4455 return TRUE;
4456 }
4457
4458 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4459 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4460 {
4461 *bytelen = len;
4462 return TRUE;
4463 }
4464 return FALSE;
4465}
4466#endif
4467
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004468/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004469 * Save list IDs for all NFA states of "prog" into "list".
4470 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004471 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004472 */
4473 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004474nfa_save_listids(prog, list)
4475 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004476 int *list;
4477{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004478 int i;
4479 nfa_state_T *p;
4480
4481 /* Order in the list is reverse, it's a bit faster that way. */
4482 p = &prog->state[0];
4483 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004484 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004485 list[i] = p->lastlist[1];
4486 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004487 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004488 }
4489}
4490
4491/*
4492 * Restore list IDs from "list" to all NFA states.
4493 */
4494 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004495nfa_restore_listids(prog, list)
4496 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004497 int *list;
4498{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004499 int i;
4500 nfa_state_T *p;
4501
4502 p = &prog->state[0];
4503 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004504 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004505 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004506 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004507 }
4508}
4509
Bram Moolenaar423532e2013-05-29 21:14:42 +02004510 static int
4511nfa_re_num_cmp(val, op, pos)
4512 long_u val;
4513 int op;
4514 long_u pos;
4515{
4516 if (op == 1) return pos > val;
4517 if (op == 2) return pos < val;
4518 return val == pos;
4519}
4520
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004521static 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 +02004522static 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 +02004523
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004525 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004526 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4527 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004528 */
4529 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004530recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004531 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004532 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004533 nfa_regprog_T *prog;
4534 regsubs_T *submatch;
4535 regsubs_T *m;
4536 int **listids;
4537{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004538 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004539 int save_reglnum = reglnum;
4540 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004541 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004542 save_se_T *save_nfa_endp = nfa_endp;
4543 save_se_T endpos;
4544 save_se_T *endposp = NULL;
4545 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004546 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004547
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004548 if (pim != NULL)
4549 {
4550 /* start at the position where the postponed match was */
4551 if (REG_MULTI)
4552 reginput = regline + pim->end.pos.col;
4553 else
4554 reginput = pim->end.ptr;
4555 }
4556
Bram Moolenaardecd9542013-06-07 16:31:50 +02004557 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004558 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4559 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4560 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004561 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004562 /* The recursive match must end at the current position. When "pim" is
4563 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004564 endposp = &endpos;
4565 if (REG_MULTI)
4566 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004567 if (pim == NULL)
4568 {
4569 endpos.se_u.pos.col = (int)(reginput - regline);
4570 endpos.se_u.pos.lnum = reglnum;
4571 }
4572 else
4573 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004574 }
4575 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004576 {
4577 if (pim == NULL)
4578 endpos.se_u.ptr = reginput;
4579 else
4580 endpos.se_u.ptr = pim->end.ptr;
4581 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004582
4583 /* Go back the specified number of bytes, or as far as the
4584 * start of the previous line, to try matching "\@<=" or
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004585 * not matching "\@<!". This is very ineffecient, limit the number of
4586 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004587 if (state->val <= 0)
4588 {
4589 if (REG_MULTI)
4590 {
4591 regline = reg_getline(--reglnum);
4592 if (regline == NULL)
4593 /* can't go before the first line */
4594 regline = reg_getline(++reglnum);
4595 }
4596 reginput = regline;
4597 }
4598 else
4599 {
4600 if (REG_MULTI && (int)(reginput - regline) < state->val)
4601 {
4602 /* Not enough bytes in this line, go to end of
4603 * previous line. */
4604 regline = reg_getline(--reglnum);
4605 if (regline == NULL)
4606 {
4607 /* can't go before the first line */
4608 regline = reg_getline(++reglnum);
4609 reginput = regline;
4610 }
4611 else
4612 reginput = regline + STRLEN(regline);
4613 }
4614 if ((int)(reginput - regline) >= state->val)
4615 {
4616 reginput -= state->val;
4617#ifdef FEAT_MBYTE
4618 if (has_mbyte)
4619 reginput -= mb_head_off(regline, reginput);
4620#endif
4621 }
4622 else
4623 reginput = regline;
4624 }
4625 }
4626
Bram Moolenaarf46da702013-06-02 22:37:42 +02004627#ifdef ENABLE_LOG
4628 if (log_fd != stderr)
4629 fclose(log_fd);
4630 log_fd = NULL;
4631#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004632 /* Have to clear the lastlist field of the NFA nodes, so that
4633 * nfa_regmatch() and addstate() can run properly after recursion. */
4634 if (nfa_ll_index == 1)
4635 {
4636 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4637 * values and clear them. */
4638 if (*listids == NULL)
4639 {
4640 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4641 if (*listids == NULL)
4642 {
4643 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4644 return 0;
4645 }
4646 }
4647 nfa_save_listids(prog, *listids);
4648 need_restore = TRUE;
4649 /* any value of nfa_listid will do */
4650 }
4651 else
4652 {
4653 /* First recursive nfa_regmatch() call, switch to the second lastlist
4654 * entry. Make sure nfa_listid is different from a previous recursive
4655 * call, because some states may still have this ID. */
4656 ++nfa_ll_index;
4657 if (nfa_listid <= nfa_alt_listid)
4658 nfa_listid = nfa_alt_listid;
4659 }
4660
4661 /* Call nfa_regmatch() to check if the current concat matches at this
4662 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004663 nfa_endp = endposp;
4664 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004665
4666 if (need_restore)
4667 nfa_restore_listids(prog, *listids);
4668 else
4669 {
4670 --nfa_ll_index;
4671 nfa_alt_listid = nfa_listid;
4672 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004673
4674 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004675 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02004676 if (REG_MULTI)
4677 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004678 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004679 nfa_match = save_nfa_match;
4680 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004681 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004682
4683#ifdef ENABLE_LOG
4684 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4685 if (log_fd != NULL)
4686 {
4687 fprintf(log_fd, "****************************\n");
4688 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4689 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4690 fprintf(log_fd, "****************************\n");
4691 }
4692 else
4693 {
4694 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4695 log_fd = stderr;
4696 }
4697#endif
4698
4699 return result;
4700}
4701
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004702static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004703static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004704
4705/*
4706 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004707 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004708 * NFA_ANY: 1
4709 * specific character: 99
4710 */
4711 static int
4712failure_chance(state, depth)
4713 nfa_state_T *state;
4714 int depth;
4715{
4716 int c = state->c;
4717 int l, r;
4718
4719 /* detect looping */
4720 if (depth > 4)
4721 return 1;
4722
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004723 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004724 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004725 case NFA_SPLIT:
4726 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4727 /* avoid recursive stuff */
4728 return 1;
4729 /* two alternatives, use the lowest failure chance */
4730 l = failure_chance(state->out, depth + 1);
4731 r = failure_chance(state->out1, depth + 1);
4732 return l < r ? l : r;
4733
4734 case NFA_ANY:
4735 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004736 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004737
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004738 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004739 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004740 /* empty match works always */
4741 return 0;
4742
Bram Moolenaar44c71db2013-06-14 22:33:51 +02004743 case NFA_START_INVISIBLE:
4744 case NFA_START_INVISIBLE_FIRST:
4745 case NFA_START_INVISIBLE_NEG:
4746 case NFA_START_INVISIBLE_NEG_FIRST:
4747 case NFA_START_INVISIBLE_BEFORE:
4748 case NFA_START_INVISIBLE_BEFORE_FIRST:
4749 case NFA_START_INVISIBLE_BEFORE_NEG:
4750 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
4751 case NFA_START_PATTERN:
4752 /* recursive regmatch is expensive, use low failure chance */
4753 return 5;
4754
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004755 case NFA_BOL:
4756 case NFA_EOL:
4757 case NFA_BOF:
4758 case NFA_EOF:
4759 case NFA_NEWL:
4760 return 99;
4761
4762 case NFA_BOW:
4763 case NFA_EOW:
4764 return 90;
4765
4766 case NFA_MOPEN:
4767 case NFA_MOPEN1:
4768 case NFA_MOPEN2:
4769 case NFA_MOPEN3:
4770 case NFA_MOPEN4:
4771 case NFA_MOPEN5:
4772 case NFA_MOPEN6:
4773 case NFA_MOPEN7:
4774 case NFA_MOPEN8:
4775 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004776#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004777 case NFA_ZOPEN:
4778 case NFA_ZOPEN1:
4779 case NFA_ZOPEN2:
4780 case NFA_ZOPEN3:
4781 case NFA_ZOPEN4:
4782 case NFA_ZOPEN5:
4783 case NFA_ZOPEN6:
4784 case NFA_ZOPEN7:
4785 case NFA_ZOPEN8:
4786 case NFA_ZOPEN9:
4787 case NFA_ZCLOSE:
4788 case NFA_ZCLOSE1:
4789 case NFA_ZCLOSE2:
4790 case NFA_ZCLOSE3:
4791 case NFA_ZCLOSE4:
4792 case NFA_ZCLOSE5:
4793 case NFA_ZCLOSE6:
4794 case NFA_ZCLOSE7:
4795 case NFA_ZCLOSE8:
4796 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004797#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004798 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004799 case NFA_MCLOSE1:
4800 case NFA_MCLOSE2:
4801 case NFA_MCLOSE3:
4802 case NFA_MCLOSE4:
4803 case NFA_MCLOSE5:
4804 case NFA_MCLOSE6:
4805 case NFA_MCLOSE7:
4806 case NFA_MCLOSE8:
4807 case NFA_MCLOSE9:
4808 case NFA_NCLOSE:
4809 return failure_chance(state->out, depth + 1);
4810
4811 case NFA_BACKREF1:
4812 case NFA_BACKREF2:
4813 case NFA_BACKREF3:
4814 case NFA_BACKREF4:
4815 case NFA_BACKREF5:
4816 case NFA_BACKREF6:
4817 case NFA_BACKREF7:
4818 case NFA_BACKREF8:
4819 case NFA_BACKREF9:
4820#ifdef FEAT_SYN_HL
4821 case NFA_ZREF1:
4822 case NFA_ZREF2:
4823 case NFA_ZREF3:
4824 case NFA_ZREF4:
4825 case NFA_ZREF5:
4826 case NFA_ZREF6:
4827 case NFA_ZREF7:
4828 case NFA_ZREF8:
4829 case NFA_ZREF9:
4830#endif
4831 /* backreferences don't match in many places */
4832 return 94;
4833
4834 case NFA_LNUM_GT:
4835 case NFA_LNUM_LT:
4836 case NFA_COL_GT:
4837 case NFA_COL_LT:
4838 case NFA_VCOL_GT:
4839 case NFA_VCOL_LT:
4840 case NFA_MARK_GT:
4841 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004842 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004843 /* before/after positions don't match very often */
4844 return 85;
4845
4846 case NFA_LNUM:
4847 return 90;
4848
4849 case NFA_CURSOR:
4850 case NFA_COL:
4851 case NFA_VCOL:
4852 case NFA_MARK:
4853 /* specific positions rarely match */
4854 return 98;
4855
4856 case NFA_COMPOSING:
4857 return 95;
4858
4859 default:
4860 if (c > 0)
4861 /* character match fails often */
4862 return 95;
4863 }
4864
4865 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004866 return 50;
4867}
4868
Bram Moolenaarf46da702013-06-02 22:37:42 +02004869/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004870 * Skip until the char "c" we know a match must start with.
4871 */
4872 static int
4873skip_to_start(c, colp)
4874 int c;
4875 colnr_T *colp;
4876{
4877 char_u *s;
4878
4879 /* Used often, do some work to avoid call overhead. */
4880 if (!ireg_ic
4881#ifdef FEAT_MBYTE
4882 && !has_mbyte
4883#endif
4884 )
4885 s = vim_strbyte(regline + *colp, c);
4886 else
4887 s = cstrchr(regline + *colp, c);
4888 if (s == NULL)
4889 return FAIL;
4890 *colp = (int)(s - regline);
4891 return OK;
4892}
4893
4894/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004895 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004896 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004897 * Returns zero for no match, 1 for a match.
4898 */
4899 static long
4900find_match_text(startcol, regstart, match_text)
4901 colnr_T startcol;
4902 int regstart;
4903 char_u *match_text;
4904{
4905 colnr_T col = startcol;
4906 int c1, c2;
4907 int len1, len2;
4908 int match;
4909
4910 for (;;)
4911 {
4912 match = TRUE;
4913 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4914 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4915 {
4916 c1 = PTR2CHAR(match_text + len1);
4917 c2 = PTR2CHAR(regline + col + len2);
4918 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4919 {
4920 match = FALSE;
4921 break;
4922 }
4923 len2 += MB_CHAR2LEN(c2);
4924 }
4925 if (match
4926#ifdef FEAT_MBYTE
4927 /* check that no composing char follows */
4928 && !(enc_utf8
4929 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4930#endif
4931 )
4932 {
4933 cleanup_subexpr();
4934 if (REG_MULTI)
4935 {
4936 reg_startpos[0].lnum = reglnum;
4937 reg_startpos[0].col = col;
4938 reg_endpos[0].lnum = reglnum;
4939 reg_endpos[0].col = col + len2;
4940 }
4941 else
4942 {
4943 reg_startp[0] = regline + col;
4944 reg_endp[0] = regline + col + len2;
4945 }
4946 return 1L;
4947 }
4948
4949 /* Try finding regstart after the current match. */
4950 col += MB_CHAR2LEN(regstart); /* skip regstart */
4951 if (skip_to_start(regstart, &col) == FAIL)
4952 break;
4953 }
4954 return 0L;
4955}
4956
4957/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004958 * Main matching routine.
4959 *
4960 * Run NFA to determine whether it matches reginput.
4961 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004962 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004963 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004964 * Return TRUE if there is a match, FALSE otherwise.
4965 * Note: Caller must ensure that: start != NULL.
4966 */
4967 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004968nfa_regmatch(prog, start, submatch, m)
4969 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004970 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004971 regsubs_T *submatch;
4972 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004973{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004974 int result;
4975 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004976 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004977 int go_to_nextline = FALSE;
4978 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004979 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004980 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004981 nfa_list_T *thislist;
4982 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004983 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004984 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004985 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004986 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02004987 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004988 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004989#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004990 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004991
4992 if (debug == NULL)
4993 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004994 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004995 return FALSE;
4996 }
4997#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004998 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004999
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005000 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005001 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005002 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005003 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005004 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005005 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005006 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005007 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005008
5009#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005010 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005011 if (log_fd != NULL)
5012 {
5013 fprintf(log_fd, "**********************************\n");
5014 nfa_set_code(start->c);
5015 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5016 abs(start->id), code);
5017 fprintf(log_fd, "**********************************\n");
5018 }
5019 else
5020 {
5021 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5022 log_fd = stderr;
5023 }
5024#endif
5025
5026 thislist = &list[0];
5027 thislist->n = 0;
5028 nextlist = &list[1];
5029 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005031 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005032#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005033 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005034
5035 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5036 * it's the first MOPEN. */
5037 if (toplevel)
5038 {
5039 if (REG_MULTI)
5040 {
5041 m->norm.list.multi[0].start.lnum = reglnum;
5042 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5043 }
5044 else
5045 m->norm.list.line[0].start = reginput;
5046 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005047 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005048 }
5049 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005050 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005052#define ADD_STATE_IF_MATCH(state) \
5053 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005054 add_state = state->out; \
5055 add_off = clen; \
5056 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005057
5058 /*
5059 * Run for each character.
5060 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005061 for (;;)
5062 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005063 int curc;
5064 int clen;
5065
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005066#ifdef FEAT_MBYTE
5067 if (has_mbyte)
5068 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005069 curc = (*mb_ptr2char)(reginput);
5070 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005071 }
5072 else
5073#endif
5074 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005075 curc = *reginput;
5076 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005077 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005078 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005079 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005080 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005081 go_to_nextline = FALSE;
5082 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005083
5084 /* swap lists */
5085 thislist = &list[flag];
5086 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005087 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005088 ++nfa_listid;
5089 thislist->id = nfa_listid;
5090 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005091
5092#ifdef ENABLE_LOG
5093 fprintf(log_fd, "------------------------------------------\n");
5094 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005095 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005096 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005097 {
5098 int i;
5099
5100 for (i = 0; i < thislist->n; i++)
5101 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5102 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005103 fprintf(log_fd, "\n");
5104#endif
5105
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005106#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005107 fprintf(debug, "\n-------------------\n");
5108#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005109 /*
5110 * If the state lists are empty we can stop.
5111 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005112 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005113 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005114
5115 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005116 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005117 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005118 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005119
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005120#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005121 nfa_set_code(t->state->c);
5122 fprintf(debug, "%s, ", code);
5123#endif
5124#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005125 {
5126 int col;
5127
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005128 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005129 col = -1;
5130 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005131 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005132 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005133 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005134 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005135 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5136 abs(t->state->id), (int)t->state->c, code, col,
5137 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005138 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005139#endif
5140
5141 /*
5142 * Handle the possible codes of the current state.
5143 * The most important is NFA_MATCH.
5144 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005145 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005146 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005147 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005148 switch (t->state->c)
5149 {
5150 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005151 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005152 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005153 copy_sub(&submatch->norm, &t->subs.norm);
5154#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005155 if (nfa_has_zsubexpr)
5156 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005157#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005158#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005159 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005160#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005161 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005162 * states at this position. When the list of states is going
5163 * to be empty quit without advancing, so that "reginput" is
5164 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005165 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005166 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005167 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005168 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005169
5170 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005171 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005172 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005173 /*
5174 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005175 * NFA_START_INVISIBLE_BEFORE node.
5176 * They surround a zero-width group, used with "\@=", "\&",
5177 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005178 * If we got here, it means that the current "invisible" group
5179 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005180 * nfa_regmatch(). For a look-behind match only when it ends
5181 * in the position in "nfa_endp".
5182 * Submatches are stored in *m, and used in the parent call.
5183 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005184#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005185 if (nfa_endp != NULL)
5186 {
5187 if (REG_MULTI)
5188 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5189 (int)reglnum,
5190 (int)nfa_endp->se_u.pos.lnum,
5191 (int)(reginput - regline),
5192 nfa_endp->se_u.pos.col);
5193 else
5194 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5195 (int)(reginput - regline),
5196 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005197 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005198#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005199 /* If "nfa_endp" is set it's only a match if it ends at
5200 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005201 if (nfa_endp != NULL && (REG_MULTI
5202 ? (reglnum != nfa_endp->se_u.pos.lnum
5203 || (int)(reginput - regline)
5204 != nfa_endp->se_u.pos.col)
5205 : reginput != nfa_endp->se_u.ptr))
5206 break;
5207
5208 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005209 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005210 {
5211 copy_sub(&m->norm, &t->subs.norm);
5212#ifdef FEAT_SYN_HL
5213 if (nfa_has_zsubexpr)
5214 copy_sub(&m->synt, &t->subs.synt);
5215#endif
5216 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005217#ifdef ENABLE_LOG
5218 fprintf(log_fd, "Match found:\n");
5219 log_subsexpr(m);
5220#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005221 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005222 break;
5223
5224 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005225 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005226 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005227 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005228 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005229 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005230 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005231 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005232 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005233#ifdef ENABLE_LOG
5234 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5235 failure_chance(t->state->out, 0),
5236 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005237#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005238 /* Do it directly if there already is a PIM or when
5239 * nfa_postprocess() detected it will work better. */
5240 if (t->pim.result != NFA_PIM_UNUSED
5241 || t->state->c == NFA_START_INVISIBLE_FIRST
5242 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5243 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5244 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005245 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005246 int in_use = m->norm.in_use;
5247
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005248 /* Copy submatch info for the recursive call, so that
5249 * \1 can be matched. */
5250 copy_sub_off(&m->norm, &t->subs.norm);
5251
Bram Moolenaara2d95102013-06-04 14:23:05 +02005252 /*
5253 * First try matching the invisible match, then what
5254 * follows.
5255 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005256 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005257 submatch, m, &listids);
5258
Bram Moolenaardecd9542013-06-07 16:31:50 +02005259 /* for \@! and \@<! it is a match when the result is
5260 * FALSE */
5261 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005262 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5263 || t->state->c
5264 == NFA_START_INVISIBLE_BEFORE_NEG
5265 || t->state->c
5266 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005267 {
5268 /* Copy submatch info from the recursive call */
5269 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005270#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005271 if (nfa_has_zsubexpr)
5272 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005273#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005274
Bram Moolenaara2d95102013-06-04 14:23:05 +02005275 /* t->state->out1 is the corresponding
5276 * END_INVISIBLE node; Add its out to the current
5277 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005278 add_here = TRUE;
5279 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005280 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005281 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005282 }
5283 else
5284 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005285 nfa_pim_T pim;
5286
Bram Moolenaara2d95102013-06-04 14:23:05 +02005287 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005288 * First try matching what follows. Only if a match
5289 * is found verify the invisible match matches. Add a
5290 * nfa_pim_T to the following states, it contains info
5291 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005292 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005293 pim.state = t->state;
5294 pim.result = NFA_PIM_TODO;
5295 pim.subs.norm.in_use = 0;
5296#ifdef FEAT_SYN_HL
5297 pim.subs.synt.in_use = 0;
5298#endif
5299 if (REG_MULTI)
5300 {
5301 pim.end.pos.col = (int)(reginput - regline);
5302 pim.end.pos.lnum = reglnum;
5303 }
5304 else
5305 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005306
5307 /* t->state->out1 is the corresponding END_INVISIBLE
5308 * node; Add its out to the current list (zero-width
5309 * match). */
5310 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005311 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005312 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005313 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005314 break;
5315
Bram Moolenaar87953742013-06-05 18:52:40 +02005316 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005317 {
5318 nfa_state_T *skip = NULL;
5319#ifdef ENABLE_LOG
5320 int skip_lid = 0;
5321#endif
5322
5323 /* There is no point in trying to match the pattern if the
5324 * output state is not going to be added to the list. */
5325 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5326 {
5327 skip = t->state->out1->out;
5328#ifdef ENABLE_LOG
5329 skip_lid = nextlist->id;
5330#endif
5331 }
5332 else if (state_in_list(nextlist,
5333 t->state->out1->out->out, &t->subs))
5334 {
5335 skip = t->state->out1->out->out;
5336#ifdef ENABLE_LOG
5337 skip_lid = nextlist->id;
5338#endif
5339 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005340 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005341 t->state->out1->out->out, &t->subs))
5342 {
5343 skip = t->state->out1->out->out;
5344#ifdef ENABLE_LOG
5345 skip_lid = thislist->id;
5346#endif
5347 }
5348 if (skip != NULL)
5349 {
5350#ifdef ENABLE_LOG
5351 nfa_set_code(skip->c);
5352 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5353 abs(skip->id), skip_lid, skip->c, code);
5354#endif
5355 break;
5356 }
5357
Bram Moolenaar87953742013-06-05 18:52:40 +02005358 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005359 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005360 submatch, m, &listids);
5361 if (result)
5362 {
5363 int bytelen;
5364
5365#ifdef ENABLE_LOG
5366 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5367 log_subsexpr(m);
5368#endif
5369 /* Copy submatch info from the recursive call */
5370 copy_sub_off(&t->subs.norm, &m->norm);
5371#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005372 if (nfa_has_zsubexpr)
5373 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005374#endif
5375 /* Now we need to skip over the matched text and then
5376 * continue with what follows. */
5377 if (REG_MULTI)
5378 /* TODO: multi-line match */
5379 bytelen = m->norm.list.multi[0].end.col
5380 - (int)(reginput - regline);
5381 else
5382 bytelen = (int)(m->norm.list.line[0].end - reginput);
5383
5384#ifdef ENABLE_LOG
5385 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5386#endif
5387 if (bytelen == 0)
5388 {
5389 /* empty match, output of corresponding
5390 * NFA_END_PATTERN/NFA_SKIP to be used at current
5391 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005392 add_here = TRUE;
5393 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005394 }
5395 else if (bytelen <= clen)
5396 {
5397 /* match current character, output of corresponding
5398 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005399 add_state = t->state->out1->out->out;
5400 add_off = clen;
5401 }
5402 else
5403 {
5404 /* skip over the matched characters, set character
5405 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005406 add_state = t->state->out1->out;
5407 add_off = bytelen;
5408 add_count = bytelen - clen;
5409 }
5410 }
5411 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005412 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005413
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005414 case NFA_BOL:
5415 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005416 {
5417 add_here = TRUE;
5418 add_state = t->state->out;
5419 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005420 break;
5421
5422 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005423 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005424 {
5425 add_here = TRUE;
5426 add_state = t->state->out;
5427 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005428 break;
5429
5430 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005431 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005432
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005433 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005434 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005435#ifdef FEAT_MBYTE
5436 else if (has_mbyte)
5437 {
5438 int this_class;
5439
5440 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005441 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005442 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005443 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005445 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005446 }
5447#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005448 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005449 || (reginput > regline
5450 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005451 result = FALSE;
5452 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005453 {
5454 add_here = TRUE;
5455 add_state = t->state->out;
5456 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005457 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005458
5459 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005460 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005461 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005462 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005463#ifdef FEAT_MBYTE
5464 else if (has_mbyte)
5465 {
5466 int this_class, prev_class;
5467
5468 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005469 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005470 prev_class = reg_prev_class();
5471 if (this_class == prev_class
5472 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005473 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005474 }
5475#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005476 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005477 || (reginput[0] != NUL
5478 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005479 result = FALSE;
5480 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005481 {
5482 add_here = TRUE;
5483 add_state = t->state->out;
5484 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005485 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005486
Bram Moolenaar4b780632013-05-31 22:14:52 +02005487 case NFA_BOF:
5488 if (reglnum == 0 && reginput == regline
5489 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005490 {
5491 add_here = TRUE;
5492 add_state = t->state->out;
5493 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005494 break;
5495
5496 case NFA_EOF:
5497 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005498 {
5499 add_here = TRUE;
5500 add_state = t->state->out;
5501 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005502 break;
5503
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005504#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005505 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005506 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005507 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005508 int len = 0;
5509 nfa_state_T *end;
5510 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005511 int cchars[MAX_MCO];
5512 int ccount = 0;
5513 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005514
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005516 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005517 if (utf_iscomposing(sta->c))
5518 {
5519 /* Only match composing character(s), ignore base
5520 * character. Used for ".{composing}" and "{composing}"
5521 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005522 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005523 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005524 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005525 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005526 /* If \Z was present, then ignore composing characters.
5527 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005528 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005529 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005530 else
5531 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005532 while (sta->c != NFA_END_COMPOSING)
5533 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005534 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005535
5536 /* Check base character matches first, unless ignored. */
5537 else if (len > 0 || mc == sta->c)
5538 {
5539 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005540 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005541 len += mb_char2len(mc);
5542 sta = sta->out;
5543 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005544
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005545 /* We don't care about the order of composing characters.
5546 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005547 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005548 {
5549 mc = mb_ptr2char(reginput + len);
5550 cchars[ccount++] = mc;
5551 len += mb_char2len(mc);
5552 if (ccount == MAX_MCO)
5553 break;
5554 }
5555
5556 /* Check that each composing char in the pattern matches a
5557 * composing char in the text. We do not check if all
5558 * composing chars are matched. */
5559 result = OK;
5560 while (sta->c != NFA_END_COMPOSING)
5561 {
5562 for (j = 0; j < ccount; ++j)
5563 if (cchars[j] == sta->c)
5564 break;
5565 if (j == ccount)
5566 {
5567 result = FAIL;
5568 break;
5569 }
5570 sta = sta->out;
5571 }
5572 }
5573 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005574 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005575
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005576 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005577 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005578 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005579 }
5580#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005581
5582 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005583 if (curc == NUL && !reg_line_lbr && REG_MULTI
5584 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005585 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005586 go_to_nextline = TRUE;
5587 /* Pass -1 for the offset, which means taking the position
5588 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005589 add_state = t->state->out;
5590 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005592 else if (curc == '\n' && reg_line_lbr)
5593 {
5594 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005595 add_state = t->state->out;
5596 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005597 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598 break;
5599
Bram Moolenaar417bad22013-06-07 14:08:30 +02005600 case NFA_START_COLL:
5601 case NFA_START_NEG_COLL:
5602 {
5603 /* What follows is a list of characters, until NFA_END_COLL.
5604 * One of them must match or none of them must match. */
5605 nfa_state_T *state;
5606 int result_if_matched;
5607 int c1, c2;
5608
5609 /* Never match EOL. If it's part of the collection it is added
5610 * as a separate state with an OR. */
5611 if (curc == NUL)
5612 break;
5613
5614 state = t->state->out;
5615 result_if_matched = (t->state->c == NFA_START_COLL);
5616 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005617 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005618 if (state->c == NFA_END_COLL)
5619 {
5620 result = !result_if_matched;
5621 break;
5622 }
5623 if (state->c == NFA_RANGE_MIN)
5624 {
5625 c1 = state->val;
5626 state = state->out; /* advance to NFA_RANGE_MAX */
5627 c2 = state->val;
5628#ifdef ENABLE_LOG
5629 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5630 curc, c1, c2);
5631#endif
5632 if (curc >= c1 && curc <= c2)
5633 {
5634 result = result_if_matched;
5635 break;
5636 }
5637 if (ireg_ic)
5638 {
5639 int curc_low = MB_TOLOWER(curc);
5640 int done = FALSE;
5641
5642 for ( ; c1 <= c2; ++c1)
5643 if (MB_TOLOWER(c1) == curc_low)
5644 {
5645 result = result_if_matched;
5646 done = TRUE;
5647 break;
5648 }
5649 if (done)
5650 break;
5651 }
5652 }
5653 else if (state->c < 0 ? check_char_class(state->c, curc)
5654 : (curc == state->c
5655 || (ireg_ic && MB_TOLOWER(curc)
5656 == MB_TOLOWER(state->c))))
5657 {
5658 result = result_if_matched;
5659 break;
5660 }
5661 state = state->out;
5662 }
5663 if (result)
5664 {
5665 /* next state is in out of the NFA_END_COLL, out1 of
5666 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005667 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005668 add_off = clen;
5669 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005670 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005671 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005672
5673 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005674 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005675 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005676 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005677 add_state = t->state->out;
5678 add_off = clen;
5679 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005680 break;
5681
5682 /*
5683 * Character classes like \a for alpha, \d for digit etc.
5684 */
5685 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005686 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005687 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005688 break;
5689
5690 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005691 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005692 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005693 break;
5694
5695 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005696 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005697 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698 break;
5699
5700 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005701 result = !VIM_ISDIGIT(curc)
5702 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005703 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005704 break;
5705
5706 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005707 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005708 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005709 break;
5710
5711 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005712 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005713 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005714 break;
5715
5716 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005717 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005718 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005719 break;
5720
5721 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005722 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005723 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005724 break;
5725
5726 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005727 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005728 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005729 break;
5730
5731 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005732 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005733 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005734 break;
5735
5736 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005737 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005738 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005739 break;
5740
5741 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005742 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005743 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005744 break;
5745
5746 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005747 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005748 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005749 break;
5750
5751 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005752 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005753 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005754 break;
5755
5756 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005757 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005758 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005759 break;
5760
5761 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005762 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005763 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005764 break;
5765
5766 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005767 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005768 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005769 break;
5770
5771 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005772 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005773 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005774 break;
5775
5776 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005777 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005778 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005779 break;
5780
5781 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005782 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005783 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005784 break;
5785
5786 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005787 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005788 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005789 break;
5790
5791 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005792 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005793 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005794 break;
5795
5796 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005797 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005798 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005799 break;
5800
5801 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005802 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005803 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005804 break;
5805
5806 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005807 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005808 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005809 break;
5810
5811 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005812 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005813 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005814 break;
5815
Bram Moolenaar5714b802013-05-28 22:03:20 +02005816 case NFA_BACKREF1:
5817 case NFA_BACKREF2:
5818 case NFA_BACKREF3:
5819 case NFA_BACKREF4:
5820 case NFA_BACKREF5:
5821 case NFA_BACKREF6:
5822 case NFA_BACKREF7:
5823 case NFA_BACKREF8:
5824 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005825#ifdef FEAT_SYN_HL
5826 case NFA_ZREF1:
5827 case NFA_ZREF2:
5828 case NFA_ZREF3:
5829 case NFA_ZREF4:
5830 case NFA_ZREF5:
5831 case NFA_ZREF6:
5832 case NFA_ZREF7:
5833 case NFA_ZREF8:
5834 case NFA_ZREF9:
5835#endif
5836 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005837 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005838 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005839 int bytelen;
5840
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005841 if (t->state->c <= NFA_BACKREF9)
5842 {
5843 subidx = t->state->c - NFA_BACKREF1 + 1;
5844 result = match_backref(&t->subs.norm, subidx, &bytelen);
5845 }
5846#ifdef FEAT_SYN_HL
5847 else
5848 {
5849 subidx = t->state->c - NFA_ZREF1 + 1;
5850 result = match_zref(subidx, &bytelen);
5851 }
5852#endif
5853
Bram Moolenaar5714b802013-05-28 22:03:20 +02005854 if (result)
5855 {
5856 if (bytelen == 0)
5857 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005858 /* empty match always works, output of NFA_SKIP to be
5859 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005860 add_here = TRUE;
5861 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005862 }
5863 else if (bytelen <= clen)
5864 {
5865 /* match current character, jump ahead to out of
5866 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005867 add_state = t->state->out->out;
5868 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005869 }
5870 else
5871 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005872 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005873 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005874 add_state = t->state->out;
5875 add_off = bytelen;
5876 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005877 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005878 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005879 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005880 }
5881 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005882 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005883 if (t->count - clen <= 0)
5884 {
5885 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005886 add_state = t->state->out;
5887 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005888 }
5889 else
5890 {
5891 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005892 add_state = t->state;
5893 add_off = 0;
5894 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005895 }
5896 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005897
Bram Moolenaar423532e2013-05-29 21:14:42 +02005898 case NFA_LNUM:
5899 case NFA_LNUM_GT:
5900 case NFA_LNUM_LT:
5901 result = (REG_MULTI &&
5902 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5903 (long_u)(reglnum + reg_firstlnum)));
5904 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005905 {
5906 add_here = TRUE;
5907 add_state = t->state->out;
5908 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005909 break;
5910
5911 case NFA_COL:
5912 case NFA_COL_GT:
5913 case NFA_COL_LT:
5914 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5915 (long_u)(reginput - regline) + 1);
5916 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005917 {
5918 add_here = TRUE;
5919 add_state = t->state->out;
5920 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005921 break;
5922
5923 case NFA_VCOL:
5924 case NFA_VCOL_GT:
5925 case NFA_VCOL_LT:
5926 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5927 (long_u)win_linetabsize(
5928 reg_win == NULL ? curwin : reg_win,
5929 regline, (colnr_T)(reginput - regline)) + 1);
5930 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005931 {
5932 add_here = TRUE;
5933 add_state = t->state->out;
5934 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005935 break;
5936
Bram Moolenaar044aa292013-06-04 21:27:38 +02005937 case NFA_MARK:
5938 case NFA_MARK_GT:
5939 case NFA_MARK_LT:
5940 {
5941 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5942
5943 /* Compare the mark position to the match position. */
5944 result = (pos != NULL /* mark doesn't exist */
5945 && pos->lnum > 0 /* mark isn't set in reg_buf */
5946 && (pos->lnum == reglnum + reg_firstlnum
5947 ? (pos->col == (colnr_T)(reginput - regline)
5948 ? t->state->c == NFA_MARK
5949 : (pos->col < (colnr_T)(reginput - regline)
5950 ? t->state->c == NFA_MARK_GT
5951 : t->state->c == NFA_MARK_LT))
5952 : (pos->lnum < reglnum + reg_firstlnum
5953 ? t->state->c == NFA_MARK_GT
5954 : t->state->c == NFA_MARK_LT)));
5955 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005956 {
5957 add_here = TRUE;
5958 add_state = t->state->out;
5959 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005960 break;
5961 }
5962
Bram Moolenaar423532e2013-05-29 21:14:42 +02005963 case NFA_CURSOR:
5964 result = (reg_win != NULL
5965 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5966 && ((colnr_T)(reginput - regline)
5967 == reg_win->w_cursor.col));
5968 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005969 {
5970 add_here = TRUE;
5971 add_state = t->state->out;
5972 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005973 break;
5974
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005975 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005976#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005977 result = reg_match_visual();
5978 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005979 {
5980 add_here = TRUE;
5981 add_state = t->state->out;
5982 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005983#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005984 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005985
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005986 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005987 {
5988 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005989
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005990 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005991 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005992 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005993 result = (c == curc);
5994
5995 if (!result && ireg_ic)
5996 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005997#ifdef FEAT_MBYTE
5998 /* If there is a composing character which is not being
5999 * ignored there can be no match. Match with composing
6000 * character uses NFA_COMPOSING above. */
6001 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006002 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006003 result = FALSE;
6004#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006005 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006006 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006007 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006008
6009 } /* switch (t->state->c) */
6010
6011 if (add_state != NULL)
6012 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006013 nfa_pim_T *pim;
6014
6015 if (t->pim.result == NFA_PIM_UNUSED)
6016 pim = NULL;
6017 else
6018 pim = &t->pim;
6019
6020 /* Handle the postponed invisible match if the match might end
6021 * without advancing and before the end of the line. */
6022 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006023 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006024 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006025 {
6026#ifdef ENABLE_LOG
6027 fprintf(log_fd, "\n");
6028 fprintf(log_fd, "==================================\n");
6029 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6030 fprintf(log_fd, "\n");
6031#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006032 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006033 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006034 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006035 /* for \@! and \@<! it is a match when the result is
6036 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006037 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006038 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6039 || pim->state->c
6040 == NFA_START_INVISIBLE_BEFORE_NEG
6041 || pim->state->c
6042 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006043 {
6044 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006045 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006046#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006047 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006048 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006049#endif
6050 }
6051 }
6052 else
6053 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006054 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006055#ifdef ENABLE_LOG
6056 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006057 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006058 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6059 fprintf(log_fd, "\n");
6060#endif
6061 }
6062
Bram Moolenaardecd9542013-06-07 16:31:50 +02006063 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006064 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006065 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6066 || pim->state->c
6067 == NFA_START_INVISIBLE_BEFORE_NEG
6068 || pim->state->c
6069 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006070 {
6071 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006072 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006073#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006074 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006075 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006076#endif
6077 }
6078 else
6079 /* look-behind match failed, don't add the state */
6080 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006081
6082 /* Postponed invisible match was handled, don't add it to
6083 * following states. */
6084 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006085 }
6086
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006087 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006088 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006089 else
6090 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006091 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006092 if (add_count > 0)
6093 nextlist->t[nextlist->n - 1].count = add_count;
6094 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006095 }
6096
6097 } /* for (thislist = thislist; thislist->state; thislist++) */
6098
Bram Moolenaare23febd2013-05-26 18:40:14 +02006099 /* Look for the start of a match in the current position by adding the
6100 * start state to the list of states.
6101 * The first found match is the leftmost one, thus the order of states
6102 * matters!
6103 * Do not add the start state in recursive calls of nfa_regmatch(),
6104 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006105 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006106 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006107 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006108 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006109 && reglnum == 0
6110 && clen != 0
6111 && (ireg_maxcol == 0
6112 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006113 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006114 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006115 ? (reglnum < nfa_endp->se_u.pos.lnum
6116 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006117 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006118 < nfa_endp->se_u.pos.col))
6119 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006120 {
6121#ifdef ENABLE_LOG
6122 fprintf(log_fd, "(---) STARTSTATE\n");
6123#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006124 /* Inline optimized code for addstate() if we know the state is
6125 * the first MOPEN. */
6126 if (toplevel)
6127 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006128 int add = TRUE;
6129 int c;
6130
6131 if (prog->regstart != NUL && clen != 0)
6132 {
6133 if (nextlist->n == 0)
6134 {
6135 colnr_T col = (colnr_T)(reginput - regline) + clen;
6136
6137 /* Nextlist is empty, we can skip ahead to the
6138 * character that must appear at the start. */
6139 if (skip_to_start(prog->regstart, &col) == FAIL)
6140 break;
6141#ifdef ENABLE_LOG
6142 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6143 col - ((colnr_T)(reginput - regline) + clen));
6144#endif
6145 reginput = regline + col - clen;
6146 }
6147 else
6148 {
6149 /* Checking if the required start character matches is
6150 * cheaper than adding a state that won't match. */
6151 c = PTR2CHAR(reginput + clen);
6152 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6153 != MB_TOLOWER(prog->regstart)))
6154 {
6155#ifdef ENABLE_LOG
6156 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6157#endif
6158 add = FALSE;
6159 }
6160 }
6161 }
6162
6163 if (add)
6164 {
6165 if (REG_MULTI)
6166 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006167 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006168 else
6169 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006170 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006171 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006172 }
6173 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006174 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006175 }
6176
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006177#ifdef ENABLE_LOG
6178 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006179 {
6180 int i;
6181
6182 for (i = 0; i < thislist->n; i++)
6183 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6184 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006185 fprintf(log_fd, "\n");
6186#endif
6187
6188nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006189 /* Advance to the next character, or advance to the next line, or
6190 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006191 if (clen != 0)
6192 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006193 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6194 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006195 reg_nextline();
6196 else
6197 break;
6198 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006199
6200#ifdef ENABLE_LOG
6201 if (log_fd != stderr)
6202 fclose(log_fd);
6203 log_fd = NULL;
6204#endif
6205
6206theend:
6207 /* Free memory */
6208 vim_free(list[0].t);
6209 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006210 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006211#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006212#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006213 fclose(debug);
6214#endif
6215
Bram Moolenaar963fee22013-05-26 21:47:28 +02006216 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006217}
6218
6219/*
6220 * Try match of "prog" with at regline["col"].
6221 * Returns 0 for failure, number of lines contained in the match otherwise.
6222 */
6223 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006224nfa_regtry(prog, col)
6225 nfa_regprog_T *prog;
6226 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006227{
6228 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006229 regsubs_T subs, m;
6230 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006231#ifdef ENABLE_LOG
6232 FILE *f;
6233#endif
6234
6235 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006236
6237#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006238 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006239 if (f != NULL)
6240 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006241 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006242#ifdef DEBUG
6243 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6244#endif
6245 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006246 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006247 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006248 fprintf(f, "\n\n");
6249 fclose(f);
6250 }
6251 else
6252 EMSG(_("Could not open temporary log file for writing "));
6253#endif
6254
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006255 clear_sub(&subs.norm);
6256 clear_sub(&m.norm);
6257#ifdef FEAT_SYN_HL
6258 clear_sub(&subs.synt);
6259 clear_sub(&m.synt);
6260#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006261
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006262 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006263 return 0;
6264
6265 cleanup_subexpr();
6266 if (REG_MULTI)
6267 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006268 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006269 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006270 reg_startpos[i] = subs.norm.list.multi[i].start;
6271 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006272 }
6273
6274 if (reg_startpos[0].lnum < 0)
6275 {
6276 reg_startpos[0].lnum = 0;
6277 reg_startpos[0].col = col;
6278 }
6279 if (reg_endpos[0].lnum < 0)
6280 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006281 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006282 reg_endpos[0].lnum = reglnum;
6283 reg_endpos[0].col = (int)(reginput - regline);
6284 }
6285 else
6286 /* Use line number of "\ze". */
6287 reglnum = reg_endpos[0].lnum;
6288 }
6289 else
6290 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006291 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006292 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006293 reg_startp[i] = subs.norm.list.line[i].start;
6294 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006295 }
6296
6297 if (reg_startp[0] == NULL)
6298 reg_startp[0] = regline + col;
6299 if (reg_endp[0] == NULL)
6300 reg_endp[0] = reginput;
6301 }
6302
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006303#ifdef FEAT_SYN_HL
6304 /* Package any found \z(...\) matches for export. Default is none. */
6305 unref_extmatch(re_extmatch_out);
6306 re_extmatch_out = NULL;
6307
6308 if (prog->reghasz == REX_SET)
6309 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006310 cleanup_zsubexpr();
6311 re_extmatch_out = make_extmatch();
6312 for (i = 0; i < subs.synt.in_use; i++)
6313 {
6314 if (REG_MULTI)
6315 {
6316 struct multipos *mpos = &subs.synt.list.multi[i];
6317
6318 /* Only accept single line matches. */
6319 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6320 re_extmatch_out->matches[i] =
6321 vim_strnsave(reg_getline(mpos->start.lnum)
6322 + mpos->start.col,
6323 mpos->end.col - mpos->start.col);
6324 }
6325 else
6326 {
6327 struct linepos *lpos = &subs.synt.list.line[i];
6328
6329 if (lpos->start != NULL && lpos->end != NULL)
6330 re_extmatch_out->matches[i] =
6331 vim_strnsave(lpos->start,
6332 (int)(lpos->end - lpos->start));
6333 }
6334 }
6335 }
6336#endif
6337
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006338 return 1 + reglnum;
6339}
6340
6341/*
6342 * Match a regexp against a string ("line" points to the string) or multiple
6343 * lines ("line" is NULL, use reg_getline()).
6344 *
6345 * Returns 0 for failure, number of lines contained in the match otherwise.
6346 */
6347 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006348nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006349 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006350 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006351{
6352 nfa_regprog_T *prog;
6353 long retval = 0L;
6354 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006355 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006356
6357 if (REG_MULTI)
6358 {
6359 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6360 line = reg_getline((linenr_T)0); /* relative to the cursor */
6361 reg_startpos = reg_mmatch->startpos;
6362 reg_endpos = reg_mmatch->endpos;
6363 }
6364 else
6365 {
6366 prog = (nfa_regprog_T *)reg_match->regprog;
6367 reg_startp = reg_match->startp;
6368 reg_endp = reg_match->endp;
6369 }
6370
6371 /* Be paranoid... */
6372 if (prog == NULL || line == NULL)
6373 {
6374 EMSG(_(e_null));
6375 goto theend;
6376 }
6377
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006378 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6379 if (prog->regflags & RF_ICASE)
6380 ireg_ic = TRUE;
6381 else if (prog->regflags & RF_NOICASE)
6382 ireg_ic = FALSE;
6383
6384#ifdef FEAT_MBYTE
6385 /* If pattern contains "\Z" overrule value of ireg_icombine */
6386 if (prog->regflags & RF_ICOMBINE)
6387 ireg_icombine = TRUE;
6388#endif
6389
6390 regline = line;
6391 reglnum = 0; /* relative to line */
6392
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006393 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006394 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006395 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006396 nfa_listid = 1;
6397 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006398#ifdef DEBUG
6399 nfa_regengine.expr = prog->pattern;
6400#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401
Bram Moolenaard89616e2013-06-06 18:46:06 +02006402 if (prog->reganch && col > 0)
6403 return 0L;
6404
Bram Moolenaar473de612013-06-08 18:19:48 +02006405 need_clear_subexpr = TRUE;
6406#ifdef FEAT_SYN_HL
6407 /* Clear the external match subpointers if necessary. */
6408 if (prog->reghasz == REX_SET)
6409 {
6410 nfa_has_zsubexpr = TRUE;
6411 need_clear_zsubexpr = TRUE;
6412 }
6413 else
6414 nfa_has_zsubexpr = FALSE;
6415#endif
6416
Bram Moolenaard89616e2013-06-06 18:46:06 +02006417 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006418 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006419 /* Skip ahead until a character we know the match must start with.
6420 * When there is none there is no match. */
6421 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006422 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006423
Bram Moolenaar473de612013-06-08 18:19:48 +02006424 /* If match_text is set it contains the full text that must match.
6425 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006426 if (prog->match_text != NULL
6427#ifdef FEAT_MBYTE
6428 && !ireg_icombine
6429#endif
6430 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006431 return find_match_text(col, prog->regstart, prog->match_text);
6432 }
6433
Bram Moolenaard89616e2013-06-06 18:46:06 +02006434 /* If the start column is past the maximum column: no need to try. */
6435 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6436 goto theend;
6437
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006438 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006439 for (i = 0; i < nstate; ++i)
6440 {
6441 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006442 prog->state[i].lastlist[0] = 0;
6443 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006444 }
6445
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006446 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006447
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006448#ifdef DEBUG
6449 nfa_regengine.expr = NULL;
6450#endif
6451
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006452theend:
6453 return retval;
6454}
6455
6456/*
6457 * Compile a regular expression into internal code for the NFA matcher.
6458 * Returns the program in allocated space. Returns NULL for an error.
6459 */
6460 static regprog_T *
6461nfa_regcomp(expr, re_flags)
6462 char_u *expr;
6463 int re_flags;
6464{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006465 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006466 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006467 int *postfix;
6468
6469 if (expr == NULL)
6470 return NULL;
6471
6472#ifdef DEBUG
6473 nfa_regengine.expr = expr;
6474#endif
6475
6476 init_class_tab();
6477
6478 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6479 return NULL;
6480
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006481 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006482 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006483 postfix = re2post();
6484 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006485 {
6486 /* TODO: only give this error for debugging? */
6487 if (post_ptr >= post_end)
6488 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006489 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006490 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006491
6492 /*
6493 * In order to build the NFA, we parse the input regexp twice:
6494 * 1. first pass to count size (so we can allocate space)
6495 * 2. second to emit code
6496 */
6497#ifdef ENABLE_LOG
6498 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006499 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006500
6501 if (f != NULL)
6502 {
6503 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6504 fclose(f);
6505 }
6506 }
6507#endif
6508
6509 /*
6510 * PASS 1
6511 * Count number of NFA states in "nstate". Do not build the NFA.
6512 */
6513 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006514
Bram Moolenaar16619a22013-06-11 18:42:36 +02006515 /* allocate the regprog with space for the compiled regexp */
6516 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006517 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6518 if (prog == NULL)
6519 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006520 state_ptr = prog->state;
6521
6522 /*
6523 * PASS 2
6524 * Build the NFA
6525 */
6526 prog->start = post2nfa(postfix, post_ptr, FALSE);
6527 if (prog->start == NULL)
6528 goto fail;
6529
6530 prog->regflags = regflags;
6531 prog->engine = &nfa_regengine;
6532 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006533 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006534 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006535 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006536
Bram Moolenaara2947e22013-06-11 22:44:09 +02006537 nfa_postprocess(prog);
6538
Bram Moolenaard89616e2013-06-06 18:46:06 +02006539 prog->reganch = nfa_get_reganch(prog->start, 0);
6540 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006541 prog->match_text = nfa_get_match_text(prog->start);
6542
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006543#ifdef ENABLE_LOG
6544 nfa_postfix_dump(expr, OK);
6545 nfa_dump(prog);
6546#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006547#ifdef FEAT_SYN_HL
6548 /* Remember whether this pattern has any \z specials in it. */
6549 prog->reghasz = re_has_z;
6550#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006551#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006552 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006553 nfa_regengine.expr = NULL;
6554#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006555
6556out:
6557 vim_free(post_start);
6558 post_start = post_ptr = post_end = NULL;
6559 state_ptr = NULL;
6560 return (regprog_T *)prog;
6561
6562fail:
6563 vim_free(prog);
6564 prog = NULL;
6565#ifdef ENABLE_LOG
6566 nfa_postfix_dump(expr, FAIL);
6567#endif
6568#ifdef DEBUG
6569 nfa_regengine.expr = NULL;
6570#endif
6571 goto out;
6572}
6573
Bram Moolenaar473de612013-06-08 18:19:48 +02006574/*
6575 * Free a compiled regexp program, returned by nfa_regcomp().
6576 */
6577 static void
6578nfa_regfree(prog)
6579 regprog_T *prog;
6580{
6581 if (prog != NULL)
6582 {
6583 vim_free(((nfa_regprog_T *)prog)->match_text);
6584#ifdef DEBUG
6585 vim_free(((nfa_regprog_T *)prog)->pattern);
6586#endif
6587 vim_free(prog);
6588 }
6589}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006590
6591/*
6592 * Match a regexp against a string.
6593 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6594 * Uses curbuf for line count and 'iskeyword'.
6595 *
6596 * Return TRUE if there is a match, FALSE if not.
6597 */
6598 static int
6599nfa_regexec(rmp, line, col)
6600 regmatch_T *rmp;
6601 char_u *line; /* string to match against */
6602 colnr_T col; /* column to start looking for match */
6603{
6604 reg_match = rmp;
6605 reg_mmatch = NULL;
6606 reg_maxline = 0;
6607 reg_line_lbr = FALSE;
6608 reg_buf = curbuf;
6609 reg_win = NULL;
6610 ireg_ic = rmp->rm_ic;
6611#ifdef FEAT_MBYTE
6612 ireg_icombine = FALSE;
6613#endif
6614 ireg_maxcol = 0;
6615 return (nfa_regexec_both(line, col) != 0);
6616}
6617
6618#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6619 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6620
6621static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6622
6623/*
6624 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6625 */
6626 static int
6627nfa_regexec_nl(rmp, line, col)
6628 regmatch_T *rmp;
6629 char_u *line; /* string to match against */
6630 colnr_T col; /* column to start looking for match */
6631{
6632 reg_match = rmp;
6633 reg_mmatch = NULL;
6634 reg_maxline = 0;
6635 reg_line_lbr = TRUE;
6636 reg_buf = curbuf;
6637 reg_win = NULL;
6638 ireg_ic = rmp->rm_ic;
6639#ifdef FEAT_MBYTE
6640 ireg_icombine = FALSE;
6641#endif
6642 ireg_maxcol = 0;
6643 return (nfa_regexec_both(line, col) != 0);
6644}
6645#endif
6646
6647
6648/*
6649 * Match a regexp against multiple lines.
6650 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6651 * Uses curbuf for line count and 'iskeyword'.
6652 *
6653 * Return zero if there is no match. Return number of lines contained in the
6654 * match otherwise.
6655 *
6656 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6657 *
6658 * ! Also NOTE : match may actually be in another line. e.g.:
6659 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6660 *
6661 * +-------------------------+
6662 * |a |
6663 * |b |
6664 * |c |
6665 * | |
6666 * +-------------------------+
6667 *
6668 * then nfa_regexec_multi() returns 3. while the original
6669 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6670 *
6671 * FIXME if this behavior is not compatible.
6672 */
6673 static long
6674nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6675 regmmatch_T *rmp;
6676 win_T *win; /* window in which to search or NULL */
6677 buf_T *buf; /* buffer in which to search */
6678 linenr_T lnum; /* nr of line to start looking for match */
6679 colnr_T col; /* column to start looking for match */
6680 proftime_T *tm UNUSED; /* timeout limit or NULL */
6681{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006682 reg_match = NULL;
6683 reg_mmatch = rmp;
6684 reg_buf = buf;
6685 reg_win = win;
6686 reg_firstlnum = lnum;
6687 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6688 reg_line_lbr = FALSE;
6689 ireg_ic = rmp->rmm_ic;
6690#ifdef FEAT_MBYTE
6691 ireg_icombine = FALSE;
6692#endif
6693 ireg_maxcol = rmp->rmm_maxcol;
6694
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006695 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006696}
6697
6698#ifdef DEBUG
6699# undef ENABLE_LOG
6700#endif