blob: e67f6380c13df8fdd00e6842aa2b99531b51c39b [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
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002037 * maximum is much larger than the minimum and when the maximum is
2038 * large. Bail out if we can use the other engine. */
2039 if ((nfa_re_flags & RE_AUTO)
2040 && (maxval > minval + 200 || maxval > 500))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002041 return FAIL;
2042
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002044 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002045 /* Save parse state after the repeated atom and the \{} */
2046 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002047
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002048 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2049 for (i = 0; i < maxval; i++)
2050 {
2051 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002052 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002053 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054 if (nfa_regatom() == FAIL)
2055 return FAIL;
2056 /* after "minval" times, atoms are optional */
2057 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002058 {
2059 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002060 {
2061 if (greedy)
2062 EMIT(NFA_STAR);
2063 else
2064 EMIT(NFA_STAR_NONGREEDY);
2065 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002066 else
2067 EMIT(quest);
2068 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002069 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002071 if (i + 1 > minval && maxval == MAX_LIMIT)
2072 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073 }
2074
2075 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002076 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 curchr = -1;
2078
2079 break;
2080
2081
2082 default:
2083 break;
2084 } /* end switch */
2085
2086 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002088 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002089
2090 return OK;
2091}
2092
2093/*
2094 * Parse one or more pieces, concatenated. It matches a match for the
2095 * first piece, followed by a match for the second piece, etc. Example:
2096 * "f[0-9]b", first matches "f", then a digit and then "b".
2097 *
2098 * concat ::= piece
2099 * or piece piece
2100 * or piece piece piece
2101 * etc.
2102 */
2103 static int
2104nfa_regconcat()
2105{
2106 int cont = TRUE;
2107 int first = TRUE;
2108
2109 while (cont)
2110 {
2111 switch (peekchr())
2112 {
2113 case NUL:
2114 case Magic('|'):
2115 case Magic('&'):
2116 case Magic(')'):
2117 cont = FALSE;
2118 break;
2119
2120 case Magic('Z'):
2121#ifdef FEAT_MBYTE
2122 regflags |= RF_ICOMBINE;
2123#endif
2124 skipchr_keepstart();
2125 break;
2126 case Magic('c'):
2127 regflags |= RF_ICASE;
2128 skipchr_keepstart();
2129 break;
2130 case Magic('C'):
2131 regflags |= RF_NOICASE;
2132 skipchr_keepstart();
2133 break;
2134 case Magic('v'):
2135 reg_magic = MAGIC_ALL;
2136 skipchr_keepstart();
2137 curchr = -1;
2138 break;
2139 case Magic('m'):
2140 reg_magic = MAGIC_ON;
2141 skipchr_keepstart();
2142 curchr = -1;
2143 break;
2144 case Magic('M'):
2145 reg_magic = MAGIC_OFF;
2146 skipchr_keepstart();
2147 curchr = -1;
2148 break;
2149 case Magic('V'):
2150 reg_magic = MAGIC_NONE;
2151 skipchr_keepstart();
2152 curchr = -1;
2153 break;
2154
2155 default:
2156 if (nfa_regpiece() == FAIL)
2157 return FAIL;
2158 if (first == FALSE)
2159 EMIT(NFA_CONCAT);
2160 else
2161 first = FALSE;
2162 break;
2163 }
2164 }
2165
2166 return OK;
2167}
2168
2169/*
2170 * Parse a branch, one or more concats, separated by "\&". It matches the
2171 * last concat, but only if all the preceding concats also match at the same
2172 * position. Examples:
2173 * "foobeep\&..." matches "foo" in "foobeep".
2174 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2175 *
2176 * branch ::= concat
2177 * or concat \& concat
2178 * or concat \& concat \& concat
2179 * etc.
2180 */
2181 static int
2182nfa_regbranch()
2183{
2184 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002185 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002186
Bram Moolenaar16299b52013-05-30 18:45:23 +02002187 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002188
2189 /* First branch, possibly the only one */
2190 if (nfa_regconcat() == FAIL)
2191 return FAIL;
2192
2193 ch = peekchr();
2194 /* Try next concats */
2195 while (ch == Magic('&'))
2196 {
2197 skipchr();
2198 EMIT(NFA_NOPEN);
2199 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002200 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 if (nfa_regconcat() == FAIL)
2202 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002203 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002204 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002205 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002206 EMIT(NFA_CONCAT);
2207 ch = peekchr();
2208 }
2209
Bram Moolenaar699c1202013-09-25 16:41:54 +02002210 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002211 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002212 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002213
2214 return OK;
2215}
2216
2217/*
2218 * Parse a pattern, one or more branches, separated by "\|". It matches
2219 * anything that matches one of the branches. Example: "foo\|beep" matches
2220 * "foo" and matches "beep". If more than one branch matches, the first one
2221 * is used.
2222 *
2223 * pattern ::= branch
2224 * or branch \| branch
2225 * or branch \| branch \| branch
2226 * etc.
2227 */
2228 static int
2229nfa_reg(paren)
2230 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
2231{
2232 int parno = 0;
2233
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234 if (paren == REG_PAREN)
2235 {
2236 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002238 parno = regnpar++;
2239 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002240#ifdef FEAT_SYN_HL
2241 else if (paren == REG_ZPAREN)
2242 {
2243 /* Make a ZOPEN node. */
2244 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002245 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002246 parno = regnzpar++;
2247 }
2248#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002249
2250 if (nfa_regbranch() == FAIL)
2251 return FAIL; /* cascaded error */
2252
2253 while (peekchr() == Magic('|'))
2254 {
2255 skipchr();
2256 if (nfa_regbranch() == FAIL)
2257 return FAIL; /* cascaded error */
2258 EMIT(NFA_OR);
2259 }
2260
2261 /* Check for proper termination. */
2262 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2263 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002264 if (paren == REG_NPAREN)
2265 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2266 else
2267 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2268 }
2269 else if (paren == REG_NOPAREN && peekchr() != NUL)
2270 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002271 if (peekchr() == Magic(')'))
2272 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2273 else
2274 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2275 }
2276 /*
2277 * Here we set the flag allowing back references to this set of
2278 * parentheses.
2279 */
2280 if (paren == REG_PAREN)
2281 {
2282 had_endbrace[parno] = TRUE; /* have seen the close paren */
2283 EMIT(NFA_MOPEN + parno);
2284 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002285#ifdef FEAT_SYN_HL
2286 else if (paren == REG_ZPAREN)
2287 EMIT(NFA_ZOPEN + parno);
2288#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289
2290 return OK;
2291}
2292
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293#ifdef DEBUG
2294static char_u code[50];
2295
2296 static void
2297nfa_set_code(c)
2298 int c;
2299{
2300 int addnl = FALSE;
2301
2302 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2303 {
2304 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002305 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002306 }
2307
2308 STRCPY(code, "");
2309 switch (c)
2310 {
2311 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2312 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2313 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2314 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2315 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2316 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2317
Bram Moolenaar5714b802013-05-28 22:03:20 +02002318 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2319 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2320 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2321 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2322 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2323 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2324 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2325 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2326 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002327#ifdef FEAT_SYN_HL
2328 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2329 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2330 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2331 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2332 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2333 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2334 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2335 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2336 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2337#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002338 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2339
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340 case NFA_PREV_ATOM_NO_WIDTH:
2341 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002342 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2343 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002344 case NFA_PREV_ATOM_JUST_BEFORE:
2345 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2346 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2347 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002348 case NFA_PREV_ATOM_LIKE_PATTERN:
2349 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2350
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002351 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2352 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002353 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002354 case NFA_START_INVISIBLE_FIRST:
2355 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002356 case NFA_START_INVISIBLE_NEG:
2357 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002358 case NFA_START_INVISIBLE_NEG_FIRST:
2359 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002360 case NFA_START_INVISIBLE_BEFORE:
2361 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002362 case NFA_START_INVISIBLE_BEFORE_FIRST:
2363 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002364 case NFA_START_INVISIBLE_BEFORE_NEG:
2365 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002366 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2367 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002368 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002370 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002371 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002373 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2374 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002375 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002376
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002377 case NFA_MOPEN:
2378 case NFA_MOPEN1:
2379 case NFA_MOPEN2:
2380 case NFA_MOPEN3:
2381 case NFA_MOPEN4:
2382 case NFA_MOPEN5:
2383 case NFA_MOPEN6:
2384 case NFA_MOPEN7:
2385 case NFA_MOPEN8:
2386 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387 STRCPY(code, "NFA_MOPEN(x)");
2388 code[10] = c - NFA_MOPEN + '0';
2389 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002390 case NFA_MCLOSE:
2391 case NFA_MCLOSE1:
2392 case NFA_MCLOSE2:
2393 case NFA_MCLOSE3:
2394 case NFA_MCLOSE4:
2395 case NFA_MCLOSE5:
2396 case NFA_MCLOSE6:
2397 case NFA_MCLOSE7:
2398 case NFA_MCLOSE8:
2399 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002400 STRCPY(code, "NFA_MCLOSE(x)");
2401 code[11] = c - NFA_MCLOSE + '0';
2402 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002403#ifdef FEAT_SYN_HL
2404 case NFA_ZOPEN:
2405 case NFA_ZOPEN1:
2406 case NFA_ZOPEN2:
2407 case NFA_ZOPEN3:
2408 case NFA_ZOPEN4:
2409 case NFA_ZOPEN5:
2410 case NFA_ZOPEN6:
2411 case NFA_ZOPEN7:
2412 case NFA_ZOPEN8:
2413 case NFA_ZOPEN9:
2414 STRCPY(code, "NFA_ZOPEN(x)");
2415 code[10] = c - NFA_ZOPEN + '0';
2416 break;
2417 case NFA_ZCLOSE:
2418 case NFA_ZCLOSE1:
2419 case NFA_ZCLOSE2:
2420 case NFA_ZCLOSE3:
2421 case NFA_ZCLOSE4:
2422 case NFA_ZCLOSE5:
2423 case NFA_ZCLOSE6:
2424 case NFA_ZCLOSE7:
2425 case NFA_ZCLOSE8:
2426 case NFA_ZCLOSE9:
2427 STRCPY(code, "NFA_ZCLOSE(x)");
2428 code[11] = c - NFA_ZCLOSE + '0';
2429 break;
2430#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2432 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2433 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2434 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002435 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2436 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002437 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2438 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2439 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2440 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2441 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2442 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2443 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2444 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2445 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2446 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2447 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2448 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2449 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2450 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002451 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002452
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002453 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002454 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2455 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2456 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002457 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002459
2460 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2461 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2462 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2463 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2464 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2465 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2466 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2467
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002468 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2469 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2470 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2471 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2472 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2473 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2474 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2475 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2476 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2477 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2478 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2479 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2480 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2481 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2482 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2483 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2484
2485 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2486 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2487 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2488 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2489 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2490 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2491 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2492 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2493 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2494 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2495 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2496 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2497 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2498 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2499 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2500 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2501 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2502 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2503 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2504 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2505 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2506 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2507 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2508 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2509 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2510 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2511 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002512 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2513 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2514 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2515 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002516
2517 default:
2518 STRCPY(code, "CHAR(x)");
2519 code[5] = c;
2520 }
2521
2522 if (addnl == TRUE)
2523 STRCAT(code, " + NEWLINE ");
2524
2525}
2526
2527#ifdef ENABLE_LOG
2528static FILE *log_fd;
2529
2530/*
2531 * Print the postfix notation of the current regexp.
2532 */
2533 static void
2534nfa_postfix_dump(expr, retval)
2535 char_u *expr;
2536 int retval;
2537{
2538 int *p;
2539 FILE *f;
2540
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002541 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002542 if (f != NULL)
2543 {
2544 fprintf(f, "\n-------------------------\n");
2545 if (retval == FAIL)
2546 fprintf(f, ">>> NFA engine failed ... \n");
2547 else if (retval == OK)
2548 fprintf(f, ">>> NFA engine succeeded !\n");
2549 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002550 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002551 {
2552 nfa_set_code(*p);
2553 fprintf(f, "%s, ", code);
2554 }
2555 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002556 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002557 fprintf(f, "%d ", *p);
2558 fprintf(f, "\n\n");
2559 fclose(f);
2560 }
2561}
2562
2563/*
2564 * Print the NFA starting with a root node "state".
2565 */
2566 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002567nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002568 FILE *debugf;
2569 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002570{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002571 garray_T indent;
2572
2573 ga_init2(&indent, 1, 64);
2574 ga_append(&indent, '\0');
2575 nfa_print_state2(debugf, state, &indent);
2576 ga_clear(&indent);
2577}
2578
2579 static void
2580nfa_print_state2(debugf, state, indent)
2581 FILE *debugf;
2582 nfa_state_T *state;
2583 garray_T *indent;
2584{
2585 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002586
2587 if (state == NULL)
2588 return;
2589
2590 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002591
2592 /* Output indent */
2593 p = (char_u *)indent->ga_data;
2594 if (indent->ga_len >= 3)
2595 {
2596 int last = indent->ga_len - 3;
2597 char_u save[2];
2598
2599 STRNCPY(save, &p[last], 2);
2600 STRNCPY(&p[last], "+-", 2);
2601 fprintf(debugf, " %s", p);
2602 STRNCPY(&p[last], save, 2);
2603 }
2604 else
2605 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002606
2607 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002608 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002609 code,
2610 state->c,
2611 abs(state->id),
2612 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002613 if (state->id < 0)
2614 return;
2615
2616 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002617
2618 /* grow indent for state->out */
2619 indent->ga_len -= 1;
2620 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002621 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002622 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002623 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002624 ga_append(indent, '\0');
2625
2626 nfa_print_state2(debugf, state->out, indent);
2627
2628 /* replace last part of indent for state->out1 */
2629 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002630 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002631 ga_append(indent, '\0');
2632
2633 nfa_print_state2(debugf, state->out1, indent);
2634
2635 /* shrink indent */
2636 indent->ga_len -= 3;
2637 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638}
2639
2640/*
2641 * Print the NFA state machine.
2642 */
2643 static void
2644nfa_dump(prog)
2645 nfa_regprog_T *prog;
2646{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002647 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648
2649 if (debugf != NULL)
2650 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002651 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002652
Bram Moolenaar473de612013-06-08 18:19:48 +02002653 if (prog->reganch)
2654 fprintf(debugf, "reganch: %d\n", prog->reganch);
2655 if (prog->regstart != NUL)
2656 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2657 prog->regstart, prog->regstart);
2658 if (prog->match_text != NULL)
2659 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002660
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002661 fclose(debugf);
2662 }
2663}
2664#endif /* ENABLE_LOG */
2665#endif /* DEBUG */
2666
2667/*
2668 * Parse r.e. @expr and convert it into postfix form.
2669 * Return the postfix string on success, NULL otherwise.
2670 */
2671 static int *
2672re2post()
2673{
2674 if (nfa_reg(REG_NOPAREN) == FAIL)
2675 return NULL;
2676 EMIT(NFA_MOPEN);
2677 return post_start;
2678}
2679
2680/* NB. Some of the code below is inspired by Russ's. */
2681
2682/*
2683 * Represents an NFA state plus zero or one or two arrows exiting.
2684 * if c == MATCH, no arrows out; matching state.
2685 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2686 * If c < 256, labeled arrow with character c to out.
2687 */
2688
2689static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2690
2691/*
2692 * Allocate and initialize nfa_state_T.
2693 */
2694 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002695alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696 int c;
2697 nfa_state_T *out;
2698 nfa_state_T *out1;
2699{
2700 nfa_state_T *s;
2701
2702 if (istate >= nstate)
2703 return NULL;
2704
2705 s = &state_ptr[istate++];
2706
2707 s->c = c;
2708 s->out = out;
2709 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002710 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002711
2712 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002713 s->lastlist[0] = 0;
2714 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715
2716 return s;
2717}
2718
2719/*
2720 * A partially built NFA without the matching state filled in.
2721 * Frag_T.start points at the start state.
2722 * Frag_T.out is a list of places that need to be set to the
2723 * next state for this fragment.
2724 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002725
2726/* Since the out pointers in the list are always
2727 * uninitialized, we use the pointers themselves
2728 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002729typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002730union Ptrlist
2731{
2732 Ptrlist *next;
2733 nfa_state_T *s;
2734};
2735
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002736struct Frag
2737{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002738 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739 Ptrlist *out;
2740};
2741typedef struct Frag Frag_T;
2742
2743static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2744static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2745static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2746static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2747static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2748static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2749
2750/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002751 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752 */
2753 static Frag_T
2754frag(start, out)
2755 nfa_state_T *start;
2756 Ptrlist *out;
2757{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002758 Frag_T n;
2759
2760 n.start = start;
2761 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762 return n;
2763}
2764
2765/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002766 * Create singleton list containing just outp.
2767 */
2768 static Ptrlist *
2769list1(outp)
2770 nfa_state_T **outp;
2771{
2772 Ptrlist *l;
2773
2774 l = (Ptrlist *)outp;
2775 l->next = NULL;
2776 return l;
2777}
2778
2779/*
2780 * Patch the list of states at out to point to start.
2781 */
2782 static void
2783patch(l, s)
2784 Ptrlist *l;
2785 nfa_state_T *s;
2786{
2787 Ptrlist *next;
2788
2789 for (; l; l = next)
2790 {
2791 next = l->next;
2792 l->s = s;
2793 }
2794}
2795
2796
2797/*
2798 * Join the two lists l1 and l2, returning the combination.
2799 */
2800 static Ptrlist *
2801append(l1, l2)
2802 Ptrlist *l1;
2803 Ptrlist *l2;
2804{
2805 Ptrlist *oldl1;
2806
2807 oldl1 = l1;
2808 while (l1->next)
2809 l1 = l1->next;
2810 l1->next = l2;
2811 return oldl1;
2812}
2813
2814/*
2815 * Stack used for transforming postfix form into NFA.
2816 */
2817static Frag_T empty;
2818
2819 static void
2820st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002821 int *postfix UNUSED;
2822 int *end UNUSED;
2823 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002825#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826 FILE *df;
2827 int *p2;
2828
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002829 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002830 if (df)
2831 {
2832 fprintf(df, "Error popping the stack!\n");
2833#ifdef DEBUG
2834 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2835#endif
2836 fprintf(df, "Postfix form is: ");
2837#ifdef DEBUG
2838 for (p2 = postfix; p2 < end; p2++)
2839 {
2840 nfa_set_code(*p2);
2841 fprintf(df, "%s, ", code);
2842 }
2843 nfa_set_code(*p);
2844 fprintf(df, "\nCurrent position is: ");
2845 for (p2 = postfix; p2 <= p; p2 ++)
2846 {
2847 nfa_set_code(*p2);
2848 fprintf(df, "%s, ", code);
2849 }
2850#else
2851 for (p2 = postfix; p2 < end; p2++)
2852 {
2853 fprintf(df, "%d, ", *p2);
2854 }
2855 fprintf(df, "\nCurrent position is: ");
2856 for (p2 = postfix; p2 <= p; p2 ++)
2857 {
2858 fprintf(df, "%d, ", *p2);
2859 }
2860#endif
2861 fprintf(df, "\n--------------------------\n");
2862 fclose(df);
2863 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002864#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002865 EMSG(_("E874: (NFA) Could not pop the stack !"));
2866}
2867
2868/*
2869 * Push an item onto the stack.
2870 */
2871 static void
2872st_push(s, p, stack_end)
2873 Frag_T s;
2874 Frag_T **p;
2875 Frag_T *stack_end;
2876{
2877 Frag_T *stackp = *p;
2878
2879 if (stackp >= stack_end)
2880 return;
2881 *stackp = s;
2882 *p = *p + 1;
2883}
2884
2885/*
2886 * Pop an item from the stack.
2887 */
2888 static Frag_T
2889st_pop(p, stack)
2890 Frag_T **p;
2891 Frag_T *stack;
2892{
2893 Frag_T *stackp;
2894
2895 *p = *p - 1;
2896 stackp = *p;
2897 if (stackp < stack)
2898 return empty;
2899 return **p;
2900}
2901
2902/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002903 * Estimate the maximum byte length of anything matching "state".
2904 * When unknown or unlimited return -1.
2905 */
2906 static int
2907nfa_max_width(startstate, depth)
2908 nfa_state_T *startstate;
2909 int depth;
2910{
2911 int l, r;
2912 nfa_state_T *state = startstate;
2913 int len = 0;
2914
2915 /* detect looping in a NFA_SPLIT */
2916 if (depth > 4)
2917 return -1;
2918
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002919 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002920 {
2921 switch (state->c)
2922 {
2923 case NFA_END_INVISIBLE:
2924 case NFA_END_INVISIBLE_NEG:
2925 /* the end, return what we have */
2926 return len;
2927
2928 case NFA_SPLIT:
2929 /* two alternatives, use the maximum */
2930 l = nfa_max_width(state->out, depth + 1);
2931 r = nfa_max_width(state->out1, depth + 1);
2932 if (l < 0 || r < 0)
2933 return -1;
2934 return len + (l > r ? l : r);
2935
2936 case NFA_ANY:
2937 case NFA_START_COLL:
2938 case NFA_START_NEG_COLL:
2939 /* matches some character, including composing chars */
2940#ifdef FEAT_MBYTE
2941 if (enc_utf8)
2942 len += MB_MAXBYTES;
2943 else if (has_mbyte)
2944 len += 2;
2945 else
2946#endif
2947 ++len;
2948 if (state->c != NFA_ANY)
2949 {
2950 /* skip over the characters */
2951 state = state->out1->out;
2952 continue;
2953 }
2954 break;
2955
2956 case NFA_DIGIT:
2957 case NFA_WHITE:
2958 case NFA_HEX:
2959 case NFA_OCTAL:
2960 /* ascii */
2961 ++len;
2962 break;
2963
2964 case NFA_IDENT:
2965 case NFA_SIDENT:
2966 case NFA_KWORD:
2967 case NFA_SKWORD:
2968 case NFA_FNAME:
2969 case NFA_SFNAME:
2970 case NFA_PRINT:
2971 case NFA_SPRINT:
2972 case NFA_NWHITE:
2973 case NFA_NDIGIT:
2974 case NFA_NHEX:
2975 case NFA_NOCTAL:
2976 case NFA_WORD:
2977 case NFA_NWORD:
2978 case NFA_HEAD:
2979 case NFA_NHEAD:
2980 case NFA_ALPHA:
2981 case NFA_NALPHA:
2982 case NFA_LOWER:
2983 case NFA_NLOWER:
2984 case NFA_UPPER:
2985 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002986 case NFA_LOWER_IC:
2987 case NFA_NLOWER_IC:
2988 case NFA_UPPER_IC:
2989 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002990 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002991 /* possibly non-ascii */
2992#ifdef FEAT_MBYTE
2993 if (has_mbyte)
2994 len += 3;
2995 else
2996#endif
2997 ++len;
2998 break;
2999
3000 case NFA_START_INVISIBLE:
3001 case NFA_START_INVISIBLE_NEG:
3002 case NFA_START_INVISIBLE_BEFORE:
3003 case NFA_START_INVISIBLE_BEFORE_NEG:
3004 /* zero-width, out1 points to the END state */
3005 state = state->out1->out;
3006 continue;
3007
3008 case NFA_BACKREF1:
3009 case NFA_BACKREF2:
3010 case NFA_BACKREF3:
3011 case NFA_BACKREF4:
3012 case NFA_BACKREF5:
3013 case NFA_BACKREF6:
3014 case NFA_BACKREF7:
3015 case NFA_BACKREF8:
3016 case NFA_BACKREF9:
3017#ifdef FEAT_SYN_HL
3018 case NFA_ZREF1:
3019 case NFA_ZREF2:
3020 case NFA_ZREF3:
3021 case NFA_ZREF4:
3022 case NFA_ZREF5:
3023 case NFA_ZREF6:
3024 case NFA_ZREF7:
3025 case NFA_ZREF8:
3026 case NFA_ZREF9:
3027#endif
3028 case NFA_NEWL:
3029 case NFA_SKIP:
3030 /* unknown width */
3031 return -1;
3032
3033 case NFA_BOL:
3034 case NFA_EOL:
3035 case NFA_BOF:
3036 case NFA_EOF:
3037 case NFA_BOW:
3038 case NFA_EOW:
3039 case NFA_MOPEN:
3040 case NFA_MOPEN1:
3041 case NFA_MOPEN2:
3042 case NFA_MOPEN3:
3043 case NFA_MOPEN4:
3044 case NFA_MOPEN5:
3045 case NFA_MOPEN6:
3046 case NFA_MOPEN7:
3047 case NFA_MOPEN8:
3048 case NFA_MOPEN9:
3049#ifdef FEAT_SYN_HL
3050 case NFA_ZOPEN:
3051 case NFA_ZOPEN1:
3052 case NFA_ZOPEN2:
3053 case NFA_ZOPEN3:
3054 case NFA_ZOPEN4:
3055 case NFA_ZOPEN5:
3056 case NFA_ZOPEN6:
3057 case NFA_ZOPEN7:
3058 case NFA_ZOPEN8:
3059 case NFA_ZOPEN9:
3060 case NFA_ZCLOSE:
3061 case NFA_ZCLOSE1:
3062 case NFA_ZCLOSE2:
3063 case NFA_ZCLOSE3:
3064 case NFA_ZCLOSE4:
3065 case NFA_ZCLOSE5:
3066 case NFA_ZCLOSE6:
3067 case NFA_ZCLOSE7:
3068 case NFA_ZCLOSE8:
3069 case NFA_ZCLOSE9:
3070#endif
3071 case NFA_MCLOSE:
3072 case NFA_MCLOSE1:
3073 case NFA_MCLOSE2:
3074 case NFA_MCLOSE3:
3075 case NFA_MCLOSE4:
3076 case NFA_MCLOSE5:
3077 case NFA_MCLOSE6:
3078 case NFA_MCLOSE7:
3079 case NFA_MCLOSE8:
3080 case NFA_MCLOSE9:
3081 case NFA_NOPEN:
3082 case NFA_NCLOSE:
3083
3084 case NFA_LNUM_GT:
3085 case NFA_LNUM_LT:
3086 case NFA_COL_GT:
3087 case NFA_COL_LT:
3088 case NFA_VCOL_GT:
3089 case NFA_VCOL_LT:
3090 case NFA_MARK_GT:
3091 case NFA_MARK_LT:
3092 case NFA_VISUAL:
3093 case NFA_LNUM:
3094 case NFA_CURSOR:
3095 case NFA_COL:
3096 case NFA_VCOL:
3097 case NFA_MARK:
3098
3099 case NFA_ZSTART:
3100 case NFA_ZEND:
3101 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003102 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003103 case NFA_START_PATTERN:
3104 case NFA_END_PATTERN:
3105 case NFA_COMPOSING:
3106 case NFA_END_COMPOSING:
3107 /* zero-width */
3108 break;
3109
3110 default:
3111 if (state->c < 0)
3112 /* don't know what this is */
3113 return -1;
3114 /* normal character */
3115 len += MB_CHAR2LEN(state->c);
3116 break;
3117 }
3118
3119 /* normal way to continue */
3120 state = state->out;
3121 }
3122
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003123 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003124 return -1;
3125}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003126
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003127/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003128 * Convert a postfix form into its equivalent NFA.
3129 * Return the NFA start state on success, NULL otherwise.
3130 */
3131 static nfa_state_T *
3132post2nfa(postfix, end, nfa_calc_size)
3133 int *postfix;
3134 int *end;
3135 int nfa_calc_size;
3136{
3137 int *p;
3138 int mopen;
3139 int mclose;
3140 Frag_T *stack = NULL;
3141 Frag_T *stackp = NULL;
3142 Frag_T *stack_end = NULL;
3143 Frag_T e1;
3144 Frag_T e2;
3145 Frag_T e;
3146 nfa_state_T *s;
3147 nfa_state_T *s1;
3148 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003149 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003150
3151 if (postfix == NULL)
3152 return NULL;
3153
Bram Moolenaar053bb602013-05-20 13:55:21 +02003154#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003155#define POP() st_pop(&stackp, stack); \
3156 if (stackp < stack) \
3157 { \
3158 st_error(postfix, end, p); \
3159 return NULL; \
3160 }
3161
3162 if (nfa_calc_size == FALSE)
3163 {
3164 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003165 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003166 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003167 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003168 }
3169
3170 for (p = postfix; p < end; ++p)
3171 {
3172 switch (*p)
3173 {
3174 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003175 /* Concatenation.
3176 * Pay attention: this operator does not exist in the r.e. itself
3177 * (it is implicit, really). It is added when r.e. is translated
3178 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 if (nfa_calc_size == TRUE)
3180 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003181 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003182 break;
3183 }
3184 e2 = POP();
3185 e1 = POP();
3186 patch(e1.out, e2.start);
3187 PUSH(frag(e1.start, e2.out));
3188 break;
3189
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003190 case NFA_OR:
3191 /* Alternation */
3192 if (nfa_calc_size == TRUE)
3193 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003194 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 break;
3196 }
3197 e2 = POP();
3198 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003199 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003201 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202 PUSH(frag(s, append(e1.out, e2.out)));
3203 break;
3204
3205 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003206 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207 if (nfa_calc_size == TRUE)
3208 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003209 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003210 break;
3211 }
3212 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003213 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003214 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003215 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216 patch(e.out, s);
3217 PUSH(frag(s, list1(&s->out1)));
3218 break;
3219
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003220 case NFA_STAR_NONGREEDY:
3221 /* Zero or more, prefer zero */
3222 if (nfa_calc_size == TRUE)
3223 {
3224 nstate++;
3225 break;
3226 }
3227 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003228 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003229 if (s == NULL)
3230 goto theend;
3231 patch(e.out, s);
3232 PUSH(frag(s, list1(&s->out)));
3233 break;
3234
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003235 case NFA_QUEST:
3236 /* one or zero atoms=> greedy match */
3237 if (nfa_calc_size == TRUE)
3238 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003239 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 break;
3241 }
3242 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003243 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003245 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003246 PUSH(frag(s, append(e.out, list1(&s->out1))));
3247 break;
3248
3249 case NFA_QUEST_NONGREEDY:
3250 /* zero or one atoms => non-greedy match */
3251 if (nfa_calc_size == TRUE)
3252 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003253 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 break;
3255 }
3256 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003257 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003258 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003259 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003260 PUSH(frag(s, append(e.out, list1(&s->out))));
3261 break;
3262
Bram Moolenaar417bad22013-06-07 14:08:30 +02003263 case NFA_END_COLL:
3264 case NFA_END_NEG_COLL:
3265 /* On the stack is the sequence starting with NFA_START_COLL or
3266 * NFA_START_NEG_COLL and all possible characters. Patch it to
3267 * add the output to the start. */
3268 if (nfa_calc_size == TRUE)
3269 {
3270 nstate++;
3271 break;
3272 }
3273 e = POP();
3274 s = alloc_state(NFA_END_COLL, NULL, NULL);
3275 if (s == NULL)
3276 goto theend;
3277 patch(e.out, s);
3278 e.start->out1 = s;
3279 PUSH(frag(e.start, list1(&s->out)));
3280 break;
3281
3282 case NFA_RANGE:
3283 /* Before this are two characters, the low and high end of a
3284 * range. Turn them into two states with MIN and MAX. */
3285 if (nfa_calc_size == TRUE)
3286 {
3287 /* nstate += 0; */
3288 break;
3289 }
3290 e2 = POP();
3291 e1 = POP();
3292 e2.start->val = e2.start->c;
3293 e2.start->c = NFA_RANGE_MAX;
3294 e1.start->val = e1.start->c;
3295 e1.start->c = NFA_RANGE_MIN;
3296 patch(e1.out, e2.start);
3297 PUSH(frag(e1.start, e2.out));
3298 break;
3299
Bram Moolenaar699c1202013-09-25 16:41:54 +02003300 case NFA_EMPTY:
3301 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 if (nfa_calc_size == TRUE)
3303 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003304 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 break;
3306 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003307 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003309 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 PUSH(frag(s, list1(&s->out)));
3311 break;
3312
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003313 case NFA_OPT_CHARS:
3314 {
3315 int n;
3316
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003317 /* \%[abc] implemented as:
3318 * NFA_SPLIT
3319 * +-CHAR(a)
3320 * | +-NFA_SPLIT
3321 * | +-CHAR(b)
3322 * | | +-NFA_SPLIT
3323 * | | +-CHAR(c)
3324 * | | | +-next
3325 * | | +- next
3326 * | +- next
3327 * +- next
3328 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003329 n = *++p; /* get number of characters */
3330 if (nfa_calc_size == TRUE)
3331 {
3332 nstate += n;
3333 break;
3334 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003335 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003336 e1.out = NULL; /* stores list with out1's */
3337 s1 = NULL; /* previous NFA_SPLIT to connect to */
3338 while (n-- > 0)
3339 {
3340 e = POP(); /* get character */
3341 s = alloc_state(NFA_SPLIT, e.start, NULL);
3342 if (s == NULL)
3343 goto theend;
3344 if (e1.out == NULL)
3345 e1 = e;
3346 patch(e.out, s1);
3347 append(e1.out, list1(&s->out1));
3348 s1 = s;
3349 }
3350 PUSH(frag(s, e1.out));
3351 break;
3352 }
3353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003355 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003356 case NFA_PREV_ATOM_JUST_BEFORE:
3357 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003358 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003359 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003360 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3361 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003362 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003363 int start_state;
3364 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003365 int n = 0;
3366 nfa_state_T *zend;
3367 nfa_state_T *skip;
3368
Bram Moolenaardecd9542013-06-07 16:31:50 +02003369 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003370 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003371 case NFA_PREV_ATOM_NO_WIDTH:
3372 start_state = NFA_START_INVISIBLE;
3373 end_state = NFA_END_INVISIBLE;
3374 break;
3375 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3376 start_state = NFA_START_INVISIBLE_NEG;
3377 end_state = NFA_END_INVISIBLE_NEG;
3378 break;
3379 case NFA_PREV_ATOM_JUST_BEFORE:
3380 start_state = NFA_START_INVISIBLE_BEFORE;
3381 end_state = NFA_END_INVISIBLE;
3382 break;
3383 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3384 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3385 end_state = NFA_END_INVISIBLE_NEG;
3386 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003387 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003388 start_state = NFA_START_PATTERN;
3389 end_state = NFA_END_PATTERN;
3390 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003391 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003392
3393 if (before)
3394 n = *++p; /* get the count */
3395
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003396 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003397 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003398 * The \@<= operator: match for the preceding atom.
3399 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003401 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402
3403 if (nfa_calc_size == TRUE)
3404 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003405 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003406 break;
3407 }
3408 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003409 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003410 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003411 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412
Bram Moolenaar87953742013-06-05 18:52:40 +02003413 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003414 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003415 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003416 if (pattern)
3417 {
3418 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3419 skip = alloc_state(NFA_SKIP, NULL, NULL);
3420 zend = alloc_state(NFA_ZEND, s1, NULL);
3421 s1->out= skip;
3422 patch(e.out, zend);
3423 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003424 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003425 else
3426 {
3427 patch(e.out, s1);
3428 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003429 if (before)
3430 {
3431 if (n <= 0)
3432 /* See if we can guess the maximum width, it avoids a
3433 * lot of pointless tries. */
3434 n = nfa_max_width(e.start, 0);
3435 s->val = n; /* store the count */
3436 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003437 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003438 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003439 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003440
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003441#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003442 case NFA_COMPOSING: /* char with composing char */
3443#if 0
3444 /* TODO */
3445 if (regflags & RF_ICOMBINE)
3446 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003447 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003448 }
3449#endif
3450 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003451#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003452
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003453 case NFA_MOPEN: /* \( \) Submatch */
3454 case NFA_MOPEN1:
3455 case NFA_MOPEN2:
3456 case NFA_MOPEN3:
3457 case NFA_MOPEN4:
3458 case NFA_MOPEN5:
3459 case NFA_MOPEN6:
3460 case NFA_MOPEN7:
3461 case NFA_MOPEN8:
3462 case NFA_MOPEN9:
3463#ifdef FEAT_SYN_HL
3464 case NFA_ZOPEN: /* \z( \) Submatch */
3465 case NFA_ZOPEN1:
3466 case NFA_ZOPEN2:
3467 case NFA_ZOPEN3:
3468 case NFA_ZOPEN4:
3469 case NFA_ZOPEN5:
3470 case NFA_ZOPEN6:
3471 case NFA_ZOPEN7:
3472 case NFA_ZOPEN8:
3473 case NFA_ZOPEN9:
3474#endif
3475 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003476 if (nfa_calc_size == TRUE)
3477 {
3478 nstate += 2;
3479 break;
3480 }
3481
3482 mopen = *p;
3483 switch (*p)
3484 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003485 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3486#ifdef FEAT_SYN_HL
3487 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3488 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3489 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3490 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3491 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3492 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3493 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3494 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3495 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3496 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3497#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003498#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003499 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003500#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003501 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003502 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503 mclose = *p + NSUBEXP;
3504 break;
3505 }
3506
3507 /* Allow "NFA_MOPEN" as a valid postfix representation for
3508 * the empty regexp "". In this case, the NFA will be
3509 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3510 * empty groups of parenthesis, and empty mbyte chars */
3511 if (stackp == stack)
3512 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003513 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003515 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003516 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003517 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003518 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519 patch(list1(&s->out), s1);
3520 PUSH(frag(s, list1(&s1->out)));
3521 break;
3522 }
3523
3524 /* At least one node was emitted before NFA_MOPEN, so
3525 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3526 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003527 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003528 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003529 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530
Bram Moolenaar525666f2013-06-02 16:40:55 +02003531 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003532 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003533 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003534 patch(e.out, s1);
3535
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003536#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003537 if (mopen == NFA_COMPOSING)
3538 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003539 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003540#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003541
3542 PUSH(frag(s, list1(&s1->out)));
3543 break;
3544
Bram Moolenaar5714b802013-05-28 22:03:20 +02003545 case NFA_BACKREF1:
3546 case NFA_BACKREF2:
3547 case NFA_BACKREF3:
3548 case NFA_BACKREF4:
3549 case NFA_BACKREF5:
3550 case NFA_BACKREF6:
3551 case NFA_BACKREF7:
3552 case NFA_BACKREF8:
3553 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003554#ifdef FEAT_SYN_HL
3555 case NFA_ZREF1:
3556 case NFA_ZREF2:
3557 case NFA_ZREF3:
3558 case NFA_ZREF4:
3559 case NFA_ZREF5:
3560 case NFA_ZREF6:
3561 case NFA_ZREF7:
3562 case NFA_ZREF8:
3563 case NFA_ZREF9:
3564#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003565 if (nfa_calc_size == TRUE)
3566 {
3567 nstate += 2;
3568 break;
3569 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003570 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003571 if (s == NULL)
3572 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003573 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003574 if (s1 == NULL)
3575 goto theend;
3576 patch(list1(&s->out), s1);
3577 PUSH(frag(s, list1(&s1->out)));
3578 break;
3579
Bram Moolenaar423532e2013-05-29 21:14:42 +02003580 case NFA_LNUM:
3581 case NFA_LNUM_GT:
3582 case NFA_LNUM_LT:
3583 case NFA_VCOL:
3584 case NFA_VCOL_GT:
3585 case NFA_VCOL_LT:
3586 case NFA_COL:
3587 case NFA_COL_GT:
3588 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003589 case NFA_MARK:
3590 case NFA_MARK_GT:
3591 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003592 {
3593 int n = *++p; /* lnum, col or mark name */
3594
Bram Moolenaar423532e2013-05-29 21:14:42 +02003595 if (nfa_calc_size == TRUE)
3596 {
3597 nstate += 1;
3598 break;
3599 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003600 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003601 if (s == NULL)
3602 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003603 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003604 PUSH(frag(s, list1(&s->out)));
3605 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003606 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003607
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003608 case NFA_ZSTART:
3609 case NFA_ZEND:
3610 default:
3611 /* Operands */
3612 if (nfa_calc_size == TRUE)
3613 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003614 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615 break;
3616 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003617 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003618 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003619 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003620 PUSH(frag(s, list1(&s->out)));
3621 break;
3622
3623 } /* switch(*p) */
3624
3625 } /* for(p = postfix; *p; ++p) */
3626
3627 if (nfa_calc_size == TRUE)
3628 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003629 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003630 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003631 }
3632
3633 e = POP();
3634 if (stackp != stack)
3635 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3636
3637 if (istate >= nstate)
3638 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3639
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640 matchstate = &state_ptr[istate++]; /* the match state */
3641 matchstate->c = NFA_MATCH;
3642 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003643 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644
3645 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003646 ret = e.start;
3647
3648theend:
3649 vim_free(stack);
3650 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651
3652#undef POP1
3653#undef PUSH1
3654#undef POP2
3655#undef PUSH2
3656#undef POP
3657#undef PUSH
3658}
3659
Bram Moolenaara2947e22013-06-11 22:44:09 +02003660/*
3661 * After building the NFA program, inspect it to add optimization hints.
3662 */
3663 static void
3664nfa_postprocess(prog)
3665 nfa_regprog_T *prog;
3666{
3667 int i;
3668 int c;
3669
3670 for (i = 0; i < prog->nstate; ++i)
3671 {
3672 c = prog->state[i].c;
3673 if (c == NFA_START_INVISIBLE
3674 || c == NFA_START_INVISIBLE_NEG
3675 || c == NFA_START_INVISIBLE_BEFORE
3676 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3677 {
3678 int directly;
3679
3680 /* Do it directly when what follows is possibly the end of the
3681 * match. */
3682 if (match_follows(prog->state[i].out1->out, 0))
3683 directly = TRUE;
3684 else
3685 {
3686 int ch_invisible = failure_chance(prog->state[i].out, 0);
3687 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3688
3689 /* Postpone when the invisible match is expensive or has a
3690 * lower chance of failing. */
3691 if (c == NFA_START_INVISIBLE_BEFORE
3692 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3693 {
3694 /* "before" matches are very expensive when
3695 * unbounded, always prefer what follows then,
3696 * unless what follows will always match.
3697 * Otherwise strongly prefer what follows. */
3698 if (prog->state[i].val <= 0 && ch_follows > 0)
3699 directly = FALSE;
3700 else
3701 directly = ch_follows * 10 < ch_invisible;
3702 }
3703 else
3704 {
3705 /* normal invisible, first do the one with the
3706 * highest failure chance */
3707 directly = ch_follows < ch_invisible;
3708 }
3709 }
3710 if (directly)
3711 /* switch to the _FIRST state */
3712 ++prog->state[i].c;
3713 }
3714 }
3715}
3716
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717/****************************************************************
3718 * NFA execution code.
3719 ****************************************************************/
3720
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003721typedef struct
3722{
3723 int in_use; /* number of subexpr with useful info */
3724
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003725 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003726 union
3727 {
3728 struct multipos
3729 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003730 linenr_T start_lnum;
3731 linenr_T end_lnum;
3732 colnr_T start_col;
3733 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003734 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003735 struct linepos
3736 {
3737 char_u *start;
3738 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003739 } line[NSUBEXP];
3740 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003741} regsub_T;
3742
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003743typedef struct
3744{
3745 regsub_T norm; /* \( .. \) matches */
3746#ifdef FEAT_SYN_HL
3747 regsub_T synt; /* \z( .. \) matches */
3748#endif
3749} regsubs_T;
3750
Bram Moolenaara2d95102013-06-04 14:23:05 +02003751/* nfa_pim_T stores a Postponed Invisible Match. */
3752typedef struct nfa_pim_S nfa_pim_T;
3753struct nfa_pim_S
3754{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003755 int result; /* NFA_PIM_*, see below */
3756 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003757 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003758 union
3759 {
3760 lpos_T pos;
3761 char_u *ptr;
3762 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003763};
3764
3765/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003766#define NFA_PIM_UNUSED 0 /* pim not used */
3767#define NFA_PIM_TODO 1 /* pim not done yet */
3768#define NFA_PIM_MATCH 2 /* pim executed, matches */
3769#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003770
3771
Bram Moolenaar963fee22013-05-26 21:47:28 +02003772/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003773typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774{
3775 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003776 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003777 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3778 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003779 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003780} nfa_thread_T;
3781
Bram Moolenaar963fee22013-05-26 21:47:28 +02003782/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003783typedef struct
3784{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003785 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003786 int n; /* nr of states currently in "t" */
3787 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003788 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003789 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003790} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003791
Bram Moolenaar5714b802013-05-28 22:03:20 +02003792#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003793static void log_subsexpr __ARGS((regsubs_T *subs));
3794static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003795static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003796
3797 static void
3798log_subsexpr(subs)
3799 regsubs_T *subs;
3800{
3801 log_subexpr(&subs->norm);
3802# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003803 if (nfa_has_zsubexpr)
3804 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003805# endif
3806}
3807
Bram Moolenaar5714b802013-05-28 22:03:20 +02003808 static void
3809log_subexpr(sub)
3810 regsub_T *sub;
3811{
3812 int j;
3813
3814 for (j = 0; j < sub->in_use; j++)
3815 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003816 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003817 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003818 sub->list.multi[j].start_col,
3819 (int)sub->list.multi[j].start_lnum,
3820 sub->list.multi[j].end_col,
3821 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003822 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003823 {
3824 char *s = (char *)sub->list.line[j].start;
3825 char *e = (char *)sub->list.line[j].end;
3826
Bram Moolenaar87953742013-06-05 18:52:40 +02003827 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003828 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003829 s == NULL ? "NULL" : s,
3830 e == NULL ? "NULL" : e);
3831 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003832}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003833
3834 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003835pim_info(pim)
3836 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003837{
3838 static char buf[30];
3839
3840 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3841 buf[0] = NUL;
3842 else
3843 {
3844 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3845 : (int)(pim->end.ptr - reginput));
3846 }
3847 return buf;
3848}
3849
Bram Moolenaar5714b802013-05-28 22:03:20 +02003850#endif
3851
Bram Moolenaar963fee22013-05-26 21:47:28 +02003852/* Used during execution: whether a match has been found. */
3853static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003854#ifdef FEAT_RELTIME
3855static proftime_T *nfa_time_limit;
3856static int nfa_time_count;
3857#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003858
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003859static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003860static void clear_sub __ARGS((regsub_T *sub));
3861static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3862static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaarf2118842013-09-25 18:16:38 +02003863static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003864static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003865static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003866static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3867static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003868static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003869static 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 +02003870static 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 +02003871
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003872/*
3873 * Copy postponed invisible match info from "from" to "to".
3874 */
3875 static void
3876copy_pim(to, from)
3877 nfa_pim_T *to;
3878 nfa_pim_T *from;
3879{
3880 to->result = from->result;
3881 to->state = from->state;
3882 copy_sub(&to->subs.norm, &from->subs.norm);
3883#ifdef FEAT_SYN_HL
3884 if (nfa_has_zsubexpr)
3885 copy_sub(&to->subs.synt, &from->subs.synt);
3886#endif
3887 to->end = from->end;
3888}
3889
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003890 static void
3891clear_sub(sub)
3892 regsub_T *sub;
3893{
3894 if (REG_MULTI)
3895 /* Use 0xff to set lnum to -1 */
3896 vim_memset(sub->list.multi, 0xff,
3897 sizeof(struct multipos) * nfa_nsubexpr);
3898 else
3899 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3900 sub->in_use = 0;
3901}
3902
3903/*
3904 * Copy the submatches from "from" to "to".
3905 */
3906 static void
3907copy_sub(to, from)
3908 regsub_T *to;
3909 regsub_T *from;
3910{
3911 to->in_use = from->in_use;
3912 if (from->in_use > 0)
3913 {
3914 /* Copy the match start and end positions. */
3915 if (REG_MULTI)
3916 mch_memmove(&to->list.multi[0],
3917 &from->list.multi[0],
3918 sizeof(struct multipos) * from->in_use);
3919 else
3920 mch_memmove(&to->list.line[0],
3921 &from->list.line[0],
3922 sizeof(struct linepos) * from->in_use);
3923 }
3924}
3925
3926/*
3927 * Like copy_sub() but exclude the main match.
3928 */
3929 static void
3930copy_sub_off(to, from)
3931 regsub_T *to;
3932 regsub_T *from;
3933{
3934 if (to->in_use < from->in_use)
3935 to->in_use = from->in_use;
3936 if (from->in_use > 1)
3937 {
3938 /* Copy the match start and end positions. */
3939 if (REG_MULTI)
3940 mch_memmove(&to->list.multi[1],
3941 &from->list.multi[1],
3942 sizeof(struct multipos) * (from->in_use - 1));
3943 else
3944 mch_memmove(&to->list.line[1],
3945 &from->list.line[1],
3946 sizeof(struct linepos) * (from->in_use - 1));
3947 }
3948}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003949
Bram Moolenaar428e9872013-05-30 17:05:39 +02003950/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003951 * Like copy_sub() but only do the end of the main match if \ze is present.
3952 */
3953 static void
3954copy_ze_off(to, from)
3955 regsub_T *to;
3956 regsub_T *from;
3957{
3958 if (nfa_has_zend)
3959 {
3960 if (REG_MULTI)
3961 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003962 if (from->list.multi[0].end_lnum >= 0)
3963 {
3964 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3965 to->list.multi[0].end_col = from->list.multi[0].end_col;
3966 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02003967 }
3968 else
3969 {
3970 if (from->list.line[0].end != NULL)
3971 to->list.line[0].end = from->list.line[0].end;
3972 }
3973 }
3974}
3975
3976/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003977 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003978 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003979 */
3980 static int
3981sub_equal(sub1, sub2)
3982 regsub_T *sub1;
3983 regsub_T *sub2;
3984{
3985 int i;
3986 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003987 linenr_T s1;
3988 linenr_T s2;
3989 char_u *sp1;
3990 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003991
3992 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3993 if (REG_MULTI)
3994 {
3995 for (i = 0; i < todo; ++i)
3996 {
3997 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003998 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003999 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004000 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004001 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004002 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004003 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004004 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004005 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004006 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004007 if (s1 != -1 && sub1->list.multi[i].start_col
4008 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004009 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004010
4011 if (nfa_has_backref)
4012 {
4013 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004014 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004015 else
4016 s1 = -1;
4017 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004018 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004019 else
4020 s2 = -1;
4021 if (s1 != s2)
4022 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004023 if (s1 != -1 && sub1->list.multi[i].end_col
4024 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004025 return FALSE;
4026 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004027 }
4028 }
4029 else
4030 {
4031 for (i = 0; i < todo; ++i)
4032 {
4033 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004034 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004035 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004036 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004038 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004039 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004040 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004041 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004042 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004043 if (nfa_has_backref)
4044 {
4045 if (i < sub1->in_use)
4046 sp1 = sub1->list.line[i].end;
4047 else
4048 sp1 = NULL;
4049 if (i < sub2->in_use)
4050 sp2 = sub2->list.line[i].end;
4051 else
4052 sp2 = NULL;
4053 if (sp1 != sp2)
4054 return FALSE;
4055 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004056 }
4057 }
4058
4059 return TRUE;
4060}
4061
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004062#ifdef ENABLE_LOG
4063 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004064report_state(char *action,
4065 regsub_T *sub,
4066 nfa_state_T *state,
4067 int lid,
4068 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004069{
4070 int col;
4071
4072 if (sub->in_use <= 0)
4073 col = -1;
4074 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004075 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004076 else
4077 col = (int)(sub->list.line[0].start - regline);
4078 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004079 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4080 action, abs(state->id), lid, state->c, code, col,
4081 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004082}
4083#endif
4084
Bram Moolenaar43e02982013-06-07 17:31:29 +02004085/*
4086 * Return TRUE if the same state is already in list "l" with the same
4087 * positions as "subs".
4088 */
4089 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02004090has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02004091 nfa_list_T *l; /* runtime state list */
4092 nfa_state_T *state; /* state to update */
4093 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004094 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004095{
4096 nfa_thread_T *thread;
4097 int i;
4098
4099 for (i = 0; i < l->n; ++i)
4100 {
4101 thread = &l->t[i];
4102 if (thread->state->id == state->id
4103 && sub_equal(&thread->subs.norm, &subs->norm)
4104#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004105 && (!nfa_has_zsubexpr
4106 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004107#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004108 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004109 return TRUE;
4110 }
4111 return FALSE;
4112}
4113
4114/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004115 * Return TRUE if "one" and "two" are equal. That includes when both are not
4116 * set.
4117 */
4118 static int
4119pim_equal(one, two)
4120 nfa_pim_T *one;
4121 nfa_pim_T *two;
4122{
4123 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4124 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4125
4126 if (one_unused)
4127 /* one is unused: equal when two is also unused */
4128 return two_unused;
4129 if (two_unused)
4130 /* one is used and two is not: not equal */
4131 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004132 /* compare the state id */
4133 if (one->state->id != two->state->id)
4134 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004135 /* compare the position */
4136 if (REG_MULTI)
4137 return one->end.pos.lnum == two->end.pos.lnum
4138 && one->end.pos.col == two->end.pos.col;
4139 return one->end.ptr == two->end.ptr;
4140}
4141
4142/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004143 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4144 */
4145 static int
4146match_follows(startstate, depth)
4147 nfa_state_T *startstate;
4148 int depth;
4149{
4150 nfa_state_T *state = startstate;
4151
4152 /* avoid too much recursion */
4153 if (depth > 10)
4154 return FALSE;
4155
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004156 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004157 {
4158 switch (state->c)
4159 {
4160 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004161 case NFA_MCLOSE:
4162 case NFA_END_INVISIBLE:
4163 case NFA_END_INVISIBLE_NEG:
4164 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004165 return TRUE;
4166
4167 case NFA_SPLIT:
4168 return match_follows(state->out, depth + 1)
4169 || match_follows(state->out1, depth + 1);
4170
4171 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004172 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004173 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004174 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004175 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004176 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004177 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004178 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004179 case NFA_COMPOSING:
4180 /* skip ahead to next state */
4181 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004182 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004183
4184 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004185 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004186 case NFA_IDENT:
4187 case NFA_SIDENT:
4188 case NFA_KWORD:
4189 case NFA_SKWORD:
4190 case NFA_FNAME:
4191 case NFA_SFNAME:
4192 case NFA_PRINT:
4193 case NFA_SPRINT:
4194 case NFA_WHITE:
4195 case NFA_NWHITE:
4196 case NFA_DIGIT:
4197 case NFA_NDIGIT:
4198 case NFA_HEX:
4199 case NFA_NHEX:
4200 case NFA_OCTAL:
4201 case NFA_NOCTAL:
4202 case NFA_WORD:
4203 case NFA_NWORD:
4204 case NFA_HEAD:
4205 case NFA_NHEAD:
4206 case NFA_ALPHA:
4207 case NFA_NALPHA:
4208 case NFA_LOWER:
4209 case NFA_NLOWER:
4210 case NFA_UPPER:
4211 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004212 case NFA_LOWER_IC:
4213 case NFA_NLOWER_IC:
4214 case NFA_UPPER_IC:
4215 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004216 case NFA_START_COLL:
4217 case NFA_START_NEG_COLL:
4218 case NFA_NEWL:
4219 /* state will advance input */
4220 return FALSE;
4221
4222 default:
4223 if (state->c > 0)
4224 /* state will advance input */
4225 return FALSE;
4226
4227 /* Others: zero-width or possibly zero-width, might still find
4228 * a match at the same position, keep looking. */
4229 break;
4230 }
4231 state = state->out;
4232 }
4233 return FALSE;
4234}
4235
4236
4237/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004238 * Return TRUE if "state" is already in list "l".
4239 */
4240 static int
4241state_in_list(l, state, subs)
4242 nfa_list_T *l; /* runtime state list */
4243 nfa_state_T *state; /* state to update */
4244 regsubs_T *subs; /* pointers to subexpressions */
4245{
4246 if (state->lastlist[nfa_ll_index] == l->id)
4247 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004248 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004249 return TRUE;
4250 }
4251 return FALSE;
4252}
4253
Bram Moolenaard05bf562013-06-30 23:24:08 +02004254/*
4255 * Add "state" and possibly what follows to state list ".".
4256 * Returns "subs_arg", possibly copied into temp_subs.
4257 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004258 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 Moolenaara1d2c582015-02-10 18:18:17 +01004395 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004396 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4397 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004398 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004399
4400 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004401 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004402 thread = &l->t[l->n++];
4403 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004404 if (pim == NULL)
4405 thread->pim.result = NFA_PIM_UNUSED;
4406 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004407 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004408 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004409 l->has_pim = TRUE;
4410 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004411 copy_sub(&thread->subs.norm, &subs->norm);
4412#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004413 if (nfa_has_zsubexpr)
4414 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004415#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004416#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004417 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004418 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004419#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004420 }
4421
4422#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004423 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004424 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004425#endif
4426 switch (state->c)
4427 {
4428 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004429 break;
4430
4431 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004432 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004433 subs = addstate(l, state->out, subs, pim, off);
4434 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004435 break;
4436
Bram Moolenaar699c1202013-09-25 16:41:54 +02004437 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004438 case NFA_NOPEN:
4439 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004440 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004441 break;
4442
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004443 case NFA_MOPEN:
4444 case NFA_MOPEN1:
4445 case NFA_MOPEN2:
4446 case NFA_MOPEN3:
4447 case NFA_MOPEN4:
4448 case NFA_MOPEN5:
4449 case NFA_MOPEN6:
4450 case NFA_MOPEN7:
4451 case NFA_MOPEN8:
4452 case NFA_MOPEN9:
4453#ifdef FEAT_SYN_HL
4454 case NFA_ZOPEN:
4455 case NFA_ZOPEN1:
4456 case NFA_ZOPEN2:
4457 case NFA_ZOPEN3:
4458 case NFA_ZOPEN4:
4459 case NFA_ZOPEN5:
4460 case NFA_ZOPEN6:
4461 case NFA_ZOPEN7:
4462 case NFA_ZOPEN8:
4463 case NFA_ZOPEN9:
4464#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004465 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004466 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004467 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004468 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004469 sub = &subs->norm;
4470 }
4471#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004472 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004473 {
4474 subidx = state->c - NFA_ZOPEN;
4475 sub = &subs->synt;
4476 }
4477#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004478 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004479 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004480 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004481 sub = &subs->norm;
4482 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004483
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004484 /* avoid compiler warnings */
4485 save_ptr = NULL;
4486 save_lpos.lnum = 0;
4487 save_lpos.col = 0;
4488
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004489 /* Set the position (with "off" added) in the subexpression. Save
4490 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004491 if (REG_MULTI)
4492 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004493 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004494 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004495 save_lpos.lnum = sub->list.multi[subidx].start_lnum;
4496 save_lpos.col = sub->list.multi[subidx].start_col;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004497 save_in_use = -1;
4498 }
4499 else
4500 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004501 save_in_use = sub->in_use;
4502 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004503 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004504 sub->list.multi[i].start_lnum = -1;
4505 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004506 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004507 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004508 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004509 if (off == -1)
4510 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004511 sub->list.multi[subidx].start_lnum = reglnum + 1;
4512 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004513 }
4514 else
4515 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004516 sub->list.multi[subidx].start_lnum = reglnum;
4517 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004518 (colnr_T)(reginput - regline + off);
4519 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004520 }
4521 else
4522 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004523 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004524 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004525 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004526 save_in_use = -1;
4527 }
4528 else
4529 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004530 save_in_use = sub->in_use;
4531 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004532 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004533 sub->list.line[i].start = NULL;
4534 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004535 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004536 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004537 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004538 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539 }
4540
Bram Moolenaard05bf562013-06-30 23:24:08 +02004541 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004542 /* "subs" may have changed, need to set "sub" again */
4543#ifdef FEAT_SYN_HL
4544 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4545 sub = &subs->synt;
4546 else
4547#endif
4548 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004549
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004550 if (save_in_use == -1)
4551 {
4552 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004553 {
4554 sub->list.multi[subidx].start_lnum = save_lpos.lnum;
4555 sub->list.multi[subidx].start_col = save_lpos.col;
4556 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004557 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004558 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004559 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004561 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004562 break;
4563
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004564 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004565 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004566 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004567 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004568 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004569 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004570 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004571 break;
4572 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004573 case NFA_MCLOSE1:
4574 case NFA_MCLOSE2:
4575 case NFA_MCLOSE3:
4576 case NFA_MCLOSE4:
4577 case NFA_MCLOSE5:
4578 case NFA_MCLOSE6:
4579 case NFA_MCLOSE7:
4580 case NFA_MCLOSE8:
4581 case NFA_MCLOSE9:
4582#ifdef FEAT_SYN_HL
4583 case NFA_ZCLOSE:
4584 case NFA_ZCLOSE1:
4585 case NFA_ZCLOSE2:
4586 case NFA_ZCLOSE3:
4587 case NFA_ZCLOSE4:
4588 case NFA_ZCLOSE5:
4589 case NFA_ZCLOSE6:
4590 case NFA_ZCLOSE7:
4591 case NFA_ZCLOSE8:
4592 case NFA_ZCLOSE9:
4593#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004594 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004595 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004596 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004597 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004598 sub = &subs->norm;
4599 }
4600#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004601 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004602 {
4603 subidx = state->c - NFA_ZCLOSE;
4604 sub = &subs->synt;
4605 }
4606#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004607 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004608 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004609 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004610 sub = &subs->norm;
4611 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004612
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004613 /* We don't fill in gaps here, there must have been an MOPEN that
4614 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004615 save_in_use = sub->in_use;
4616 if (sub->in_use <= subidx)
4617 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004618 if (REG_MULTI)
4619 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004620 save_lpos.lnum = sub->list.multi[subidx].end_lnum;
4621 save_lpos.col = sub->list.multi[subidx].end_col;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004622 if (off == -1)
4623 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004624 sub->list.multi[subidx].end_lnum = reglnum + 1;
4625 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004626 }
4627 else
4628 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004629 sub->list.multi[subidx].end_lnum = reglnum;
4630 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004631 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004632 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004633 /* avoid compiler warnings */
4634 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004635 }
4636 else
4637 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004638 save_ptr = sub->list.line[subidx].end;
4639 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004640 /* avoid compiler warnings */
4641 save_lpos.lnum = 0;
4642 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004643 }
4644
Bram Moolenaard05bf562013-06-30 23:24:08 +02004645 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004646 /* "subs" may have changed, need to set "sub" again */
4647#ifdef FEAT_SYN_HL
4648 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4649 sub = &subs->synt;
4650 else
4651#endif
4652 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004653
4654 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004655 {
4656 sub->list.multi[subidx].end_lnum = save_lpos.lnum;
4657 sub->list.multi[subidx].end_col = save_lpos.col;
4658 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004659 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004660 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004661 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004662 break;
4663 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004664 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004665}
4666
4667/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004668 * Like addstate(), but the new state(s) are put at position "*ip".
4669 * Used for zero-width matches, next state to use is the added one.
4670 * This makes sure the order of states to be tried does not change, which
4671 * matters for alternatives.
4672 */
4673 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004674addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004675 nfa_list_T *l; /* runtime state list */
4676 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004677 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004678 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004679 int *ip;
4680{
4681 int tlen = l->n;
4682 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004683 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004684
4685 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004686 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004687
Bram Moolenaar4b417062013-05-25 20:19:50 +02004688 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004689 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004690 return;
4691
4692 /* re-order to put the new state at the current position */
4693 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004694 if (count == 0)
4695 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004696 if (count == 1)
4697 {
4698 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004699 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004700 }
4701 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004702 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004703 if (l->n + count - 1 >= l->len)
4704 {
4705 /* not enough space to move the new states, reallocate the list
4706 * and move the states to the right position */
4707 nfa_thread_T *newl;
4708
4709 l->len = l->len * 3 / 2 + 50;
4710 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4711 if (newl == NULL)
4712 return;
4713 mch_memmove(&(newl[0]),
4714 &(l->t[0]),
4715 sizeof(nfa_thread_T) * listidx);
4716 mch_memmove(&(newl[listidx]),
4717 &(l->t[l->n - count]),
4718 sizeof(nfa_thread_T) * count);
4719 mch_memmove(&(newl[listidx + count]),
4720 &(l->t[listidx + 1]),
4721 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4722 vim_free(l->t);
4723 l->t = newl;
4724 }
4725 else
4726 {
4727 /* make space for new states, then move them from the
4728 * end to the current position */
4729 mch_memmove(&(l->t[listidx + count]),
4730 &(l->t[listidx + 1]),
4731 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4732 mch_memmove(&(l->t[listidx]),
4733 &(l->t[l->n - 1]),
4734 sizeof(nfa_thread_T) * count);
4735 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004736 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004737 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004738 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004739}
4740
4741/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004742 * Check character class "class" against current character c.
4743 */
4744 static int
4745check_char_class(class, c)
4746 int class;
4747 int c;
4748{
4749 switch (class)
4750 {
4751 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004752 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004753 return OK;
4754 break;
4755 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004756 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004757 return OK;
4758 break;
4759 case NFA_CLASS_BLANK:
4760 if (c == ' ' || c == '\t')
4761 return OK;
4762 break;
4763 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004764 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004765 return OK;
4766 break;
4767 case NFA_CLASS_DIGIT:
4768 if (VIM_ISDIGIT(c))
4769 return OK;
4770 break;
4771 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004772 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004773 return OK;
4774 break;
4775 case NFA_CLASS_LOWER:
4776 if (MB_ISLOWER(c))
4777 return OK;
4778 break;
4779 case NFA_CLASS_PRINT:
4780 if (vim_isprintc(c))
4781 return OK;
4782 break;
4783 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004784 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004785 return OK;
4786 break;
4787 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004788 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004789 return OK;
4790 break;
4791 case NFA_CLASS_UPPER:
4792 if (MB_ISUPPER(c))
4793 return OK;
4794 break;
4795 case NFA_CLASS_XDIGIT:
4796 if (vim_isxdigit(c))
4797 return OK;
4798 break;
4799 case NFA_CLASS_TAB:
4800 if (c == '\t')
4801 return OK;
4802 break;
4803 case NFA_CLASS_RETURN:
4804 if (c == '\r')
4805 return OK;
4806 break;
4807 case NFA_CLASS_BACKSPACE:
4808 if (c == '\b')
4809 return OK;
4810 break;
4811 case NFA_CLASS_ESCAPE:
4812 if (c == '\033')
4813 return OK;
4814 break;
4815
4816 default:
4817 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004818 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004819 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004820 }
4821 return FAIL;
4822}
4823
Bram Moolenaar5714b802013-05-28 22:03:20 +02004824/*
4825 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004826 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004827 */
4828 static int
4829match_backref(sub, subidx, bytelen)
4830 regsub_T *sub; /* pointers to subexpressions */
4831 int subidx;
4832 int *bytelen; /* out: length of match in bytes */
4833{
4834 int len;
4835
4836 if (sub->in_use <= subidx)
4837 {
4838retempty:
4839 /* backref was not set, match an empty string */
4840 *bytelen = 0;
4841 return TRUE;
4842 }
4843
4844 if (REG_MULTI)
4845 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004846 if (sub->list.multi[subidx].start_lnum < 0
4847 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004848 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004849 if (sub->list.multi[subidx].start_lnum == reglnum
4850 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004851 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004852 len = sub->list.multi[subidx].end_col
4853 - sub->list.multi[subidx].start_col;
4854 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004855 reginput, &len) == 0)
4856 {
4857 *bytelen = len;
4858 return TRUE;
4859 }
4860 }
4861 else
4862 {
4863 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004864 sub->list.multi[subidx].start_lnum,
4865 sub->list.multi[subidx].start_col,
4866 sub->list.multi[subidx].end_lnum,
4867 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004868 bytelen) == RA_MATCH)
4869 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004870 }
4871 }
4872 else
4873 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004874 if (sub->list.line[subidx].start == NULL
4875 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004876 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004877 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4878 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004879 {
4880 *bytelen = len;
4881 return TRUE;
4882 }
4883 }
4884 return FALSE;
4885}
4886
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004887#ifdef FEAT_SYN_HL
4888
4889static int match_zref __ARGS((int subidx, int *bytelen));
4890
4891/*
4892 * Check for a match with \z subexpression "subidx".
4893 * Return TRUE if it matches.
4894 */
4895 static int
4896match_zref(subidx, bytelen)
4897 int subidx;
4898 int *bytelen; /* out: length of match in bytes */
4899{
4900 int len;
4901
4902 cleanup_zsubexpr();
4903 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4904 {
4905 /* backref was not set, match an empty string */
4906 *bytelen = 0;
4907 return TRUE;
4908 }
4909
4910 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4911 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4912 {
4913 *bytelen = len;
4914 return TRUE;
4915 }
4916 return FALSE;
4917}
4918#endif
4919
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004921 * Save list IDs for all NFA states of "prog" into "list".
4922 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004923 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004924 */
4925 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004926nfa_save_listids(prog, list)
4927 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004928 int *list;
4929{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004930 int i;
4931 nfa_state_T *p;
4932
4933 /* Order in the list is reverse, it's a bit faster that way. */
4934 p = &prog->state[0];
4935 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004936 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004937 list[i] = p->lastlist[1];
4938 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004939 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940 }
4941}
4942
4943/*
4944 * Restore list IDs from "list" to all NFA states.
4945 */
4946 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004947nfa_restore_listids(prog, list)
4948 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004949 int *list;
4950{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004951 int i;
4952 nfa_state_T *p;
4953
4954 p = &prog->state[0];
4955 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004956 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004957 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004958 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004959 }
4960}
4961
Bram Moolenaar423532e2013-05-29 21:14:42 +02004962 static int
4963nfa_re_num_cmp(val, op, pos)
4964 long_u val;
4965 int op;
4966 long_u pos;
4967{
4968 if (op == 1) return pos > val;
4969 if (op == 2) return pos < val;
4970 return val == pos;
4971}
4972
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004973static 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 +02004974static 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 +02004975
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004976/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004977 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004978 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4979 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004980 */
4981 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004982recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004983 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004984 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004985 nfa_regprog_T *prog;
4986 regsubs_T *submatch;
4987 regsubs_T *m;
4988 int **listids;
4989{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004990 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004991 int save_reglnum = reglnum;
4992 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004993 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004994 save_se_T *save_nfa_endp = nfa_endp;
4995 save_se_T endpos;
4996 save_se_T *endposp = NULL;
4997 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004998 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004999
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005000 if (pim != NULL)
5001 {
5002 /* start at the position where the postponed match was */
5003 if (REG_MULTI)
5004 reginput = regline + pim->end.pos.col;
5005 else
5006 reginput = pim->end.ptr;
5007 }
5008
Bram Moolenaardecd9542013-06-07 16:31:50 +02005009 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005010 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5011 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5012 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005013 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005014 /* The recursive match must end at the current position. When "pim" is
5015 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005016 endposp = &endpos;
5017 if (REG_MULTI)
5018 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005019 if (pim == NULL)
5020 {
5021 endpos.se_u.pos.col = (int)(reginput - regline);
5022 endpos.se_u.pos.lnum = reglnum;
5023 }
5024 else
5025 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005026 }
5027 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005028 {
5029 if (pim == NULL)
5030 endpos.se_u.ptr = reginput;
5031 else
5032 endpos.se_u.ptr = pim->end.ptr;
5033 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005034
5035 /* Go back the specified number of bytes, or as far as the
5036 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005037 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005038 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005039 if (state->val <= 0)
5040 {
5041 if (REG_MULTI)
5042 {
5043 regline = reg_getline(--reglnum);
5044 if (regline == NULL)
5045 /* can't go before the first line */
5046 regline = reg_getline(++reglnum);
5047 }
5048 reginput = regline;
5049 }
5050 else
5051 {
5052 if (REG_MULTI && (int)(reginput - regline) < state->val)
5053 {
5054 /* Not enough bytes in this line, go to end of
5055 * previous line. */
5056 regline = reg_getline(--reglnum);
5057 if (regline == NULL)
5058 {
5059 /* can't go before the first line */
5060 regline = reg_getline(++reglnum);
5061 reginput = regline;
5062 }
5063 else
5064 reginput = regline + STRLEN(regline);
5065 }
5066 if ((int)(reginput - regline) >= state->val)
5067 {
5068 reginput -= state->val;
5069#ifdef FEAT_MBYTE
5070 if (has_mbyte)
5071 reginput -= mb_head_off(regline, reginput);
5072#endif
5073 }
5074 else
5075 reginput = regline;
5076 }
5077 }
5078
Bram Moolenaarf46da702013-06-02 22:37:42 +02005079#ifdef ENABLE_LOG
5080 if (log_fd != stderr)
5081 fclose(log_fd);
5082 log_fd = NULL;
5083#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005084 /* Have to clear the lastlist field of the NFA nodes, so that
5085 * nfa_regmatch() and addstate() can run properly after recursion. */
5086 if (nfa_ll_index == 1)
5087 {
5088 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5089 * values and clear them. */
5090 if (*listids == NULL)
5091 {
5092 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5093 if (*listids == NULL)
5094 {
5095 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5096 return 0;
5097 }
5098 }
5099 nfa_save_listids(prog, *listids);
5100 need_restore = TRUE;
5101 /* any value of nfa_listid will do */
5102 }
5103 else
5104 {
5105 /* First recursive nfa_regmatch() call, switch to the second lastlist
5106 * entry. Make sure nfa_listid is different from a previous recursive
5107 * call, because some states may still have this ID. */
5108 ++nfa_ll_index;
5109 if (nfa_listid <= nfa_alt_listid)
5110 nfa_listid = nfa_alt_listid;
5111 }
5112
5113 /* Call nfa_regmatch() to check if the current concat matches at this
5114 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005115 nfa_endp = endposp;
5116 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005117
5118 if (need_restore)
5119 nfa_restore_listids(prog, *listids);
5120 else
5121 {
5122 --nfa_ll_index;
5123 nfa_alt_listid = nfa_listid;
5124 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005125
5126 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005127 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005128 if (REG_MULTI)
5129 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005130 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005131 nfa_match = save_nfa_match;
5132 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005133 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005134
5135#ifdef ENABLE_LOG
5136 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5137 if (log_fd != NULL)
5138 {
5139 fprintf(log_fd, "****************************\n");
5140 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5141 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5142 fprintf(log_fd, "****************************\n");
5143 }
5144 else
5145 {
5146 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5147 log_fd = stderr;
5148 }
5149#endif
5150
5151 return result;
5152}
5153
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005154static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005155static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005156
5157/*
5158 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005159 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005160 * NFA_ANY: 1
5161 * specific character: 99
5162 */
5163 static int
5164failure_chance(state, depth)
5165 nfa_state_T *state;
5166 int depth;
5167{
5168 int c = state->c;
5169 int l, r;
5170
5171 /* detect looping */
5172 if (depth > 4)
5173 return 1;
5174
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005175 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005176 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005177 case NFA_SPLIT:
5178 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5179 /* avoid recursive stuff */
5180 return 1;
5181 /* two alternatives, use the lowest failure chance */
5182 l = failure_chance(state->out, depth + 1);
5183 r = failure_chance(state->out1, depth + 1);
5184 return l < r ? l : r;
5185
5186 case NFA_ANY:
5187 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005188 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005189
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005190 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005191 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005192 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005193 /* empty match works always */
5194 return 0;
5195
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005196 case NFA_START_INVISIBLE:
5197 case NFA_START_INVISIBLE_FIRST:
5198 case NFA_START_INVISIBLE_NEG:
5199 case NFA_START_INVISIBLE_NEG_FIRST:
5200 case NFA_START_INVISIBLE_BEFORE:
5201 case NFA_START_INVISIBLE_BEFORE_FIRST:
5202 case NFA_START_INVISIBLE_BEFORE_NEG:
5203 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5204 case NFA_START_PATTERN:
5205 /* recursive regmatch is expensive, use low failure chance */
5206 return 5;
5207
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005208 case NFA_BOL:
5209 case NFA_EOL:
5210 case NFA_BOF:
5211 case NFA_EOF:
5212 case NFA_NEWL:
5213 return 99;
5214
5215 case NFA_BOW:
5216 case NFA_EOW:
5217 return 90;
5218
5219 case NFA_MOPEN:
5220 case NFA_MOPEN1:
5221 case NFA_MOPEN2:
5222 case NFA_MOPEN3:
5223 case NFA_MOPEN4:
5224 case NFA_MOPEN5:
5225 case NFA_MOPEN6:
5226 case NFA_MOPEN7:
5227 case NFA_MOPEN8:
5228 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005229#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005230 case NFA_ZOPEN:
5231 case NFA_ZOPEN1:
5232 case NFA_ZOPEN2:
5233 case NFA_ZOPEN3:
5234 case NFA_ZOPEN4:
5235 case NFA_ZOPEN5:
5236 case NFA_ZOPEN6:
5237 case NFA_ZOPEN7:
5238 case NFA_ZOPEN8:
5239 case NFA_ZOPEN9:
5240 case NFA_ZCLOSE:
5241 case NFA_ZCLOSE1:
5242 case NFA_ZCLOSE2:
5243 case NFA_ZCLOSE3:
5244 case NFA_ZCLOSE4:
5245 case NFA_ZCLOSE5:
5246 case NFA_ZCLOSE6:
5247 case NFA_ZCLOSE7:
5248 case NFA_ZCLOSE8:
5249 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005250#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005251 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005252 case NFA_MCLOSE1:
5253 case NFA_MCLOSE2:
5254 case NFA_MCLOSE3:
5255 case NFA_MCLOSE4:
5256 case NFA_MCLOSE5:
5257 case NFA_MCLOSE6:
5258 case NFA_MCLOSE7:
5259 case NFA_MCLOSE8:
5260 case NFA_MCLOSE9:
5261 case NFA_NCLOSE:
5262 return failure_chance(state->out, depth + 1);
5263
5264 case NFA_BACKREF1:
5265 case NFA_BACKREF2:
5266 case NFA_BACKREF3:
5267 case NFA_BACKREF4:
5268 case NFA_BACKREF5:
5269 case NFA_BACKREF6:
5270 case NFA_BACKREF7:
5271 case NFA_BACKREF8:
5272 case NFA_BACKREF9:
5273#ifdef FEAT_SYN_HL
5274 case NFA_ZREF1:
5275 case NFA_ZREF2:
5276 case NFA_ZREF3:
5277 case NFA_ZREF4:
5278 case NFA_ZREF5:
5279 case NFA_ZREF6:
5280 case NFA_ZREF7:
5281 case NFA_ZREF8:
5282 case NFA_ZREF9:
5283#endif
5284 /* backreferences don't match in many places */
5285 return 94;
5286
5287 case NFA_LNUM_GT:
5288 case NFA_LNUM_LT:
5289 case NFA_COL_GT:
5290 case NFA_COL_LT:
5291 case NFA_VCOL_GT:
5292 case NFA_VCOL_LT:
5293 case NFA_MARK_GT:
5294 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005295 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005296 /* before/after positions don't match very often */
5297 return 85;
5298
5299 case NFA_LNUM:
5300 return 90;
5301
5302 case NFA_CURSOR:
5303 case NFA_COL:
5304 case NFA_VCOL:
5305 case NFA_MARK:
5306 /* specific positions rarely match */
5307 return 98;
5308
5309 case NFA_COMPOSING:
5310 return 95;
5311
5312 default:
5313 if (c > 0)
5314 /* character match fails often */
5315 return 95;
5316 }
5317
5318 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005319 return 50;
5320}
5321
Bram Moolenaarf46da702013-06-02 22:37:42 +02005322/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005323 * Skip until the char "c" we know a match must start with.
5324 */
5325 static int
5326skip_to_start(c, colp)
5327 int c;
5328 colnr_T *colp;
5329{
5330 char_u *s;
5331
5332 /* Used often, do some work to avoid call overhead. */
5333 if (!ireg_ic
5334#ifdef FEAT_MBYTE
5335 && !has_mbyte
5336#endif
5337 )
5338 s = vim_strbyte(regline + *colp, c);
5339 else
5340 s = cstrchr(regline + *colp, c);
5341 if (s == NULL)
5342 return FAIL;
5343 *colp = (int)(s - regline);
5344 return OK;
5345}
5346
5347/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005348 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005349 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005350 * Returns zero for no match, 1 for a match.
5351 */
5352 static long
5353find_match_text(startcol, regstart, match_text)
5354 colnr_T startcol;
5355 int regstart;
5356 char_u *match_text;
5357{
5358 colnr_T col = startcol;
5359 int c1, c2;
5360 int len1, len2;
5361 int match;
5362
5363 for (;;)
5364 {
5365 match = TRUE;
5366 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5367 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5368 {
5369 c1 = PTR2CHAR(match_text + len1);
5370 c2 = PTR2CHAR(regline + col + len2);
5371 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5372 {
5373 match = FALSE;
5374 break;
5375 }
5376 len2 += MB_CHAR2LEN(c2);
5377 }
5378 if (match
5379#ifdef FEAT_MBYTE
5380 /* check that no composing char follows */
5381 && !(enc_utf8
5382 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5383#endif
5384 )
5385 {
5386 cleanup_subexpr();
5387 if (REG_MULTI)
5388 {
5389 reg_startpos[0].lnum = reglnum;
5390 reg_startpos[0].col = col;
5391 reg_endpos[0].lnum = reglnum;
5392 reg_endpos[0].col = col + len2;
5393 }
5394 else
5395 {
5396 reg_startp[0] = regline + col;
5397 reg_endp[0] = regline + col + len2;
5398 }
5399 return 1L;
5400 }
5401
5402 /* Try finding regstart after the current match. */
5403 col += MB_CHAR2LEN(regstart); /* skip regstart */
5404 if (skip_to_start(regstart, &col) == FAIL)
5405 break;
5406 }
5407 return 0L;
5408}
5409
5410/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005411 * Main matching routine.
5412 *
5413 * Run NFA to determine whether it matches reginput.
5414 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005415 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005416 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005417 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005418 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005419 * Note: Caller must ensure that: start != NULL.
5420 */
5421 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005422nfa_regmatch(prog, start, submatch, m)
5423 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005424 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005425 regsubs_T *submatch;
5426 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005427{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005428 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005429 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005430 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005431 int go_to_nextline = FALSE;
5432 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005433 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005434 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005435 nfa_list_T *thislist;
5436 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005437 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005438 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005439 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005440 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005441 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005442 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005443#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005444 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005445
5446 if (debug == NULL)
5447 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005448 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005449 return FALSE;
5450 }
5451#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005452 /* Some patterns may take a long time to match, especially when using
5453 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5454 fast_breakcheck();
5455 if (got_int)
5456 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005457#ifdef FEAT_RELTIME
5458 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5459 return FALSE;
5460#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005461
Bram Moolenaar963fee22013-05-26 21:47:28 +02005462 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005463
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005464 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005465 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005466
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005467 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005468 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005469 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005470 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005471 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005472 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005473
5474#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005475 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 if (log_fd != NULL)
5477 {
5478 fprintf(log_fd, "**********************************\n");
5479 nfa_set_code(start->c);
5480 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5481 abs(start->id), code);
5482 fprintf(log_fd, "**********************************\n");
5483 }
5484 else
5485 {
5486 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5487 log_fd = stderr;
5488 }
5489#endif
5490
5491 thislist = &list[0];
5492 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005493 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005494 nextlist = &list[1];
5495 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005496 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005497#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005498 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005499#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005500 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005501
5502 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5503 * it's the first MOPEN. */
5504 if (toplevel)
5505 {
5506 if (REG_MULTI)
5507 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005508 m->norm.list.multi[0].start_lnum = reglnum;
5509 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005510 }
5511 else
5512 m->norm.list.line[0].start = reginput;
5513 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005514 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005515 }
5516 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005517 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005519#define ADD_STATE_IF_MATCH(state) \
5520 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005521 add_state = state->out; \
5522 add_off = clen; \
5523 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005524
5525 /*
5526 * Run for each character.
5527 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005528 for (;;)
5529 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005530 int curc;
5531 int clen;
5532
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533#ifdef FEAT_MBYTE
5534 if (has_mbyte)
5535 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005536 curc = (*mb_ptr2char)(reginput);
5537 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538 }
5539 else
5540#endif
5541 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005542 curc = *reginput;
5543 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005544 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005545 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005546 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005547 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005548 go_to_nextline = FALSE;
5549 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550
5551 /* swap lists */
5552 thislist = &list[flag];
5553 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005554 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005555 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005556 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005557 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5558 {
5559 /* too many states, retry with old engine */
5560 nfa_match = NFA_TOO_EXPENSIVE;
5561 goto theend;
5562 }
5563
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005564 thislist->id = nfa_listid;
5565 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005566
5567#ifdef ENABLE_LOG
5568 fprintf(log_fd, "------------------------------------------\n");
5569 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005570 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005571 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005572 {
5573 int i;
5574
5575 for (i = 0; i < thislist->n; i++)
5576 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005578 fprintf(log_fd, "\n");
5579#endif
5580
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005581#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582 fprintf(debug, "\n-------------------\n");
5583#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005584 /*
5585 * If the state lists are empty we can stop.
5586 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005587 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005588 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005589
5590 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005591 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005592 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005593 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005594
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005595#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005596 nfa_set_code(t->state->c);
5597 fprintf(debug, "%s, ", code);
5598#endif
5599#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005600 {
5601 int col;
5602
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005603 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005604 col = -1;
5605 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005606 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005607 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005608 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005609 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005610 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5611 abs(t->state->id), (int)t->state->c, code, col,
5612 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005613 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614#endif
5615
5616 /*
5617 * Handle the possible codes of the current state.
5618 * The most important is NFA_MATCH.
5619 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005620 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005621 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005622 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005623 switch (t->state->c)
5624 {
5625 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005626 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005627#ifdef FEAT_MBYTE
5628 /* If the match ends before a composing characters and
5629 * ireg_icombine is not set, that is not really a match. */
5630 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5631 break;
5632#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005633 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005634 copy_sub(&submatch->norm, &t->subs.norm);
5635#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005636 if (nfa_has_zsubexpr)
5637 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005638#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005639#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005640 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005642 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005643 * states at this position. When the list of states is going
5644 * to be empty quit without advancing, so that "reginput" is
5645 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005646 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005647 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005648 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005649 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005650
5651 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005652 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005653 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005654 /*
5655 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005656 * NFA_START_INVISIBLE_BEFORE node.
5657 * They surround a zero-width group, used with "\@=", "\&",
5658 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005659 * If we got here, it means that the current "invisible" group
5660 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005661 * nfa_regmatch(). For a look-behind match only when it ends
5662 * in the position in "nfa_endp".
5663 * Submatches are stored in *m, and used in the parent call.
5664 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005665#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005666 if (nfa_endp != NULL)
5667 {
5668 if (REG_MULTI)
5669 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5670 (int)reglnum,
5671 (int)nfa_endp->se_u.pos.lnum,
5672 (int)(reginput - regline),
5673 nfa_endp->se_u.pos.col);
5674 else
5675 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5676 (int)(reginput - regline),
5677 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005678 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005679#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005680 /* If "nfa_endp" is set it's only a match if it ends at
5681 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005682 if (nfa_endp != NULL && (REG_MULTI
5683 ? (reglnum != nfa_endp->se_u.pos.lnum
5684 || (int)(reginput - regline)
5685 != nfa_endp->se_u.pos.col)
5686 : reginput != nfa_endp->se_u.ptr))
5687 break;
5688
5689 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005690 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005691 {
5692 copy_sub(&m->norm, &t->subs.norm);
5693#ifdef FEAT_SYN_HL
5694 if (nfa_has_zsubexpr)
5695 copy_sub(&m->synt, &t->subs.synt);
5696#endif
5697 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005698#ifdef ENABLE_LOG
5699 fprintf(log_fd, "Match found:\n");
5700 log_subsexpr(m);
5701#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005702 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005703 /* See comment above at "goto nextchar". */
5704 if (nextlist->n == 0)
5705 clen = 0;
5706 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005707
5708 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005709 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005710 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005711 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005712 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005713 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005714 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005715 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005716 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005717#ifdef ENABLE_LOG
5718 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5719 failure_chance(t->state->out, 0),
5720 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005721#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005722 /* Do it directly if there already is a PIM or when
5723 * nfa_postprocess() detected it will work better. */
5724 if (t->pim.result != NFA_PIM_UNUSED
5725 || t->state->c == NFA_START_INVISIBLE_FIRST
5726 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5727 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5728 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005729 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005730 int in_use = m->norm.in_use;
5731
Bram Moolenaar699c1202013-09-25 16:41:54 +02005732 /* Copy submatch info for the recursive call, opposite
5733 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005734 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005735#ifdef FEAT_SYN_HL
5736 if (nfa_has_zsubexpr)
5737 copy_sub_off(&m->synt, &t->subs.synt);
5738#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005739
Bram Moolenaara2d95102013-06-04 14:23:05 +02005740 /*
5741 * First try matching the invisible match, then what
5742 * follows.
5743 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005744 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005745 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005746 if (result == NFA_TOO_EXPENSIVE)
5747 {
5748 nfa_match = result;
5749 goto theend;
5750 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005751
Bram Moolenaardecd9542013-06-07 16:31:50 +02005752 /* for \@! and \@<! it is a match when the result is
5753 * FALSE */
5754 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005755 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5756 || t->state->c
5757 == NFA_START_INVISIBLE_BEFORE_NEG
5758 || t->state->c
5759 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005760 {
5761 /* Copy submatch info from the recursive call */
5762 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005763#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005764 if (nfa_has_zsubexpr)
5765 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005766#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005767 /* If the pattern has \ze and it matched in the
5768 * sub pattern, use it. */
5769 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005770
Bram Moolenaara2d95102013-06-04 14:23:05 +02005771 /* t->state->out1 is the corresponding
5772 * END_INVISIBLE node; Add its out to the current
5773 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005774 add_here = TRUE;
5775 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005776 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005777 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005778 }
5779 else
5780 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005781 nfa_pim_T pim;
5782
Bram Moolenaara2d95102013-06-04 14:23:05 +02005783 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005784 * First try matching what follows. Only if a match
5785 * is found verify the invisible match matches. Add a
5786 * nfa_pim_T to the following states, it contains info
5787 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005788 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005789 pim.state = t->state;
5790 pim.result = NFA_PIM_TODO;
5791 pim.subs.norm.in_use = 0;
5792#ifdef FEAT_SYN_HL
5793 pim.subs.synt.in_use = 0;
5794#endif
5795 if (REG_MULTI)
5796 {
5797 pim.end.pos.col = (int)(reginput - regline);
5798 pim.end.pos.lnum = reglnum;
5799 }
5800 else
5801 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005802
5803 /* t->state->out1 is the corresponding END_INVISIBLE
5804 * node; Add its out to the current list (zero-width
5805 * match). */
5806 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005807 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005808 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005809 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005810 break;
5811
Bram Moolenaar87953742013-06-05 18:52:40 +02005812 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005813 {
5814 nfa_state_T *skip = NULL;
5815#ifdef ENABLE_LOG
5816 int skip_lid = 0;
5817#endif
5818
5819 /* There is no point in trying to match the pattern if the
5820 * output state is not going to be added to the list. */
5821 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5822 {
5823 skip = t->state->out1->out;
5824#ifdef ENABLE_LOG
5825 skip_lid = nextlist->id;
5826#endif
5827 }
5828 else if (state_in_list(nextlist,
5829 t->state->out1->out->out, &t->subs))
5830 {
5831 skip = t->state->out1->out->out;
5832#ifdef ENABLE_LOG
5833 skip_lid = nextlist->id;
5834#endif
5835 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005836 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005837 t->state->out1->out->out, &t->subs))
5838 {
5839 skip = t->state->out1->out->out;
5840#ifdef ENABLE_LOG
5841 skip_lid = thislist->id;
5842#endif
5843 }
5844 if (skip != NULL)
5845 {
5846#ifdef ENABLE_LOG
5847 nfa_set_code(skip->c);
5848 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5849 abs(skip->id), skip_lid, skip->c, code);
5850#endif
5851 break;
5852 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005853 /* Copy submatch info to the recursive call, opposite of what
5854 * happens afterwards. */
5855 copy_sub_off(&m->norm, &t->subs.norm);
5856#ifdef FEAT_SYN_HL
5857 if (nfa_has_zsubexpr)
5858 copy_sub_off(&m->synt, &t->subs.synt);
5859#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005860
Bram Moolenaar87953742013-06-05 18:52:40 +02005861 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005862 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005863 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005864 if (result == NFA_TOO_EXPENSIVE)
5865 {
5866 nfa_match = result;
5867 goto theend;
5868 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005869 if (result)
5870 {
5871 int bytelen;
5872
5873#ifdef ENABLE_LOG
5874 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5875 log_subsexpr(m);
5876#endif
5877 /* Copy submatch info from the recursive call */
5878 copy_sub_off(&t->subs.norm, &m->norm);
5879#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005880 if (nfa_has_zsubexpr)
5881 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005882#endif
5883 /* Now we need to skip over the matched text and then
5884 * continue with what follows. */
5885 if (REG_MULTI)
5886 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005887 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005888 - (int)(reginput - regline);
5889 else
5890 bytelen = (int)(m->norm.list.line[0].end - reginput);
5891
5892#ifdef ENABLE_LOG
5893 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5894#endif
5895 if (bytelen == 0)
5896 {
5897 /* empty match, output of corresponding
5898 * NFA_END_PATTERN/NFA_SKIP to be used at current
5899 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005900 add_here = TRUE;
5901 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005902 }
5903 else if (bytelen <= clen)
5904 {
5905 /* match current character, output of corresponding
5906 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005907 add_state = t->state->out1->out->out;
5908 add_off = clen;
5909 }
5910 else
5911 {
5912 /* skip over the matched characters, set character
5913 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005914 add_state = t->state->out1->out;
5915 add_off = bytelen;
5916 add_count = bytelen - clen;
5917 }
5918 }
5919 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005920 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005921
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005922 case NFA_BOL:
5923 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005924 {
5925 add_here = TRUE;
5926 add_state = t->state->out;
5927 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005928 break;
5929
5930 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005931 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005932 {
5933 add_here = TRUE;
5934 add_state = t->state->out;
5935 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005936 break;
5937
5938 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005939 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005940
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005941 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005942 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005943#ifdef FEAT_MBYTE
5944 else if (has_mbyte)
5945 {
5946 int this_class;
5947
5948 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005949 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005950 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005951 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005952 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005953 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005954 }
5955#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005956 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005957 || (reginput > regline
5958 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005959 result = FALSE;
5960 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005961 {
5962 add_here = TRUE;
5963 add_state = t->state->out;
5964 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005965 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005966
5967 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005968 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005969 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005970 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005971#ifdef FEAT_MBYTE
5972 else if (has_mbyte)
5973 {
5974 int this_class, prev_class;
5975
5976 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005977 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005978 prev_class = reg_prev_class();
5979 if (this_class == prev_class
5980 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005981 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005982 }
5983#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005984 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005985 || (reginput[0] != NUL
5986 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005987 result = FALSE;
5988 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005989 {
5990 add_here = TRUE;
5991 add_state = t->state->out;
5992 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005993 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005994
Bram Moolenaar4b780632013-05-31 22:14:52 +02005995 case NFA_BOF:
5996 if (reglnum == 0 && reginput == regline
5997 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005998 {
5999 add_here = TRUE;
6000 add_state = t->state->out;
6001 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006002 break;
6003
6004 case NFA_EOF:
6005 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006006 {
6007 add_here = TRUE;
6008 add_state = t->state->out;
6009 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006010 break;
6011
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006012#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006013 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006014 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006015 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006016 int len = 0;
6017 nfa_state_T *end;
6018 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006019 int cchars[MAX_MCO];
6020 int ccount = 0;
6021 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006022
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006023 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006024 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006025 if (utf_iscomposing(sta->c))
6026 {
6027 /* Only match composing character(s), ignore base
6028 * character. Used for ".{composing}" and "{composing}"
6029 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006030 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006031 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02006032 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006033 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006034 /* If \Z was present, then ignore composing characters.
6035 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006036 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006037 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006038 else
6039 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006040 while (sta->c != NFA_END_COMPOSING)
6041 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006042 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006043
6044 /* Check base character matches first, unless ignored. */
6045 else if (len > 0 || mc == sta->c)
6046 {
6047 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006048 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006049 len += mb_char2len(mc);
6050 sta = sta->out;
6051 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006052
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006053 /* We don't care about the order of composing characters.
6054 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006055 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006056 {
6057 mc = mb_ptr2char(reginput + len);
6058 cchars[ccount++] = mc;
6059 len += mb_char2len(mc);
6060 if (ccount == MAX_MCO)
6061 break;
6062 }
6063
6064 /* Check that each composing char in the pattern matches a
6065 * composing char in the text. We do not check if all
6066 * composing chars are matched. */
6067 result = OK;
6068 while (sta->c != NFA_END_COMPOSING)
6069 {
6070 for (j = 0; j < ccount; ++j)
6071 if (cchars[j] == sta->c)
6072 break;
6073 if (j == ccount)
6074 {
6075 result = FAIL;
6076 break;
6077 }
6078 sta = sta->out;
6079 }
6080 }
6081 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006082 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006083
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006084 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006085 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006086 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006087 }
6088#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006089
6090 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006091 if (curc == NUL && !reg_line_lbr && REG_MULTI
6092 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006094 go_to_nextline = TRUE;
6095 /* Pass -1 for the offset, which means taking the position
6096 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006097 add_state = t->state->out;
6098 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006099 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006100 else if (curc == '\n' && reg_line_lbr)
6101 {
6102 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006103 add_state = t->state->out;
6104 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006105 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006106 break;
6107
Bram Moolenaar417bad22013-06-07 14:08:30 +02006108 case NFA_START_COLL:
6109 case NFA_START_NEG_COLL:
6110 {
6111 /* What follows is a list of characters, until NFA_END_COLL.
6112 * One of them must match or none of them must match. */
6113 nfa_state_T *state;
6114 int result_if_matched;
6115 int c1, c2;
6116
6117 /* Never match EOL. If it's part of the collection it is added
6118 * as a separate state with an OR. */
6119 if (curc == NUL)
6120 break;
6121
6122 state = t->state->out;
6123 result_if_matched = (t->state->c == NFA_START_COLL);
6124 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006125 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006126 if (state->c == NFA_END_COLL)
6127 {
6128 result = !result_if_matched;
6129 break;
6130 }
6131 if (state->c == NFA_RANGE_MIN)
6132 {
6133 c1 = state->val;
6134 state = state->out; /* advance to NFA_RANGE_MAX */
6135 c2 = state->val;
6136#ifdef ENABLE_LOG
6137 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6138 curc, c1, c2);
6139#endif
6140 if (curc >= c1 && curc <= c2)
6141 {
6142 result = result_if_matched;
6143 break;
6144 }
6145 if (ireg_ic)
6146 {
6147 int curc_low = MB_TOLOWER(curc);
6148 int done = FALSE;
6149
6150 for ( ; c1 <= c2; ++c1)
6151 if (MB_TOLOWER(c1) == curc_low)
6152 {
6153 result = result_if_matched;
6154 done = TRUE;
6155 break;
6156 }
6157 if (done)
6158 break;
6159 }
6160 }
6161 else if (state->c < 0 ? check_char_class(state->c, curc)
6162 : (curc == state->c
6163 || (ireg_ic && MB_TOLOWER(curc)
6164 == MB_TOLOWER(state->c))))
6165 {
6166 result = result_if_matched;
6167 break;
6168 }
6169 state = state->out;
6170 }
6171 if (result)
6172 {
6173 /* next state is in out of the NFA_END_COLL, out1 of
6174 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006175 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006176 add_off = clen;
6177 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006178 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006179 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006180
6181 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006182 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006183 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006184 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006185 add_state = t->state->out;
6186 add_off = clen;
6187 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006188 break;
6189
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006190 case NFA_ANY_COMPOSING:
6191 /* On a composing character skip over it. Otherwise do
6192 * nothing. Always matches. */
6193#ifdef FEAT_MBYTE
6194 if (enc_utf8 && utf_iscomposing(curc))
6195 {
6196 add_off = clen;
6197 }
6198 else
6199#endif
6200 {
6201 add_here = TRUE;
6202 add_off = 0;
6203 }
6204 add_state = t->state->out;
6205 break;
6206
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006207 /*
6208 * Character classes like \a for alpha, \d for digit etc.
6209 */
6210 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006211 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006212 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006213 break;
6214
6215 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006216 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006217 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006218 break;
6219
6220 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006221 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006222 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006223 break;
6224
6225 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006226 result = !VIM_ISDIGIT(curc)
6227 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006228 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006229 break;
6230
6231 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006232 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006233 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006234 break;
6235
6236 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006237 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006238 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006239 break;
6240
6241 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006242 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006243 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006244 break;
6245
6246 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006247 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006248 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006249 break;
6250
6251 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006252 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006253 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006254 break;
6255
6256 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006257 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006258 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006259 break;
6260
6261 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006262 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006263 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006264 break;
6265
6266 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006267 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006268 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006269 break;
6270
6271 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006272 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006273 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006274 break;
6275
6276 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006277 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006278 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006279 break;
6280
6281 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006282 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006283 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006284 break;
6285
6286 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006287 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006288 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006289 break;
6290
6291 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006292 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006293 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006294 break;
6295
6296 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006297 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006298 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006299 break;
6300
6301 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006302 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006303 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006304 break;
6305
6306 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006307 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006308 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006309 break;
6310
6311 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006312 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006313 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006314 break;
6315
6316 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006317 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006318 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006319 break;
6320
6321 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006322 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006323 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006324 break;
6325
6326 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006327 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006328 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006329 break;
6330
6331 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006332 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006333 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006334 break;
6335
6336 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006337 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006338 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006339 break;
6340
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006341 case NFA_LOWER_IC: /* [a-z] */
6342 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6343 ADD_STATE_IF_MATCH(t->state);
6344 break;
6345
6346 case NFA_NLOWER_IC: /* [^a-z] */
6347 result = curc != NUL
6348 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6349 ADD_STATE_IF_MATCH(t->state);
6350 break;
6351
6352 case NFA_UPPER_IC: /* [A-Z] */
6353 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6354 ADD_STATE_IF_MATCH(t->state);
6355 break;
6356
6357 case NFA_NUPPER_IC: /* ^[A-Z] */
6358 result = curc != NUL
6359 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6360 ADD_STATE_IF_MATCH(t->state);
6361 break;
6362
Bram Moolenaar5714b802013-05-28 22:03:20 +02006363 case NFA_BACKREF1:
6364 case NFA_BACKREF2:
6365 case NFA_BACKREF3:
6366 case NFA_BACKREF4:
6367 case NFA_BACKREF5:
6368 case NFA_BACKREF6:
6369 case NFA_BACKREF7:
6370 case NFA_BACKREF8:
6371 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006372#ifdef FEAT_SYN_HL
6373 case NFA_ZREF1:
6374 case NFA_ZREF2:
6375 case NFA_ZREF3:
6376 case NFA_ZREF4:
6377 case NFA_ZREF5:
6378 case NFA_ZREF6:
6379 case NFA_ZREF7:
6380 case NFA_ZREF8:
6381 case NFA_ZREF9:
6382#endif
6383 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006384 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006385 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006386 int bytelen;
6387
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006388 if (t->state->c <= NFA_BACKREF9)
6389 {
6390 subidx = t->state->c - NFA_BACKREF1 + 1;
6391 result = match_backref(&t->subs.norm, subidx, &bytelen);
6392 }
6393#ifdef FEAT_SYN_HL
6394 else
6395 {
6396 subidx = t->state->c - NFA_ZREF1 + 1;
6397 result = match_zref(subidx, &bytelen);
6398 }
6399#endif
6400
Bram Moolenaar5714b802013-05-28 22:03:20 +02006401 if (result)
6402 {
6403 if (bytelen == 0)
6404 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006405 /* empty match always works, output of NFA_SKIP to be
6406 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006407 add_here = TRUE;
6408 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006409 }
6410 else if (bytelen <= clen)
6411 {
6412 /* match current character, jump ahead to out of
6413 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006414 add_state = t->state->out->out;
6415 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006416 }
6417 else
6418 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006419 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006420 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006421 add_state = t->state->out;
6422 add_off = bytelen;
6423 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006424 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006425 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006426 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006427 }
6428 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006429 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006430 if (t->count - clen <= 0)
6431 {
6432 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006433 add_state = t->state->out;
6434 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006435 }
6436 else
6437 {
6438 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006439 add_state = t->state;
6440 add_off = 0;
6441 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006442 }
6443 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006444
Bram Moolenaar423532e2013-05-29 21:14:42 +02006445 case NFA_LNUM:
6446 case NFA_LNUM_GT:
6447 case NFA_LNUM_LT:
6448 result = (REG_MULTI &&
6449 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6450 (long_u)(reglnum + reg_firstlnum)));
6451 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006452 {
6453 add_here = TRUE;
6454 add_state = t->state->out;
6455 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006456 break;
6457
6458 case NFA_COL:
6459 case NFA_COL_GT:
6460 case NFA_COL_LT:
6461 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6462 (long_u)(reginput - regline) + 1);
6463 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006464 {
6465 add_here = TRUE;
6466 add_state = t->state->out;
6467 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006468 break;
6469
6470 case NFA_VCOL:
6471 case NFA_VCOL_GT:
6472 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006473 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006474 int op = t->state->c - NFA_VCOL;
6475 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaaref795d12015-01-18 16:46:32 +01006476 win_T *wp = reg_win == NULL ? curwin : reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006477
6478 /* Bail out quickly when there can't be a match, avoid the
6479 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006480 if (op != 1 && col > t->state->val
6481#ifdef FEAT_MBYTE
6482 * (has_mbyte ? MB_MAXBYTES : 1)
6483#endif
6484 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006485 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006486 result = FALSE;
6487 if (op == 1 && col - 1 > t->state->val && col > 100)
6488 {
6489 int ts = wp->w_buffer->b_p_ts;
6490
6491 /* Guess that a character won't use more columns than
6492 * 'tabstop', with a minimum of 4. */
6493 if (ts < 4)
6494 ts = 4;
6495 result = col > t->state->val * ts;
6496 }
6497 if (!result)
6498 result = nfa_re_num_cmp(t->state->val, op,
6499 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006500 if (result)
6501 {
6502 add_here = TRUE;
6503 add_state = t->state->out;
6504 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006505 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006506 break;
6507
Bram Moolenaar044aa292013-06-04 21:27:38 +02006508 case NFA_MARK:
6509 case NFA_MARK_GT:
6510 case NFA_MARK_LT:
6511 {
6512 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6513
6514 /* Compare the mark position to the match position. */
6515 result = (pos != NULL /* mark doesn't exist */
6516 && pos->lnum > 0 /* mark isn't set in reg_buf */
6517 && (pos->lnum == reglnum + reg_firstlnum
6518 ? (pos->col == (colnr_T)(reginput - regline)
6519 ? t->state->c == NFA_MARK
6520 : (pos->col < (colnr_T)(reginput - regline)
6521 ? t->state->c == NFA_MARK_GT
6522 : t->state->c == NFA_MARK_LT))
6523 : (pos->lnum < reglnum + reg_firstlnum
6524 ? t->state->c == NFA_MARK_GT
6525 : t->state->c == NFA_MARK_LT)));
6526 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006527 {
6528 add_here = TRUE;
6529 add_state = t->state->out;
6530 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006531 break;
6532 }
6533
Bram Moolenaar423532e2013-05-29 21:14:42 +02006534 case NFA_CURSOR:
6535 result = (reg_win != NULL
6536 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6537 && ((colnr_T)(reginput - regline)
6538 == reg_win->w_cursor.col));
6539 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006540 {
6541 add_here = TRUE;
6542 add_state = t->state->out;
6543 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006544 break;
6545
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006546 case NFA_VISUAL:
6547 result = reg_match_visual();
6548 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006549 {
6550 add_here = TRUE;
6551 add_state = t->state->out;
6552 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006553 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006554
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006555 case NFA_MOPEN1:
6556 case NFA_MOPEN2:
6557 case NFA_MOPEN3:
6558 case NFA_MOPEN4:
6559 case NFA_MOPEN5:
6560 case NFA_MOPEN6:
6561 case NFA_MOPEN7:
6562 case NFA_MOPEN8:
6563 case NFA_MOPEN9:
6564#ifdef FEAT_SYN_HL
6565 case NFA_ZOPEN:
6566 case NFA_ZOPEN1:
6567 case NFA_ZOPEN2:
6568 case NFA_ZOPEN3:
6569 case NFA_ZOPEN4:
6570 case NFA_ZOPEN5:
6571 case NFA_ZOPEN6:
6572 case NFA_ZOPEN7:
6573 case NFA_ZOPEN8:
6574 case NFA_ZOPEN9:
6575#endif
6576 case NFA_NOPEN:
6577 case NFA_ZSTART:
6578 /* These states are only added to be able to bail out when
6579 * they are added again, nothing is to be done. */
6580 break;
6581
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006582 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006583 {
6584 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006585
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006586#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006587 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006588 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006589#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006590 result = (c == curc);
6591
6592 if (!result && ireg_ic)
6593 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006594#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006595 /* If ireg_icombine is not set only skip over the character
6596 * itself. When it is set skip over composing characters. */
6597 if (result && enc_utf8 && !ireg_icombine)
6598 clen = utf_char2len(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006599#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006600 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006601 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006602 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006603
6604 } /* switch (t->state->c) */
6605
6606 if (add_state != NULL)
6607 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006608 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006609 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006610
6611 if (t->pim.result == NFA_PIM_UNUSED)
6612 pim = NULL;
6613 else
6614 pim = &t->pim;
6615
6616 /* Handle the postponed invisible match if the match might end
6617 * without advancing and before the end of the line. */
6618 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006619 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006620 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006621 {
6622#ifdef ENABLE_LOG
6623 fprintf(log_fd, "\n");
6624 fprintf(log_fd, "==================================\n");
6625 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6626 fprintf(log_fd, "\n");
6627#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006628 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006629 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006630 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006631 /* for \@! and \@<! it is a match when the result is
6632 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006633 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006634 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6635 || pim->state->c
6636 == NFA_START_INVISIBLE_BEFORE_NEG
6637 || pim->state->c
6638 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006639 {
6640 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006641 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006642#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006643 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006644 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006645#endif
6646 }
6647 }
6648 else
6649 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006650 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006651#ifdef ENABLE_LOG
6652 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006653 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006654 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6655 fprintf(log_fd, "\n");
6656#endif
6657 }
6658
Bram Moolenaardecd9542013-06-07 16:31:50 +02006659 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006660 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006661 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6662 || pim->state->c
6663 == NFA_START_INVISIBLE_BEFORE_NEG
6664 || pim->state->c
6665 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006666 {
6667 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006668 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006669#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006670 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006671 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006672#endif
6673 }
6674 else
6675 /* look-behind match failed, don't add the state */
6676 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006677
6678 /* Postponed invisible match was handled, don't add it to
6679 * following states. */
6680 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006681 }
6682
Bram Moolenaara951e352013-10-06 15:46:11 +02006683 /* If "pim" points into l->t it will become invalid when
6684 * adding the state causes the list to be reallocated. Make a
6685 * local copy to avoid that. */
6686 if (pim == &t->pim)
6687 {
6688 copy_pim(&pim_copy, pim);
6689 pim = &pim_copy;
6690 }
6691
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006692 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006693 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006694 else
6695 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006696 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006697 if (add_count > 0)
6698 nextlist->t[nextlist->n - 1].count = add_count;
6699 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006700 }
6701
6702 } /* for (thislist = thislist; thislist->state; thislist++) */
6703
Bram Moolenaare23febd2013-05-26 18:40:14 +02006704 /* Look for the start of a match in the current position by adding the
6705 * start state to the list of states.
6706 * The first found match is the leftmost one, thus the order of states
6707 * matters!
6708 * Do not add the start state in recursive calls of nfa_regmatch(),
6709 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006710 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006711 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006712 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006713 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006714 && reglnum == 0
6715 && clen != 0
6716 && (ireg_maxcol == 0
6717 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006718 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006719 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006720 ? (reglnum < nfa_endp->se_u.pos.lnum
6721 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006722 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006723 < nfa_endp->se_u.pos.col))
6724 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006725 {
6726#ifdef ENABLE_LOG
6727 fprintf(log_fd, "(---) STARTSTATE\n");
6728#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006729 /* Inline optimized code for addstate() if we know the state is
6730 * the first MOPEN. */
6731 if (toplevel)
6732 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006733 int add = TRUE;
6734 int c;
6735
6736 if (prog->regstart != NUL && clen != 0)
6737 {
6738 if (nextlist->n == 0)
6739 {
6740 colnr_T col = (colnr_T)(reginput - regline) + clen;
6741
6742 /* Nextlist is empty, we can skip ahead to the
6743 * character that must appear at the start. */
6744 if (skip_to_start(prog->regstart, &col) == FAIL)
6745 break;
6746#ifdef ENABLE_LOG
6747 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6748 col - ((colnr_T)(reginput - regline) + clen));
6749#endif
6750 reginput = regline + col - clen;
6751 }
6752 else
6753 {
6754 /* Checking if the required start character matches is
6755 * cheaper than adding a state that won't match. */
6756 c = PTR2CHAR(reginput + clen);
6757 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6758 != MB_TOLOWER(prog->regstart)))
6759 {
6760#ifdef ENABLE_LOG
6761 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6762#endif
6763 add = FALSE;
6764 }
6765 }
6766 }
6767
6768 if (add)
6769 {
6770 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006771 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006772 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006773 else
6774 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006775 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006776 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006777 }
6778 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006779 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006780 }
6781
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006782#ifdef ENABLE_LOG
6783 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006784 {
6785 int i;
6786
6787 for (i = 0; i < thislist->n; i++)
6788 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6789 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006790 fprintf(log_fd, "\n");
6791#endif
6792
6793nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006794 /* Advance to the next character, or advance to the next line, or
6795 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006796 if (clen != 0)
6797 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006798 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6799 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006800 reg_nextline();
6801 else
6802 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006803
6804 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006805 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006806 if (got_int)
6807 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006808#ifdef FEAT_RELTIME
6809 /* Check for timeout once in a twenty times to avoid overhead. */
6810 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6811 {
6812 nfa_time_count = 0;
6813 if (profile_passed_limit(nfa_time_limit))
6814 break;
6815 }
6816#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006817 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006818
6819#ifdef ENABLE_LOG
6820 if (log_fd != stderr)
6821 fclose(log_fd);
6822 log_fd = NULL;
6823#endif
6824
6825theend:
6826 /* Free memory */
6827 vim_free(list[0].t);
6828 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006829 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006830#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006831#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006832 fclose(debug);
6833#endif
6834
Bram Moolenaar963fee22013-05-26 21:47:28 +02006835 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006836}
6837
6838/*
6839 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006840 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006841 */
6842 static long
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006843nfa_regtry(prog, col, tm)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006844 nfa_regprog_T *prog;
6845 colnr_T col;
Bram Moolenaar168e0492015-02-05 20:29:59 +01006846 proftime_T *tm UNUSED; /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006847{
6848 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006849 regsubs_T subs, m;
6850 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006851 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006852#ifdef ENABLE_LOG
6853 FILE *f;
6854#endif
6855
6856 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006857#ifdef FEAT_RELTIME
6858 nfa_time_limit = tm;
6859 nfa_time_count = 0;
6860#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006861
6862#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006863 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006864 if (f != NULL)
6865 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006866 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006867#ifdef DEBUG
6868 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6869#endif
6870 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006871 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006872 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006873 fprintf(f, "\n\n");
6874 fclose(f);
6875 }
6876 else
6877 EMSG(_("Could not open temporary log file for writing "));
6878#endif
6879
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006880 clear_sub(&subs.norm);
6881 clear_sub(&m.norm);
6882#ifdef FEAT_SYN_HL
6883 clear_sub(&subs.synt);
6884 clear_sub(&m.synt);
6885#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006886
Bram Moolenaarfda37292014-11-05 14:27:36 +01006887 result = nfa_regmatch(prog, start, &subs, &m);
6888 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006889 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006890 else if (result == NFA_TOO_EXPENSIVE)
6891 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006892
6893 cleanup_subexpr();
6894 if (REG_MULTI)
6895 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006896 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006897 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006898 reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6899 reg_startpos[i].col = subs.norm.list.multi[i].start_col;
6900
6901 reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6902 reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006903 }
6904
6905 if (reg_startpos[0].lnum < 0)
6906 {
6907 reg_startpos[0].lnum = 0;
6908 reg_startpos[0].col = col;
6909 }
6910 if (reg_endpos[0].lnum < 0)
6911 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006912 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006913 reg_endpos[0].lnum = reglnum;
6914 reg_endpos[0].col = (int)(reginput - regline);
6915 }
6916 else
6917 /* Use line number of "\ze". */
6918 reglnum = reg_endpos[0].lnum;
6919 }
6920 else
6921 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006922 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006923 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006924 reg_startp[i] = subs.norm.list.line[i].start;
6925 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006926 }
6927
6928 if (reg_startp[0] == NULL)
6929 reg_startp[0] = regline + col;
6930 if (reg_endp[0] == NULL)
6931 reg_endp[0] = reginput;
6932 }
6933
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006934#ifdef FEAT_SYN_HL
6935 /* Package any found \z(...\) matches for export. Default is none. */
6936 unref_extmatch(re_extmatch_out);
6937 re_extmatch_out = NULL;
6938
6939 if (prog->reghasz == REX_SET)
6940 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006941 cleanup_zsubexpr();
6942 re_extmatch_out = make_extmatch();
6943 for (i = 0; i < subs.synt.in_use; i++)
6944 {
6945 if (REG_MULTI)
6946 {
6947 struct multipos *mpos = &subs.synt.list.multi[i];
6948
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006949 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006950 if (mpos->start_lnum >= 0
6951 && mpos->start_lnum == mpos->end_lnum
6952 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006953 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006954 vim_strnsave(reg_getline(mpos->start_lnum)
6955 + mpos->start_col,
6956 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006957 }
6958 else
6959 {
6960 struct linepos *lpos = &subs.synt.list.line[i];
6961
6962 if (lpos->start != NULL && lpos->end != NULL)
6963 re_extmatch_out->matches[i] =
6964 vim_strnsave(lpos->start,
6965 (int)(lpos->end - lpos->start));
6966 }
6967 }
6968 }
6969#endif
6970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006971 return 1 + reglnum;
6972}
6973
6974/*
6975 * Match a regexp against a string ("line" points to the string) or multiple
6976 * lines ("line" is NULL, use reg_getline()).
6977 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01006978 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006979 */
6980 static long
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006981nfa_regexec_both(line, startcol, tm)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006982 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006983 colnr_T startcol; /* column to start looking for match */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006984 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006985{
6986 nfa_regprog_T *prog;
6987 long retval = 0L;
6988 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006989 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006990
6991 if (REG_MULTI)
6992 {
6993 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6994 line = reg_getline((linenr_T)0); /* relative to the cursor */
6995 reg_startpos = reg_mmatch->startpos;
6996 reg_endpos = reg_mmatch->endpos;
6997 }
6998 else
6999 {
7000 prog = (nfa_regprog_T *)reg_match->regprog;
7001 reg_startp = reg_match->startp;
7002 reg_endp = reg_match->endp;
7003 }
7004
7005 /* Be paranoid... */
7006 if (prog == NULL || line == NULL)
7007 {
7008 EMSG(_(e_null));
7009 goto theend;
7010 }
7011
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007012 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
7013 if (prog->regflags & RF_ICASE)
7014 ireg_ic = TRUE;
7015 else if (prog->regflags & RF_NOICASE)
7016 ireg_ic = FALSE;
7017
7018#ifdef FEAT_MBYTE
7019 /* If pattern contains "\Z" overrule value of ireg_icombine */
7020 if (prog->regflags & RF_ICOMBINE)
7021 ireg_icombine = TRUE;
7022#endif
7023
7024 regline = line;
7025 reglnum = 0; /* relative to line */
7026
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007027 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007028 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007029 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007030 nfa_listid = 1;
7031 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007032 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007033
Bram Moolenaard89616e2013-06-06 18:46:06 +02007034 if (prog->reganch && col > 0)
7035 return 0L;
7036
Bram Moolenaar473de612013-06-08 18:19:48 +02007037 need_clear_subexpr = TRUE;
7038#ifdef FEAT_SYN_HL
7039 /* Clear the external match subpointers if necessary. */
7040 if (prog->reghasz == REX_SET)
7041 {
7042 nfa_has_zsubexpr = TRUE;
7043 need_clear_zsubexpr = TRUE;
7044 }
7045 else
7046 nfa_has_zsubexpr = FALSE;
7047#endif
7048
Bram Moolenaard89616e2013-06-06 18:46:06 +02007049 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007050 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007051 /* Skip ahead until a character we know the match must start with.
7052 * When there is none there is no match. */
7053 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007054 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007055
Bram Moolenaar473de612013-06-08 18:19:48 +02007056 /* If match_text is set it contains the full text that must match.
7057 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007058 if (prog->match_text != NULL
7059#ifdef FEAT_MBYTE
7060 && !ireg_icombine
7061#endif
7062 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007063 return find_match_text(col, prog->regstart, prog->match_text);
7064 }
7065
Bram Moolenaard89616e2013-06-06 18:46:06 +02007066 /* If the start column is past the maximum column: no need to try. */
7067 if (ireg_maxcol > 0 && col >= ireg_maxcol)
7068 goto theend;
7069
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007070 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007071 for (i = 0; i < nstate; ++i)
7072 {
7073 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007074 prog->state[i].lastlist[0] = 0;
7075 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007076 }
7077
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007078 retval = nfa_regtry(prog, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007079
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007080 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007081
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007082theend:
7083 return retval;
7084}
7085
7086/*
7087 * Compile a regular expression into internal code for the NFA matcher.
7088 * Returns the program in allocated space. Returns NULL for an error.
7089 */
7090 static regprog_T *
7091nfa_regcomp(expr, re_flags)
7092 char_u *expr;
7093 int re_flags;
7094{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007095 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007096 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007097 int *postfix;
7098
7099 if (expr == NULL)
7100 return NULL;
7101
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007102 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007103 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007104
7105 init_class_tab();
7106
7107 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7108 return NULL;
7109
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007110 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007111 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007112 postfix = re2post();
7113 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007114 {
7115 /* TODO: only give this error for debugging? */
7116 if (post_ptr >= post_end)
7117 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007118 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007119 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007120
7121 /*
7122 * In order to build the NFA, we parse the input regexp twice:
7123 * 1. first pass to count size (so we can allocate space)
7124 * 2. second to emit code
7125 */
7126#ifdef ENABLE_LOG
7127 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007128 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007129
7130 if (f != NULL)
7131 {
7132 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7133 fclose(f);
7134 }
7135 }
7136#endif
7137
7138 /*
7139 * PASS 1
7140 * Count number of NFA states in "nstate". Do not build the NFA.
7141 */
7142 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007143
Bram Moolenaar16619a22013-06-11 18:42:36 +02007144 /* allocate the regprog with space for the compiled regexp */
7145 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007146 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7147 if (prog == NULL)
7148 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007149 state_ptr = prog->state;
7150
7151 /*
7152 * PASS 2
7153 * Build the NFA
7154 */
7155 prog->start = post2nfa(postfix, post_ptr, FALSE);
7156 if (prog->start == NULL)
7157 goto fail;
7158
7159 prog->regflags = regflags;
7160 prog->engine = &nfa_regengine;
7161 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007162 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007163 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007164 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007165
Bram Moolenaara2947e22013-06-11 22:44:09 +02007166 nfa_postprocess(prog);
7167
Bram Moolenaard89616e2013-06-06 18:46:06 +02007168 prog->reganch = nfa_get_reganch(prog->start, 0);
7169 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007170 prog->match_text = nfa_get_match_text(prog->start);
7171
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007172#ifdef ENABLE_LOG
7173 nfa_postfix_dump(expr, OK);
7174 nfa_dump(prog);
7175#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007176#ifdef FEAT_SYN_HL
7177 /* Remember whether this pattern has any \z specials in it. */
7178 prog->reghasz = re_has_z;
7179#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007180 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007181 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007182
7183out:
7184 vim_free(post_start);
7185 post_start = post_ptr = post_end = NULL;
7186 state_ptr = NULL;
7187 return (regprog_T *)prog;
7188
7189fail:
7190 vim_free(prog);
7191 prog = NULL;
7192#ifdef ENABLE_LOG
7193 nfa_postfix_dump(expr, FAIL);
7194#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007195 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007196 goto out;
7197}
7198
Bram Moolenaar473de612013-06-08 18:19:48 +02007199/*
7200 * Free a compiled regexp program, returned by nfa_regcomp().
7201 */
7202 static void
7203nfa_regfree(prog)
7204 regprog_T *prog;
7205{
7206 if (prog != NULL)
7207 {
7208 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007209 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007210 vim_free(prog);
7211 }
7212}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007213
7214/*
7215 * Match a regexp against a string.
7216 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7217 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007218 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007220 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007221 */
7222 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007223nfa_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007224 regmatch_T *rmp;
7225 char_u *line; /* string to match against */
7226 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007227 int line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228{
7229 reg_match = rmp;
7230 reg_mmatch = NULL;
7231 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007232 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007233 reg_buf = curbuf;
7234 reg_win = NULL;
7235 ireg_ic = rmp->rm_ic;
7236#ifdef FEAT_MBYTE
7237 ireg_icombine = FALSE;
7238#endif
7239 ireg_maxcol = 0;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007240 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007241}
7242
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007243
7244/*
7245 * Match a regexp against multiple lines.
7246 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7247 * Uses curbuf for line count and 'iskeyword'.
7248 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007249 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007250 * match otherwise.
7251 *
7252 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7253 *
7254 * ! Also NOTE : match may actually be in another line. e.g.:
7255 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7256 *
7257 * +-------------------------+
7258 * |a |
7259 * |b |
7260 * |c |
7261 * | |
7262 * +-------------------------+
7263 *
7264 * then nfa_regexec_multi() returns 3. while the original
7265 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7266 *
7267 * FIXME if this behavior is not compatible.
7268 */
7269 static long
7270nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7271 regmmatch_T *rmp;
7272 win_T *win; /* window in which to search or NULL */
7273 buf_T *buf; /* buffer in which to search */
7274 linenr_T lnum; /* nr of line to start looking for match */
7275 colnr_T col; /* column to start looking for match */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007276 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007277{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007278 reg_match = NULL;
7279 reg_mmatch = rmp;
7280 reg_buf = buf;
7281 reg_win = win;
7282 reg_firstlnum = lnum;
7283 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7284 reg_line_lbr = FALSE;
7285 ireg_ic = rmp->rmm_ic;
7286#ifdef FEAT_MBYTE
7287 ireg_icombine = FALSE;
7288#endif
7289 ireg_maxcol = rmp->rmm_maxcol;
7290
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007291 return nfa_regexec_both(NULL, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007292}
7293
7294#ifdef DEBUG
7295# undef ENABLE_LOG
7296#endif