blob: f0e7744322308e72151f6155353c950106c867ef [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 */
41 NFA_END_NEG_COLL, /* [^abc] end (only used in postfix) */
42 NFA_RANGE, /* range of the two previous items (only
43 * used in postfix) */
44 NFA_RANGE_MIN, /* low end of a range */
45 NFA_RANGE_MAX, /* high end of a range */
46
47 NFA_CONCAT, /* concatenate two previous items (only
48 * used in postfix) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020049 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020050 NFA_STAR, /* greedy * */
51 NFA_STAR_NONGREEDY, /* non-greedy * */
52 NFA_QUEST, /* greedy \? */
53 NFA_QUEST_NONGREEDY, /* non-greedy \? */
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 Moolenaardecd9542013-06-07 16:31:50 +020067 NFA_START_INVISIBLE_NEG,
Bram Moolenaar61602c52013-06-01 19:54:43 +020068 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020069 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020070 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020071 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020072 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020073 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020074 NFA_COMPOSING, /* Next nodes in NFA are part of the
75 composing multibyte char */
76 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020077 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078
79 /* The following are used only in the postfix form, not in the NFA */
80 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
81 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
82 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
83 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
84 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
85
Bram Moolenaar5714b802013-05-28 22:03:20 +020086 NFA_BACKREF1, /* \1 */
87 NFA_BACKREF2, /* \2 */
88 NFA_BACKREF3, /* \3 */
89 NFA_BACKREF4, /* \4 */
90 NFA_BACKREF5, /* \5 */
91 NFA_BACKREF6, /* \6 */
92 NFA_BACKREF7, /* \7 */
93 NFA_BACKREF8, /* \8 */
94 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020095#ifdef FEAT_SYN_HL
96 NFA_ZREF1, /* \z1 */
97 NFA_ZREF2, /* \z2 */
98 NFA_ZREF3, /* \z3 */
99 NFA_ZREF4, /* \z4 */
100 NFA_ZREF5, /* \z5 */
101 NFA_ZREF6, /* \z6 */
102 NFA_ZREF7, /* \z7 */
103 NFA_ZREF8, /* \z8 */
104 NFA_ZREF9, /* \z9 */
105#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200106 NFA_SKIP, /* Skip characters */
107
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200108 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200109 NFA_MOPEN1,
110 NFA_MOPEN2,
111 NFA_MOPEN3,
112 NFA_MOPEN4,
113 NFA_MOPEN5,
114 NFA_MOPEN6,
115 NFA_MOPEN7,
116 NFA_MOPEN8,
117 NFA_MOPEN9,
118
119 NFA_MCLOSE,
120 NFA_MCLOSE1,
121 NFA_MCLOSE2,
122 NFA_MCLOSE3,
123 NFA_MCLOSE4,
124 NFA_MCLOSE5,
125 NFA_MCLOSE6,
126 NFA_MCLOSE7,
127 NFA_MCLOSE8,
128 NFA_MCLOSE9,
129
130#ifdef FEAT_SYN_HL
131 NFA_ZOPEN,
132 NFA_ZOPEN1,
133 NFA_ZOPEN2,
134 NFA_ZOPEN3,
135 NFA_ZOPEN4,
136 NFA_ZOPEN5,
137 NFA_ZOPEN6,
138 NFA_ZOPEN7,
139 NFA_ZOPEN8,
140 NFA_ZOPEN9,
141
142 NFA_ZCLOSE,
143 NFA_ZCLOSE1,
144 NFA_ZCLOSE2,
145 NFA_ZCLOSE3,
146 NFA_ZCLOSE4,
147 NFA_ZCLOSE5,
148 NFA_ZCLOSE6,
149 NFA_ZCLOSE7,
150 NFA_ZCLOSE8,
151 NFA_ZCLOSE9,
152#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200153
154 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200155 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200156 NFA_ANYOF, /* Match any character in this string. */
157 NFA_ANYBUT, /* Match any character not in this string. */
158 NFA_IDENT, /* Match identifier char */
159 NFA_SIDENT, /* Match identifier char but no digit */
160 NFA_KWORD, /* Match keyword char */
161 NFA_SKWORD, /* Match word char but no digit */
162 NFA_FNAME, /* Match file name char */
163 NFA_SFNAME, /* Match file name char but no digit */
164 NFA_PRINT, /* Match printable char */
165 NFA_SPRINT, /* Match printable char but no digit */
166 NFA_WHITE, /* Match whitespace char */
167 NFA_NWHITE, /* Match non-whitespace char */
168 NFA_DIGIT, /* Match digit char */
169 NFA_NDIGIT, /* Match non-digit char */
170 NFA_HEX, /* Match hex char */
171 NFA_NHEX, /* Match non-hex char */
172 NFA_OCTAL, /* Match octal char */
173 NFA_NOCTAL, /* Match non-octal char */
174 NFA_WORD, /* Match word char */
175 NFA_NWORD, /* Match non-word char */
176 NFA_HEAD, /* Match head char */
177 NFA_NHEAD, /* Match non-head char */
178 NFA_ALPHA, /* Match alpha char */
179 NFA_NALPHA, /* Match non-alpha char */
180 NFA_LOWER, /* Match lowercase char */
181 NFA_NLOWER, /* Match non-lowercase char */
182 NFA_UPPER, /* Match uppercase char */
183 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200184
185 NFA_CURSOR, /* Match cursor pos */
186 NFA_LNUM, /* Match line number */
187 NFA_LNUM_GT, /* Match > line number */
188 NFA_LNUM_LT, /* Match < line number */
189 NFA_COL, /* Match cursor column */
190 NFA_COL_GT, /* Match > cursor column */
191 NFA_COL_LT, /* Match < cursor column */
192 NFA_VCOL, /* Match cursor virtual column */
193 NFA_VCOL_GT, /* Match > cursor virtual column */
194 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200195 NFA_MARK, /* Match mark */
196 NFA_MARK_GT, /* Match > mark */
197 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200198 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200199
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200200 NFA_FIRST_NL = NFA_ANY + ADD_NL,
201 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
202
203 /* Character classes [:alnum:] etc */
204 NFA_CLASS_ALNUM,
205 NFA_CLASS_ALPHA,
206 NFA_CLASS_BLANK,
207 NFA_CLASS_CNTRL,
208 NFA_CLASS_DIGIT,
209 NFA_CLASS_GRAPH,
210 NFA_CLASS_LOWER,
211 NFA_CLASS_PRINT,
212 NFA_CLASS_PUNCT,
213 NFA_CLASS_SPACE,
214 NFA_CLASS_UPPER,
215 NFA_CLASS_XDIGIT,
216 NFA_CLASS_TAB,
217 NFA_CLASS_RETURN,
218 NFA_CLASS_BACKSPACE,
219 NFA_CLASS_ESCAPE
220};
221
222/* Keep in sync with classchars. */
223static int nfa_classcodes[] = {
224 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
225 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
226 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
227 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
228 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
229 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
230 NFA_UPPER, NFA_NUPPER
231};
232
233static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
234
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200235/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200236static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237
Bram Moolenaar428e9872013-05-30 17:05:39 +0200238/* NFA regexp \1 .. \9 encountered. */
239static int nfa_has_backref;
240
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200241#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200242/* NFA regexp has \z( ), set zsubexpr. */
243static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200244#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200245
Bram Moolenaar963fee22013-05-26 21:47:28 +0200246/* Number of sub expressions actually being used during execution. 1 if only
247 * the whole match (subexpr 0) is used. */
248static int nfa_nsubexpr;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250static int *post_start; /* holds the postfix form of r.e. */
251static int *post_end;
252static int *post_ptr;
253
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200254static int nstate; /* Number of states in the NFA. Also used when
255 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200256static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar307aa162013-06-02 16:34:21 +0200258/* If not NULL match must end at this position */
259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200261/* listid is global, so that it increases on recursive calls to
262 * nfa_regmatch(), which means we don't have to clear the lastlist field of
263 * all the states. */
264static int nfa_listid;
265static int nfa_alt_listid;
266
267/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
268static int nfa_ll_index = 0;
269
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200270static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200271static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
272static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200273static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200274static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200275static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276static int nfa_regatom __ARGS((void));
277static int nfa_regpiece __ARGS((void));
278static int nfa_regconcat __ARGS((void));
279static int nfa_regbranch __ARGS((void));
280static int nfa_reg __ARGS((int paren));
281#ifdef DEBUG
282static void nfa_set_code __ARGS((int c));
283static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200284static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
285static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286static void nfa_dump __ARGS((nfa_regprog_T *prog));
287#endif
288static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200289static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
291static int check_char_class __ARGS((int class, int c));
292static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200293static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
294static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200295static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200296static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
298static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200299static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
301static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
302
303/* helper functions used when doing re2post() ... regatom() parsing */
304#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200305 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306 return FAIL; \
307 *post_ptr++ = c; \
308 } while (0)
309
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310/*
311 * Initialize internal variables before NFA compilation.
312 * Return OK on success, FAIL otherwise.
313 */
314 static int
315nfa_regcomp_start(expr, re_flags)
316 char_u *expr;
317 int re_flags; /* see vim_regcomp() */
318{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200319 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200320 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200321
322 nstate = 0;
323 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200324 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200325 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200327 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200328 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200329 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200330
331 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200332 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200333
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200334 post_start = (int *)lalloc(postfix_size, TRUE);
335 if (post_start == NULL)
336 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200337 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200338 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200340 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200342 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200343 regcomp_start(expr, re_flags);
344
345 return OK;
346}
347
348/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200349 * Figure out if the NFA state list starts with an anchor, must match at start
350 * of the line.
351 */
352 static int
353nfa_get_reganch(start, depth)
354 nfa_state_T *start;
355 int depth;
356{
357 nfa_state_T *p = start;
358
359 if (depth > 4)
360 return 0;
361
362 while (p != NULL)
363 {
364 switch (p->c)
365 {
366 case NFA_BOL:
367 case NFA_BOF:
368 return 1; /* yes! */
369
370 case NFA_ZSTART:
371 case NFA_ZEND:
372 case NFA_CURSOR:
373 case NFA_VISUAL:
374
375 case NFA_MOPEN:
376 case NFA_MOPEN1:
377 case NFA_MOPEN2:
378 case NFA_MOPEN3:
379 case NFA_MOPEN4:
380 case NFA_MOPEN5:
381 case NFA_MOPEN6:
382 case NFA_MOPEN7:
383 case NFA_MOPEN8:
384 case NFA_MOPEN9:
385 case NFA_NOPEN:
386#ifdef FEAT_SYN_HL
387 case NFA_ZOPEN:
388 case NFA_ZOPEN1:
389 case NFA_ZOPEN2:
390 case NFA_ZOPEN3:
391 case NFA_ZOPEN4:
392 case NFA_ZOPEN5:
393 case NFA_ZOPEN6:
394 case NFA_ZOPEN7:
395 case NFA_ZOPEN8:
396 case NFA_ZOPEN9:
397#endif
398 p = p->out;
399 break;
400
401 case NFA_SPLIT:
402 return nfa_get_reganch(p->out, depth + 1)
403 && nfa_get_reganch(p->out1, depth + 1);
404
405 default:
406 return 0; /* noooo */
407 }
408 }
409 return 0;
410}
411
412/*
413 * Figure out if the NFA state list starts with a character which must match
414 * at start of the match.
415 */
416 static int
417nfa_get_regstart(start, depth)
418 nfa_state_T *start;
419 int depth;
420{
421 nfa_state_T *p = start;
422
423 if (depth > 4)
424 return 0;
425
426 while (p != NULL)
427 {
428 switch (p->c)
429 {
430 /* all kinds of zero-width matches */
431 case NFA_BOL:
432 case NFA_BOF:
433 case NFA_BOW:
434 case NFA_EOW:
435 case NFA_ZSTART:
436 case NFA_ZEND:
437 case NFA_CURSOR:
438 case NFA_VISUAL:
439 case NFA_LNUM:
440 case NFA_LNUM_GT:
441 case NFA_LNUM_LT:
442 case NFA_COL:
443 case NFA_COL_GT:
444 case NFA_COL_LT:
445 case NFA_VCOL:
446 case NFA_VCOL_GT:
447 case NFA_VCOL_LT:
448 case NFA_MARK:
449 case NFA_MARK_GT:
450 case NFA_MARK_LT:
451
452 case NFA_MOPEN:
453 case NFA_MOPEN1:
454 case NFA_MOPEN2:
455 case NFA_MOPEN3:
456 case NFA_MOPEN4:
457 case NFA_MOPEN5:
458 case NFA_MOPEN6:
459 case NFA_MOPEN7:
460 case NFA_MOPEN8:
461 case NFA_MOPEN9:
462 case NFA_NOPEN:
463#ifdef FEAT_SYN_HL
464 case NFA_ZOPEN:
465 case NFA_ZOPEN1:
466 case NFA_ZOPEN2:
467 case NFA_ZOPEN3:
468 case NFA_ZOPEN4:
469 case NFA_ZOPEN5:
470 case NFA_ZOPEN6:
471 case NFA_ZOPEN7:
472 case NFA_ZOPEN8:
473 case NFA_ZOPEN9:
474#endif
475 p = p->out;
476 break;
477
478 case NFA_SPLIT:
479 {
480 int c1 = nfa_get_regstart(p->out, depth + 1);
481 int c2 = nfa_get_regstart(p->out1, depth + 1);
482
483 if (c1 == c2)
484 return c1; /* yes! */
485 return 0;
486 }
487
488 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200489 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200490 return p->c; /* yes! */
491 return 0;
492 }
493 }
494 return 0;
495}
496
497/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200498 * Figure out if the NFA state list contains just literal text and nothing
499 * else. If so return a string with what must match after regstart.
500 * Otherwise return NULL.
501 */
502 static char_u *
503nfa_get_match_text(start)
504 nfa_state_T *start;
505{
506 nfa_state_T *p = start;
507 int len = 0;
508 char_u *ret;
509 char_u *s;
510
511 if (p->c != NFA_MOPEN)
512 return NULL; /* just in case */
513 p = p->out;
514 while (p->c > 0)
515 {
516 len += MB_CHAR2LEN(p->c);
517 p = p->out;
518 }
519 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
520 return NULL;
521
522 ret = alloc(len);
523 if (ret != NULL)
524 {
525 len = 0;
526 p = start->out->out; /* skip first char, it goes into regstart */
527 s = ret;
528 while (p->c > 0)
529 {
530#ifdef FEAT_MBYTE
531 if (has_mbyte)
532 s += (*mb_char2bytes)(p->c, s);
533 else
534#endif
535 *s++ = p->c;
536 p = p->out;
537 }
538 *s = NUL;
539 }
540 return ret;
541}
542
543/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200544 * Allocate more space for post_start. Called when
545 * running above the estimated number of states.
546 */
547 static int
548realloc_post_list()
549{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200550 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200551 int new_max = nstate_max + 1000;
552 int *new_start;
553 int *old_start;
554
555 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
556 if (new_start == NULL)
557 return FAIL;
558 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200559 old_start = post_start;
560 post_start = new_start;
561 post_ptr = new_start + (post_ptr - old_start);
562 post_end = post_start + new_max;
563 vim_free(old_start);
564 return OK;
565}
566
567/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200568 * Search between "start" and "end" and try to recognize a
569 * character class in expanded form. For example [0-9].
570 * On success, return the id the character class to be emitted.
571 * On failure, return 0 (=FAIL)
572 * Start points to the first char of the range, while end should point
573 * to the closing brace.
574 */
575 static int
576nfa_recognize_char_class(start, end, extra_newl)
577 char_u *start;
578 char_u *end;
579 int extra_newl;
580{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200581# define CLASS_not 0x80
582# define CLASS_af 0x40
583# define CLASS_AF 0x20
584# define CLASS_az 0x10
585# define CLASS_AZ 0x08
586# define CLASS_o7 0x04
587# define CLASS_o9 0x02
588# define CLASS_underscore 0x01
589
590 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200591 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200592 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593
594 if (extra_newl == TRUE)
595 newl = TRUE;
596
597 if (*end != ']')
598 return FAIL;
599 p = start;
600 if (*p == '^')
601 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200602 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200603 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 }
605
606 while (p < end)
607 {
608 if (p + 2 < end && *(p + 1) == '-')
609 {
610 switch (*p)
611 {
612 case '0':
613 if (*(p + 2) == '9')
614 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200615 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200616 break;
617 }
618 else
619 if (*(p + 2) == '7')
620 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200621 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200622 break;
623 }
624 case 'a':
625 if (*(p + 2) == 'z')
626 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200627 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200628 break;
629 }
630 else
631 if (*(p + 2) == 'f')
632 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200633 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 break;
635 }
636 case 'A':
637 if (*(p + 2) == 'Z')
638 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200639 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200640 break;
641 }
642 else
643 if (*(p + 2) == 'F')
644 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200645 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200646 break;
647 }
648 /* FALLTHROUGH */
649 default:
650 return FAIL;
651 }
652 p += 3;
653 }
654 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
655 {
656 newl = TRUE;
657 p += 2;
658 }
659 else if (*p == '_')
660 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200661 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200662 p ++;
663 }
664 else if (*p == '\n')
665 {
666 newl = TRUE;
667 p ++;
668 }
669 else
670 return FAIL;
671 } /* while (p < end) */
672
673 if (p != end)
674 return FAIL;
675
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200676 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200678
679 switch (config)
680 {
681 case CLASS_o9:
682 return extra_newl + NFA_DIGIT;
683 case CLASS_not | CLASS_o9:
684 return extra_newl + NFA_NDIGIT;
685 case CLASS_af | CLASS_AF | CLASS_o9:
686 return extra_newl + NFA_HEX;
687 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
688 return extra_newl + NFA_NHEX;
689 case CLASS_o7:
690 return extra_newl + NFA_OCTAL;
691 case CLASS_not | CLASS_o7:
692 return extra_newl + NFA_NOCTAL;
693 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
694 return extra_newl + NFA_WORD;
695 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
696 return extra_newl + NFA_NWORD;
697 case CLASS_az | CLASS_AZ | CLASS_underscore:
698 return extra_newl + NFA_HEAD;
699 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
700 return extra_newl + NFA_NHEAD;
701 case CLASS_az | CLASS_AZ:
702 return extra_newl + NFA_ALPHA;
703 case CLASS_not | CLASS_az | CLASS_AZ:
704 return extra_newl + NFA_NALPHA;
705 case CLASS_az:
706 return extra_newl + NFA_LOWER;
707 case CLASS_not | CLASS_az:
708 return extra_newl + NFA_NLOWER;
709 case CLASS_AZ:
710 return extra_newl + NFA_UPPER;
711 case CLASS_not | CLASS_AZ:
712 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200713 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200714 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200715}
716
717/*
718 * Produce the bytes for equivalence class "c".
719 * Currently only handles latin1, latin9 and utf-8.
720 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
721 * equivalent to 'a OR b OR c'
722 *
723 * NOTE! When changing this function, also update reg_equi_class()
724 */
725 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200726nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200727 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200729#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730
731#ifdef FEAT_MBYTE
732 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
733 || STRCMP(p_enc, "iso-8859-15") == 0)
734#endif
735 {
736 switch (c)
737 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200738 case 'A': case 0300: case 0301: case 0302:
739 case 0303: case 0304: case 0305:
740 EMIT2('A'); EMIT2(0300); EMIT2(0301);
741 EMIT2(0302); EMIT2(0303); EMIT2(0304);
742 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200743 return OK;
744
Bram Moolenaar417bad22013-06-07 14:08:30 +0200745 case 'C': case 0307:
746 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200747 return OK;
748
Bram Moolenaar417bad22013-06-07 14:08:30 +0200749 case 'E': case 0310: case 0311: case 0312: case 0313:
750 EMIT2('E'); EMIT2(0310); EMIT2(0311);
751 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200752 return OK;
753
Bram Moolenaar417bad22013-06-07 14:08:30 +0200754 case 'I': case 0314: case 0315: case 0316: case 0317:
755 EMIT2('I'); EMIT2(0314); EMIT2(0315);
756 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200757 return OK;
758
Bram Moolenaar417bad22013-06-07 14:08:30 +0200759 case 'N': case 0321:
760 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200761 return OK;
762
Bram Moolenaar417bad22013-06-07 14:08:30 +0200763 case 'O': case 0322: case 0323: case 0324: case 0325:
764 case 0326:
765 EMIT2('O'); EMIT2(0322); EMIT2(0323);
766 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200767 return OK;
768
Bram Moolenaar417bad22013-06-07 14:08:30 +0200769 case 'U': case 0331: case 0332: case 0333: case 0334:
770 EMIT2('U'); EMIT2(0331); EMIT2(0332);
771 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200772 return OK;
773
Bram Moolenaar417bad22013-06-07 14:08:30 +0200774 case 'Y': case 0335:
775 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 return OK;
777
Bram Moolenaar417bad22013-06-07 14:08:30 +0200778 case 'a': case 0340: case 0341: case 0342:
779 case 0343: case 0344: case 0345:
780 EMIT2('a'); EMIT2(0340); EMIT2(0341);
781 EMIT2(0342); EMIT2(0343); EMIT2(0344);
782 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200783 return OK;
784
Bram Moolenaar417bad22013-06-07 14:08:30 +0200785 case 'c': case 0347:
786 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200787 return OK;
788
Bram Moolenaar417bad22013-06-07 14:08:30 +0200789 case 'e': case 0350: case 0351: case 0352: case 0353:
790 EMIT2('e'); EMIT2(0350); EMIT2(0351);
791 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200792 return OK;
793
Bram Moolenaar417bad22013-06-07 14:08:30 +0200794 case 'i': case 0354: case 0355: case 0356: case 0357:
795 EMIT2('i'); EMIT2(0354); EMIT2(0355);
796 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200797 return OK;
798
Bram Moolenaar417bad22013-06-07 14:08:30 +0200799 case 'n': case 0361:
800 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200801 return OK;
802
Bram Moolenaar417bad22013-06-07 14:08:30 +0200803 case 'o': case 0362: case 0363: case 0364: case 0365:
804 case 0366:
805 EMIT2('o'); EMIT2(0362); EMIT2(0363);
806 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200807 return OK;
808
Bram Moolenaar417bad22013-06-07 14:08:30 +0200809 case 'u': case 0371: case 0372: case 0373: case 0374:
810 EMIT2('u'); EMIT2(0371); EMIT2(0372);
811 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 return OK;
813
Bram Moolenaar417bad22013-06-07 14:08:30 +0200814 case 'y': case 0375: case 0377:
815 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 return OK;
817
818 default:
819 return FAIL;
820 }
821 }
822
823 EMIT(c);
824 return OK;
825#undef EMIT2
826}
827
828/*
829 * Code to parse regular expression.
830 *
831 * We try to reuse parsing functions in regexp.c to
832 * minimize surprise and keep the syntax consistent.
833 */
834
835/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200836 * Parse the lowest level.
837 *
838 * An atom can be one of a long list of items. Many atoms match one character
839 * in the text. It is often an ordinary character or a character class.
840 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
841 * is only for syntax highlighting.
842 *
843 * atom ::= ordinary-atom
844 * or \( pattern \)
845 * or \%( pattern \)
846 * or \z( pattern \)
847 */
848 static int
849nfa_regatom()
850{
851 int c;
852 int charclass;
853 int equiclass;
854 int collclass;
855 int got_coll_char;
856 char_u *p;
857 char_u *endp;
858#ifdef FEAT_MBYTE
859 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200860#endif
861 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200862 int emit_range;
863 int negated;
864 int result;
865 int startc = -1;
866 int endc = -1;
867 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200869 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200870 switch (c)
871 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200872 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200873 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
874
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 case Magic('^'):
876 EMIT(NFA_BOL);
877 break;
878
879 case Magic('$'):
880 EMIT(NFA_EOL);
881#if defined(FEAT_SYN_HL) || defined(PROTO)
882 had_eol = TRUE;
883#endif
884 break;
885
886 case Magic('<'):
887 EMIT(NFA_BOW);
888 break;
889
890 case Magic('>'):
891 EMIT(NFA_EOW);
892 break;
893
894 case Magic('_'):
895 c = no_Magic(getchr());
896 if (c == '^') /* "\_^" is start-of-line */
897 {
898 EMIT(NFA_BOL);
899 break;
900 }
901 if (c == '$') /* "\_$" is end-of-line */
902 {
903 EMIT(NFA_EOL);
904#if defined(FEAT_SYN_HL) || defined(PROTO)
905 had_eol = TRUE;
906#endif
907 break;
908 }
909
910 extra = ADD_NL;
911
912 /* "\_[" is collection plus newline */
913 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200914 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200915
916 /* "\_x" is character class plus newline */
917 /*FALLTHROUGH*/
918
919 /*
920 * Character classes.
921 */
922 case Magic('.'):
923 case Magic('i'):
924 case Magic('I'):
925 case Magic('k'):
926 case Magic('K'):
927 case Magic('f'):
928 case Magic('F'):
929 case Magic('p'):
930 case Magic('P'):
931 case Magic('s'):
932 case Magic('S'):
933 case Magic('d'):
934 case Magic('D'):
935 case Magic('x'):
936 case Magic('X'):
937 case Magic('o'):
938 case Magic('O'):
939 case Magic('w'):
940 case Magic('W'):
941 case Magic('h'):
942 case Magic('H'):
943 case Magic('a'):
944 case Magic('A'):
945 case Magic('l'):
946 case Magic('L'):
947 case Magic('u'):
948 case Magic('U'):
949 p = vim_strchr(classchars, no_Magic(c));
950 if (p == NULL)
951 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200952 EMSGN("INTERNAL: Unknown character class char: %ld", c);
953 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200954 }
955#ifdef FEAT_MBYTE
956 /* When '.' is followed by a composing char ignore the dot, so that
957 * the composing char is matched here. */
958 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
959 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200960 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200961 c = getchr();
962 goto nfa_do_multibyte;
963 }
964#endif
965 EMIT(nfa_classcodes[p - classchars]);
966 if (extra == ADD_NL)
967 {
968 EMIT(NFA_NEWL);
969 EMIT(NFA_OR);
970 regflags |= RF_HASNL;
971 }
972 break;
973
974 case Magic('n'):
975 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200976 /* In a string "\n" matches a newline character. */
977 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200978 else
979 {
980 /* In buffer text "\n" matches the end of a line. */
981 EMIT(NFA_NEWL);
982 regflags |= RF_HASNL;
983 }
984 break;
985
986 case Magic('('):
987 if (nfa_reg(REG_PAREN) == FAIL)
988 return FAIL; /* cascaded error */
989 break;
990
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200991 case Magic('|'):
992 case Magic('&'):
993 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200994 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200995 return FAIL;
996
997 case Magic('='):
998 case Magic('?'):
999 case Magic('+'):
1000 case Magic('@'):
1001 case Magic('*'):
1002 case Magic('{'):
1003 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001004 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001005 return FAIL;
1006
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001007 case Magic('~'):
1008 {
1009 char_u *lp;
1010
1011 /* Previous substitute pattern.
1012 * Generated as "\%(pattern\)". */
1013 if (reg_prev_sub == NULL)
1014 {
1015 EMSG(_(e_nopresub));
1016 return FAIL;
1017 }
1018 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1019 {
1020 EMIT(PTR2CHAR(lp));
1021 if (lp != reg_prev_sub)
1022 EMIT(NFA_CONCAT);
1023 }
1024 EMIT(NFA_NOPEN);
1025 break;
1026 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001027
Bram Moolenaar428e9872013-05-30 17:05:39 +02001028 case Magic('1'):
1029 case Magic('2'):
1030 case Magic('3'):
1031 case Magic('4'):
1032 case Magic('5'):
1033 case Magic('6'):
1034 case Magic('7'):
1035 case Magic('8'):
1036 case Magic('9'):
1037 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1038 nfa_has_backref = TRUE;
1039 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001040
1041 case Magic('z'):
1042 c = no_Magic(getchr());
1043 switch (c)
1044 {
1045 case 's':
1046 EMIT(NFA_ZSTART);
1047 break;
1048 case 'e':
1049 EMIT(NFA_ZEND);
1050 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001051 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001052#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001053 case '1':
1054 case '2':
1055 case '3':
1056 case '4':
1057 case '5':
1058 case '6':
1059 case '7':
1060 case '8':
1061 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001062 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001063 if (reg_do_extmatch != REX_USE)
1064 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001065 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1066 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001067 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001068 re_has_z = REX_USE;
1069 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001070 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001071 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001072 if (reg_do_extmatch != REX_SET)
1073 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001074 if (nfa_reg(REG_ZPAREN) == FAIL)
1075 return FAIL; /* cascaded error */
1076 re_has_z = REX_SET;
1077 break;
1078#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001079 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001080 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001081 no_Magic(c));
1082 return FAIL;
1083 }
1084 break;
1085
1086 case Magic('%'):
1087 c = no_Magic(getchr());
1088 switch (c)
1089 {
1090 /* () without a back reference */
1091 case '(':
1092 if (nfa_reg(REG_NPAREN) == FAIL)
1093 return FAIL;
1094 EMIT(NFA_NOPEN);
1095 break;
1096
1097 case 'd': /* %d123 decimal */
1098 case 'o': /* %o123 octal */
1099 case 'x': /* %xab hex 2 */
1100 case 'u': /* %uabcd hex 4 */
1101 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001102 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001103 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001104
Bram Moolenaar47196582013-05-25 22:04:23 +02001105 switch (c)
1106 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001107 case 'd': nr = getdecchrs(); break;
1108 case 'o': nr = getoctchrs(); break;
1109 case 'x': nr = gethexchrs(2); break;
1110 case 'u': nr = gethexchrs(4); break;
1111 case 'U': nr = gethexchrs(8); break;
1112 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001113 }
1114
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001115 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001116 EMSG2_RET_FAIL(
1117 _("E678: Invalid character after %s%%[dxouU]"),
1118 reg_magic == MAGIC_ALL);
1119 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001120 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001121 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001122 break;
1123
1124 /* Catch \%^ and \%$ regardless of where they appear in the
1125 * pattern -- regardless of whether or not it makes sense. */
1126 case '^':
1127 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001128 break;
1129
1130 case '$':
1131 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 break;
1133
1134 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001135 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001136 break;
1137
1138 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001139 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001140 break;
1141
1142 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001143 {
1144 int n;
1145
1146 /* \%[abc] */
1147 for (n = 0; (c = getchr()) != ']'; ++n)
1148 {
1149 if (c == NUL)
1150 EMSG2_RET_FAIL(_(e_missing_sb),
1151 reg_magic == MAGIC_ALL);
1152 EMIT(c);
1153 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001154 if (n == 0)
1155 EMSG2_RET_FAIL(_(e_empty_sb),
1156 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001157 EMIT(NFA_OPT_CHARS);
1158 EMIT(n);
1159 break;
1160 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001161
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001162 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001163 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001164 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001165 int cmp = c;
1166
1167 if (c == '<' || c == '>')
1168 c = getchr();
1169 while (VIM_ISDIGIT(c))
1170 {
1171 n = n * 10 + (c - '0');
1172 c = getchr();
1173 }
1174 if (c == 'l' || c == 'c' || c == 'v')
1175 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001176 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001177 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001178 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001179 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001180 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001181 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001182 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001183 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001184 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001185 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001186 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001187 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001188 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001189 break;
1190 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001191 else if (c == '\'' && n == 0)
1192 {
1193 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001194 EMIT(cmp == '<' ? NFA_MARK_LT :
1195 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001196 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001197 break;
1198 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001199 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001200 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1201 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001202 return FAIL;
1203 }
1204 break;
1205
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001207collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001209 * [abc] uses NFA_START_COLL - NFA_END_COLL
1210 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1211 * Each character is produced as a regular state, using
1212 * NFA_CONCAT to bind them together.
1213 * Besides normal characters there can be:
1214 * - character classes NFA_CLASS_*
1215 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001216 */
1217
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001218 p = regparse;
1219 endp = skip_anyof(p);
1220 if (*endp == ']')
1221 {
1222 /*
1223 * Try to reverse engineer character classes. For example,
1224 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1225 * and perform the necessary substitutions in the NFA.
1226 */
1227 result = nfa_recognize_char_class(regparse, endp,
1228 extra == ADD_NL);
1229 if (result != FAIL)
1230 {
1231 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1232 EMIT(result);
1233 else /* must be char class + newline */
1234 {
1235 EMIT(result - ADD_NL);
1236 EMIT(NFA_NEWL);
1237 EMIT(NFA_OR);
1238 }
1239 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001240 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 return OK;
1242 }
1243 /*
1244 * Failed to recognize a character class. Use the simple
1245 * version that turns [abc] into 'a' OR 'b' OR 'c'
1246 */
1247 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001248 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001249 if (*regparse == '^') /* negated range */
1250 {
1251 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001252 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001253 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001255 else
1256 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001257 if (*regparse == '-')
1258 {
1259 startc = '-';
1260 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001261 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001262 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263 }
1264 /* Emit the OR branches for each character in the [] */
1265 emit_range = FALSE;
1266 while (regparse < endp)
1267 {
1268 oldstartc = startc;
1269 startc = -1;
1270 got_coll_char = FALSE;
1271 if (*regparse == '[')
1272 {
1273 /* Check for [: :], [= =], [. .] */
1274 equiclass = collclass = 0;
1275 charclass = get_char_class(&regparse);
1276 if (charclass == CLASS_NONE)
1277 {
1278 equiclass = get_equi_class(&regparse);
1279 if (equiclass == 0)
1280 collclass = get_coll_element(&regparse);
1281 }
1282
1283 /* Character class like [:alpha:] */
1284 if (charclass != CLASS_NONE)
1285 {
1286 switch (charclass)
1287 {
1288 case CLASS_ALNUM:
1289 EMIT(NFA_CLASS_ALNUM);
1290 break;
1291 case CLASS_ALPHA:
1292 EMIT(NFA_CLASS_ALPHA);
1293 break;
1294 case CLASS_BLANK:
1295 EMIT(NFA_CLASS_BLANK);
1296 break;
1297 case CLASS_CNTRL:
1298 EMIT(NFA_CLASS_CNTRL);
1299 break;
1300 case CLASS_DIGIT:
1301 EMIT(NFA_CLASS_DIGIT);
1302 break;
1303 case CLASS_GRAPH:
1304 EMIT(NFA_CLASS_GRAPH);
1305 break;
1306 case CLASS_LOWER:
1307 EMIT(NFA_CLASS_LOWER);
1308 break;
1309 case CLASS_PRINT:
1310 EMIT(NFA_CLASS_PRINT);
1311 break;
1312 case CLASS_PUNCT:
1313 EMIT(NFA_CLASS_PUNCT);
1314 break;
1315 case CLASS_SPACE:
1316 EMIT(NFA_CLASS_SPACE);
1317 break;
1318 case CLASS_UPPER:
1319 EMIT(NFA_CLASS_UPPER);
1320 break;
1321 case CLASS_XDIGIT:
1322 EMIT(NFA_CLASS_XDIGIT);
1323 break;
1324 case CLASS_TAB:
1325 EMIT(NFA_CLASS_TAB);
1326 break;
1327 case CLASS_RETURN:
1328 EMIT(NFA_CLASS_RETURN);
1329 break;
1330 case CLASS_BACKSPACE:
1331 EMIT(NFA_CLASS_BACKSPACE);
1332 break;
1333 case CLASS_ESCAPE:
1334 EMIT(NFA_CLASS_ESCAPE);
1335 break;
1336 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001337 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001338 continue;
1339 }
1340 /* Try equivalence class [=a=] and the like */
1341 if (equiclass != 0)
1342 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001343 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 if (result == FAIL)
1345 {
1346 /* should never happen */
1347 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1348 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001349 continue;
1350 }
1351 /* Try collating class like [. .] */
1352 if (collclass != 0)
1353 {
1354 startc = collclass; /* allow [.a.]-x as a range */
1355 /* Will emit the proper atom at the end of the
1356 * while loop. */
1357 }
1358 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001359 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1360 * start character. */
1361 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001362 {
1363 emit_range = TRUE;
1364 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001365 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 continue; /* reading the end of the range */
1367 }
1368
1369 /* Now handle simple and escaped characters.
1370 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1371 * accepts "\t", "\e", etc., but only when the 'l' flag in
1372 * 'cpoptions' is not included.
1373 * Posix doesn't recognize backslash at all.
1374 */
1375 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001376 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377 && regparse + 1 <= endp
1378 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001379 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001380 && vim_strchr(REGEXP_ABBR, regparse[1])
1381 != NULL)
1382 )
1383 )
1384 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001385 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001387 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001388 startc = reg_string ? NL : NFA_NEWL;
1389 else
1390 if (*regparse == 'd'
1391 || *regparse == 'o'
1392 || *regparse == 'x'
1393 || *regparse == 'u'
1394 || *regparse == 'U'
1395 )
1396 {
1397 /* TODO(RE) This needs more testing */
1398 startc = coll_get_char();
1399 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001400 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001401 }
1402 else
1403 {
1404 /* \r,\t,\e,\b */
1405 startc = backslash_trans(*regparse);
1406 }
1407 }
1408
1409 /* Normal printable char */
1410 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001411 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001412
1413 /* Previous char was '-', so this char is end of range. */
1414 if (emit_range)
1415 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001416 endc = startc;
1417 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001418 if (startc > endc)
1419 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001420
1421 if (endc > startc + 2)
1422 {
1423 /* Emit a range instead of the sequence of
1424 * individual characters. */
1425 if (startc == 0)
1426 /* \x00 is translated to \x0a, start at \x01. */
1427 EMIT(1);
1428 else
1429 --post_ptr; /* remove NFA_CONCAT */
1430 EMIT(endc);
1431 EMIT(NFA_RANGE);
1432 EMIT(NFA_CONCAT);
1433 }
1434 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001435#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001436 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 || (*mb_char2len)(endc) > 1))
1438 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001439 /* Emit the characters in the range.
1440 * "startc" was already emitted, so skip it.
1441 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 for (c = startc + 1; c <= endc; c++)
1443 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001444 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001445 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001446 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 }
1448 else
1449#endif
1450 {
1451#ifdef EBCDIC
1452 int alpha_only = FALSE;
1453
1454 /* for alphabetical range skip the gaps
1455 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1456 if (isalpha(startc) && isalpha(endc))
1457 alpha_only = TRUE;
1458#endif
1459 /* Emit the range. "startc" was already emitted, so
1460 * skip it. */
1461 for (c = startc + 1; c <= endc; c++)
1462#ifdef EBCDIC
1463 if (!alpha_only || isalpha(startc))
1464#endif
1465 {
1466 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001467 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001468 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001469 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001470 emit_range = FALSE;
1471 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001472 }
1473 else
1474 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001475 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001476 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 * Normally, simply emit startc. But if we get char
1478 * code=0 from a collating char, then replace it with
1479 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001480 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001481 * the backtracking engine. */
1482 if (startc == NFA_NEWL)
1483 {
1484 /* Line break can't be matched as part of the
1485 * collection, add an OR below. But not for negated
1486 * range. */
1487 if (!negated)
1488 extra = ADD_NL;
1489 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001490 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001491 {
1492 if (got_coll_char == TRUE && startc == 0)
1493 EMIT(0x0a);
1494 else
1495 EMIT(startc);
1496 EMIT(NFA_CONCAT);
1497 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 }
1499
Bram Moolenaar51a29832013-05-28 22:30:35 +02001500 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 } /* while (p < endp) */
1502
Bram Moolenaar51a29832013-05-28 22:30:35 +02001503 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 if (*regparse == '-') /* if last, '-' is just a char */
1505 {
1506 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001507 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001509 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 /* skip the trailing ] */
1512 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001513 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001514
1515 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001517 EMIT(NFA_END_NEG_COLL);
1518 else
1519 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001520
1521 /* \_[] also matches \n but it's not negated */
1522 if (extra == ADD_NL)
1523 {
1524 EMIT(reg_string ? NL : NFA_NEWL);
1525 EMIT(NFA_OR);
1526 }
1527
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001528 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001529 } /* if exists closing ] */
1530
1531 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001533 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001534
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001535 default:
1536 {
1537#ifdef FEAT_MBYTE
1538 int plen;
1539
1540nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001541 /* plen is length of current char with composing chars */
1542 if (enc_utf8 && ((*mb_char2len)(c)
1543 != (plen = (*mb_ptr2len)(old_regparse))
1544 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001546 int i = 0;
1547
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001548 /* A base character plus composing characters, or just one
1549 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001550 * This requires creating a separate atom as if enclosing
1551 * the characters in (), where NFA_COMPOSING is the ( and
1552 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 * building the postfix form, not the NFA itself;
1554 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001555 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001556 for (;;)
1557 {
1558 EMIT(c);
1559 if (i > 0)
1560 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001561 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001562 break;
1563 c = utf_ptr2char(old_regparse + i);
1564 }
1565 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001566 regparse = old_regparse + plen;
1567 }
1568 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001569#endif
1570 {
1571 c = no_Magic(c);
1572 EMIT(c);
1573 }
1574 return OK;
1575 }
1576 }
1577
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001578 return OK;
1579}
1580
1581/*
1582 * Parse something followed by possible [*+=].
1583 *
1584 * A piece is an atom, possibly followed by a multi, an indication of how many
1585 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1586 * characters: "", "a", "aa", etc.
1587 *
1588 * piece ::= atom
1589 * or atom multi
1590 */
1591 static int
1592nfa_regpiece()
1593{
1594 int i;
1595 int op;
1596 int ret;
1597 long minval, maxval;
1598 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001599 parse_state_T old_state;
1600 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001601 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001602 int old_post_pos;
1603 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 int quest;
1605
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001606 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1607 * next. */
1608 save_parse_state(&old_state);
1609
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001611 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612
1613 ret = nfa_regatom();
1614 if (ret == FAIL)
1615 return FAIL; /* cascaded error */
1616
1617 op = peekchr();
1618 if (re_multi_type(op) == NOT_MULTI)
1619 return OK;
1620
1621 skipchr();
1622 switch (op)
1623 {
1624 case Magic('*'):
1625 EMIT(NFA_STAR);
1626 break;
1627
1628 case Magic('+'):
1629 /*
1630 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1631 * first and only submatch would be "aaa". But the backtracking
1632 * engine interprets the plus as "try matching one more time", and
1633 * a* matches a second time at the end of the input, the empty
1634 * string.
1635 * The submatch will the empty string.
1636 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001637 * In order to be consistent with the old engine, we replace
1638 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001640 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001641 curchr = -1;
1642 if (nfa_regatom() == FAIL)
1643 return FAIL;
1644 EMIT(NFA_STAR);
1645 EMIT(NFA_CONCAT);
1646 skipchr(); /* skip the \+ */
1647 break;
1648
1649 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001650 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001652 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 switch(op)
1654 {
1655 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001656 /* \@= */
1657 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001659 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001660 /* \@! */
1661 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001662 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001664 op = no_Magic(getchr());
1665 if (op == '=')
1666 /* \@<= */
1667 i = NFA_PREV_ATOM_JUST_BEFORE;
1668 else if (op == '!')
1669 /* \@<! */
1670 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1671 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001672 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001673 /* \@> */
1674 i = NFA_PREV_ATOM_LIKE_PATTERN;
1675 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001677 if (i == 0)
1678 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001679 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1680 return FAIL;
1681 }
1682 EMIT(i);
1683 if (i == NFA_PREV_ATOM_JUST_BEFORE
1684 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1685 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001686 break;
1687
1688 case Magic('?'):
1689 case Magic('='):
1690 EMIT(NFA_QUEST);
1691 break;
1692
1693 case Magic('{'):
1694 /* a{2,5} will expand to 'aaa?a?a?'
1695 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1696 * version of '?'
1697 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1698 * parenthesis have the same id
1699 */
1700
1701 greedy = TRUE;
1702 c2 = peekchr();
1703 if (c2 == '-' || c2 == Magic('-'))
1704 {
1705 skipchr();
1706 greedy = FALSE;
1707 }
1708 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001709 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001710
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001711 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1712 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001713 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001714 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001715 if (greedy)
1716 /* \{}, \{0,} */
1717 EMIT(NFA_STAR);
1718 else
1719 /* \{-}, \{-0,} */
1720 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001721 break;
1722 }
1723
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001724 /* Special case: x{0} or x{-0} */
1725 if (maxval == 0)
1726 {
1727 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001728 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001729 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1730 EMIT(NFA_SKIP_CHAR);
1731 return OK;
1732 }
1733
1734 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001735 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001736 /* Save parse state after the repeated atom and the \{} */
1737 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1740 for (i = 0; i < maxval; i++)
1741 {
1742 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001743 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001744 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745 if (nfa_regatom() == FAIL)
1746 return FAIL;
1747 /* after "minval" times, atoms are optional */
1748 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001749 {
1750 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001751 {
1752 if (greedy)
1753 EMIT(NFA_STAR);
1754 else
1755 EMIT(NFA_STAR_NONGREEDY);
1756 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001757 else
1758 EMIT(quest);
1759 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001760 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001761 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001762 if (i + 1 > minval && maxval == MAX_LIMIT)
1763 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001764 }
1765
1766 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001767 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001768 curchr = -1;
1769
1770 break;
1771
1772
1773 default:
1774 break;
1775 } /* end switch */
1776
1777 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001779 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780
1781 return OK;
1782}
1783
1784/*
1785 * Parse one or more pieces, concatenated. It matches a match for the
1786 * first piece, followed by a match for the second piece, etc. Example:
1787 * "f[0-9]b", first matches "f", then a digit and then "b".
1788 *
1789 * concat ::= piece
1790 * or piece piece
1791 * or piece piece piece
1792 * etc.
1793 */
1794 static int
1795nfa_regconcat()
1796{
1797 int cont = TRUE;
1798 int first = TRUE;
1799
1800 while (cont)
1801 {
1802 switch (peekchr())
1803 {
1804 case NUL:
1805 case Magic('|'):
1806 case Magic('&'):
1807 case Magic(')'):
1808 cont = FALSE;
1809 break;
1810
1811 case Magic('Z'):
1812#ifdef FEAT_MBYTE
1813 regflags |= RF_ICOMBINE;
1814#endif
1815 skipchr_keepstart();
1816 break;
1817 case Magic('c'):
1818 regflags |= RF_ICASE;
1819 skipchr_keepstart();
1820 break;
1821 case Magic('C'):
1822 regflags |= RF_NOICASE;
1823 skipchr_keepstart();
1824 break;
1825 case Magic('v'):
1826 reg_magic = MAGIC_ALL;
1827 skipchr_keepstart();
1828 curchr = -1;
1829 break;
1830 case Magic('m'):
1831 reg_magic = MAGIC_ON;
1832 skipchr_keepstart();
1833 curchr = -1;
1834 break;
1835 case Magic('M'):
1836 reg_magic = MAGIC_OFF;
1837 skipchr_keepstart();
1838 curchr = -1;
1839 break;
1840 case Magic('V'):
1841 reg_magic = MAGIC_NONE;
1842 skipchr_keepstart();
1843 curchr = -1;
1844 break;
1845
1846 default:
1847 if (nfa_regpiece() == FAIL)
1848 return FAIL;
1849 if (first == FALSE)
1850 EMIT(NFA_CONCAT);
1851 else
1852 first = FALSE;
1853 break;
1854 }
1855 }
1856
1857 return OK;
1858}
1859
1860/*
1861 * Parse a branch, one or more concats, separated by "\&". It matches the
1862 * last concat, but only if all the preceding concats also match at the same
1863 * position. Examples:
1864 * "foobeep\&..." matches "foo" in "foobeep".
1865 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1866 *
1867 * branch ::= concat
1868 * or concat \& concat
1869 * or concat \& concat \& concat
1870 * etc.
1871 */
1872 static int
1873nfa_regbranch()
1874{
1875 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001876 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877
Bram Moolenaar16299b52013-05-30 18:45:23 +02001878 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001879
1880 /* First branch, possibly the only one */
1881 if (nfa_regconcat() == FAIL)
1882 return FAIL;
1883
1884 ch = peekchr();
1885 /* Try next concats */
1886 while (ch == Magic('&'))
1887 {
1888 skipchr();
1889 EMIT(NFA_NOPEN);
1890 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001891 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001892 if (nfa_regconcat() == FAIL)
1893 return FAIL;
1894 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001895 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 EMIT(NFA_SKIP_CHAR);
1897 EMIT(NFA_CONCAT);
1898 ch = peekchr();
1899 }
1900
1901 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001902 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903 EMIT(NFA_SKIP_CHAR);
1904
1905 return OK;
1906}
1907
1908/*
1909 * Parse a pattern, one or more branches, separated by "\|". It matches
1910 * anything that matches one of the branches. Example: "foo\|beep" matches
1911 * "foo" and matches "beep". If more than one branch matches, the first one
1912 * is used.
1913 *
1914 * pattern ::= branch
1915 * or branch \| branch
1916 * or branch \| branch \| branch
1917 * etc.
1918 */
1919 static int
1920nfa_reg(paren)
1921 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1922{
1923 int parno = 0;
1924
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 if (paren == REG_PAREN)
1926 {
1927 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929 parno = regnpar++;
1930 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001931#ifdef FEAT_SYN_HL
1932 else if (paren == REG_ZPAREN)
1933 {
1934 /* Make a ZOPEN node. */
1935 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001936 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001937 parno = regnzpar++;
1938 }
1939#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940
1941 if (nfa_regbranch() == FAIL)
1942 return FAIL; /* cascaded error */
1943
1944 while (peekchr() == Magic('|'))
1945 {
1946 skipchr();
1947 if (nfa_regbranch() == FAIL)
1948 return FAIL; /* cascaded error */
1949 EMIT(NFA_OR);
1950 }
1951
1952 /* Check for proper termination. */
1953 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1954 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001955 if (paren == REG_NPAREN)
1956 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1957 else
1958 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1959 }
1960 else if (paren == REG_NOPAREN && peekchr() != NUL)
1961 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 if (peekchr() == Magic(')'))
1963 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1964 else
1965 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1966 }
1967 /*
1968 * Here we set the flag allowing back references to this set of
1969 * parentheses.
1970 */
1971 if (paren == REG_PAREN)
1972 {
1973 had_endbrace[parno] = TRUE; /* have seen the close paren */
1974 EMIT(NFA_MOPEN + parno);
1975 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001976#ifdef FEAT_SYN_HL
1977 else if (paren == REG_ZPAREN)
1978 EMIT(NFA_ZOPEN + parno);
1979#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001980
1981 return OK;
1982}
1983
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001984#ifdef DEBUG
1985static char_u code[50];
1986
1987 static void
1988nfa_set_code(c)
1989 int c;
1990{
1991 int addnl = FALSE;
1992
1993 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1994 {
1995 addnl = TRUE;
1996 c -= ADD_NL;
1997 }
1998
1999 STRCPY(code, "");
2000 switch (c)
2001 {
2002 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2003 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2004 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2005 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2006 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2007 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2008
Bram Moolenaar5714b802013-05-28 22:03:20 +02002009 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2010 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2011 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2012 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2013 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2014 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2015 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2016 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2017 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002018#ifdef FEAT_SYN_HL
2019 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2020 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2021 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2022 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2023 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2024 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2025 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2026 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2027 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2028#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002029 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2030
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002031 case NFA_PREV_ATOM_NO_WIDTH:
2032 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002033 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2034 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002035 case NFA_PREV_ATOM_JUST_BEFORE:
2036 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2037 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2038 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002039 case NFA_PREV_ATOM_LIKE_PATTERN:
2040 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2041
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002042 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2043 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002045 case NFA_START_INVISIBLE_NEG:
2046 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002047 case NFA_START_INVISIBLE_BEFORE:
2048 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002049 case NFA_START_INVISIBLE_BEFORE_NEG:
2050 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002051 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002052 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002053 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002054 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002055
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2057 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002058 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002060 case NFA_MOPEN:
2061 case NFA_MOPEN1:
2062 case NFA_MOPEN2:
2063 case NFA_MOPEN3:
2064 case NFA_MOPEN4:
2065 case NFA_MOPEN5:
2066 case NFA_MOPEN6:
2067 case NFA_MOPEN7:
2068 case NFA_MOPEN8:
2069 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 STRCPY(code, "NFA_MOPEN(x)");
2071 code[10] = c - NFA_MOPEN + '0';
2072 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002073 case NFA_MCLOSE:
2074 case NFA_MCLOSE1:
2075 case NFA_MCLOSE2:
2076 case NFA_MCLOSE3:
2077 case NFA_MCLOSE4:
2078 case NFA_MCLOSE5:
2079 case NFA_MCLOSE6:
2080 case NFA_MCLOSE7:
2081 case NFA_MCLOSE8:
2082 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 STRCPY(code, "NFA_MCLOSE(x)");
2084 code[11] = c - NFA_MCLOSE + '0';
2085 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002086#ifdef FEAT_SYN_HL
2087 case NFA_ZOPEN:
2088 case NFA_ZOPEN1:
2089 case NFA_ZOPEN2:
2090 case NFA_ZOPEN3:
2091 case NFA_ZOPEN4:
2092 case NFA_ZOPEN5:
2093 case NFA_ZOPEN6:
2094 case NFA_ZOPEN7:
2095 case NFA_ZOPEN8:
2096 case NFA_ZOPEN9:
2097 STRCPY(code, "NFA_ZOPEN(x)");
2098 code[10] = c - NFA_ZOPEN + '0';
2099 break;
2100 case NFA_ZCLOSE:
2101 case NFA_ZCLOSE1:
2102 case NFA_ZCLOSE2:
2103 case NFA_ZCLOSE3:
2104 case NFA_ZCLOSE4:
2105 case NFA_ZCLOSE5:
2106 case NFA_ZCLOSE6:
2107 case NFA_ZCLOSE7:
2108 case NFA_ZCLOSE8:
2109 case NFA_ZCLOSE9:
2110 STRCPY(code, "NFA_ZCLOSE(x)");
2111 code[11] = c - NFA_ZCLOSE + '0';
2112 break;
2113#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002114 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2115 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2116 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2117 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002118 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2119 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002120 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2121 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2122 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2123 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2124 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2125 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2126 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2127 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2128 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2129 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2130 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2131 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2132 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2133 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2134
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002135 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002136 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2137 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2138 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002139 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2140 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002141
2142 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2143 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2144 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2145 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2146 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2147 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2148 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2149
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2151 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2152 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2153 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2154 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2155 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2156 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2157 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2158 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2159 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2160 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2161 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2162 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2163 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2164 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2165 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2166
2167 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2168 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2169 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2170 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2171 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2172 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2173 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2174 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2175 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2176 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2177 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2178 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2179 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2180 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2181 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2182 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2183 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2184 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2185 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2186 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2187 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2188 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2189 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2190 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2191 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2192 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2193 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2194
2195 default:
2196 STRCPY(code, "CHAR(x)");
2197 code[5] = c;
2198 }
2199
2200 if (addnl == TRUE)
2201 STRCAT(code, " + NEWLINE ");
2202
2203}
2204
2205#ifdef ENABLE_LOG
2206static FILE *log_fd;
2207
2208/*
2209 * Print the postfix notation of the current regexp.
2210 */
2211 static void
2212nfa_postfix_dump(expr, retval)
2213 char_u *expr;
2214 int retval;
2215{
2216 int *p;
2217 FILE *f;
2218
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002219 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 if (f != NULL)
2221 {
2222 fprintf(f, "\n-------------------------\n");
2223 if (retval == FAIL)
2224 fprintf(f, ">>> NFA engine failed ... \n");
2225 else if (retval == OK)
2226 fprintf(f, ">>> NFA engine succeeded !\n");
2227 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002228 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229 {
2230 nfa_set_code(*p);
2231 fprintf(f, "%s, ", code);
2232 }
2233 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002234 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002235 fprintf(f, "%d ", *p);
2236 fprintf(f, "\n\n");
2237 fclose(f);
2238 }
2239}
2240
2241/*
2242 * Print the NFA starting with a root node "state".
2243 */
2244 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002245nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002246 FILE *debugf;
2247 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002248{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002249 garray_T indent;
2250
2251 ga_init2(&indent, 1, 64);
2252 ga_append(&indent, '\0');
2253 nfa_print_state2(debugf, state, &indent);
2254 ga_clear(&indent);
2255}
2256
2257 static void
2258nfa_print_state2(debugf, state, indent)
2259 FILE *debugf;
2260 nfa_state_T *state;
2261 garray_T *indent;
2262{
2263 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002264
2265 if (state == NULL)
2266 return;
2267
2268 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002269
2270 /* Output indent */
2271 p = (char_u *)indent->ga_data;
2272 if (indent->ga_len >= 3)
2273 {
2274 int last = indent->ga_len - 3;
2275 char_u save[2];
2276
2277 STRNCPY(save, &p[last], 2);
2278 STRNCPY(&p[last], "+-", 2);
2279 fprintf(debugf, " %s", p);
2280 STRNCPY(&p[last], save, 2);
2281 }
2282 else
2283 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002284
2285 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002286 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002287 code,
2288 state->c,
2289 abs(state->id),
2290 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002291 if (state->id < 0)
2292 return;
2293
2294 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002295
2296 /* grow indent for state->out */
2297 indent->ga_len -= 1;
2298 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002299 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002300 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002301 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002302 ga_append(indent, '\0');
2303
2304 nfa_print_state2(debugf, state->out, indent);
2305
2306 /* replace last part of indent for state->out1 */
2307 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002308 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002309 ga_append(indent, '\0');
2310
2311 nfa_print_state2(debugf, state->out1, indent);
2312
2313 /* shrink indent */
2314 indent->ga_len -= 3;
2315 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002316}
2317
2318/*
2319 * Print the NFA state machine.
2320 */
2321 static void
2322nfa_dump(prog)
2323 nfa_regprog_T *prog;
2324{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002325 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002326
2327 if (debugf != NULL)
2328 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002329 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002330
Bram Moolenaar473de612013-06-08 18:19:48 +02002331 if (prog->reganch)
2332 fprintf(debugf, "reganch: %d\n", prog->reganch);
2333 if (prog->regstart != NUL)
2334 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2335 prog->regstart, prog->regstart);
2336 if (prog->match_text != NULL)
2337 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 fclose(debugf);
2340 }
2341}
2342#endif /* ENABLE_LOG */
2343#endif /* DEBUG */
2344
2345/*
2346 * Parse r.e. @expr and convert it into postfix form.
2347 * Return the postfix string on success, NULL otherwise.
2348 */
2349 static int *
2350re2post()
2351{
2352 if (nfa_reg(REG_NOPAREN) == FAIL)
2353 return NULL;
2354 EMIT(NFA_MOPEN);
2355 return post_start;
2356}
2357
2358/* NB. Some of the code below is inspired by Russ's. */
2359
2360/*
2361 * Represents an NFA state plus zero or one or two arrows exiting.
2362 * if c == MATCH, no arrows out; matching state.
2363 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2364 * If c < 256, labeled arrow with character c to out.
2365 */
2366
2367static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2368
2369/*
2370 * Allocate and initialize nfa_state_T.
2371 */
2372 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002373alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002374 int c;
2375 nfa_state_T *out;
2376 nfa_state_T *out1;
2377{
2378 nfa_state_T *s;
2379
2380 if (istate >= nstate)
2381 return NULL;
2382
2383 s = &state_ptr[istate++];
2384
2385 s->c = c;
2386 s->out = out;
2387 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002388 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389
2390 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002391 s->lastlist[0] = 0;
2392 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002393
2394 return s;
2395}
2396
2397/*
2398 * A partially built NFA without the matching state filled in.
2399 * Frag_T.start points at the start state.
2400 * Frag_T.out is a list of places that need to be set to the
2401 * next state for this fragment.
2402 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002403
2404/* Since the out pointers in the list are always
2405 * uninitialized, we use the pointers themselves
2406 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002407typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002408union Ptrlist
2409{
2410 Ptrlist *next;
2411 nfa_state_T *s;
2412};
2413
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002414struct Frag
2415{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002416 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002417 Ptrlist *out;
2418};
2419typedef struct Frag Frag_T;
2420
2421static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2422static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2423static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2424static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2425static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2426static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2427
2428/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002429 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 */
2431 static Frag_T
2432frag(start, out)
2433 nfa_state_T *start;
2434 Ptrlist *out;
2435{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002436 Frag_T n;
2437
2438 n.start = start;
2439 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440 return n;
2441}
2442
2443/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 * Create singleton list containing just outp.
2445 */
2446 static Ptrlist *
2447list1(outp)
2448 nfa_state_T **outp;
2449{
2450 Ptrlist *l;
2451
2452 l = (Ptrlist *)outp;
2453 l->next = NULL;
2454 return l;
2455}
2456
2457/*
2458 * Patch the list of states at out to point to start.
2459 */
2460 static void
2461patch(l, s)
2462 Ptrlist *l;
2463 nfa_state_T *s;
2464{
2465 Ptrlist *next;
2466
2467 for (; l; l = next)
2468 {
2469 next = l->next;
2470 l->s = s;
2471 }
2472}
2473
2474
2475/*
2476 * Join the two lists l1 and l2, returning the combination.
2477 */
2478 static Ptrlist *
2479append(l1, l2)
2480 Ptrlist *l1;
2481 Ptrlist *l2;
2482{
2483 Ptrlist *oldl1;
2484
2485 oldl1 = l1;
2486 while (l1->next)
2487 l1 = l1->next;
2488 l1->next = l2;
2489 return oldl1;
2490}
2491
2492/*
2493 * Stack used for transforming postfix form into NFA.
2494 */
2495static Frag_T empty;
2496
2497 static void
2498st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002499 int *postfix UNUSED;
2500 int *end UNUSED;
2501 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002502{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002503#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504 FILE *df;
2505 int *p2;
2506
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002507 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002508 if (df)
2509 {
2510 fprintf(df, "Error popping the stack!\n");
2511#ifdef DEBUG
2512 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2513#endif
2514 fprintf(df, "Postfix form is: ");
2515#ifdef DEBUG
2516 for (p2 = postfix; p2 < end; p2++)
2517 {
2518 nfa_set_code(*p2);
2519 fprintf(df, "%s, ", code);
2520 }
2521 nfa_set_code(*p);
2522 fprintf(df, "\nCurrent position is: ");
2523 for (p2 = postfix; p2 <= p; p2 ++)
2524 {
2525 nfa_set_code(*p2);
2526 fprintf(df, "%s, ", code);
2527 }
2528#else
2529 for (p2 = postfix; p2 < end; p2++)
2530 {
2531 fprintf(df, "%d, ", *p2);
2532 }
2533 fprintf(df, "\nCurrent position is: ");
2534 for (p2 = postfix; p2 <= p; p2 ++)
2535 {
2536 fprintf(df, "%d, ", *p2);
2537 }
2538#endif
2539 fprintf(df, "\n--------------------------\n");
2540 fclose(df);
2541 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002542#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 EMSG(_("E874: (NFA) Could not pop the stack !"));
2544}
2545
2546/*
2547 * Push an item onto the stack.
2548 */
2549 static void
2550st_push(s, p, stack_end)
2551 Frag_T s;
2552 Frag_T **p;
2553 Frag_T *stack_end;
2554{
2555 Frag_T *stackp = *p;
2556
2557 if (stackp >= stack_end)
2558 return;
2559 *stackp = s;
2560 *p = *p + 1;
2561}
2562
2563/*
2564 * Pop an item from the stack.
2565 */
2566 static Frag_T
2567st_pop(p, stack)
2568 Frag_T **p;
2569 Frag_T *stack;
2570{
2571 Frag_T *stackp;
2572
2573 *p = *p - 1;
2574 stackp = *p;
2575 if (stackp < stack)
2576 return empty;
2577 return **p;
2578}
2579
2580/*
2581 * Convert a postfix form into its equivalent NFA.
2582 * Return the NFA start state on success, NULL otherwise.
2583 */
2584 static nfa_state_T *
2585post2nfa(postfix, end, nfa_calc_size)
2586 int *postfix;
2587 int *end;
2588 int nfa_calc_size;
2589{
2590 int *p;
2591 int mopen;
2592 int mclose;
2593 Frag_T *stack = NULL;
2594 Frag_T *stackp = NULL;
2595 Frag_T *stack_end = NULL;
2596 Frag_T e1;
2597 Frag_T e2;
2598 Frag_T e;
2599 nfa_state_T *s;
2600 nfa_state_T *s1;
2601 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002602 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002603
2604 if (postfix == NULL)
2605 return NULL;
2606
Bram Moolenaar053bb602013-05-20 13:55:21 +02002607#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608#define POP() st_pop(&stackp, stack); \
2609 if (stackp < stack) \
2610 { \
2611 st_error(postfix, end, p); \
2612 return NULL; \
2613 }
2614
2615 if (nfa_calc_size == FALSE)
2616 {
2617 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002618 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002619 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002620 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002621 }
2622
2623 for (p = postfix; p < end; ++p)
2624 {
2625 switch (*p)
2626 {
2627 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002628 /* Concatenation.
2629 * Pay attention: this operator does not exist in the r.e. itself
2630 * (it is implicit, really). It is added when r.e. is translated
2631 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002632 if (nfa_calc_size == TRUE)
2633 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002634 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635 break;
2636 }
2637 e2 = POP();
2638 e1 = POP();
2639 patch(e1.out, e2.start);
2640 PUSH(frag(e1.start, e2.out));
2641 break;
2642
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 case NFA_OR:
2644 /* Alternation */
2645 if (nfa_calc_size == TRUE)
2646 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002647 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 break;
2649 }
2650 e2 = POP();
2651 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002652 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002653 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002654 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002655 PUSH(frag(s, append(e1.out, e2.out)));
2656 break;
2657
2658 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002659 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002660 if (nfa_calc_size == TRUE)
2661 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002662 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002663 break;
2664 }
2665 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002666 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002667 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002668 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002669 patch(e.out, s);
2670 PUSH(frag(s, list1(&s->out1)));
2671 break;
2672
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002673 case NFA_STAR_NONGREEDY:
2674 /* Zero or more, prefer zero */
2675 if (nfa_calc_size == TRUE)
2676 {
2677 nstate++;
2678 break;
2679 }
2680 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002681 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002682 if (s == NULL)
2683 goto theend;
2684 patch(e.out, s);
2685 PUSH(frag(s, list1(&s->out)));
2686 break;
2687
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002688 case NFA_QUEST:
2689 /* one or zero atoms=> greedy match */
2690 if (nfa_calc_size == TRUE)
2691 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002692 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002693 break;
2694 }
2695 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002696 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002698 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699 PUSH(frag(s, append(e.out, list1(&s->out1))));
2700 break;
2701
2702 case NFA_QUEST_NONGREEDY:
2703 /* zero or one atoms => non-greedy match */
2704 if (nfa_calc_size == TRUE)
2705 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002706 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002707 break;
2708 }
2709 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002710 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002711 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002712 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002713 PUSH(frag(s, append(e.out, list1(&s->out))));
2714 break;
2715
Bram Moolenaar417bad22013-06-07 14:08:30 +02002716 case NFA_END_COLL:
2717 case NFA_END_NEG_COLL:
2718 /* On the stack is the sequence starting with NFA_START_COLL or
2719 * NFA_START_NEG_COLL and all possible characters. Patch it to
2720 * add the output to the start. */
2721 if (nfa_calc_size == TRUE)
2722 {
2723 nstate++;
2724 break;
2725 }
2726 e = POP();
2727 s = alloc_state(NFA_END_COLL, NULL, NULL);
2728 if (s == NULL)
2729 goto theend;
2730 patch(e.out, s);
2731 e.start->out1 = s;
2732 PUSH(frag(e.start, list1(&s->out)));
2733 break;
2734
2735 case NFA_RANGE:
2736 /* Before this are two characters, the low and high end of a
2737 * range. Turn them into two states with MIN and MAX. */
2738 if (nfa_calc_size == TRUE)
2739 {
2740 /* nstate += 0; */
2741 break;
2742 }
2743 e2 = POP();
2744 e1 = POP();
2745 e2.start->val = e2.start->c;
2746 e2.start->c = NFA_RANGE_MAX;
2747 e1.start->val = e1.start->c;
2748 e1.start->c = NFA_RANGE_MIN;
2749 patch(e1.out, e2.start);
2750 PUSH(frag(e1.start, e2.out));
2751 break;
2752
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002753 case NFA_SKIP_CHAR:
2754 /* Symbol of 0-length, Used in a repetition
2755 * with max/min count of 0 */
2756 if (nfa_calc_size == TRUE)
2757 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002758 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002759 break;
2760 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002761 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002763 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002764 PUSH(frag(s, list1(&s->out)));
2765 break;
2766
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002767 case NFA_OPT_CHARS:
2768 {
2769 int n;
2770
2771 /* \%[abc] */
2772 n = *++p; /* get number of characters */
2773 if (nfa_calc_size == TRUE)
2774 {
2775 nstate += n;
2776 break;
2777 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002778 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002779 e1.out = NULL; /* stores list with out1's */
2780 s1 = NULL; /* previous NFA_SPLIT to connect to */
2781 while (n-- > 0)
2782 {
2783 e = POP(); /* get character */
2784 s = alloc_state(NFA_SPLIT, e.start, NULL);
2785 if (s == NULL)
2786 goto theend;
2787 if (e1.out == NULL)
2788 e1 = e;
2789 patch(e.out, s1);
2790 append(e1.out, list1(&s->out1));
2791 s1 = s;
2792 }
2793 PUSH(frag(s, e1.out));
2794 break;
2795 }
2796
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002797 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002798 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002799 case NFA_PREV_ATOM_JUST_BEFORE:
2800 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002801 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002802 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002803 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2804 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002805 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002806 int start_state;
2807 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02002808 int n = 0;
2809 nfa_state_T *zend;
2810 nfa_state_T *skip;
2811
Bram Moolenaardecd9542013-06-07 16:31:50 +02002812 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02002813 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02002814 case NFA_PREV_ATOM_NO_WIDTH:
2815 start_state = NFA_START_INVISIBLE;
2816 end_state = NFA_END_INVISIBLE;
2817 break;
2818 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2819 start_state = NFA_START_INVISIBLE_NEG;
2820 end_state = NFA_END_INVISIBLE_NEG;
2821 break;
2822 case NFA_PREV_ATOM_JUST_BEFORE:
2823 start_state = NFA_START_INVISIBLE_BEFORE;
2824 end_state = NFA_END_INVISIBLE;
2825 break;
2826 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2827 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
2828 end_state = NFA_END_INVISIBLE_NEG;
2829 break;
2830 case NFA_PREV_ATOM_LIKE_PATTERN:
2831 start_state = NFA_START_PATTERN;
2832 end_state = NFA_END_PATTERN;
2833 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002834 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002835
2836 if (before)
2837 n = *++p; /* get the count */
2838
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002839 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002840 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002841 * The \@<= operator: match for the preceding atom.
2842 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002843 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002844 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845
2846 if (nfa_calc_size == TRUE)
2847 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002848 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849 break;
2850 }
2851 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002852 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002853 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002854 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002855
Bram Moolenaar87953742013-06-05 18:52:40 +02002856 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002857 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002858 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002859 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002860 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002861 if (pattern)
2862 {
2863 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2864 skip = alloc_state(NFA_SKIP, NULL, NULL);
2865 zend = alloc_state(NFA_ZEND, s1, NULL);
2866 s1->out= skip;
2867 patch(e.out, zend);
2868 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002869 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002870 else
2871 {
2872 patch(e.out, s1);
2873 PUSH(frag(s, list1(&s1->out)));
2874 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002876 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002878#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002879 case NFA_COMPOSING: /* char with composing char */
2880#if 0
2881 /* TODO */
2882 if (regflags & RF_ICOMBINE)
2883 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002884 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002885 }
2886#endif
2887 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002888#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002889
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002890 case NFA_MOPEN: /* \( \) Submatch */
2891 case NFA_MOPEN1:
2892 case NFA_MOPEN2:
2893 case NFA_MOPEN3:
2894 case NFA_MOPEN4:
2895 case NFA_MOPEN5:
2896 case NFA_MOPEN6:
2897 case NFA_MOPEN7:
2898 case NFA_MOPEN8:
2899 case NFA_MOPEN9:
2900#ifdef FEAT_SYN_HL
2901 case NFA_ZOPEN: /* \z( \) Submatch */
2902 case NFA_ZOPEN1:
2903 case NFA_ZOPEN2:
2904 case NFA_ZOPEN3:
2905 case NFA_ZOPEN4:
2906 case NFA_ZOPEN5:
2907 case NFA_ZOPEN6:
2908 case NFA_ZOPEN7:
2909 case NFA_ZOPEN8:
2910 case NFA_ZOPEN9:
2911#endif
2912 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002913 if (nfa_calc_size == TRUE)
2914 {
2915 nstate += 2;
2916 break;
2917 }
2918
2919 mopen = *p;
2920 switch (*p)
2921 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002922 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2923#ifdef FEAT_SYN_HL
2924 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2925 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2926 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2927 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2928 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2929 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2930 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2931 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2932 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2933 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2934#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002935#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002936 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002937#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002939 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 mclose = *p + NSUBEXP;
2941 break;
2942 }
2943
2944 /* Allow "NFA_MOPEN" as a valid postfix representation for
2945 * the empty regexp "". In this case, the NFA will be
2946 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2947 * empty groups of parenthesis, and empty mbyte chars */
2948 if (stackp == stack)
2949 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002950 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002951 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002952 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002953 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002954 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002955 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956 patch(list1(&s->out), s1);
2957 PUSH(frag(s, list1(&s1->out)));
2958 break;
2959 }
2960
2961 /* At least one node was emitted before NFA_MOPEN, so
2962 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2963 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002964 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002965 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002966 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002967
Bram Moolenaar525666f2013-06-02 16:40:55 +02002968 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002969 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002970 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002971 patch(e.out, s1);
2972
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002973#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002974 if (mopen == NFA_COMPOSING)
2975 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002977#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002978
2979 PUSH(frag(s, list1(&s1->out)));
2980 break;
2981
Bram Moolenaar5714b802013-05-28 22:03:20 +02002982 case NFA_BACKREF1:
2983 case NFA_BACKREF2:
2984 case NFA_BACKREF3:
2985 case NFA_BACKREF4:
2986 case NFA_BACKREF5:
2987 case NFA_BACKREF6:
2988 case NFA_BACKREF7:
2989 case NFA_BACKREF8:
2990 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002991#ifdef FEAT_SYN_HL
2992 case NFA_ZREF1:
2993 case NFA_ZREF2:
2994 case NFA_ZREF3:
2995 case NFA_ZREF4:
2996 case NFA_ZREF5:
2997 case NFA_ZREF6:
2998 case NFA_ZREF7:
2999 case NFA_ZREF8:
3000 case NFA_ZREF9:
3001#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003002 if (nfa_calc_size == TRUE)
3003 {
3004 nstate += 2;
3005 break;
3006 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003007 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003008 if (s == NULL)
3009 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003010 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003011 if (s1 == NULL)
3012 goto theend;
3013 patch(list1(&s->out), s1);
3014 PUSH(frag(s, list1(&s1->out)));
3015 break;
3016
Bram Moolenaar423532e2013-05-29 21:14:42 +02003017 case NFA_LNUM:
3018 case NFA_LNUM_GT:
3019 case NFA_LNUM_LT:
3020 case NFA_VCOL:
3021 case NFA_VCOL_GT:
3022 case NFA_VCOL_LT:
3023 case NFA_COL:
3024 case NFA_COL_GT:
3025 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003026 case NFA_MARK:
3027 case NFA_MARK_GT:
3028 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003029 {
3030 int n = *++p; /* lnum, col or mark name */
3031
Bram Moolenaar423532e2013-05-29 21:14:42 +02003032 if (nfa_calc_size == TRUE)
3033 {
3034 nstate += 1;
3035 break;
3036 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003037 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003038 if (s == NULL)
3039 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003040 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003041 PUSH(frag(s, list1(&s->out)));
3042 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003043 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003044
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045 case NFA_ZSTART:
3046 case NFA_ZEND:
3047 default:
3048 /* Operands */
3049 if (nfa_calc_size == TRUE)
3050 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003051 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003052 break;
3053 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003054 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003055 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003056 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003057 PUSH(frag(s, list1(&s->out)));
3058 break;
3059
3060 } /* switch(*p) */
3061
3062 } /* for(p = postfix; *p; ++p) */
3063
3064 if (nfa_calc_size == TRUE)
3065 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003066 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003067 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003068 }
3069
3070 e = POP();
3071 if (stackp != stack)
3072 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3073
3074 if (istate >= nstate)
3075 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3076
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003077 matchstate = &state_ptr[istate++]; /* the match state */
3078 matchstate->c = NFA_MATCH;
3079 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003080 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003081
3082 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003083 ret = e.start;
3084
3085theend:
3086 vim_free(stack);
3087 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003088
3089#undef POP1
3090#undef PUSH1
3091#undef POP2
3092#undef PUSH2
3093#undef POP
3094#undef PUSH
3095}
3096
3097/****************************************************************
3098 * NFA execution code.
3099 ****************************************************************/
3100
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003101typedef struct
3102{
3103 int in_use; /* number of subexpr with useful info */
3104
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003105 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003106 union
3107 {
3108 struct multipos
3109 {
3110 lpos_T start;
3111 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003112 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003113 struct linepos
3114 {
3115 char_u *start;
3116 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003117 } line[NSUBEXP];
3118 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003119} regsub_T;
3120
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003121typedef struct
3122{
3123 regsub_T norm; /* \( .. \) matches */
3124#ifdef FEAT_SYN_HL
3125 regsub_T synt; /* \z( .. \) matches */
3126#endif
3127} regsubs_T;
3128
Bram Moolenaara2d95102013-06-04 14:23:05 +02003129/* nfa_pim_T stores a Postponed Invisible Match. */
3130typedef struct nfa_pim_S nfa_pim_T;
3131struct nfa_pim_S
3132{
3133 nfa_state_T *state;
3134 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
3135 nfa_pim_T *pim; /* another PIM at the same position */
3136 regsubs_T subs; /* submatch info, only party used */
3137};
3138
3139/* Values for done in nfa_pim_T. */
3140#define NFA_PIM_TODO 0
3141#define NFA_PIM_MATCH 1
3142#define NFA_PIM_NOMATCH -1
3143
3144
Bram Moolenaar963fee22013-05-26 21:47:28 +02003145/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003146typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003147{
3148 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003149 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003150 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003151 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003152} nfa_thread_T;
3153
Bram Moolenaar963fee22013-05-26 21:47:28 +02003154/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003155typedef struct
3156{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003157 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003158 int n; /* nr of states currently in "t" */
3159 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003160 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003161} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003162
Bram Moolenaar5714b802013-05-28 22:03:20 +02003163#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003164static void log_subsexpr __ARGS((regsubs_T *subs));
3165static void log_subexpr __ARGS((regsub_T *sub));
3166
3167 static void
3168log_subsexpr(subs)
3169 regsubs_T *subs;
3170{
3171 log_subexpr(&subs->norm);
3172# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003173 if (nfa_has_zsubexpr)
3174 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003175# endif
3176}
3177
Bram Moolenaar5714b802013-05-28 22:03:20 +02003178 static void
3179log_subexpr(sub)
3180 regsub_T *sub;
3181{
3182 int j;
3183
3184 for (j = 0; j < sub->in_use; j++)
3185 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003186 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003187 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003188 sub->list.multi[j].start.col,
3189 (int)sub->list.multi[j].start.lnum,
3190 sub->list.multi[j].end.col,
3191 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003192 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003193 {
3194 char *s = (char *)sub->list.line[j].start;
3195 char *e = (char *)sub->list.line[j].end;
3196
Bram Moolenaar87953742013-06-05 18:52:40 +02003197 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003198 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003199 s == NULL ? "NULL" : s,
3200 e == NULL ? "NULL" : e);
3201 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003202}
3203#endif
3204
Bram Moolenaar963fee22013-05-26 21:47:28 +02003205/* Used during execution: whether a match has been found. */
3206static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003207
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003208static void clear_sub __ARGS((regsub_T *sub));
3209static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3210static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003211static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003212static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
3213static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003214static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003215static 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 +02003216
3217 static void
3218clear_sub(sub)
3219 regsub_T *sub;
3220{
3221 if (REG_MULTI)
3222 /* Use 0xff to set lnum to -1 */
3223 vim_memset(sub->list.multi, 0xff,
3224 sizeof(struct multipos) * nfa_nsubexpr);
3225 else
3226 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3227 sub->in_use = 0;
3228}
3229
3230/*
3231 * Copy the submatches from "from" to "to".
3232 */
3233 static void
3234copy_sub(to, from)
3235 regsub_T *to;
3236 regsub_T *from;
3237{
3238 to->in_use = from->in_use;
3239 if (from->in_use > 0)
3240 {
3241 /* Copy the match start and end positions. */
3242 if (REG_MULTI)
3243 mch_memmove(&to->list.multi[0],
3244 &from->list.multi[0],
3245 sizeof(struct multipos) * from->in_use);
3246 else
3247 mch_memmove(&to->list.line[0],
3248 &from->list.line[0],
3249 sizeof(struct linepos) * from->in_use);
3250 }
3251}
3252
3253/*
3254 * Like copy_sub() but exclude the main match.
3255 */
3256 static void
3257copy_sub_off(to, from)
3258 regsub_T *to;
3259 regsub_T *from;
3260{
3261 if (to->in_use < from->in_use)
3262 to->in_use = from->in_use;
3263 if (from->in_use > 1)
3264 {
3265 /* Copy the match start and end positions. */
3266 if (REG_MULTI)
3267 mch_memmove(&to->list.multi[1],
3268 &from->list.multi[1],
3269 sizeof(struct multipos) * (from->in_use - 1));
3270 else
3271 mch_memmove(&to->list.line[1],
3272 &from->list.line[1],
3273 sizeof(struct linepos) * (from->in_use - 1));
3274 }
3275}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276
Bram Moolenaar428e9872013-05-30 17:05:39 +02003277/*
3278 * Return TRUE if "sub1" and "sub2" have the same positions.
3279 */
3280 static int
3281sub_equal(sub1, sub2)
3282 regsub_T *sub1;
3283 regsub_T *sub2;
3284{
3285 int i;
3286 int todo;
3287 linenr_T s1, e1;
3288 linenr_T s2, e2;
3289 char_u *sp1, *ep1;
3290 char_u *sp2, *ep2;
3291
3292 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3293 if (REG_MULTI)
3294 {
3295 for (i = 0; i < todo; ++i)
3296 {
3297 if (i < sub1->in_use)
3298 {
3299 s1 = sub1->list.multi[i].start.lnum;
3300 e1 = sub1->list.multi[i].end.lnum;
3301 }
3302 else
3303 {
3304 s1 = 0;
3305 e1 = 0;
3306 }
3307 if (i < sub2->in_use)
3308 {
3309 s2 = sub2->list.multi[i].start.lnum;
3310 e2 = sub2->list.multi[i].end.lnum;
3311 }
3312 else
3313 {
3314 s2 = 0;
3315 e2 = 0;
3316 }
3317 if (s1 != s2 || e1 != e2)
3318 return FALSE;
3319 if (s1 != 0 && sub1->list.multi[i].start.col
3320 != sub2->list.multi[i].start.col)
3321 return FALSE;
3322 if (e1 != 0 && sub1->list.multi[i].end.col
3323 != sub2->list.multi[i].end.col)
3324 return FALSE;
3325 }
3326 }
3327 else
3328 {
3329 for (i = 0; i < todo; ++i)
3330 {
3331 if (i < sub1->in_use)
3332 {
3333 sp1 = sub1->list.line[i].start;
3334 ep1 = sub1->list.line[i].end;
3335 }
3336 else
3337 {
3338 sp1 = NULL;
3339 ep1 = NULL;
3340 }
3341 if (i < sub2->in_use)
3342 {
3343 sp2 = sub2->list.line[i].start;
3344 ep2 = sub2->list.line[i].end;
3345 }
3346 else
3347 {
3348 sp2 = NULL;
3349 ep2 = NULL;
3350 }
3351 if (sp1 != sp2 || ep1 != ep2)
3352 return FALSE;
3353 }
3354 }
3355
3356 return TRUE;
3357}
3358
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003359#ifdef ENABLE_LOG
3360 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003361report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003362{
3363 int col;
3364
3365 if (sub->in_use <= 0)
3366 col = -1;
3367 else if (REG_MULTI)
3368 col = sub->list.multi[0].start.col;
3369 else
3370 col = (int)(sub->list.line[0].start - regline);
3371 nfa_set_code(state->c);
3372 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3373 action, abs(state->id), lid, state->c, code, col);
3374}
3375#endif
3376
Bram Moolenaar43e02982013-06-07 17:31:29 +02003377/*
3378 * Return TRUE if the same state is already in list "l" with the same
3379 * positions as "subs".
3380 */
3381 static int
3382has_state_with_pos(l, state, subs)
3383 nfa_list_T *l; /* runtime state list */
3384 nfa_state_T *state; /* state to update */
3385 regsubs_T *subs; /* pointers to subexpressions */
3386{
3387 nfa_thread_T *thread;
3388 int i;
3389
3390 for (i = 0; i < l->n; ++i)
3391 {
3392 thread = &l->t[i];
3393 if (thread->state->id == state->id
3394 && sub_equal(&thread->subs.norm, &subs->norm)
3395#ifdef FEAT_SYN_HL
3396 && (!nfa_has_zsubexpr ||
3397 sub_equal(&thread->subs.synt, &subs->synt))
3398#endif
3399 )
3400 return TRUE;
3401 }
3402 return FALSE;
3403}
3404
3405/*
3406 * Return TRUE if "state" is already in list "l".
3407 */
3408 static int
3409state_in_list(l, state, subs)
3410 nfa_list_T *l; /* runtime state list */
3411 nfa_state_T *state; /* state to update */
3412 regsubs_T *subs; /* pointers to subexpressions */
3413{
3414 if (state->lastlist[nfa_ll_index] == l->id)
3415 {
3416 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3417 return TRUE;
3418 }
3419 return FALSE;
3420}
3421
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003423addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003424 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003425 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003426 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003427 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003429 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003430 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003431 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003432 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003433 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003434 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003435 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003436#ifdef ENABLE_LOG
3437 int did_print = FALSE;
3438#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439
3440 if (l == NULL || state == NULL)
3441 return;
3442
3443 switch (state->c)
3444 {
3445 case NFA_SPLIT:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003446 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003447 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448 case NFA_NCLOSE:
3449 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003450 case NFA_MCLOSE1:
3451 case NFA_MCLOSE2:
3452 case NFA_MCLOSE3:
3453 case NFA_MCLOSE4:
3454 case NFA_MCLOSE5:
3455 case NFA_MCLOSE6:
3456 case NFA_MCLOSE7:
3457 case NFA_MCLOSE8:
3458 case NFA_MCLOSE9:
3459#ifdef FEAT_SYN_HL
3460 case NFA_ZCLOSE:
3461 case NFA_ZCLOSE1:
3462 case NFA_ZCLOSE2:
3463 case NFA_ZCLOSE3:
3464 case NFA_ZCLOSE4:
3465 case NFA_ZCLOSE5:
3466 case NFA_ZCLOSE6:
3467 case NFA_ZCLOSE7:
3468 case NFA_ZCLOSE8:
3469 case NFA_ZCLOSE9:
3470#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003471 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003472 /* These nodes are not added themselves but their "out" and/or
3473 * "out1" may be added below. */
3474 break;
3475
3476 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003477 case NFA_MOPEN1:
3478 case NFA_MOPEN2:
3479 case NFA_MOPEN3:
3480 case NFA_MOPEN4:
3481 case NFA_MOPEN5:
3482 case NFA_MOPEN6:
3483 case NFA_MOPEN7:
3484 case NFA_MOPEN8:
3485 case NFA_MOPEN9:
3486#ifdef FEAT_SYN_HL
3487 case NFA_ZOPEN:
3488 case NFA_ZOPEN1:
3489 case NFA_ZOPEN2:
3490 case NFA_ZOPEN3:
3491 case NFA_ZOPEN4:
3492 case NFA_ZOPEN5:
3493 case NFA_ZOPEN6:
3494 case NFA_ZOPEN7:
3495 case NFA_ZOPEN8:
3496 case NFA_ZOPEN9:
3497#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003498 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003499 /* These nodes do not need to be added, but we need to bail out
3500 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003501 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003502 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003503 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 break;
3505
Bram Moolenaar307aa162013-06-02 16:34:21 +02003506 case NFA_BOL:
3507 case NFA_BOF:
3508 /* "^" won't match past end-of-line, don't bother trying.
3509 * Except when we are going to the next line for a look-behind
3510 * match. */
3511 if (reginput > regline
3512 && (nfa_endp == NULL
3513 || !REG_MULTI
3514 || reglnum == nfa_endp->se_u.pos.lnum))
3515 goto skip_add;
3516 /* FALLTHROUGH */
3517
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003519 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003521 /* This state is already in the list, don't add it again,
3522 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003523 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003524 {
3525skip_add:
3526#ifdef ENABLE_LOG
3527 nfa_set_code(state->c);
3528 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3529 abs(state->id), l->id, state->c, code);
3530#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003531 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003532 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003533
Bram Moolenaar43e02982013-06-07 17:31:29 +02003534 if (has_state_with_pos(l, state, subs))
3535 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003536 }
3537
Bram Moolenaara2d95102013-06-04 14:23:05 +02003538 /* when there are backreferences or look-behind matches the number
3539 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003540 if (nfa_has_backref && l->n == l->len)
3541 {
3542 int newlen = l->len * 3 / 2 + 50;
3543
3544 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3545 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003546 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003547
3548 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003549 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003550 thread = &l->t[l->n++];
3551 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003552 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003553 copy_sub(&thread->subs.norm, &subs->norm);
3554#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003555 if (nfa_has_zsubexpr)
3556 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003557#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003558#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003559 report_state("Adding", &thread->subs.norm, state, l->id);
3560 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003561#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003562 }
3563
3564#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003565 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003566 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003567#endif
3568 switch (state->c)
3569 {
3570 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003571 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003572 break;
3573
3574 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003575 /* order matters here */
3576 addstate(l, state->out, subs, off);
3577 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578 break;
3579
Bram Moolenaar5714b802013-05-28 22:03:20 +02003580 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003581 case NFA_NOPEN:
3582 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003583 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003584 break;
3585
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003586 case NFA_MOPEN:
3587 case NFA_MOPEN1:
3588 case NFA_MOPEN2:
3589 case NFA_MOPEN3:
3590 case NFA_MOPEN4:
3591 case NFA_MOPEN5:
3592 case NFA_MOPEN6:
3593 case NFA_MOPEN7:
3594 case NFA_MOPEN8:
3595 case NFA_MOPEN9:
3596#ifdef FEAT_SYN_HL
3597 case NFA_ZOPEN:
3598 case NFA_ZOPEN1:
3599 case NFA_ZOPEN2:
3600 case NFA_ZOPEN3:
3601 case NFA_ZOPEN4:
3602 case NFA_ZOPEN5:
3603 case NFA_ZOPEN6:
3604 case NFA_ZOPEN7:
3605 case NFA_ZOPEN8:
3606 case NFA_ZOPEN9:
3607#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003608 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003609 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003610 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003611 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003612 sub = &subs->norm;
3613 }
3614#ifdef FEAT_SYN_HL
3615 else if (state->c >= NFA_ZOPEN)
3616 {
3617 subidx = state->c - NFA_ZOPEN;
3618 sub = &subs->synt;
3619 }
3620#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003621 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003622 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003623 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003624 sub = &subs->norm;
3625 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003626
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003627 /* Set the position (with "off") in the subexpression. Save and
3628 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003629 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 if (REG_MULTI)
3631 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003632 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003633 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003634 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003635 save_in_use = -1;
3636 }
3637 else
3638 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003639 save_in_use = sub->in_use;
3640 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003641 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003642 sub->list.multi[i].start.lnum = -1;
3643 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003644 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003645 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003646 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003647 if (off == -1)
3648 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003649 sub->list.multi[subidx].start.lnum = reglnum + 1;
3650 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003651 }
3652 else
3653 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003654 sub->list.multi[subidx].start.lnum = reglnum;
3655 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003656 (colnr_T)(reginput - regline + off);
3657 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003658 }
3659 else
3660 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003661 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003662 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003663 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003664 save_in_use = -1;
3665 }
3666 else
3667 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003668 save_in_use = sub->in_use;
3669 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003670 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003671 sub->list.line[i].start = NULL;
3672 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003673 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003674 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003675 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003676 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003677 }
3678
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003679 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003680
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003681 if (save_in_use == -1)
3682 {
3683 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003684 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003685 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003686 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003687 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003689 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003690 break;
3691
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003692 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003693 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003695 /* Do not overwrite the position set by \ze. If no \ze
3696 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003697 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003698 break;
3699 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003700 case NFA_MCLOSE1:
3701 case NFA_MCLOSE2:
3702 case NFA_MCLOSE3:
3703 case NFA_MCLOSE4:
3704 case NFA_MCLOSE5:
3705 case NFA_MCLOSE6:
3706 case NFA_MCLOSE7:
3707 case NFA_MCLOSE8:
3708 case NFA_MCLOSE9:
3709#ifdef FEAT_SYN_HL
3710 case NFA_ZCLOSE:
3711 case NFA_ZCLOSE1:
3712 case NFA_ZCLOSE2:
3713 case NFA_ZCLOSE3:
3714 case NFA_ZCLOSE4:
3715 case NFA_ZCLOSE5:
3716 case NFA_ZCLOSE6:
3717 case NFA_ZCLOSE7:
3718 case NFA_ZCLOSE8:
3719 case NFA_ZCLOSE9:
3720#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003721 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003723 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003725 sub = &subs->norm;
3726 }
3727#ifdef FEAT_SYN_HL
3728 else if (state->c >= NFA_ZCLOSE)
3729 {
3730 subidx = state->c - NFA_ZCLOSE;
3731 sub = &subs->synt;
3732 }
3733#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003734 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003735 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003736 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003737 sub = &subs->norm;
3738 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003740 /* We don't fill in gaps here, there must have been an MOPEN that
3741 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003742 save_in_use = sub->in_use;
3743 if (sub->in_use <= subidx)
3744 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003745 if (REG_MULTI)
3746 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003747 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003748 if (off == -1)
3749 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003750 sub->list.multi[subidx].end.lnum = reglnum + 1;
3751 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003752 }
3753 else
3754 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003755 sub->list.multi[subidx].end.lnum = reglnum;
3756 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003757 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003758 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759 }
3760 else
3761 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003762 save_ptr = sub->list.line[subidx].end;
3763 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764 }
3765
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003766 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767
3768 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003769 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003770 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003771 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003772 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003773 break;
3774 }
3775}
3776
3777/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003778 * Like addstate(), but the new state(s) are put at position "*ip".
3779 * Used for zero-width matches, next state to use is the added one.
3780 * This makes sure the order of states to be tried does not change, which
3781 * matters for alternatives.
3782 */
3783 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003784addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003785 nfa_list_T *l; /* runtime state list */
3786 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003787 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003788 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003789 int *ip;
3790{
3791 int tlen = l->n;
3792 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003793 int listidx = *ip;
3794 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003795
3796 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003797 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003798
Bram Moolenaara2d95102013-06-04 14:23:05 +02003799 /* fill in the "pim" field in the new states */
3800 if (pim != NULL)
3801 for (i = tlen; i < l->n; ++i)
3802 l->t[i].pim = pim;
3803
Bram Moolenaar4b417062013-05-25 20:19:50 +02003804 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003805 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003806 return;
3807
3808 /* re-order to put the new state at the current position */
3809 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003810 if (count == 1)
3811 {
3812 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003813 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003814 }
3815 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003816 {
3817 /* make space for new states, then move them from the
3818 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003819 mch_memmove(&(l->t[listidx + count]),
3820 &(l->t[listidx + 1]),
3821 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3822 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003823 &(l->t[l->n - 1]),
3824 sizeof(nfa_thread_T) * count);
3825 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003826 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003827 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003828}
3829
3830/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831 * Check character class "class" against current character c.
3832 */
3833 static int
3834check_char_class(class, c)
3835 int class;
3836 int c;
3837{
3838 switch (class)
3839 {
3840 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003841 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003842 return OK;
3843 break;
3844 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003845 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846 return OK;
3847 break;
3848 case NFA_CLASS_BLANK:
3849 if (c == ' ' || c == '\t')
3850 return OK;
3851 break;
3852 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003853 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 return OK;
3855 break;
3856 case NFA_CLASS_DIGIT:
3857 if (VIM_ISDIGIT(c))
3858 return OK;
3859 break;
3860 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003861 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003862 return OK;
3863 break;
3864 case NFA_CLASS_LOWER:
3865 if (MB_ISLOWER(c))
3866 return OK;
3867 break;
3868 case NFA_CLASS_PRINT:
3869 if (vim_isprintc(c))
3870 return OK;
3871 break;
3872 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003873 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003874 return OK;
3875 break;
3876 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02003877 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003878 return OK;
3879 break;
3880 case NFA_CLASS_UPPER:
3881 if (MB_ISUPPER(c))
3882 return OK;
3883 break;
3884 case NFA_CLASS_XDIGIT:
3885 if (vim_isxdigit(c))
3886 return OK;
3887 break;
3888 case NFA_CLASS_TAB:
3889 if (c == '\t')
3890 return OK;
3891 break;
3892 case NFA_CLASS_RETURN:
3893 if (c == '\r')
3894 return OK;
3895 break;
3896 case NFA_CLASS_BACKSPACE:
3897 if (c == '\b')
3898 return OK;
3899 break;
3900 case NFA_CLASS_ESCAPE:
3901 if (c == '\033')
3902 return OK;
3903 break;
3904
3905 default:
3906 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02003907 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
3908 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003909 }
3910 return FAIL;
3911}
3912
Bram Moolenaar5714b802013-05-28 22:03:20 +02003913static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3914
3915/*
3916 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003917 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003918 */
3919 static int
3920match_backref(sub, subidx, bytelen)
3921 regsub_T *sub; /* pointers to subexpressions */
3922 int subidx;
3923 int *bytelen; /* out: length of match in bytes */
3924{
3925 int len;
3926
3927 if (sub->in_use <= subidx)
3928 {
3929retempty:
3930 /* backref was not set, match an empty string */
3931 *bytelen = 0;
3932 return TRUE;
3933 }
3934
3935 if (REG_MULTI)
3936 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003937 if (sub->list.multi[subidx].start.lnum < 0
3938 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003939 goto retempty;
3940 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003941 len = sub->list.multi[subidx].end.col
3942 - sub->list.multi[subidx].start.col;
3943 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003944 reginput, &len) == 0)
3945 {
3946 *bytelen = len;
3947 return TRUE;
3948 }
3949 }
3950 else
3951 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003952 if (sub->list.line[subidx].start == NULL
3953 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003954 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003955 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3956 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003957 {
3958 *bytelen = len;
3959 return TRUE;
3960 }
3961 }
3962 return FALSE;
3963}
3964
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003965#ifdef FEAT_SYN_HL
3966
3967static int match_zref __ARGS((int subidx, int *bytelen));
3968
3969/*
3970 * Check for a match with \z subexpression "subidx".
3971 * Return TRUE if it matches.
3972 */
3973 static int
3974match_zref(subidx, bytelen)
3975 int subidx;
3976 int *bytelen; /* out: length of match in bytes */
3977{
3978 int len;
3979
3980 cleanup_zsubexpr();
3981 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3982 {
3983 /* backref was not set, match an empty string */
3984 *bytelen = 0;
3985 return TRUE;
3986 }
3987
3988 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3989 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3990 {
3991 *bytelen = len;
3992 return TRUE;
3993 }
3994 return FALSE;
3995}
3996#endif
3997
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003998/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003999 * Save list IDs for all NFA states of "prog" into "list".
4000 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004001 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002 */
4003 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004004nfa_save_listids(prog, list)
4005 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004006 int *list;
4007{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004008 int i;
4009 nfa_state_T *p;
4010
4011 /* Order in the list is reverse, it's a bit faster that way. */
4012 p = &prog->state[0];
4013 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004014 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004015 list[i] = p->lastlist[1];
4016 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004017 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004018 }
4019}
4020
4021/*
4022 * Restore list IDs from "list" to all NFA states.
4023 */
4024 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004025nfa_restore_listids(prog, list)
4026 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004027 int *list;
4028{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004029 int i;
4030 nfa_state_T *p;
4031
4032 p = &prog->state[0];
4033 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004034 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004035 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004036 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037 }
4038}
4039
Bram Moolenaar423532e2013-05-29 21:14:42 +02004040 static int
4041nfa_re_num_cmp(val, op, pos)
4042 long_u val;
4043 int op;
4044 long_u pos;
4045{
4046 if (op == 1) return pos > val;
4047 if (op == 2) return pos < val;
4048 return val == pos;
4049}
4050
Bram Moolenaarf46da702013-06-02 22:37:42 +02004051static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004052static 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 +02004053
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004054/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004055 * Recursively call nfa_regmatch()
4056 */
4057 static int
4058recursive_regmatch(state, prog, submatch, m, listids)
4059 nfa_state_T *state;
4060 nfa_regprog_T *prog;
4061 regsubs_T *submatch;
4062 regsubs_T *m;
4063 int **listids;
4064{
4065 char_u *save_reginput = reginput;
4066 char_u *save_regline = regline;
4067 int save_reglnum = reglnum;
4068 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004069 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004070 save_se_T *save_nfa_endp = nfa_endp;
4071 save_se_T endpos;
4072 save_se_T *endposp = NULL;
4073 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004074 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004075
Bram Moolenaardecd9542013-06-07 16:31:50 +02004076 if (state->c == NFA_START_INVISIBLE_BEFORE
4077 || state->c == NFA_START_INVISIBLE_BEFORE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004078 {
4079 /* The recursive match must end at the current position. */
4080 endposp = &endpos;
4081 if (REG_MULTI)
4082 {
4083 endpos.se_u.pos.col = (int)(reginput - regline);
4084 endpos.se_u.pos.lnum = reglnum;
4085 }
4086 else
4087 endpos.se_u.ptr = reginput;
4088
4089 /* Go back the specified number of bytes, or as far as the
4090 * start of the previous line, to try matching "\@<=" or
4091 * not matching "\@<!".
4092 * TODO: This is very inefficient! Would be better to
4093 * first check for a match with what follows. */
4094 if (state->val <= 0)
4095 {
4096 if (REG_MULTI)
4097 {
4098 regline = reg_getline(--reglnum);
4099 if (regline == NULL)
4100 /* can't go before the first line */
4101 regline = reg_getline(++reglnum);
4102 }
4103 reginput = regline;
4104 }
4105 else
4106 {
4107 if (REG_MULTI && (int)(reginput - regline) < state->val)
4108 {
4109 /* Not enough bytes in this line, go to end of
4110 * previous line. */
4111 regline = reg_getline(--reglnum);
4112 if (regline == NULL)
4113 {
4114 /* can't go before the first line */
4115 regline = reg_getline(++reglnum);
4116 reginput = regline;
4117 }
4118 else
4119 reginput = regline + STRLEN(regline);
4120 }
4121 if ((int)(reginput - regline) >= state->val)
4122 {
4123 reginput -= state->val;
4124#ifdef FEAT_MBYTE
4125 if (has_mbyte)
4126 reginput -= mb_head_off(regline, reginput);
4127#endif
4128 }
4129 else
4130 reginput = regline;
4131 }
4132 }
4133
Bram Moolenaarf46da702013-06-02 22:37:42 +02004134#ifdef ENABLE_LOG
4135 if (log_fd != stderr)
4136 fclose(log_fd);
4137 log_fd = NULL;
4138#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004139 /* Have to clear the lastlist field of the NFA nodes, so that
4140 * nfa_regmatch() and addstate() can run properly after recursion. */
4141 if (nfa_ll_index == 1)
4142 {
4143 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4144 * values and clear them. */
4145 if (*listids == NULL)
4146 {
4147 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4148 if (*listids == NULL)
4149 {
4150 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4151 return 0;
4152 }
4153 }
4154 nfa_save_listids(prog, *listids);
4155 need_restore = TRUE;
4156 /* any value of nfa_listid will do */
4157 }
4158 else
4159 {
4160 /* First recursive nfa_regmatch() call, switch to the second lastlist
4161 * entry. Make sure nfa_listid is different from a previous recursive
4162 * call, because some states may still have this ID. */
4163 ++nfa_ll_index;
4164 if (nfa_listid <= nfa_alt_listid)
4165 nfa_listid = nfa_alt_listid;
4166 }
4167
4168 /* Call nfa_regmatch() to check if the current concat matches at this
4169 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004170 nfa_endp = endposp;
4171 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004172
4173 if (need_restore)
4174 nfa_restore_listids(prog, *listids);
4175 else
4176 {
4177 --nfa_ll_index;
4178 nfa_alt_listid = nfa_listid;
4179 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004180
4181 /* restore position in input text */
4182 reginput = save_reginput;
4183 regline = save_regline;
4184 reglnum = save_reglnum;
4185 nfa_match = save_nfa_match;
4186 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004187 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004188
4189#ifdef ENABLE_LOG
4190 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4191 if (log_fd != NULL)
4192 {
4193 fprintf(log_fd, "****************************\n");
4194 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4195 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4196 fprintf(log_fd, "****************************\n");
4197 }
4198 else
4199 {
4200 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4201 log_fd = stderr;
4202 }
4203#endif
4204
4205 return result;
4206}
4207
Bram Moolenaara2d95102013-06-04 14:23:05 +02004208static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004209static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004210static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004211
4212/*
4213 * Estimate the chance of a match with "state" failing.
4214 * NFA_ANY: 1
4215 * specific character: 99
4216 */
4217 static int
4218failure_chance(state, depth)
4219 nfa_state_T *state;
4220 int depth;
4221{
4222 int c = state->c;
4223 int l, r;
4224
4225 /* detect looping */
4226 if (depth > 4)
4227 return 1;
4228
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004229 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004230 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004231 case NFA_SPLIT:
4232 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4233 /* avoid recursive stuff */
4234 return 1;
4235 /* two alternatives, use the lowest failure chance */
4236 l = failure_chance(state->out, depth + 1);
4237 r = failure_chance(state->out1, depth + 1);
4238 return l < r ? l : r;
4239
4240 case NFA_ANY:
4241 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004242 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004243 case NFA_MATCH:
4244 /* empty match works always */
4245 return 0;
4246
4247 case NFA_BOL:
4248 case NFA_EOL:
4249 case NFA_BOF:
4250 case NFA_EOF:
4251 case NFA_NEWL:
4252 return 99;
4253
4254 case NFA_BOW:
4255 case NFA_EOW:
4256 return 90;
4257
4258 case NFA_MOPEN:
4259 case NFA_MOPEN1:
4260 case NFA_MOPEN2:
4261 case NFA_MOPEN3:
4262 case NFA_MOPEN4:
4263 case NFA_MOPEN5:
4264 case NFA_MOPEN6:
4265 case NFA_MOPEN7:
4266 case NFA_MOPEN8:
4267 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004268#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004269 case NFA_ZOPEN:
4270 case NFA_ZOPEN1:
4271 case NFA_ZOPEN2:
4272 case NFA_ZOPEN3:
4273 case NFA_ZOPEN4:
4274 case NFA_ZOPEN5:
4275 case NFA_ZOPEN6:
4276 case NFA_ZOPEN7:
4277 case NFA_ZOPEN8:
4278 case NFA_ZOPEN9:
4279 case NFA_ZCLOSE:
4280 case NFA_ZCLOSE1:
4281 case NFA_ZCLOSE2:
4282 case NFA_ZCLOSE3:
4283 case NFA_ZCLOSE4:
4284 case NFA_ZCLOSE5:
4285 case NFA_ZCLOSE6:
4286 case NFA_ZCLOSE7:
4287 case NFA_ZCLOSE8:
4288 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004289#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004290 case NFA_NOPEN:
4291 case NFA_MCLOSE:
4292 case NFA_MCLOSE1:
4293 case NFA_MCLOSE2:
4294 case NFA_MCLOSE3:
4295 case NFA_MCLOSE4:
4296 case NFA_MCLOSE5:
4297 case NFA_MCLOSE6:
4298 case NFA_MCLOSE7:
4299 case NFA_MCLOSE8:
4300 case NFA_MCLOSE9:
4301 case NFA_NCLOSE:
4302 return failure_chance(state->out, depth + 1);
4303
4304 case NFA_BACKREF1:
4305 case NFA_BACKREF2:
4306 case NFA_BACKREF3:
4307 case NFA_BACKREF4:
4308 case NFA_BACKREF5:
4309 case NFA_BACKREF6:
4310 case NFA_BACKREF7:
4311 case NFA_BACKREF8:
4312 case NFA_BACKREF9:
4313#ifdef FEAT_SYN_HL
4314 case NFA_ZREF1:
4315 case NFA_ZREF2:
4316 case NFA_ZREF3:
4317 case NFA_ZREF4:
4318 case NFA_ZREF5:
4319 case NFA_ZREF6:
4320 case NFA_ZREF7:
4321 case NFA_ZREF8:
4322 case NFA_ZREF9:
4323#endif
4324 /* backreferences don't match in many places */
4325 return 94;
4326
4327 case NFA_LNUM_GT:
4328 case NFA_LNUM_LT:
4329 case NFA_COL_GT:
4330 case NFA_COL_LT:
4331 case NFA_VCOL_GT:
4332 case NFA_VCOL_LT:
4333 case NFA_MARK_GT:
4334 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004335 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004336 /* before/after positions don't match very often */
4337 return 85;
4338
4339 case NFA_LNUM:
4340 return 90;
4341
4342 case NFA_CURSOR:
4343 case NFA_COL:
4344 case NFA_VCOL:
4345 case NFA_MARK:
4346 /* specific positions rarely match */
4347 return 98;
4348
4349 case NFA_COMPOSING:
4350 return 95;
4351
4352 default:
4353 if (c > 0)
4354 /* character match fails often */
4355 return 95;
4356 }
4357
4358 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004359 return 50;
4360}
4361
Bram Moolenaarf46da702013-06-02 22:37:42 +02004362/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004363 * Skip until the char "c" we know a match must start with.
4364 */
4365 static int
4366skip_to_start(c, colp)
4367 int c;
4368 colnr_T *colp;
4369{
4370 char_u *s;
4371
4372 /* Used often, do some work to avoid call overhead. */
4373 if (!ireg_ic
4374#ifdef FEAT_MBYTE
4375 && !has_mbyte
4376#endif
4377 )
4378 s = vim_strbyte(regline + *colp, c);
4379 else
4380 s = cstrchr(regline + *colp, c);
4381 if (s == NULL)
4382 return FAIL;
4383 *colp = (int)(s - regline);
4384 return OK;
4385}
4386
4387/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004388 * Check for a match with match_text.
4389 * Called after skip_to_start() has find regstart.
4390 * Returns zero for no match, 1 for a match.
4391 */
4392 static long
4393find_match_text(startcol, regstart, match_text)
4394 colnr_T startcol;
4395 int regstart;
4396 char_u *match_text;
4397{
4398 colnr_T col = startcol;
4399 int c1, c2;
4400 int len1, len2;
4401 int match;
4402
4403 for (;;)
4404 {
4405 match = TRUE;
4406 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4407 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4408 {
4409 c1 = PTR2CHAR(match_text + len1);
4410 c2 = PTR2CHAR(regline + col + len2);
4411 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4412 {
4413 match = FALSE;
4414 break;
4415 }
4416 len2 += MB_CHAR2LEN(c2);
4417 }
4418 if (match
4419#ifdef FEAT_MBYTE
4420 /* check that no composing char follows */
4421 && !(enc_utf8
4422 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4423#endif
4424 )
4425 {
4426 cleanup_subexpr();
4427 if (REG_MULTI)
4428 {
4429 reg_startpos[0].lnum = reglnum;
4430 reg_startpos[0].col = col;
4431 reg_endpos[0].lnum = reglnum;
4432 reg_endpos[0].col = col + len2;
4433 }
4434 else
4435 {
4436 reg_startp[0] = regline + col;
4437 reg_endp[0] = regline + col + len2;
4438 }
4439 return 1L;
4440 }
4441
4442 /* Try finding regstart after the current match. */
4443 col += MB_CHAR2LEN(regstart); /* skip regstart */
4444 if (skip_to_start(regstart, &col) == FAIL)
4445 break;
4446 }
4447 return 0L;
4448}
4449
4450/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004451 * Main matching routine.
4452 *
4453 * Run NFA to determine whether it matches reginput.
4454 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004455 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004456 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004457 * Return TRUE if there is a match, FALSE otherwise.
4458 * Note: Caller must ensure that: start != NULL.
4459 */
4460 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004461nfa_regmatch(prog, start, submatch, m)
4462 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004463 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004464 regsubs_T *submatch;
4465 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004466{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004467 int result;
4468 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004470 int go_to_nextline = FALSE;
4471 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004472 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004473 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004474 nfa_list_T *thislist;
4475 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004476 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004477 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004478 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004479 int add_count;
4480 int add_off;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004481 garray_T pimlist;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004482 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004483#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004484 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004485
4486 if (debug == NULL)
4487 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004488 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004489 return FALSE;
4490 }
4491#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004492 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004493 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004494
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004495 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004496 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004497 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004498 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004499 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004500 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004501 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004502 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004503
4504#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004505 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004506 if (log_fd != NULL)
4507 {
4508 fprintf(log_fd, "**********************************\n");
4509 nfa_set_code(start->c);
4510 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4511 abs(start->id), code);
4512 fprintf(log_fd, "**********************************\n");
4513 }
4514 else
4515 {
4516 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4517 log_fd = stderr;
4518 }
4519#endif
4520
4521 thislist = &list[0];
4522 thislist->n = 0;
4523 nextlist = &list[1];
4524 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004526 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004527#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004528 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004529
4530 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4531 * it's the first MOPEN. */
4532 if (toplevel)
4533 {
4534 if (REG_MULTI)
4535 {
4536 m->norm.list.multi[0].start.lnum = reglnum;
4537 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4538 }
4539 else
4540 m->norm.list.line[0].start = reginput;
4541 m->norm.in_use = 1;
4542 addstate(thislist, start->out, m, 0);
4543 }
4544 else
4545 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004546
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004547#define ADD_STATE_IF_MATCH(state) \
4548 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004549 add_state = state->out; \
4550 add_off = clen; \
4551 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004552
4553 /*
4554 * Run for each character.
4555 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004556 for (;;)
4557 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004558 int curc;
4559 int clen;
4560
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561#ifdef FEAT_MBYTE
4562 if (has_mbyte)
4563 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004564 curc = (*mb_ptr2char)(reginput);
4565 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004566 }
4567 else
4568#endif
4569 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004570 curc = *reginput;
4571 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004572 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004573 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004574 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004575 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004576 go_to_nextline = FALSE;
4577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578
4579 /* swap lists */
4580 thislist = &list[flag];
4581 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004582 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004583 ++nfa_listid;
4584 thislist->id = nfa_listid;
4585 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004586
Bram Moolenaara2d95102013-06-04 14:23:05 +02004587 pimlist.ga_len = 0;
4588
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004589#ifdef ENABLE_LOG
4590 fprintf(log_fd, "------------------------------------------\n");
4591 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004592 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004593 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004594 {
4595 int i;
4596
4597 for (i = 0; i < thislist->n; i++)
4598 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4599 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 fprintf(log_fd, "\n");
4601#endif
4602
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004603#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004604 fprintf(debug, "\n-------------------\n");
4605#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004606 /*
4607 * If the state lists are empty we can stop.
4608 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004609 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004610 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611
4612 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004613 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004614 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004615 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004616
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004617#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004618 nfa_set_code(t->state->c);
4619 fprintf(debug, "%s, ", code);
4620#endif
4621#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004622 {
4623 int col;
4624
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004625 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004626 col = -1;
4627 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004628 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004629 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004630 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004631 nfa_set_code(t->state->c);
4632 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4633 abs(t->state->id), (int)t->state->c, code, col);
4634 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004635#endif
4636
4637 /*
4638 * Handle the possible codes of the current state.
4639 * The most important is NFA_MATCH.
4640 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004641 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004642 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004643 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004644 switch (t->state->c)
4645 {
4646 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004647 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004648 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004649 copy_sub(&submatch->norm, &t->subs.norm);
4650#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004651 if (nfa_has_zsubexpr)
4652 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004653#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004654#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004655 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004656#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004657 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004658 * states at this position. When the list of states is going
4659 * to be empty quit without advancing, so that "reginput" is
4660 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004661 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004662 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004663 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004664 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004665
4666 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004667 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02004668 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004669 /*
4670 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004671 * NFA_START_INVISIBLE_BEFORE node.
4672 * They surround a zero-width group, used with "\@=", "\&",
4673 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004674 * If we got here, it means that the current "invisible" group
4675 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004676 * nfa_regmatch(). For a look-behind match only when it ends
4677 * in the position in "nfa_endp".
4678 * Submatches are stored in *m, and used in the parent call.
4679 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004680#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004681 if (nfa_endp != NULL)
4682 {
4683 if (REG_MULTI)
4684 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4685 (int)reglnum,
4686 (int)nfa_endp->se_u.pos.lnum,
4687 (int)(reginput - regline),
4688 nfa_endp->se_u.pos.col);
4689 else
4690 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4691 (int)(reginput - regline),
4692 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004693 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004694#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004695 /* If "nfa_endp" is set it's only a match if it ends at
4696 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004697 if (nfa_endp != NULL && (REG_MULTI
4698 ? (reglnum != nfa_endp->se_u.pos.lnum
4699 || (int)(reginput - regline)
4700 != nfa_endp->se_u.pos.col)
4701 : reginput != nfa_endp->se_u.ptr))
4702 break;
4703
4704 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02004705 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004706 {
4707 copy_sub(&m->norm, &t->subs.norm);
4708#ifdef FEAT_SYN_HL
4709 if (nfa_has_zsubexpr)
4710 copy_sub(&m->synt, &t->subs.synt);
4711#endif
4712 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004713#ifdef ENABLE_LOG
4714 fprintf(log_fd, "Match found:\n");
4715 log_subsexpr(m);
4716#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004717 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004718 break;
4719
4720 case NFA_START_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004721 case NFA_START_INVISIBLE_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004722 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004723 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004725 nfa_pim_T *pim;
4726 int cout = t->state->out1->out->c;
4727
4728 /* Do it directly when what follows is possibly end of
4729 * match (closing paren).
4730 * Postpone when it is \@<= or \@<!, these are expensive.
4731 * TODO: remove the check for t->pim and check multiple
4732 * where it's used?
4733 * Otherwise first do the one that has the highest chance
4734 * of failing. */
4735 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004736#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004737 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004738#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004739 || cout == NFA_NCLOSE
4740 || t->pim != NULL
4741 || (t->state->c != NFA_START_INVISIBLE_BEFORE
Bram Moolenaardecd9542013-06-07 16:31:50 +02004742 && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG
Bram Moolenaara2d95102013-06-04 14:23:05 +02004743 && failure_chance(t->state->out1->out, 0)
4744 < failure_chance(t->state->out, 0)))
4745 {
4746 /*
4747 * First try matching the invisible match, then what
4748 * follows.
4749 */
4750 result = recursive_regmatch(t->state, prog,
4751 submatch, m, &listids);
4752
Bram Moolenaardecd9542013-06-07 16:31:50 +02004753 /* for \@! and \@<! it is a match when the result is
4754 * FALSE */
4755 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
4756 || t->state->c
4757 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004758 {
4759 /* Copy submatch info from the recursive call */
4760 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004761#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004762 if (nfa_has_zsubexpr)
4763 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004764#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004765
Bram Moolenaara2d95102013-06-04 14:23:05 +02004766 /* t->state->out1 is the corresponding
4767 * END_INVISIBLE node; Add its out to the current
4768 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004769 add_here = TRUE;
4770 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004771 }
4772 }
4773 else
4774 {
4775 /*
4776 * First try matching what follows at the current
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004777 * position. Only if a match is found, before
4778 * addstate() is called, then verify the invisible
4779 * match matches. Add a nfa_pim_T to the following
4780 * states, it contains info about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02004781 */
4782 if (ga_grow(&pimlist, 1) == FAIL)
4783 goto theend;
4784 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4785 ++pimlist.ga_len;
4786 pim->state = t->state;
4787 pim->pim = NULL;
4788 pim->result = NFA_PIM_TODO;
4789
4790 /* t->state->out1 is the corresponding END_INVISIBLE
4791 * node; Add its out to the current list (zero-width
4792 * match). */
4793 addstate_here(thislist, t->state->out1->out, &t->subs,
4794 pim, &listidx);
4795 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004796 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004797 break;
4798
Bram Moolenaar87953742013-06-05 18:52:40 +02004799 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02004800 {
4801 nfa_state_T *skip = NULL;
4802#ifdef ENABLE_LOG
4803 int skip_lid = 0;
4804#endif
4805
4806 /* There is no point in trying to match the pattern if the
4807 * output state is not going to be added to the list. */
4808 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
4809 {
4810 skip = t->state->out1->out;
4811#ifdef ENABLE_LOG
4812 skip_lid = nextlist->id;
4813#endif
4814 }
4815 else if (state_in_list(nextlist,
4816 t->state->out1->out->out, &t->subs))
4817 {
4818 skip = t->state->out1->out->out;
4819#ifdef ENABLE_LOG
4820 skip_lid = nextlist->id;
4821#endif
4822 }
4823 else if(state_in_list(thislist,
4824 t->state->out1->out->out, &t->subs))
4825 {
4826 skip = t->state->out1->out->out;
4827#ifdef ENABLE_LOG
4828 skip_lid = thislist->id;
4829#endif
4830 }
4831 if (skip != NULL)
4832 {
4833#ifdef ENABLE_LOG
4834 nfa_set_code(skip->c);
4835 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
4836 abs(skip->id), skip_lid, skip->c, code);
4837#endif
4838 break;
4839 }
4840
Bram Moolenaar87953742013-06-05 18:52:40 +02004841 /* First try matching the pattern. */
4842 result = recursive_regmatch(t->state, prog,
4843 submatch, m, &listids);
4844 if (result)
4845 {
4846 int bytelen;
4847
4848#ifdef ENABLE_LOG
4849 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4850 log_subsexpr(m);
4851#endif
4852 /* Copy submatch info from the recursive call */
4853 copy_sub_off(&t->subs.norm, &m->norm);
4854#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004855 if (nfa_has_zsubexpr)
4856 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02004857#endif
4858 /* Now we need to skip over the matched text and then
4859 * continue with what follows. */
4860 if (REG_MULTI)
4861 /* TODO: multi-line match */
4862 bytelen = m->norm.list.multi[0].end.col
4863 - (int)(reginput - regline);
4864 else
4865 bytelen = (int)(m->norm.list.line[0].end - reginput);
4866
4867#ifdef ENABLE_LOG
4868 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4869#endif
4870 if (bytelen == 0)
4871 {
4872 /* empty match, output of corresponding
4873 * NFA_END_PATTERN/NFA_SKIP to be used at current
4874 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004875 add_here = TRUE;
4876 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02004877 }
4878 else if (bytelen <= clen)
4879 {
4880 /* match current character, output of corresponding
4881 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02004882 add_state = t->state->out1->out->out;
4883 add_off = clen;
4884 }
4885 else
4886 {
4887 /* skip over the matched characters, set character
4888 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02004889 add_state = t->state->out1->out;
4890 add_off = bytelen;
4891 add_count = bytelen - clen;
4892 }
4893 }
4894 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02004895 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004896
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004897 case NFA_BOL:
4898 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004899 {
4900 add_here = TRUE;
4901 add_state = t->state->out;
4902 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004903 break;
4904
4905 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004906 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004907 {
4908 add_here = TRUE;
4909 add_state = t->state->out;
4910 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004911 break;
4912
4913 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004914 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004915
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004916 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004917 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004918#ifdef FEAT_MBYTE
4919 else if (has_mbyte)
4920 {
4921 int this_class;
4922
4923 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004924 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004926 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004927 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004928 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004929 }
4930#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004931 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004932 || (reginput > regline
4933 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02004934 result = FALSE;
4935 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004936 {
4937 add_here = TRUE;
4938 add_state = t->state->out;
4939 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004941
4942 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004943 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004944 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004945 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946#ifdef FEAT_MBYTE
4947 else if (has_mbyte)
4948 {
4949 int this_class, prev_class;
4950
4951 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004952 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004953 prev_class = reg_prev_class();
4954 if (this_class == prev_class
4955 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02004956 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004957 }
4958#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004959 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004960 || (reginput[0] != NUL
4961 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02004962 result = FALSE;
4963 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004964 {
4965 add_here = TRUE;
4966 add_state = t->state->out;
4967 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004968 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004969
Bram Moolenaar4b780632013-05-31 22:14:52 +02004970 case NFA_BOF:
4971 if (reglnum == 0 && reginput == regline
4972 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004973 {
4974 add_here = TRUE;
4975 add_state = t->state->out;
4976 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02004977 break;
4978
4979 case NFA_EOF:
4980 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004981 {
4982 add_here = TRUE;
4983 add_state = t->state->out;
4984 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02004985 break;
4986
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004987#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004988 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004989 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004990 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004991 int len = 0;
4992 nfa_state_T *end;
4993 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004994 int cchars[MAX_MCO];
4995 int ccount = 0;
4996 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004997
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004998 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004999 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005000 if (utf_iscomposing(sta->c))
5001 {
5002 /* Only match composing character(s), ignore base
5003 * character. Used for ".{composing}" and "{composing}"
5004 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005005 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005006 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005007 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005008 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005009 /* If \Z was present, then ignore composing characters.
5010 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005011 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005012 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005013 else
5014 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005015 while (sta->c != NFA_END_COMPOSING)
5016 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005017 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005018
5019 /* Check base character matches first, unless ignored. */
5020 else if (len > 0 || mc == sta->c)
5021 {
5022 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005023 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005024 len += mb_char2len(mc);
5025 sta = sta->out;
5026 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005027
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005028 /* We don't care about the order of composing characters.
5029 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005030 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005031 {
5032 mc = mb_ptr2char(reginput + len);
5033 cchars[ccount++] = mc;
5034 len += mb_char2len(mc);
5035 if (ccount == MAX_MCO)
5036 break;
5037 }
5038
5039 /* Check that each composing char in the pattern matches a
5040 * composing char in the text. We do not check if all
5041 * composing chars are matched. */
5042 result = OK;
5043 while (sta->c != NFA_END_COMPOSING)
5044 {
5045 for (j = 0; j < ccount; ++j)
5046 if (cchars[j] == sta->c)
5047 break;
5048 if (j == ccount)
5049 {
5050 result = FAIL;
5051 break;
5052 }
5053 sta = sta->out;
5054 }
5055 }
5056 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005057 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005058
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005059 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005060 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005061 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005062 }
5063#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005064
5065 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005066 if (curc == NUL && !reg_line_lbr && REG_MULTI
5067 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005069 go_to_nextline = TRUE;
5070 /* Pass -1 for the offset, which means taking the position
5071 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005072 add_state = t->state->out;
5073 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005074 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005075 else if (curc == '\n' && reg_line_lbr)
5076 {
5077 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005078 add_state = t->state->out;
5079 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005080 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005081 break;
5082
Bram Moolenaar417bad22013-06-07 14:08:30 +02005083 case NFA_START_COLL:
5084 case NFA_START_NEG_COLL:
5085 {
5086 /* What follows is a list of characters, until NFA_END_COLL.
5087 * One of them must match or none of them must match. */
5088 nfa_state_T *state;
5089 int result_if_matched;
5090 int c1, c2;
5091
5092 /* Never match EOL. If it's part of the collection it is added
5093 * as a separate state with an OR. */
5094 if (curc == NUL)
5095 break;
5096
5097 state = t->state->out;
5098 result_if_matched = (t->state->c == NFA_START_COLL);
5099 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005100 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005101 if (state->c == NFA_END_COLL)
5102 {
5103 result = !result_if_matched;
5104 break;
5105 }
5106 if (state->c == NFA_RANGE_MIN)
5107 {
5108 c1 = state->val;
5109 state = state->out; /* advance to NFA_RANGE_MAX */
5110 c2 = state->val;
5111#ifdef ENABLE_LOG
5112 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5113 curc, c1, c2);
5114#endif
5115 if (curc >= c1 && curc <= c2)
5116 {
5117 result = result_if_matched;
5118 break;
5119 }
5120 if (ireg_ic)
5121 {
5122 int curc_low = MB_TOLOWER(curc);
5123 int done = FALSE;
5124
5125 for ( ; c1 <= c2; ++c1)
5126 if (MB_TOLOWER(c1) == curc_low)
5127 {
5128 result = result_if_matched;
5129 done = TRUE;
5130 break;
5131 }
5132 if (done)
5133 break;
5134 }
5135 }
5136 else if (state->c < 0 ? check_char_class(state->c, curc)
5137 : (curc == state->c
5138 || (ireg_ic && MB_TOLOWER(curc)
5139 == MB_TOLOWER(state->c))))
5140 {
5141 result = result_if_matched;
5142 break;
5143 }
5144 state = state->out;
5145 }
5146 if (result)
5147 {
5148 /* next state is in out of the NFA_END_COLL, out1 of
5149 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005150 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005151 add_off = clen;
5152 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005153 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005154 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005155
5156 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005157 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005158 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005159 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005160 add_state = t->state->out;
5161 add_off = clen;
5162 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005163 break;
5164
5165 /*
5166 * Character classes like \a for alpha, \d for digit etc.
5167 */
5168 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005169 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005170 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005171 break;
5172
5173 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005174 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005175 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005176 break;
5177
5178 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005179 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005180 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005181 break;
5182
5183 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005184 result = !VIM_ISDIGIT(curc)
5185 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005186 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005187 break;
5188
5189 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005190 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005191 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005192 break;
5193
5194 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005195 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005196 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005197 break;
5198
5199 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005200 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005201 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005202 break;
5203
5204 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005205 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005206 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005207 break;
5208
5209 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005210 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005211 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005212 break;
5213
5214 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005215 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005216 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005217 break;
5218
5219 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005220 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005221 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005222 break;
5223
5224 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005225 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005226 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005227 break;
5228
5229 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005230 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005231 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005232 break;
5233
5234 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005235 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005236 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005237 break;
5238
5239 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005240 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005241 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005242 break;
5243
5244 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005245 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005246 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005247 break;
5248
5249 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005250 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005251 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005252 break;
5253
5254 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005255 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005256 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005257 break;
5258
5259 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005260 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005261 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005262 break;
5263
5264 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005265 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005266 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005267 break;
5268
5269 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005270 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005271 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005272 break;
5273
5274 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005275 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005276 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005277 break;
5278
5279 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005280 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005281 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005282 break;
5283
5284 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005285 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005286 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005287 break;
5288
5289 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005290 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005291 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005292 break;
5293
5294 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005295 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005296 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005297 break;
5298
Bram Moolenaar5714b802013-05-28 22:03:20 +02005299 case NFA_BACKREF1:
5300 case NFA_BACKREF2:
5301 case NFA_BACKREF3:
5302 case NFA_BACKREF4:
5303 case NFA_BACKREF5:
5304 case NFA_BACKREF6:
5305 case NFA_BACKREF7:
5306 case NFA_BACKREF8:
5307 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005308#ifdef FEAT_SYN_HL
5309 case NFA_ZREF1:
5310 case NFA_ZREF2:
5311 case NFA_ZREF3:
5312 case NFA_ZREF4:
5313 case NFA_ZREF5:
5314 case NFA_ZREF6:
5315 case NFA_ZREF7:
5316 case NFA_ZREF8:
5317 case NFA_ZREF9:
5318#endif
5319 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005320 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005321 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005322 int bytelen;
5323
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005324 if (t->state->c <= NFA_BACKREF9)
5325 {
5326 subidx = t->state->c - NFA_BACKREF1 + 1;
5327 result = match_backref(&t->subs.norm, subidx, &bytelen);
5328 }
5329#ifdef FEAT_SYN_HL
5330 else
5331 {
5332 subidx = t->state->c - NFA_ZREF1 + 1;
5333 result = match_zref(subidx, &bytelen);
5334 }
5335#endif
5336
Bram Moolenaar5714b802013-05-28 22:03:20 +02005337 if (result)
5338 {
5339 if (bytelen == 0)
5340 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005341 /* empty match always works, output of NFA_SKIP to be
5342 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005343 add_here = TRUE;
5344 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005345 }
5346 else if (bytelen <= clen)
5347 {
5348 /* match current character, jump ahead to out of
5349 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005350 add_state = t->state->out->out;
5351 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005352 }
5353 else
5354 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005355 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005356 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005357 add_state = t->state->out;
5358 add_off = bytelen;
5359 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005360 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005361 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005362 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005363 }
5364 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005365 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005366 if (t->count - clen <= 0)
5367 {
5368 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005369 add_state = t->state->out;
5370 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005371 }
5372 else
5373 {
5374 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005375 add_state = t->state;
5376 add_off = 0;
5377 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005378 }
5379 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005380
Bram Moolenaar423532e2013-05-29 21:14:42 +02005381 case NFA_LNUM:
5382 case NFA_LNUM_GT:
5383 case NFA_LNUM_LT:
5384 result = (REG_MULTI &&
5385 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5386 (long_u)(reglnum + reg_firstlnum)));
5387 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005388 {
5389 add_here = TRUE;
5390 add_state = t->state->out;
5391 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005392 break;
5393
5394 case NFA_COL:
5395 case NFA_COL_GT:
5396 case NFA_COL_LT:
5397 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5398 (long_u)(reginput - regline) + 1);
5399 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005400 {
5401 add_here = TRUE;
5402 add_state = t->state->out;
5403 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005404 break;
5405
5406 case NFA_VCOL:
5407 case NFA_VCOL_GT:
5408 case NFA_VCOL_LT:
5409 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5410 (long_u)win_linetabsize(
5411 reg_win == NULL ? curwin : reg_win,
5412 regline, (colnr_T)(reginput - regline)) + 1);
5413 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005414 {
5415 add_here = TRUE;
5416 add_state = t->state->out;
5417 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005418 break;
5419
Bram Moolenaar044aa292013-06-04 21:27:38 +02005420 case NFA_MARK:
5421 case NFA_MARK_GT:
5422 case NFA_MARK_LT:
5423 {
5424 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5425
5426 /* Compare the mark position to the match position. */
5427 result = (pos != NULL /* mark doesn't exist */
5428 && pos->lnum > 0 /* mark isn't set in reg_buf */
5429 && (pos->lnum == reglnum + reg_firstlnum
5430 ? (pos->col == (colnr_T)(reginput - regline)
5431 ? t->state->c == NFA_MARK
5432 : (pos->col < (colnr_T)(reginput - regline)
5433 ? t->state->c == NFA_MARK_GT
5434 : t->state->c == NFA_MARK_LT))
5435 : (pos->lnum < reglnum + reg_firstlnum
5436 ? t->state->c == NFA_MARK_GT
5437 : t->state->c == NFA_MARK_LT)));
5438 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005439 {
5440 add_here = TRUE;
5441 add_state = t->state->out;
5442 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005443 break;
5444 }
5445
Bram Moolenaar423532e2013-05-29 21:14:42 +02005446 case NFA_CURSOR:
5447 result = (reg_win != NULL
5448 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5449 && ((colnr_T)(reginput - regline)
5450 == reg_win->w_cursor.col));
5451 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005452 {
5453 add_here = TRUE;
5454 add_state = t->state->out;
5455 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005456 break;
5457
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005458 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005459#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005460 result = reg_match_visual();
5461 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005462 {
5463 add_here = TRUE;
5464 add_state = t->state->out;
5465 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005466#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005467 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005468
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005469 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005470 {
5471 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005472
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005473 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005474 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005475 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005476 result = (c == curc);
5477
5478 if (!result && ireg_ic)
5479 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005480#ifdef FEAT_MBYTE
5481 /* If there is a composing character which is not being
5482 * ignored there can be no match. Match with composing
5483 * character uses NFA_COMPOSING above. */
5484 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005485 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005486 result = FALSE;
5487#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005488 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005489 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005490 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005491
5492 } /* switch (t->state->c) */
5493
5494 if (add_state != NULL)
5495 {
5496 if (t->pim != NULL)
5497 {
5498 /* postponed invisible match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005499 if (t->pim->result == NFA_PIM_TODO)
5500 {
5501#ifdef ENABLE_LOG
5502 fprintf(log_fd, "\n");
5503 fprintf(log_fd, "==================================\n");
5504 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5505 fprintf(log_fd, "\n");
5506#endif
5507 result = recursive_regmatch(t->pim->state,
5508 prog, submatch, m, &listids);
5509 t->pim->result = result ? NFA_PIM_MATCH
5510 : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005511 /* for \@! and \@<! it is a match when the result is
5512 * FALSE */
5513 if (result != (t->pim->state->c
5514 == NFA_START_INVISIBLE_NEG
5515 || t->pim->state->c
5516 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005517 {
5518 /* Copy submatch info from the recursive call */
5519 copy_sub_off(&t->pim->subs.norm, &m->norm);
5520#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005521 if (nfa_has_zsubexpr)
5522 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005523#endif
5524 }
5525 }
5526 else
5527 {
5528 result = (t->pim->result == NFA_PIM_MATCH);
5529#ifdef ENABLE_LOG
5530 fprintf(log_fd, "\n");
5531 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5532 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5533 fprintf(log_fd, "\n");
5534#endif
5535 }
5536
Bram Moolenaardecd9542013-06-07 16:31:50 +02005537 /* for \@! and \@<! it is a match when result is FALSE */
5538 if (result != (t->pim->state->c == NFA_START_INVISIBLE_NEG
5539 || t->pim->state->c
5540 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005541 {
5542 /* Copy submatch info from the recursive call */
5543 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5544#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005545 if (nfa_has_zsubexpr)
5546 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005547#endif
5548 }
5549 else
5550 /* look-behind match failed, don't add the state */
5551 continue;
5552 }
5553
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005554 if (add_here)
5555 addstate_here(thislist, add_state, &t->subs, NULL, &listidx);
5556 else
5557 {
5558 addstate(nextlist, add_state, &t->subs, add_off);
5559 if (add_count > 0)
5560 nextlist->t[nextlist->n - 1].count = add_count;
5561 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562 }
5563
5564 } /* for (thislist = thislist; thislist->state; thislist++) */
5565
Bram Moolenaare23febd2013-05-26 18:40:14 +02005566 /* Look for the start of a match in the current position by adding the
5567 * start state to the list of states.
5568 * The first found match is the leftmost one, thus the order of states
5569 * matters!
5570 * Do not add the start state in recursive calls of nfa_regmatch(),
5571 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005572 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005573 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005574 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005575 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02005576 && reglnum == 0
5577 && clen != 0
5578 && (ireg_maxcol == 0
5579 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005580 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005581 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005582 ? (reglnum < nfa_endp->se_u.pos.lnum
5583 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005584 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005585 < nfa_endp->se_u.pos.col))
5586 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005587 {
5588#ifdef ENABLE_LOG
5589 fprintf(log_fd, "(---) STARTSTATE\n");
5590#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005591 /* Inline optimized code for addstate() if we know the state is
5592 * the first MOPEN. */
5593 if (toplevel)
5594 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005595 int add = TRUE;
5596 int c;
5597
5598 if (prog->regstart != NUL && clen != 0)
5599 {
5600 if (nextlist->n == 0)
5601 {
5602 colnr_T col = (colnr_T)(reginput - regline) + clen;
5603
5604 /* Nextlist is empty, we can skip ahead to the
5605 * character that must appear at the start. */
5606 if (skip_to_start(prog->regstart, &col) == FAIL)
5607 break;
5608#ifdef ENABLE_LOG
5609 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
5610 col - ((colnr_T)(reginput - regline) + clen));
5611#endif
5612 reginput = regline + col - clen;
5613 }
5614 else
5615 {
5616 /* Checking if the required start character matches is
5617 * cheaper than adding a state that won't match. */
5618 c = PTR2CHAR(reginput + clen);
5619 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
5620 != MB_TOLOWER(prog->regstart)))
5621 {
5622#ifdef ENABLE_LOG
5623 fprintf(log_fd, " Skipping start state, regstart does not match\n");
5624#endif
5625 add = FALSE;
5626 }
5627 }
5628 }
5629
5630 if (add)
5631 {
5632 if (REG_MULTI)
5633 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005634 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005635 else
5636 m->norm.list.line[0].start = reginput + clen;
5637 addstate(nextlist, start->out, m, clen);
5638 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005639 }
5640 else
5641 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005642 }
5643
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005644#ifdef ENABLE_LOG
5645 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005646 {
5647 int i;
5648
5649 for (i = 0; i < thislist->n; i++)
5650 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5651 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005652 fprintf(log_fd, "\n");
5653#endif
5654
5655nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005656 /* Advance to the next character, or advance to the next line, or
5657 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005658 if (clen != 0)
5659 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005660 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5661 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005662 reg_nextline();
5663 else
5664 break;
5665 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005666
5667#ifdef ENABLE_LOG
5668 if (log_fd != stderr)
5669 fclose(log_fd);
5670 log_fd = NULL;
5671#endif
5672
5673theend:
5674 /* Free memory */
5675 vim_free(list[0].t);
5676 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005677 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005678 ga_clear(&pimlist);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005679#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005680#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005681 fclose(debug);
5682#endif
5683
Bram Moolenaar963fee22013-05-26 21:47:28 +02005684 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005685}
5686
5687/*
5688 * Try match of "prog" with at regline["col"].
5689 * Returns 0 for failure, number of lines contained in the match otherwise.
5690 */
5691 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005692nfa_regtry(prog, col)
5693 nfa_regprog_T *prog;
5694 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005695{
5696 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005697 regsubs_T subs, m;
5698 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005699#ifdef ENABLE_LOG
5700 FILE *f;
5701#endif
5702
5703 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005704
5705#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005706 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005707 if (f != NULL)
5708 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005709 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005710#ifdef DEBUG
5711 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5712#endif
5713 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005714 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005715 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005716 fprintf(f, "\n\n");
5717 fclose(f);
5718 }
5719 else
5720 EMSG(_("Could not open temporary log file for writing "));
5721#endif
5722
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005723 clear_sub(&subs.norm);
5724 clear_sub(&m.norm);
5725#ifdef FEAT_SYN_HL
5726 clear_sub(&subs.synt);
5727 clear_sub(&m.synt);
5728#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005729
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005730 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005731 return 0;
5732
5733 cleanup_subexpr();
5734 if (REG_MULTI)
5735 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005736 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005737 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005738 reg_startpos[i] = subs.norm.list.multi[i].start;
5739 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005740 }
5741
5742 if (reg_startpos[0].lnum < 0)
5743 {
5744 reg_startpos[0].lnum = 0;
5745 reg_startpos[0].col = col;
5746 }
5747 if (reg_endpos[0].lnum < 0)
5748 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005749 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005750 reg_endpos[0].lnum = reglnum;
5751 reg_endpos[0].col = (int)(reginput - regline);
5752 }
5753 else
5754 /* Use line number of "\ze". */
5755 reglnum = reg_endpos[0].lnum;
5756 }
5757 else
5758 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005759 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005760 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005761 reg_startp[i] = subs.norm.list.line[i].start;
5762 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005763 }
5764
5765 if (reg_startp[0] == NULL)
5766 reg_startp[0] = regline + col;
5767 if (reg_endp[0] == NULL)
5768 reg_endp[0] = reginput;
5769 }
5770
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005771#ifdef FEAT_SYN_HL
5772 /* Package any found \z(...\) matches for export. Default is none. */
5773 unref_extmatch(re_extmatch_out);
5774 re_extmatch_out = NULL;
5775
5776 if (prog->reghasz == REX_SET)
5777 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005778 cleanup_zsubexpr();
5779 re_extmatch_out = make_extmatch();
5780 for (i = 0; i < subs.synt.in_use; i++)
5781 {
5782 if (REG_MULTI)
5783 {
5784 struct multipos *mpos = &subs.synt.list.multi[i];
5785
5786 /* Only accept single line matches. */
5787 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5788 re_extmatch_out->matches[i] =
5789 vim_strnsave(reg_getline(mpos->start.lnum)
5790 + mpos->start.col,
5791 mpos->end.col - mpos->start.col);
5792 }
5793 else
5794 {
5795 struct linepos *lpos = &subs.synt.list.line[i];
5796
5797 if (lpos->start != NULL && lpos->end != NULL)
5798 re_extmatch_out->matches[i] =
5799 vim_strnsave(lpos->start,
5800 (int)(lpos->end - lpos->start));
5801 }
5802 }
5803 }
5804#endif
5805
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005806 return 1 + reglnum;
5807}
5808
5809/*
5810 * Match a regexp against a string ("line" points to the string) or multiple
5811 * lines ("line" is NULL, use reg_getline()).
5812 *
5813 * Returns 0 for failure, number of lines contained in the match otherwise.
5814 */
5815 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02005816nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005817 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005818 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005819{
5820 nfa_regprog_T *prog;
5821 long retval = 0L;
5822 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005823 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005824
5825 if (REG_MULTI)
5826 {
5827 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5828 line = reg_getline((linenr_T)0); /* relative to the cursor */
5829 reg_startpos = reg_mmatch->startpos;
5830 reg_endpos = reg_mmatch->endpos;
5831 }
5832 else
5833 {
5834 prog = (nfa_regprog_T *)reg_match->regprog;
5835 reg_startp = reg_match->startp;
5836 reg_endp = reg_match->endp;
5837 }
5838
5839 /* Be paranoid... */
5840 if (prog == NULL || line == NULL)
5841 {
5842 EMSG(_(e_null));
5843 goto theend;
5844 }
5845
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005846 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5847 if (prog->regflags & RF_ICASE)
5848 ireg_ic = TRUE;
5849 else if (prog->regflags & RF_NOICASE)
5850 ireg_ic = FALSE;
5851
5852#ifdef FEAT_MBYTE
5853 /* If pattern contains "\Z" overrule value of ireg_icombine */
5854 if (prog->regflags & RF_ICOMBINE)
5855 ireg_icombine = TRUE;
5856#endif
5857
5858 regline = line;
5859 reglnum = 0; /* relative to line */
5860
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005861 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005862 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005863 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005864 nfa_listid = 1;
5865 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005866#ifdef DEBUG
5867 nfa_regengine.expr = prog->pattern;
5868#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005869
Bram Moolenaard89616e2013-06-06 18:46:06 +02005870 if (prog->reganch && col > 0)
5871 return 0L;
5872
Bram Moolenaar473de612013-06-08 18:19:48 +02005873 need_clear_subexpr = TRUE;
5874#ifdef FEAT_SYN_HL
5875 /* Clear the external match subpointers if necessary. */
5876 if (prog->reghasz == REX_SET)
5877 {
5878 nfa_has_zsubexpr = TRUE;
5879 need_clear_zsubexpr = TRUE;
5880 }
5881 else
5882 nfa_has_zsubexpr = FALSE;
5883#endif
5884
Bram Moolenaard89616e2013-06-06 18:46:06 +02005885 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02005886 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005887 /* Skip ahead until a character we know the match must start with.
5888 * When there is none there is no match. */
5889 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02005890 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005891
Bram Moolenaar473de612013-06-08 18:19:48 +02005892 /* If match_text is set it contains the full text that must match.
5893 * Nothing else to try. Doesn't handle combining chars well. */
5894 if (prog->match_text != NULL && !ireg_icombine)
5895 return find_match_text(col, prog->regstart, prog->match_text);
5896 }
5897
Bram Moolenaard89616e2013-06-06 18:46:06 +02005898 /* If the start column is past the maximum column: no need to try. */
5899 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5900 goto theend;
5901
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005902 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005903 for (i = 0; i < nstate; ++i)
5904 {
5905 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005906 prog->state[i].lastlist[0] = 0;
5907 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005908 }
5909
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005910 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005911
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005912#ifdef DEBUG
5913 nfa_regengine.expr = NULL;
5914#endif
5915
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005916theend:
5917 return retval;
5918}
5919
5920/*
5921 * Compile a regular expression into internal code for the NFA matcher.
5922 * Returns the program in allocated space. Returns NULL for an error.
5923 */
5924 static regprog_T *
5925nfa_regcomp(expr, re_flags)
5926 char_u *expr;
5927 int re_flags;
5928{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005929 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005930 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005931 int *postfix;
5932
5933 if (expr == NULL)
5934 return NULL;
5935
5936#ifdef DEBUG
5937 nfa_regengine.expr = expr;
5938#endif
5939
5940 init_class_tab();
5941
5942 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5943 return NULL;
5944
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005945 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005946 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005947 postfix = re2post();
5948 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005949 {
5950 /* TODO: only give this error for debugging? */
5951 if (post_ptr >= post_end)
5952 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005953 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005954 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005955
5956 /*
5957 * In order to build the NFA, we parse the input regexp twice:
5958 * 1. first pass to count size (so we can allocate space)
5959 * 2. second to emit code
5960 */
5961#ifdef ENABLE_LOG
5962 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005963 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005964
5965 if (f != NULL)
5966 {
5967 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5968 fclose(f);
5969 }
5970 }
5971#endif
5972
5973 /*
5974 * PASS 1
5975 * Count number of NFA states in "nstate". Do not build the NFA.
5976 */
5977 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005978
5979 /* Space for compiled regexp */
5980 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5981 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5982 if (prog == NULL)
5983 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005984 state_ptr = prog->state;
5985
5986 /*
5987 * PASS 2
5988 * Build the NFA
5989 */
5990 prog->start = post2nfa(postfix, post_ptr, FALSE);
5991 if (prog->start == NULL)
5992 goto fail;
5993
5994 prog->regflags = regflags;
5995 prog->engine = &nfa_regengine;
5996 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005997 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005998 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005999 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006000
6001 prog->reganch = nfa_get_reganch(prog->start, 0);
6002 prog->regstart = nfa_get_regstart(prog->start, 0);
6003
Bram Moolenaar473de612013-06-08 18:19:48 +02006004 prog->match_text = nfa_get_match_text(prog->start);
6005
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006006#ifdef ENABLE_LOG
6007 nfa_postfix_dump(expr, OK);
6008 nfa_dump(prog);
6009#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006010#ifdef FEAT_SYN_HL
6011 /* Remember whether this pattern has any \z specials in it. */
6012 prog->reghasz = re_has_z;
6013#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006014#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006015 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006016 nfa_regengine.expr = NULL;
6017#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006018
6019out:
6020 vim_free(post_start);
6021 post_start = post_ptr = post_end = NULL;
6022 state_ptr = NULL;
6023 return (regprog_T *)prog;
6024
6025fail:
6026 vim_free(prog);
6027 prog = NULL;
6028#ifdef ENABLE_LOG
6029 nfa_postfix_dump(expr, FAIL);
6030#endif
6031#ifdef DEBUG
6032 nfa_regengine.expr = NULL;
6033#endif
6034 goto out;
6035}
6036
Bram Moolenaar473de612013-06-08 18:19:48 +02006037/*
6038 * Free a compiled regexp program, returned by nfa_regcomp().
6039 */
6040 static void
6041nfa_regfree(prog)
6042 regprog_T *prog;
6043{
6044 if (prog != NULL)
6045 {
6046 vim_free(((nfa_regprog_T *)prog)->match_text);
6047#ifdef DEBUG
6048 vim_free(((nfa_regprog_T *)prog)->pattern);
6049#endif
6050 vim_free(prog);
6051 }
6052}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006053
6054/*
6055 * Match a regexp against a string.
6056 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6057 * Uses curbuf for line count and 'iskeyword'.
6058 *
6059 * Return TRUE if there is a match, FALSE if not.
6060 */
6061 static int
6062nfa_regexec(rmp, line, col)
6063 regmatch_T *rmp;
6064 char_u *line; /* string to match against */
6065 colnr_T col; /* column to start looking for match */
6066{
6067 reg_match = rmp;
6068 reg_mmatch = NULL;
6069 reg_maxline = 0;
6070 reg_line_lbr = FALSE;
6071 reg_buf = curbuf;
6072 reg_win = NULL;
6073 ireg_ic = rmp->rm_ic;
6074#ifdef FEAT_MBYTE
6075 ireg_icombine = FALSE;
6076#endif
6077 ireg_maxcol = 0;
6078 return (nfa_regexec_both(line, col) != 0);
6079}
6080
6081#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6082 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6083
6084static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6085
6086/*
6087 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6088 */
6089 static int
6090nfa_regexec_nl(rmp, line, col)
6091 regmatch_T *rmp;
6092 char_u *line; /* string to match against */
6093 colnr_T col; /* column to start looking for match */
6094{
6095 reg_match = rmp;
6096 reg_mmatch = NULL;
6097 reg_maxline = 0;
6098 reg_line_lbr = TRUE;
6099 reg_buf = curbuf;
6100 reg_win = NULL;
6101 ireg_ic = rmp->rm_ic;
6102#ifdef FEAT_MBYTE
6103 ireg_icombine = FALSE;
6104#endif
6105 ireg_maxcol = 0;
6106 return (nfa_regexec_both(line, col) != 0);
6107}
6108#endif
6109
6110
6111/*
6112 * Match a regexp against multiple lines.
6113 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6114 * Uses curbuf for line count and 'iskeyword'.
6115 *
6116 * Return zero if there is no match. Return number of lines contained in the
6117 * match otherwise.
6118 *
6119 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6120 *
6121 * ! Also NOTE : match may actually be in another line. e.g.:
6122 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6123 *
6124 * +-------------------------+
6125 * |a |
6126 * |b |
6127 * |c |
6128 * | |
6129 * +-------------------------+
6130 *
6131 * then nfa_regexec_multi() returns 3. while the original
6132 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6133 *
6134 * FIXME if this behavior is not compatible.
6135 */
6136 static long
6137nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6138 regmmatch_T *rmp;
6139 win_T *win; /* window in which to search or NULL */
6140 buf_T *buf; /* buffer in which to search */
6141 linenr_T lnum; /* nr of line to start looking for match */
6142 colnr_T col; /* column to start looking for match */
6143 proftime_T *tm UNUSED; /* timeout limit or NULL */
6144{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006145 reg_match = NULL;
6146 reg_mmatch = rmp;
6147 reg_buf = buf;
6148 reg_win = win;
6149 reg_firstlnum = lnum;
6150 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6151 reg_line_lbr = FALSE;
6152 ireg_ic = rmp->rmm_ic;
6153#ifdef FEAT_MBYTE
6154 ireg_icombine = FALSE;
6155#endif
6156 ireg_maxcol = rmp->rmm_maxcol;
6157
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006158 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006159}
6160
6161#ifdef DEBUG
6162# undef ENABLE_LOG
6163#endif