blob: d64e5ae6b1d24df1890576b57e6d6155bebd04f5 [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 Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
53 NFA_STAR, /* greedy * (posfix only) */
54 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaare0ad3652015-01-27 12:59:55 +0100247/* re_flags passed to nfa_regcomp() */
248static int nfa_re_flags;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200251static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaar428e9872013-05-30 17:05:39 +0200253/* NFA regexp \1 .. \9 encountered. */
254static int nfa_has_backref;
255
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257/* NFA regexp has \z( ), set zsubexpr. */
258static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200259#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200260
Bram Moolenaar963fee22013-05-26 21:47:28 +0200261/* Number of sub expressions actually being used during execution. 1 if only
262 * the whole match (subexpr 0) is used. */
263static int nfa_nsubexpr;
264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265static int *post_start; /* holds the postfix form of r.e. */
266static int *post_end;
267static int *post_ptr;
268
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200269static int nstate; /* Number of states in the NFA. Also used when
270 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200271static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar307aa162013-06-02 16:34:21 +0200273/* If not NULL match must end at this position */
274static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200276/* listid is global, so that it increases on recursive calls to
277 * nfa_regmatch(), which means we don't have to clear the lastlist field of
278 * all the states. */
279static int nfa_listid;
280static int nfa_alt_listid;
281
282/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
283static int nfa_ll_index = 0;
284
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200285static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200286static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
287static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200288static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200289static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200291static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292static int nfa_regatom __ARGS((void));
293static int nfa_regpiece __ARGS((void));
294static int nfa_regconcat __ARGS((void));
295static int nfa_regbranch __ARGS((void));
296static int nfa_reg __ARGS((int paren));
297#ifdef DEBUG
298static void nfa_set_code __ARGS((int c));
299static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200300static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
301static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302static void nfa_dump __ARGS((nfa_regprog_T *prog));
303#endif
304static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200305static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +0200306static void st_error __ARGS((int *postfix, int *end, int *p));
307static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200309static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200311static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
312static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200313static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200314static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200315static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
316static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200317static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaar2af78a12014-04-23 19:06:37 +0200318static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200319static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200320static int match_follows __ARGS((nfa_state_T *startstate, int depth));
321static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322
323/* helper functions used when doing re2post() ... regatom() parsing */
324#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200325 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326 return FAIL; \
327 *post_ptr++ = c; \
328 } while (0)
329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330/*
331 * Initialize internal variables before NFA compilation.
332 * Return OK on success, FAIL otherwise.
333 */
334 static int
335nfa_regcomp_start(expr, re_flags)
336 char_u *expr;
337 int re_flags; /* see vim_regcomp() */
338{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200339 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200340 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341
342 nstate = 0;
343 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200344 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200345 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200348 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200349 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200350
351 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200352 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354 post_start = (int *)lalloc(postfix_size, TRUE);
355 if (post_start == NULL)
356 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200358 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200360 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200361
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200362 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200363 regcomp_start(expr, re_flags);
364
365 return OK;
366}
367
368/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200369 * Figure out if the NFA state list starts with an anchor, must match at start
370 * of the line.
371 */
372 static int
373nfa_get_reganch(start, depth)
374 nfa_state_T *start;
375 int depth;
376{
377 nfa_state_T *p = start;
378
379 if (depth > 4)
380 return 0;
381
382 while (p != NULL)
383 {
384 switch (p->c)
385 {
386 case NFA_BOL:
387 case NFA_BOF:
388 return 1; /* yes! */
389
390 case NFA_ZSTART:
391 case NFA_ZEND:
392 case NFA_CURSOR:
393 case NFA_VISUAL:
394
395 case NFA_MOPEN:
396 case NFA_MOPEN1:
397 case NFA_MOPEN2:
398 case NFA_MOPEN3:
399 case NFA_MOPEN4:
400 case NFA_MOPEN5:
401 case NFA_MOPEN6:
402 case NFA_MOPEN7:
403 case NFA_MOPEN8:
404 case NFA_MOPEN9:
405 case NFA_NOPEN:
406#ifdef FEAT_SYN_HL
407 case NFA_ZOPEN:
408 case NFA_ZOPEN1:
409 case NFA_ZOPEN2:
410 case NFA_ZOPEN3:
411 case NFA_ZOPEN4:
412 case NFA_ZOPEN5:
413 case NFA_ZOPEN6:
414 case NFA_ZOPEN7:
415 case NFA_ZOPEN8:
416 case NFA_ZOPEN9:
417#endif
418 p = p->out;
419 break;
420
421 case NFA_SPLIT:
422 return nfa_get_reganch(p->out, depth + 1)
423 && nfa_get_reganch(p->out1, depth + 1);
424
425 default:
426 return 0; /* noooo */
427 }
428 }
429 return 0;
430}
431
432/*
433 * Figure out if the NFA state list starts with a character which must match
434 * at start of the match.
435 */
436 static int
437nfa_get_regstart(start, depth)
438 nfa_state_T *start;
439 int depth;
440{
441 nfa_state_T *p = start;
442
443 if (depth > 4)
444 return 0;
445
446 while (p != NULL)
447 {
448 switch (p->c)
449 {
450 /* all kinds of zero-width matches */
451 case NFA_BOL:
452 case NFA_BOF:
453 case NFA_BOW:
454 case NFA_EOW:
455 case NFA_ZSTART:
456 case NFA_ZEND:
457 case NFA_CURSOR:
458 case NFA_VISUAL:
459 case NFA_LNUM:
460 case NFA_LNUM_GT:
461 case NFA_LNUM_LT:
462 case NFA_COL:
463 case NFA_COL_GT:
464 case NFA_COL_LT:
465 case NFA_VCOL:
466 case NFA_VCOL_GT:
467 case NFA_VCOL_LT:
468 case NFA_MARK:
469 case NFA_MARK_GT:
470 case NFA_MARK_LT:
471
472 case NFA_MOPEN:
473 case NFA_MOPEN1:
474 case NFA_MOPEN2:
475 case NFA_MOPEN3:
476 case NFA_MOPEN4:
477 case NFA_MOPEN5:
478 case NFA_MOPEN6:
479 case NFA_MOPEN7:
480 case NFA_MOPEN8:
481 case NFA_MOPEN9:
482 case NFA_NOPEN:
483#ifdef FEAT_SYN_HL
484 case NFA_ZOPEN:
485 case NFA_ZOPEN1:
486 case NFA_ZOPEN2:
487 case NFA_ZOPEN3:
488 case NFA_ZOPEN4:
489 case NFA_ZOPEN5:
490 case NFA_ZOPEN6:
491 case NFA_ZOPEN7:
492 case NFA_ZOPEN8:
493 case NFA_ZOPEN9:
494#endif
495 p = p->out;
496 break;
497
498 case NFA_SPLIT:
499 {
500 int c1 = nfa_get_regstart(p->out, depth + 1);
501 int c2 = nfa_get_regstart(p->out1, depth + 1);
502
503 if (c1 == c2)
504 return c1; /* yes! */
505 return 0;
506 }
507
508 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200509 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200510 return p->c; /* yes! */
511 return 0;
512 }
513 }
514 return 0;
515}
516
517/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200518 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200519 * else. If so return a string in allocated memory with what must match after
520 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200521 */
522 static char_u *
523nfa_get_match_text(start)
524 nfa_state_T *start;
525{
526 nfa_state_T *p = start;
527 int len = 0;
528 char_u *ret;
529 char_u *s;
530
531 if (p->c != NFA_MOPEN)
532 return NULL; /* just in case */
533 p = p->out;
534 while (p->c > 0)
535 {
536 len += MB_CHAR2LEN(p->c);
537 p = p->out;
538 }
539 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
540 return NULL;
541
542 ret = alloc(len);
543 if (ret != NULL)
544 {
545 len = 0;
546 p = start->out->out; /* skip first char, it goes into regstart */
547 s = ret;
548 while (p->c > 0)
549 {
550#ifdef FEAT_MBYTE
551 if (has_mbyte)
552 s += (*mb_char2bytes)(p->c, s);
553 else
554#endif
555 *s++ = p->c;
556 p = p->out;
557 }
558 *s = NUL;
559 }
560 return ret;
561}
562
563/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200564 * Allocate more space for post_start. Called when
565 * running above the estimated number of states.
566 */
567 static int
568realloc_post_list()
569{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200570 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200571 int new_max = nstate_max + 1000;
572 int *new_start;
573 int *old_start;
574
575 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
576 if (new_start == NULL)
577 return FAIL;
578 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200579 old_start = post_start;
580 post_start = new_start;
581 post_ptr = new_start + (post_ptr - old_start);
582 post_end = post_start + new_max;
583 vim_free(old_start);
584 return OK;
585}
586
587/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200588 * Search between "start" and "end" and try to recognize a
589 * character class in expanded form. For example [0-9].
590 * On success, return the id the character class to be emitted.
591 * On failure, return 0 (=FAIL)
592 * Start points to the first char of the range, while end should point
593 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200594 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
595 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200596 */
597 static int
598nfa_recognize_char_class(start, end, extra_newl)
599 char_u *start;
600 char_u *end;
601 int extra_newl;
602{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200603# define CLASS_not 0x80
604# define CLASS_af 0x40
605# define CLASS_AF 0x20
606# define CLASS_az 0x10
607# define CLASS_AZ 0x08
608# define CLASS_o7 0x04
609# define CLASS_o9 0x02
610# define CLASS_underscore 0x01
611
612 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200613 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200614 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200615
616 if (extra_newl == TRUE)
617 newl = TRUE;
618
619 if (*end != ']')
620 return FAIL;
621 p = start;
622 if (*p == '^')
623 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200624 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200625 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200626 }
627
628 while (p < end)
629 {
630 if (p + 2 < end && *(p + 1) == '-')
631 {
632 switch (*p)
633 {
634 case '0':
635 if (*(p + 2) == '9')
636 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200637 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 break;
639 }
640 else
641 if (*(p + 2) == '7')
642 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200643 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 break;
645 }
646 case 'a':
647 if (*(p + 2) == 'z')
648 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200649 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200650 break;
651 }
652 else
653 if (*(p + 2) == 'f')
654 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200655 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200656 break;
657 }
658 case 'A':
659 if (*(p + 2) == 'Z')
660 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200661 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200662 break;
663 }
664 else
665 if (*(p + 2) == 'F')
666 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200667 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200668 break;
669 }
670 /* FALLTHROUGH */
671 default:
672 return FAIL;
673 }
674 p += 3;
675 }
676 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
677 {
678 newl = TRUE;
679 p += 2;
680 }
681 else if (*p == '_')
682 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200683 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200684 p ++;
685 }
686 else if (*p == '\n')
687 {
688 newl = TRUE;
689 p ++;
690 }
691 else
692 return FAIL;
693 } /* while (p < end) */
694
695 if (p != end)
696 return FAIL;
697
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200699 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200700
701 switch (config)
702 {
703 case CLASS_o9:
704 return extra_newl + NFA_DIGIT;
705 case CLASS_not | CLASS_o9:
706 return extra_newl + NFA_NDIGIT;
707 case CLASS_af | CLASS_AF | CLASS_o9:
708 return extra_newl + NFA_HEX;
709 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
710 return extra_newl + NFA_NHEX;
711 case CLASS_o7:
712 return extra_newl + NFA_OCTAL;
713 case CLASS_not | CLASS_o7:
714 return extra_newl + NFA_NOCTAL;
715 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
716 return extra_newl + NFA_WORD;
717 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
718 return extra_newl + NFA_NWORD;
719 case CLASS_az | CLASS_AZ | CLASS_underscore:
720 return extra_newl + NFA_HEAD;
721 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
722 return extra_newl + NFA_NHEAD;
723 case CLASS_az | CLASS_AZ:
724 return extra_newl + NFA_ALPHA;
725 case CLASS_not | CLASS_az | CLASS_AZ:
726 return extra_newl + NFA_NALPHA;
727 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200728 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200729 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200730 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200731 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200732 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200733 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200734 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200736 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200737}
738
739/*
740 * Produce the bytes for equivalence class "c".
741 * Currently only handles latin1, latin9 and utf-8.
742 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
743 * equivalent to 'a OR b OR c'
744 *
745 * NOTE! When changing this function, also update reg_equi_class()
746 */
747 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200748nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200751#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
752#ifdef FEAT_MBYTE
753# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
754#else
755# define EMITMBC(c)
756#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200757
758#ifdef FEAT_MBYTE
759 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
760 || STRCMP(p_enc, "iso-8859-15") == 0)
761#endif
762 {
763 switch (c)
764 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200765 case 'A': case 0300: case 0301: case 0302:
766 case 0303: case 0304: case 0305:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200767 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
768 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
769 EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302);
770 EMIT2(0303); EMIT2(0304); EMIT2(0305);
771 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
772 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
773 EMITMBC(0x1ea2)
774 return OK;
775
776 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
777 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200778 return OK;
779
Bram Moolenaar417bad22013-06-07 14:08:30 +0200780 case 'C': case 0307:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200781 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
782 EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108)
783 EMITMBC(0x10a) EMITMBC(0x10c)
784 return OK;
785
786 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
787 CASEMBC(0x1e0e) CASEMBC(0x1e10)
788 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
789 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200790 return OK;
791
Bram Moolenaar417bad22013-06-07 14:08:30 +0200792 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200793 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
794 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
795 EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312);
796 EMIT2(0313);
797 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
798 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
799 EMITMBC(0x1ebc)
800 return OK;
801
802 case 'F': CASEMBC(0x1e1e)
803 EMIT2('F'); EMITMBC(0x1e1e)
804 return OK;
805
806 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
807 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
808 CASEMBC(0x1e20)
809 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
810 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
811 EMITMBC(0x1f4) EMITMBC(0x1e20)
812 return OK;
813
814 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
815 CASEMBC(0x1e26) CASEMBC(0x1e28)
816 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
817 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200818 return OK;
819
Bram Moolenaar417bad22013-06-07 14:08:30 +0200820 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200821 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
822 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
823 EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316);
824 EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a)
825 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
826 EMITMBC(0x1cf) EMITMBC(0x1ec8)
827 return OK;
828
829 case 'J': CASEMBC(0x134)
830 EMIT2('J'); EMITMBC(0x134)
831 return OK;
832
833 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
834 CASEMBC(0x1e34)
835 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
836 EMITMBC(0x1e34)
837 return OK;
838
839 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
840 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
841 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
842 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
843 return OK;
844
845 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
846 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200847 return OK;
848
Bram Moolenaar417bad22013-06-07 14:08:30 +0200849 case 'N': case 0321:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200850 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
851 CASEMBC(0x1e48)
852 EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145)
853 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200854 return OK;
855
Bram Moolenaar417bad22013-06-07 14:08:30 +0200856 case 'O': case 0322: case 0323: case 0324: case 0325:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200857 case 0326: case 0330:
858 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
859 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
860 EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324);
861 EMIT2(0325); EMIT2(0326); EMIT2(0330);
862 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
863 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
864 EMITMBC(0x1ec) EMITMBC(0x1ece)
865 return OK;
866
867 case 'P': case 0x1e54: case 0x1e56:
868 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
869 return OK;
870
871 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
872 CASEMBC(0x1e58) CASEMBC(0x1e5e)
873 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
874 EMITMBC(0x1e58) EMITMBC(0x1e5e)
875 return OK;
876
877 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
878 CASEMBC(0x160) CASEMBC(0x1e60)
879 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
880 EMITMBC(0x160) EMITMBC(0x1e60)
881 return OK;
882
883 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
884 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
885 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
886 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200887 return OK;
888
Bram Moolenaar417bad22013-06-07 14:08:30 +0200889 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200890 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
891 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
892 CASEMBC(0x1ee6)
893 EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333);
894 EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a)
895 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
896 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
897 EMITMBC(0x1ee6)
898 return OK;
899
900 case 'V': CASEMBC(0x1e7c)
901 EMIT2('V'); EMITMBC(0x1e7c)
902 return OK;
903
904 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
905 CASEMBC(0x1e84) CASEMBC(0x1e86)
906 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
907 EMITMBC(0x1e84) EMITMBC(0x1e86)
908 return OK;
909
910 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
911 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200912 return OK;
913
Bram Moolenaar417bad22013-06-07 14:08:30 +0200914 case 'Y': case 0335:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200915 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
916 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
917 EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178)
918 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
919 EMITMBC(0x1ef8)
920 return OK;
921
922 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
923 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
924 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
925 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200926 return OK;
927
Bram Moolenaar417bad22013-06-07 14:08:30 +0200928 case 'a': case 0340: case 0341: case 0342:
929 case 0343: case 0344: case 0345:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200930 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
931 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
932 EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342);
933 EMIT2(0343); EMIT2(0344); EMIT2(0345);
934 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
935 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
936 EMITMBC(0x1ea3)
937 return OK;
938
939 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
940 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200941 return OK;
942
Bram Moolenaar417bad22013-06-07 14:08:30 +0200943 case 'c': case 0347:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200944 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
945 EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109)
946 EMITMBC(0x10b) EMITMBC(0x10d)
947 return OK;
948
949 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
950 CASEMBC(0x1e11)
951 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) EMITMBC(0x1e0b)
952 EMITMBC(0x01e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200953 return OK;
954
Bram Moolenaar417bad22013-06-07 14:08:30 +0200955 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200956 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
957 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
958 EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352);
959 EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115)
960 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
961 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
962 return OK;
963
964 case 'f': CASEMBC(0x1e1f)
965 EMIT2('f'); EMITMBC(0x1e1f)
966 return OK;
967
968 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
969 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
970 CASEMBC(0x1e21)
971 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
972 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
973 EMITMBC(0x1f5) EMITMBC(0x1e21)
974 return OK;
975
976 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
977 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
978 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
979 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200980 return OK;
981
Bram Moolenaar417bad22013-06-07 14:08:30 +0200982 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200983 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
984 CASEMBC(0x1d0) CASEMBC(0x1ec9)
985 EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356);
986 EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b)
987 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
988 EMITMBC(0x1ec9)
989 return OK;
990
991 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
992 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
993 return OK;
994
995 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
996 CASEMBC(0x1e35)
997 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
998 EMITMBC(0x1e35)
999 return OK;
1000
1001 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1002 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1003 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1004 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1005 return OK;
1006
1007 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1008 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001009 return OK;
1010
Bram Moolenaar417bad22013-06-07 14:08:30 +02001011 case 'n': case 0361:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001012 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1013 CASEMBC(0x1e45) CASEMBC(0x1e49)
1014 EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146)
1015 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1016 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001017 return OK;
1018
Bram Moolenaar417bad22013-06-07 14:08:30 +02001019 case 'o': case 0362: case 0363: case 0364: case 0365:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001020 case 0366: case 0370:
1021 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1022 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
1023 EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364);
1024 EMIT2(0365); EMIT2(0366); EMIT2(0370);
1025 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1026 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1027 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1028 return OK;
1029
1030 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1031 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1032 return OK;
1033
1034 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1035 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1036 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1037 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1038 return OK;
1039
1040 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1041 CASEMBC(0x161) CASEMBC(0x1e61)
1042 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1043 EMITMBC(0x161) EMITMBC(0x1e61)
1044 return OK;
1045
1046 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1047 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1048 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1049 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001050 return OK;
1051
Bram Moolenaar417bad22013-06-07 14:08:30 +02001052 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001053 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1054 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1055 CASEMBC(0x1ee7)
1056 EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373);
1057 EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b)
1058 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1059 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1060 EMITMBC(0x1ee7)
1061 return OK;
1062
1063 case 'v': CASEMBC(0x1e7d)
1064 EMIT2('v'); EMITMBC(0x1e7d)
1065 return OK;
1066
1067 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1068 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1069 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1070 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1071 return OK;
1072
1073 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1074 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001075 return OK;
1076
Bram Moolenaar417bad22013-06-07 14:08:30 +02001077 case 'y': case 0375: case 0377:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001078 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1079 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1080 EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177)
1081 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1082 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001083 return OK;
1084
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001085 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1086 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1087 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1088 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1089 return OK;
1090
1091 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001092 }
1093 }
1094
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001095 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001096 return OK;
1097#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001098#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001099}
1100
1101/*
1102 * Code to parse regular expression.
1103 *
1104 * We try to reuse parsing functions in regexp.c to
1105 * minimize surprise and keep the syntax consistent.
1106 */
1107
1108/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001109 * Parse the lowest level.
1110 *
1111 * An atom can be one of a long list of items. Many atoms match one character
1112 * in the text. It is often an ordinary character or a character class.
1113 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1114 * is only for syntax highlighting.
1115 *
1116 * atom ::= ordinary-atom
1117 * or \( pattern \)
1118 * or \%( pattern \)
1119 * or \z( pattern \)
1120 */
1121 static int
1122nfa_regatom()
1123{
1124 int c;
1125 int charclass;
1126 int equiclass;
1127 int collclass;
1128 int got_coll_char;
1129 char_u *p;
1130 char_u *endp;
1131#ifdef FEAT_MBYTE
1132 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001133#endif
1134 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001135 int emit_range;
1136 int negated;
1137 int result;
1138 int startc = -1;
1139 int endc = -1;
1140 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001142 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001143 switch (c)
1144 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001145 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001146 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001147
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001148 case Magic('^'):
1149 EMIT(NFA_BOL);
1150 break;
1151
1152 case Magic('$'):
1153 EMIT(NFA_EOL);
1154#if defined(FEAT_SYN_HL) || defined(PROTO)
1155 had_eol = TRUE;
1156#endif
1157 break;
1158
1159 case Magic('<'):
1160 EMIT(NFA_BOW);
1161 break;
1162
1163 case Magic('>'):
1164 EMIT(NFA_EOW);
1165 break;
1166
1167 case Magic('_'):
1168 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001169 if (c == NUL)
1170 EMSG_RET_FAIL(_(e_nul_found));
1171
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001172 if (c == '^') /* "\_^" is start-of-line */
1173 {
1174 EMIT(NFA_BOL);
1175 break;
1176 }
1177 if (c == '$') /* "\_$" is end-of-line */
1178 {
1179 EMIT(NFA_EOL);
1180#if defined(FEAT_SYN_HL) || defined(PROTO)
1181 had_eol = TRUE;
1182#endif
1183 break;
1184 }
1185
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001186 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001187
1188 /* "\_[" is collection plus newline */
1189 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001190 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001191
1192 /* "\_x" is character class plus newline */
1193 /*FALLTHROUGH*/
1194
1195 /*
1196 * Character classes.
1197 */
1198 case Magic('.'):
1199 case Magic('i'):
1200 case Magic('I'):
1201 case Magic('k'):
1202 case Magic('K'):
1203 case Magic('f'):
1204 case Magic('F'):
1205 case Magic('p'):
1206 case Magic('P'):
1207 case Magic('s'):
1208 case Magic('S'):
1209 case Magic('d'):
1210 case Magic('D'):
1211 case Magic('x'):
1212 case Magic('X'):
1213 case Magic('o'):
1214 case Magic('O'):
1215 case Magic('w'):
1216 case Magic('W'):
1217 case Magic('h'):
1218 case Magic('H'):
1219 case Magic('a'):
1220 case Magic('A'):
1221 case Magic('l'):
1222 case Magic('L'):
1223 case Magic('u'):
1224 case Magic('U'):
1225 p = vim_strchr(classchars, no_Magic(c));
1226 if (p == NULL)
1227 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001228 if (extra == NFA_ADD_NL)
1229 {
1230 EMSGN(_(e_ill_char_class), c);
1231 rc_did_emsg = TRUE;
1232 return FAIL;
1233 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001234 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1235 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001236 }
1237#ifdef FEAT_MBYTE
1238 /* When '.' is followed by a composing char ignore the dot, so that
1239 * the composing char is matched here. */
1240 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1241 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001242 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243 c = getchr();
1244 goto nfa_do_multibyte;
1245 }
1246#endif
1247 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001248 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001249 {
1250 EMIT(NFA_NEWL);
1251 EMIT(NFA_OR);
1252 regflags |= RF_HASNL;
1253 }
1254 break;
1255
1256 case Magic('n'):
1257 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001258 /* In a string "\n" matches a newline character. */
1259 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260 else
1261 {
1262 /* In buffer text "\n" matches the end of a line. */
1263 EMIT(NFA_NEWL);
1264 regflags |= RF_HASNL;
1265 }
1266 break;
1267
1268 case Magic('('):
1269 if (nfa_reg(REG_PAREN) == FAIL)
1270 return FAIL; /* cascaded error */
1271 break;
1272
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 case Magic('|'):
1274 case Magic('&'):
1275 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001276 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001277 return FAIL;
1278
1279 case Magic('='):
1280 case Magic('?'):
1281 case Magic('+'):
1282 case Magic('@'):
1283 case Magic('*'):
1284 case Magic('{'):
1285 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001286 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001287 return FAIL;
1288
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001289 case Magic('~'):
1290 {
1291 char_u *lp;
1292
1293 /* Previous substitute pattern.
1294 * Generated as "\%(pattern\)". */
1295 if (reg_prev_sub == NULL)
1296 {
1297 EMSG(_(e_nopresub));
1298 return FAIL;
1299 }
1300 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1301 {
1302 EMIT(PTR2CHAR(lp));
1303 if (lp != reg_prev_sub)
1304 EMIT(NFA_CONCAT);
1305 }
1306 EMIT(NFA_NOPEN);
1307 break;
1308 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001309
Bram Moolenaar428e9872013-05-30 17:05:39 +02001310 case Magic('1'):
1311 case Magic('2'):
1312 case Magic('3'):
1313 case Magic('4'):
1314 case Magic('5'):
1315 case Magic('6'):
1316 case Magic('7'):
1317 case Magic('8'):
1318 case Magic('9'):
1319 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1320 nfa_has_backref = TRUE;
1321 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001322
1323 case Magic('z'):
1324 c = no_Magic(getchr());
1325 switch (c)
1326 {
1327 case 's':
1328 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001329 if (re_mult_next("\\zs") == FAIL)
1330 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 break;
1332 case 'e':
1333 EMIT(NFA_ZEND);
1334 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001335 if (re_mult_next("\\ze") == FAIL)
1336 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001337 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001338#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001339 case '1':
1340 case '2':
1341 case '3':
1342 case '4':
1343 case '5':
1344 case '6':
1345 case '7':
1346 case '8':
1347 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001348 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001349 if (reg_do_extmatch != REX_USE)
1350 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001351 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1352 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001353 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001354 re_has_z = REX_USE;
1355 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001356 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001357 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001358 if (reg_do_extmatch != REX_SET)
1359 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001360 if (nfa_reg(REG_ZPAREN) == FAIL)
1361 return FAIL; /* cascaded error */
1362 re_has_z = REX_SET;
1363 break;
1364#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001365 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001366 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001367 no_Magic(c));
1368 return FAIL;
1369 }
1370 break;
1371
1372 case Magic('%'):
1373 c = no_Magic(getchr());
1374 switch (c)
1375 {
1376 /* () without a back reference */
1377 case '(':
1378 if (nfa_reg(REG_NPAREN) == FAIL)
1379 return FAIL;
1380 EMIT(NFA_NOPEN);
1381 break;
1382
1383 case 'd': /* %d123 decimal */
1384 case 'o': /* %o123 octal */
1385 case 'x': /* %xab hex 2 */
1386 case 'u': /* %uabcd hex 4 */
1387 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001388 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001389 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390
Bram Moolenaar47196582013-05-25 22:04:23 +02001391 switch (c)
1392 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001393 case 'd': nr = getdecchrs(); break;
1394 case 'o': nr = getoctchrs(); break;
1395 case 'x': nr = gethexchrs(2); break;
1396 case 'u': nr = gethexchrs(4); break;
1397 case 'U': nr = gethexchrs(8); break;
1398 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001399 }
1400
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001401 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001402 EMSG2_RET_FAIL(
1403 _("E678: Invalid character after %s%%[dxouU]"),
1404 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001405 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001406 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001407 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001408 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001409 break;
1410
1411 /* Catch \%^ and \%$ regardless of where they appear in the
1412 * pattern -- regardless of whether or not it makes sense. */
1413 case '^':
1414 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001415 break;
1416
1417 case '$':
1418 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001419 break;
1420
1421 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001422 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423 break;
1424
1425 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001426 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001427 break;
1428
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001429 case 'C':
1430 EMIT(NFA_ANY_COMPOSING);
1431 break;
1432
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001433 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001434 {
1435 int n;
1436
1437 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001438 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001439 {
1440 if (c == NUL)
1441 EMSG2_RET_FAIL(_(e_missing_sb),
1442 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001443 /* recursive call! */
1444 if (nfa_regatom() == FAIL)
1445 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001446 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001447 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001448 if (n == 0)
1449 EMSG2_RET_FAIL(_(e_empty_sb),
1450 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001451 EMIT(NFA_OPT_CHARS);
1452 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001453
1454 /* Emit as "\%(\%[abc]\)" to be able to handle
1455 * "\%[abc]*" which would cause the empty string to be
1456 * matched an unlimited number of times. NFA_NOPEN is
1457 * added only once at a position, while NFA_SPLIT is
1458 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001459 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001460 * a lot. */
1461 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001462 break;
1463 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001466 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001467 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001468 int cmp = c;
1469
1470 if (c == '<' || c == '>')
1471 c = getchr();
1472 while (VIM_ISDIGIT(c))
1473 {
1474 n = n * 10 + (c - '0');
1475 c = getchr();
1476 }
1477 if (c == 'l' || c == 'c' || c == 'v')
1478 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001479 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001480 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001481 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001482 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001483 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001484 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001485 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001486 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001487 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001488 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001489 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001490 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001491 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001492 break;
1493 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001494 else if (c == '\'' && n == 0)
1495 {
1496 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001497 EMIT(cmp == '<' ? NFA_MARK_LT :
1498 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001499 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001500 break;
1501 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001502 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001503 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1504 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001505 return FAIL;
1506 }
1507 break;
1508
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001510collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001512 * [abc] uses NFA_START_COLL - NFA_END_COLL
1513 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1514 * Each character is produced as a regular state, using
1515 * NFA_CONCAT to bind them together.
1516 * Besides normal characters there can be:
1517 * - character classes NFA_CLASS_*
1518 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001519 */
1520
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521 p = regparse;
1522 endp = skip_anyof(p);
1523 if (*endp == ']')
1524 {
1525 /*
1526 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001527 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001528 * and perform the necessary substitutions in the NFA.
1529 */
1530 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001531 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 if (result != FAIL)
1533 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001534 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001535 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001536 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 EMIT(NFA_NEWL);
1538 EMIT(NFA_OR);
1539 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001540 else
1541 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001543 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001544 return OK;
1545 }
1546 /*
1547 * Failed to recognize a character class. Use the simple
1548 * version that turns [abc] into 'a' OR 'b' OR 'c'
1549 */
1550 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001552 if (*regparse == '^') /* negated range */
1553 {
1554 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001555 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001556 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001557 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001558 else
1559 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001560 if (*regparse == '-')
1561 {
1562 startc = '-';
1563 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001564 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001565 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001566 }
1567 /* Emit the OR branches for each character in the [] */
1568 emit_range = FALSE;
1569 while (regparse < endp)
1570 {
1571 oldstartc = startc;
1572 startc = -1;
1573 got_coll_char = FALSE;
1574 if (*regparse == '[')
1575 {
1576 /* Check for [: :], [= =], [. .] */
1577 equiclass = collclass = 0;
1578 charclass = get_char_class(&regparse);
1579 if (charclass == CLASS_NONE)
1580 {
1581 equiclass = get_equi_class(&regparse);
1582 if (equiclass == 0)
1583 collclass = get_coll_element(&regparse);
1584 }
1585
1586 /* Character class like [:alpha:] */
1587 if (charclass != CLASS_NONE)
1588 {
1589 switch (charclass)
1590 {
1591 case CLASS_ALNUM:
1592 EMIT(NFA_CLASS_ALNUM);
1593 break;
1594 case CLASS_ALPHA:
1595 EMIT(NFA_CLASS_ALPHA);
1596 break;
1597 case CLASS_BLANK:
1598 EMIT(NFA_CLASS_BLANK);
1599 break;
1600 case CLASS_CNTRL:
1601 EMIT(NFA_CLASS_CNTRL);
1602 break;
1603 case CLASS_DIGIT:
1604 EMIT(NFA_CLASS_DIGIT);
1605 break;
1606 case CLASS_GRAPH:
1607 EMIT(NFA_CLASS_GRAPH);
1608 break;
1609 case CLASS_LOWER:
1610 EMIT(NFA_CLASS_LOWER);
1611 break;
1612 case CLASS_PRINT:
1613 EMIT(NFA_CLASS_PRINT);
1614 break;
1615 case CLASS_PUNCT:
1616 EMIT(NFA_CLASS_PUNCT);
1617 break;
1618 case CLASS_SPACE:
1619 EMIT(NFA_CLASS_SPACE);
1620 break;
1621 case CLASS_UPPER:
1622 EMIT(NFA_CLASS_UPPER);
1623 break;
1624 case CLASS_XDIGIT:
1625 EMIT(NFA_CLASS_XDIGIT);
1626 break;
1627 case CLASS_TAB:
1628 EMIT(NFA_CLASS_TAB);
1629 break;
1630 case CLASS_RETURN:
1631 EMIT(NFA_CLASS_RETURN);
1632 break;
1633 case CLASS_BACKSPACE:
1634 EMIT(NFA_CLASS_BACKSPACE);
1635 break;
1636 case CLASS_ESCAPE:
1637 EMIT(NFA_CLASS_ESCAPE);
1638 break;
1639 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001640 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001641 continue;
1642 }
1643 /* Try equivalence class [=a=] and the like */
1644 if (equiclass != 0)
1645 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001646 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001647 if (result == FAIL)
1648 {
1649 /* should never happen */
1650 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1651 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001652 continue;
1653 }
1654 /* Try collating class like [. .] */
1655 if (collclass != 0)
1656 {
1657 startc = collclass; /* allow [.a.]-x as a range */
1658 /* Will emit the proper atom at the end of the
1659 * while loop. */
1660 }
1661 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001662 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1663 * start character. */
1664 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001665 {
1666 emit_range = TRUE;
1667 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001668 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 continue; /* reading the end of the range */
1670 }
1671
1672 /* Now handle simple and escaped characters.
1673 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1674 * accepts "\t", "\e", etc., but only when the 'l' flag in
1675 * 'cpoptions' is not included.
1676 * Posix doesn't recognize backslash at all.
1677 */
1678 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001679 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001680 && regparse + 1 <= endp
1681 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001682 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001683 && vim_strchr(REGEXP_ABBR, regparse[1])
1684 != NULL)
1685 )
1686 )
1687 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001688 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001690 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 startc = reg_string ? NL : NFA_NEWL;
1692 else
1693 if (*regparse == 'd'
1694 || *regparse == 'o'
1695 || *regparse == 'x'
1696 || *regparse == 'u'
1697 || *regparse == 'U'
1698 )
1699 {
1700 /* TODO(RE) This needs more testing */
1701 startc = coll_get_char();
1702 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001703 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001704 }
1705 else
1706 {
1707 /* \r,\t,\e,\b */
1708 startc = backslash_trans(*regparse);
1709 }
1710 }
1711
1712 /* Normal printable char */
1713 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001714 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001715
1716 /* Previous char was '-', so this char is end of range. */
1717 if (emit_range)
1718 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001719 endc = startc;
1720 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001721 if (startc > endc)
1722 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001723
1724 if (endc > startc + 2)
1725 {
1726 /* Emit a range instead of the sequence of
1727 * individual characters. */
1728 if (startc == 0)
1729 /* \x00 is translated to \x0a, start at \x01. */
1730 EMIT(1);
1731 else
1732 --post_ptr; /* remove NFA_CONCAT */
1733 EMIT(endc);
1734 EMIT(NFA_RANGE);
1735 EMIT(NFA_CONCAT);
1736 }
1737 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001739 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740 || (*mb_char2len)(endc) > 1))
1741 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001742 /* Emit the characters in the range.
1743 * "startc" was already emitted, so skip it.
1744 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745 for (c = startc + 1; c <= endc; c++)
1746 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001747 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001748 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001749 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001750 }
1751 else
1752#endif
1753 {
1754#ifdef EBCDIC
1755 int alpha_only = FALSE;
1756
1757 /* for alphabetical range skip the gaps
1758 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1759 if (isalpha(startc) && isalpha(endc))
1760 alpha_only = TRUE;
1761#endif
1762 /* Emit the range. "startc" was already emitted, so
1763 * skip it. */
1764 for (c = startc + 1; c <= endc; c++)
1765#ifdef EBCDIC
1766 if (!alpha_only || isalpha(startc))
1767#endif
1768 {
1769 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001770 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001771 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001772 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001773 emit_range = FALSE;
1774 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001775 }
1776 else
1777 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001778 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001779 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780 * Normally, simply emit startc. But if we get char
1781 * code=0 from a collating char, then replace it with
1782 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001784 * the backtracking engine. */
1785 if (startc == NFA_NEWL)
1786 {
1787 /* Line break can't be matched as part of the
1788 * collection, add an OR below. But not for negated
1789 * range. */
1790 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001791 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001792 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001793 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001794 {
1795 if (got_coll_char == TRUE && startc == 0)
1796 EMIT(0x0a);
1797 else
1798 EMIT(startc);
1799 EMIT(NFA_CONCAT);
1800 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 }
1802
Bram Moolenaar51a29832013-05-28 22:30:35 +02001803 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001804 } /* while (p < endp) */
1805
Bram Moolenaar51a29832013-05-28 22:30:35 +02001806 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001807 if (*regparse == '-') /* if last, '-' is just a char */
1808 {
1809 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001810 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001811 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001813 /* skip the trailing ] */
1814 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001815 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001816
1817 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001818 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001819 EMIT(NFA_END_NEG_COLL);
1820 else
1821 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001822
1823 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001824 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001825 {
1826 EMIT(reg_string ? NL : NFA_NEWL);
1827 EMIT(NFA_OR);
1828 }
1829
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001830 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001831 } /* if exists closing ] */
1832
1833 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001835 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001836
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001837 default:
1838 {
1839#ifdef FEAT_MBYTE
1840 int plen;
1841
1842nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001843 /* plen is length of current char with composing chars */
1844 if (enc_utf8 && ((*mb_char2len)(c)
1845 != (plen = (*mb_ptr2len)(old_regparse))
1846 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001848 int i = 0;
1849
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001850 /* A base character plus composing characters, or just one
1851 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001852 * This requires creating a separate atom as if enclosing
1853 * the characters in (), where NFA_COMPOSING is the ( and
1854 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 * building the postfix form, not the NFA itself;
1856 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001857 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001858 for (;;)
1859 {
1860 EMIT(c);
1861 if (i > 0)
1862 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001863 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001864 break;
1865 c = utf_ptr2char(old_regparse + i);
1866 }
1867 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 regparse = old_regparse + plen;
1869 }
1870 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871#endif
1872 {
1873 c = no_Magic(c);
1874 EMIT(c);
1875 }
1876 return OK;
1877 }
1878 }
1879
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 return OK;
1881}
1882
1883/*
1884 * Parse something followed by possible [*+=].
1885 *
1886 * A piece is an atom, possibly followed by a multi, an indication of how many
1887 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1888 * characters: "", "a", "aa", etc.
1889 *
1890 * piece ::= atom
1891 * or atom multi
1892 */
1893 static int
1894nfa_regpiece()
1895{
1896 int i;
1897 int op;
1898 int ret;
1899 long minval, maxval;
1900 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001901 parse_state_T old_state;
1902 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001904 int old_post_pos;
1905 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 int quest;
1907
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001908 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1909 * next. */
1910 save_parse_state(&old_state);
1911
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001913 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914
1915 ret = nfa_regatom();
1916 if (ret == FAIL)
1917 return FAIL; /* cascaded error */
1918
1919 op = peekchr();
1920 if (re_multi_type(op) == NOT_MULTI)
1921 return OK;
1922
1923 skipchr();
1924 switch (op)
1925 {
1926 case Magic('*'):
1927 EMIT(NFA_STAR);
1928 break;
1929
1930 case Magic('+'):
1931 /*
1932 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1933 * first and only submatch would be "aaa". But the backtracking
1934 * engine interprets the plus as "try matching one more time", and
1935 * a* matches a second time at the end of the input, the empty
1936 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001937 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001939 * In order to be consistent with the old engine, we replace
1940 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001942 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 curchr = -1;
1944 if (nfa_regatom() == FAIL)
1945 return FAIL;
1946 EMIT(NFA_STAR);
1947 EMIT(NFA_CONCAT);
1948 skipchr(); /* skip the \+ */
1949 break;
1950
1951 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001952 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001954 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001955 switch(op)
1956 {
1957 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001958 /* \@= */
1959 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001961 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001962 /* \@! */
1963 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001964 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001965 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001966 op = no_Magic(getchr());
1967 if (op == '=')
1968 /* \@<= */
1969 i = NFA_PREV_ATOM_JUST_BEFORE;
1970 else if (op == '!')
1971 /* \@<! */
1972 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1973 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001974 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001975 /* \@> */
1976 i = NFA_PREV_ATOM_LIKE_PATTERN;
1977 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001978 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001979 if (i == 0)
1980 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001981 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1982 return FAIL;
1983 }
1984 EMIT(i);
1985 if (i == NFA_PREV_ATOM_JUST_BEFORE
1986 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1987 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001988 break;
1989
1990 case Magic('?'):
1991 case Magic('='):
1992 EMIT(NFA_QUEST);
1993 break;
1994
1995 case Magic('{'):
1996 /* a{2,5} will expand to 'aaa?a?a?'
1997 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1998 * version of '?'
1999 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2000 * parenthesis have the same id
2001 */
2002
2003 greedy = TRUE;
2004 c2 = peekchr();
2005 if (c2 == '-' || c2 == Magic('-'))
2006 {
2007 skipchr();
2008 greedy = FALSE;
2009 }
2010 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002012
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002013 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2014 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002015 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002016 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002017 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002018 /* \{}, \{0,} */
2019 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002020 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002021 /* \{-}, \{-0,} */
2022 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 break;
2024 }
2025
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002026 /* Special case: x{0} or x{-0} */
2027 if (maxval == 0)
2028 {
2029 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002030 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002031 /* NFA_EMPTY is 0-length and works everywhere */
2032 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033 return OK;
2034 }
2035
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002036 /* The engine is very inefficient (uses too many states) when the
2037 * maximum is much larger than the minimum. Bail out if we can
2038 * use the other engine. */
2039 if ((nfa_re_flags & RE_AUTO) && maxval > minval + 200)
2040 return FAIL;
2041
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002043 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002044 /* Save parse state after the repeated atom and the \{} */
2045 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002047 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2048 for (i = 0; i < maxval; i++)
2049 {
2050 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002051 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002052 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 if (nfa_regatom() == FAIL)
2054 return FAIL;
2055 /* after "minval" times, atoms are optional */
2056 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002057 {
2058 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002059 {
2060 if (greedy)
2061 EMIT(NFA_STAR);
2062 else
2063 EMIT(NFA_STAR_NONGREEDY);
2064 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002065 else
2066 EMIT(quest);
2067 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002068 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002070 if (i + 1 > minval && maxval == MAX_LIMIT)
2071 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 }
2073
2074 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002075 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002076 curchr = -1;
2077
2078 break;
2079
2080
2081 default:
2082 break;
2083 } /* end switch */
2084
2085 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002086 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002088
2089 return OK;
2090}
2091
2092/*
2093 * Parse one or more pieces, concatenated. It matches a match for the
2094 * first piece, followed by a match for the second piece, etc. Example:
2095 * "f[0-9]b", first matches "f", then a digit and then "b".
2096 *
2097 * concat ::= piece
2098 * or piece piece
2099 * or piece piece piece
2100 * etc.
2101 */
2102 static int
2103nfa_regconcat()
2104{
2105 int cont = TRUE;
2106 int first = TRUE;
2107
2108 while (cont)
2109 {
2110 switch (peekchr())
2111 {
2112 case NUL:
2113 case Magic('|'):
2114 case Magic('&'):
2115 case Magic(')'):
2116 cont = FALSE;
2117 break;
2118
2119 case Magic('Z'):
2120#ifdef FEAT_MBYTE
2121 regflags |= RF_ICOMBINE;
2122#endif
2123 skipchr_keepstart();
2124 break;
2125 case Magic('c'):
2126 regflags |= RF_ICASE;
2127 skipchr_keepstart();
2128 break;
2129 case Magic('C'):
2130 regflags |= RF_NOICASE;
2131 skipchr_keepstart();
2132 break;
2133 case Magic('v'):
2134 reg_magic = MAGIC_ALL;
2135 skipchr_keepstart();
2136 curchr = -1;
2137 break;
2138 case Magic('m'):
2139 reg_magic = MAGIC_ON;
2140 skipchr_keepstart();
2141 curchr = -1;
2142 break;
2143 case Magic('M'):
2144 reg_magic = MAGIC_OFF;
2145 skipchr_keepstart();
2146 curchr = -1;
2147 break;
2148 case Magic('V'):
2149 reg_magic = MAGIC_NONE;
2150 skipchr_keepstart();
2151 curchr = -1;
2152 break;
2153
2154 default:
2155 if (nfa_regpiece() == FAIL)
2156 return FAIL;
2157 if (first == FALSE)
2158 EMIT(NFA_CONCAT);
2159 else
2160 first = FALSE;
2161 break;
2162 }
2163 }
2164
2165 return OK;
2166}
2167
2168/*
2169 * Parse a branch, one or more concats, separated by "\&". It matches the
2170 * last concat, but only if all the preceding concats also match at the same
2171 * position. Examples:
2172 * "foobeep\&..." matches "foo" in "foobeep".
2173 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2174 *
2175 * branch ::= concat
2176 * or concat \& concat
2177 * or concat \& concat \& concat
2178 * etc.
2179 */
2180 static int
2181nfa_regbranch()
2182{
2183 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002184 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002185
Bram Moolenaar16299b52013-05-30 18:45:23 +02002186 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187
2188 /* First branch, possibly the only one */
2189 if (nfa_regconcat() == FAIL)
2190 return FAIL;
2191
2192 ch = peekchr();
2193 /* Try next concats */
2194 while (ch == Magic('&'))
2195 {
2196 skipchr();
2197 EMIT(NFA_NOPEN);
2198 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002199 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002200 if (nfa_regconcat() == FAIL)
2201 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002202 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002203 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002204 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002205 EMIT(NFA_CONCAT);
2206 ch = peekchr();
2207 }
2208
Bram Moolenaar699c1202013-09-25 16:41:54 +02002209 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002210 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002211 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002212
2213 return OK;
2214}
2215
2216/*
2217 * Parse a pattern, one or more branches, separated by "\|". It matches
2218 * anything that matches one of the branches. Example: "foo\|beep" matches
2219 * "foo" and matches "beep". If more than one branch matches, the first one
2220 * is used.
2221 *
2222 * pattern ::= branch
2223 * or branch \| branch
2224 * or branch \| branch \| branch
2225 * etc.
2226 */
2227 static int
2228nfa_reg(paren)
2229 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
2230{
2231 int parno = 0;
2232
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233 if (paren == REG_PAREN)
2234 {
2235 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237 parno = regnpar++;
2238 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002239#ifdef FEAT_SYN_HL
2240 else if (paren == REG_ZPAREN)
2241 {
2242 /* Make a ZOPEN node. */
2243 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002244 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002245 parno = regnzpar++;
2246 }
2247#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002248
2249 if (nfa_regbranch() == FAIL)
2250 return FAIL; /* cascaded error */
2251
2252 while (peekchr() == Magic('|'))
2253 {
2254 skipchr();
2255 if (nfa_regbranch() == FAIL)
2256 return FAIL; /* cascaded error */
2257 EMIT(NFA_OR);
2258 }
2259
2260 /* Check for proper termination. */
2261 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2262 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002263 if (paren == REG_NPAREN)
2264 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2265 else
2266 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2267 }
2268 else if (paren == REG_NOPAREN && peekchr() != NUL)
2269 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002270 if (peekchr() == Magic(')'))
2271 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2272 else
2273 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2274 }
2275 /*
2276 * Here we set the flag allowing back references to this set of
2277 * parentheses.
2278 */
2279 if (paren == REG_PAREN)
2280 {
2281 had_endbrace[parno] = TRUE; /* have seen the close paren */
2282 EMIT(NFA_MOPEN + parno);
2283 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002284#ifdef FEAT_SYN_HL
2285 else if (paren == REG_ZPAREN)
2286 EMIT(NFA_ZOPEN + parno);
2287#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288
2289 return OK;
2290}
2291
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002292#ifdef DEBUG
2293static char_u code[50];
2294
2295 static void
2296nfa_set_code(c)
2297 int c;
2298{
2299 int addnl = FALSE;
2300
2301 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2302 {
2303 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002304 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002305 }
2306
2307 STRCPY(code, "");
2308 switch (c)
2309 {
2310 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2311 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2312 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2313 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2314 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2315 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2316
Bram Moolenaar5714b802013-05-28 22:03:20 +02002317 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2318 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2319 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2320 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2321 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2322 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2323 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2324 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2325 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002326#ifdef FEAT_SYN_HL
2327 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2328 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2329 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2330 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2331 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2332 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2333 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2334 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2335 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2336#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002337 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 case NFA_PREV_ATOM_NO_WIDTH:
2340 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002341 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2342 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002343 case NFA_PREV_ATOM_JUST_BEFORE:
2344 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2345 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2346 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002347 case NFA_PREV_ATOM_LIKE_PATTERN:
2348 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2349
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002350 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2351 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002353 case NFA_START_INVISIBLE_FIRST:
2354 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002355 case NFA_START_INVISIBLE_NEG:
2356 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002357 case NFA_START_INVISIBLE_NEG_FIRST:
2358 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002359 case NFA_START_INVISIBLE_BEFORE:
2360 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002361 case NFA_START_INVISIBLE_BEFORE_FIRST:
2362 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002363 case NFA_START_INVISIBLE_BEFORE_NEG:
2364 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002365 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2366 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002367 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002368 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002369 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002370 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2373 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002374 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002375
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002376 case NFA_MOPEN:
2377 case NFA_MOPEN1:
2378 case NFA_MOPEN2:
2379 case NFA_MOPEN3:
2380 case NFA_MOPEN4:
2381 case NFA_MOPEN5:
2382 case NFA_MOPEN6:
2383 case NFA_MOPEN7:
2384 case NFA_MOPEN8:
2385 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002386 STRCPY(code, "NFA_MOPEN(x)");
2387 code[10] = c - NFA_MOPEN + '0';
2388 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002389 case NFA_MCLOSE:
2390 case NFA_MCLOSE1:
2391 case NFA_MCLOSE2:
2392 case NFA_MCLOSE3:
2393 case NFA_MCLOSE4:
2394 case NFA_MCLOSE5:
2395 case NFA_MCLOSE6:
2396 case NFA_MCLOSE7:
2397 case NFA_MCLOSE8:
2398 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002399 STRCPY(code, "NFA_MCLOSE(x)");
2400 code[11] = c - NFA_MCLOSE + '0';
2401 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002402#ifdef FEAT_SYN_HL
2403 case NFA_ZOPEN:
2404 case NFA_ZOPEN1:
2405 case NFA_ZOPEN2:
2406 case NFA_ZOPEN3:
2407 case NFA_ZOPEN4:
2408 case NFA_ZOPEN5:
2409 case NFA_ZOPEN6:
2410 case NFA_ZOPEN7:
2411 case NFA_ZOPEN8:
2412 case NFA_ZOPEN9:
2413 STRCPY(code, "NFA_ZOPEN(x)");
2414 code[10] = c - NFA_ZOPEN + '0';
2415 break;
2416 case NFA_ZCLOSE:
2417 case NFA_ZCLOSE1:
2418 case NFA_ZCLOSE2:
2419 case NFA_ZCLOSE3:
2420 case NFA_ZCLOSE4:
2421 case NFA_ZCLOSE5:
2422 case NFA_ZCLOSE6:
2423 case NFA_ZCLOSE7:
2424 case NFA_ZCLOSE8:
2425 case NFA_ZCLOSE9:
2426 STRCPY(code, "NFA_ZCLOSE(x)");
2427 code[11] = c - NFA_ZCLOSE + '0';
2428 break;
2429#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2431 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2432 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2433 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002434 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2435 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002436 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2437 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2438 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2439 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2440 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2441 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2442 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2443 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2444 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2445 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2446 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2447 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2448 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2449 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002450 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002451
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002453 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2454 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2455 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002456 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002457 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002458
2459 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2460 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2461 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2462 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2463 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2464 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2465 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2466
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002467 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2468 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2469 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2470 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2471 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2472 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2473 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2474 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2475 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2476 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2477 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2478 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2479 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2480 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2481 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2482 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2483
2484 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2485 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2486 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2487 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2488 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2489 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2490 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2491 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2492 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2493 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2494 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2495 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2496 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2497 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2498 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2499 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2500 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2501 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2502 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2503 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2504 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2505 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2506 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2507 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2508 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2509 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2510 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002511 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2512 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2513 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2514 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515
2516 default:
2517 STRCPY(code, "CHAR(x)");
2518 code[5] = c;
2519 }
2520
2521 if (addnl == TRUE)
2522 STRCAT(code, " + NEWLINE ");
2523
2524}
2525
2526#ifdef ENABLE_LOG
2527static FILE *log_fd;
2528
2529/*
2530 * Print the postfix notation of the current regexp.
2531 */
2532 static void
2533nfa_postfix_dump(expr, retval)
2534 char_u *expr;
2535 int retval;
2536{
2537 int *p;
2538 FILE *f;
2539
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002540 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002541 if (f != NULL)
2542 {
2543 fprintf(f, "\n-------------------------\n");
2544 if (retval == FAIL)
2545 fprintf(f, ">>> NFA engine failed ... \n");
2546 else if (retval == OK)
2547 fprintf(f, ">>> NFA engine succeeded !\n");
2548 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002549 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550 {
2551 nfa_set_code(*p);
2552 fprintf(f, "%s, ", code);
2553 }
2554 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002555 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002556 fprintf(f, "%d ", *p);
2557 fprintf(f, "\n\n");
2558 fclose(f);
2559 }
2560}
2561
2562/*
2563 * Print the NFA starting with a root node "state".
2564 */
2565 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002566nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002567 FILE *debugf;
2568 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002569{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002570 garray_T indent;
2571
2572 ga_init2(&indent, 1, 64);
2573 ga_append(&indent, '\0');
2574 nfa_print_state2(debugf, state, &indent);
2575 ga_clear(&indent);
2576}
2577
2578 static void
2579nfa_print_state2(debugf, state, indent)
2580 FILE *debugf;
2581 nfa_state_T *state;
2582 garray_T *indent;
2583{
2584 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002585
2586 if (state == NULL)
2587 return;
2588
2589 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002590
2591 /* Output indent */
2592 p = (char_u *)indent->ga_data;
2593 if (indent->ga_len >= 3)
2594 {
2595 int last = indent->ga_len - 3;
2596 char_u save[2];
2597
2598 STRNCPY(save, &p[last], 2);
2599 STRNCPY(&p[last], "+-", 2);
2600 fprintf(debugf, " %s", p);
2601 STRNCPY(&p[last], save, 2);
2602 }
2603 else
2604 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002605
2606 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002607 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002608 code,
2609 state->c,
2610 abs(state->id),
2611 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002612 if (state->id < 0)
2613 return;
2614
2615 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002616
2617 /* grow indent for state->out */
2618 indent->ga_len -= 1;
2619 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002620 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002621 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002622 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002623 ga_append(indent, '\0');
2624
2625 nfa_print_state2(debugf, state->out, indent);
2626
2627 /* replace last part of indent for state->out1 */
2628 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002629 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002630 ga_append(indent, '\0');
2631
2632 nfa_print_state2(debugf, state->out1, indent);
2633
2634 /* shrink indent */
2635 indent->ga_len -= 3;
2636 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002637}
2638
2639/*
2640 * Print the NFA state machine.
2641 */
2642 static void
2643nfa_dump(prog)
2644 nfa_regprog_T *prog;
2645{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002646 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647
2648 if (debugf != NULL)
2649 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002650 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002651
Bram Moolenaar473de612013-06-08 18:19:48 +02002652 if (prog->reganch)
2653 fprintf(debugf, "reganch: %d\n", prog->reganch);
2654 if (prog->regstart != NUL)
2655 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2656 prog->regstart, prog->regstart);
2657 if (prog->match_text != NULL)
2658 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002659
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002660 fclose(debugf);
2661 }
2662}
2663#endif /* ENABLE_LOG */
2664#endif /* DEBUG */
2665
2666/*
2667 * Parse r.e. @expr and convert it into postfix form.
2668 * Return the postfix string on success, NULL otherwise.
2669 */
2670 static int *
2671re2post()
2672{
2673 if (nfa_reg(REG_NOPAREN) == FAIL)
2674 return NULL;
2675 EMIT(NFA_MOPEN);
2676 return post_start;
2677}
2678
2679/* NB. Some of the code below is inspired by Russ's. */
2680
2681/*
2682 * Represents an NFA state plus zero or one or two arrows exiting.
2683 * if c == MATCH, no arrows out; matching state.
2684 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2685 * If c < 256, labeled arrow with character c to out.
2686 */
2687
2688static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2689
2690/*
2691 * Allocate and initialize nfa_state_T.
2692 */
2693 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002694alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002695 int c;
2696 nfa_state_T *out;
2697 nfa_state_T *out1;
2698{
2699 nfa_state_T *s;
2700
2701 if (istate >= nstate)
2702 return NULL;
2703
2704 s = &state_ptr[istate++];
2705
2706 s->c = c;
2707 s->out = out;
2708 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002709 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002710
2711 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002712 s->lastlist[0] = 0;
2713 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714
2715 return s;
2716}
2717
2718/*
2719 * A partially built NFA without the matching state filled in.
2720 * Frag_T.start points at the start state.
2721 * Frag_T.out is a list of places that need to be set to the
2722 * next state for this fragment.
2723 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002724
2725/* Since the out pointers in the list are always
2726 * uninitialized, we use the pointers themselves
2727 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002728typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002729union Ptrlist
2730{
2731 Ptrlist *next;
2732 nfa_state_T *s;
2733};
2734
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002735struct Frag
2736{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002737 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 Ptrlist *out;
2739};
2740typedef struct Frag Frag_T;
2741
2742static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2743static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2744static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2745static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2746static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2747static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2748
2749/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002750 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002751 */
2752 static Frag_T
2753frag(start, out)
2754 nfa_state_T *start;
2755 Ptrlist *out;
2756{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002757 Frag_T n;
2758
2759 n.start = start;
2760 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002761 return n;
2762}
2763
2764/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765 * Create singleton list containing just outp.
2766 */
2767 static Ptrlist *
2768list1(outp)
2769 nfa_state_T **outp;
2770{
2771 Ptrlist *l;
2772
2773 l = (Ptrlist *)outp;
2774 l->next = NULL;
2775 return l;
2776}
2777
2778/*
2779 * Patch the list of states at out to point to start.
2780 */
2781 static void
2782patch(l, s)
2783 Ptrlist *l;
2784 nfa_state_T *s;
2785{
2786 Ptrlist *next;
2787
2788 for (; l; l = next)
2789 {
2790 next = l->next;
2791 l->s = s;
2792 }
2793}
2794
2795
2796/*
2797 * Join the two lists l1 and l2, returning the combination.
2798 */
2799 static Ptrlist *
2800append(l1, l2)
2801 Ptrlist *l1;
2802 Ptrlist *l2;
2803{
2804 Ptrlist *oldl1;
2805
2806 oldl1 = l1;
2807 while (l1->next)
2808 l1 = l1->next;
2809 l1->next = l2;
2810 return oldl1;
2811}
2812
2813/*
2814 * Stack used for transforming postfix form into NFA.
2815 */
2816static Frag_T empty;
2817
2818 static void
2819st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002820 int *postfix UNUSED;
2821 int *end UNUSED;
2822 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002823{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002824#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002825 FILE *df;
2826 int *p2;
2827
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002828 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002829 if (df)
2830 {
2831 fprintf(df, "Error popping the stack!\n");
2832#ifdef DEBUG
2833 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2834#endif
2835 fprintf(df, "Postfix form is: ");
2836#ifdef DEBUG
2837 for (p2 = postfix; p2 < end; p2++)
2838 {
2839 nfa_set_code(*p2);
2840 fprintf(df, "%s, ", code);
2841 }
2842 nfa_set_code(*p);
2843 fprintf(df, "\nCurrent position is: ");
2844 for (p2 = postfix; p2 <= p; p2 ++)
2845 {
2846 nfa_set_code(*p2);
2847 fprintf(df, "%s, ", code);
2848 }
2849#else
2850 for (p2 = postfix; p2 < end; p2++)
2851 {
2852 fprintf(df, "%d, ", *p2);
2853 }
2854 fprintf(df, "\nCurrent position is: ");
2855 for (p2 = postfix; p2 <= p; p2 ++)
2856 {
2857 fprintf(df, "%d, ", *p2);
2858 }
2859#endif
2860 fprintf(df, "\n--------------------------\n");
2861 fclose(df);
2862 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002863#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002864 EMSG(_("E874: (NFA) Could not pop the stack !"));
2865}
2866
2867/*
2868 * Push an item onto the stack.
2869 */
2870 static void
2871st_push(s, p, stack_end)
2872 Frag_T s;
2873 Frag_T **p;
2874 Frag_T *stack_end;
2875{
2876 Frag_T *stackp = *p;
2877
2878 if (stackp >= stack_end)
2879 return;
2880 *stackp = s;
2881 *p = *p + 1;
2882}
2883
2884/*
2885 * Pop an item from the stack.
2886 */
2887 static Frag_T
2888st_pop(p, stack)
2889 Frag_T **p;
2890 Frag_T *stack;
2891{
2892 Frag_T *stackp;
2893
2894 *p = *p - 1;
2895 stackp = *p;
2896 if (stackp < stack)
2897 return empty;
2898 return **p;
2899}
2900
2901/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002902 * Estimate the maximum byte length of anything matching "state".
2903 * When unknown or unlimited return -1.
2904 */
2905 static int
2906nfa_max_width(startstate, depth)
2907 nfa_state_T *startstate;
2908 int depth;
2909{
2910 int l, r;
2911 nfa_state_T *state = startstate;
2912 int len = 0;
2913
2914 /* detect looping in a NFA_SPLIT */
2915 if (depth > 4)
2916 return -1;
2917
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002918 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002919 {
2920 switch (state->c)
2921 {
2922 case NFA_END_INVISIBLE:
2923 case NFA_END_INVISIBLE_NEG:
2924 /* the end, return what we have */
2925 return len;
2926
2927 case NFA_SPLIT:
2928 /* two alternatives, use the maximum */
2929 l = nfa_max_width(state->out, depth + 1);
2930 r = nfa_max_width(state->out1, depth + 1);
2931 if (l < 0 || r < 0)
2932 return -1;
2933 return len + (l > r ? l : r);
2934
2935 case NFA_ANY:
2936 case NFA_START_COLL:
2937 case NFA_START_NEG_COLL:
2938 /* matches some character, including composing chars */
2939#ifdef FEAT_MBYTE
2940 if (enc_utf8)
2941 len += MB_MAXBYTES;
2942 else if (has_mbyte)
2943 len += 2;
2944 else
2945#endif
2946 ++len;
2947 if (state->c != NFA_ANY)
2948 {
2949 /* skip over the characters */
2950 state = state->out1->out;
2951 continue;
2952 }
2953 break;
2954
2955 case NFA_DIGIT:
2956 case NFA_WHITE:
2957 case NFA_HEX:
2958 case NFA_OCTAL:
2959 /* ascii */
2960 ++len;
2961 break;
2962
2963 case NFA_IDENT:
2964 case NFA_SIDENT:
2965 case NFA_KWORD:
2966 case NFA_SKWORD:
2967 case NFA_FNAME:
2968 case NFA_SFNAME:
2969 case NFA_PRINT:
2970 case NFA_SPRINT:
2971 case NFA_NWHITE:
2972 case NFA_NDIGIT:
2973 case NFA_NHEX:
2974 case NFA_NOCTAL:
2975 case NFA_WORD:
2976 case NFA_NWORD:
2977 case NFA_HEAD:
2978 case NFA_NHEAD:
2979 case NFA_ALPHA:
2980 case NFA_NALPHA:
2981 case NFA_LOWER:
2982 case NFA_NLOWER:
2983 case NFA_UPPER:
2984 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002985 case NFA_LOWER_IC:
2986 case NFA_NLOWER_IC:
2987 case NFA_UPPER_IC:
2988 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002989 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002990 /* possibly non-ascii */
2991#ifdef FEAT_MBYTE
2992 if (has_mbyte)
2993 len += 3;
2994 else
2995#endif
2996 ++len;
2997 break;
2998
2999 case NFA_START_INVISIBLE:
3000 case NFA_START_INVISIBLE_NEG:
3001 case NFA_START_INVISIBLE_BEFORE:
3002 case NFA_START_INVISIBLE_BEFORE_NEG:
3003 /* zero-width, out1 points to the END state */
3004 state = state->out1->out;
3005 continue;
3006
3007 case NFA_BACKREF1:
3008 case NFA_BACKREF2:
3009 case NFA_BACKREF3:
3010 case NFA_BACKREF4:
3011 case NFA_BACKREF5:
3012 case NFA_BACKREF6:
3013 case NFA_BACKREF7:
3014 case NFA_BACKREF8:
3015 case NFA_BACKREF9:
3016#ifdef FEAT_SYN_HL
3017 case NFA_ZREF1:
3018 case NFA_ZREF2:
3019 case NFA_ZREF3:
3020 case NFA_ZREF4:
3021 case NFA_ZREF5:
3022 case NFA_ZREF6:
3023 case NFA_ZREF7:
3024 case NFA_ZREF8:
3025 case NFA_ZREF9:
3026#endif
3027 case NFA_NEWL:
3028 case NFA_SKIP:
3029 /* unknown width */
3030 return -1;
3031
3032 case NFA_BOL:
3033 case NFA_EOL:
3034 case NFA_BOF:
3035 case NFA_EOF:
3036 case NFA_BOW:
3037 case NFA_EOW:
3038 case NFA_MOPEN:
3039 case NFA_MOPEN1:
3040 case NFA_MOPEN2:
3041 case NFA_MOPEN3:
3042 case NFA_MOPEN4:
3043 case NFA_MOPEN5:
3044 case NFA_MOPEN6:
3045 case NFA_MOPEN7:
3046 case NFA_MOPEN8:
3047 case NFA_MOPEN9:
3048#ifdef FEAT_SYN_HL
3049 case NFA_ZOPEN:
3050 case NFA_ZOPEN1:
3051 case NFA_ZOPEN2:
3052 case NFA_ZOPEN3:
3053 case NFA_ZOPEN4:
3054 case NFA_ZOPEN5:
3055 case NFA_ZOPEN6:
3056 case NFA_ZOPEN7:
3057 case NFA_ZOPEN8:
3058 case NFA_ZOPEN9:
3059 case NFA_ZCLOSE:
3060 case NFA_ZCLOSE1:
3061 case NFA_ZCLOSE2:
3062 case NFA_ZCLOSE3:
3063 case NFA_ZCLOSE4:
3064 case NFA_ZCLOSE5:
3065 case NFA_ZCLOSE6:
3066 case NFA_ZCLOSE7:
3067 case NFA_ZCLOSE8:
3068 case NFA_ZCLOSE9:
3069#endif
3070 case NFA_MCLOSE:
3071 case NFA_MCLOSE1:
3072 case NFA_MCLOSE2:
3073 case NFA_MCLOSE3:
3074 case NFA_MCLOSE4:
3075 case NFA_MCLOSE5:
3076 case NFA_MCLOSE6:
3077 case NFA_MCLOSE7:
3078 case NFA_MCLOSE8:
3079 case NFA_MCLOSE9:
3080 case NFA_NOPEN:
3081 case NFA_NCLOSE:
3082
3083 case NFA_LNUM_GT:
3084 case NFA_LNUM_LT:
3085 case NFA_COL_GT:
3086 case NFA_COL_LT:
3087 case NFA_VCOL_GT:
3088 case NFA_VCOL_LT:
3089 case NFA_MARK_GT:
3090 case NFA_MARK_LT:
3091 case NFA_VISUAL:
3092 case NFA_LNUM:
3093 case NFA_CURSOR:
3094 case NFA_COL:
3095 case NFA_VCOL:
3096 case NFA_MARK:
3097
3098 case NFA_ZSTART:
3099 case NFA_ZEND:
3100 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003101 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003102 case NFA_START_PATTERN:
3103 case NFA_END_PATTERN:
3104 case NFA_COMPOSING:
3105 case NFA_END_COMPOSING:
3106 /* zero-width */
3107 break;
3108
3109 default:
3110 if (state->c < 0)
3111 /* don't know what this is */
3112 return -1;
3113 /* normal character */
3114 len += MB_CHAR2LEN(state->c);
3115 break;
3116 }
3117
3118 /* normal way to continue */
3119 state = state->out;
3120 }
3121
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003122 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003123 return -1;
3124}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003125
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003126/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003127 * Convert a postfix form into its equivalent NFA.
3128 * Return the NFA start state on success, NULL otherwise.
3129 */
3130 static nfa_state_T *
3131post2nfa(postfix, end, nfa_calc_size)
3132 int *postfix;
3133 int *end;
3134 int nfa_calc_size;
3135{
3136 int *p;
3137 int mopen;
3138 int mclose;
3139 Frag_T *stack = NULL;
3140 Frag_T *stackp = NULL;
3141 Frag_T *stack_end = NULL;
3142 Frag_T e1;
3143 Frag_T e2;
3144 Frag_T e;
3145 nfa_state_T *s;
3146 nfa_state_T *s1;
3147 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003148 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003149
3150 if (postfix == NULL)
3151 return NULL;
3152
Bram Moolenaar053bb602013-05-20 13:55:21 +02003153#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003154#define POP() st_pop(&stackp, stack); \
3155 if (stackp < stack) \
3156 { \
3157 st_error(postfix, end, p); \
3158 return NULL; \
3159 }
3160
3161 if (nfa_calc_size == FALSE)
3162 {
3163 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003164 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003165 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003166 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003167 }
3168
3169 for (p = postfix; p < end; ++p)
3170 {
3171 switch (*p)
3172 {
3173 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003174 /* Concatenation.
3175 * Pay attention: this operator does not exist in the r.e. itself
3176 * (it is implicit, really). It is added when r.e. is translated
3177 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003178 if (nfa_calc_size == TRUE)
3179 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003180 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003181 break;
3182 }
3183 e2 = POP();
3184 e1 = POP();
3185 patch(e1.out, e2.start);
3186 PUSH(frag(e1.start, e2.out));
3187 break;
3188
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189 case NFA_OR:
3190 /* Alternation */
3191 if (nfa_calc_size == TRUE)
3192 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003193 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003194 break;
3195 }
3196 e2 = POP();
3197 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003198 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003199 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003200 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003201 PUSH(frag(s, append(e1.out, e2.out)));
3202 break;
3203
3204 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003205 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206 if (nfa_calc_size == TRUE)
3207 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003208 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209 break;
3210 }
3211 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003212 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003214 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003215 patch(e.out, s);
3216 PUSH(frag(s, list1(&s->out1)));
3217 break;
3218
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003219 case NFA_STAR_NONGREEDY:
3220 /* Zero or more, prefer zero */
3221 if (nfa_calc_size == TRUE)
3222 {
3223 nstate++;
3224 break;
3225 }
3226 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003227 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003228 if (s == NULL)
3229 goto theend;
3230 patch(e.out, s);
3231 PUSH(frag(s, list1(&s->out)));
3232 break;
3233
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 case NFA_QUEST:
3235 /* one or zero atoms=> greedy match */
3236 if (nfa_calc_size == TRUE)
3237 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003238 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239 break;
3240 }
3241 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003242 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003243 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003244 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245 PUSH(frag(s, append(e.out, list1(&s->out1))));
3246 break;
3247
3248 case NFA_QUEST_NONGREEDY:
3249 /* zero or one atoms => non-greedy match */
3250 if (nfa_calc_size == TRUE)
3251 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003252 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253 break;
3254 }
3255 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003256 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003257 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003258 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 PUSH(frag(s, append(e.out, list1(&s->out))));
3260 break;
3261
Bram Moolenaar417bad22013-06-07 14:08:30 +02003262 case NFA_END_COLL:
3263 case NFA_END_NEG_COLL:
3264 /* On the stack is the sequence starting with NFA_START_COLL or
3265 * NFA_START_NEG_COLL and all possible characters. Patch it to
3266 * add the output to the start. */
3267 if (nfa_calc_size == TRUE)
3268 {
3269 nstate++;
3270 break;
3271 }
3272 e = POP();
3273 s = alloc_state(NFA_END_COLL, NULL, NULL);
3274 if (s == NULL)
3275 goto theend;
3276 patch(e.out, s);
3277 e.start->out1 = s;
3278 PUSH(frag(e.start, list1(&s->out)));
3279 break;
3280
3281 case NFA_RANGE:
3282 /* Before this are two characters, the low and high end of a
3283 * range. Turn them into two states with MIN and MAX. */
3284 if (nfa_calc_size == TRUE)
3285 {
3286 /* nstate += 0; */
3287 break;
3288 }
3289 e2 = POP();
3290 e1 = POP();
3291 e2.start->val = e2.start->c;
3292 e2.start->c = NFA_RANGE_MAX;
3293 e1.start->val = e1.start->c;
3294 e1.start->c = NFA_RANGE_MIN;
3295 patch(e1.out, e2.start);
3296 PUSH(frag(e1.start, e2.out));
3297 break;
3298
Bram Moolenaar699c1202013-09-25 16:41:54 +02003299 case NFA_EMPTY:
3300 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 if (nfa_calc_size == TRUE)
3302 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003303 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304 break;
3305 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003306 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003308 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003309 PUSH(frag(s, list1(&s->out)));
3310 break;
3311
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003312 case NFA_OPT_CHARS:
3313 {
3314 int n;
3315
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003316 /* \%[abc] implemented as:
3317 * NFA_SPLIT
3318 * +-CHAR(a)
3319 * | +-NFA_SPLIT
3320 * | +-CHAR(b)
3321 * | | +-NFA_SPLIT
3322 * | | +-CHAR(c)
3323 * | | | +-next
3324 * | | +- next
3325 * | +- next
3326 * +- next
3327 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003328 n = *++p; /* get number of characters */
3329 if (nfa_calc_size == TRUE)
3330 {
3331 nstate += n;
3332 break;
3333 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003334 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003335 e1.out = NULL; /* stores list with out1's */
3336 s1 = NULL; /* previous NFA_SPLIT to connect to */
3337 while (n-- > 0)
3338 {
3339 e = POP(); /* get character */
3340 s = alloc_state(NFA_SPLIT, e.start, NULL);
3341 if (s == NULL)
3342 goto theend;
3343 if (e1.out == NULL)
3344 e1 = e;
3345 patch(e.out, s1);
3346 append(e1.out, list1(&s->out1));
3347 s1 = s;
3348 }
3349 PUSH(frag(s, e1.out));
3350 break;
3351 }
3352
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003353 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003354 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003355 case NFA_PREV_ATOM_JUST_BEFORE:
3356 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003357 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003358 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003359 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3360 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003361 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003362 int start_state;
3363 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003364 int n = 0;
3365 nfa_state_T *zend;
3366 nfa_state_T *skip;
3367
Bram Moolenaardecd9542013-06-07 16:31:50 +02003368 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003369 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003370 case NFA_PREV_ATOM_NO_WIDTH:
3371 start_state = NFA_START_INVISIBLE;
3372 end_state = NFA_END_INVISIBLE;
3373 break;
3374 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3375 start_state = NFA_START_INVISIBLE_NEG;
3376 end_state = NFA_END_INVISIBLE_NEG;
3377 break;
3378 case NFA_PREV_ATOM_JUST_BEFORE:
3379 start_state = NFA_START_INVISIBLE_BEFORE;
3380 end_state = NFA_END_INVISIBLE;
3381 break;
3382 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3383 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3384 end_state = NFA_END_INVISIBLE_NEG;
3385 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003386 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003387 start_state = NFA_START_PATTERN;
3388 end_state = NFA_END_PATTERN;
3389 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003390 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003391
3392 if (before)
3393 n = *++p; /* get the count */
3394
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003395 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003396 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003397 * The \@<= operator: match for the preceding atom.
3398 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003399 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003400 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003401
3402 if (nfa_calc_size == TRUE)
3403 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003404 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003405 break;
3406 }
3407 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003408 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003409 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003410 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003411
Bram Moolenaar87953742013-06-05 18:52:40 +02003412 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003414 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003415 if (pattern)
3416 {
3417 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3418 skip = alloc_state(NFA_SKIP, NULL, NULL);
3419 zend = alloc_state(NFA_ZEND, s1, NULL);
3420 s1->out= skip;
3421 patch(e.out, zend);
3422 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003423 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003424 else
3425 {
3426 patch(e.out, s1);
3427 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003428 if (before)
3429 {
3430 if (n <= 0)
3431 /* See if we can guess the maximum width, it avoids a
3432 * lot of pointless tries. */
3433 n = nfa_max_width(e.start, 0);
3434 s->val = n; /* store the count */
3435 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003436 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003437 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003440#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003441 case NFA_COMPOSING: /* char with composing char */
3442#if 0
3443 /* TODO */
3444 if (regflags & RF_ICOMBINE)
3445 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003446 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003447 }
3448#endif
3449 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003450#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003451
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003452 case NFA_MOPEN: /* \( \) Submatch */
3453 case NFA_MOPEN1:
3454 case NFA_MOPEN2:
3455 case NFA_MOPEN3:
3456 case NFA_MOPEN4:
3457 case NFA_MOPEN5:
3458 case NFA_MOPEN6:
3459 case NFA_MOPEN7:
3460 case NFA_MOPEN8:
3461 case NFA_MOPEN9:
3462#ifdef FEAT_SYN_HL
3463 case NFA_ZOPEN: /* \z( \) Submatch */
3464 case NFA_ZOPEN1:
3465 case NFA_ZOPEN2:
3466 case NFA_ZOPEN3:
3467 case NFA_ZOPEN4:
3468 case NFA_ZOPEN5:
3469 case NFA_ZOPEN6:
3470 case NFA_ZOPEN7:
3471 case NFA_ZOPEN8:
3472 case NFA_ZOPEN9:
3473#endif
3474 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 if (nfa_calc_size == TRUE)
3476 {
3477 nstate += 2;
3478 break;
3479 }
3480
3481 mopen = *p;
3482 switch (*p)
3483 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003484 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3485#ifdef FEAT_SYN_HL
3486 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3487 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3488 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3489 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3490 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3491 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3492 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3493 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3494 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3495 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3496#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003497#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003498 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003499#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003500 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003501 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502 mclose = *p + NSUBEXP;
3503 break;
3504 }
3505
3506 /* Allow "NFA_MOPEN" as a valid postfix representation for
3507 * the empty regexp "". In this case, the NFA will be
3508 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3509 * empty groups of parenthesis, and empty mbyte chars */
3510 if (stackp == stack)
3511 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003512 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003513 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003514 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003515 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003516 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003517 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 patch(list1(&s->out), s1);
3519 PUSH(frag(s, list1(&s1->out)));
3520 break;
3521 }
3522
3523 /* At least one node was emitted before NFA_MOPEN, so
3524 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3525 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003526 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003527 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003528 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529
Bram Moolenaar525666f2013-06-02 16:40:55 +02003530 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003531 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003532 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003533 patch(e.out, s1);
3534
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003535#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003536 if (mopen == NFA_COMPOSING)
3537 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003538 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003539#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540
3541 PUSH(frag(s, list1(&s1->out)));
3542 break;
3543
Bram Moolenaar5714b802013-05-28 22:03:20 +02003544 case NFA_BACKREF1:
3545 case NFA_BACKREF2:
3546 case NFA_BACKREF3:
3547 case NFA_BACKREF4:
3548 case NFA_BACKREF5:
3549 case NFA_BACKREF6:
3550 case NFA_BACKREF7:
3551 case NFA_BACKREF8:
3552 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003553#ifdef FEAT_SYN_HL
3554 case NFA_ZREF1:
3555 case NFA_ZREF2:
3556 case NFA_ZREF3:
3557 case NFA_ZREF4:
3558 case NFA_ZREF5:
3559 case NFA_ZREF6:
3560 case NFA_ZREF7:
3561 case NFA_ZREF8:
3562 case NFA_ZREF9:
3563#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003564 if (nfa_calc_size == TRUE)
3565 {
3566 nstate += 2;
3567 break;
3568 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003569 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003570 if (s == NULL)
3571 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003572 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003573 if (s1 == NULL)
3574 goto theend;
3575 patch(list1(&s->out), s1);
3576 PUSH(frag(s, list1(&s1->out)));
3577 break;
3578
Bram Moolenaar423532e2013-05-29 21:14:42 +02003579 case NFA_LNUM:
3580 case NFA_LNUM_GT:
3581 case NFA_LNUM_LT:
3582 case NFA_VCOL:
3583 case NFA_VCOL_GT:
3584 case NFA_VCOL_LT:
3585 case NFA_COL:
3586 case NFA_COL_GT:
3587 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003588 case NFA_MARK:
3589 case NFA_MARK_GT:
3590 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003591 {
3592 int n = *++p; /* lnum, col or mark name */
3593
Bram Moolenaar423532e2013-05-29 21:14:42 +02003594 if (nfa_calc_size == TRUE)
3595 {
3596 nstate += 1;
3597 break;
3598 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003599 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003600 if (s == NULL)
3601 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003602 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003603 PUSH(frag(s, list1(&s->out)));
3604 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003605 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003606
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003607 case NFA_ZSTART:
3608 case NFA_ZEND:
3609 default:
3610 /* Operands */
3611 if (nfa_calc_size == TRUE)
3612 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003613 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003614 break;
3615 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003616 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003618 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 PUSH(frag(s, list1(&s->out)));
3620 break;
3621
3622 } /* switch(*p) */
3623
3624 } /* for(p = postfix; *p; ++p) */
3625
3626 if (nfa_calc_size == TRUE)
3627 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003628 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003629 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 }
3631
3632 e = POP();
3633 if (stackp != stack)
3634 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3635
3636 if (istate >= nstate)
3637 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3638
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639 matchstate = &state_ptr[istate++]; /* the match state */
3640 matchstate->c = NFA_MATCH;
3641 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003642 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643
3644 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003645 ret = e.start;
3646
3647theend:
3648 vim_free(stack);
3649 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003650
3651#undef POP1
3652#undef PUSH1
3653#undef POP2
3654#undef PUSH2
3655#undef POP
3656#undef PUSH
3657}
3658
Bram Moolenaara2947e22013-06-11 22:44:09 +02003659/*
3660 * After building the NFA program, inspect it to add optimization hints.
3661 */
3662 static void
3663nfa_postprocess(prog)
3664 nfa_regprog_T *prog;
3665{
3666 int i;
3667 int c;
3668
3669 for (i = 0; i < prog->nstate; ++i)
3670 {
3671 c = prog->state[i].c;
3672 if (c == NFA_START_INVISIBLE
3673 || c == NFA_START_INVISIBLE_NEG
3674 || c == NFA_START_INVISIBLE_BEFORE
3675 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3676 {
3677 int directly;
3678
3679 /* Do it directly when what follows is possibly the end of the
3680 * match. */
3681 if (match_follows(prog->state[i].out1->out, 0))
3682 directly = TRUE;
3683 else
3684 {
3685 int ch_invisible = failure_chance(prog->state[i].out, 0);
3686 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3687
3688 /* Postpone when the invisible match is expensive or has a
3689 * lower chance of failing. */
3690 if (c == NFA_START_INVISIBLE_BEFORE
3691 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3692 {
3693 /* "before" matches are very expensive when
3694 * unbounded, always prefer what follows then,
3695 * unless what follows will always match.
3696 * Otherwise strongly prefer what follows. */
3697 if (prog->state[i].val <= 0 && ch_follows > 0)
3698 directly = FALSE;
3699 else
3700 directly = ch_follows * 10 < ch_invisible;
3701 }
3702 else
3703 {
3704 /* normal invisible, first do the one with the
3705 * highest failure chance */
3706 directly = ch_follows < ch_invisible;
3707 }
3708 }
3709 if (directly)
3710 /* switch to the _FIRST state */
3711 ++prog->state[i].c;
3712 }
3713 }
3714}
3715
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003716/****************************************************************
3717 * NFA execution code.
3718 ****************************************************************/
3719
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003720typedef struct
3721{
3722 int in_use; /* number of subexpr with useful info */
3723
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003724 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003725 union
3726 {
3727 struct multipos
3728 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003729 linenr_T start_lnum;
3730 linenr_T end_lnum;
3731 colnr_T start_col;
3732 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003733 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003734 struct linepos
3735 {
3736 char_u *start;
3737 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003738 } line[NSUBEXP];
3739 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003740} regsub_T;
3741
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003742typedef struct
3743{
3744 regsub_T norm; /* \( .. \) matches */
3745#ifdef FEAT_SYN_HL
3746 regsub_T synt; /* \z( .. \) matches */
3747#endif
3748} regsubs_T;
3749
Bram Moolenaara2d95102013-06-04 14:23:05 +02003750/* nfa_pim_T stores a Postponed Invisible Match. */
3751typedef struct nfa_pim_S nfa_pim_T;
3752struct nfa_pim_S
3753{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003754 int result; /* NFA_PIM_*, see below */
3755 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003756 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003757 union
3758 {
3759 lpos_T pos;
3760 char_u *ptr;
3761 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003762};
3763
3764/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003765#define NFA_PIM_UNUSED 0 /* pim not used */
3766#define NFA_PIM_TODO 1 /* pim not done yet */
3767#define NFA_PIM_MATCH 2 /* pim executed, matches */
3768#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003769
3770
Bram Moolenaar963fee22013-05-26 21:47:28 +02003771/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003772typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003773{
3774 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003775 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003776 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3777 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003778 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003779} nfa_thread_T;
3780
Bram Moolenaar963fee22013-05-26 21:47:28 +02003781/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003782typedef struct
3783{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003784 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003785 int n; /* nr of states currently in "t" */
3786 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003787 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003788 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003789} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003790
Bram Moolenaar5714b802013-05-28 22:03:20 +02003791#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003792static void log_subsexpr __ARGS((regsubs_T *subs));
3793static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003794static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003795
3796 static void
3797log_subsexpr(subs)
3798 regsubs_T *subs;
3799{
3800 log_subexpr(&subs->norm);
3801# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003802 if (nfa_has_zsubexpr)
3803 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003804# endif
3805}
3806
Bram Moolenaar5714b802013-05-28 22:03:20 +02003807 static void
3808log_subexpr(sub)
3809 regsub_T *sub;
3810{
3811 int j;
3812
3813 for (j = 0; j < sub->in_use; j++)
3814 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003815 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003816 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003817 sub->list.multi[j].start_col,
3818 (int)sub->list.multi[j].start_lnum,
3819 sub->list.multi[j].end_col,
3820 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003821 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003822 {
3823 char *s = (char *)sub->list.line[j].start;
3824 char *e = (char *)sub->list.line[j].end;
3825
Bram Moolenaar87953742013-06-05 18:52:40 +02003826 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003827 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003828 s == NULL ? "NULL" : s,
3829 e == NULL ? "NULL" : e);
3830 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003831}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003832
3833 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003834pim_info(pim)
3835 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003836{
3837 static char buf[30];
3838
3839 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3840 buf[0] = NUL;
3841 else
3842 {
3843 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3844 : (int)(pim->end.ptr - reginput));
3845 }
3846 return buf;
3847}
3848
Bram Moolenaar5714b802013-05-28 22:03:20 +02003849#endif
3850
Bram Moolenaar963fee22013-05-26 21:47:28 +02003851/* Used during execution: whether a match has been found. */
3852static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003853
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003854static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003855static void clear_sub __ARGS((regsub_T *sub));
3856static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3857static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaarf2118842013-09-25 18:16:38 +02003858static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003859static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003860static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003861static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3862static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003863static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003864static regsubs_T *addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003865static 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 +02003866
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003867/*
3868 * Copy postponed invisible match info from "from" to "to".
3869 */
3870 static void
3871copy_pim(to, from)
3872 nfa_pim_T *to;
3873 nfa_pim_T *from;
3874{
3875 to->result = from->result;
3876 to->state = from->state;
3877 copy_sub(&to->subs.norm, &from->subs.norm);
3878#ifdef FEAT_SYN_HL
3879 if (nfa_has_zsubexpr)
3880 copy_sub(&to->subs.synt, &from->subs.synt);
3881#endif
3882 to->end = from->end;
3883}
3884
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003885 static void
3886clear_sub(sub)
3887 regsub_T *sub;
3888{
3889 if (REG_MULTI)
3890 /* Use 0xff to set lnum to -1 */
3891 vim_memset(sub->list.multi, 0xff,
3892 sizeof(struct multipos) * nfa_nsubexpr);
3893 else
3894 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3895 sub->in_use = 0;
3896}
3897
3898/*
3899 * Copy the submatches from "from" to "to".
3900 */
3901 static void
3902copy_sub(to, from)
3903 regsub_T *to;
3904 regsub_T *from;
3905{
3906 to->in_use = from->in_use;
3907 if (from->in_use > 0)
3908 {
3909 /* Copy the match start and end positions. */
3910 if (REG_MULTI)
3911 mch_memmove(&to->list.multi[0],
3912 &from->list.multi[0],
3913 sizeof(struct multipos) * from->in_use);
3914 else
3915 mch_memmove(&to->list.line[0],
3916 &from->list.line[0],
3917 sizeof(struct linepos) * from->in_use);
3918 }
3919}
3920
3921/*
3922 * Like copy_sub() but exclude the main match.
3923 */
3924 static void
3925copy_sub_off(to, from)
3926 regsub_T *to;
3927 regsub_T *from;
3928{
3929 if (to->in_use < from->in_use)
3930 to->in_use = from->in_use;
3931 if (from->in_use > 1)
3932 {
3933 /* Copy the match start and end positions. */
3934 if (REG_MULTI)
3935 mch_memmove(&to->list.multi[1],
3936 &from->list.multi[1],
3937 sizeof(struct multipos) * (from->in_use - 1));
3938 else
3939 mch_memmove(&to->list.line[1],
3940 &from->list.line[1],
3941 sizeof(struct linepos) * (from->in_use - 1));
3942 }
3943}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003944
Bram Moolenaar428e9872013-05-30 17:05:39 +02003945/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003946 * Like copy_sub() but only do the end of the main match if \ze is present.
3947 */
3948 static void
3949copy_ze_off(to, from)
3950 regsub_T *to;
3951 regsub_T *from;
3952{
3953 if (nfa_has_zend)
3954 {
3955 if (REG_MULTI)
3956 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003957 if (from->list.multi[0].end_lnum >= 0)
3958 {
3959 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3960 to->list.multi[0].end_col = from->list.multi[0].end_col;
3961 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02003962 }
3963 else
3964 {
3965 if (from->list.line[0].end != NULL)
3966 to->list.line[0].end = from->list.line[0].end;
3967 }
3968 }
3969}
3970
3971/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003972 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003973 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003974 */
3975 static int
3976sub_equal(sub1, sub2)
3977 regsub_T *sub1;
3978 regsub_T *sub2;
3979{
3980 int i;
3981 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003982 linenr_T s1;
3983 linenr_T s2;
3984 char_u *sp1;
3985 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003986
3987 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3988 if (REG_MULTI)
3989 {
3990 for (i = 0; i < todo; ++i)
3991 {
3992 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003993 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003994 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003995 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003996 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003997 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003998 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003999 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004000 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004001 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004002 if (s1 != -1 && sub1->list.multi[i].start_col
4003 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004004 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004005
4006 if (nfa_has_backref)
4007 {
4008 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004009 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004010 else
4011 s1 = -1;
4012 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004013 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004014 else
4015 s2 = -1;
4016 if (s1 != s2)
4017 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004018 if (s1 != -1 && sub1->list.multi[i].end_col
4019 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004020 return FALSE;
4021 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004022 }
4023 }
4024 else
4025 {
4026 for (i = 0; i < todo; ++i)
4027 {
4028 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004029 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004030 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004031 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004032 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004033 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004034 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004035 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004036 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004038 if (nfa_has_backref)
4039 {
4040 if (i < sub1->in_use)
4041 sp1 = sub1->list.line[i].end;
4042 else
4043 sp1 = NULL;
4044 if (i < sub2->in_use)
4045 sp2 = sub2->list.line[i].end;
4046 else
4047 sp2 = NULL;
4048 if (sp1 != sp2)
4049 return FALSE;
4050 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004051 }
4052 }
4053
4054 return TRUE;
4055}
4056
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004057#ifdef ENABLE_LOG
4058 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004059report_state(char *action,
4060 regsub_T *sub,
4061 nfa_state_T *state,
4062 int lid,
4063 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004064{
4065 int col;
4066
4067 if (sub->in_use <= 0)
4068 col = -1;
4069 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004070 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004071 else
4072 col = (int)(sub->list.line[0].start - regline);
4073 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004074 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4075 action, abs(state->id), lid, state->c, code, col,
4076 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004077}
4078#endif
4079
Bram Moolenaar43e02982013-06-07 17:31:29 +02004080/*
4081 * Return TRUE if the same state is already in list "l" with the same
4082 * positions as "subs".
4083 */
4084 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02004085has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02004086 nfa_list_T *l; /* runtime state list */
4087 nfa_state_T *state; /* state to update */
4088 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004089 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004090{
4091 nfa_thread_T *thread;
4092 int i;
4093
4094 for (i = 0; i < l->n; ++i)
4095 {
4096 thread = &l->t[i];
4097 if (thread->state->id == state->id
4098 && sub_equal(&thread->subs.norm, &subs->norm)
4099#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004100 && (!nfa_has_zsubexpr
4101 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004102#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004103 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004104 return TRUE;
4105 }
4106 return FALSE;
4107}
4108
4109/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004110 * Return TRUE if "one" and "two" are equal. That includes when both are not
4111 * set.
4112 */
4113 static int
4114pim_equal(one, two)
4115 nfa_pim_T *one;
4116 nfa_pim_T *two;
4117{
4118 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4119 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4120
4121 if (one_unused)
4122 /* one is unused: equal when two is also unused */
4123 return two_unused;
4124 if (two_unused)
4125 /* one is used and two is not: not equal */
4126 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004127 /* compare the state id */
4128 if (one->state->id != two->state->id)
4129 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004130 /* compare the position */
4131 if (REG_MULTI)
4132 return one->end.pos.lnum == two->end.pos.lnum
4133 && one->end.pos.col == two->end.pos.col;
4134 return one->end.ptr == two->end.ptr;
4135}
4136
4137/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004138 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4139 */
4140 static int
4141match_follows(startstate, depth)
4142 nfa_state_T *startstate;
4143 int depth;
4144{
4145 nfa_state_T *state = startstate;
4146
4147 /* avoid too much recursion */
4148 if (depth > 10)
4149 return FALSE;
4150
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004151 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004152 {
4153 switch (state->c)
4154 {
4155 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004156 case NFA_MCLOSE:
4157 case NFA_END_INVISIBLE:
4158 case NFA_END_INVISIBLE_NEG:
4159 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004160 return TRUE;
4161
4162 case NFA_SPLIT:
4163 return match_follows(state->out, depth + 1)
4164 || match_follows(state->out1, depth + 1);
4165
4166 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004167 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004168 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004169 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004170 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004171 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004172 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004173 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004174 case NFA_COMPOSING:
4175 /* skip ahead to next state */
4176 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004177 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004178
4179 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004180 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004181 case NFA_IDENT:
4182 case NFA_SIDENT:
4183 case NFA_KWORD:
4184 case NFA_SKWORD:
4185 case NFA_FNAME:
4186 case NFA_SFNAME:
4187 case NFA_PRINT:
4188 case NFA_SPRINT:
4189 case NFA_WHITE:
4190 case NFA_NWHITE:
4191 case NFA_DIGIT:
4192 case NFA_NDIGIT:
4193 case NFA_HEX:
4194 case NFA_NHEX:
4195 case NFA_OCTAL:
4196 case NFA_NOCTAL:
4197 case NFA_WORD:
4198 case NFA_NWORD:
4199 case NFA_HEAD:
4200 case NFA_NHEAD:
4201 case NFA_ALPHA:
4202 case NFA_NALPHA:
4203 case NFA_LOWER:
4204 case NFA_NLOWER:
4205 case NFA_UPPER:
4206 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004207 case NFA_LOWER_IC:
4208 case NFA_NLOWER_IC:
4209 case NFA_UPPER_IC:
4210 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004211 case NFA_START_COLL:
4212 case NFA_START_NEG_COLL:
4213 case NFA_NEWL:
4214 /* state will advance input */
4215 return FALSE;
4216
4217 default:
4218 if (state->c > 0)
4219 /* state will advance input */
4220 return FALSE;
4221
4222 /* Others: zero-width or possibly zero-width, might still find
4223 * a match at the same position, keep looking. */
4224 break;
4225 }
4226 state = state->out;
4227 }
4228 return FALSE;
4229}
4230
4231
4232/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004233 * Return TRUE if "state" is already in list "l".
4234 */
4235 static int
4236state_in_list(l, state, subs)
4237 nfa_list_T *l; /* runtime state list */
4238 nfa_state_T *state; /* state to update */
4239 regsubs_T *subs; /* pointers to subexpressions */
4240{
4241 if (state->lastlist[nfa_ll_index] == l->id)
4242 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004243 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004244 return TRUE;
4245 }
4246 return FALSE;
4247}
4248
Bram Moolenaard05bf562013-06-30 23:24:08 +02004249/*
4250 * Add "state" and possibly what follows to state list ".".
4251 * Returns "subs_arg", possibly copied into temp_subs.
4252 */
4253
4254 static regsubs_T *
4255addstate(l, state, subs_arg, pim, off)
4256 nfa_list_T *l; /* runtime state list */
4257 nfa_state_T *state; /* state to update */
4258 regsubs_T *subs_arg; /* pointers to subexpressions */
4259 nfa_pim_T *pim; /* postponed look-behind match */
4260 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004261{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004262 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004263 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004264 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004265 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004266 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004267 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004268 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004269 regsubs_T *subs = subs_arg;
4270 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004271#ifdef ENABLE_LOG
4272 int did_print = FALSE;
4273#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004274
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004275 switch (state->c)
4276 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004277 case NFA_NCLOSE:
4278 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004279 case NFA_MCLOSE1:
4280 case NFA_MCLOSE2:
4281 case NFA_MCLOSE3:
4282 case NFA_MCLOSE4:
4283 case NFA_MCLOSE5:
4284 case NFA_MCLOSE6:
4285 case NFA_MCLOSE7:
4286 case NFA_MCLOSE8:
4287 case NFA_MCLOSE9:
4288#ifdef FEAT_SYN_HL
4289 case NFA_ZCLOSE:
4290 case NFA_ZCLOSE1:
4291 case NFA_ZCLOSE2:
4292 case NFA_ZCLOSE3:
4293 case NFA_ZCLOSE4:
4294 case NFA_ZCLOSE5:
4295 case NFA_ZCLOSE6:
4296 case NFA_ZCLOSE7:
4297 case NFA_ZCLOSE8:
4298 case NFA_ZCLOSE9:
4299#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004300 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004301 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004302 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004303 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004304 /* These nodes are not added themselves but their "out" and/or
4305 * "out1" may be added below. */
4306 break;
4307
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004308 case NFA_BOL:
4309 case NFA_BOF:
4310 /* "^" won't match past end-of-line, don't bother trying.
4311 * Except when at the end of the line, or when we are going to the
4312 * next line for a look-behind match. */
4313 if (reginput > regline
4314 && *reginput != NUL
4315 && (nfa_endp == NULL
4316 || !REG_MULTI
4317 || reglnum == nfa_endp->se_u.pos.lnum))
4318 goto skip_add;
4319 /* FALLTHROUGH */
4320
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004321 case NFA_MOPEN1:
4322 case NFA_MOPEN2:
4323 case NFA_MOPEN3:
4324 case NFA_MOPEN4:
4325 case NFA_MOPEN5:
4326 case NFA_MOPEN6:
4327 case NFA_MOPEN7:
4328 case NFA_MOPEN8:
4329 case NFA_MOPEN9:
4330#ifdef FEAT_SYN_HL
4331 case NFA_ZOPEN:
4332 case NFA_ZOPEN1:
4333 case NFA_ZOPEN2:
4334 case NFA_ZOPEN3:
4335 case NFA_ZOPEN4:
4336 case NFA_ZOPEN5:
4337 case NFA_ZOPEN6:
4338 case NFA_ZOPEN7:
4339 case NFA_ZOPEN8:
4340 case NFA_ZOPEN9:
4341#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004342 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004343 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004344 /* These nodes need to be added so that we can bail out when it
4345 * was added to this list before at the same position to avoid an
4346 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004347
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004348 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004349 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004351 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004352 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004353 * when there is a PIM. For NFA_MATCH check the position,
4354 * lower position is preferred. */
4355 if (!nfa_has_backref && pim == NULL && !l->has_pim
4356 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004357 {
4358skip_add:
4359#ifdef ENABLE_LOG
4360 nfa_set_code(state->c);
4361 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4362 abs(state->id), l->id, state->c, code);
4363#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004364 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004365 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004366
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004367 /* Do not add the state again when it exists with the same
4368 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004369 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004370 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004371 }
4372
Bram Moolenaara0169122013-06-26 18:16:58 +02004373 /* When there are backreferences or PIMs the number of states may
4374 * be (a lot) bigger than anticipated. */
4375 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004376 {
4377 int newlen = l->len * 3 / 2 + 50;
4378
Bram Moolenaard05bf562013-06-30 23:24:08 +02004379 if (subs != &temp_subs)
4380 {
4381 /* "subs" may point into the current array, need to make a
4382 * copy before it becomes invalid. */
4383 copy_sub(&temp_subs.norm, &subs->norm);
4384#ifdef FEAT_SYN_HL
4385 if (nfa_has_zsubexpr)
4386 copy_sub(&temp_subs.synt, &subs->synt);
4387#endif
4388 subs = &temp_subs;
4389 }
4390
Bram Moolenaar428e9872013-05-30 17:05:39 +02004391 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4392 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004394
4395 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004396 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004397 thread = &l->t[l->n++];
4398 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004399 if (pim == NULL)
4400 thread->pim.result = NFA_PIM_UNUSED;
4401 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004402 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004403 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004404 l->has_pim = TRUE;
4405 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004406 copy_sub(&thread->subs.norm, &subs->norm);
4407#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004408 if (nfa_has_zsubexpr)
4409 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004410#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004411#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004412 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004413 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004414#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004415 }
4416
4417#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004418 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004419 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004420#endif
4421 switch (state->c)
4422 {
4423 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004424 break;
4425
4426 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004427 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004428 subs = addstate(l, state->out, subs, pim, off);
4429 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004430 break;
4431
Bram Moolenaar699c1202013-09-25 16:41:54 +02004432 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004433 case NFA_NOPEN:
4434 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004435 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004436 break;
4437
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004438 case NFA_MOPEN:
4439 case NFA_MOPEN1:
4440 case NFA_MOPEN2:
4441 case NFA_MOPEN3:
4442 case NFA_MOPEN4:
4443 case NFA_MOPEN5:
4444 case NFA_MOPEN6:
4445 case NFA_MOPEN7:
4446 case NFA_MOPEN8:
4447 case NFA_MOPEN9:
4448#ifdef FEAT_SYN_HL
4449 case NFA_ZOPEN:
4450 case NFA_ZOPEN1:
4451 case NFA_ZOPEN2:
4452 case NFA_ZOPEN3:
4453 case NFA_ZOPEN4:
4454 case NFA_ZOPEN5:
4455 case NFA_ZOPEN6:
4456 case NFA_ZOPEN7:
4457 case NFA_ZOPEN8:
4458 case NFA_ZOPEN9:
4459#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004460 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004461 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004462 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004463 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004464 sub = &subs->norm;
4465 }
4466#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004467 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004468 {
4469 subidx = state->c - NFA_ZOPEN;
4470 sub = &subs->synt;
4471 }
4472#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004473 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004474 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004475 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004476 sub = &subs->norm;
4477 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004478
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004479 /* avoid compiler warnings */
4480 save_ptr = NULL;
4481 save_lpos.lnum = 0;
4482 save_lpos.col = 0;
4483
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004484 /* Set the position (with "off" added) in the subexpression. Save
4485 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004486 if (REG_MULTI)
4487 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004488 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004489 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004490 save_lpos.lnum = sub->list.multi[subidx].start_lnum;
4491 save_lpos.col = sub->list.multi[subidx].start_col;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004492 save_in_use = -1;
4493 }
4494 else
4495 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004496 save_in_use = sub->in_use;
4497 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004498 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004499 sub->list.multi[i].start_lnum = -1;
4500 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004501 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004502 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004503 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004504 if (off == -1)
4505 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004506 sub->list.multi[subidx].start_lnum = reglnum + 1;
4507 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004508 }
4509 else
4510 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004511 sub->list.multi[subidx].start_lnum = reglnum;
4512 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004513 (colnr_T)(reginput - regline + off);
4514 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004515 }
4516 else
4517 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004518 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004519 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004520 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004521 save_in_use = -1;
4522 }
4523 else
4524 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004525 save_in_use = sub->in_use;
4526 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004527 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004528 sub->list.line[i].start = NULL;
4529 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004530 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004531 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004532 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004533 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004534 }
4535
Bram Moolenaard05bf562013-06-30 23:24:08 +02004536 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004537 /* "subs" may have changed, need to set "sub" again */
4538#ifdef FEAT_SYN_HL
4539 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4540 sub = &subs->synt;
4541 else
4542#endif
4543 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004544
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004545 if (save_in_use == -1)
4546 {
4547 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004548 {
4549 sub->list.multi[subidx].start_lnum = save_lpos.lnum;
4550 sub->list.multi[subidx].start_col = save_lpos.col;
4551 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004552 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004553 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004554 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004555 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004556 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557 break;
4558
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004559 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004560 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004561 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004562 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004564 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004565 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004566 break;
4567 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004568 case NFA_MCLOSE1:
4569 case NFA_MCLOSE2:
4570 case NFA_MCLOSE3:
4571 case NFA_MCLOSE4:
4572 case NFA_MCLOSE5:
4573 case NFA_MCLOSE6:
4574 case NFA_MCLOSE7:
4575 case NFA_MCLOSE8:
4576 case NFA_MCLOSE9:
4577#ifdef FEAT_SYN_HL
4578 case NFA_ZCLOSE:
4579 case NFA_ZCLOSE1:
4580 case NFA_ZCLOSE2:
4581 case NFA_ZCLOSE3:
4582 case NFA_ZCLOSE4:
4583 case NFA_ZCLOSE5:
4584 case NFA_ZCLOSE6:
4585 case NFA_ZCLOSE7:
4586 case NFA_ZCLOSE8:
4587 case NFA_ZCLOSE9:
4588#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004589 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004590 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004591 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004592 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004593 sub = &subs->norm;
4594 }
4595#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004596 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004597 {
4598 subidx = state->c - NFA_ZCLOSE;
4599 sub = &subs->synt;
4600 }
4601#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004602 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004603 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004604 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004605 sub = &subs->norm;
4606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004607
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004608 /* We don't fill in gaps here, there must have been an MOPEN that
4609 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004610 save_in_use = sub->in_use;
4611 if (sub->in_use <= subidx)
4612 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004613 if (REG_MULTI)
4614 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004615 save_lpos.lnum = sub->list.multi[subidx].end_lnum;
4616 save_lpos.col = sub->list.multi[subidx].end_col;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004617 if (off == -1)
4618 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004619 sub->list.multi[subidx].end_lnum = reglnum + 1;
4620 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004621 }
4622 else
4623 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004624 sub->list.multi[subidx].end_lnum = reglnum;
4625 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004626 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004627 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004628 /* avoid compiler warnings */
4629 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004630 }
4631 else
4632 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004633 save_ptr = sub->list.line[subidx].end;
4634 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004635 /* avoid compiler warnings */
4636 save_lpos.lnum = 0;
4637 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638 }
4639
Bram Moolenaard05bf562013-06-30 23:24:08 +02004640 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004641 /* "subs" may have changed, need to set "sub" again */
4642#ifdef FEAT_SYN_HL
4643 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4644 sub = &subs->synt;
4645 else
4646#endif
4647 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004648
4649 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004650 {
4651 sub->list.multi[subidx].end_lnum = save_lpos.lnum;
4652 sub->list.multi[subidx].end_col = save_lpos.col;
4653 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004654 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004655 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004656 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004657 break;
4658 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004659 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004660}
4661
4662/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004663 * Like addstate(), but the new state(s) are put at position "*ip".
4664 * Used for zero-width matches, next state to use is the added one.
4665 * This makes sure the order of states to be tried does not change, which
4666 * matters for alternatives.
4667 */
4668 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004669addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004670 nfa_list_T *l; /* runtime state list */
4671 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004672 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004673 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004674 int *ip;
4675{
4676 int tlen = l->n;
4677 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004678 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004679
4680 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004681 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004682
Bram Moolenaar4b417062013-05-25 20:19:50 +02004683 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004684 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004685 return;
4686
4687 /* re-order to put the new state at the current position */
4688 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004689 if (count == 0)
4690 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004691 if (count == 1)
4692 {
4693 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004694 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004695 }
4696 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004697 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004698 if (l->n + count - 1 >= l->len)
4699 {
4700 /* not enough space to move the new states, reallocate the list
4701 * and move the states to the right position */
4702 nfa_thread_T *newl;
4703
4704 l->len = l->len * 3 / 2 + 50;
4705 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4706 if (newl == NULL)
4707 return;
4708 mch_memmove(&(newl[0]),
4709 &(l->t[0]),
4710 sizeof(nfa_thread_T) * listidx);
4711 mch_memmove(&(newl[listidx]),
4712 &(l->t[l->n - count]),
4713 sizeof(nfa_thread_T) * count);
4714 mch_memmove(&(newl[listidx + count]),
4715 &(l->t[listidx + 1]),
4716 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4717 vim_free(l->t);
4718 l->t = newl;
4719 }
4720 else
4721 {
4722 /* make space for new states, then move them from the
4723 * end to the current position */
4724 mch_memmove(&(l->t[listidx + count]),
4725 &(l->t[listidx + 1]),
4726 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4727 mch_memmove(&(l->t[listidx]),
4728 &(l->t[l->n - 1]),
4729 sizeof(nfa_thread_T) * count);
4730 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004731 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004732 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004733 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004734}
4735
4736/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004737 * Check character class "class" against current character c.
4738 */
4739 static int
4740check_char_class(class, c)
4741 int class;
4742 int c;
4743{
4744 switch (class)
4745 {
4746 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004747 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004748 return OK;
4749 break;
4750 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004751 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752 return OK;
4753 break;
4754 case NFA_CLASS_BLANK:
4755 if (c == ' ' || c == '\t')
4756 return OK;
4757 break;
4758 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004759 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004760 return OK;
4761 break;
4762 case NFA_CLASS_DIGIT:
4763 if (VIM_ISDIGIT(c))
4764 return OK;
4765 break;
4766 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004767 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004768 return OK;
4769 break;
4770 case NFA_CLASS_LOWER:
4771 if (MB_ISLOWER(c))
4772 return OK;
4773 break;
4774 case NFA_CLASS_PRINT:
4775 if (vim_isprintc(c))
4776 return OK;
4777 break;
4778 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004779 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004780 return OK;
4781 break;
4782 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004783 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004784 return OK;
4785 break;
4786 case NFA_CLASS_UPPER:
4787 if (MB_ISUPPER(c))
4788 return OK;
4789 break;
4790 case NFA_CLASS_XDIGIT:
4791 if (vim_isxdigit(c))
4792 return OK;
4793 break;
4794 case NFA_CLASS_TAB:
4795 if (c == '\t')
4796 return OK;
4797 break;
4798 case NFA_CLASS_RETURN:
4799 if (c == '\r')
4800 return OK;
4801 break;
4802 case NFA_CLASS_BACKSPACE:
4803 if (c == '\b')
4804 return OK;
4805 break;
4806 case NFA_CLASS_ESCAPE:
4807 if (c == '\033')
4808 return OK;
4809 break;
4810
4811 default:
4812 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004813 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004814 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004815 }
4816 return FAIL;
4817}
4818
Bram Moolenaar5714b802013-05-28 22:03:20 +02004819/*
4820 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004821 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004822 */
4823 static int
4824match_backref(sub, subidx, bytelen)
4825 regsub_T *sub; /* pointers to subexpressions */
4826 int subidx;
4827 int *bytelen; /* out: length of match in bytes */
4828{
4829 int len;
4830
4831 if (sub->in_use <= subidx)
4832 {
4833retempty:
4834 /* backref was not set, match an empty string */
4835 *bytelen = 0;
4836 return TRUE;
4837 }
4838
4839 if (REG_MULTI)
4840 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004841 if (sub->list.multi[subidx].start_lnum < 0
4842 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004843 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004844 if (sub->list.multi[subidx].start_lnum == reglnum
4845 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004846 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004847 len = sub->list.multi[subidx].end_col
4848 - sub->list.multi[subidx].start_col;
4849 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004850 reginput, &len) == 0)
4851 {
4852 *bytelen = len;
4853 return TRUE;
4854 }
4855 }
4856 else
4857 {
4858 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004859 sub->list.multi[subidx].start_lnum,
4860 sub->list.multi[subidx].start_col,
4861 sub->list.multi[subidx].end_lnum,
4862 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004863 bytelen) == RA_MATCH)
4864 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004865 }
4866 }
4867 else
4868 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004869 if (sub->list.line[subidx].start == NULL
4870 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004871 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004872 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4873 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004874 {
4875 *bytelen = len;
4876 return TRUE;
4877 }
4878 }
4879 return FALSE;
4880}
4881
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004882#ifdef FEAT_SYN_HL
4883
4884static int match_zref __ARGS((int subidx, int *bytelen));
4885
4886/*
4887 * Check for a match with \z subexpression "subidx".
4888 * Return TRUE if it matches.
4889 */
4890 static int
4891match_zref(subidx, bytelen)
4892 int subidx;
4893 int *bytelen; /* out: length of match in bytes */
4894{
4895 int len;
4896
4897 cleanup_zsubexpr();
4898 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4899 {
4900 /* backref was not set, match an empty string */
4901 *bytelen = 0;
4902 return TRUE;
4903 }
4904
4905 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4906 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4907 {
4908 *bytelen = len;
4909 return TRUE;
4910 }
4911 return FALSE;
4912}
4913#endif
4914
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004915/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004916 * Save list IDs for all NFA states of "prog" into "list".
4917 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004918 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004919 */
4920 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004921nfa_save_listids(prog, list)
4922 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004923 int *list;
4924{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004925 int i;
4926 nfa_state_T *p;
4927
4928 /* Order in the list is reverse, it's a bit faster that way. */
4929 p = &prog->state[0];
4930 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004931 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004932 list[i] = p->lastlist[1];
4933 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004934 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004935 }
4936}
4937
4938/*
4939 * Restore list IDs from "list" to all NFA states.
4940 */
4941 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004942nfa_restore_listids(prog, list)
4943 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004944 int *list;
4945{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004946 int i;
4947 nfa_state_T *p;
4948
4949 p = &prog->state[0];
4950 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004951 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004952 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004953 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004954 }
4955}
4956
Bram Moolenaar423532e2013-05-29 21:14:42 +02004957 static int
4958nfa_re_num_cmp(val, op, pos)
4959 long_u val;
4960 int op;
4961 long_u pos;
4962{
4963 if (op == 1) return pos > val;
4964 if (op == 2) return pos < val;
4965 return val == pos;
4966}
4967
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004968static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004969static 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 +02004970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004971/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004972 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004973 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4974 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004975 */
4976 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004977recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004978 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004979 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004980 nfa_regprog_T *prog;
4981 regsubs_T *submatch;
4982 regsubs_T *m;
4983 int **listids;
4984{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004985 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004986 int save_reglnum = reglnum;
4987 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004988 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004989 save_se_T *save_nfa_endp = nfa_endp;
4990 save_se_T endpos;
4991 save_se_T *endposp = NULL;
4992 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004993 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004994
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004995 if (pim != NULL)
4996 {
4997 /* start at the position where the postponed match was */
4998 if (REG_MULTI)
4999 reginput = regline + pim->end.pos.col;
5000 else
5001 reginput = pim->end.ptr;
5002 }
5003
Bram Moolenaardecd9542013-06-07 16:31:50 +02005004 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005005 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5006 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5007 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005008 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005009 /* The recursive match must end at the current position. When "pim" is
5010 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005011 endposp = &endpos;
5012 if (REG_MULTI)
5013 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005014 if (pim == NULL)
5015 {
5016 endpos.se_u.pos.col = (int)(reginput - regline);
5017 endpos.se_u.pos.lnum = reglnum;
5018 }
5019 else
5020 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005021 }
5022 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005023 {
5024 if (pim == NULL)
5025 endpos.se_u.ptr = reginput;
5026 else
5027 endpos.se_u.ptr = pim->end.ptr;
5028 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005029
5030 /* Go back the specified number of bytes, or as far as the
5031 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005032 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005033 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005034 if (state->val <= 0)
5035 {
5036 if (REG_MULTI)
5037 {
5038 regline = reg_getline(--reglnum);
5039 if (regline == NULL)
5040 /* can't go before the first line */
5041 regline = reg_getline(++reglnum);
5042 }
5043 reginput = regline;
5044 }
5045 else
5046 {
5047 if (REG_MULTI && (int)(reginput - regline) < state->val)
5048 {
5049 /* Not enough bytes in this line, go to end of
5050 * previous line. */
5051 regline = reg_getline(--reglnum);
5052 if (regline == NULL)
5053 {
5054 /* can't go before the first line */
5055 regline = reg_getline(++reglnum);
5056 reginput = regline;
5057 }
5058 else
5059 reginput = regline + STRLEN(regline);
5060 }
5061 if ((int)(reginput - regline) >= state->val)
5062 {
5063 reginput -= state->val;
5064#ifdef FEAT_MBYTE
5065 if (has_mbyte)
5066 reginput -= mb_head_off(regline, reginput);
5067#endif
5068 }
5069 else
5070 reginput = regline;
5071 }
5072 }
5073
Bram Moolenaarf46da702013-06-02 22:37:42 +02005074#ifdef ENABLE_LOG
5075 if (log_fd != stderr)
5076 fclose(log_fd);
5077 log_fd = NULL;
5078#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005079 /* Have to clear the lastlist field of the NFA nodes, so that
5080 * nfa_regmatch() and addstate() can run properly after recursion. */
5081 if (nfa_ll_index == 1)
5082 {
5083 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5084 * values and clear them. */
5085 if (*listids == NULL)
5086 {
5087 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5088 if (*listids == NULL)
5089 {
5090 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5091 return 0;
5092 }
5093 }
5094 nfa_save_listids(prog, *listids);
5095 need_restore = TRUE;
5096 /* any value of nfa_listid will do */
5097 }
5098 else
5099 {
5100 /* First recursive nfa_regmatch() call, switch to the second lastlist
5101 * entry. Make sure nfa_listid is different from a previous recursive
5102 * call, because some states may still have this ID. */
5103 ++nfa_ll_index;
5104 if (nfa_listid <= nfa_alt_listid)
5105 nfa_listid = nfa_alt_listid;
5106 }
5107
5108 /* Call nfa_regmatch() to check if the current concat matches at this
5109 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005110 nfa_endp = endposp;
5111 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005112
5113 if (need_restore)
5114 nfa_restore_listids(prog, *listids);
5115 else
5116 {
5117 --nfa_ll_index;
5118 nfa_alt_listid = nfa_listid;
5119 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005120
5121 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005122 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005123 if (REG_MULTI)
5124 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005125 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005126 nfa_match = save_nfa_match;
5127 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005128 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005129
5130#ifdef ENABLE_LOG
5131 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5132 if (log_fd != NULL)
5133 {
5134 fprintf(log_fd, "****************************\n");
5135 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5136 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5137 fprintf(log_fd, "****************************\n");
5138 }
5139 else
5140 {
5141 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5142 log_fd = stderr;
5143 }
5144#endif
5145
5146 return result;
5147}
5148
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005149static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005150static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005151
5152/*
5153 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005154 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005155 * NFA_ANY: 1
5156 * specific character: 99
5157 */
5158 static int
5159failure_chance(state, depth)
5160 nfa_state_T *state;
5161 int depth;
5162{
5163 int c = state->c;
5164 int l, r;
5165
5166 /* detect looping */
5167 if (depth > 4)
5168 return 1;
5169
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005170 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005171 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005172 case NFA_SPLIT:
5173 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5174 /* avoid recursive stuff */
5175 return 1;
5176 /* two alternatives, use the lowest failure chance */
5177 l = failure_chance(state->out, depth + 1);
5178 r = failure_chance(state->out1, depth + 1);
5179 return l < r ? l : r;
5180
5181 case NFA_ANY:
5182 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005183 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005184
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005185 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005186 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005187 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005188 /* empty match works always */
5189 return 0;
5190
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005191 case NFA_START_INVISIBLE:
5192 case NFA_START_INVISIBLE_FIRST:
5193 case NFA_START_INVISIBLE_NEG:
5194 case NFA_START_INVISIBLE_NEG_FIRST:
5195 case NFA_START_INVISIBLE_BEFORE:
5196 case NFA_START_INVISIBLE_BEFORE_FIRST:
5197 case NFA_START_INVISIBLE_BEFORE_NEG:
5198 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5199 case NFA_START_PATTERN:
5200 /* recursive regmatch is expensive, use low failure chance */
5201 return 5;
5202
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005203 case NFA_BOL:
5204 case NFA_EOL:
5205 case NFA_BOF:
5206 case NFA_EOF:
5207 case NFA_NEWL:
5208 return 99;
5209
5210 case NFA_BOW:
5211 case NFA_EOW:
5212 return 90;
5213
5214 case NFA_MOPEN:
5215 case NFA_MOPEN1:
5216 case NFA_MOPEN2:
5217 case NFA_MOPEN3:
5218 case NFA_MOPEN4:
5219 case NFA_MOPEN5:
5220 case NFA_MOPEN6:
5221 case NFA_MOPEN7:
5222 case NFA_MOPEN8:
5223 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005224#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005225 case NFA_ZOPEN:
5226 case NFA_ZOPEN1:
5227 case NFA_ZOPEN2:
5228 case NFA_ZOPEN3:
5229 case NFA_ZOPEN4:
5230 case NFA_ZOPEN5:
5231 case NFA_ZOPEN6:
5232 case NFA_ZOPEN7:
5233 case NFA_ZOPEN8:
5234 case NFA_ZOPEN9:
5235 case NFA_ZCLOSE:
5236 case NFA_ZCLOSE1:
5237 case NFA_ZCLOSE2:
5238 case NFA_ZCLOSE3:
5239 case NFA_ZCLOSE4:
5240 case NFA_ZCLOSE5:
5241 case NFA_ZCLOSE6:
5242 case NFA_ZCLOSE7:
5243 case NFA_ZCLOSE8:
5244 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005245#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005246 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005247 case NFA_MCLOSE1:
5248 case NFA_MCLOSE2:
5249 case NFA_MCLOSE3:
5250 case NFA_MCLOSE4:
5251 case NFA_MCLOSE5:
5252 case NFA_MCLOSE6:
5253 case NFA_MCLOSE7:
5254 case NFA_MCLOSE8:
5255 case NFA_MCLOSE9:
5256 case NFA_NCLOSE:
5257 return failure_chance(state->out, depth + 1);
5258
5259 case NFA_BACKREF1:
5260 case NFA_BACKREF2:
5261 case NFA_BACKREF3:
5262 case NFA_BACKREF4:
5263 case NFA_BACKREF5:
5264 case NFA_BACKREF6:
5265 case NFA_BACKREF7:
5266 case NFA_BACKREF8:
5267 case NFA_BACKREF9:
5268#ifdef FEAT_SYN_HL
5269 case NFA_ZREF1:
5270 case NFA_ZREF2:
5271 case NFA_ZREF3:
5272 case NFA_ZREF4:
5273 case NFA_ZREF5:
5274 case NFA_ZREF6:
5275 case NFA_ZREF7:
5276 case NFA_ZREF8:
5277 case NFA_ZREF9:
5278#endif
5279 /* backreferences don't match in many places */
5280 return 94;
5281
5282 case NFA_LNUM_GT:
5283 case NFA_LNUM_LT:
5284 case NFA_COL_GT:
5285 case NFA_COL_LT:
5286 case NFA_VCOL_GT:
5287 case NFA_VCOL_LT:
5288 case NFA_MARK_GT:
5289 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005290 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005291 /* before/after positions don't match very often */
5292 return 85;
5293
5294 case NFA_LNUM:
5295 return 90;
5296
5297 case NFA_CURSOR:
5298 case NFA_COL:
5299 case NFA_VCOL:
5300 case NFA_MARK:
5301 /* specific positions rarely match */
5302 return 98;
5303
5304 case NFA_COMPOSING:
5305 return 95;
5306
5307 default:
5308 if (c > 0)
5309 /* character match fails often */
5310 return 95;
5311 }
5312
5313 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005314 return 50;
5315}
5316
Bram Moolenaarf46da702013-06-02 22:37:42 +02005317/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005318 * Skip until the char "c" we know a match must start with.
5319 */
5320 static int
5321skip_to_start(c, colp)
5322 int c;
5323 colnr_T *colp;
5324{
5325 char_u *s;
5326
5327 /* Used often, do some work to avoid call overhead. */
5328 if (!ireg_ic
5329#ifdef FEAT_MBYTE
5330 && !has_mbyte
5331#endif
5332 )
5333 s = vim_strbyte(regline + *colp, c);
5334 else
5335 s = cstrchr(regline + *colp, c);
5336 if (s == NULL)
5337 return FAIL;
5338 *colp = (int)(s - regline);
5339 return OK;
5340}
5341
5342/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005343 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005344 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005345 * Returns zero for no match, 1 for a match.
5346 */
5347 static long
5348find_match_text(startcol, regstart, match_text)
5349 colnr_T startcol;
5350 int regstart;
5351 char_u *match_text;
5352{
5353 colnr_T col = startcol;
5354 int c1, c2;
5355 int len1, len2;
5356 int match;
5357
5358 for (;;)
5359 {
5360 match = TRUE;
5361 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5362 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5363 {
5364 c1 = PTR2CHAR(match_text + len1);
5365 c2 = PTR2CHAR(regline + col + len2);
5366 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5367 {
5368 match = FALSE;
5369 break;
5370 }
5371 len2 += MB_CHAR2LEN(c2);
5372 }
5373 if (match
5374#ifdef FEAT_MBYTE
5375 /* check that no composing char follows */
5376 && !(enc_utf8
5377 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5378#endif
5379 )
5380 {
5381 cleanup_subexpr();
5382 if (REG_MULTI)
5383 {
5384 reg_startpos[0].lnum = reglnum;
5385 reg_startpos[0].col = col;
5386 reg_endpos[0].lnum = reglnum;
5387 reg_endpos[0].col = col + len2;
5388 }
5389 else
5390 {
5391 reg_startp[0] = regline + col;
5392 reg_endp[0] = regline + col + len2;
5393 }
5394 return 1L;
5395 }
5396
5397 /* Try finding regstart after the current match. */
5398 col += MB_CHAR2LEN(regstart); /* skip regstart */
5399 if (skip_to_start(regstart, &col) == FAIL)
5400 break;
5401 }
5402 return 0L;
5403}
5404
5405/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005406 * Main matching routine.
5407 *
5408 * Run NFA to determine whether it matches reginput.
5409 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005410 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005411 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005412 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005413 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005414 * Note: Caller must ensure that: start != NULL.
5415 */
5416 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005417nfa_regmatch(prog, start, submatch, m)
5418 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005419 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005420 regsubs_T *submatch;
5421 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005422{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005423 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005424 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005425 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005426 int go_to_nextline = FALSE;
5427 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005428 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005429 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005430 nfa_list_T *thislist;
5431 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005432 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005433 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005434 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005435 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005436 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005437 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005438#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005439 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005440
5441 if (debug == NULL)
5442 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005443 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444 return FALSE;
5445 }
5446#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005447 /* Some patterns may take a long time to match, especially when using
5448 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5449 fast_breakcheck();
5450 if (got_int)
5451 return FALSE;
5452
Bram Moolenaar963fee22013-05-26 21:47:28 +02005453 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005454
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005455 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005456 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005457
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005458 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005459 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005460 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005461 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005462 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005463 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005464
5465#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005466 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005467 if (log_fd != NULL)
5468 {
5469 fprintf(log_fd, "**********************************\n");
5470 nfa_set_code(start->c);
5471 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5472 abs(start->id), code);
5473 fprintf(log_fd, "**********************************\n");
5474 }
5475 else
5476 {
5477 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5478 log_fd = stderr;
5479 }
5480#endif
5481
5482 thislist = &list[0];
5483 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005484 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005485 nextlist = &list[1];
5486 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005487 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005488#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005489 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005490#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005491 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005492
5493 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5494 * it's the first MOPEN. */
5495 if (toplevel)
5496 {
5497 if (REG_MULTI)
5498 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005499 m->norm.list.multi[0].start_lnum = reglnum;
5500 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005501 }
5502 else
5503 m->norm.list.line[0].start = reginput;
5504 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005505 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005506 }
5507 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005508 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005509
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005510#define ADD_STATE_IF_MATCH(state) \
5511 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005512 add_state = state->out; \
5513 add_off = clen; \
5514 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515
5516 /*
5517 * Run for each character.
5518 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005519 for (;;)
5520 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005521 int curc;
5522 int clen;
5523
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005524#ifdef FEAT_MBYTE
5525 if (has_mbyte)
5526 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005527 curc = (*mb_ptr2char)(reginput);
5528 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529 }
5530 else
5531#endif
5532 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005533 curc = *reginput;
5534 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005535 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005536 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005537 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005538 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005539 go_to_nextline = FALSE;
5540 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005541
5542 /* swap lists */
5543 thislist = &list[flag];
5544 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005545 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005546 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005547 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005548 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5549 {
5550 /* too many states, retry with old engine */
5551 nfa_match = NFA_TOO_EXPENSIVE;
5552 goto theend;
5553 }
5554
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005555 thislist->id = nfa_listid;
5556 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005557
5558#ifdef ENABLE_LOG
5559 fprintf(log_fd, "------------------------------------------\n");
5560 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005561 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005563 {
5564 int i;
5565
5566 for (i = 0; i < thislist->n; i++)
5567 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5568 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005569 fprintf(log_fd, "\n");
5570#endif
5571
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005572#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005573 fprintf(debug, "\n-------------------\n");
5574#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005575 /*
5576 * If the state lists are empty we can stop.
5577 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005578 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005579 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005580
5581 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005582 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005583 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005584 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005585
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005586#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005587 nfa_set_code(t->state->c);
5588 fprintf(debug, "%s, ", code);
5589#endif
5590#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005591 {
5592 int col;
5593
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005594 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005595 col = -1;
5596 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005597 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005598 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005599 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005600 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005601 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5602 abs(t->state->id), (int)t->state->c, code, col,
5603 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005604 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005605#endif
5606
5607 /*
5608 * Handle the possible codes of the current state.
5609 * The most important is NFA_MATCH.
5610 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005611 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005612 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005613 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614 switch (t->state->c)
5615 {
5616 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005617 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005618#ifdef FEAT_MBYTE
5619 /* If the match ends before a composing characters and
5620 * ireg_icombine is not set, that is not really a match. */
5621 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5622 break;
5623#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005624 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005625 copy_sub(&submatch->norm, &t->subs.norm);
5626#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005627 if (nfa_has_zsubexpr)
5628 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005629#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005630#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005631 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005632#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005633 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005634 * states at this position. When the list of states is going
5635 * to be empty quit without advancing, so that "reginput" is
5636 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005637 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005638 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005639 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005640 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641
5642 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005643 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005644 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005645 /*
5646 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005647 * NFA_START_INVISIBLE_BEFORE node.
5648 * They surround a zero-width group, used with "\@=", "\&",
5649 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005650 * If we got here, it means that the current "invisible" group
5651 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005652 * nfa_regmatch(). For a look-behind match only when it ends
5653 * in the position in "nfa_endp".
5654 * Submatches are stored in *m, and used in the parent call.
5655 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005656#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005657 if (nfa_endp != NULL)
5658 {
5659 if (REG_MULTI)
5660 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5661 (int)reglnum,
5662 (int)nfa_endp->se_u.pos.lnum,
5663 (int)(reginput - regline),
5664 nfa_endp->se_u.pos.col);
5665 else
5666 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5667 (int)(reginput - regline),
5668 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005669 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005670#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005671 /* If "nfa_endp" is set it's only a match if it ends at
5672 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005673 if (nfa_endp != NULL && (REG_MULTI
5674 ? (reglnum != nfa_endp->se_u.pos.lnum
5675 || (int)(reginput - regline)
5676 != nfa_endp->se_u.pos.col)
5677 : reginput != nfa_endp->se_u.ptr))
5678 break;
5679
5680 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005681 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005682 {
5683 copy_sub(&m->norm, &t->subs.norm);
5684#ifdef FEAT_SYN_HL
5685 if (nfa_has_zsubexpr)
5686 copy_sub(&m->synt, &t->subs.synt);
5687#endif
5688 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005689#ifdef ENABLE_LOG
5690 fprintf(log_fd, "Match found:\n");
5691 log_subsexpr(m);
5692#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005693 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005694 /* See comment above at "goto nextchar". */
5695 if (nextlist->n == 0)
5696 clen = 0;
5697 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698
5699 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005700 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005701 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005702 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005703 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005704 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005705 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005706 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005707 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005708#ifdef ENABLE_LOG
5709 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5710 failure_chance(t->state->out, 0),
5711 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005712#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005713 /* Do it directly if there already is a PIM or when
5714 * nfa_postprocess() detected it will work better. */
5715 if (t->pim.result != NFA_PIM_UNUSED
5716 || t->state->c == NFA_START_INVISIBLE_FIRST
5717 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5718 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5719 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005720 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005721 int in_use = m->norm.in_use;
5722
Bram Moolenaar699c1202013-09-25 16:41:54 +02005723 /* Copy submatch info for the recursive call, opposite
5724 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005725 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005726#ifdef FEAT_SYN_HL
5727 if (nfa_has_zsubexpr)
5728 copy_sub_off(&m->synt, &t->subs.synt);
5729#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005730
Bram Moolenaara2d95102013-06-04 14:23:05 +02005731 /*
5732 * First try matching the invisible match, then what
5733 * follows.
5734 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005735 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005736 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005737 if (result == NFA_TOO_EXPENSIVE)
5738 {
5739 nfa_match = result;
5740 goto theend;
5741 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005742
Bram Moolenaardecd9542013-06-07 16:31:50 +02005743 /* for \@! and \@<! it is a match when the result is
5744 * FALSE */
5745 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005746 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5747 || t->state->c
5748 == NFA_START_INVISIBLE_BEFORE_NEG
5749 || t->state->c
5750 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005751 {
5752 /* Copy submatch info from the recursive call */
5753 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005754#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005755 if (nfa_has_zsubexpr)
5756 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005757#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005758 /* If the pattern has \ze and it matched in the
5759 * sub pattern, use it. */
5760 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005761
Bram Moolenaara2d95102013-06-04 14:23:05 +02005762 /* t->state->out1 is the corresponding
5763 * END_INVISIBLE node; Add its out to the current
5764 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005765 add_here = TRUE;
5766 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005767 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005768 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005769 }
5770 else
5771 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005772 nfa_pim_T pim;
5773
Bram Moolenaara2d95102013-06-04 14:23:05 +02005774 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005775 * First try matching what follows. Only if a match
5776 * is found verify the invisible match matches. Add a
5777 * nfa_pim_T to the following states, it contains info
5778 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005779 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005780 pim.state = t->state;
5781 pim.result = NFA_PIM_TODO;
5782 pim.subs.norm.in_use = 0;
5783#ifdef FEAT_SYN_HL
5784 pim.subs.synt.in_use = 0;
5785#endif
5786 if (REG_MULTI)
5787 {
5788 pim.end.pos.col = (int)(reginput - regline);
5789 pim.end.pos.lnum = reglnum;
5790 }
5791 else
5792 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005793
5794 /* t->state->out1 is the corresponding END_INVISIBLE
5795 * node; Add its out to the current list (zero-width
5796 * match). */
5797 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005798 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005799 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005800 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005801 break;
5802
Bram Moolenaar87953742013-06-05 18:52:40 +02005803 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005804 {
5805 nfa_state_T *skip = NULL;
5806#ifdef ENABLE_LOG
5807 int skip_lid = 0;
5808#endif
5809
5810 /* There is no point in trying to match the pattern if the
5811 * output state is not going to be added to the list. */
5812 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5813 {
5814 skip = t->state->out1->out;
5815#ifdef ENABLE_LOG
5816 skip_lid = nextlist->id;
5817#endif
5818 }
5819 else if (state_in_list(nextlist,
5820 t->state->out1->out->out, &t->subs))
5821 {
5822 skip = t->state->out1->out->out;
5823#ifdef ENABLE_LOG
5824 skip_lid = nextlist->id;
5825#endif
5826 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005827 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005828 t->state->out1->out->out, &t->subs))
5829 {
5830 skip = t->state->out1->out->out;
5831#ifdef ENABLE_LOG
5832 skip_lid = thislist->id;
5833#endif
5834 }
5835 if (skip != NULL)
5836 {
5837#ifdef ENABLE_LOG
5838 nfa_set_code(skip->c);
5839 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5840 abs(skip->id), skip_lid, skip->c, code);
5841#endif
5842 break;
5843 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005844 /* Copy submatch info to the recursive call, opposite of what
5845 * happens afterwards. */
5846 copy_sub_off(&m->norm, &t->subs.norm);
5847#ifdef FEAT_SYN_HL
5848 if (nfa_has_zsubexpr)
5849 copy_sub_off(&m->synt, &t->subs.synt);
5850#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005851
Bram Moolenaar87953742013-06-05 18:52:40 +02005852 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005853 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005854 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005855 if (result == NFA_TOO_EXPENSIVE)
5856 {
5857 nfa_match = result;
5858 goto theend;
5859 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005860 if (result)
5861 {
5862 int bytelen;
5863
5864#ifdef ENABLE_LOG
5865 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5866 log_subsexpr(m);
5867#endif
5868 /* Copy submatch info from the recursive call */
5869 copy_sub_off(&t->subs.norm, &m->norm);
5870#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005871 if (nfa_has_zsubexpr)
5872 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005873#endif
5874 /* Now we need to skip over the matched text and then
5875 * continue with what follows. */
5876 if (REG_MULTI)
5877 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005878 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005879 - (int)(reginput - regline);
5880 else
5881 bytelen = (int)(m->norm.list.line[0].end - reginput);
5882
5883#ifdef ENABLE_LOG
5884 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5885#endif
5886 if (bytelen == 0)
5887 {
5888 /* empty match, output of corresponding
5889 * NFA_END_PATTERN/NFA_SKIP to be used at current
5890 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005891 add_here = TRUE;
5892 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005893 }
5894 else if (bytelen <= clen)
5895 {
5896 /* match current character, output of corresponding
5897 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005898 add_state = t->state->out1->out->out;
5899 add_off = clen;
5900 }
5901 else
5902 {
5903 /* skip over the matched characters, set character
5904 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005905 add_state = t->state->out1->out;
5906 add_off = bytelen;
5907 add_count = bytelen - clen;
5908 }
5909 }
5910 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005911 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005912
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005913 case NFA_BOL:
5914 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005915 {
5916 add_here = TRUE;
5917 add_state = t->state->out;
5918 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005919 break;
5920
5921 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005922 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005923 {
5924 add_here = TRUE;
5925 add_state = t->state->out;
5926 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005927 break;
5928
5929 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005930 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005931
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005932 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005933 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005934#ifdef FEAT_MBYTE
5935 else if (has_mbyte)
5936 {
5937 int this_class;
5938
5939 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005940 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005941 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005942 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005943 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005944 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005945 }
5946#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005947 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005948 || (reginput > regline
5949 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005950 result = FALSE;
5951 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005952 {
5953 add_here = TRUE;
5954 add_state = t->state->out;
5955 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005956 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005957
5958 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005959 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005960 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005961 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005962#ifdef FEAT_MBYTE
5963 else if (has_mbyte)
5964 {
5965 int this_class, prev_class;
5966
5967 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005968 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005969 prev_class = reg_prev_class();
5970 if (this_class == prev_class
5971 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005972 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005973 }
5974#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005975 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005976 || (reginput[0] != NUL
5977 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005978 result = FALSE;
5979 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005980 {
5981 add_here = TRUE;
5982 add_state = t->state->out;
5983 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005984 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005985
Bram Moolenaar4b780632013-05-31 22:14:52 +02005986 case NFA_BOF:
5987 if (reglnum == 0 && reginput == regline
5988 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005989 {
5990 add_here = TRUE;
5991 add_state = t->state->out;
5992 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005993 break;
5994
5995 case NFA_EOF:
5996 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005997 {
5998 add_here = TRUE;
5999 add_state = t->state->out;
6000 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006001 break;
6002
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006003#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006004 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006005 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006006 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006007 int len = 0;
6008 nfa_state_T *end;
6009 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006010 int cchars[MAX_MCO];
6011 int ccount = 0;
6012 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006013
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006014 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006015 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006016 if (utf_iscomposing(sta->c))
6017 {
6018 /* Only match composing character(s), ignore base
6019 * character. Used for ".{composing}" and "{composing}"
6020 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006021 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006022 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02006023 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006024 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006025 /* If \Z was present, then ignore composing characters.
6026 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006027 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006028 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006029 else
6030 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006031 while (sta->c != NFA_END_COMPOSING)
6032 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006033 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006034
6035 /* Check base character matches first, unless ignored. */
6036 else if (len > 0 || mc == sta->c)
6037 {
6038 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006039 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006040 len += mb_char2len(mc);
6041 sta = sta->out;
6042 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006043
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006044 /* We don't care about the order of composing characters.
6045 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006046 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006047 {
6048 mc = mb_ptr2char(reginput + len);
6049 cchars[ccount++] = mc;
6050 len += mb_char2len(mc);
6051 if (ccount == MAX_MCO)
6052 break;
6053 }
6054
6055 /* Check that each composing char in the pattern matches a
6056 * composing char in the text. We do not check if all
6057 * composing chars are matched. */
6058 result = OK;
6059 while (sta->c != NFA_END_COMPOSING)
6060 {
6061 for (j = 0; j < ccount; ++j)
6062 if (cchars[j] == sta->c)
6063 break;
6064 if (j == ccount)
6065 {
6066 result = FAIL;
6067 break;
6068 }
6069 sta = sta->out;
6070 }
6071 }
6072 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006073 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006074
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006075 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006076 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006077 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006078 }
6079#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006080
6081 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006082 if (curc == NUL && !reg_line_lbr && REG_MULTI
6083 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006084 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006085 go_to_nextline = TRUE;
6086 /* Pass -1 for the offset, which means taking the position
6087 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006088 add_state = t->state->out;
6089 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006090 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006091 else if (curc == '\n' && reg_line_lbr)
6092 {
6093 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006094 add_state = t->state->out;
6095 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006096 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006097 break;
6098
Bram Moolenaar417bad22013-06-07 14:08:30 +02006099 case NFA_START_COLL:
6100 case NFA_START_NEG_COLL:
6101 {
6102 /* What follows is a list of characters, until NFA_END_COLL.
6103 * One of them must match or none of them must match. */
6104 nfa_state_T *state;
6105 int result_if_matched;
6106 int c1, c2;
6107
6108 /* Never match EOL. If it's part of the collection it is added
6109 * as a separate state with an OR. */
6110 if (curc == NUL)
6111 break;
6112
6113 state = t->state->out;
6114 result_if_matched = (t->state->c == NFA_START_COLL);
6115 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006116 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006117 if (state->c == NFA_END_COLL)
6118 {
6119 result = !result_if_matched;
6120 break;
6121 }
6122 if (state->c == NFA_RANGE_MIN)
6123 {
6124 c1 = state->val;
6125 state = state->out; /* advance to NFA_RANGE_MAX */
6126 c2 = state->val;
6127#ifdef ENABLE_LOG
6128 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6129 curc, c1, c2);
6130#endif
6131 if (curc >= c1 && curc <= c2)
6132 {
6133 result = result_if_matched;
6134 break;
6135 }
6136 if (ireg_ic)
6137 {
6138 int curc_low = MB_TOLOWER(curc);
6139 int done = FALSE;
6140
6141 for ( ; c1 <= c2; ++c1)
6142 if (MB_TOLOWER(c1) == curc_low)
6143 {
6144 result = result_if_matched;
6145 done = TRUE;
6146 break;
6147 }
6148 if (done)
6149 break;
6150 }
6151 }
6152 else if (state->c < 0 ? check_char_class(state->c, curc)
6153 : (curc == state->c
6154 || (ireg_ic && MB_TOLOWER(curc)
6155 == MB_TOLOWER(state->c))))
6156 {
6157 result = result_if_matched;
6158 break;
6159 }
6160 state = state->out;
6161 }
6162 if (result)
6163 {
6164 /* next state is in out of the NFA_END_COLL, out1 of
6165 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006166 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006167 add_off = clen;
6168 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006169 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006170 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171
6172 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006173 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006174 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006175 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006176 add_state = t->state->out;
6177 add_off = clen;
6178 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006179 break;
6180
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006181 case NFA_ANY_COMPOSING:
6182 /* On a composing character skip over it. Otherwise do
6183 * nothing. Always matches. */
6184#ifdef FEAT_MBYTE
6185 if (enc_utf8 && utf_iscomposing(curc))
6186 {
6187 add_off = clen;
6188 }
6189 else
6190#endif
6191 {
6192 add_here = TRUE;
6193 add_off = 0;
6194 }
6195 add_state = t->state->out;
6196 break;
6197
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006198 /*
6199 * Character classes like \a for alpha, \d for digit etc.
6200 */
6201 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006202 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006203 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006204 break;
6205
6206 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006207 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006208 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006209 break;
6210
6211 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006212 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006213 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006214 break;
6215
6216 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006217 result = !VIM_ISDIGIT(curc)
6218 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006219 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006220 break;
6221
6222 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006223 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006224 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006225 break;
6226
6227 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006228 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006229 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006230 break;
6231
6232 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006233 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006234 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006235 break;
6236
6237 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006238 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006239 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006240 break;
6241
6242 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006243 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006244 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006245 break;
6246
6247 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006248 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006249 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006250 break;
6251
6252 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006253 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006254 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006255 break;
6256
6257 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006258 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006259 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006260 break;
6261
6262 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006263 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006264 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006265 break;
6266
6267 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006268 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006269 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006270 break;
6271
6272 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006273 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006274 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006275 break;
6276
6277 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006278 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006279 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006280 break;
6281
6282 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006283 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006284 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006285 break;
6286
6287 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006288 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006289 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006290 break;
6291
6292 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006293 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006294 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006295 break;
6296
6297 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006298 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006299 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006300 break;
6301
6302 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006303 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006304 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006305 break;
6306
6307 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006308 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006309 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006310 break;
6311
6312 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006313 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006314 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006315 break;
6316
6317 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006318 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006319 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006320 break;
6321
6322 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006323 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006324 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006325 break;
6326
6327 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006328 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006329 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006330 break;
6331
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006332 case NFA_LOWER_IC: /* [a-z] */
6333 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6334 ADD_STATE_IF_MATCH(t->state);
6335 break;
6336
6337 case NFA_NLOWER_IC: /* [^a-z] */
6338 result = curc != NUL
6339 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6340 ADD_STATE_IF_MATCH(t->state);
6341 break;
6342
6343 case NFA_UPPER_IC: /* [A-Z] */
6344 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6345 ADD_STATE_IF_MATCH(t->state);
6346 break;
6347
6348 case NFA_NUPPER_IC: /* ^[A-Z] */
6349 result = curc != NUL
6350 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6351 ADD_STATE_IF_MATCH(t->state);
6352 break;
6353
Bram Moolenaar5714b802013-05-28 22:03:20 +02006354 case NFA_BACKREF1:
6355 case NFA_BACKREF2:
6356 case NFA_BACKREF3:
6357 case NFA_BACKREF4:
6358 case NFA_BACKREF5:
6359 case NFA_BACKREF6:
6360 case NFA_BACKREF7:
6361 case NFA_BACKREF8:
6362 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006363#ifdef FEAT_SYN_HL
6364 case NFA_ZREF1:
6365 case NFA_ZREF2:
6366 case NFA_ZREF3:
6367 case NFA_ZREF4:
6368 case NFA_ZREF5:
6369 case NFA_ZREF6:
6370 case NFA_ZREF7:
6371 case NFA_ZREF8:
6372 case NFA_ZREF9:
6373#endif
6374 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006375 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006376 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006377 int bytelen;
6378
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006379 if (t->state->c <= NFA_BACKREF9)
6380 {
6381 subidx = t->state->c - NFA_BACKREF1 + 1;
6382 result = match_backref(&t->subs.norm, subidx, &bytelen);
6383 }
6384#ifdef FEAT_SYN_HL
6385 else
6386 {
6387 subidx = t->state->c - NFA_ZREF1 + 1;
6388 result = match_zref(subidx, &bytelen);
6389 }
6390#endif
6391
Bram Moolenaar5714b802013-05-28 22:03:20 +02006392 if (result)
6393 {
6394 if (bytelen == 0)
6395 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006396 /* empty match always works, output of NFA_SKIP to be
6397 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006398 add_here = TRUE;
6399 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006400 }
6401 else if (bytelen <= clen)
6402 {
6403 /* match current character, jump ahead to out of
6404 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006405 add_state = t->state->out->out;
6406 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006407 }
6408 else
6409 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006410 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006411 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006412 add_state = t->state->out;
6413 add_off = bytelen;
6414 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006415 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006416 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006417 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006418 }
6419 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006420 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006421 if (t->count - clen <= 0)
6422 {
6423 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006424 add_state = t->state->out;
6425 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006426 }
6427 else
6428 {
6429 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006430 add_state = t->state;
6431 add_off = 0;
6432 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006433 }
6434 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006435
Bram Moolenaar423532e2013-05-29 21:14:42 +02006436 case NFA_LNUM:
6437 case NFA_LNUM_GT:
6438 case NFA_LNUM_LT:
6439 result = (REG_MULTI &&
6440 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6441 (long_u)(reglnum + reg_firstlnum)));
6442 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006443 {
6444 add_here = TRUE;
6445 add_state = t->state->out;
6446 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006447 break;
6448
6449 case NFA_COL:
6450 case NFA_COL_GT:
6451 case NFA_COL_LT:
6452 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6453 (long_u)(reginput - regline) + 1);
6454 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006455 {
6456 add_here = TRUE;
6457 add_state = t->state->out;
6458 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006459 break;
6460
6461 case NFA_VCOL:
6462 case NFA_VCOL_GT:
6463 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006464 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006465 int op = t->state->c - NFA_VCOL;
6466 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaaref795d12015-01-18 16:46:32 +01006467 win_T *wp = reg_win == NULL ? curwin : reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006468
6469 /* Bail out quickly when there can't be a match, avoid the
6470 * overhead of win_linetabsize() on long lines. */
Bram Moolenaaref795d12015-01-18 16:46:32 +01006471 if (op != 1 && col > t->state->val)
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006472 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006473 result = FALSE;
6474 if (op == 1 && col - 1 > t->state->val && col > 100)
6475 {
6476 int ts = wp->w_buffer->b_p_ts;
6477
6478 /* Guess that a character won't use more columns than
6479 * 'tabstop', with a minimum of 4. */
6480 if (ts < 4)
6481 ts = 4;
6482 result = col > t->state->val * ts;
6483 }
6484 if (!result)
6485 result = nfa_re_num_cmp(t->state->val, op,
6486 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006487 if (result)
6488 {
6489 add_here = TRUE;
6490 add_state = t->state->out;
6491 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006492 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006493 break;
6494
Bram Moolenaar044aa292013-06-04 21:27:38 +02006495 case NFA_MARK:
6496 case NFA_MARK_GT:
6497 case NFA_MARK_LT:
6498 {
6499 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6500
6501 /* Compare the mark position to the match position. */
6502 result = (pos != NULL /* mark doesn't exist */
6503 && pos->lnum > 0 /* mark isn't set in reg_buf */
6504 && (pos->lnum == reglnum + reg_firstlnum
6505 ? (pos->col == (colnr_T)(reginput - regline)
6506 ? t->state->c == NFA_MARK
6507 : (pos->col < (colnr_T)(reginput - regline)
6508 ? t->state->c == NFA_MARK_GT
6509 : t->state->c == NFA_MARK_LT))
6510 : (pos->lnum < reglnum + reg_firstlnum
6511 ? t->state->c == NFA_MARK_GT
6512 : t->state->c == NFA_MARK_LT)));
6513 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006514 {
6515 add_here = TRUE;
6516 add_state = t->state->out;
6517 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006518 break;
6519 }
6520
Bram Moolenaar423532e2013-05-29 21:14:42 +02006521 case NFA_CURSOR:
6522 result = (reg_win != NULL
6523 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6524 && ((colnr_T)(reginput - regline)
6525 == reg_win->w_cursor.col));
6526 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006527 {
6528 add_here = TRUE;
6529 add_state = t->state->out;
6530 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006531 break;
6532
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006533 case NFA_VISUAL:
6534 result = reg_match_visual();
6535 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006536 {
6537 add_here = TRUE;
6538 add_state = t->state->out;
6539 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006540 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006541
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006542 case NFA_MOPEN1:
6543 case NFA_MOPEN2:
6544 case NFA_MOPEN3:
6545 case NFA_MOPEN4:
6546 case NFA_MOPEN5:
6547 case NFA_MOPEN6:
6548 case NFA_MOPEN7:
6549 case NFA_MOPEN8:
6550 case NFA_MOPEN9:
6551#ifdef FEAT_SYN_HL
6552 case NFA_ZOPEN:
6553 case NFA_ZOPEN1:
6554 case NFA_ZOPEN2:
6555 case NFA_ZOPEN3:
6556 case NFA_ZOPEN4:
6557 case NFA_ZOPEN5:
6558 case NFA_ZOPEN6:
6559 case NFA_ZOPEN7:
6560 case NFA_ZOPEN8:
6561 case NFA_ZOPEN9:
6562#endif
6563 case NFA_NOPEN:
6564 case NFA_ZSTART:
6565 /* These states are only added to be able to bail out when
6566 * they are added again, nothing is to be done. */
6567 break;
6568
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006569 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006570 {
6571 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006572
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006573#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006574 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006575 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006576#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006577 result = (c == curc);
6578
6579 if (!result && ireg_ic)
6580 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006581#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006582 /* If ireg_icombine is not set only skip over the character
6583 * itself. When it is set skip over composing characters. */
6584 if (result && enc_utf8 && !ireg_icombine)
6585 clen = utf_char2len(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006586#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006587 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006588 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006589 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006590
6591 } /* switch (t->state->c) */
6592
6593 if (add_state != NULL)
6594 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006595 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006596 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006597
6598 if (t->pim.result == NFA_PIM_UNUSED)
6599 pim = NULL;
6600 else
6601 pim = &t->pim;
6602
6603 /* Handle the postponed invisible match if the match might end
6604 * without advancing and before the end of the line. */
6605 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006606 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006607 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006608 {
6609#ifdef ENABLE_LOG
6610 fprintf(log_fd, "\n");
6611 fprintf(log_fd, "==================================\n");
6612 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6613 fprintf(log_fd, "\n");
6614#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006615 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006616 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006617 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006618 /* for \@! and \@<! it is a match when the result is
6619 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006620 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006621 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6622 || pim->state->c
6623 == NFA_START_INVISIBLE_BEFORE_NEG
6624 || pim->state->c
6625 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006626 {
6627 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006628 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006629#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006630 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006631 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006632#endif
6633 }
6634 }
6635 else
6636 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006637 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006638#ifdef ENABLE_LOG
6639 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006640 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006641 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6642 fprintf(log_fd, "\n");
6643#endif
6644 }
6645
Bram Moolenaardecd9542013-06-07 16:31:50 +02006646 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006647 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006648 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6649 || pim->state->c
6650 == NFA_START_INVISIBLE_BEFORE_NEG
6651 || pim->state->c
6652 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006653 {
6654 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006655 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006656#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006657 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006658 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006659#endif
6660 }
6661 else
6662 /* look-behind match failed, don't add the state */
6663 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006664
6665 /* Postponed invisible match was handled, don't add it to
6666 * following states. */
6667 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006668 }
6669
Bram Moolenaara951e352013-10-06 15:46:11 +02006670 /* If "pim" points into l->t it will become invalid when
6671 * adding the state causes the list to be reallocated. Make a
6672 * local copy to avoid that. */
6673 if (pim == &t->pim)
6674 {
6675 copy_pim(&pim_copy, pim);
6676 pim = &pim_copy;
6677 }
6678
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006679 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006680 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006681 else
6682 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006683 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006684 if (add_count > 0)
6685 nextlist->t[nextlist->n - 1].count = add_count;
6686 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006687 }
6688
6689 } /* for (thislist = thislist; thislist->state; thislist++) */
6690
Bram Moolenaare23febd2013-05-26 18:40:14 +02006691 /* Look for the start of a match in the current position by adding the
6692 * start state to the list of states.
6693 * The first found match is the leftmost one, thus the order of states
6694 * matters!
6695 * Do not add the start state in recursive calls of nfa_regmatch(),
6696 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006697 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006698 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006699 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006700 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006701 && reglnum == 0
6702 && clen != 0
6703 && (ireg_maxcol == 0
6704 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006705 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006706 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006707 ? (reglnum < nfa_endp->se_u.pos.lnum
6708 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006709 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006710 < nfa_endp->se_u.pos.col))
6711 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006712 {
6713#ifdef ENABLE_LOG
6714 fprintf(log_fd, "(---) STARTSTATE\n");
6715#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006716 /* Inline optimized code for addstate() if we know the state is
6717 * the first MOPEN. */
6718 if (toplevel)
6719 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006720 int add = TRUE;
6721 int c;
6722
6723 if (prog->regstart != NUL && clen != 0)
6724 {
6725 if (nextlist->n == 0)
6726 {
6727 colnr_T col = (colnr_T)(reginput - regline) + clen;
6728
6729 /* Nextlist is empty, we can skip ahead to the
6730 * character that must appear at the start. */
6731 if (skip_to_start(prog->regstart, &col) == FAIL)
6732 break;
6733#ifdef ENABLE_LOG
6734 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6735 col - ((colnr_T)(reginput - regline) + clen));
6736#endif
6737 reginput = regline + col - clen;
6738 }
6739 else
6740 {
6741 /* Checking if the required start character matches is
6742 * cheaper than adding a state that won't match. */
6743 c = PTR2CHAR(reginput + clen);
6744 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6745 != MB_TOLOWER(prog->regstart)))
6746 {
6747#ifdef ENABLE_LOG
6748 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6749#endif
6750 add = FALSE;
6751 }
6752 }
6753 }
6754
6755 if (add)
6756 {
6757 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006758 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006759 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006760 else
6761 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006762 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006763 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006764 }
6765 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006766 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006767 }
6768
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006769#ifdef ENABLE_LOG
6770 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006771 {
6772 int i;
6773
6774 for (i = 0; i < thislist->n; i++)
6775 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6776 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006777 fprintf(log_fd, "\n");
6778#endif
6779
6780nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006781 /* Advance to the next character, or advance to the next line, or
6782 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006783 if (clen != 0)
6784 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006785 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6786 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006787 reg_nextline();
6788 else
6789 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006790
6791 /* Allow interrupting with CTRL-C. */
6792 fast_breakcheck();
6793 if (got_int)
6794 break;
Bram Moolenaar35b23862013-05-22 23:00:40 +02006795 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006796
6797#ifdef ENABLE_LOG
6798 if (log_fd != stderr)
6799 fclose(log_fd);
6800 log_fd = NULL;
6801#endif
6802
6803theend:
6804 /* Free memory */
6805 vim_free(list[0].t);
6806 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006807 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006808#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006809#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006810 fclose(debug);
6811#endif
6812
Bram Moolenaar963fee22013-05-26 21:47:28 +02006813 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006814}
6815
6816/*
6817 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006818 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006819 */
6820 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006821nfa_regtry(prog, col)
6822 nfa_regprog_T *prog;
6823 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006824{
6825 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006826 regsubs_T subs, m;
6827 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006828 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006829#ifdef ENABLE_LOG
6830 FILE *f;
6831#endif
6832
6833 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006834
6835#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006836 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006837 if (f != NULL)
6838 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006839 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006840#ifdef DEBUG
6841 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6842#endif
6843 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006844 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006845 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006846 fprintf(f, "\n\n");
6847 fclose(f);
6848 }
6849 else
6850 EMSG(_("Could not open temporary log file for writing "));
6851#endif
6852
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006853 clear_sub(&subs.norm);
6854 clear_sub(&m.norm);
6855#ifdef FEAT_SYN_HL
6856 clear_sub(&subs.synt);
6857 clear_sub(&m.synt);
6858#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006859
Bram Moolenaarfda37292014-11-05 14:27:36 +01006860 result = nfa_regmatch(prog, start, &subs, &m);
6861 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006862 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006863 else if (result == NFA_TOO_EXPENSIVE)
6864 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006865
6866 cleanup_subexpr();
6867 if (REG_MULTI)
6868 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006869 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006870 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006871 reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6872 reg_startpos[i].col = subs.norm.list.multi[i].start_col;
6873
6874 reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6875 reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006876 }
6877
6878 if (reg_startpos[0].lnum < 0)
6879 {
6880 reg_startpos[0].lnum = 0;
6881 reg_startpos[0].col = col;
6882 }
6883 if (reg_endpos[0].lnum < 0)
6884 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006885 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006886 reg_endpos[0].lnum = reglnum;
6887 reg_endpos[0].col = (int)(reginput - regline);
6888 }
6889 else
6890 /* Use line number of "\ze". */
6891 reglnum = reg_endpos[0].lnum;
6892 }
6893 else
6894 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006895 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006896 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006897 reg_startp[i] = subs.norm.list.line[i].start;
6898 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006899 }
6900
6901 if (reg_startp[0] == NULL)
6902 reg_startp[0] = regline + col;
6903 if (reg_endp[0] == NULL)
6904 reg_endp[0] = reginput;
6905 }
6906
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006907#ifdef FEAT_SYN_HL
6908 /* Package any found \z(...\) matches for export. Default is none. */
6909 unref_extmatch(re_extmatch_out);
6910 re_extmatch_out = NULL;
6911
6912 if (prog->reghasz == REX_SET)
6913 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006914 cleanup_zsubexpr();
6915 re_extmatch_out = make_extmatch();
6916 for (i = 0; i < subs.synt.in_use; i++)
6917 {
6918 if (REG_MULTI)
6919 {
6920 struct multipos *mpos = &subs.synt.list.multi[i];
6921
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006922 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006923 if (mpos->start_lnum >= 0
6924 && mpos->start_lnum == mpos->end_lnum
6925 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006926 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006927 vim_strnsave(reg_getline(mpos->start_lnum)
6928 + mpos->start_col,
6929 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006930 }
6931 else
6932 {
6933 struct linepos *lpos = &subs.synt.list.line[i];
6934
6935 if (lpos->start != NULL && lpos->end != NULL)
6936 re_extmatch_out->matches[i] =
6937 vim_strnsave(lpos->start,
6938 (int)(lpos->end - lpos->start));
6939 }
6940 }
6941 }
6942#endif
6943
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006944 return 1 + reglnum;
6945}
6946
6947/*
6948 * Match a regexp against a string ("line" points to the string) or multiple
6949 * lines ("line" is NULL, use reg_getline()).
6950 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01006951 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006952 */
6953 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006954nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006955 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006956 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006957{
6958 nfa_regprog_T *prog;
6959 long retval = 0L;
6960 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006961 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006962
6963 if (REG_MULTI)
6964 {
6965 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6966 line = reg_getline((linenr_T)0); /* relative to the cursor */
6967 reg_startpos = reg_mmatch->startpos;
6968 reg_endpos = reg_mmatch->endpos;
6969 }
6970 else
6971 {
6972 prog = (nfa_regprog_T *)reg_match->regprog;
6973 reg_startp = reg_match->startp;
6974 reg_endp = reg_match->endp;
6975 }
6976
6977 /* Be paranoid... */
6978 if (prog == NULL || line == NULL)
6979 {
6980 EMSG(_(e_null));
6981 goto theend;
6982 }
6983
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006984 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6985 if (prog->regflags & RF_ICASE)
6986 ireg_ic = TRUE;
6987 else if (prog->regflags & RF_NOICASE)
6988 ireg_ic = FALSE;
6989
6990#ifdef FEAT_MBYTE
6991 /* If pattern contains "\Z" overrule value of ireg_icombine */
6992 if (prog->regflags & RF_ICOMBINE)
6993 ireg_icombine = TRUE;
6994#endif
6995
6996 regline = line;
6997 reglnum = 0; /* relative to line */
6998
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006999 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007000 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007001 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007002 nfa_listid = 1;
7003 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007004 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007005
Bram Moolenaard89616e2013-06-06 18:46:06 +02007006 if (prog->reganch && col > 0)
7007 return 0L;
7008
Bram Moolenaar473de612013-06-08 18:19:48 +02007009 need_clear_subexpr = TRUE;
7010#ifdef FEAT_SYN_HL
7011 /* Clear the external match subpointers if necessary. */
7012 if (prog->reghasz == REX_SET)
7013 {
7014 nfa_has_zsubexpr = TRUE;
7015 need_clear_zsubexpr = TRUE;
7016 }
7017 else
7018 nfa_has_zsubexpr = FALSE;
7019#endif
7020
Bram Moolenaard89616e2013-06-06 18:46:06 +02007021 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007022 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007023 /* Skip ahead until a character we know the match must start with.
7024 * When there is none there is no match. */
7025 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007026 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007027
Bram Moolenaar473de612013-06-08 18:19:48 +02007028 /* If match_text is set it contains the full text that must match.
7029 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007030 if (prog->match_text != NULL
7031#ifdef FEAT_MBYTE
7032 && !ireg_icombine
7033#endif
7034 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007035 return find_match_text(col, prog->regstart, prog->match_text);
7036 }
7037
Bram Moolenaard89616e2013-06-06 18:46:06 +02007038 /* If the start column is past the maximum column: no need to try. */
7039 if (ireg_maxcol > 0 && col >= ireg_maxcol)
7040 goto theend;
7041
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007042 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007043 for (i = 0; i < nstate; ++i)
7044 {
7045 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007046 prog->state[i].lastlist[0] = 0;
7047 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007048 }
7049
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007050 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007051
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007052 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007053
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007054theend:
7055 return retval;
7056}
7057
7058/*
7059 * Compile a regular expression into internal code for the NFA matcher.
7060 * Returns the program in allocated space. Returns NULL for an error.
7061 */
7062 static regprog_T *
7063nfa_regcomp(expr, re_flags)
7064 char_u *expr;
7065 int re_flags;
7066{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007067 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007068 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007069 int *postfix;
7070
7071 if (expr == NULL)
7072 return NULL;
7073
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007074 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007075 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007076
7077 init_class_tab();
7078
7079 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7080 return NULL;
7081
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007082 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007083 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007084 postfix = re2post();
7085 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007086 {
7087 /* TODO: only give this error for debugging? */
7088 if (post_ptr >= post_end)
7089 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007090 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007091 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007092
7093 /*
7094 * In order to build the NFA, we parse the input regexp twice:
7095 * 1. first pass to count size (so we can allocate space)
7096 * 2. second to emit code
7097 */
7098#ifdef ENABLE_LOG
7099 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007100 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007101
7102 if (f != NULL)
7103 {
7104 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7105 fclose(f);
7106 }
7107 }
7108#endif
7109
7110 /*
7111 * PASS 1
7112 * Count number of NFA states in "nstate". Do not build the NFA.
7113 */
7114 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007115
Bram Moolenaar16619a22013-06-11 18:42:36 +02007116 /* allocate the regprog with space for the compiled regexp */
7117 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007118 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7119 if (prog == NULL)
7120 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007121 state_ptr = prog->state;
7122
7123 /*
7124 * PASS 2
7125 * Build the NFA
7126 */
7127 prog->start = post2nfa(postfix, post_ptr, FALSE);
7128 if (prog->start == NULL)
7129 goto fail;
7130
7131 prog->regflags = regflags;
7132 prog->engine = &nfa_regengine;
7133 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007134 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007135 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007136 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007137
Bram Moolenaara2947e22013-06-11 22:44:09 +02007138 nfa_postprocess(prog);
7139
Bram Moolenaard89616e2013-06-06 18:46:06 +02007140 prog->reganch = nfa_get_reganch(prog->start, 0);
7141 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007142 prog->match_text = nfa_get_match_text(prog->start);
7143
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007144#ifdef ENABLE_LOG
7145 nfa_postfix_dump(expr, OK);
7146 nfa_dump(prog);
7147#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007148#ifdef FEAT_SYN_HL
7149 /* Remember whether this pattern has any \z specials in it. */
7150 prog->reghasz = re_has_z;
7151#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007152 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007153 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007154
7155out:
7156 vim_free(post_start);
7157 post_start = post_ptr = post_end = NULL;
7158 state_ptr = NULL;
7159 return (regprog_T *)prog;
7160
7161fail:
7162 vim_free(prog);
7163 prog = NULL;
7164#ifdef ENABLE_LOG
7165 nfa_postfix_dump(expr, FAIL);
7166#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007167 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168 goto out;
7169}
7170
Bram Moolenaar473de612013-06-08 18:19:48 +02007171/*
7172 * Free a compiled regexp program, returned by nfa_regcomp().
7173 */
7174 static void
7175nfa_regfree(prog)
7176 regprog_T *prog;
7177{
7178 if (prog != NULL)
7179 {
7180 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007181 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007182 vim_free(prog);
7183 }
7184}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007185
7186/*
7187 * Match a regexp against a string.
7188 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7189 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007190 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007191 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007192 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007193 */
7194 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007195nfa_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007196 regmatch_T *rmp;
7197 char_u *line; /* string to match against */
7198 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007199 int line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007200{
7201 reg_match = rmp;
7202 reg_mmatch = NULL;
7203 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007204 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007205 reg_buf = curbuf;
7206 reg_win = NULL;
7207 ireg_ic = rmp->rm_ic;
7208#ifdef FEAT_MBYTE
7209 ireg_icombine = FALSE;
7210#endif
7211 ireg_maxcol = 0;
Bram Moolenaar8c731502014-11-23 15:57:49 +01007212 return nfa_regexec_both(line, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007213}
7214
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007215
7216/*
7217 * Match a regexp against multiple lines.
7218 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7219 * Uses curbuf for line count and 'iskeyword'.
7220 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007221 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007222 * match otherwise.
7223 *
7224 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7225 *
7226 * ! Also NOTE : match may actually be in another line. e.g.:
7227 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7228 *
7229 * +-------------------------+
7230 * |a |
7231 * |b |
7232 * |c |
7233 * | |
7234 * +-------------------------+
7235 *
7236 * then nfa_regexec_multi() returns 3. while the original
7237 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7238 *
7239 * FIXME if this behavior is not compatible.
7240 */
7241 static long
7242nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7243 regmmatch_T *rmp;
7244 win_T *win; /* window in which to search or NULL */
7245 buf_T *buf; /* buffer in which to search */
7246 linenr_T lnum; /* nr of line to start looking for match */
7247 colnr_T col; /* column to start looking for match */
7248 proftime_T *tm UNUSED; /* timeout limit or NULL */
7249{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007250 reg_match = NULL;
7251 reg_mmatch = rmp;
7252 reg_buf = buf;
7253 reg_win = win;
7254 reg_firstlnum = lnum;
7255 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7256 reg_line_lbr = FALSE;
7257 ireg_ic = rmp->rmm_ic;
7258#ifdef FEAT_MBYTE
7259 ireg_icombine = FALSE;
7260#endif
7261 ireg_maxcol = rmp->rmm_maxcol;
7262
Bram Moolenaarf878bf02013-05-21 21:20:20 +02007263 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007264}
7265
7266#ifdef DEBUG
7267# undef ENABLE_LOG
7268#endif