blob: 1e16bd98aa6b9f823ae6065794e4fce88821ad06 [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 Moolenaar70781ee2015-02-03 16:49:24 +0100314static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col, proftime_T *tm));
315static long nfa_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316static 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 Moolenaar70781ee2015-02-03 16:49:24 +01003853#ifdef FEAT_RELTIME
3854static proftime_T *nfa_time_limit;
3855static int nfa_time_count;
3856#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003857
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003858static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003859static void clear_sub __ARGS((regsub_T *sub));
3860static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3861static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaarf2118842013-09-25 18:16:38 +02003862static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003863static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003864static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003865static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3866static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003867static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003868static 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 +02003869static 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 +02003870
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003871/*
3872 * Copy postponed invisible match info from "from" to "to".
3873 */
3874 static void
3875copy_pim(to, from)
3876 nfa_pim_T *to;
3877 nfa_pim_T *from;
3878{
3879 to->result = from->result;
3880 to->state = from->state;
3881 copy_sub(&to->subs.norm, &from->subs.norm);
3882#ifdef FEAT_SYN_HL
3883 if (nfa_has_zsubexpr)
3884 copy_sub(&to->subs.synt, &from->subs.synt);
3885#endif
3886 to->end = from->end;
3887}
3888
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003889 static void
3890clear_sub(sub)
3891 regsub_T *sub;
3892{
3893 if (REG_MULTI)
3894 /* Use 0xff to set lnum to -1 */
3895 vim_memset(sub->list.multi, 0xff,
3896 sizeof(struct multipos) * nfa_nsubexpr);
3897 else
3898 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3899 sub->in_use = 0;
3900}
3901
3902/*
3903 * Copy the submatches from "from" to "to".
3904 */
3905 static void
3906copy_sub(to, from)
3907 regsub_T *to;
3908 regsub_T *from;
3909{
3910 to->in_use = from->in_use;
3911 if (from->in_use > 0)
3912 {
3913 /* Copy the match start and end positions. */
3914 if (REG_MULTI)
3915 mch_memmove(&to->list.multi[0],
3916 &from->list.multi[0],
3917 sizeof(struct multipos) * from->in_use);
3918 else
3919 mch_memmove(&to->list.line[0],
3920 &from->list.line[0],
3921 sizeof(struct linepos) * from->in_use);
3922 }
3923}
3924
3925/*
3926 * Like copy_sub() but exclude the main match.
3927 */
3928 static void
3929copy_sub_off(to, from)
3930 regsub_T *to;
3931 regsub_T *from;
3932{
3933 if (to->in_use < from->in_use)
3934 to->in_use = from->in_use;
3935 if (from->in_use > 1)
3936 {
3937 /* Copy the match start and end positions. */
3938 if (REG_MULTI)
3939 mch_memmove(&to->list.multi[1],
3940 &from->list.multi[1],
3941 sizeof(struct multipos) * (from->in_use - 1));
3942 else
3943 mch_memmove(&to->list.line[1],
3944 &from->list.line[1],
3945 sizeof(struct linepos) * (from->in_use - 1));
3946 }
3947}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003948
Bram Moolenaar428e9872013-05-30 17:05:39 +02003949/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003950 * Like copy_sub() but only do the end of the main match if \ze is present.
3951 */
3952 static void
3953copy_ze_off(to, from)
3954 regsub_T *to;
3955 regsub_T *from;
3956{
3957 if (nfa_has_zend)
3958 {
3959 if (REG_MULTI)
3960 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003961 if (from->list.multi[0].end_lnum >= 0)
3962 {
3963 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3964 to->list.multi[0].end_col = from->list.multi[0].end_col;
3965 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02003966 }
3967 else
3968 {
3969 if (from->list.line[0].end != NULL)
3970 to->list.line[0].end = from->list.line[0].end;
3971 }
3972 }
3973}
3974
3975/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003976 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003977 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003978 */
3979 static int
3980sub_equal(sub1, sub2)
3981 regsub_T *sub1;
3982 regsub_T *sub2;
3983{
3984 int i;
3985 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003986 linenr_T s1;
3987 linenr_T s2;
3988 char_u *sp1;
3989 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003990
3991 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3992 if (REG_MULTI)
3993 {
3994 for (i = 0; i < todo; ++i)
3995 {
3996 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003997 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003998 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003999 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004000 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004001 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004002 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004003 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004004 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004005 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004006 if (s1 != -1 && sub1->list.multi[i].start_col
4007 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004008 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004009
4010 if (nfa_has_backref)
4011 {
4012 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004013 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004014 else
4015 s1 = -1;
4016 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004017 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004018 else
4019 s2 = -1;
4020 if (s1 != s2)
4021 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004022 if (s1 != -1 && sub1->list.multi[i].end_col
4023 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004024 return FALSE;
4025 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004026 }
4027 }
4028 else
4029 {
4030 for (i = 0; i < todo; ++i)
4031 {
4032 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004033 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004034 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004035 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004036 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004038 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004039 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004040 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004041 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004042 if (nfa_has_backref)
4043 {
4044 if (i < sub1->in_use)
4045 sp1 = sub1->list.line[i].end;
4046 else
4047 sp1 = NULL;
4048 if (i < sub2->in_use)
4049 sp2 = sub2->list.line[i].end;
4050 else
4051 sp2 = NULL;
4052 if (sp1 != sp2)
4053 return FALSE;
4054 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004055 }
4056 }
4057
4058 return TRUE;
4059}
4060
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004061#ifdef ENABLE_LOG
4062 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004063report_state(char *action,
4064 regsub_T *sub,
4065 nfa_state_T *state,
4066 int lid,
4067 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004068{
4069 int col;
4070
4071 if (sub->in_use <= 0)
4072 col = -1;
4073 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004074 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004075 else
4076 col = (int)(sub->list.line[0].start - regline);
4077 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004078 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4079 action, abs(state->id), lid, state->c, code, col,
4080 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004081}
4082#endif
4083
Bram Moolenaar43e02982013-06-07 17:31:29 +02004084/*
4085 * Return TRUE if the same state is already in list "l" with the same
4086 * positions as "subs".
4087 */
4088 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02004089has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02004090 nfa_list_T *l; /* runtime state list */
4091 nfa_state_T *state; /* state to update */
4092 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004093 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004094{
4095 nfa_thread_T *thread;
4096 int i;
4097
4098 for (i = 0; i < l->n; ++i)
4099 {
4100 thread = &l->t[i];
4101 if (thread->state->id == state->id
4102 && sub_equal(&thread->subs.norm, &subs->norm)
4103#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004104 && (!nfa_has_zsubexpr
4105 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004106#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004107 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004108 return TRUE;
4109 }
4110 return FALSE;
4111}
4112
4113/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004114 * Return TRUE if "one" and "two" are equal. That includes when both are not
4115 * set.
4116 */
4117 static int
4118pim_equal(one, two)
4119 nfa_pim_T *one;
4120 nfa_pim_T *two;
4121{
4122 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4123 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4124
4125 if (one_unused)
4126 /* one is unused: equal when two is also unused */
4127 return two_unused;
4128 if (two_unused)
4129 /* one is used and two is not: not equal */
4130 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004131 /* compare the state id */
4132 if (one->state->id != two->state->id)
4133 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004134 /* compare the position */
4135 if (REG_MULTI)
4136 return one->end.pos.lnum == two->end.pos.lnum
4137 && one->end.pos.col == two->end.pos.col;
4138 return one->end.ptr == two->end.ptr;
4139}
4140
4141/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004142 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4143 */
4144 static int
4145match_follows(startstate, depth)
4146 nfa_state_T *startstate;
4147 int depth;
4148{
4149 nfa_state_T *state = startstate;
4150
4151 /* avoid too much recursion */
4152 if (depth > 10)
4153 return FALSE;
4154
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004155 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004156 {
4157 switch (state->c)
4158 {
4159 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004160 case NFA_MCLOSE:
4161 case NFA_END_INVISIBLE:
4162 case NFA_END_INVISIBLE_NEG:
4163 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004164 return TRUE;
4165
4166 case NFA_SPLIT:
4167 return match_follows(state->out, depth + 1)
4168 || match_follows(state->out1, depth + 1);
4169
4170 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004171 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004172 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004173 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004174 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004175 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004176 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004177 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004178 case NFA_COMPOSING:
4179 /* skip ahead to next state */
4180 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004181 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004182
4183 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004184 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004185 case NFA_IDENT:
4186 case NFA_SIDENT:
4187 case NFA_KWORD:
4188 case NFA_SKWORD:
4189 case NFA_FNAME:
4190 case NFA_SFNAME:
4191 case NFA_PRINT:
4192 case NFA_SPRINT:
4193 case NFA_WHITE:
4194 case NFA_NWHITE:
4195 case NFA_DIGIT:
4196 case NFA_NDIGIT:
4197 case NFA_HEX:
4198 case NFA_NHEX:
4199 case NFA_OCTAL:
4200 case NFA_NOCTAL:
4201 case NFA_WORD:
4202 case NFA_NWORD:
4203 case NFA_HEAD:
4204 case NFA_NHEAD:
4205 case NFA_ALPHA:
4206 case NFA_NALPHA:
4207 case NFA_LOWER:
4208 case NFA_NLOWER:
4209 case NFA_UPPER:
4210 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004211 case NFA_LOWER_IC:
4212 case NFA_NLOWER_IC:
4213 case NFA_UPPER_IC:
4214 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004215 case NFA_START_COLL:
4216 case NFA_START_NEG_COLL:
4217 case NFA_NEWL:
4218 /* state will advance input */
4219 return FALSE;
4220
4221 default:
4222 if (state->c > 0)
4223 /* state will advance input */
4224 return FALSE;
4225
4226 /* Others: zero-width or possibly zero-width, might still find
4227 * a match at the same position, keep looking. */
4228 break;
4229 }
4230 state = state->out;
4231 }
4232 return FALSE;
4233}
4234
4235
4236/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004237 * Return TRUE if "state" is already in list "l".
4238 */
4239 static int
4240state_in_list(l, state, subs)
4241 nfa_list_T *l; /* runtime state list */
4242 nfa_state_T *state; /* state to update */
4243 regsubs_T *subs; /* pointers to subexpressions */
4244{
4245 if (state->lastlist[nfa_ll_index] == l->id)
4246 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004247 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004248 return TRUE;
4249 }
4250 return FALSE;
4251}
4252
Bram Moolenaard05bf562013-06-30 23:24:08 +02004253/*
4254 * Add "state" and possibly what follows to state list ".".
4255 * Returns "subs_arg", possibly copied into temp_subs.
4256 */
4257
4258 static regsubs_T *
4259addstate(l, state, subs_arg, pim, off)
4260 nfa_list_T *l; /* runtime state list */
4261 nfa_state_T *state; /* state to update */
4262 regsubs_T *subs_arg; /* pointers to subexpressions */
4263 nfa_pim_T *pim; /* postponed look-behind match */
4264 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004265{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004266 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004267 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004268 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004269 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004270 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004271 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004272 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004273 regsubs_T *subs = subs_arg;
4274 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004275#ifdef ENABLE_LOG
4276 int did_print = FALSE;
4277#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004279 switch (state->c)
4280 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004281 case NFA_NCLOSE:
4282 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004283 case NFA_MCLOSE1:
4284 case NFA_MCLOSE2:
4285 case NFA_MCLOSE3:
4286 case NFA_MCLOSE4:
4287 case NFA_MCLOSE5:
4288 case NFA_MCLOSE6:
4289 case NFA_MCLOSE7:
4290 case NFA_MCLOSE8:
4291 case NFA_MCLOSE9:
4292#ifdef FEAT_SYN_HL
4293 case NFA_ZCLOSE:
4294 case NFA_ZCLOSE1:
4295 case NFA_ZCLOSE2:
4296 case NFA_ZCLOSE3:
4297 case NFA_ZCLOSE4:
4298 case NFA_ZCLOSE5:
4299 case NFA_ZCLOSE6:
4300 case NFA_ZCLOSE7:
4301 case NFA_ZCLOSE8:
4302 case NFA_ZCLOSE9:
4303#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004304 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004305 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004306 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004307 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004308 /* These nodes are not added themselves but their "out" and/or
4309 * "out1" may be added below. */
4310 break;
4311
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004312 case NFA_BOL:
4313 case NFA_BOF:
4314 /* "^" won't match past end-of-line, don't bother trying.
4315 * Except when at the end of the line, or when we are going to the
4316 * next line for a look-behind match. */
4317 if (reginput > regline
4318 && *reginput != NUL
4319 && (nfa_endp == NULL
4320 || !REG_MULTI
4321 || reglnum == nfa_endp->se_u.pos.lnum))
4322 goto skip_add;
4323 /* FALLTHROUGH */
4324
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004325 case NFA_MOPEN1:
4326 case NFA_MOPEN2:
4327 case NFA_MOPEN3:
4328 case NFA_MOPEN4:
4329 case NFA_MOPEN5:
4330 case NFA_MOPEN6:
4331 case NFA_MOPEN7:
4332 case NFA_MOPEN8:
4333 case NFA_MOPEN9:
4334#ifdef FEAT_SYN_HL
4335 case NFA_ZOPEN:
4336 case NFA_ZOPEN1:
4337 case NFA_ZOPEN2:
4338 case NFA_ZOPEN3:
4339 case NFA_ZOPEN4:
4340 case NFA_ZOPEN5:
4341 case NFA_ZOPEN6:
4342 case NFA_ZOPEN7:
4343 case NFA_ZOPEN8:
4344 case NFA_ZOPEN9:
4345#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004346 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004347 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004348 /* These nodes need to be added so that we can bail out when it
4349 * was added to this list before at the same position to avoid an
4350 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004352 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004353 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004355 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004356 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004357 * when there is a PIM. For NFA_MATCH check the position,
4358 * lower position is preferred. */
4359 if (!nfa_has_backref && pim == NULL && !l->has_pim
4360 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004361 {
4362skip_add:
4363#ifdef ENABLE_LOG
4364 nfa_set_code(state->c);
4365 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4366 abs(state->id), l->id, state->c, code);
4367#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004368 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004369 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004370
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004371 /* Do not add the state again when it exists with the same
4372 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004373 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004374 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004375 }
4376
Bram Moolenaara0169122013-06-26 18:16:58 +02004377 /* When there are backreferences or PIMs the number of states may
4378 * be (a lot) bigger than anticipated. */
4379 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004380 {
4381 int newlen = l->len * 3 / 2 + 50;
4382
Bram Moolenaard05bf562013-06-30 23:24:08 +02004383 if (subs != &temp_subs)
4384 {
4385 /* "subs" may point into the current array, need to make a
4386 * copy before it becomes invalid. */
4387 copy_sub(&temp_subs.norm, &subs->norm);
4388#ifdef FEAT_SYN_HL
4389 if (nfa_has_zsubexpr)
4390 copy_sub(&temp_subs.synt, &subs->synt);
4391#endif
4392 subs = &temp_subs;
4393 }
4394
Bram Moolenaar428e9872013-05-30 17:05:39 +02004395 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4396 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004397 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004398
4399 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004400 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004401 thread = &l->t[l->n++];
4402 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004403 if (pim == NULL)
4404 thread->pim.result = NFA_PIM_UNUSED;
4405 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004406 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004407 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004408 l->has_pim = TRUE;
4409 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004410 copy_sub(&thread->subs.norm, &subs->norm);
4411#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004412 if (nfa_has_zsubexpr)
4413 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004414#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004415#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004416 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004417 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004418#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004419 }
4420
4421#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004422 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004423 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004424#endif
4425 switch (state->c)
4426 {
4427 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004428 break;
4429
4430 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004431 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004432 subs = addstate(l, state->out, subs, pim, off);
4433 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004434 break;
4435
Bram Moolenaar699c1202013-09-25 16:41:54 +02004436 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004437 case NFA_NOPEN:
4438 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004439 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004440 break;
4441
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004442 case NFA_MOPEN:
4443 case NFA_MOPEN1:
4444 case NFA_MOPEN2:
4445 case NFA_MOPEN3:
4446 case NFA_MOPEN4:
4447 case NFA_MOPEN5:
4448 case NFA_MOPEN6:
4449 case NFA_MOPEN7:
4450 case NFA_MOPEN8:
4451 case NFA_MOPEN9:
4452#ifdef FEAT_SYN_HL
4453 case NFA_ZOPEN:
4454 case NFA_ZOPEN1:
4455 case NFA_ZOPEN2:
4456 case NFA_ZOPEN3:
4457 case NFA_ZOPEN4:
4458 case NFA_ZOPEN5:
4459 case NFA_ZOPEN6:
4460 case NFA_ZOPEN7:
4461 case NFA_ZOPEN8:
4462 case NFA_ZOPEN9:
4463#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004464 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004465 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004466 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004467 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004468 sub = &subs->norm;
4469 }
4470#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004471 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004472 {
4473 subidx = state->c - NFA_ZOPEN;
4474 sub = &subs->synt;
4475 }
4476#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004477 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004478 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004479 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004480 sub = &subs->norm;
4481 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004482
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004483 /* avoid compiler warnings */
4484 save_ptr = NULL;
4485 save_lpos.lnum = 0;
4486 save_lpos.col = 0;
4487
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004488 /* Set the position (with "off" added) in the subexpression. Save
4489 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004490 if (REG_MULTI)
4491 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004492 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004493 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004494 save_lpos.lnum = sub->list.multi[subidx].start_lnum;
4495 save_lpos.col = sub->list.multi[subidx].start_col;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004496 save_in_use = -1;
4497 }
4498 else
4499 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004500 save_in_use = sub->in_use;
4501 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004502 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004503 sub->list.multi[i].start_lnum = -1;
4504 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004505 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004506 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004507 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004508 if (off == -1)
4509 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004510 sub->list.multi[subidx].start_lnum = reglnum + 1;
4511 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004512 }
4513 else
4514 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004515 sub->list.multi[subidx].start_lnum = reglnum;
4516 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004517 (colnr_T)(reginput - regline + off);
4518 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 }
4520 else
4521 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004522 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004523 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004524 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004525 save_in_use = -1;
4526 }
4527 else
4528 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004529 save_in_use = sub->in_use;
4530 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004531 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004532 sub->list.line[i].start = NULL;
4533 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004534 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004535 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004536 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004537 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004538 }
4539
Bram Moolenaard05bf562013-06-30 23:24:08 +02004540 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004541 /* "subs" may have changed, need to set "sub" again */
4542#ifdef FEAT_SYN_HL
4543 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4544 sub = &subs->synt;
4545 else
4546#endif
4547 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004549 if (save_in_use == -1)
4550 {
4551 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004552 {
4553 sub->list.multi[subidx].start_lnum = save_lpos.lnum;
4554 sub->list.multi[subidx].start_col = save_lpos.col;
4555 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004556 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004557 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004558 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004560 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561 break;
4562
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004563 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004564 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004565 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004566 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004567 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004568 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004569 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004570 break;
4571 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004572 case NFA_MCLOSE1:
4573 case NFA_MCLOSE2:
4574 case NFA_MCLOSE3:
4575 case NFA_MCLOSE4:
4576 case NFA_MCLOSE5:
4577 case NFA_MCLOSE6:
4578 case NFA_MCLOSE7:
4579 case NFA_MCLOSE8:
4580 case NFA_MCLOSE9:
4581#ifdef FEAT_SYN_HL
4582 case NFA_ZCLOSE:
4583 case NFA_ZCLOSE1:
4584 case NFA_ZCLOSE2:
4585 case NFA_ZCLOSE3:
4586 case NFA_ZCLOSE4:
4587 case NFA_ZCLOSE5:
4588 case NFA_ZCLOSE6:
4589 case NFA_ZCLOSE7:
4590 case NFA_ZCLOSE8:
4591 case NFA_ZCLOSE9:
4592#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004593 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004594 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004595 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004596 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004597 sub = &subs->norm;
4598 }
4599#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004600 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004601 {
4602 subidx = state->c - NFA_ZCLOSE;
4603 sub = &subs->synt;
4604 }
4605#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004606 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004607 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004608 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004609 sub = &subs->norm;
4610 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004612 /* We don't fill in gaps here, there must have been an MOPEN that
4613 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004614 save_in_use = sub->in_use;
4615 if (sub->in_use <= subidx)
4616 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004617 if (REG_MULTI)
4618 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004619 save_lpos.lnum = sub->list.multi[subidx].end_lnum;
4620 save_lpos.col = sub->list.multi[subidx].end_col;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004621 if (off == -1)
4622 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004623 sub->list.multi[subidx].end_lnum = reglnum + 1;
4624 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004625 }
4626 else
4627 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004628 sub->list.multi[subidx].end_lnum = reglnum;
4629 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004630 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004631 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004632 /* avoid compiler warnings */
4633 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004634 }
4635 else
4636 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004637 save_ptr = sub->list.line[subidx].end;
4638 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004639 /* avoid compiler warnings */
4640 save_lpos.lnum = 0;
4641 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004642 }
4643
Bram Moolenaard05bf562013-06-30 23:24:08 +02004644 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004645 /* "subs" may have changed, need to set "sub" again */
4646#ifdef FEAT_SYN_HL
4647 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4648 sub = &subs->synt;
4649 else
4650#endif
4651 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004652
4653 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004654 {
4655 sub->list.multi[subidx].end_lnum = save_lpos.lnum;
4656 sub->list.multi[subidx].end_col = save_lpos.col;
4657 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004658 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004659 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004660 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004661 break;
4662 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004663 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004664}
4665
4666/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004667 * Like addstate(), but the new state(s) are put at position "*ip".
4668 * Used for zero-width matches, next state to use is the added one.
4669 * This makes sure the order of states to be tried does not change, which
4670 * matters for alternatives.
4671 */
4672 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004673addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004674 nfa_list_T *l; /* runtime state list */
4675 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004676 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004677 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004678 int *ip;
4679{
4680 int tlen = l->n;
4681 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004682 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004683
4684 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004685 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004686
Bram Moolenaar4b417062013-05-25 20:19:50 +02004687 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004688 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004689 return;
4690
4691 /* re-order to put the new state at the current position */
4692 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004693 if (count == 0)
4694 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004695 if (count == 1)
4696 {
4697 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004698 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004699 }
4700 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004701 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004702 if (l->n + count - 1 >= l->len)
4703 {
4704 /* not enough space to move the new states, reallocate the list
4705 * and move the states to the right position */
4706 nfa_thread_T *newl;
4707
4708 l->len = l->len * 3 / 2 + 50;
4709 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4710 if (newl == NULL)
4711 return;
4712 mch_memmove(&(newl[0]),
4713 &(l->t[0]),
4714 sizeof(nfa_thread_T) * listidx);
4715 mch_memmove(&(newl[listidx]),
4716 &(l->t[l->n - count]),
4717 sizeof(nfa_thread_T) * count);
4718 mch_memmove(&(newl[listidx + count]),
4719 &(l->t[listidx + 1]),
4720 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4721 vim_free(l->t);
4722 l->t = newl;
4723 }
4724 else
4725 {
4726 /* make space for new states, then move them from the
4727 * end to the current position */
4728 mch_memmove(&(l->t[listidx + count]),
4729 &(l->t[listidx + 1]),
4730 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4731 mch_memmove(&(l->t[listidx]),
4732 &(l->t[l->n - 1]),
4733 sizeof(nfa_thread_T) * count);
4734 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004735 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004736 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004737 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004738}
4739
4740/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004741 * Check character class "class" against current character c.
4742 */
4743 static int
4744check_char_class(class, c)
4745 int class;
4746 int c;
4747{
4748 switch (class)
4749 {
4750 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004751 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752 return OK;
4753 break;
4754 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004755 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004756 return OK;
4757 break;
4758 case NFA_CLASS_BLANK:
4759 if (c == ' ' || c == '\t')
4760 return OK;
4761 break;
4762 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004763 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004764 return OK;
4765 break;
4766 case NFA_CLASS_DIGIT:
4767 if (VIM_ISDIGIT(c))
4768 return OK;
4769 break;
4770 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004771 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004772 return OK;
4773 break;
4774 case NFA_CLASS_LOWER:
4775 if (MB_ISLOWER(c))
4776 return OK;
4777 break;
4778 case NFA_CLASS_PRINT:
4779 if (vim_isprintc(c))
4780 return OK;
4781 break;
4782 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004783 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004784 return OK;
4785 break;
4786 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004787 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004788 return OK;
4789 break;
4790 case NFA_CLASS_UPPER:
4791 if (MB_ISUPPER(c))
4792 return OK;
4793 break;
4794 case NFA_CLASS_XDIGIT:
4795 if (vim_isxdigit(c))
4796 return OK;
4797 break;
4798 case NFA_CLASS_TAB:
4799 if (c == '\t')
4800 return OK;
4801 break;
4802 case NFA_CLASS_RETURN:
4803 if (c == '\r')
4804 return OK;
4805 break;
4806 case NFA_CLASS_BACKSPACE:
4807 if (c == '\b')
4808 return OK;
4809 break;
4810 case NFA_CLASS_ESCAPE:
4811 if (c == '\033')
4812 return OK;
4813 break;
4814
4815 default:
4816 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004817 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004818 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004819 }
4820 return FAIL;
4821}
4822
Bram Moolenaar5714b802013-05-28 22:03:20 +02004823/*
4824 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004825 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004826 */
4827 static int
4828match_backref(sub, subidx, bytelen)
4829 regsub_T *sub; /* pointers to subexpressions */
4830 int subidx;
4831 int *bytelen; /* out: length of match in bytes */
4832{
4833 int len;
4834
4835 if (sub->in_use <= subidx)
4836 {
4837retempty:
4838 /* backref was not set, match an empty string */
4839 *bytelen = 0;
4840 return TRUE;
4841 }
4842
4843 if (REG_MULTI)
4844 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004845 if (sub->list.multi[subidx].start_lnum < 0
4846 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004847 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004848 if (sub->list.multi[subidx].start_lnum == reglnum
4849 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004850 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004851 len = sub->list.multi[subidx].end_col
4852 - sub->list.multi[subidx].start_col;
4853 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004854 reginput, &len) == 0)
4855 {
4856 *bytelen = len;
4857 return TRUE;
4858 }
4859 }
4860 else
4861 {
4862 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004863 sub->list.multi[subidx].start_lnum,
4864 sub->list.multi[subidx].start_col,
4865 sub->list.multi[subidx].end_lnum,
4866 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004867 bytelen) == RA_MATCH)
4868 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004869 }
4870 }
4871 else
4872 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004873 if (sub->list.line[subidx].start == NULL
4874 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004875 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004876 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4877 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004878 {
4879 *bytelen = len;
4880 return TRUE;
4881 }
4882 }
4883 return FALSE;
4884}
4885
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004886#ifdef FEAT_SYN_HL
4887
4888static int match_zref __ARGS((int subidx, int *bytelen));
4889
4890/*
4891 * Check for a match with \z subexpression "subidx".
4892 * Return TRUE if it matches.
4893 */
4894 static int
4895match_zref(subidx, bytelen)
4896 int subidx;
4897 int *bytelen; /* out: length of match in bytes */
4898{
4899 int len;
4900
4901 cleanup_zsubexpr();
4902 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4903 {
4904 /* backref was not set, match an empty string */
4905 *bytelen = 0;
4906 return TRUE;
4907 }
4908
4909 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4910 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4911 {
4912 *bytelen = len;
4913 return TRUE;
4914 }
4915 return FALSE;
4916}
4917#endif
4918
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004919/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004920 * Save list IDs for all NFA states of "prog" into "list".
4921 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004922 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004923 */
4924 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004925nfa_save_listids(prog, list)
4926 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004927 int *list;
4928{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004929 int i;
4930 nfa_state_T *p;
4931
4932 /* Order in the list is reverse, it's a bit faster that way. */
4933 p = &prog->state[0];
4934 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004935 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004936 list[i] = p->lastlist[1];
4937 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004938 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004939 }
4940}
4941
4942/*
4943 * Restore list IDs from "list" to all NFA states.
4944 */
4945 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004946nfa_restore_listids(prog, list)
4947 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004948 int *list;
4949{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004950 int i;
4951 nfa_state_T *p;
4952
4953 p = &prog->state[0];
4954 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004955 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004956 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004957 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004958 }
4959}
4960
Bram Moolenaar423532e2013-05-29 21:14:42 +02004961 static int
4962nfa_re_num_cmp(val, op, pos)
4963 long_u val;
4964 int op;
4965 long_u pos;
4966{
4967 if (op == 1) return pos > val;
4968 if (op == 2) return pos < val;
4969 return val == pos;
4970}
4971
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004972static 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 +02004973static 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 +02004974
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004975/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004976 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004977 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4978 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004979 */
4980 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004981recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004982 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004983 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004984 nfa_regprog_T *prog;
4985 regsubs_T *submatch;
4986 regsubs_T *m;
4987 int **listids;
4988{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004989 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004990 int save_reglnum = reglnum;
4991 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004992 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004993 save_se_T *save_nfa_endp = nfa_endp;
4994 save_se_T endpos;
4995 save_se_T *endposp = NULL;
4996 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004997 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004998
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004999 if (pim != NULL)
5000 {
5001 /* start at the position where the postponed match was */
5002 if (REG_MULTI)
5003 reginput = regline + pim->end.pos.col;
5004 else
5005 reginput = pim->end.ptr;
5006 }
5007
Bram Moolenaardecd9542013-06-07 16:31:50 +02005008 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005009 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5010 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5011 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005012 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005013 /* The recursive match must end at the current position. When "pim" is
5014 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005015 endposp = &endpos;
5016 if (REG_MULTI)
5017 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005018 if (pim == NULL)
5019 {
5020 endpos.se_u.pos.col = (int)(reginput - regline);
5021 endpos.se_u.pos.lnum = reglnum;
5022 }
5023 else
5024 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005025 }
5026 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005027 {
5028 if (pim == NULL)
5029 endpos.se_u.ptr = reginput;
5030 else
5031 endpos.se_u.ptr = pim->end.ptr;
5032 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005033
5034 /* Go back the specified number of bytes, or as far as the
5035 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005036 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005037 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005038 if (state->val <= 0)
5039 {
5040 if (REG_MULTI)
5041 {
5042 regline = reg_getline(--reglnum);
5043 if (regline == NULL)
5044 /* can't go before the first line */
5045 regline = reg_getline(++reglnum);
5046 }
5047 reginput = regline;
5048 }
5049 else
5050 {
5051 if (REG_MULTI && (int)(reginput - regline) < state->val)
5052 {
5053 /* Not enough bytes in this line, go to end of
5054 * previous line. */
5055 regline = reg_getline(--reglnum);
5056 if (regline == NULL)
5057 {
5058 /* can't go before the first line */
5059 regline = reg_getline(++reglnum);
5060 reginput = regline;
5061 }
5062 else
5063 reginput = regline + STRLEN(regline);
5064 }
5065 if ((int)(reginput - regline) >= state->val)
5066 {
5067 reginput -= state->val;
5068#ifdef FEAT_MBYTE
5069 if (has_mbyte)
5070 reginput -= mb_head_off(regline, reginput);
5071#endif
5072 }
5073 else
5074 reginput = regline;
5075 }
5076 }
5077
Bram Moolenaarf46da702013-06-02 22:37:42 +02005078#ifdef ENABLE_LOG
5079 if (log_fd != stderr)
5080 fclose(log_fd);
5081 log_fd = NULL;
5082#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005083 /* Have to clear the lastlist field of the NFA nodes, so that
5084 * nfa_regmatch() and addstate() can run properly after recursion. */
5085 if (nfa_ll_index == 1)
5086 {
5087 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5088 * values and clear them. */
5089 if (*listids == NULL)
5090 {
5091 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5092 if (*listids == NULL)
5093 {
5094 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5095 return 0;
5096 }
5097 }
5098 nfa_save_listids(prog, *listids);
5099 need_restore = TRUE;
5100 /* any value of nfa_listid will do */
5101 }
5102 else
5103 {
5104 /* First recursive nfa_regmatch() call, switch to the second lastlist
5105 * entry. Make sure nfa_listid is different from a previous recursive
5106 * call, because some states may still have this ID. */
5107 ++nfa_ll_index;
5108 if (nfa_listid <= nfa_alt_listid)
5109 nfa_listid = nfa_alt_listid;
5110 }
5111
5112 /* Call nfa_regmatch() to check if the current concat matches at this
5113 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005114 nfa_endp = endposp;
5115 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005116
5117 if (need_restore)
5118 nfa_restore_listids(prog, *listids);
5119 else
5120 {
5121 --nfa_ll_index;
5122 nfa_alt_listid = nfa_listid;
5123 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005124
5125 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005126 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005127 if (REG_MULTI)
5128 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005129 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005130 nfa_match = save_nfa_match;
5131 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005132 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005133
5134#ifdef ENABLE_LOG
5135 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5136 if (log_fd != NULL)
5137 {
5138 fprintf(log_fd, "****************************\n");
5139 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5140 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5141 fprintf(log_fd, "****************************\n");
5142 }
5143 else
5144 {
5145 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5146 log_fd = stderr;
5147 }
5148#endif
5149
5150 return result;
5151}
5152
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005153static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005154static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005155
5156/*
5157 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005158 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005159 * NFA_ANY: 1
5160 * specific character: 99
5161 */
5162 static int
5163failure_chance(state, depth)
5164 nfa_state_T *state;
5165 int depth;
5166{
5167 int c = state->c;
5168 int l, r;
5169
5170 /* detect looping */
5171 if (depth > 4)
5172 return 1;
5173
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005174 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005175 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005176 case NFA_SPLIT:
5177 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5178 /* avoid recursive stuff */
5179 return 1;
5180 /* two alternatives, use the lowest failure chance */
5181 l = failure_chance(state->out, depth + 1);
5182 r = failure_chance(state->out1, depth + 1);
5183 return l < r ? l : r;
5184
5185 case NFA_ANY:
5186 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005187 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005188
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005189 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005190 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005191 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005192 /* empty match works always */
5193 return 0;
5194
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005195 case NFA_START_INVISIBLE:
5196 case NFA_START_INVISIBLE_FIRST:
5197 case NFA_START_INVISIBLE_NEG:
5198 case NFA_START_INVISIBLE_NEG_FIRST:
5199 case NFA_START_INVISIBLE_BEFORE:
5200 case NFA_START_INVISIBLE_BEFORE_FIRST:
5201 case NFA_START_INVISIBLE_BEFORE_NEG:
5202 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5203 case NFA_START_PATTERN:
5204 /* recursive regmatch is expensive, use low failure chance */
5205 return 5;
5206
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005207 case NFA_BOL:
5208 case NFA_EOL:
5209 case NFA_BOF:
5210 case NFA_EOF:
5211 case NFA_NEWL:
5212 return 99;
5213
5214 case NFA_BOW:
5215 case NFA_EOW:
5216 return 90;
5217
5218 case NFA_MOPEN:
5219 case NFA_MOPEN1:
5220 case NFA_MOPEN2:
5221 case NFA_MOPEN3:
5222 case NFA_MOPEN4:
5223 case NFA_MOPEN5:
5224 case NFA_MOPEN6:
5225 case NFA_MOPEN7:
5226 case NFA_MOPEN8:
5227 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005228#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005229 case NFA_ZOPEN:
5230 case NFA_ZOPEN1:
5231 case NFA_ZOPEN2:
5232 case NFA_ZOPEN3:
5233 case NFA_ZOPEN4:
5234 case NFA_ZOPEN5:
5235 case NFA_ZOPEN6:
5236 case NFA_ZOPEN7:
5237 case NFA_ZOPEN8:
5238 case NFA_ZOPEN9:
5239 case NFA_ZCLOSE:
5240 case NFA_ZCLOSE1:
5241 case NFA_ZCLOSE2:
5242 case NFA_ZCLOSE3:
5243 case NFA_ZCLOSE4:
5244 case NFA_ZCLOSE5:
5245 case NFA_ZCLOSE6:
5246 case NFA_ZCLOSE7:
5247 case NFA_ZCLOSE8:
5248 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005249#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005250 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005251 case NFA_MCLOSE1:
5252 case NFA_MCLOSE2:
5253 case NFA_MCLOSE3:
5254 case NFA_MCLOSE4:
5255 case NFA_MCLOSE5:
5256 case NFA_MCLOSE6:
5257 case NFA_MCLOSE7:
5258 case NFA_MCLOSE8:
5259 case NFA_MCLOSE9:
5260 case NFA_NCLOSE:
5261 return failure_chance(state->out, depth + 1);
5262
5263 case NFA_BACKREF1:
5264 case NFA_BACKREF2:
5265 case NFA_BACKREF3:
5266 case NFA_BACKREF4:
5267 case NFA_BACKREF5:
5268 case NFA_BACKREF6:
5269 case NFA_BACKREF7:
5270 case NFA_BACKREF8:
5271 case NFA_BACKREF9:
5272#ifdef FEAT_SYN_HL
5273 case NFA_ZREF1:
5274 case NFA_ZREF2:
5275 case NFA_ZREF3:
5276 case NFA_ZREF4:
5277 case NFA_ZREF5:
5278 case NFA_ZREF6:
5279 case NFA_ZREF7:
5280 case NFA_ZREF8:
5281 case NFA_ZREF9:
5282#endif
5283 /* backreferences don't match in many places */
5284 return 94;
5285
5286 case NFA_LNUM_GT:
5287 case NFA_LNUM_LT:
5288 case NFA_COL_GT:
5289 case NFA_COL_LT:
5290 case NFA_VCOL_GT:
5291 case NFA_VCOL_LT:
5292 case NFA_MARK_GT:
5293 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005294 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005295 /* before/after positions don't match very often */
5296 return 85;
5297
5298 case NFA_LNUM:
5299 return 90;
5300
5301 case NFA_CURSOR:
5302 case NFA_COL:
5303 case NFA_VCOL:
5304 case NFA_MARK:
5305 /* specific positions rarely match */
5306 return 98;
5307
5308 case NFA_COMPOSING:
5309 return 95;
5310
5311 default:
5312 if (c > 0)
5313 /* character match fails often */
5314 return 95;
5315 }
5316
5317 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005318 return 50;
5319}
5320
Bram Moolenaarf46da702013-06-02 22:37:42 +02005321/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005322 * Skip until the char "c" we know a match must start with.
5323 */
5324 static int
5325skip_to_start(c, colp)
5326 int c;
5327 colnr_T *colp;
5328{
5329 char_u *s;
5330
5331 /* Used often, do some work to avoid call overhead. */
5332 if (!ireg_ic
5333#ifdef FEAT_MBYTE
5334 && !has_mbyte
5335#endif
5336 )
5337 s = vim_strbyte(regline + *colp, c);
5338 else
5339 s = cstrchr(regline + *colp, c);
5340 if (s == NULL)
5341 return FAIL;
5342 *colp = (int)(s - regline);
5343 return OK;
5344}
5345
5346/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005347 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005348 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005349 * Returns zero for no match, 1 for a match.
5350 */
5351 static long
5352find_match_text(startcol, regstart, match_text)
5353 colnr_T startcol;
5354 int regstart;
5355 char_u *match_text;
5356{
5357 colnr_T col = startcol;
5358 int c1, c2;
5359 int len1, len2;
5360 int match;
5361
5362 for (;;)
5363 {
5364 match = TRUE;
5365 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5366 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5367 {
5368 c1 = PTR2CHAR(match_text + len1);
5369 c2 = PTR2CHAR(regline + col + len2);
5370 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5371 {
5372 match = FALSE;
5373 break;
5374 }
5375 len2 += MB_CHAR2LEN(c2);
5376 }
5377 if (match
5378#ifdef FEAT_MBYTE
5379 /* check that no composing char follows */
5380 && !(enc_utf8
5381 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5382#endif
5383 )
5384 {
5385 cleanup_subexpr();
5386 if (REG_MULTI)
5387 {
5388 reg_startpos[0].lnum = reglnum;
5389 reg_startpos[0].col = col;
5390 reg_endpos[0].lnum = reglnum;
5391 reg_endpos[0].col = col + len2;
5392 }
5393 else
5394 {
5395 reg_startp[0] = regline + col;
5396 reg_endp[0] = regline + col + len2;
5397 }
5398 return 1L;
5399 }
5400
5401 /* Try finding regstart after the current match. */
5402 col += MB_CHAR2LEN(regstart); /* skip regstart */
5403 if (skip_to_start(regstart, &col) == FAIL)
5404 break;
5405 }
5406 return 0L;
5407}
5408
5409/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 * Main matching routine.
5411 *
5412 * Run NFA to determine whether it matches reginput.
5413 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005414 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005415 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005416 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005417 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005418 * Note: Caller must ensure that: start != NULL.
5419 */
5420 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005421nfa_regmatch(prog, start, submatch, m)
5422 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005423 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005424 regsubs_T *submatch;
5425 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005426{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005427 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005428 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005429 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005430 int go_to_nextline = FALSE;
5431 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005432 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005433 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005434 nfa_list_T *thislist;
5435 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005436 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005437 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005438 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005439 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005440 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005441 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005442#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005443 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444
5445 if (debug == NULL)
5446 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005447 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005448 return FALSE;
5449 }
5450#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005451 /* Some patterns may take a long time to match, especially when using
5452 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5453 fast_breakcheck();
5454 if (got_int)
5455 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005456#ifdef FEAT_RELTIME
5457 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5458 return FALSE;
5459#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005460
Bram Moolenaar963fee22013-05-26 21:47:28 +02005461 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005462
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005463 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005464 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005465
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005466 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005467 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005468 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005469 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005470 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005471 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005472
5473#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005474 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005475 if (log_fd != NULL)
5476 {
5477 fprintf(log_fd, "**********************************\n");
5478 nfa_set_code(start->c);
5479 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5480 abs(start->id), code);
5481 fprintf(log_fd, "**********************************\n");
5482 }
5483 else
5484 {
5485 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5486 log_fd = stderr;
5487 }
5488#endif
5489
5490 thislist = &list[0];
5491 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005492 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005493 nextlist = &list[1];
5494 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005495 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005496#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005497 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005498#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005499 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005500
5501 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5502 * it's the first MOPEN. */
5503 if (toplevel)
5504 {
5505 if (REG_MULTI)
5506 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005507 m->norm.list.multi[0].start_lnum = reglnum;
5508 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005509 }
5510 else
5511 m->norm.list.line[0].start = reginput;
5512 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005513 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005514 }
5515 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005516 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005517
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005518#define ADD_STATE_IF_MATCH(state) \
5519 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005520 add_state = state->out; \
5521 add_off = clen; \
5522 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005523
5524 /*
5525 * Run for each character.
5526 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005527 for (;;)
5528 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005529 int curc;
5530 int clen;
5531
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005532#ifdef FEAT_MBYTE
5533 if (has_mbyte)
5534 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005535 curc = (*mb_ptr2char)(reginput);
5536 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005537 }
5538 else
5539#endif
5540 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005541 curc = *reginput;
5542 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005543 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005544 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005545 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005546 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005547 go_to_nextline = FALSE;
5548 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005549
5550 /* swap lists */
5551 thislist = &list[flag];
5552 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005553 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005554 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005555 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005556 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5557 {
5558 /* too many states, retry with old engine */
5559 nfa_match = NFA_TOO_EXPENSIVE;
5560 goto theend;
5561 }
5562
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005563 thislist->id = nfa_listid;
5564 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005565
5566#ifdef ENABLE_LOG
5567 fprintf(log_fd, "------------------------------------------\n");
5568 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005569 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005570 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005571 {
5572 int i;
5573
5574 for (i = 0; i < thislist->n; i++)
5575 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5576 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005577 fprintf(log_fd, "\n");
5578#endif
5579
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005580#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005581 fprintf(debug, "\n-------------------\n");
5582#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005583 /*
5584 * If the state lists are empty we can stop.
5585 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005586 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005587 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005588
5589 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005590 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005592 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005593
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005594#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005595 nfa_set_code(t->state->c);
5596 fprintf(debug, "%s, ", code);
5597#endif
5598#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005599 {
5600 int col;
5601
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005602 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005603 col = -1;
5604 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005605 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005606 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005607 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005608 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005609 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5610 abs(t->state->id), (int)t->state->c, code, col,
5611 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005612 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005613#endif
5614
5615 /*
5616 * Handle the possible codes of the current state.
5617 * The most important is NFA_MATCH.
5618 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005619 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005620 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005621 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005622 switch (t->state->c)
5623 {
5624 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005625 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005626#ifdef FEAT_MBYTE
5627 /* If the match ends before a composing characters and
5628 * ireg_icombine is not set, that is not really a match. */
5629 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5630 break;
5631#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005632 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005633 copy_sub(&submatch->norm, &t->subs.norm);
5634#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005635 if (nfa_has_zsubexpr)
5636 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005637#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005638#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005639 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005640#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005641 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005642 * states at this position. When the list of states is going
5643 * to be empty quit without advancing, so that "reginput" is
5644 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005645 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005646 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005647 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005648 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005649
5650 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005651 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005652 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005653 /*
5654 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005655 * NFA_START_INVISIBLE_BEFORE node.
5656 * They surround a zero-width group, used with "\@=", "\&",
5657 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005658 * If we got here, it means that the current "invisible" group
5659 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005660 * nfa_regmatch(). For a look-behind match only when it ends
5661 * in the position in "nfa_endp".
5662 * Submatches are stored in *m, and used in the parent call.
5663 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005664#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005665 if (nfa_endp != NULL)
5666 {
5667 if (REG_MULTI)
5668 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5669 (int)reglnum,
5670 (int)nfa_endp->se_u.pos.lnum,
5671 (int)(reginput - regline),
5672 nfa_endp->se_u.pos.col);
5673 else
5674 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5675 (int)(reginput - regline),
5676 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005677 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005678#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005679 /* If "nfa_endp" is set it's only a match if it ends at
5680 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005681 if (nfa_endp != NULL && (REG_MULTI
5682 ? (reglnum != nfa_endp->se_u.pos.lnum
5683 || (int)(reginput - regline)
5684 != nfa_endp->se_u.pos.col)
5685 : reginput != nfa_endp->se_u.ptr))
5686 break;
5687
5688 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005689 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005690 {
5691 copy_sub(&m->norm, &t->subs.norm);
5692#ifdef FEAT_SYN_HL
5693 if (nfa_has_zsubexpr)
5694 copy_sub(&m->synt, &t->subs.synt);
5695#endif
5696 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005697#ifdef ENABLE_LOG
5698 fprintf(log_fd, "Match found:\n");
5699 log_subsexpr(m);
5700#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005701 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005702 /* See comment above at "goto nextchar". */
5703 if (nextlist->n == 0)
5704 clen = 0;
5705 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005706
5707 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005708 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005709 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005710 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005711 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005712 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005713 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005714 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005715 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005716#ifdef ENABLE_LOG
5717 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5718 failure_chance(t->state->out, 0),
5719 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005720#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005721 /* Do it directly if there already is a PIM or when
5722 * nfa_postprocess() detected it will work better. */
5723 if (t->pim.result != NFA_PIM_UNUSED
5724 || t->state->c == NFA_START_INVISIBLE_FIRST
5725 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5726 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5727 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005728 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005729 int in_use = m->norm.in_use;
5730
Bram Moolenaar699c1202013-09-25 16:41:54 +02005731 /* Copy submatch info for the recursive call, opposite
5732 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005733 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005734#ifdef FEAT_SYN_HL
5735 if (nfa_has_zsubexpr)
5736 copy_sub_off(&m->synt, &t->subs.synt);
5737#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005738
Bram Moolenaara2d95102013-06-04 14:23:05 +02005739 /*
5740 * First try matching the invisible match, then what
5741 * follows.
5742 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005743 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005744 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005745 if (result == NFA_TOO_EXPENSIVE)
5746 {
5747 nfa_match = result;
5748 goto theend;
5749 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005750
Bram Moolenaardecd9542013-06-07 16:31:50 +02005751 /* for \@! and \@<! it is a match when the result is
5752 * FALSE */
5753 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005754 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5755 || t->state->c
5756 == NFA_START_INVISIBLE_BEFORE_NEG
5757 || t->state->c
5758 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005759 {
5760 /* Copy submatch info from the recursive call */
5761 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005762#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005763 if (nfa_has_zsubexpr)
5764 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005765#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005766 /* If the pattern has \ze and it matched in the
5767 * sub pattern, use it. */
5768 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005769
Bram Moolenaara2d95102013-06-04 14:23:05 +02005770 /* t->state->out1 is the corresponding
5771 * END_INVISIBLE node; Add its out to the current
5772 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005773 add_here = TRUE;
5774 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005775 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005776 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005777 }
5778 else
5779 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005780 nfa_pim_T pim;
5781
Bram Moolenaara2d95102013-06-04 14:23:05 +02005782 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005783 * First try matching what follows. Only if a match
5784 * is found verify the invisible match matches. Add a
5785 * nfa_pim_T to the following states, it contains info
5786 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005787 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005788 pim.state = t->state;
5789 pim.result = NFA_PIM_TODO;
5790 pim.subs.norm.in_use = 0;
5791#ifdef FEAT_SYN_HL
5792 pim.subs.synt.in_use = 0;
5793#endif
5794 if (REG_MULTI)
5795 {
5796 pim.end.pos.col = (int)(reginput - regline);
5797 pim.end.pos.lnum = reglnum;
5798 }
5799 else
5800 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005801
5802 /* t->state->out1 is the corresponding END_INVISIBLE
5803 * node; Add its out to the current list (zero-width
5804 * match). */
5805 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005806 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005807 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005808 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005809 break;
5810
Bram Moolenaar87953742013-06-05 18:52:40 +02005811 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005812 {
5813 nfa_state_T *skip = NULL;
5814#ifdef ENABLE_LOG
5815 int skip_lid = 0;
5816#endif
5817
5818 /* There is no point in trying to match the pattern if the
5819 * output state is not going to be added to the list. */
5820 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5821 {
5822 skip = t->state->out1->out;
5823#ifdef ENABLE_LOG
5824 skip_lid = nextlist->id;
5825#endif
5826 }
5827 else if (state_in_list(nextlist,
5828 t->state->out1->out->out, &t->subs))
5829 {
5830 skip = t->state->out1->out->out;
5831#ifdef ENABLE_LOG
5832 skip_lid = nextlist->id;
5833#endif
5834 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005835 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005836 t->state->out1->out->out, &t->subs))
5837 {
5838 skip = t->state->out1->out->out;
5839#ifdef ENABLE_LOG
5840 skip_lid = thislist->id;
5841#endif
5842 }
5843 if (skip != NULL)
5844 {
5845#ifdef ENABLE_LOG
5846 nfa_set_code(skip->c);
5847 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5848 abs(skip->id), skip_lid, skip->c, code);
5849#endif
5850 break;
5851 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005852 /* Copy submatch info to the recursive call, opposite of what
5853 * happens afterwards. */
5854 copy_sub_off(&m->norm, &t->subs.norm);
5855#ifdef FEAT_SYN_HL
5856 if (nfa_has_zsubexpr)
5857 copy_sub_off(&m->synt, &t->subs.synt);
5858#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005859
Bram Moolenaar87953742013-06-05 18:52:40 +02005860 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005861 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005862 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005863 if (result == NFA_TOO_EXPENSIVE)
5864 {
5865 nfa_match = result;
5866 goto theend;
5867 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005868 if (result)
5869 {
5870 int bytelen;
5871
5872#ifdef ENABLE_LOG
5873 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5874 log_subsexpr(m);
5875#endif
5876 /* Copy submatch info from the recursive call */
5877 copy_sub_off(&t->subs.norm, &m->norm);
5878#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005879 if (nfa_has_zsubexpr)
5880 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005881#endif
5882 /* Now we need to skip over the matched text and then
5883 * continue with what follows. */
5884 if (REG_MULTI)
5885 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005886 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005887 - (int)(reginput - regline);
5888 else
5889 bytelen = (int)(m->norm.list.line[0].end - reginput);
5890
5891#ifdef ENABLE_LOG
5892 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5893#endif
5894 if (bytelen == 0)
5895 {
5896 /* empty match, output of corresponding
5897 * NFA_END_PATTERN/NFA_SKIP to be used at current
5898 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005899 add_here = TRUE;
5900 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005901 }
5902 else if (bytelen <= clen)
5903 {
5904 /* match current character, output of corresponding
5905 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005906 add_state = t->state->out1->out->out;
5907 add_off = clen;
5908 }
5909 else
5910 {
5911 /* skip over the matched characters, set character
5912 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005913 add_state = t->state->out1->out;
5914 add_off = bytelen;
5915 add_count = bytelen - clen;
5916 }
5917 }
5918 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005919 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005920
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005921 case NFA_BOL:
5922 if (reginput == regline)
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_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005930 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005931 {
5932 add_here = TRUE;
5933 add_state = t->state->out;
5934 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005935 break;
5936
5937 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005938 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005939
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005940 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005941 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005942#ifdef FEAT_MBYTE
5943 else if (has_mbyte)
5944 {
5945 int this_class;
5946
5947 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005948 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005949 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005950 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005951 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005952 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005953 }
5954#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005955 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005956 || (reginput > regline
5957 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005958 result = FALSE;
5959 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005960 {
5961 add_here = TRUE;
5962 add_state = t->state->out;
5963 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005964 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005965
5966 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005967 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005968 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005969 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005970#ifdef FEAT_MBYTE
5971 else if (has_mbyte)
5972 {
5973 int this_class, prev_class;
5974
5975 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005976 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005977 prev_class = reg_prev_class();
5978 if (this_class == prev_class
5979 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005980 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005981 }
5982#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005983 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005984 || (reginput[0] != NUL
5985 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005986 result = FALSE;
5987 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005988 {
5989 add_here = TRUE;
5990 add_state = t->state->out;
5991 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005992 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005993
Bram Moolenaar4b780632013-05-31 22:14:52 +02005994 case NFA_BOF:
5995 if (reglnum == 0 && reginput == regline
5996 && (!REG_MULTI || reg_firstlnum == 1))
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
6003 case NFA_EOF:
6004 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006005 {
6006 add_here = TRUE;
6007 add_state = t->state->out;
6008 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006009 break;
6010
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006011#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006012 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006013 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006014 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006015 int len = 0;
6016 nfa_state_T *end;
6017 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006018 int cchars[MAX_MCO];
6019 int ccount = 0;
6020 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006021
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006022 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006023 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006024 if (utf_iscomposing(sta->c))
6025 {
6026 /* Only match composing character(s), ignore base
6027 * character. Used for ".{composing}" and "{composing}"
6028 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006029 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006030 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02006031 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006032 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006033 /* If \Z was present, then ignore composing characters.
6034 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006035 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006036 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006037 else
6038 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006039 while (sta->c != NFA_END_COMPOSING)
6040 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006041 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006042
6043 /* Check base character matches first, unless ignored. */
6044 else if (len > 0 || mc == sta->c)
6045 {
6046 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006047 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006048 len += mb_char2len(mc);
6049 sta = sta->out;
6050 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006051
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006052 /* We don't care about the order of composing characters.
6053 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006054 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006055 {
6056 mc = mb_ptr2char(reginput + len);
6057 cchars[ccount++] = mc;
6058 len += mb_char2len(mc);
6059 if (ccount == MAX_MCO)
6060 break;
6061 }
6062
6063 /* Check that each composing char in the pattern matches a
6064 * composing char in the text. We do not check if all
6065 * composing chars are matched. */
6066 result = OK;
6067 while (sta->c != NFA_END_COMPOSING)
6068 {
6069 for (j = 0; j < ccount; ++j)
6070 if (cchars[j] == sta->c)
6071 break;
6072 if (j == ccount)
6073 {
6074 result = FAIL;
6075 break;
6076 }
6077 sta = sta->out;
6078 }
6079 }
6080 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006081 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006082
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006083 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006084 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006085 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006086 }
6087#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006088
6089 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006090 if (curc == NUL && !reg_line_lbr && REG_MULTI
6091 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006092 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006093 go_to_nextline = TRUE;
6094 /* Pass -1 for the offset, which means taking the position
6095 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006096 add_state = t->state->out;
6097 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006098 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006099 else if (curc == '\n' && reg_line_lbr)
6100 {
6101 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006102 add_state = t->state->out;
6103 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006104 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006105 break;
6106
Bram Moolenaar417bad22013-06-07 14:08:30 +02006107 case NFA_START_COLL:
6108 case NFA_START_NEG_COLL:
6109 {
6110 /* What follows is a list of characters, until NFA_END_COLL.
6111 * One of them must match or none of them must match. */
6112 nfa_state_T *state;
6113 int result_if_matched;
6114 int c1, c2;
6115
6116 /* Never match EOL. If it's part of the collection it is added
6117 * as a separate state with an OR. */
6118 if (curc == NUL)
6119 break;
6120
6121 state = t->state->out;
6122 result_if_matched = (t->state->c == NFA_START_COLL);
6123 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006124 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006125 if (state->c == NFA_END_COLL)
6126 {
6127 result = !result_if_matched;
6128 break;
6129 }
6130 if (state->c == NFA_RANGE_MIN)
6131 {
6132 c1 = state->val;
6133 state = state->out; /* advance to NFA_RANGE_MAX */
6134 c2 = state->val;
6135#ifdef ENABLE_LOG
6136 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6137 curc, c1, c2);
6138#endif
6139 if (curc >= c1 && curc <= c2)
6140 {
6141 result = result_if_matched;
6142 break;
6143 }
6144 if (ireg_ic)
6145 {
6146 int curc_low = MB_TOLOWER(curc);
6147 int done = FALSE;
6148
6149 for ( ; c1 <= c2; ++c1)
6150 if (MB_TOLOWER(c1) == curc_low)
6151 {
6152 result = result_if_matched;
6153 done = TRUE;
6154 break;
6155 }
6156 if (done)
6157 break;
6158 }
6159 }
6160 else if (state->c < 0 ? check_char_class(state->c, curc)
6161 : (curc == state->c
6162 || (ireg_ic && MB_TOLOWER(curc)
6163 == MB_TOLOWER(state->c))))
6164 {
6165 result = result_if_matched;
6166 break;
6167 }
6168 state = state->out;
6169 }
6170 if (result)
6171 {
6172 /* next state is in out of the NFA_END_COLL, out1 of
6173 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006174 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006175 add_off = clen;
6176 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006177 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006178 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006179
6180 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006181 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006182 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006183 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006184 add_state = t->state->out;
6185 add_off = clen;
6186 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006187 break;
6188
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006189 case NFA_ANY_COMPOSING:
6190 /* On a composing character skip over it. Otherwise do
6191 * nothing. Always matches. */
6192#ifdef FEAT_MBYTE
6193 if (enc_utf8 && utf_iscomposing(curc))
6194 {
6195 add_off = clen;
6196 }
6197 else
6198#endif
6199 {
6200 add_here = TRUE;
6201 add_off = 0;
6202 }
6203 add_state = t->state->out;
6204 break;
6205
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006206 /*
6207 * Character classes like \a for alpha, \d for digit etc.
6208 */
6209 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006210 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006211 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006212 break;
6213
6214 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006215 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006216 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006217 break;
6218
6219 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006220 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006221 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006222 break;
6223
6224 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006225 result = !VIM_ISDIGIT(curc)
6226 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006227 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006228 break;
6229
6230 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006231 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006232 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006233 break;
6234
6235 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006236 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006237 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006238 break;
6239
6240 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006241 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006242 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006243 break;
6244
6245 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006246 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006247 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006248 break;
6249
6250 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006251 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006252 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006253 break;
6254
6255 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006256 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006257 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006258 break;
6259
6260 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006261 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006262 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006263 break;
6264
6265 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006266 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006267 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006268 break;
6269
6270 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006271 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006272 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006273 break;
6274
6275 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006276 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006277 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006278 break;
6279
6280 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006281 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006282 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006283 break;
6284
6285 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006286 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006287 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006288 break;
6289
6290 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006291 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006292 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006293 break;
6294
6295 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006296 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006297 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006298 break;
6299
6300 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006301 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006302 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006303 break;
6304
6305 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006306 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006307 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006308 break;
6309
6310 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006311 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006312 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006313 break;
6314
6315 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006316 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006317 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318 break;
6319
6320 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006321 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006322 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323 break;
6324
6325 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006326 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006327 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006328 break;
6329
6330 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006331 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006332 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006333 break;
6334
6335 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006336 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006337 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006338 break;
6339
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006340 case NFA_LOWER_IC: /* [a-z] */
6341 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6342 ADD_STATE_IF_MATCH(t->state);
6343 break;
6344
6345 case NFA_NLOWER_IC: /* [^a-z] */
6346 result = curc != NUL
6347 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6348 ADD_STATE_IF_MATCH(t->state);
6349 break;
6350
6351 case NFA_UPPER_IC: /* [A-Z] */
6352 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6353 ADD_STATE_IF_MATCH(t->state);
6354 break;
6355
6356 case NFA_NUPPER_IC: /* ^[A-Z] */
6357 result = curc != NUL
6358 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6359 ADD_STATE_IF_MATCH(t->state);
6360 break;
6361
Bram Moolenaar5714b802013-05-28 22:03:20 +02006362 case NFA_BACKREF1:
6363 case NFA_BACKREF2:
6364 case NFA_BACKREF3:
6365 case NFA_BACKREF4:
6366 case NFA_BACKREF5:
6367 case NFA_BACKREF6:
6368 case NFA_BACKREF7:
6369 case NFA_BACKREF8:
6370 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006371#ifdef FEAT_SYN_HL
6372 case NFA_ZREF1:
6373 case NFA_ZREF2:
6374 case NFA_ZREF3:
6375 case NFA_ZREF4:
6376 case NFA_ZREF5:
6377 case NFA_ZREF6:
6378 case NFA_ZREF7:
6379 case NFA_ZREF8:
6380 case NFA_ZREF9:
6381#endif
6382 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006383 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006384 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006385 int bytelen;
6386
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006387 if (t->state->c <= NFA_BACKREF9)
6388 {
6389 subidx = t->state->c - NFA_BACKREF1 + 1;
6390 result = match_backref(&t->subs.norm, subidx, &bytelen);
6391 }
6392#ifdef FEAT_SYN_HL
6393 else
6394 {
6395 subidx = t->state->c - NFA_ZREF1 + 1;
6396 result = match_zref(subidx, &bytelen);
6397 }
6398#endif
6399
Bram Moolenaar5714b802013-05-28 22:03:20 +02006400 if (result)
6401 {
6402 if (bytelen == 0)
6403 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006404 /* empty match always works, output of NFA_SKIP to be
6405 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006406 add_here = TRUE;
6407 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006408 }
6409 else if (bytelen <= clen)
6410 {
6411 /* match current character, jump ahead to out of
6412 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006413 add_state = t->state->out->out;
6414 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006415 }
6416 else
6417 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006418 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006419 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006420 add_state = t->state->out;
6421 add_off = bytelen;
6422 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006423 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006424 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006425 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006426 }
6427 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006428 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006429 if (t->count - clen <= 0)
6430 {
6431 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006432 add_state = t->state->out;
6433 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006434 }
6435 else
6436 {
6437 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006438 add_state = t->state;
6439 add_off = 0;
6440 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006441 }
6442 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006443
Bram Moolenaar423532e2013-05-29 21:14:42 +02006444 case NFA_LNUM:
6445 case NFA_LNUM_GT:
6446 case NFA_LNUM_LT:
6447 result = (REG_MULTI &&
6448 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6449 (long_u)(reglnum + reg_firstlnum)));
6450 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006451 {
6452 add_here = TRUE;
6453 add_state = t->state->out;
6454 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006455 break;
6456
6457 case NFA_COL:
6458 case NFA_COL_GT:
6459 case NFA_COL_LT:
6460 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6461 (long_u)(reginput - regline) + 1);
6462 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006463 {
6464 add_here = TRUE;
6465 add_state = t->state->out;
6466 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006467 break;
6468
6469 case NFA_VCOL:
6470 case NFA_VCOL_GT:
6471 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006472 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006473 int op = t->state->c - NFA_VCOL;
6474 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaaref795d12015-01-18 16:46:32 +01006475 win_T *wp = reg_win == NULL ? curwin : reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006476
6477 /* Bail out quickly when there can't be a match, avoid the
6478 * overhead of win_linetabsize() on long lines. */
Bram Moolenaaref795d12015-01-18 16:46:32 +01006479 if (op != 1 && col > t->state->val)
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006480 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006481 result = FALSE;
6482 if (op == 1 && col - 1 > t->state->val && col > 100)
6483 {
6484 int ts = wp->w_buffer->b_p_ts;
6485
6486 /* Guess that a character won't use more columns than
6487 * 'tabstop', with a minimum of 4. */
6488 if (ts < 4)
6489 ts = 4;
6490 result = col > t->state->val * ts;
6491 }
6492 if (!result)
6493 result = nfa_re_num_cmp(t->state->val, op,
6494 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006495 if (result)
6496 {
6497 add_here = TRUE;
6498 add_state = t->state->out;
6499 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006500 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006501 break;
6502
Bram Moolenaar044aa292013-06-04 21:27:38 +02006503 case NFA_MARK:
6504 case NFA_MARK_GT:
6505 case NFA_MARK_LT:
6506 {
6507 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6508
6509 /* Compare the mark position to the match position. */
6510 result = (pos != NULL /* mark doesn't exist */
6511 && pos->lnum > 0 /* mark isn't set in reg_buf */
6512 && (pos->lnum == reglnum + reg_firstlnum
6513 ? (pos->col == (colnr_T)(reginput - regline)
6514 ? t->state->c == NFA_MARK
6515 : (pos->col < (colnr_T)(reginput - regline)
6516 ? t->state->c == NFA_MARK_GT
6517 : t->state->c == NFA_MARK_LT))
6518 : (pos->lnum < reglnum + reg_firstlnum
6519 ? t->state->c == NFA_MARK_GT
6520 : t->state->c == NFA_MARK_LT)));
6521 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006522 {
6523 add_here = TRUE;
6524 add_state = t->state->out;
6525 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006526 break;
6527 }
6528
Bram Moolenaar423532e2013-05-29 21:14:42 +02006529 case NFA_CURSOR:
6530 result = (reg_win != NULL
6531 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6532 && ((colnr_T)(reginput - regline)
6533 == reg_win->w_cursor.col));
6534 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006535 {
6536 add_here = TRUE;
6537 add_state = t->state->out;
6538 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006539 break;
6540
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006541 case NFA_VISUAL:
6542 result = reg_match_visual();
6543 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006544 {
6545 add_here = TRUE;
6546 add_state = t->state->out;
6547 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006548 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006549
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006550 case NFA_MOPEN1:
6551 case NFA_MOPEN2:
6552 case NFA_MOPEN3:
6553 case NFA_MOPEN4:
6554 case NFA_MOPEN5:
6555 case NFA_MOPEN6:
6556 case NFA_MOPEN7:
6557 case NFA_MOPEN8:
6558 case NFA_MOPEN9:
6559#ifdef FEAT_SYN_HL
6560 case NFA_ZOPEN:
6561 case NFA_ZOPEN1:
6562 case NFA_ZOPEN2:
6563 case NFA_ZOPEN3:
6564 case NFA_ZOPEN4:
6565 case NFA_ZOPEN5:
6566 case NFA_ZOPEN6:
6567 case NFA_ZOPEN7:
6568 case NFA_ZOPEN8:
6569 case NFA_ZOPEN9:
6570#endif
6571 case NFA_NOPEN:
6572 case NFA_ZSTART:
6573 /* These states are only added to be able to bail out when
6574 * they are added again, nothing is to be done. */
6575 break;
6576
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006577 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006578 {
6579 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006580
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006581#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006582 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006583 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006584#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006585 result = (c == curc);
6586
6587 if (!result && ireg_ic)
6588 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006589#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006590 /* If ireg_icombine is not set only skip over the character
6591 * itself. When it is set skip over composing characters. */
6592 if (result && enc_utf8 && !ireg_icombine)
6593 clen = utf_char2len(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006594#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006595 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006596 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006597 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006598
6599 } /* switch (t->state->c) */
6600
6601 if (add_state != NULL)
6602 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006603 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006604 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006605
6606 if (t->pim.result == NFA_PIM_UNUSED)
6607 pim = NULL;
6608 else
6609 pim = &t->pim;
6610
6611 /* Handle the postponed invisible match if the match might end
6612 * without advancing and before the end of the line. */
6613 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006614 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006615 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006616 {
6617#ifdef ENABLE_LOG
6618 fprintf(log_fd, "\n");
6619 fprintf(log_fd, "==================================\n");
6620 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6621 fprintf(log_fd, "\n");
6622#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006623 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006624 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006625 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006626 /* for \@! and \@<! it is a match when the result is
6627 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006628 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006629 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6630 || pim->state->c
6631 == NFA_START_INVISIBLE_BEFORE_NEG
6632 || pim->state->c
6633 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006634 {
6635 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006636 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006637#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006638 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006639 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006640#endif
6641 }
6642 }
6643 else
6644 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006645 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006646#ifdef ENABLE_LOG
6647 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006648 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006649 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6650 fprintf(log_fd, "\n");
6651#endif
6652 }
6653
Bram Moolenaardecd9542013-06-07 16:31:50 +02006654 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006655 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006656 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6657 || pim->state->c
6658 == NFA_START_INVISIBLE_BEFORE_NEG
6659 || pim->state->c
6660 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006661 {
6662 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006663 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006664#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006665 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006666 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006667#endif
6668 }
6669 else
6670 /* look-behind match failed, don't add the state */
6671 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006672
6673 /* Postponed invisible match was handled, don't add it to
6674 * following states. */
6675 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006676 }
6677
Bram Moolenaara951e352013-10-06 15:46:11 +02006678 /* If "pim" points into l->t it will become invalid when
6679 * adding the state causes the list to be reallocated. Make a
6680 * local copy to avoid that. */
6681 if (pim == &t->pim)
6682 {
6683 copy_pim(&pim_copy, pim);
6684 pim = &pim_copy;
6685 }
6686
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006687 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006688 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006689 else
6690 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006691 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006692 if (add_count > 0)
6693 nextlist->t[nextlist->n - 1].count = add_count;
6694 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006695 }
6696
6697 } /* for (thislist = thislist; thislist->state; thislist++) */
6698
Bram Moolenaare23febd2013-05-26 18:40:14 +02006699 /* Look for the start of a match in the current position by adding the
6700 * start state to the list of states.
6701 * The first found match is the leftmost one, thus the order of states
6702 * matters!
6703 * Do not add the start state in recursive calls of nfa_regmatch(),
6704 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006705 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006706 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006707 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006708 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006709 && reglnum == 0
6710 && clen != 0
6711 && (ireg_maxcol == 0
6712 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006713 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006714 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006715 ? (reglnum < nfa_endp->se_u.pos.lnum
6716 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006717 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006718 < nfa_endp->se_u.pos.col))
6719 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006720 {
6721#ifdef ENABLE_LOG
6722 fprintf(log_fd, "(---) STARTSTATE\n");
6723#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006724 /* Inline optimized code for addstate() if we know the state is
6725 * the first MOPEN. */
6726 if (toplevel)
6727 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006728 int add = TRUE;
6729 int c;
6730
6731 if (prog->regstart != NUL && clen != 0)
6732 {
6733 if (nextlist->n == 0)
6734 {
6735 colnr_T col = (colnr_T)(reginput - regline) + clen;
6736
6737 /* Nextlist is empty, we can skip ahead to the
6738 * character that must appear at the start. */
6739 if (skip_to_start(prog->regstart, &col) == FAIL)
6740 break;
6741#ifdef ENABLE_LOG
6742 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6743 col - ((colnr_T)(reginput - regline) + clen));
6744#endif
6745 reginput = regline + col - clen;
6746 }
6747 else
6748 {
6749 /* Checking if the required start character matches is
6750 * cheaper than adding a state that won't match. */
6751 c = PTR2CHAR(reginput + clen);
6752 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6753 != MB_TOLOWER(prog->regstart)))
6754 {
6755#ifdef ENABLE_LOG
6756 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6757#endif
6758 add = FALSE;
6759 }
6760 }
6761 }
6762
6763 if (add)
6764 {
6765 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006766 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006767 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006768 else
6769 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006770 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006771 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006772 }
6773 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006774 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006775 }
6776
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006777#ifdef ENABLE_LOG
6778 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006779 {
6780 int i;
6781
6782 for (i = 0; i < thislist->n; i++)
6783 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6784 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006785 fprintf(log_fd, "\n");
6786#endif
6787
6788nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006789 /* Advance to the next character, or advance to the next line, or
6790 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006791 if (clen != 0)
6792 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006793 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6794 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006795 reg_nextline();
6796 else
6797 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006798
6799 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006800 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006801 if (got_int)
6802 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006803#ifdef FEAT_RELTIME
6804 /* Check for timeout once in a twenty times to avoid overhead. */
6805 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6806 {
6807 nfa_time_count = 0;
6808 if (profile_passed_limit(nfa_time_limit))
6809 break;
6810 }
6811#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006812 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006813
6814#ifdef ENABLE_LOG
6815 if (log_fd != stderr)
6816 fclose(log_fd);
6817 log_fd = NULL;
6818#endif
6819
6820theend:
6821 /* Free memory */
6822 vim_free(list[0].t);
6823 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006824 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006825#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006826#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006827 fclose(debug);
6828#endif
6829
Bram Moolenaar963fee22013-05-26 21:47:28 +02006830 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006831}
6832
6833/*
6834 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006835 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006836 */
6837 static long
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006838nfa_regtry(prog, col, tm)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006839 nfa_regprog_T *prog;
6840 colnr_T col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006841 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006842{
6843 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006844 regsubs_T subs, m;
6845 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006846 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006847#ifdef ENABLE_LOG
6848 FILE *f;
6849#endif
6850
6851 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006852#ifdef FEAT_RELTIME
6853 nfa_time_limit = tm;
6854 nfa_time_count = 0;
6855#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006856
6857#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006858 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006859 if (f != NULL)
6860 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006861 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006862#ifdef DEBUG
6863 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6864#endif
6865 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006866 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006867 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006868 fprintf(f, "\n\n");
6869 fclose(f);
6870 }
6871 else
6872 EMSG(_("Could not open temporary log file for writing "));
6873#endif
6874
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006875 clear_sub(&subs.norm);
6876 clear_sub(&m.norm);
6877#ifdef FEAT_SYN_HL
6878 clear_sub(&subs.synt);
6879 clear_sub(&m.synt);
6880#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006881
Bram Moolenaarfda37292014-11-05 14:27:36 +01006882 result = nfa_regmatch(prog, start, &subs, &m);
6883 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006884 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006885 else if (result == NFA_TOO_EXPENSIVE)
6886 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006887
6888 cleanup_subexpr();
6889 if (REG_MULTI)
6890 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006891 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006892 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006893 reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6894 reg_startpos[i].col = subs.norm.list.multi[i].start_col;
6895
6896 reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6897 reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006898 }
6899
6900 if (reg_startpos[0].lnum < 0)
6901 {
6902 reg_startpos[0].lnum = 0;
6903 reg_startpos[0].col = col;
6904 }
6905 if (reg_endpos[0].lnum < 0)
6906 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006907 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006908 reg_endpos[0].lnum = reglnum;
6909 reg_endpos[0].col = (int)(reginput - regline);
6910 }
6911 else
6912 /* Use line number of "\ze". */
6913 reglnum = reg_endpos[0].lnum;
6914 }
6915 else
6916 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006917 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006918 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006919 reg_startp[i] = subs.norm.list.line[i].start;
6920 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006921 }
6922
6923 if (reg_startp[0] == NULL)
6924 reg_startp[0] = regline + col;
6925 if (reg_endp[0] == NULL)
6926 reg_endp[0] = reginput;
6927 }
6928
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006929#ifdef FEAT_SYN_HL
6930 /* Package any found \z(...\) matches for export. Default is none. */
6931 unref_extmatch(re_extmatch_out);
6932 re_extmatch_out = NULL;
6933
6934 if (prog->reghasz == REX_SET)
6935 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006936 cleanup_zsubexpr();
6937 re_extmatch_out = make_extmatch();
6938 for (i = 0; i < subs.synt.in_use; i++)
6939 {
6940 if (REG_MULTI)
6941 {
6942 struct multipos *mpos = &subs.synt.list.multi[i];
6943
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006944 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006945 if (mpos->start_lnum >= 0
6946 && mpos->start_lnum == mpos->end_lnum
6947 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006948 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006949 vim_strnsave(reg_getline(mpos->start_lnum)
6950 + mpos->start_col,
6951 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006952 }
6953 else
6954 {
6955 struct linepos *lpos = &subs.synt.list.line[i];
6956
6957 if (lpos->start != NULL && lpos->end != NULL)
6958 re_extmatch_out->matches[i] =
6959 vim_strnsave(lpos->start,
6960 (int)(lpos->end - lpos->start));
6961 }
6962 }
6963 }
6964#endif
6965
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006966 return 1 + reglnum;
6967}
6968
6969/*
6970 * Match a regexp against a string ("line" points to the string) or multiple
6971 * lines ("line" is NULL, use reg_getline()).
6972 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01006973 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006974 */
6975 static long
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006976nfa_regexec_both(line, startcol, tm)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006977 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006978 colnr_T startcol; /* column to start looking for match */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006979 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006980{
6981 nfa_regprog_T *prog;
6982 long retval = 0L;
6983 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006984 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006985
6986 if (REG_MULTI)
6987 {
6988 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6989 line = reg_getline((linenr_T)0); /* relative to the cursor */
6990 reg_startpos = reg_mmatch->startpos;
6991 reg_endpos = reg_mmatch->endpos;
6992 }
6993 else
6994 {
6995 prog = (nfa_regprog_T *)reg_match->regprog;
6996 reg_startp = reg_match->startp;
6997 reg_endp = reg_match->endp;
6998 }
6999
7000 /* Be paranoid... */
7001 if (prog == NULL || line == NULL)
7002 {
7003 EMSG(_(e_null));
7004 goto theend;
7005 }
7006
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007007 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
7008 if (prog->regflags & RF_ICASE)
7009 ireg_ic = TRUE;
7010 else if (prog->regflags & RF_NOICASE)
7011 ireg_ic = FALSE;
7012
7013#ifdef FEAT_MBYTE
7014 /* If pattern contains "\Z" overrule value of ireg_icombine */
7015 if (prog->regflags & RF_ICOMBINE)
7016 ireg_icombine = TRUE;
7017#endif
7018
7019 regline = line;
7020 reglnum = 0; /* relative to line */
7021
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007022 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007023 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007024 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007025 nfa_listid = 1;
7026 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007027 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028
Bram Moolenaard89616e2013-06-06 18:46:06 +02007029 if (prog->reganch && col > 0)
7030 return 0L;
7031
Bram Moolenaar473de612013-06-08 18:19:48 +02007032 need_clear_subexpr = TRUE;
7033#ifdef FEAT_SYN_HL
7034 /* Clear the external match subpointers if necessary. */
7035 if (prog->reghasz == REX_SET)
7036 {
7037 nfa_has_zsubexpr = TRUE;
7038 need_clear_zsubexpr = TRUE;
7039 }
7040 else
7041 nfa_has_zsubexpr = FALSE;
7042#endif
7043
Bram Moolenaard89616e2013-06-06 18:46:06 +02007044 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007045 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007046 /* Skip ahead until a character we know the match must start with.
7047 * When there is none there is no match. */
7048 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007049 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007050
Bram Moolenaar473de612013-06-08 18:19:48 +02007051 /* If match_text is set it contains the full text that must match.
7052 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007053 if (prog->match_text != NULL
7054#ifdef FEAT_MBYTE
7055 && !ireg_icombine
7056#endif
7057 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007058 return find_match_text(col, prog->regstart, prog->match_text);
7059 }
7060
Bram Moolenaard89616e2013-06-06 18:46:06 +02007061 /* If the start column is past the maximum column: no need to try. */
7062 if (ireg_maxcol > 0 && col >= ireg_maxcol)
7063 goto theend;
7064
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007065 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007066 for (i = 0; i < nstate; ++i)
7067 {
7068 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007069 prog->state[i].lastlist[0] = 0;
7070 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007071 }
7072
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007073 retval = nfa_regtry(prog, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007074
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007075 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007076
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007077theend:
7078 return retval;
7079}
7080
7081/*
7082 * Compile a regular expression into internal code for the NFA matcher.
7083 * Returns the program in allocated space. Returns NULL for an error.
7084 */
7085 static regprog_T *
7086nfa_regcomp(expr, re_flags)
7087 char_u *expr;
7088 int re_flags;
7089{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007090 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007091 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007092 int *postfix;
7093
7094 if (expr == NULL)
7095 return NULL;
7096
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007097 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007098 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007099
7100 init_class_tab();
7101
7102 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7103 return NULL;
7104
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007105 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007106 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007107 postfix = re2post();
7108 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007109 {
7110 /* TODO: only give this error for debugging? */
7111 if (post_ptr >= post_end)
7112 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007113 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007114 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007115
7116 /*
7117 * In order to build the NFA, we parse the input regexp twice:
7118 * 1. first pass to count size (so we can allocate space)
7119 * 2. second to emit code
7120 */
7121#ifdef ENABLE_LOG
7122 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007123 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007124
7125 if (f != NULL)
7126 {
7127 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7128 fclose(f);
7129 }
7130 }
7131#endif
7132
7133 /*
7134 * PASS 1
7135 * Count number of NFA states in "nstate". Do not build the NFA.
7136 */
7137 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007138
Bram Moolenaar16619a22013-06-11 18:42:36 +02007139 /* allocate the regprog with space for the compiled regexp */
7140 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007141 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7142 if (prog == NULL)
7143 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007144 state_ptr = prog->state;
7145
7146 /*
7147 * PASS 2
7148 * Build the NFA
7149 */
7150 prog->start = post2nfa(postfix, post_ptr, FALSE);
7151 if (prog->start == NULL)
7152 goto fail;
7153
7154 prog->regflags = regflags;
7155 prog->engine = &nfa_regengine;
7156 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007157 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007158 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007159 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007160
Bram Moolenaara2947e22013-06-11 22:44:09 +02007161 nfa_postprocess(prog);
7162
Bram Moolenaard89616e2013-06-06 18:46:06 +02007163 prog->reganch = nfa_get_reganch(prog->start, 0);
7164 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007165 prog->match_text = nfa_get_match_text(prog->start);
7166
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007167#ifdef ENABLE_LOG
7168 nfa_postfix_dump(expr, OK);
7169 nfa_dump(prog);
7170#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007171#ifdef FEAT_SYN_HL
7172 /* Remember whether this pattern has any \z specials in it. */
7173 prog->reghasz = re_has_z;
7174#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007175 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007176 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007177
7178out:
7179 vim_free(post_start);
7180 post_start = post_ptr = post_end = NULL;
7181 state_ptr = NULL;
7182 return (regprog_T *)prog;
7183
7184fail:
7185 vim_free(prog);
7186 prog = NULL;
7187#ifdef ENABLE_LOG
7188 nfa_postfix_dump(expr, FAIL);
7189#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007190 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007191 goto out;
7192}
7193
Bram Moolenaar473de612013-06-08 18:19:48 +02007194/*
7195 * Free a compiled regexp program, returned by nfa_regcomp().
7196 */
7197 static void
7198nfa_regfree(prog)
7199 regprog_T *prog;
7200{
7201 if (prog != NULL)
7202 {
7203 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007204 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007205 vim_free(prog);
7206 }
7207}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208
7209/*
7210 * Match a regexp against a string.
7211 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7212 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007213 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007214 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007215 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007216 */
7217 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007218nfa_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219 regmatch_T *rmp;
7220 char_u *line; /* string to match against */
7221 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007222 int line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007223{
7224 reg_match = rmp;
7225 reg_mmatch = NULL;
7226 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007227 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228 reg_buf = curbuf;
7229 reg_win = NULL;
7230 ireg_ic = rmp->rm_ic;
7231#ifdef FEAT_MBYTE
7232 ireg_icombine = FALSE;
7233#endif
7234 ireg_maxcol = 0;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007235 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007236}
7237
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007238
7239/*
7240 * Match a regexp against multiple lines.
7241 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7242 * Uses curbuf for line count and 'iskeyword'.
7243 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007244 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007245 * match otherwise.
7246 *
7247 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7248 *
7249 * ! Also NOTE : match may actually be in another line. e.g.:
7250 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7251 *
7252 * +-------------------------+
7253 * |a |
7254 * |b |
7255 * |c |
7256 * | |
7257 * +-------------------------+
7258 *
7259 * then nfa_regexec_multi() returns 3. while the original
7260 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7261 *
7262 * FIXME if this behavior is not compatible.
7263 */
7264 static long
7265nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7266 regmmatch_T *rmp;
7267 win_T *win; /* window in which to search or NULL */
7268 buf_T *buf; /* buffer in which to search */
7269 linenr_T lnum; /* nr of line to start looking for match */
7270 colnr_T col; /* column to start looking for match */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007271 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007272{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007273 reg_match = NULL;
7274 reg_mmatch = rmp;
7275 reg_buf = buf;
7276 reg_win = win;
7277 reg_firstlnum = lnum;
7278 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7279 reg_line_lbr = FALSE;
7280 ireg_ic = rmp->rmm_ic;
7281#ifdef FEAT_MBYTE
7282 ireg_icombine = FALSE;
7283#endif
7284 ireg_maxcol = rmp->rmm_maxcol;
7285
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007286 return nfa_regexec_both(NULL, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007287}
7288
7289#ifdef DEBUG
7290# undef ENABLE_LOG
7291#endif