blob: 4ccb05a2b3bdaefdfd3883306326bfc939f2c7f9 [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 Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200248static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaar428e9872013-05-30 17:05:39 +0200250/* NFA regexp \1 .. \9 encountered. */
251static int nfa_has_backref;
252
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200253#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200254/* NFA regexp has \z( ), set zsubexpr. */
255static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257
Bram Moolenaar963fee22013-05-26 21:47:28 +0200258/* Number of sub expressions actually being used during execution. 1 if only
259 * the whole match (subexpr 0) is used. */
260static int nfa_nsubexpr;
261
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262static int *post_start; /* holds the postfix form of r.e. */
263static int *post_end;
264static int *post_ptr;
265
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200266static int nstate; /* Number of states in the NFA. Also used when
267 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200268static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200269
Bram Moolenaar307aa162013-06-02 16:34:21 +0200270/* If not NULL match must end at this position */
271static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200273/* listid is global, so that it increases on recursive calls to
274 * nfa_regmatch(), which means we don't have to clear the lastlist field of
275 * all the states. */
276static int nfa_listid;
277static int nfa_alt_listid;
278
279/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
280static int nfa_ll_index = 0;
281
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200282static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200283static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
284static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200285static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200286static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200288static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289static int nfa_regatom __ARGS((void));
290static int nfa_regpiece __ARGS((void));
291static int nfa_regconcat __ARGS((void));
292static int nfa_regbranch __ARGS((void));
293static int nfa_reg __ARGS((int paren));
Bram Moolenaar2d46e602014-08-29 11:56:32 +0200294static int re_mult_next __ARGS((char *what));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295#ifdef DEBUG
296static void nfa_set_code __ARGS((int c));
297static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200298static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
299static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300static void nfa_dump __ARGS((nfa_regprog_T *prog));
301#endif
302static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200303static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +0200304static void st_error __ARGS((int *postfix, int *end, int *p));
305static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200307static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200309static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
310static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200311static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200312static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200313static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
314static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200315static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaar2af78a12014-04-23 19:06:37 +0200316static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200317static 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 +0200318static int match_follows __ARGS((nfa_state_T *startstate, int depth));
319static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200320
321/* helper functions used when doing re2post() ... regatom() parsing */
322#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200323 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324 return FAIL; \
325 *post_ptr++ = c; \
326 } while (0)
327
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328/*
329 * Initialize internal variables before NFA compilation.
330 * Return OK on success, FAIL otherwise.
331 */
332 static int
333nfa_regcomp_start(expr, re_flags)
334 char_u *expr;
335 int re_flags; /* see vim_regcomp() */
336{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200337 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200338 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339
340 nstate = 0;
341 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200342 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200343 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200345 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200346 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200347 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200348
349 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200350 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200352 post_start = (int *)lalloc(postfix_size, TRUE);
353 if (post_start == NULL)
354 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200355 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200356 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200358 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200360 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200361 regcomp_start(expr, re_flags);
362
363 return OK;
364}
365
366/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200367 * Figure out if the NFA state list starts with an anchor, must match at start
368 * of the line.
369 */
370 static int
371nfa_get_reganch(start, depth)
372 nfa_state_T *start;
373 int depth;
374{
375 nfa_state_T *p = start;
376
377 if (depth > 4)
378 return 0;
379
380 while (p != NULL)
381 {
382 switch (p->c)
383 {
384 case NFA_BOL:
385 case NFA_BOF:
386 return 1; /* yes! */
387
388 case NFA_ZSTART:
389 case NFA_ZEND:
390 case NFA_CURSOR:
391 case NFA_VISUAL:
392
393 case NFA_MOPEN:
394 case NFA_MOPEN1:
395 case NFA_MOPEN2:
396 case NFA_MOPEN3:
397 case NFA_MOPEN4:
398 case NFA_MOPEN5:
399 case NFA_MOPEN6:
400 case NFA_MOPEN7:
401 case NFA_MOPEN8:
402 case NFA_MOPEN9:
403 case NFA_NOPEN:
404#ifdef FEAT_SYN_HL
405 case NFA_ZOPEN:
406 case NFA_ZOPEN1:
407 case NFA_ZOPEN2:
408 case NFA_ZOPEN3:
409 case NFA_ZOPEN4:
410 case NFA_ZOPEN5:
411 case NFA_ZOPEN6:
412 case NFA_ZOPEN7:
413 case NFA_ZOPEN8:
414 case NFA_ZOPEN9:
415#endif
416 p = p->out;
417 break;
418
419 case NFA_SPLIT:
420 return nfa_get_reganch(p->out, depth + 1)
421 && nfa_get_reganch(p->out1, depth + 1);
422
423 default:
424 return 0; /* noooo */
425 }
426 }
427 return 0;
428}
429
430/*
431 * Figure out if the NFA state list starts with a character which must match
432 * at start of the match.
433 */
434 static int
435nfa_get_regstart(start, depth)
436 nfa_state_T *start;
437 int depth;
438{
439 nfa_state_T *p = start;
440
441 if (depth > 4)
442 return 0;
443
444 while (p != NULL)
445 {
446 switch (p->c)
447 {
448 /* all kinds of zero-width matches */
449 case NFA_BOL:
450 case NFA_BOF:
451 case NFA_BOW:
452 case NFA_EOW:
453 case NFA_ZSTART:
454 case NFA_ZEND:
455 case NFA_CURSOR:
456 case NFA_VISUAL:
457 case NFA_LNUM:
458 case NFA_LNUM_GT:
459 case NFA_LNUM_LT:
460 case NFA_COL:
461 case NFA_COL_GT:
462 case NFA_COL_LT:
463 case NFA_VCOL:
464 case NFA_VCOL_GT:
465 case NFA_VCOL_LT:
466 case NFA_MARK:
467 case NFA_MARK_GT:
468 case NFA_MARK_LT:
469
470 case NFA_MOPEN:
471 case NFA_MOPEN1:
472 case NFA_MOPEN2:
473 case NFA_MOPEN3:
474 case NFA_MOPEN4:
475 case NFA_MOPEN5:
476 case NFA_MOPEN6:
477 case NFA_MOPEN7:
478 case NFA_MOPEN8:
479 case NFA_MOPEN9:
480 case NFA_NOPEN:
481#ifdef FEAT_SYN_HL
482 case NFA_ZOPEN:
483 case NFA_ZOPEN1:
484 case NFA_ZOPEN2:
485 case NFA_ZOPEN3:
486 case NFA_ZOPEN4:
487 case NFA_ZOPEN5:
488 case NFA_ZOPEN6:
489 case NFA_ZOPEN7:
490 case NFA_ZOPEN8:
491 case NFA_ZOPEN9:
492#endif
493 p = p->out;
494 break;
495
496 case NFA_SPLIT:
497 {
498 int c1 = nfa_get_regstart(p->out, depth + 1);
499 int c2 = nfa_get_regstart(p->out1, depth + 1);
500
501 if (c1 == c2)
502 return c1; /* yes! */
503 return 0;
504 }
505
506 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200507 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200508 return p->c; /* yes! */
509 return 0;
510 }
511 }
512 return 0;
513}
514
515/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200516 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200517 * else. If so return a string in allocated memory with what must match after
518 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200519 */
520 static char_u *
521nfa_get_match_text(start)
522 nfa_state_T *start;
523{
524 nfa_state_T *p = start;
525 int len = 0;
526 char_u *ret;
527 char_u *s;
528
529 if (p->c != NFA_MOPEN)
530 return NULL; /* just in case */
531 p = p->out;
532 while (p->c > 0)
533 {
534 len += MB_CHAR2LEN(p->c);
535 p = p->out;
536 }
537 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
538 return NULL;
539
540 ret = alloc(len);
541 if (ret != NULL)
542 {
543 len = 0;
544 p = start->out->out; /* skip first char, it goes into regstart */
545 s = ret;
546 while (p->c > 0)
547 {
548#ifdef FEAT_MBYTE
549 if (has_mbyte)
550 s += (*mb_char2bytes)(p->c, s);
551 else
552#endif
553 *s++ = p->c;
554 p = p->out;
555 }
556 *s = NUL;
557 }
558 return ret;
559}
560
561/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200562 * Allocate more space for post_start. Called when
563 * running above the estimated number of states.
564 */
565 static int
566realloc_post_list()
567{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200568 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200569 int new_max = nstate_max + 1000;
570 int *new_start;
571 int *old_start;
572
573 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
574 if (new_start == NULL)
575 return FAIL;
576 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200577 old_start = post_start;
578 post_start = new_start;
579 post_ptr = new_start + (post_ptr - old_start);
580 post_end = post_start + new_max;
581 vim_free(old_start);
582 return OK;
583}
584
585/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200586 * Search between "start" and "end" and try to recognize a
587 * character class in expanded form. For example [0-9].
588 * On success, return the id the character class to be emitted.
589 * On failure, return 0 (=FAIL)
590 * Start points to the first char of the range, while end should point
591 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200592 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
593 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200594 */
595 static int
596nfa_recognize_char_class(start, end, extra_newl)
597 char_u *start;
598 char_u *end;
599 int extra_newl;
600{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200601# define CLASS_not 0x80
602# define CLASS_af 0x40
603# define CLASS_AF 0x20
604# define CLASS_az 0x10
605# define CLASS_AZ 0x08
606# define CLASS_o7 0x04
607# define CLASS_o9 0x02
608# define CLASS_underscore 0x01
609
610 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200612 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200613
614 if (extra_newl == TRUE)
615 newl = TRUE;
616
617 if (*end != ']')
618 return FAIL;
619 p = start;
620 if (*p == '^')
621 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200622 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200623 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200624 }
625
626 while (p < end)
627 {
628 if (p + 2 < end && *(p + 1) == '-')
629 {
630 switch (*p)
631 {
632 case '0':
633 if (*(p + 2) == '9')
634 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200635 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200636 break;
637 }
638 else
639 if (*(p + 2) == '7')
640 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200641 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200642 break;
643 }
644 case 'a':
645 if (*(p + 2) == 'z')
646 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200647 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200648 break;
649 }
650 else
651 if (*(p + 2) == 'f')
652 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200653 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200654 break;
655 }
656 case 'A':
657 if (*(p + 2) == 'Z')
658 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200659 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660 break;
661 }
662 else
663 if (*(p + 2) == 'F')
664 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200665 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200666 break;
667 }
668 /* FALLTHROUGH */
669 default:
670 return FAIL;
671 }
672 p += 3;
673 }
674 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
675 {
676 newl = TRUE;
677 p += 2;
678 }
679 else if (*p == '_')
680 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200681 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200682 p ++;
683 }
684 else if (*p == '\n')
685 {
686 newl = TRUE;
687 p ++;
688 }
689 else
690 return FAIL;
691 } /* while (p < end) */
692
693 if (p != end)
694 return FAIL;
695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200696 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200697 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200698
699 switch (config)
700 {
701 case CLASS_o9:
702 return extra_newl + NFA_DIGIT;
703 case CLASS_not | CLASS_o9:
704 return extra_newl + NFA_NDIGIT;
705 case CLASS_af | CLASS_AF | CLASS_o9:
706 return extra_newl + NFA_HEX;
707 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
708 return extra_newl + NFA_NHEX;
709 case CLASS_o7:
710 return extra_newl + NFA_OCTAL;
711 case CLASS_not | CLASS_o7:
712 return extra_newl + NFA_NOCTAL;
713 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
714 return extra_newl + NFA_WORD;
715 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
716 return extra_newl + NFA_NWORD;
717 case CLASS_az | CLASS_AZ | CLASS_underscore:
718 return extra_newl + NFA_HEAD;
719 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
720 return extra_newl + NFA_NHEAD;
721 case CLASS_az | CLASS_AZ:
722 return extra_newl + NFA_ALPHA;
723 case CLASS_not | CLASS_az | CLASS_AZ:
724 return extra_newl + NFA_NALPHA;
725 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200726 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200727 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200728 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200729 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200730 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200731 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200732 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200733 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200734 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735}
736
737/*
738 * Produce the bytes for equivalence class "c".
739 * Currently only handles latin1, latin9 and utf-8.
740 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
741 * equivalent to 'a OR b OR c'
742 *
743 * NOTE! When changing this function, also update reg_equi_class()
744 */
745 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200746nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200747 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200748{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200749#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
750#ifdef FEAT_MBYTE
751# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
752#else
753# define EMITMBC(c)
754#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200755
756#ifdef FEAT_MBYTE
757 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
758 || STRCMP(p_enc, "iso-8859-15") == 0)
759#endif
760 {
761 switch (c)
762 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200763 case 'A': case 0300: case 0301: case 0302:
764 case 0303: case 0304: case 0305:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200765 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
766 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
767 EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302);
768 EMIT2(0303); EMIT2(0304); EMIT2(0305);
769 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
770 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
771 EMITMBC(0x1ea2)
772 return OK;
773
774 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
775 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 return OK;
777
Bram Moolenaar417bad22013-06-07 14:08:30 +0200778 case 'C': case 0307:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200779 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
780 EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108)
781 EMITMBC(0x10a) EMITMBC(0x10c)
782 return OK;
783
784 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
785 CASEMBC(0x1e0e) CASEMBC(0x1e10)
786 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
787 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 return OK;
789
Bram Moolenaar417bad22013-06-07 14:08:30 +0200790 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200791 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
792 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
793 EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312);
794 EMIT2(0313);
795 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
796 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
797 EMITMBC(0x1ebc)
798 return OK;
799
800 case 'F': CASEMBC(0x1e1e)
801 EMIT2('F'); EMITMBC(0x1e1e)
802 return OK;
803
804 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
805 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
806 CASEMBC(0x1e20)
807 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
808 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
809 EMITMBC(0x1f4) EMITMBC(0x1e20)
810 return OK;
811
812 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
813 CASEMBC(0x1e26) CASEMBC(0x1e28)
814 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
815 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 return OK;
817
Bram Moolenaar417bad22013-06-07 14:08:30 +0200818 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200819 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
820 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
821 EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316);
822 EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a)
823 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
824 EMITMBC(0x1cf) EMITMBC(0x1ec8)
825 return OK;
826
827 case 'J': CASEMBC(0x134)
828 EMIT2('J'); EMITMBC(0x134)
829 return OK;
830
831 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
832 CASEMBC(0x1e34)
833 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
834 EMITMBC(0x1e34)
835 return OK;
836
837 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
838 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
839 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
840 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
841 return OK;
842
843 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
844 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200845 return OK;
846
Bram Moolenaar417bad22013-06-07 14:08:30 +0200847 case 'N': case 0321:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200848 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
849 CASEMBC(0x1e48)
850 EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145)
851 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200852 return OK;
853
Bram Moolenaar417bad22013-06-07 14:08:30 +0200854 case 'O': case 0322: case 0323: case 0324: case 0325:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200855 case 0326: case 0330:
856 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
857 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
858 EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324);
859 EMIT2(0325); EMIT2(0326); EMIT2(0330);
860 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
861 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
862 EMITMBC(0x1ec) EMITMBC(0x1ece)
863 return OK;
864
865 case 'P': case 0x1e54: case 0x1e56:
866 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
867 return OK;
868
869 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
870 CASEMBC(0x1e58) CASEMBC(0x1e5e)
871 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
872 EMITMBC(0x1e58) EMITMBC(0x1e5e)
873 return OK;
874
875 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
876 CASEMBC(0x160) CASEMBC(0x1e60)
877 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
878 EMITMBC(0x160) EMITMBC(0x1e60)
879 return OK;
880
881 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
882 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
883 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
884 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200885 return OK;
886
Bram Moolenaar417bad22013-06-07 14:08:30 +0200887 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200888 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
889 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
890 CASEMBC(0x1ee6)
891 EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333);
892 EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a)
893 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
894 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
895 EMITMBC(0x1ee6)
896 return OK;
897
898 case 'V': CASEMBC(0x1e7c)
899 EMIT2('V'); EMITMBC(0x1e7c)
900 return OK;
901
902 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
903 CASEMBC(0x1e84) CASEMBC(0x1e86)
904 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
905 EMITMBC(0x1e84) EMITMBC(0x1e86)
906 return OK;
907
908 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
909 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200910 return OK;
911
Bram Moolenaar417bad22013-06-07 14:08:30 +0200912 case 'Y': case 0335:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200913 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
914 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
915 EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178)
916 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
917 EMITMBC(0x1ef8)
918 return OK;
919
920 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
921 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
922 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
923 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200924 return OK;
925
Bram Moolenaar417bad22013-06-07 14:08:30 +0200926 case 'a': case 0340: case 0341: case 0342:
927 case 0343: case 0344: case 0345:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200928 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
929 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
930 EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342);
931 EMIT2(0343); EMIT2(0344); EMIT2(0345);
932 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
933 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
934 EMITMBC(0x1ea3)
935 return OK;
936
937 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
938 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200939 return OK;
940
Bram Moolenaar417bad22013-06-07 14:08:30 +0200941 case 'c': case 0347:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200942 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
943 EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109)
944 EMITMBC(0x10b) EMITMBC(0x10d)
945 return OK;
946
947 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
948 CASEMBC(0x1e11)
949 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) EMITMBC(0x1e0b)
950 EMITMBC(0x01e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200951 return OK;
952
Bram Moolenaar417bad22013-06-07 14:08:30 +0200953 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200954 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
955 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
956 EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352);
957 EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115)
958 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
959 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
960 return OK;
961
962 case 'f': CASEMBC(0x1e1f)
963 EMIT2('f'); EMITMBC(0x1e1f)
964 return OK;
965
966 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
967 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
968 CASEMBC(0x1e21)
969 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
970 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
971 EMITMBC(0x1f5) EMITMBC(0x1e21)
972 return OK;
973
974 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
975 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
976 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
977 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200978 return OK;
979
Bram Moolenaar417bad22013-06-07 14:08:30 +0200980 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200981 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
982 CASEMBC(0x1d0) CASEMBC(0x1ec9)
983 EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356);
984 EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b)
985 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
986 EMITMBC(0x1ec9)
987 return OK;
988
989 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
990 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
991 return OK;
992
993 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
994 CASEMBC(0x1e35)
995 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
996 EMITMBC(0x1e35)
997 return OK;
998
999 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1000 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1001 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1002 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1003 return OK;
1004
1005 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1006 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001007 return OK;
1008
Bram Moolenaar417bad22013-06-07 14:08:30 +02001009 case 'n': case 0361:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1011 CASEMBC(0x1e45) CASEMBC(0x1e49)
1012 EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146)
1013 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1014 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001015 return OK;
1016
Bram Moolenaar417bad22013-06-07 14:08:30 +02001017 case 'o': case 0362: case 0363: case 0364: case 0365:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001018 case 0366: case 0370:
1019 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1020 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
1021 EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364);
1022 EMIT2(0365); EMIT2(0366); EMIT2(0370);
1023 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1024 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1025 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1026 return OK;
1027
1028 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1029 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1030 return OK;
1031
1032 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1033 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1034 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1035 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1036 return OK;
1037
1038 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1039 CASEMBC(0x161) CASEMBC(0x1e61)
1040 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1041 EMITMBC(0x161) EMITMBC(0x1e61)
1042 return OK;
1043
1044 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1045 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1046 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1047 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001048 return OK;
1049
Bram Moolenaar417bad22013-06-07 14:08:30 +02001050 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001051 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1052 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1053 CASEMBC(0x1ee7)
1054 EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373);
1055 EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b)
1056 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1057 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1058 EMITMBC(0x1ee7)
1059 return OK;
1060
1061 case 'v': CASEMBC(0x1e7d)
1062 EMIT2('v'); EMITMBC(0x1e7d)
1063 return OK;
1064
1065 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1066 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1067 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1068 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1069 return OK;
1070
1071 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1072 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001073 return OK;
1074
Bram Moolenaar417bad22013-06-07 14:08:30 +02001075 case 'y': case 0375: case 0377:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001076 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1077 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1078 EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177)
1079 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1080 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001081 return OK;
1082
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001083 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1084 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1085 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1086 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1087 return OK;
1088
1089 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001090 }
1091 }
1092
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001093 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001094 return OK;
1095#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001096#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001097}
1098
1099/*
1100 * Code to parse regular expression.
1101 *
1102 * We try to reuse parsing functions in regexp.c to
1103 * minimize surprise and keep the syntax consistent.
1104 */
1105
1106/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001107 * Parse the lowest level.
1108 *
1109 * An atom can be one of a long list of items. Many atoms match one character
1110 * in the text. It is often an ordinary character or a character class.
1111 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1112 * is only for syntax highlighting.
1113 *
1114 * atom ::= ordinary-atom
1115 * or \( pattern \)
1116 * or \%( pattern \)
1117 * or \z( pattern \)
1118 */
1119 static int
1120nfa_regatom()
1121{
1122 int c;
1123 int charclass;
1124 int equiclass;
1125 int collclass;
1126 int got_coll_char;
1127 char_u *p;
1128 char_u *endp;
1129#ifdef FEAT_MBYTE
1130 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001131#endif
1132 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001133 int emit_range;
1134 int negated;
1135 int result;
1136 int startc = -1;
1137 int endc = -1;
1138 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001140 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 switch (c)
1142 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001143 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001144 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001145
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001146 case Magic('^'):
1147 EMIT(NFA_BOL);
1148 break;
1149
1150 case Magic('$'):
1151 EMIT(NFA_EOL);
1152#if defined(FEAT_SYN_HL) || defined(PROTO)
1153 had_eol = TRUE;
1154#endif
1155 break;
1156
1157 case Magic('<'):
1158 EMIT(NFA_BOW);
1159 break;
1160
1161 case Magic('>'):
1162 EMIT(NFA_EOW);
1163 break;
1164
1165 case Magic('_'):
1166 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001167 if (c == NUL)
1168 EMSG_RET_FAIL(_(e_nul_found));
1169
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001170 if (c == '^') /* "\_^" is start-of-line */
1171 {
1172 EMIT(NFA_BOL);
1173 break;
1174 }
1175 if (c == '$') /* "\_$" is end-of-line */
1176 {
1177 EMIT(NFA_EOL);
1178#if defined(FEAT_SYN_HL) || defined(PROTO)
1179 had_eol = TRUE;
1180#endif
1181 break;
1182 }
1183
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001184 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001185
1186 /* "\_[" is collection plus newline */
1187 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001188 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001189
1190 /* "\_x" is character class plus newline */
1191 /*FALLTHROUGH*/
1192
1193 /*
1194 * Character classes.
1195 */
1196 case Magic('.'):
1197 case Magic('i'):
1198 case Magic('I'):
1199 case Magic('k'):
1200 case Magic('K'):
1201 case Magic('f'):
1202 case Magic('F'):
1203 case Magic('p'):
1204 case Magic('P'):
1205 case Magic('s'):
1206 case Magic('S'):
1207 case Magic('d'):
1208 case Magic('D'):
1209 case Magic('x'):
1210 case Magic('X'):
1211 case Magic('o'):
1212 case Magic('O'):
1213 case Magic('w'):
1214 case Magic('W'):
1215 case Magic('h'):
1216 case Magic('H'):
1217 case Magic('a'):
1218 case Magic('A'):
1219 case Magic('l'):
1220 case Magic('L'):
1221 case Magic('u'):
1222 case Magic('U'):
1223 p = vim_strchr(classchars, no_Magic(c));
1224 if (p == NULL)
1225 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001226 if (extra == NFA_ADD_NL)
1227 {
1228 EMSGN(_(e_ill_char_class), c);
1229 rc_did_emsg = TRUE;
1230 return FAIL;
1231 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001232 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1233 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 }
1235#ifdef FEAT_MBYTE
1236 /* When '.' is followed by a composing char ignore the dot, so that
1237 * the composing char is matched here. */
1238 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1239 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001240 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 c = getchr();
1242 goto nfa_do_multibyte;
1243 }
1244#endif
1245 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001246 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001247 {
1248 EMIT(NFA_NEWL);
1249 EMIT(NFA_OR);
1250 regflags |= RF_HASNL;
1251 }
1252 break;
1253
1254 case Magic('n'):
1255 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001256 /* In a string "\n" matches a newline character. */
1257 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001258 else
1259 {
1260 /* In buffer text "\n" matches the end of a line. */
1261 EMIT(NFA_NEWL);
1262 regflags |= RF_HASNL;
1263 }
1264 break;
1265
1266 case Magic('('):
1267 if (nfa_reg(REG_PAREN) == FAIL)
1268 return FAIL; /* cascaded error */
1269 break;
1270
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271 case Magic('|'):
1272 case Magic('&'):
1273 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001274 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001275 return FAIL;
1276
1277 case Magic('='):
1278 case Magic('?'):
1279 case Magic('+'):
1280 case Magic('@'):
1281 case Magic('*'):
1282 case Magic('{'):
1283 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001284 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001285 return FAIL;
1286
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001287 case Magic('~'):
1288 {
1289 char_u *lp;
1290
1291 /* Previous substitute pattern.
1292 * Generated as "\%(pattern\)". */
1293 if (reg_prev_sub == NULL)
1294 {
1295 EMSG(_(e_nopresub));
1296 return FAIL;
1297 }
1298 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1299 {
1300 EMIT(PTR2CHAR(lp));
1301 if (lp != reg_prev_sub)
1302 EMIT(NFA_CONCAT);
1303 }
1304 EMIT(NFA_NOPEN);
1305 break;
1306 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001307
Bram Moolenaar428e9872013-05-30 17:05:39 +02001308 case Magic('1'):
1309 case Magic('2'):
1310 case Magic('3'):
1311 case Magic('4'):
1312 case Magic('5'):
1313 case Magic('6'):
1314 case Magic('7'):
1315 case Magic('8'):
1316 case Magic('9'):
1317 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1318 nfa_has_backref = TRUE;
1319 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320
1321 case Magic('z'):
1322 c = no_Magic(getchr());
1323 switch (c)
1324 {
1325 case 's':
1326 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001327 if (re_mult_next("\\zs") == FAIL)
1328 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001329 break;
1330 case 'e':
1331 EMIT(NFA_ZEND);
1332 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001333 if (re_mult_next("\\ze") == FAIL)
1334 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001335 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001336#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001337 case '1':
1338 case '2':
1339 case '3':
1340 case '4':
1341 case '5':
1342 case '6':
1343 case '7':
1344 case '8':
1345 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001346 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001347 if (reg_do_extmatch != REX_USE)
1348 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001349 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1350 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001351 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001352 re_has_z = REX_USE;
1353 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001354 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001355 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001356 if (reg_do_extmatch != REX_SET)
1357 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001358 if (nfa_reg(REG_ZPAREN) == FAIL)
1359 return FAIL; /* cascaded error */
1360 re_has_z = REX_SET;
1361 break;
1362#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001363 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001364 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001365 no_Magic(c));
1366 return FAIL;
1367 }
1368 break;
1369
1370 case Magic('%'):
1371 c = no_Magic(getchr());
1372 switch (c)
1373 {
1374 /* () without a back reference */
1375 case '(':
1376 if (nfa_reg(REG_NPAREN) == FAIL)
1377 return FAIL;
1378 EMIT(NFA_NOPEN);
1379 break;
1380
1381 case 'd': /* %d123 decimal */
1382 case 'o': /* %o123 octal */
1383 case 'x': /* %xab hex 2 */
1384 case 'u': /* %uabcd hex 4 */
1385 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001386 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001387 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001388
Bram Moolenaar47196582013-05-25 22:04:23 +02001389 switch (c)
1390 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001391 case 'd': nr = getdecchrs(); break;
1392 case 'o': nr = getoctchrs(); break;
1393 case 'x': nr = gethexchrs(2); break;
1394 case 'u': nr = gethexchrs(4); break;
1395 case 'U': nr = gethexchrs(8); break;
1396 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001397 }
1398
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001399 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001400 EMSG2_RET_FAIL(
1401 _("E678: Invalid character after %s%%[dxouU]"),
1402 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001403 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001404 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001405 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001406 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 break;
1408
1409 /* Catch \%^ and \%$ regardless of where they appear in the
1410 * pattern -- regardless of whether or not it makes sense. */
1411 case '^':
1412 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 break;
1414
1415 case '$':
1416 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 break;
1418
1419 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001420 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001421 break;
1422
1423 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001424 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001425 break;
1426
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001427 case 'C':
1428 EMIT(NFA_ANY_COMPOSING);
1429 break;
1430
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001431 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001432 {
1433 int n;
1434
1435 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001436 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001437 {
1438 if (c == NUL)
1439 EMSG2_RET_FAIL(_(e_missing_sb),
1440 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001441 /* recursive call! */
1442 if (nfa_regatom() == FAIL)
1443 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001444 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001445 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001446 if (n == 0)
1447 EMSG2_RET_FAIL(_(e_empty_sb),
1448 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001449 EMIT(NFA_OPT_CHARS);
1450 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001451
1452 /* Emit as "\%(\%[abc]\)" to be able to handle
1453 * "\%[abc]*" which would cause the empty string to be
1454 * matched an unlimited number of times. NFA_NOPEN is
1455 * added only once at a position, while NFA_SPLIT is
1456 * added multiple times. This is more efficient than
1457 * not allowsing NFA_SPLIT multiple times, it is used
1458 * a lot. */
1459 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001460 break;
1461 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001462
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001463 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001464 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001465 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001466 int cmp = c;
1467
1468 if (c == '<' || c == '>')
1469 c = getchr();
1470 while (VIM_ISDIGIT(c))
1471 {
1472 n = n * 10 + (c - '0');
1473 c = getchr();
1474 }
1475 if (c == 'l' || c == 'c' || c == 'v')
1476 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001477 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001478 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001479 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001480 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001481 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001482 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001483 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001484 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001485 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001486 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001487 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001488 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001489 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001490 break;
1491 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001492 else if (c == '\'' && n == 0)
1493 {
1494 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001495 EMIT(cmp == '<' ? NFA_MARK_LT :
1496 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001497 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001498 break;
1499 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001500 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001501 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1502 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001503 return FAIL;
1504 }
1505 break;
1506
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001507 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001508collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001510 * [abc] uses NFA_START_COLL - NFA_END_COLL
1511 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1512 * Each character is produced as a regular state, using
1513 * NFA_CONCAT to bind them together.
1514 * Besides normal characters there can be:
1515 * - character classes NFA_CLASS_*
1516 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001517 */
1518
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001519 p = regparse;
1520 endp = skip_anyof(p);
1521 if (*endp == ']')
1522 {
1523 /*
1524 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001525 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001526 * and perform the necessary substitutions in the NFA.
1527 */
1528 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001529 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 if (result != FAIL)
1531 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001532 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001533 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001534 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001535 EMIT(NFA_NEWL);
1536 EMIT(NFA_OR);
1537 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001538 else
1539 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001541 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 return OK;
1543 }
1544 /*
1545 * Failed to recognize a character class. Use the simple
1546 * version that turns [abc] into 'a' OR 'b' OR 'c'
1547 */
1548 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 if (*regparse == '^') /* negated range */
1551 {
1552 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001553 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001554 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001556 else
1557 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001558 if (*regparse == '-')
1559 {
1560 startc = '-';
1561 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001562 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001563 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 }
1565 /* Emit the OR branches for each character in the [] */
1566 emit_range = FALSE;
1567 while (regparse < endp)
1568 {
1569 oldstartc = startc;
1570 startc = -1;
1571 got_coll_char = FALSE;
1572 if (*regparse == '[')
1573 {
1574 /* Check for [: :], [= =], [. .] */
1575 equiclass = collclass = 0;
1576 charclass = get_char_class(&regparse);
1577 if (charclass == CLASS_NONE)
1578 {
1579 equiclass = get_equi_class(&regparse);
1580 if (equiclass == 0)
1581 collclass = get_coll_element(&regparse);
1582 }
1583
1584 /* Character class like [:alpha:] */
1585 if (charclass != CLASS_NONE)
1586 {
1587 switch (charclass)
1588 {
1589 case CLASS_ALNUM:
1590 EMIT(NFA_CLASS_ALNUM);
1591 break;
1592 case CLASS_ALPHA:
1593 EMIT(NFA_CLASS_ALPHA);
1594 break;
1595 case CLASS_BLANK:
1596 EMIT(NFA_CLASS_BLANK);
1597 break;
1598 case CLASS_CNTRL:
1599 EMIT(NFA_CLASS_CNTRL);
1600 break;
1601 case CLASS_DIGIT:
1602 EMIT(NFA_CLASS_DIGIT);
1603 break;
1604 case CLASS_GRAPH:
1605 EMIT(NFA_CLASS_GRAPH);
1606 break;
1607 case CLASS_LOWER:
1608 EMIT(NFA_CLASS_LOWER);
1609 break;
1610 case CLASS_PRINT:
1611 EMIT(NFA_CLASS_PRINT);
1612 break;
1613 case CLASS_PUNCT:
1614 EMIT(NFA_CLASS_PUNCT);
1615 break;
1616 case CLASS_SPACE:
1617 EMIT(NFA_CLASS_SPACE);
1618 break;
1619 case CLASS_UPPER:
1620 EMIT(NFA_CLASS_UPPER);
1621 break;
1622 case CLASS_XDIGIT:
1623 EMIT(NFA_CLASS_XDIGIT);
1624 break;
1625 case CLASS_TAB:
1626 EMIT(NFA_CLASS_TAB);
1627 break;
1628 case CLASS_RETURN:
1629 EMIT(NFA_CLASS_RETURN);
1630 break;
1631 case CLASS_BACKSPACE:
1632 EMIT(NFA_CLASS_BACKSPACE);
1633 break;
1634 case CLASS_ESCAPE:
1635 EMIT(NFA_CLASS_ESCAPE);
1636 break;
1637 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001638 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 continue;
1640 }
1641 /* Try equivalence class [=a=] and the like */
1642 if (equiclass != 0)
1643 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001644 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 if (result == FAIL)
1646 {
1647 /* should never happen */
1648 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1649 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001650 continue;
1651 }
1652 /* Try collating class like [. .] */
1653 if (collclass != 0)
1654 {
1655 startc = collclass; /* allow [.a.]-x as a range */
1656 /* Will emit the proper atom at the end of the
1657 * while loop. */
1658 }
1659 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001660 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1661 * start character. */
1662 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 {
1664 emit_range = TRUE;
1665 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001666 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001667 continue; /* reading the end of the range */
1668 }
1669
1670 /* Now handle simple and escaped characters.
1671 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1672 * accepts "\t", "\e", etc., but only when the 'l' flag in
1673 * 'cpoptions' is not included.
1674 * Posix doesn't recognize backslash at all.
1675 */
1676 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001677 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 && regparse + 1 <= endp
1679 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001680 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 && vim_strchr(REGEXP_ABBR, regparse[1])
1682 != NULL)
1683 )
1684 )
1685 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001686 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001687
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001688 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689 startc = reg_string ? NL : NFA_NEWL;
1690 else
1691 if (*regparse == 'd'
1692 || *regparse == 'o'
1693 || *regparse == 'x'
1694 || *regparse == 'u'
1695 || *regparse == 'U'
1696 )
1697 {
1698 /* TODO(RE) This needs more testing */
1699 startc = coll_get_char();
1700 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001701 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001702 }
1703 else
1704 {
1705 /* \r,\t,\e,\b */
1706 startc = backslash_trans(*regparse);
1707 }
1708 }
1709
1710 /* Normal printable char */
1711 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001712 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713
1714 /* Previous char was '-', so this char is end of range. */
1715 if (emit_range)
1716 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001717 endc = startc;
1718 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001719 if (startc > endc)
1720 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001721
1722 if (endc > startc + 2)
1723 {
1724 /* Emit a range instead of the sequence of
1725 * individual characters. */
1726 if (startc == 0)
1727 /* \x00 is translated to \x0a, start at \x01. */
1728 EMIT(1);
1729 else
1730 --post_ptr; /* remove NFA_CONCAT */
1731 EMIT(endc);
1732 EMIT(NFA_RANGE);
1733 EMIT(NFA_CONCAT);
1734 }
1735 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001736#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001737 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 || (*mb_char2len)(endc) > 1))
1739 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001740 /* Emit the characters in the range.
1741 * "startc" was already emitted, so skip it.
1742 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743 for (c = startc + 1; c <= endc; c++)
1744 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001745 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001746 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001748 }
1749 else
1750#endif
1751 {
1752#ifdef EBCDIC
1753 int alpha_only = FALSE;
1754
1755 /* for alphabetical range skip the gaps
1756 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1757 if (isalpha(startc) && isalpha(endc))
1758 alpha_only = TRUE;
1759#endif
1760 /* Emit the range. "startc" was already emitted, so
1761 * skip it. */
1762 for (c = startc + 1; c <= endc; c++)
1763#ifdef EBCDIC
1764 if (!alpha_only || isalpha(startc))
1765#endif
1766 {
1767 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001768 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001770 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001771 emit_range = FALSE;
1772 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 }
1774 else
1775 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001776 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778 * Normally, simply emit startc. But if we get char
1779 * code=0 from a collating char, then replace it with
1780 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001781 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001782 * the backtracking engine. */
1783 if (startc == NFA_NEWL)
1784 {
1785 /* Line break can't be matched as part of the
1786 * collection, add an OR below. But not for negated
1787 * range. */
1788 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001789 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001790 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001791 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001792 {
1793 if (got_coll_char == TRUE && startc == 0)
1794 EMIT(0x0a);
1795 else
1796 EMIT(startc);
1797 EMIT(NFA_CONCAT);
1798 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 }
1800
Bram Moolenaar51a29832013-05-28 22:30:35 +02001801 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001802 } /* while (p < endp) */
1803
Bram Moolenaar51a29832013-05-28 22:30:35 +02001804 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001805 if (*regparse == '-') /* if last, '-' is just a char */
1806 {
1807 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001808 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001810
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001811 /* skip the trailing ] */
1812 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001813 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001814
1815 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001817 EMIT(NFA_END_NEG_COLL);
1818 else
1819 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001820
1821 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001822 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001823 {
1824 EMIT(reg_string ? NL : NFA_NEWL);
1825 EMIT(NFA_OR);
1826 }
1827
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001828 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001829 } /* if exists closing ] */
1830
1831 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001833 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001835 default:
1836 {
1837#ifdef FEAT_MBYTE
1838 int plen;
1839
1840nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001841 /* plen is length of current char with composing chars */
1842 if (enc_utf8 && ((*mb_char2len)(c)
1843 != (plen = (*mb_ptr2len)(old_regparse))
1844 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001845 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001846 int i = 0;
1847
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001848 /* A base character plus composing characters, or just one
1849 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001850 * This requires creating a separate atom as if enclosing
1851 * the characters in (), where NFA_COMPOSING is the ( and
1852 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001853 * building the postfix form, not the NFA itself;
1854 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001855 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001856 for (;;)
1857 {
1858 EMIT(c);
1859 if (i > 0)
1860 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001861 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001862 break;
1863 c = utf_ptr2char(old_regparse + i);
1864 }
1865 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866 regparse = old_regparse + plen;
1867 }
1868 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869#endif
1870 {
1871 c = no_Magic(c);
1872 EMIT(c);
1873 }
1874 return OK;
1875 }
1876 }
1877
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001878 return OK;
1879}
1880
1881/*
1882 * Parse something followed by possible [*+=].
1883 *
1884 * A piece is an atom, possibly followed by a multi, an indication of how many
1885 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1886 * characters: "", "a", "aa", etc.
1887 *
1888 * piece ::= atom
1889 * or atom multi
1890 */
1891 static int
1892nfa_regpiece()
1893{
1894 int i;
1895 int op;
1896 int ret;
1897 long minval, maxval;
1898 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001899 parse_state_T old_state;
1900 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001902 int old_post_pos;
1903 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 int quest;
1905
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001906 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1907 * next. */
1908 save_parse_state(&old_state);
1909
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001911 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912
1913 ret = nfa_regatom();
1914 if (ret == FAIL)
1915 return FAIL; /* cascaded error */
1916
1917 op = peekchr();
1918 if (re_multi_type(op) == NOT_MULTI)
1919 return OK;
1920
1921 skipchr();
1922 switch (op)
1923 {
1924 case Magic('*'):
1925 EMIT(NFA_STAR);
1926 break;
1927
1928 case Magic('+'):
1929 /*
1930 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1931 * first and only submatch would be "aaa". But the backtracking
1932 * engine interprets the plus as "try matching one more time", and
1933 * a* matches a second time at the end of the input, the empty
1934 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001935 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001937 * In order to be consistent with the old engine, we replace
1938 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001940 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 curchr = -1;
1942 if (nfa_regatom() == FAIL)
1943 return FAIL;
1944 EMIT(NFA_STAR);
1945 EMIT(NFA_CONCAT);
1946 skipchr(); /* skip the \+ */
1947 break;
1948
1949 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001950 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001952 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 switch(op)
1954 {
1955 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001956 /* \@= */
1957 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001959 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001960 /* \@! */
1961 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001962 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001963 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001964 op = no_Magic(getchr());
1965 if (op == '=')
1966 /* \@<= */
1967 i = NFA_PREV_ATOM_JUST_BEFORE;
1968 else if (op == '!')
1969 /* \@<! */
1970 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1971 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001972 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001973 /* \@> */
1974 i = NFA_PREV_ATOM_LIKE_PATTERN;
1975 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001976 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001977 if (i == 0)
1978 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001979 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1980 return FAIL;
1981 }
1982 EMIT(i);
1983 if (i == NFA_PREV_ATOM_JUST_BEFORE
1984 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1985 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001986 break;
1987
1988 case Magic('?'):
1989 case Magic('='):
1990 EMIT(NFA_QUEST);
1991 break;
1992
1993 case Magic('{'):
1994 /* a{2,5} will expand to 'aaa?a?a?'
1995 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1996 * version of '?'
1997 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1998 * parenthesis have the same id
1999 */
2000
2001 greedy = TRUE;
2002 c2 = peekchr();
2003 if (c2 == '-' || c2 == Magic('-'))
2004 {
2005 skipchr();
2006 greedy = FALSE;
2007 }
2008 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002009 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002010
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2012 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002013 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002015 if (greedy)
2016 /* \{}, \{0,} */
2017 EMIT(NFA_STAR);
2018 else
2019 /* \{-}, \{-0,} */
2020 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002021 break;
2022 }
2023
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002024 /* Special case: x{0} or x{-0} */
2025 if (maxval == 0)
2026 {
2027 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002028 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002029 /* NFA_EMPTY is 0-length and works everywhere */
2030 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002031 return OK;
2032 }
2033
2034 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002035 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002036 /* Save parse state after the repeated atom and the \{} */
2037 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002039 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2040 for (i = 0; i < maxval; i++)
2041 {
2042 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002043 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002044 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002045 if (nfa_regatom() == FAIL)
2046 return FAIL;
2047 /* after "minval" times, atoms are optional */
2048 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002049 {
2050 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002051 {
2052 if (greedy)
2053 EMIT(NFA_STAR);
2054 else
2055 EMIT(NFA_STAR_NONGREEDY);
2056 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002057 else
2058 EMIT(quest);
2059 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002060 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002062 if (i + 1 > minval && maxval == MAX_LIMIT)
2063 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002064 }
2065
2066 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002067 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 curchr = -1;
2069
2070 break;
2071
2072
2073 default:
2074 break;
2075 } /* end switch */
2076
2077 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002078 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002079 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002080
2081 return OK;
2082}
2083
2084/*
2085 * Parse one or more pieces, concatenated. It matches a match for the
2086 * first piece, followed by a match for the second piece, etc. Example:
2087 * "f[0-9]b", first matches "f", then a digit and then "b".
2088 *
2089 * concat ::= piece
2090 * or piece piece
2091 * or piece piece piece
2092 * etc.
2093 */
2094 static int
2095nfa_regconcat()
2096{
2097 int cont = TRUE;
2098 int first = TRUE;
2099
2100 while (cont)
2101 {
2102 switch (peekchr())
2103 {
2104 case NUL:
2105 case Magic('|'):
2106 case Magic('&'):
2107 case Magic(')'):
2108 cont = FALSE;
2109 break;
2110
2111 case Magic('Z'):
2112#ifdef FEAT_MBYTE
2113 regflags |= RF_ICOMBINE;
2114#endif
2115 skipchr_keepstart();
2116 break;
2117 case Magic('c'):
2118 regflags |= RF_ICASE;
2119 skipchr_keepstart();
2120 break;
2121 case Magic('C'):
2122 regflags |= RF_NOICASE;
2123 skipchr_keepstart();
2124 break;
2125 case Magic('v'):
2126 reg_magic = MAGIC_ALL;
2127 skipchr_keepstart();
2128 curchr = -1;
2129 break;
2130 case Magic('m'):
2131 reg_magic = MAGIC_ON;
2132 skipchr_keepstart();
2133 curchr = -1;
2134 break;
2135 case Magic('M'):
2136 reg_magic = MAGIC_OFF;
2137 skipchr_keepstart();
2138 curchr = -1;
2139 break;
2140 case Magic('V'):
2141 reg_magic = MAGIC_NONE;
2142 skipchr_keepstart();
2143 curchr = -1;
2144 break;
2145
2146 default:
2147 if (nfa_regpiece() == FAIL)
2148 return FAIL;
2149 if (first == FALSE)
2150 EMIT(NFA_CONCAT);
2151 else
2152 first = FALSE;
2153 break;
2154 }
2155 }
2156
2157 return OK;
2158}
2159
2160/*
2161 * Parse a branch, one or more concats, separated by "\&". It matches the
2162 * last concat, but only if all the preceding concats also match at the same
2163 * position. Examples:
2164 * "foobeep\&..." matches "foo" in "foobeep".
2165 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2166 *
2167 * branch ::= concat
2168 * or concat \& concat
2169 * or concat \& concat \& concat
2170 * etc.
2171 */
2172 static int
2173nfa_regbranch()
2174{
2175 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002176 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002177
Bram Moolenaar16299b52013-05-30 18:45:23 +02002178 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179
2180 /* First branch, possibly the only one */
2181 if (nfa_regconcat() == FAIL)
2182 return FAIL;
2183
2184 ch = peekchr();
2185 /* Try next concats */
2186 while (ch == Magic('&'))
2187 {
2188 skipchr();
2189 EMIT(NFA_NOPEN);
2190 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002191 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002192 if (nfa_regconcat() == FAIL)
2193 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002194 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002195 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002196 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002197 EMIT(NFA_CONCAT);
2198 ch = peekchr();
2199 }
2200
Bram Moolenaar699c1202013-09-25 16:41:54 +02002201 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002202 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002203 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002204
2205 return OK;
2206}
2207
2208/*
2209 * Parse a pattern, one or more branches, separated by "\|". It matches
2210 * anything that matches one of the branches. Example: "foo\|beep" matches
2211 * "foo" and matches "beep". If more than one branch matches, the first one
2212 * is used.
2213 *
2214 * pattern ::= branch
2215 * or branch \| branch
2216 * or branch \| branch \| branch
2217 * etc.
2218 */
2219 static int
2220nfa_reg(paren)
2221 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
2222{
2223 int parno = 0;
2224
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002225 if (paren == REG_PAREN)
2226 {
2227 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229 parno = regnpar++;
2230 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002231#ifdef FEAT_SYN_HL
2232 else if (paren == REG_ZPAREN)
2233 {
2234 /* Make a ZOPEN node. */
2235 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002236 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002237 parno = regnzpar++;
2238 }
2239#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002240
2241 if (nfa_regbranch() == FAIL)
2242 return FAIL; /* cascaded error */
2243
2244 while (peekchr() == Magic('|'))
2245 {
2246 skipchr();
2247 if (nfa_regbranch() == FAIL)
2248 return FAIL; /* cascaded error */
2249 EMIT(NFA_OR);
2250 }
2251
2252 /* Check for proper termination. */
2253 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2254 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002255 if (paren == REG_NPAREN)
2256 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2257 else
2258 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2259 }
2260 else if (paren == REG_NOPAREN && peekchr() != NUL)
2261 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002262 if (peekchr() == Magic(')'))
2263 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2264 else
2265 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2266 }
2267 /*
2268 * Here we set the flag allowing back references to this set of
2269 * parentheses.
2270 */
2271 if (paren == REG_PAREN)
2272 {
2273 had_endbrace[parno] = TRUE; /* have seen the close paren */
2274 EMIT(NFA_MOPEN + parno);
2275 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002276#ifdef FEAT_SYN_HL
2277 else if (paren == REG_ZPAREN)
2278 EMIT(NFA_ZOPEN + parno);
2279#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002280
2281 return OK;
2282}
2283
Bram Moolenaar2d46e602014-08-29 11:56:32 +02002284/*
2285 * Used in a place where no * or \+ can follow.
2286 */
2287 static int
2288re_mult_next(what)
2289 char *what;
2290{
2291 if (re_multi_type(peekchr()) == MULTI_MULT)
2292 EMSG2_RET_FAIL(_("E888: (NFA regexp) cannot repeat %s"), what);
2293 return OK;
2294}
2295
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002296#ifdef DEBUG
2297static char_u code[50];
2298
2299 static void
2300nfa_set_code(c)
2301 int c;
2302{
2303 int addnl = FALSE;
2304
2305 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2306 {
2307 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002308 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 }
2310
2311 STRCPY(code, "");
2312 switch (c)
2313 {
2314 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2315 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2316 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2317 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2318 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2319 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2320
Bram Moolenaar5714b802013-05-28 22:03:20 +02002321 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2322 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2323 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2324 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2325 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2326 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2327 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2328 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2329 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002330#ifdef FEAT_SYN_HL
2331 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2332 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2333 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2334 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2335 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2336 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2337 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2338 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2339 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2340#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002341 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2342
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002343 case NFA_PREV_ATOM_NO_WIDTH:
2344 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002345 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2346 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002347 case NFA_PREV_ATOM_JUST_BEFORE:
2348 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2349 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2350 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002351 case NFA_PREV_ATOM_LIKE_PATTERN:
2352 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2353
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002354 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2355 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002356 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002357 case NFA_START_INVISIBLE_FIRST:
2358 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002359 case NFA_START_INVISIBLE_NEG:
2360 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002361 case NFA_START_INVISIBLE_NEG_FIRST:
2362 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002363 case NFA_START_INVISIBLE_BEFORE:
2364 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002365 case NFA_START_INVISIBLE_BEFORE_FIRST:
2366 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002367 case NFA_START_INVISIBLE_BEFORE_NEG:
2368 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002369 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2370 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002371 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002373 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002374 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002375
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002376 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2377 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002378 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002380 case NFA_MOPEN:
2381 case NFA_MOPEN1:
2382 case NFA_MOPEN2:
2383 case NFA_MOPEN3:
2384 case NFA_MOPEN4:
2385 case NFA_MOPEN5:
2386 case NFA_MOPEN6:
2387 case NFA_MOPEN7:
2388 case NFA_MOPEN8:
2389 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002390 STRCPY(code, "NFA_MOPEN(x)");
2391 code[10] = c - NFA_MOPEN + '0';
2392 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002393 case NFA_MCLOSE:
2394 case NFA_MCLOSE1:
2395 case NFA_MCLOSE2:
2396 case NFA_MCLOSE3:
2397 case NFA_MCLOSE4:
2398 case NFA_MCLOSE5:
2399 case NFA_MCLOSE6:
2400 case NFA_MCLOSE7:
2401 case NFA_MCLOSE8:
2402 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002403 STRCPY(code, "NFA_MCLOSE(x)");
2404 code[11] = c - NFA_MCLOSE + '0';
2405 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002406#ifdef FEAT_SYN_HL
2407 case NFA_ZOPEN:
2408 case NFA_ZOPEN1:
2409 case NFA_ZOPEN2:
2410 case NFA_ZOPEN3:
2411 case NFA_ZOPEN4:
2412 case NFA_ZOPEN5:
2413 case NFA_ZOPEN6:
2414 case NFA_ZOPEN7:
2415 case NFA_ZOPEN8:
2416 case NFA_ZOPEN9:
2417 STRCPY(code, "NFA_ZOPEN(x)");
2418 code[10] = c - NFA_ZOPEN + '0';
2419 break;
2420 case NFA_ZCLOSE:
2421 case NFA_ZCLOSE1:
2422 case NFA_ZCLOSE2:
2423 case NFA_ZCLOSE3:
2424 case NFA_ZCLOSE4:
2425 case NFA_ZCLOSE5:
2426 case NFA_ZCLOSE6:
2427 case NFA_ZCLOSE7:
2428 case NFA_ZCLOSE8:
2429 case NFA_ZCLOSE9:
2430 STRCPY(code, "NFA_ZCLOSE(x)");
2431 code[11] = c - NFA_ZCLOSE + '0';
2432 break;
2433#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002434 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2435 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2436 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2437 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002438 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2439 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002440 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2441 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2442 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2443 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2444 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2445 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2446 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2447 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2448 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2449 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2450 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2451 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2452 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2453 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002454 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002456 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002457 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2458 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2459 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002460 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002462
2463 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2464 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2465 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2466 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2467 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2468 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2469 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2470
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2472 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2473 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2474 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2475 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2476 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2477 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2478 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2479 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2480 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2481 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2482 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2483 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2484 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2485 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2486 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2487
2488 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2489 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2490 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2491 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2492 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2493 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2494 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2495 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2496 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2497 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2498 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2499 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2500 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2501 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2502 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2503 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2504 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2505 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2506 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2507 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2508 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2509 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2510 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2511 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2512 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2513 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2514 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002515 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2516 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2517 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2518 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519
2520 default:
2521 STRCPY(code, "CHAR(x)");
2522 code[5] = c;
2523 }
2524
2525 if (addnl == TRUE)
2526 STRCAT(code, " + NEWLINE ");
2527
2528}
2529
2530#ifdef ENABLE_LOG
2531static FILE *log_fd;
2532
2533/*
2534 * Print the postfix notation of the current regexp.
2535 */
2536 static void
2537nfa_postfix_dump(expr, retval)
2538 char_u *expr;
2539 int retval;
2540{
2541 int *p;
2542 FILE *f;
2543
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002544 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002545 if (f != NULL)
2546 {
2547 fprintf(f, "\n-------------------------\n");
2548 if (retval == FAIL)
2549 fprintf(f, ">>> NFA engine failed ... \n");
2550 else if (retval == OK)
2551 fprintf(f, ">>> NFA engine succeeded !\n");
2552 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002553 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002554 {
2555 nfa_set_code(*p);
2556 fprintf(f, "%s, ", code);
2557 }
2558 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002559 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560 fprintf(f, "%d ", *p);
2561 fprintf(f, "\n\n");
2562 fclose(f);
2563 }
2564}
2565
2566/*
2567 * Print the NFA starting with a root node "state".
2568 */
2569 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002570nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002571 FILE *debugf;
2572 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002574 garray_T indent;
2575
2576 ga_init2(&indent, 1, 64);
2577 ga_append(&indent, '\0');
2578 nfa_print_state2(debugf, state, &indent);
2579 ga_clear(&indent);
2580}
2581
2582 static void
2583nfa_print_state2(debugf, state, indent)
2584 FILE *debugf;
2585 nfa_state_T *state;
2586 garray_T *indent;
2587{
2588 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002589
2590 if (state == NULL)
2591 return;
2592
2593 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002594
2595 /* Output indent */
2596 p = (char_u *)indent->ga_data;
2597 if (indent->ga_len >= 3)
2598 {
2599 int last = indent->ga_len - 3;
2600 char_u save[2];
2601
2602 STRNCPY(save, &p[last], 2);
2603 STRNCPY(&p[last], "+-", 2);
2604 fprintf(debugf, " %s", p);
2605 STRNCPY(&p[last], save, 2);
2606 }
2607 else
2608 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002609
2610 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002611 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002612 code,
2613 state->c,
2614 abs(state->id),
2615 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616 if (state->id < 0)
2617 return;
2618
2619 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002620
2621 /* grow indent for state->out */
2622 indent->ga_len -= 1;
2623 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002624 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002625 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002626 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002627 ga_append(indent, '\0');
2628
2629 nfa_print_state2(debugf, state->out, indent);
2630
2631 /* replace last part of indent for state->out1 */
2632 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002633 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002634 ga_append(indent, '\0');
2635
2636 nfa_print_state2(debugf, state->out1, indent);
2637
2638 /* shrink indent */
2639 indent->ga_len -= 3;
2640 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641}
2642
2643/*
2644 * Print the NFA state machine.
2645 */
2646 static void
2647nfa_dump(prog)
2648 nfa_regprog_T *prog;
2649{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002650 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002651
2652 if (debugf != NULL)
2653 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002654 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002655
Bram Moolenaar473de612013-06-08 18:19:48 +02002656 if (prog->reganch)
2657 fprintf(debugf, "reganch: %d\n", prog->reganch);
2658 if (prog->regstart != NUL)
2659 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2660 prog->regstart, prog->regstart);
2661 if (prog->match_text != NULL)
2662 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002663
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002664 fclose(debugf);
2665 }
2666}
2667#endif /* ENABLE_LOG */
2668#endif /* DEBUG */
2669
2670/*
2671 * Parse r.e. @expr and convert it into postfix form.
2672 * Return the postfix string on success, NULL otherwise.
2673 */
2674 static int *
2675re2post()
2676{
2677 if (nfa_reg(REG_NOPAREN) == FAIL)
2678 return NULL;
2679 EMIT(NFA_MOPEN);
2680 return post_start;
2681}
2682
2683/* NB. Some of the code below is inspired by Russ's. */
2684
2685/*
2686 * Represents an NFA state plus zero or one or two arrows exiting.
2687 * if c == MATCH, no arrows out; matching state.
2688 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2689 * If c < 256, labeled arrow with character c to out.
2690 */
2691
2692static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2693
2694/*
2695 * Allocate and initialize nfa_state_T.
2696 */
2697 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002698alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699 int c;
2700 nfa_state_T *out;
2701 nfa_state_T *out1;
2702{
2703 nfa_state_T *s;
2704
2705 if (istate >= nstate)
2706 return NULL;
2707
2708 s = &state_ptr[istate++];
2709
2710 s->c = c;
2711 s->out = out;
2712 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002713 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714
2715 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002716 s->lastlist[0] = 0;
2717 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002718
2719 return s;
2720}
2721
2722/*
2723 * A partially built NFA without the matching state filled in.
2724 * Frag_T.start points at the start state.
2725 * Frag_T.out is a list of places that need to be set to the
2726 * next state for this fragment.
2727 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002728
2729/* Since the out pointers in the list are always
2730 * uninitialized, we use the pointers themselves
2731 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002733union Ptrlist
2734{
2735 Ptrlist *next;
2736 nfa_state_T *s;
2737};
2738
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739struct Frag
2740{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002741 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 Ptrlist *out;
2743};
2744typedef struct Frag Frag_T;
2745
2746static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2747static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2748static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2749static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2750static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2751static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2752
2753/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002754 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002755 */
2756 static Frag_T
2757frag(start, out)
2758 nfa_state_T *start;
2759 Ptrlist *out;
2760{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002761 Frag_T n;
2762
2763 n.start = start;
2764 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765 return n;
2766}
2767
2768/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002769 * Create singleton list containing just outp.
2770 */
2771 static Ptrlist *
2772list1(outp)
2773 nfa_state_T **outp;
2774{
2775 Ptrlist *l;
2776
2777 l = (Ptrlist *)outp;
2778 l->next = NULL;
2779 return l;
2780}
2781
2782/*
2783 * Patch the list of states at out to point to start.
2784 */
2785 static void
2786patch(l, s)
2787 Ptrlist *l;
2788 nfa_state_T *s;
2789{
2790 Ptrlist *next;
2791
2792 for (; l; l = next)
2793 {
2794 next = l->next;
2795 l->s = s;
2796 }
2797}
2798
2799
2800/*
2801 * Join the two lists l1 and l2, returning the combination.
2802 */
2803 static Ptrlist *
2804append(l1, l2)
2805 Ptrlist *l1;
2806 Ptrlist *l2;
2807{
2808 Ptrlist *oldl1;
2809
2810 oldl1 = l1;
2811 while (l1->next)
2812 l1 = l1->next;
2813 l1->next = l2;
2814 return oldl1;
2815}
2816
2817/*
2818 * Stack used for transforming postfix form into NFA.
2819 */
2820static Frag_T empty;
2821
2822 static void
2823st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002824 int *postfix UNUSED;
2825 int *end UNUSED;
2826 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002828#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002829 FILE *df;
2830 int *p2;
2831
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002832 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002833 if (df)
2834 {
2835 fprintf(df, "Error popping the stack!\n");
2836#ifdef DEBUG
2837 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2838#endif
2839 fprintf(df, "Postfix form is: ");
2840#ifdef DEBUG
2841 for (p2 = postfix; p2 < end; p2++)
2842 {
2843 nfa_set_code(*p2);
2844 fprintf(df, "%s, ", code);
2845 }
2846 nfa_set_code(*p);
2847 fprintf(df, "\nCurrent position is: ");
2848 for (p2 = postfix; p2 <= p; p2 ++)
2849 {
2850 nfa_set_code(*p2);
2851 fprintf(df, "%s, ", code);
2852 }
2853#else
2854 for (p2 = postfix; p2 < end; p2++)
2855 {
2856 fprintf(df, "%d, ", *p2);
2857 }
2858 fprintf(df, "\nCurrent position is: ");
2859 for (p2 = postfix; p2 <= p; p2 ++)
2860 {
2861 fprintf(df, "%d, ", *p2);
2862 }
2863#endif
2864 fprintf(df, "\n--------------------------\n");
2865 fclose(df);
2866 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002867#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002868 EMSG(_("E874: (NFA) Could not pop the stack !"));
2869}
2870
2871/*
2872 * Push an item onto the stack.
2873 */
2874 static void
2875st_push(s, p, stack_end)
2876 Frag_T s;
2877 Frag_T **p;
2878 Frag_T *stack_end;
2879{
2880 Frag_T *stackp = *p;
2881
2882 if (stackp >= stack_end)
2883 return;
2884 *stackp = s;
2885 *p = *p + 1;
2886}
2887
2888/*
2889 * Pop an item from the stack.
2890 */
2891 static Frag_T
2892st_pop(p, stack)
2893 Frag_T **p;
2894 Frag_T *stack;
2895{
2896 Frag_T *stackp;
2897
2898 *p = *p - 1;
2899 stackp = *p;
2900 if (stackp < stack)
2901 return empty;
2902 return **p;
2903}
2904
2905/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002906 * Estimate the maximum byte length of anything matching "state".
2907 * When unknown or unlimited return -1.
2908 */
2909 static int
2910nfa_max_width(startstate, depth)
2911 nfa_state_T *startstate;
2912 int depth;
2913{
2914 int l, r;
2915 nfa_state_T *state = startstate;
2916 int len = 0;
2917
2918 /* detect looping in a NFA_SPLIT */
2919 if (depth > 4)
2920 return -1;
2921
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002922 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002923 {
2924 switch (state->c)
2925 {
2926 case NFA_END_INVISIBLE:
2927 case NFA_END_INVISIBLE_NEG:
2928 /* the end, return what we have */
2929 return len;
2930
2931 case NFA_SPLIT:
2932 /* two alternatives, use the maximum */
2933 l = nfa_max_width(state->out, depth + 1);
2934 r = nfa_max_width(state->out1, depth + 1);
2935 if (l < 0 || r < 0)
2936 return -1;
2937 return len + (l > r ? l : r);
2938
2939 case NFA_ANY:
2940 case NFA_START_COLL:
2941 case NFA_START_NEG_COLL:
2942 /* matches some character, including composing chars */
2943#ifdef FEAT_MBYTE
2944 if (enc_utf8)
2945 len += MB_MAXBYTES;
2946 else if (has_mbyte)
2947 len += 2;
2948 else
2949#endif
2950 ++len;
2951 if (state->c != NFA_ANY)
2952 {
2953 /* skip over the characters */
2954 state = state->out1->out;
2955 continue;
2956 }
2957 break;
2958
2959 case NFA_DIGIT:
2960 case NFA_WHITE:
2961 case NFA_HEX:
2962 case NFA_OCTAL:
2963 /* ascii */
2964 ++len;
2965 break;
2966
2967 case NFA_IDENT:
2968 case NFA_SIDENT:
2969 case NFA_KWORD:
2970 case NFA_SKWORD:
2971 case NFA_FNAME:
2972 case NFA_SFNAME:
2973 case NFA_PRINT:
2974 case NFA_SPRINT:
2975 case NFA_NWHITE:
2976 case NFA_NDIGIT:
2977 case NFA_NHEX:
2978 case NFA_NOCTAL:
2979 case NFA_WORD:
2980 case NFA_NWORD:
2981 case NFA_HEAD:
2982 case NFA_NHEAD:
2983 case NFA_ALPHA:
2984 case NFA_NALPHA:
2985 case NFA_LOWER:
2986 case NFA_NLOWER:
2987 case NFA_UPPER:
2988 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002989 case NFA_LOWER_IC:
2990 case NFA_NLOWER_IC:
2991 case NFA_UPPER_IC:
2992 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002993 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002994 /* possibly non-ascii */
2995#ifdef FEAT_MBYTE
2996 if (has_mbyte)
2997 len += 3;
2998 else
2999#endif
3000 ++len;
3001 break;
3002
3003 case NFA_START_INVISIBLE:
3004 case NFA_START_INVISIBLE_NEG:
3005 case NFA_START_INVISIBLE_BEFORE:
3006 case NFA_START_INVISIBLE_BEFORE_NEG:
3007 /* zero-width, out1 points to the END state */
3008 state = state->out1->out;
3009 continue;
3010
3011 case NFA_BACKREF1:
3012 case NFA_BACKREF2:
3013 case NFA_BACKREF3:
3014 case NFA_BACKREF4:
3015 case NFA_BACKREF5:
3016 case NFA_BACKREF6:
3017 case NFA_BACKREF7:
3018 case NFA_BACKREF8:
3019 case NFA_BACKREF9:
3020#ifdef FEAT_SYN_HL
3021 case NFA_ZREF1:
3022 case NFA_ZREF2:
3023 case NFA_ZREF3:
3024 case NFA_ZREF4:
3025 case NFA_ZREF5:
3026 case NFA_ZREF6:
3027 case NFA_ZREF7:
3028 case NFA_ZREF8:
3029 case NFA_ZREF9:
3030#endif
3031 case NFA_NEWL:
3032 case NFA_SKIP:
3033 /* unknown width */
3034 return -1;
3035
3036 case NFA_BOL:
3037 case NFA_EOL:
3038 case NFA_BOF:
3039 case NFA_EOF:
3040 case NFA_BOW:
3041 case NFA_EOW:
3042 case NFA_MOPEN:
3043 case NFA_MOPEN1:
3044 case NFA_MOPEN2:
3045 case NFA_MOPEN3:
3046 case NFA_MOPEN4:
3047 case NFA_MOPEN5:
3048 case NFA_MOPEN6:
3049 case NFA_MOPEN7:
3050 case NFA_MOPEN8:
3051 case NFA_MOPEN9:
3052#ifdef FEAT_SYN_HL
3053 case NFA_ZOPEN:
3054 case NFA_ZOPEN1:
3055 case NFA_ZOPEN2:
3056 case NFA_ZOPEN3:
3057 case NFA_ZOPEN4:
3058 case NFA_ZOPEN5:
3059 case NFA_ZOPEN6:
3060 case NFA_ZOPEN7:
3061 case NFA_ZOPEN8:
3062 case NFA_ZOPEN9:
3063 case NFA_ZCLOSE:
3064 case NFA_ZCLOSE1:
3065 case NFA_ZCLOSE2:
3066 case NFA_ZCLOSE3:
3067 case NFA_ZCLOSE4:
3068 case NFA_ZCLOSE5:
3069 case NFA_ZCLOSE6:
3070 case NFA_ZCLOSE7:
3071 case NFA_ZCLOSE8:
3072 case NFA_ZCLOSE9:
3073#endif
3074 case NFA_MCLOSE:
3075 case NFA_MCLOSE1:
3076 case NFA_MCLOSE2:
3077 case NFA_MCLOSE3:
3078 case NFA_MCLOSE4:
3079 case NFA_MCLOSE5:
3080 case NFA_MCLOSE6:
3081 case NFA_MCLOSE7:
3082 case NFA_MCLOSE8:
3083 case NFA_MCLOSE9:
3084 case NFA_NOPEN:
3085 case NFA_NCLOSE:
3086
3087 case NFA_LNUM_GT:
3088 case NFA_LNUM_LT:
3089 case NFA_COL_GT:
3090 case NFA_COL_LT:
3091 case NFA_VCOL_GT:
3092 case NFA_VCOL_LT:
3093 case NFA_MARK_GT:
3094 case NFA_MARK_LT:
3095 case NFA_VISUAL:
3096 case NFA_LNUM:
3097 case NFA_CURSOR:
3098 case NFA_COL:
3099 case NFA_VCOL:
3100 case NFA_MARK:
3101
3102 case NFA_ZSTART:
3103 case NFA_ZEND:
3104 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003105 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003106 case NFA_START_PATTERN:
3107 case NFA_END_PATTERN:
3108 case NFA_COMPOSING:
3109 case NFA_END_COMPOSING:
3110 /* zero-width */
3111 break;
3112
3113 default:
3114 if (state->c < 0)
3115 /* don't know what this is */
3116 return -1;
3117 /* normal character */
3118 len += MB_CHAR2LEN(state->c);
3119 break;
3120 }
3121
3122 /* normal way to continue */
3123 state = state->out;
3124 }
3125
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003126 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003127 return -1;
3128}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003129
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003130/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003131 * Convert a postfix form into its equivalent NFA.
3132 * Return the NFA start state on success, NULL otherwise.
3133 */
3134 static nfa_state_T *
3135post2nfa(postfix, end, nfa_calc_size)
3136 int *postfix;
3137 int *end;
3138 int nfa_calc_size;
3139{
3140 int *p;
3141 int mopen;
3142 int mclose;
3143 Frag_T *stack = NULL;
3144 Frag_T *stackp = NULL;
3145 Frag_T *stack_end = NULL;
3146 Frag_T e1;
3147 Frag_T e2;
3148 Frag_T e;
3149 nfa_state_T *s;
3150 nfa_state_T *s1;
3151 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003152 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003153
3154 if (postfix == NULL)
3155 return NULL;
3156
Bram Moolenaar053bb602013-05-20 13:55:21 +02003157#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003158#define POP() st_pop(&stackp, stack); \
3159 if (stackp < stack) \
3160 { \
3161 st_error(postfix, end, p); \
3162 return NULL; \
3163 }
3164
3165 if (nfa_calc_size == FALSE)
3166 {
3167 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003168 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003169 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003170 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003171 }
3172
3173 for (p = postfix; p < end; ++p)
3174 {
3175 switch (*p)
3176 {
3177 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003178 /* Concatenation.
3179 * Pay attention: this operator does not exist in the r.e. itself
3180 * (it is implicit, really). It is added when r.e. is translated
3181 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003182 if (nfa_calc_size == TRUE)
3183 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003184 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003185 break;
3186 }
3187 e2 = POP();
3188 e1 = POP();
3189 patch(e1.out, e2.start);
3190 PUSH(frag(e1.start, e2.out));
3191 break;
3192
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 case NFA_OR:
3194 /* Alternation */
3195 if (nfa_calc_size == TRUE)
3196 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003197 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003198 break;
3199 }
3200 e2 = POP();
3201 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003202 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003204 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205 PUSH(frag(s, append(e1.out, e2.out)));
3206 break;
3207
3208 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003209 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003210 if (nfa_calc_size == TRUE)
3211 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003212 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213 break;
3214 }
3215 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003216 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003218 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219 patch(e.out, s);
3220 PUSH(frag(s, list1(&s->out1)));
3221 break;
3222
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003223 case NFA_STAR_NONGREEDY:
3224 /* Zero or more, prefer zero */
3225 if (nfa_calc_size == TRUE)
3226 {
3227 nstate++;
3228 break;
3229 }
3230 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003231 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003232 if (s == NULL)
3233 goto theend;
3234 patch(e.out, s);
3235 PUSH(frag(s, list1(&s->out)));
3236 break;
3237
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003238 case NFA_QUEST:
3239 /* one or zero atoms=> greedy match */
3240 if (nfa_calc_size == TRUE)
3241 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003242 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003243 break;
3244 }
3245 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003246 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003248 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 PUSH(frag(s, append(e.out, list1(&s->out1))));
3250 break;
3251
3252 case NFA_QUEST_NONGREEDY:
3253 /* zero or one atoms => non-greedy match */
3254 if (nfa_calc_size == TRUE)
3255 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003256 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003257 break;
3258 }
3259 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003260 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003262 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263 PUSH(frag(s, append(e.out, list1(&s->out))));
3264 break;
3265
Bram Moolenaar417bad22013-06-07 14:08:30 +02003266 case NFA_END_COLL:
3267 case NFA_END_NEG_COLL:
3268 /* On the stack is the sequence starting with NFA_START_COLL or
3269 * NFA_START_NEG_COLL and all possible characters. Patch it to
3270 * add the output to the start. */
3271 if (nfa_calc_size == TRUE)
3272 {
3273 nstate++;
3274 break;
3275 }
3276 e = POP();
3277 s = alloc_state(NFA_END_COLL, NULL, NULL);
3278 if (s == NULL)
3279 goto theend;
3280 patch(e.out, s);
3281 e.start->out1 = s;
3282 PUSH(frag(e.start, list1(&s->out)));
3283 break;
3284
3285 case NFA_RANGE:
3286 /* Before this are two characters, the low and high end of a
3287 * range. Turn them into two states with MIN and MAX. */
3288 if (nfa_calc_size == TRUE)
3289 {
3290 /* nstate += 0; */
3291 break;
3292 }
3293 e2 = POP();
3294 e1 = POP();
3295 e2.start->val = e2.start->c;
3296 e2.start->c = NFA_RANGE_MAX;
3297 e1.start->val = e1.start->c;
3298 e1.start->c = NFA_RANGE_MIN;
3299 patch(e1.out, e2.start);
3300 PUSH(frag(e1.start, e2.out));
3301 break;
3302
Bram Moolenaar699c1202013-09-25 16:41:54 +02003303 case NFA_EMPTY:
3304 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 if (nfa_calc_size == TRUE)
3306 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003307 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 break;
3309 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003310 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003312 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 PUSH(frag(s, list1(&s->out)));
3314 break;
3315
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003316 case NFA_OPT_CHARS:
3317 {
3318 int n;
3319
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003320 /* \%[abc] implemented as:
3321 * NFA_SPLIT
3322 * +-CHAR(a)
3323 * | +-NFA_SPLIT
3324 * | +-CHAR(b)
3325 * | | +-NFA_SPLIT
3326 * | | +-CHAR(c)
3327 * | | | +-next
3328 * | | +- next
3329 * | +- next
3330 * +- next
3331 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003332 n = *++p; /* get number of characters */
3333 if (nfa_calc_size == TRUE)
3334 {
3335 nstate += n;
3336 break;
3337 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003338 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003339 e1.out = NULL; /* stores list with out1's */
3340 s1 = NULL; /* previous NFA_SPLIT to connect to */
3341 while (n-- > 0)
3342 {
3343 e = POP(); /* get character */
3344 s = alloc_state(NFA_SPLIT, e.start, NULL);
3345 if (s == NULL)
3346 goto theend;
3347 if (e1.out == NULL)
3348 e1 = e;
3349 patch(e.out, s1);
3350 append(e1.out, list1(&s->out1));
3351 s1 = s;
3352 }
3353 PUSH(frag(s, e1.out));
3354 break;
3355 }
3356
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003358 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003359 case NFA_PREV_ATOM_JUST_BEFORE:
3360 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003361 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003362 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003363 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3364 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003365 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003366 int start_state;
3367 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003368 int n = 0;
3369 nfa_state_T *zend;
3370 nfa_state_T *skip;
3371
Bram Moolenaardecd9542013-06-07 16:31:50 +02003372 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003373 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003374 case NFA_PREV_ATOM_NO_WIDTH:
3375 start_state = NFA_START_INVISIBLE;
3376 end_state = NFA_END_INVISIBLE;
3377 break;
3378 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3379 start_state = NFA_START_INVISIBLE_NEG;
3380 end_state = NFA_END_INVISIBLE_NEG;
3381 break;
3382 case NFA_PREV_ATOM_JUST_BEFORE:
3383 start_state = NFA_START_INVISIBLE_BEFORE;
3384 end_state = NFA_END_INVISIBLE;
3385 break;
3386 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3387 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3388 end_state = NFA_END_INVISIBLE_NEG;
3389 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003390 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003391 start_state = NFA_START_PATTERN;
3392 end_state = NFA_END_PATTERN;
3393 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003394 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003395
3396 if (before)
3397 n = *++p; /* get the count */
3398
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003399 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003400 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003401 * The \@<= operator: match for the preceding atom.
3402 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003403 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003404 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003405
3406 if (nfa_calc_size == TRUE)
3407 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003408 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003409 break;
3410 }
3411 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003412 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003414 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003415
Bram Moolenaar87953742013-06-05 18:52:40 +02003416 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003418 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003419 if (pattern)
3420 {
3421 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3422 skip = alloc_state(NFA_SKIP, NULL, NULL);
3423 zend = alloc_state(NFA_ZEND, s1, NULL);
3424 s1->out= skip;
3425 patch(e.out, zend);
3426 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003427 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003428 else
3429 {
3430 patch(e.out, s1);
3431 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003432 if (before)
3433 {
3434 if (n <= 0)
3435 /* See if we can guess the maximum width, it avoids a
3436 * lot of pointless tries. */
3437 n = nfa_max_width(e.start, 0);
3438 s->val = n; /* store the count */
3439 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003440 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003441 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003442 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003443
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003444#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003445 case NFA_COMPOSING: /* char with composing char */
3446#if 0
3447 /* TODO */
3448 if (regflags & RF_ICOMBINE)
3449 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003450 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003451 }
3452#endif
3453 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003454#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003455
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003456 case NFA_MOPEN: /* \( \) Submatch */
3457 case NFA_MOPEN1:
3458 case NFA_MOPEN2:
3459 case NFA_MOPEN3:
3460 case NFA_MOPEN4:
3461 case NFA_MOPEN5:
3462 case NFA_MOPEN6:
3463 case NFA_MOPEN7:
3464 case NFA_MOPEN8:
3465 case NFA_MOPEN9:
3466#ifdef FEAT_SYN_HL
3467 case NFA_ZOPEN: /* \z( \) Submatch */
3468 case NFA_ZOPEN1:
3469 case NFA_ZOPEN2:
3470 case NFA_ZOPEN3:
3471 case NFA_ZOPEN4:
3472 case NFA_ZOPEN5:
3473 case NFA_ZOPEN6:
3474 case NFA_ZOPEN7:
3475 case NFA_ZOPEN8:
3476 case NFA_ZOPEN9:
3477#endif
3478 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 if (nfa_calc_size == TRUE)
3480 {
3481 nstate += 2;
3482 break;
3483 }
3484
3485 mopen = *p;
3486 switch (*p)
3487 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003488 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3489#ifdef FEAT_SYN_HL
3490 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3491 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3492 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3493 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3494 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3495 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3496 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3497 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3498 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3499 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3500#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003501#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003502 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003503#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003505 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003506 mclose = *p + NSUBEXP;
3507 break;
3508 }
3509
3510 /* Allow "NFA_MOPEN" as a valid postfix representation for
3511 * the empty regexp "". In this case, the NFA will be
3512 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3513 * empty groups of parenthesis, and empty mbyte chars */
3514 if (stackp == stack)
3515 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003516 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003517 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003518 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003519 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003521 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003522 patch(list1(&s->out), s1);
3523 PUSH(frag(s, list1(&s1->out)));
3524 break;
3525 }
3526
3527 /* At least one node was emitted before NFA_MOPEN, so
3528 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3529 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003530 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003531 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003532 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003533
Bram Moolenaar525666f2013-06-02 16:40:55 +02003534 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003535 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003536 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003537 patch(e.out, s1);
3538
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003539#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003540 if (mopen == NFA_COMPOSING)
3541 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003543#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003544
3545 PUSH(frag(s, list1(&s1->out)));
3546 break;
3547
Bram Moolenaar5714b802013-05-28 22:03:20 +02003548 case NFA_BACKREF1:
3549 case NFA_BACKREF2:
3550 case NFA_BACKREF3:
3551 case NFA_BACKREF4:
3552 case NFA_BACKREF5:
3553 case NFA_BACKREF6:
3554 case NFA_BACKREF7:
3555 case NFA_BACKREF8:
3556 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003557#ifdef FEAT_SYN_HL
3558 case NFA_ZREF1:
3559 case NFA_ZREF2:
3560 case NFA_ZREF3:
3561 case NFA_ZREF4:
3562 case NFA_ZREF5:
3563 case NFA_ZREF6:
3564 case NFA_ZREF7:
3565 case NFA_ZREF8:
3566 case NFA_ZREF9:
3567#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003568 if (nfa_calc_size == TRUE)
3569 {
3570 nstate += 2;
3571 break;
3572 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003573 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003574 if (s == NULL)
3575 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003576 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003577 if (s1 == NULL)
3578 goto theend;
3579 patch(list1(&s->out), s1);
3580 PUSH(frag(s, list1(&s1->out)));
3581 break;
3582
Bram Moolenaar423532e2013-05-29 21:14:42 +02003583 case NFA_LNUM:
3584 case NFA_LNUM_GT:
3585 case NFA_LNUM_LT:
3586 case NFA_VCOL:
3587 case NFA_VCOL_GT:
3588 case NFA_VCOL_LT:
3589 case NFA_COL:
3590 case NFA_COL_GT:
3591 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003592 case NFA_MARK:
3593 case NFA_MARK_GT:
3594 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003595 {
3596 int n = *++p; /* lnum, col or mark name */
3597
Bram Moolenaar423532e2013-05-29 21:14:42 +02003598 if (nfa_calc_size == TRUE)
3599 {
3600 nstate += 1;
3601 break;
3602 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003603 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003604 if (s == NULL)
3605 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003606 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003607 PUSH(frag(s, list1(&s->out)));
3608 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003609 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003610
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003611 case NFA_ZSTART:
3612 case NFA_ZEND:
3613 default:
3614 /* Operands */
3615 if (nfa_calc_size == TRUE)
3616 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003617 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003618 break;
3619 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003620 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003622 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 PUSH(frag(s, list1(&s->out)));
3624 break;
3625
3626 } /* switch(*p) */
3627
3628 } /* for(p = postfix; *p; ++p) */
3629
3630 if (nfa_calc_size == TRUE)
3631 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003632 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003633 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634 }
3635
3636 e = POP();
3637 if (stackp != stack)
3638 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3639
3640 if (istate >= nstate)
3641 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3642
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643 matchstate = &state_ptr[istate++]; /* the match state */
3644 matchstate->c = NFA_MATCH;
3645 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003646 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003647
3648 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003649 ret = e.start;
3650
3651theend:
3652 vim_free(stack);
3653 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003654
3655#undef POP1
3656#undef PUSH1
3657#undef POP2
3658#undef PUSH2
3659#undef POP
3660#undef PUSH
3661}
3662
Bram Moolenaara2947e22013-06-11 22:44:09 +02003663/*
3664 * After building the NFA program, inspect it to add optimization hints.
3665 */
3666 static void
3667nfa_postprocess(prog)
3668 nfa_regprog_T *prog;
3669{
3670 int i;
3671 int c;
3672
3673 for (i = 0; i < prog->nstate; ++i)
3674 {
3675 c = prog->state[i].c;
3676 if (c == NFA_START_INVISIBLE
3677 || c == NFA_START_INVISIBLE_NEG
3678 || c == NFA_START_INVISIBLE_BEFORE
3679 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3680 {
3681 int directly;
3682
3683 /* Do it directly when what follows is possibly the end of the
3684 * match. */
3685 if (match_follows(prog->state[i].out1->out, 0))
3686 directly = TRUE;
3687 else
3688 {
3689 int ch_invisible = failure_chance(prog->state[i].out, 0);
3690 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3691
3692 /* Postpone when the invisible match is expensive or has a
3693 * lower chance of failing. */
3694 if (c == NFA_START_INVISIBLE_BEFORE
3695 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3696 {
3697 /* "before" matches are very expensive when
3698 * unbounded, always prefer what follows then,
3699 * unless what follows will always match.
3700 * Otherwise strongly prefer what follows. */
3701 if (prog->state[i].val <= 0 && ch_follows > 0)
3702 directly = FALSE;
3703 else
3704 directly = ch_follows * 10 < ch_invisible;
3705 }
3706 else
3707 {
3708 /* normal invisible, first do the one with the
3709 * highest failure chance */
3710 directly = ch_follows < ch_invisible;
3711 }
3712 }
3713 if (directly)
3714 /* switch to the _FIRST state */
3715 ++prog->state[i].c;
3716 }
3717 }
3718}
3719
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720/****************************************************************
3721 * NFA execution code.
3722 ****************************************************************/
3723
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003724typedef struct
3725{
3726 int in_use; /* number of subexpr with useful info */
3727
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003728 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003729 union
3730 {
3731 struct multipos
3732 {
3733 lpos_T start;
3734 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003735 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003736 struct linepos
3737 {
3738 char_u *start;
3739 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003740 } line[NSUBEXP];
3741 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003742} regsub_T;
3743
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003744typedef struct
3745{
3746 regsub_T norm; /* \( .. \) matches */
3747#ifdef FEAT_SYN_HL
3748 regsub_T synt; /* \z( .. \) matches */
3749#endif
3750} regsubs_T;
3751
Bram Moolenaara2d95102013-06-04 14:23:05 +02003752/* nfa_pim_T stores a Postponed Invisible Match. */
3753typedef struct nfa_pim_S nfa_pim_T;
3754struct nfa_pim_S
3755{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003756 int result; /* NFA_PIM_*, see below */
3757 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003758 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003759 union
3760 {
3761 lpos_T pos;
3762 char_u *ptr;
3763 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003764};
3765
3766/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003767#define NFA_PIM_UNUSED 0 /* pim not used */
3768#define NFA_PIM_TODO 1 /* pim not done yet */
3769#define NFA_PIM_MATCH 2 /* pim executed, matches */
3770#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003771
3772
Bram Moolenaar963fee22013-05-26 21:47:28 +02003773/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003774typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003775{
3776 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003777 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003778 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3779 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003780 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003781} nfa_thread_T;
3782
Bram Moolenaar963fee22013-05-26 21:47:28 +02003783/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003784typedef struct
3785{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003786 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003787 int n; /* nr of states currently in "t" */
3788 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003789 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003790 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003791} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003792
Bram Moolenaar5714b802013-05-28 22:03:20 +02003793#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003794static void log_subsexpr __ARGS((regsubs_T *subs));
3795static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003796static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003797
3798 static void
3799log_subsexpr(subs)
3800 regsubs_T *subs;
3801{
3802 log_subexpr(&subs->norm);
3803# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003804 if (nfa_has_zsubexpr)
3805 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003806# endif
3807}
3808
Bram Moolenaar5714b802013-05-28 22:03:20 +02003809 static void
3810log_subexpr(sub)
3811 regsub_T *sub;
3812{
3813 int j;
3814
3815 for (j = 0; j < sub->in_use; j++)
3816 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003817 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003818 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003819 sub->list.multi[j].start.col,
3820 (int)sub->list.multi[j].start.lnum,
3821 sub->list.multi[j].end.col,
3822 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003823 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003824 {
3825 char *s = (char *)sub->list.line[j].start;
3826 char *e = (char *)sub->list.line[j].end;
3827
Bram Moolenaar87953742013-06-05 18:52:40 +02003828 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003829 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003830 s == NULL ? "NULL" : s,
3831 e == NULL ? "NULL" : e);
3832 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003833}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003834
3835 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003836pim_info(pim)
3837 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003838{
3839 static char buf[30];
3840
3841 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3842 buf[0] = NUL;
3843 else
3844 {
3845 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3846 : (int)(pim->end.ptr - reginput));
3847 }
3848 return buf;
3849}
3850
Bram Moolenaar5714b802013-05-28 22:03:20 +02003851#endif
3852
Bram Moolenaar963fee22013-05-26 21:47:28 +02003853/* Used during execution: whether a match has been found. */
3854static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003855
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003856static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003857static void clear_sub __ARGS((regsub_T *sub));
3858static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3859static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaarf2118842013-09-25 18:16:38 +02003860static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003861static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003862static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003863static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3864static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003865static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003866static 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 +02003867static 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 +02003868
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003869/*
3870 * Copy postponed invisible match info from "from" to "to".
3871 */
3872 static void
3873copy_pim(to, from)
3874 nfa_pim_T *to;
3875 nfa_pim_T *from;
3876{
3877 to->result = from->result;
3878 to->state = from->state;
3879 copy_sub(&to->subs.norm, &from->subs.norm);
3880#ifdef FEAT_SYN_HL
3881 if (nfa_has_zsubexpr)
3882 copy_sub(&to->subs.synt, &from->subs.synt);
3883#endif
3884 to->end = from->end;
3885}
3886
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003887 static void
3888clear_sub(sub)
3889 regsub_T *sub;
3890{
3891 if (REG_MULTI)
3892 /* Use 0xff to set lnum to -1 */
3893 vim_memset(sub->list.multi, 0xff,
3894 sizeof(struct multipos) * nfa_nsubexpr);
3895 else
3896 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3897 sub->in_use = 0;
3898}
3899
3900/*
3901 * Copy the submatches from "from" to "to".
3902 */
3903 static void
3904copy_sub(to, from)
3905 regsub_T *to;
3906 regsub_T *from;
3907{
3908 to->in_use = from->in_use;
3909 if (from->in_use > 0)
3910 {
3911 /* Copy the match start and end positions. */
3912 if (REG_MULTI)
3913 mch_memmove(&to->list.multi[0],
3914 &from->list.multi[0],
3915 sizeof(struct multipos) * from->in_use);
3916 else
3917 mch_memmove(&to->list.line[0],
3918 &from->list.line[0],
3919 sizeof(struct linepos) * from->in_use);
3920 }
3921}
3922
3923/*
3924 * Like copy_sub() but exclude the main match.
3925 */
3926 static void
3927copy_sub_off(to, from)
3928 regsub_T *to;
3929 regsub_T *from;
3930{
3931 if (to->in_use < from->in_use)
3932 to->in_use = from->in_use;
3933 if (from->in_use > 1)
3934 {
3935 /* Copy the match start and end positions. */
3936 if (REG_MULTI)
3937 mch_memmove(&to->list.multi[1],
3938 &from->list.multi[1],
3939 sizeof(struct multipos) * (from->in_use - 1));
3940 else
3941 mch_memmove(&to->list.line[1],
3942 &from->list.line[1],
3943 sizeof(struct linepos) * (from->in_use - 1));
3944 }
3945}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003946
Bram Moolenaar428e9872013-05-30 17:05:39 +02003947/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003948 * Like copy_sub() but only do the end of the main match if \ze is present.
3949 */
3950 static void
3951copy_ze_off(to, from)
3952 regsub_T *to;
3953 regsub_T *from;
3954{
3955 if (nfa_has_zend)
3956 {
3957 if (REG_MULTI)
3958 {
3959 if (from->list.multi[0].end.lnum >= 0)
3960 to->list.multi[0].end = from->list.multi[0].end;
3961 }
3962 else
3963 {
3964 if (from->list.line[0].end != NULL)
3965 to->list.line[0].end = from->list.line[0].end;
3966 }
3967 }
3968}
3969
3970/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003971 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003972 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003973 */
3974 static int
3975sub_equal(sub1, sub2)
3976 regsub_T *sub1;
3977 regsub_T *sub2;
3978{
3979 int i;
3980 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003981 linenr_T s1;
3982 linenr_T s2;
3983 char_u *sp1;
3984 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003985
3986 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3987 if (REG_MULTI)
3988 {
3989 for (i = 0; i < todo; ++i)
3990 {
3991 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003992 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003993 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003994 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003995 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003996 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003997 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003998 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003999 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004000 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02004001 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02004002 != sub2->list.multi[i].start.col)
4003 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004004
4005 if (nfa_has_backref)
4006 {
4007 if (i < sub1->in_use)
4008 s1 = sub1->list.multi[i].end.lnum;
4009 else
4010 s1 = -1;
4011 if (i < sub2->in_use)
4012 s2 = sub2->list.multi[i].end.lnum;
4013 else
4014 s2 = -1;
4015 if (s1 != s2)
4016 return FALSE;
4017 if (s1 != -1 && sub1->list.multi[i].end.col
4018 != sub2->list.multi[i].end.col)
4019 return FALSE;
4020 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004021 }
4022 }
4023 else
4024 {
4025 for (i = 0; i < todo; ++i)
4026 {
4027 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004028 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004029 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004030 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004031 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004032 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004033 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004034 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004035 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004036 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004037 if (nfa_has_backref)
4038 {
4039 if (i < sub1->in_use)
4040 sp1 = sub1->list.line[i].end;
4041 else
4042 sp1 = NULL;
4043 if (i < sub2->in_use)
4044 sp2 = sub2->list.line[i].end;
4045 else
4046 sp2 = NULL;
4047 if (sp1 != sp2)
4048 return FALSE;
4049 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004050 }
4051 }
4052
4053 return TRUE;
4054}
4055
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004056#ifdef ENABLE_LOG
4057 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004058report_state(char *action,
4059 regsub_T *sub,
4060 nfa_state_T *state,
4061 int lid,
4062 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004063{
4064 int col;
4065
4066 if (sub->in_use <= 0)
4067 col = -1;
4068 else if (REG_MULTI)
4069 col = sub->list.multi[0].start.col;
4070 else
4071 col = (int)(sub->list.line[0].start - regline);
4072 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004073 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4074 action, abs(state->id), lid, state->c, code, col,
4075 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004076}
4077#endif
4078
Bram Moolenaar43e02982013-06-07 17:31:29 +02004079/*
4080 * Return TRUE if the same state is already in list "l" with the same
4081 * positions as "subs".
4082 */
4083 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02004084has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02004085 nfa_list_T *l; /* runtime state list */
4086 nfa_state_T *state; /* state to update */
4087 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004088 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004089{
4090 nfa_thread_T *thread;
4091 int i;
4092
4093 for (i = 0; i < l->n; ++i)
4094 {
4095 thread = &l->t[i];
4096 if (thread->state->id == state->id
4097 && sub_equal(&thread->subs.norm, &subs->norm)
4098#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004099 && (!nfa_has_zsubexpr
4100 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004101#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004102 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004103 return TRUE;
4104 }
4105 return FALSE;
4106}
4107
4108/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004109 * Return TRUE if "one" and "two" are equal. That includes when both are not
4110 * set.
4111 */
4112 static int
4113pim_equal(one, two)
4114 nfa_pim_T *one;
4115 nfa_pim_T *two;
4116{
4117 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4118 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4119
4120 if (one_unused)
4121 /* one is unused: equal when two is also unused */
4122 return two_unused;
4123 if (two_unused)
4124 /* one is used and two is not: not equal */
4125 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004126 /* compare the state id */
4127 if (one->state->id != two->state->id)
4128 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004129 /* compare the position */
4130 if (REG_MULTI)
4131 return one->end.pos.lnum == two->end.pos.lnum
4132 && one->end.pos.col == two->end.pos.col;
4133 return one->end.ptr == two->end.ptr;
4134}
4135
4136/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004137 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4138 */
4139 static int
4140match_follows(startstate, depth)
4141 nfa_state_T *startstate;
4142 int depth;
4143{
4144 nfa_state_T *state = startstate;
4145
4146 /* avoid too much recursion */
4147 if (depth > 10)
4148 return FALSE;
4149
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004150 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004151 {
4152 switch (state->c)
4153 {
4154 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004155 case NFA_MCLOSE:
4156 case NFA_END_INVISIBLE:
4157 case NFA_END_INVISIBLE_NEG:
4158 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004159 return TRUE;
4160
4161 case NFA_SPLIT:
4162 return match_follows(state->out, depth + 1)
4163 || match_follows(state->out1, depth + 1);
4164
4165 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004166 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004167 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004168 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004169 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004170 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004171 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004172 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004173 case NFA_COMPOSING:
4174 /* skip ahead to next state */
4175 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004176 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004177
4178 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004179 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004180 case NFA_IDENT:
4181 case NFA_SIDENT:
4182 case NFA_KWORD:
4183 case NFA_SKWORD:
4184 case NFA_FNAME:
4185 case NFA_SFNAME:
4186 case NFA_PRINT:
4187 case NFA_SPRINT:
4188 case NFA_WHITE:
4189 case NFA_NWHITE:
4190 case NFA_DIGIT:
4191 case NFA_NDIGIT:
4192 case NFA_HEX:
4193 case NFA_NHEX:
4194 case NFA_OCTAL:
4195 case NFA_NOCTAL:
4196 case NFA_WORD:
4197 case NFA_NWORD:
4198 case NFA_HEAD:
4199 case NFA_NHEAD:
4200 case NFA_ALPHA:
4201 case NFA_NALPHA:
4202 case NFA_LOWER:
4203 case NFA_NLOWER:
4204 case NFA_UPPER:
4205 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004206 case NFA_LOWER_IC:
4207 case NFA_NLOWER_IC:
4208 case NFA_UPPER_IC:
4209 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004210 case NFA_START_COLL:
4211 case NFA_START_NEG_COLL:
4212 case NFA_NEWL:
4213 /* state will advance input */
4214 return FALSE;
4215
4216 default:
4217 if (state->c > 0)
4218 /* state will advance input */
4219 return FALSE;
4220
4221 /* Others: zero-width or possibly zero-width, might still find
4222 * a match at the same position, keep looking. */
4223 break;
4224 }
4225 state = state->out;
4226 }
4227 return FALSE;
4228}
4229
4230
4231/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004232 * Return TRUE if "state" is already in list "l".
4233 */
4234 static int
4235state_in_list(l, state, subs)
4236 nfa_list_T *l; /* runtime state list */
4237 nfa_state_T *state; /* state to update */
4238 regsubs_T *subs; /* pointers to subexpressions */
4239{
4240 if (state->lastlist[nfa_ll_index] == l->id)
4241 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004242 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004243 return TRUE;
4244 }
4245 return FALSE;
4246}
4247
Bram Moolenaard05bf562013-06-30 23:24:08 +02004248/*
4249 * Add "state" and possibly what follows to state list ".".
4250 * Returns "subs_arg", possibly copied into temp_subs.
4251 */
4252
4253 static regsubs_T *
4254addstate(l, state, subs_arg, pim, off)
4255 nfa_list_T *l; /* runtime state list */
4256 nfa_state_T *state; /* state to update */
4257 regsubs_T *subs_arg; /* pointers to subexpressions */
4258 nfa_pim_T *pim; /* postponed look-behind match */
4259 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004261 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004262 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004263 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004264 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004265 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004266 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004267 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004268 regsubs_T *subs = subs_arg;
4269 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004270#ifdef ENABLE_LOG
4271 int did_print = FALSE;
4272#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004273
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004274 switch (state->c)
4275 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004276 case NFA_NCLOSE:
4277 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004278 case NFA_MCLOSE1:
4279 case NFA_MCLOSE2:
4280 case NFA_MCLOSE3:
4281 case NFA_MCLOSE4:
4282 case NFA_MCLOSE5:
4283 case NFA_MCLOSE6:
4284 case NFA_MCLOSE7:
4285 case NFA_MCLOSE8:
4286 case NFA_MCLOSE9:
4287#ifdef FEAT_SYN_HL
4288 case NFA_ZCLOSE:
4289 case NFA_ZCLOSE1:
4290 case NFA_ZCLOSE2:
4291 case NFA_ZCLOSE3:
4292 case NFA_ZCLOSE4:
4293 case NFA_ZCLOSE5:
4294 case NFA_ZCLOSE6:
4295 case NFA_ZCLOSE7:
4296 case NFA_ZCLOSE8:
4297 case NFA_ZCLOSE9:
4298#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004299 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004300 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004301 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004302 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004303 /* These nodes are not added themselves but their "out" and/or
4304 * "out1" may be added below. */
4305 break;
4306
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004307 case NFA_BOL:
4308 case NFA_BOF:
4309 /* "^" won't match past end-of-line, don't bother trying.
4310 * Except when at the end of the line, or when we are going to the
4311 * next line for a look-behind match. */
4312 if (reginput > regline
4313 && *reginput != NUL
4314 && (nfa_endp == NULL
4315 || !REG_MULTI
4316 || reglnum == nfa_endp->se_u.pos.lnum))
4317 goto skip_add;
4318 /* FALLTHROUGH */
4319
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004320 case NFA_MOPEN1:
4321 case NFA_MOPEN2:
4322 case NFA_MOPEN3:
4323 case NFA_MOPEN4:
4324 case NFA_MOPEN5:
4325 case NFA_MOPEN6:
4326 case NFA_MOPEN7:
4327 case NFA_MOPEN8:
4328 case NFA_MOPEN9:
4329#ifdef FEAT_SYN_HL
4330 case NFA_ZOPEN:
4331 case NFA_ZOPEN1:
4332 case NFA_ZOPEN2:
4333 case NFA_ZOPEN3:
4334 case NFA_ZOPEN4:
4335 case NFA_ZOPEN5:
4336 case NFA_ZOPEN6:
4337 case NFA_ZOPEN7:
4338 case NFA_ZOPEN8:
4339 case NFA_ZOPEN9:
4340#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004341 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004342 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004343 /* These nodes need to be added so that we can bail out when it
4344 * was added to this list before at the same position to avoid an
4345 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004346
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004347 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004348 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004349 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004350 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004351 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004352 * when there is a PIM. For NFA_MATCH check the position,
4353 * lower position is preferred. */
4354 if (!nfa_has_backref && pim == NULL && !l->has_pim
4355 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004356 {
4357skip_add:
4358#ifdef ENABLE_LOG
4359 nfa_set_code(state->c);
4360 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4361 abs(state->id), l->id, state->c, code);
4362#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004363 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004364 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004365
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004366 /* Do not add the state again when it exists with the same
4367 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004368 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004369 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004370 }
4371
Bram Moolenaara0169122013-06-26 18:16:58 +02004372 /* When there are backreferences or PIMs the number of states may
4373 * be (a lot) bigger than anticipated. */
4374 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004375 {
4376 int newlen = l->len * 3 / 2 + 50;
4377
Bram Moolenaard05bf562013-06-30 23:24:08 +02004378 if (subs != &temp_subs)
4379 {
4380 /* "subs" may point into the current array, need to make a
4381 * copy before it becomes invalid. */
4382 copy_sub(&temp_subs.norm, &subs->norm);
4383#ifdef FEAT_SYN_HL
4384 if (nfa_has_zsubexpr)
4385 copy_sub(&temp_subs.synt, &subs->synt);
4386#endif
4387 subs = &temp_subs;
4388 }
4389
Bram Moolenaar428e9872013-05-30 17:05:39 +02004390 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4391 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004392 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004393
4394 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004395 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004396 thread = &l->t[l->n++];
4397 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004398 if (pim == NULL)
4399 thread->pim.result = NFA_PIM_UNUSED;
4400 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004401 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004402 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004403 l->has_pim = TRUE;
4404 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004405 copy_sub(&thread->subs.norm, &subs->norm);
4406#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004407 if (nfa_has_zsubexpr)
4408 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004409#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004410#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004411 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004412 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004413#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004414 }
4415
4416#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004417 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004418 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004419#endif
4420 switch (state->c)
4421 {
4422 case NFA_MATCH:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004423// nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004424 break;
4425
4426 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004427 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004428 subs = addstate(l, state->out, subs, pim, off);
4429 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004430 break;
4431
Bram Moolenaar699c1202013-09-25 16:41:54 +02004432 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004433 case NFA_NOPEN:
4434 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004435 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004436 break;
4437
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004438 case NFA_MOPEN:
4439 case NFA_MOPEN1:
4440 case NFA_MOPEN2:
4441 case NFA_MOPEN3:
4442 case NFA_MOPEN4:
4443 case NFA_MOPEN5:
4444 case NFA_MOPEN6:
4445 case NFA_MOPEN7:
4446 case NFA_MOPEN8:
4447 case NFA_MOPEN9:
4448#ifdef FEAT_SYN_HL
4449 case NFA_ZOPEN:
4450 case NFA_ZOPEN1:
4451 case NFA_ZOPEN2:
4452 case NFA_ZOPEN3:
4453 case NFA_ZOPEN4:
4454 case NFA_ZOPEN5:
4455 case NFA_ZOPEN6:
4456 case NFA_ZOPEN7:
4457 case NFA_ZOPEN8:
4458 case NFA_ZOPEN9:
4459#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004460 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004461 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004462 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004463 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004464 sub = &subs->norm;
4465 }
4466#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004467 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004468 {
4469 subidx = state->c - NFA_ZOPEN;
4470 sub = &subs->synt;
4471 }
4472#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004473 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004474 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004475 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004476 sub = &subs->norm;
4477 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004478
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004479 /* avoid compiler warnings */
4480 save_ptr = NULL;
4481 save_lpos.lnum = 0;
4482 save_lpos.col = 0;
4483
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004484 /* Set the position (with "off" added) in the subexpression. Save
4485 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004486 if (REG_MULTI)
4487 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004488 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004489 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004490 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004491 save_in_use = -1;
4492 }
4493 else
4494 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004495 save_in_use = sub->in_use;
4496 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004497 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004498 sub->list.multi[i].start.lnum = -1;
4499 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004500 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004501 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004502 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004503 if (off == -1)
4504 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004505 sub->list.multi[subidx].start.lnum = reglnum + 1;
4506 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004507 }
4508 else
4509 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004510 sub->list.multi[subidx].start.lnum = reglnum;
4511 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004512 (colnr_T)(reginput - regline + off);
4513 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004514 }
4515 else
4516 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004517 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004518 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004519 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004520 save_in_use = -1;
4521 }
4522 else
4523 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004524 save_in_use = sub->in_use;
4525 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004526 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004527 sub->list.line[i].start = NULL;
4528 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004529 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004530 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004531 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004532 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004533 }
4534
Bram Moolenaard05bf562013-06-30 23:24:08 +02004535 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004536 /* "subs" may have changed, need to set "sub" again */
4537#ifdef FEAT_SYN_HL
4538 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4539 sub = &subs->synt;
4540 else
4541#endif
4542 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004543
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004544 if (save_in_use == -1)
4545 {
4546 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004547 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004548 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004549 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004550 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004551 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004552 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 break;
4554
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004555 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004556 if (nfa_has_zend && (REG_MULTI
4557 ? subs->norm.list.multi[0].end.lnum >= 0
4558 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004560 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004561 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004562 break;
4563 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004564 case NFA_MCLOSE1:
4565 case NFA_MCLOSE2:
4566 case NFA_MCLOSE3:
4567 case NFA_MCLOSE4:
4568 case NFA_MCLOSE5:
4569 case NFA_MCLOSE6:
4570 case NFA_MCLOSE7:
4571 case NFA_MCLOSE8:
4572 case NFA_MCLOSE9:
4573#ifdef FEAT_SYN_HL
4574 case NFA_ZCLOSE:
4575 case NFA_ZCLOSE1:
4576 case NFA_ZCLOSE2:
4577 case NFA_ZCLOSE3:
4578 case NFA_ZCLOSE4:
4579 case NFA_ZCLOSE5:
4580 case NFA_ZCLOSE6:
4581 case NFA_ZCLOSE7:
4582 case NFA_ZCLOSE8:
4583 case NFA_ZCLOSE9:
4584#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004585 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004586 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004587 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004589 sub = &subs->norm;
4590 }
4591#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004592 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004593 {
4594 subidx = state->c - NFA_ZCLOSE;
4595 sub = &subs->synt;
4596 }
4597#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004598 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004599 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004600 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004601 sub = &subs->norm;
4602 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004603
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004604 /* We don't fill in gaps here, there must have been an MOPEN that
4605 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004606 save_in_use = sub->in_use;
4607 if (sub->in_use <= subidx)
4608 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 if (REG_MULTI)
4610 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004611 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004612 if (off == -1)
4613 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004614 sub->list.multi[subidx].end.lnum = reglnum + 1;
4615 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004616 }
4617 else
4618 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004619 sub->list.multi[subidx].end.lnum = reglnum;
4620 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004621 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004622 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004623 /* avoid compiler warnings */
4624 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004625 }
4626 else
4627 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004628 save_ptr = sub->list.line[subidx].end;
4629 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004630 /* avoid compiler warnings */
4631 save_lpos.lnum = 0;
4632 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004633 }
4634
Bram Moolenaard05bf562013-06-30 23:24:08 +02004635 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004636 /* "subs" may have changed, need to set "sub" again */
4637#ifdef FEAT_SYN_HL
4638 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4639 sub = &subs->synt;
4640 else
4641#endif
4642 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004643
4644 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004645 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004646 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004647 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004648 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004649 break;
4650 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004651 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004652}
4653
4654/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004655 * Like addstate(), but the new state(s) are put at position "*ip".
4656 * Used for zero-width matches, next state to use is the added one.
4657 * This makes sure the order of states to be tried does not change, which
4658 * matters for alternatives.
4659 */
4660 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004661addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004662 nfa_list_T *l; /* runtime state list */
4663 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004664 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004665 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004666 int *ip;
4667{
4668 int tlen = l->n;
4669 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004670 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004671
4672 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004673 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004674
Bram Moolenaar4b417062013-05-25 20:19:50 +02004675 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004676 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004677 return;
4678
4679 /* re-order to put the new state at the current position */
4680 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004681 if (count == 0)
4682 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004683 if (count == 1)
4684 {
4685 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004686 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004687 }
4688 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004689 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004690 if (l->n + count - 1 >= l->len)
4691 {
4692 /* not enough space to move the new states, reallocate the list
4693 * and move the states to the right position */
4694 nfa_thread_T *newl;
4695
4696 l->len = l->len * 3 / 2 + 50;
4697 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4698 if (newl == NULL)
4699 return;
4700 mch_memmove(&(newl[0]),
4701 &(l->t[0]),
4702 sizeof(nfa_thread_T) * listidx);
4703 mch_memmove(&(newl[listidx]),
4704 &(l->t[l->n - count]),
4705 sizeof(nfa_thread_T) * count);
4706 mch_memmove(&(newl[listidx + count]),
4707 &(l->t[listidx + 1]),
4708 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4709 vim_free(l->t);
4710 l->t = newl;
4711 }
4712 else
4713 {
4714 /* make space for new states, then move them from the
4715 * end to the current position */
4716 mch_memmove(&(l->t[listidx + count]),
4717 &(l->t[listidx + 1]),
4718 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4719 mch_memmove(&(l->t[listidx]),
4720 &(l->t[l->n - 1]),
4721 sizeof(nfa_thread_T) * count);
4722 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004723 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004724 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004725 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004726}
4727
4728/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004729 * Check character class "class" against current character c.
4730 */
4731 static int
4732check_char_class(class, c)
4733 int class;
4734 int c;
4735{
4736 switch (class)
4737 {
4738 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004739 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004740 return OK;
4741 break;
4742 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004743 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004744 return OK;
4745 break;
4746 case NFA_CLASS_BLANK:
4747 if (c == ' ' || c == '\t')
4748 return OK;
4749 break;
4750 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004751 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752 return OK;
4753 break;
4754 case NFA_CLASS_DIGIT:
4755 if (VIM_ISDIGIT(c))
4756 return OK;
4757 break;
4758 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004759 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004760 return OK;
4761 break;
4762 case NFA_CLASS_LOWER:
4763 if (MB_ISLOWER(c))
4764 return OK;
4765 break;
4766 case NFA_CLASS_PRINT:
4767 if (vim_isprintc(c))
4768 return OK;
4769 break;
4770 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004771 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004772 return OK;
4773 break;
4774 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004775 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004776 return OK;
4777 break;
4778 case NFA_CLASS_UPPER:
4779 if (MB_ISUPPER(c))
4780 return OK;
4781 break;
4782 case NFA_CLASS_XDIGIT:
4783 if (vim_isxdigit(c))
4784 return OK;
4785 break;
4786 case NFA_CLASS_TAB:
4787 if (c == '\t')
4788 return OK;
4789 break;
4790 case NFA_CLASS_RETURN:
4791 if (c == '\r')
4792 return OK;
4793 break;
4794 case NFA_CLASS_BACKSPACE:
4795 if (c == '\b')
4796 return OK;
4797 break;
4798 case NFA_CLASS_ESCAPE:
4799 if (c == '\033')
4800 return OK;
4801 break;
4802
4803 default:
4804 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004805 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004806 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004807 }
4808 return FAIL;
4809}
4810
Bram Moolenaar5714b802013-05-28 22:03:20 +02004811/*
4812 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004813 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004814 */
4815 static int
4816match_backref(sub, subidx, bytelen)
4817 regsub_T *sub; /* pointers to subexpressions */
4818 int subidx;
4819 int *bytelen; /* out: length of match in bytes */
4820{
4821 int len;
4822
4823 if (sub->in_use <= subidx)
4824 {
4825retempty:
4826 /* backref was not set, match an empty string */
4827 *bytelen = 0;
4828 return TRUE;
4829 }
4830
4831 if (REG_MULTI)
4832 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004833 if (sub->list.multi[subidx].start.lnum < 0
4834 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004835 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004836 if (sub->list.multi[subidx].start.lnum == reglnum
4837 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004838 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004839 len = sub->list.multi[subidx].end.col
4840 - sub->list.multi[subidx].start.col;
4841 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4842 reginput, &len) == 0)
4843 {
4844 *bytelen = len;
4845 return TRUE;
4846 }
4847 }
4848 else
4849 {
4850 if (match_with_backref(
4851 sub->list.multi[subidx].start.lnum,
4852 sub->list.multi[subidx].start.col,
4853 sub->list.multi[subidx].end.lnum,
4854 sub->list.multi[subidx].end.col,
4855 bytelen) == RA_MATCH)
4856 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004857 }
4858 }
4859 else
4860 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004861 if (sub->list.line[subidx].start == NULL
4862 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004863 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004864 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4865 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004866 {
4867 *bytelen = len;
4868 return TRUE;
4869 }
4870 }
4871 return FALSE;
4872}
4873
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004874#ifdef FEAT_SYN_HL
4875
4876static int match_zref __ARGS((int subidx, int *bytelen));
4877
4878/*
4879 * Check for a match with \z subexpression "subidx".
4880 * Return TRUE if it matches.
4881 */
4882 static int
4883match_zref(subidx, bytelen)
4884 int subidx;
4885 int *bytelen; /* out: length of match in bytes */
4886{
4887 int len;
4888
4889 cleanup_zsubexpr();
4890 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4891 {
4892 /* backref was not set, match an empty string */
4893 *bytelen = 0;
4894 return TRUE;
4895 }
4896
4897 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4898 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4899 {
4900 *bytelen = len;
4901 return TRUE;
4902 }
4903 return FALSE;
4904}
4905#endif
4906
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004907/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004908 * Save list IDs for all NFA states of "prog" into "list".
4909 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004910 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004911 */
4912 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004913nfa_save_listids(prog, list)
4914 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004915 int *list;
4916{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004917 int i;
4918 nfa_state_T *p;
4919
4920 /* Order in the list is reverse, it's a bit faster that way. */
4921 p = &prog->state[0];
4922 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004923 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004924 list[i] = p->lastlist[1];
4925 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004926 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004927 }
4928}
4929
4930/*
4931 * Restore list IDs from "list" to all NFA states.
4932 */
4933 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004934nfa_restore_listids(prog, list)
4935 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004936 int *list;
4937{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004938 int i;
4939 nfa_state_T *p;
4940
4941 p = &prog->state[0];
4942 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004943 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004944 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004945 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946 }
4947}
4948
Bram Moolenaar423532e2013-05-29 21:14:42 +02004949 static int
4950nfa_re_num_cmp(val, op, pos)
4951 long_u val;
4952 int op;
4953 long_u pos;
4954{
4955 if (op == 1) return pos > val;
4956 if (op == 2) return pos < val;
4957 return val == pos;
4958}
4959
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004960static 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 +02004961static 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 +02004962
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004963/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004964 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004965 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4966 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004967 */
4968 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004969recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004970 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004971 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004972 nfa_regprog_T *prog;
4973 regsubs_T *submatch;
4974 regsubs_T *m;
4975 int **listids;
4976{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004977 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004978 int save_reglnum = reglnum;
4979 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004980 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004981 save_se_T *save_nfa_endp = nfa_endp;
4982 save_se_T endpos;
4983 save_se_T *endposp = NULL;
4984 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004985 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004986
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004987 if (pim != NULL)
4988 {
4989 /* start at the position where the postponed match was */
4990 if (REG_MULTI)
4991 reginput = regline + pim->end.pos.col;
4992 else
4993 reginput = pim->end.ptr;
4994 }
4995
Bram Moolenaardecd9542013-06-07 16:31:50 +02004996 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004997 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4998 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4999 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005000 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005001 /* The recursive match must end at the current position. When "pim" is
5002 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005003 endposp = &endpos;
5004 if (REG_MULTI)
5005 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005006 if (pim == NULL)
5007 {
5008 endpos.se_u.pos.col = (int)(reginput - regline);
5009 endpos.se_u.pos.lnum = reglnum;
5010 }
5011 else
5012 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005013 }
5014 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005015 {
5016 if (pim == NULL)
5017 endpos.se_u.ptr = reginput;
5018 else
5019 endpos.se_u.ptr = pim->end.ptr;
5020 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005021
5022 /* Go back the specified number of bytes, or as far as the
5023 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005024 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005025 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005026 if (state->val <= 0)
5027 {
5028 if (REG_MULTI)
5029 {
5030 regline = reg_getline(--reglnum);
5031 if (regline == NULL)
5032 /* can't go before the first line */
5033 regline = reg_getline(++reglnum);
5034 }
5035 reginput = regline;
5036 }
5037 else
5038 {
5039 if (REG_MULTI && (int)(reginput - regline) < state->val)
5040 {
5041 /* Not enough bytes in this line, go to end of
5042 * previous line. */
5043 regline = reg_getline(--reglnum);
5044 if (regline == NULL)
5045 {
5046 /* can't go before the first line */
5047 regline = reg_getline(++reglnum);
5048 reginput = regline;
5049 }
5050 else
5051 reginput = regline + STRLEN(regline);
5052 }
5053 if ((int)(reginput - regline) >= state->val)
5054 {
5055 reginput -= state->val;
5056#ifdef FEAT_MBYTE
5057 if (has_mbyte)
5058 reginput -= mb_head_off(regline, reginput);
5059#endif
5060 }
5061 else
5062 reginput = regline;
5063 }
5064 }
5065
Bram Moolenaarf46da702013-06-02 22:37:42 +02005066#ifdef ENABLE_LOG
5067 if (log_fd != stderr)
5068 fclose(log_fd);
5069 log_fd = NULL;
5070#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005071 /* Have to clear the lastlist field of the NFA nodes, so that
5072 * nfa_regmatch() and addstate() can run properly after recursion. */
5073 if (nfa_ll_index == 1)
5074 {
5075 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5076 * values and clear them. */
5077 if (*listids == NULL)
5078 {
5079 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5080 if (*listids == NULL)
5081 {
5082 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5083 return 0;
5084 }
5085 }
5086 nfa_save_listids(prog, *listids);
5087 need_restore = TRUE;
5088 /* any value of nfa_listid will do */
5089 }
5090 else
5091 {
5092 /* First recursive nfa_regmatch() call, switch to the second lastlist
5093 * entry. Make sure nfa_listid is different from a previous recursive
5094 * call, because some states may still have this ID. */
5095 ++nfa_ll_index;
5096 if (nfa_listid <= nfa_alt_listid)
5097 nfa_listid = nfa_alt_listid;
5098 }
5099
5100 /* Call nfa_regmatch() to check if the current concat matches at this
5101 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005102 nfa_endp = endposp;
5103 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005104
5105 if (need_restore)
5106 nfa_restore_listids(prog, *listids);
5107 else
5108 {
5109 --nfa_ll_index;
5110 nfa_alt_listid = nfa_listid;
5111 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005112
5113 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005114 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005115 if (REG_MULTI)
5116 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005117 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005118 nfa_match = save_nfa_match;
5119 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005120 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005121
5122#ifdef ENABLE_LOG
5123 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5124 if (log_fd != NULL)
5125 {
5126 fprintf(log_fd, "****************************\n");
5127 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5128 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5129 fprintf(log_fd, "****************************\n");
5130 }
5131 else
5132 {
5133 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5134 log_fd = stderr;
5135 }
5136#endif
5137
5138 return result;
5139}
5140
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005141static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005142static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005143
5144/*
5145 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005146 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005147 * NFA_ANY: 1
5148 * specific character: 99
5149 */
5150 static int
5151failure_chance(state, depth)
5152 nfa_state_T *state;
5153 int depth;
5154{
5155 int c = state->c;
5156 int l, r;
5157
5158 /* detect looping */
5159 if (depth > 4)
5160 return 1;
5161
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005162 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005163 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005164 case NFA_SPLIT:
5165 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5166 /* avoid recursive stuff */
5167 return 1;
5168 /* two alternatives, use the lowest failure chance */
5169 l = failure_chance(state->out, depth + 1);
5170 r = failure_chance(state->out1, depth + 1);
5171 return l < r ? l : r;
5172
5173 case NFA_ANY:
5174 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005175 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005176
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005177 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005178 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005179 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005180 /* empty match works always */
5181 return 0;
5182
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005183 case NFA_START_INVISIBLE:
5184 case NFA_START_INVISIBLE_FIRST:
5185 case NFA_START_INVISIBLE_NEG:
5186 case NFA_START_INVISIBLE_NEG_FIRST:
5187 case NFA_START_INVISIBLE_BEFORE:
5188 case NFA_START_INVISIBLE_BEFORE_FIRST:
5189 case NFA_START_INVISIBLE_BEFORE_NEG:
5190 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5191 case NFA_START_PATTERN:
5192 /* recursive regmatch is expensive, use low failure chance */
5193 return 5;
5194
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005195 case NFA_BOL:
5196 case NFA_EOL:
5197 case NFA_BOF:
5198 case NFA_EOF:
5199 case NFA_NEWL:
5200 return 99;
5201
5202 case NFA_BOW:
5203 case NFA_EOW:
5204 return 90;
5205
5206 case NFA_MOPEN:
5207 case NFA_MOPEN1:
5208 case NFA_MOPEN2:
5209 case NFA_MOPEN3:
5210 case NFA_MOPEN4:
5211 case NFA_MOPEN5:
5212 case NFA_MOPEN6:
5213 case NFA_MOPEN7:
5214 case NFA_MOPEN8:
5215 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005216#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005217 case NFA_ZOPEN:
5218 case NFA_ZOPEN1:
5219 case NFA_ZOPEN2:
5220 case NFA_ZOPEN3:
5221 case NFA_ZOPEN4:
5222 case NFA_ZOPEN5:
5223 case NFA_ZOPEN6:
5224 case NFA_ZOPEN7:
5225 case NFA_ZOPEN8:
5226 case NFA_ZOPEN9:
5227 case NFA_ZCLOSE:
5228 case NFA_ZCLOSE1:
5229 case NFA_ZCLOSE2:
5230 case NFA_ZCLOSE3:
5231 case NFA_ZCLOSE4:
5232 case NFA_ZCLOSE5:
5233 case NFA_ZCLOSE6:
5234 case NFA_ZCLOSE7:
5235 case NFA_ZCLOSE8:
5236 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005237#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005238 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005239 case NFA_MCLOSE1:
5240 case NFA_MCLOSE2:
5241 case NFA_MCLOSE3:
5242 case NFA_MCLOSE4:
5243 case NFA_MCLOSE5:
5244 case NFA_MCLOSE6:
5245 case NFA_MCLOSE7:
5246 case NFA_MCLOSE8:
5247 case NFA_MCLOSE9:
5248 case NFA_NCLOSE:
5249 return failure_chance(state->out, depth + 1);
5250
5251 case NFA_BACKREF1:
5252 case NFA_BACKREF2:
5253 case NFA_BACKREF3:
5254 case NFA_BACKREF4:
5255 case NFA_BACKREF5:
5256 case NFA_BACKREF6:
5257 case NFA_BACKREF7:
5258 case NFA_BACKREF8:
5259 case NFA_BACKREF9:
5260#ifdef FEAT_SYN_HL
5261 case NFA_ZREF1:
5262 case NFA_ZREF2:
5263 case NFA_ZREF3:
5264 case NFA_ZREF4:
5265 case NFA_ZREF5:
5266 case NFA_ZREF6:
5267 case NFA_ZREF7:
5268 case NFA_ZREF8:
5269 case NFA_ZREF9:
5270#endif
5271 /* backreferences don't match in many places */
5272 return 94;
5273
5274 case NFA_LNUM_GT:
5275 case NFA_LNUM_LT:
5276 case NFA_COL_GT:
5277 case NFA_COL_LT:
5278 case NFA_VCOL_GT:
5279 case NFA_VCOL_LT:
5280 case NFA_MARK_GT:
5281 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005282 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005283 /* before/after positions don't match very often */
5284 return 85;
5285
5286 case NFA_LNUM:
5287 return 90;
5288
5289 case NFA_CURSOR:
5290 case NFA_COL:
5291 case NFA_VCOL:
5292 case NFA_MARK:
5293 /* specific positions rarely match */
5294 return 98;
5295
5296 case NFA_COMPOSING:
5297 return 95;
5298
5299 default:
5300 if (c > 0)
5301 /* character match fails often */
5302 return 95;
5303 }
5304
5305 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005306 return 50;
5307}
5308
Bram Moolenaarf46da702013-06-02 22:37:42 +02005309/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005310 * Skip until the char "c" we know a match must start with.
5311 */
5312 static int
5313skip_to_start(c, colp)
5314 int c;
5315 colnr_T *colp;
5316{
5317 char_u *s;
5318
5319 /* Used often, do some work to avoid call overhead. */
5320 if (!ireg_ic
5321#ifdef FEAT_MBYTE
5322 && !has_mbyte
5323#endif
5324 )
5325 s = vim_strbyte(regline + *colp, c);
5326 else
5327 s = cstrchr(regline + *colp, c);
5328 if (s == NULL)
5329 return FAIL;
5330 *colp = (int)(s - regline);
5331 return OK;
5332}
5333
5334/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005335 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005336 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005337 * Returns zero for no match, 1 for a match.
5338 */
5339 static long
5340find_match_text(startcol, regstart, match_text)
5341 colnr_T startcol;
5342 int regstart;
5343 char_u *match_text;
5344{
5345 colnr_T col = startcol;
5346 int c1, c2;
5347 int len1, len2;
5348 int match;
5349
5350 for (;;)
5351 {
5352 match = TRUE;
5353 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5354 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5355 {
5356 c1 = PTR2CHAR(match_text + len1);
5357 c2 = PTR2CHAR(regline + col + len2);
5358 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5359 {
5360 match = FALSE;
5361 break;
5362 }
5363 len2 += MB_CHAR2LEN(c2);
5364 }
5365 if (match
5366#ifdef FEAT_MBYTE
5367 /* check that no composing char follows */
5368 && !(enc_utf8
5369 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5370#endif
5371 )
5372 {
5373 cleanup_subexpr();
5374 if (REG_MULTI)
5375 {
5376 reg_startpos[0].lnum = reglnum;
5377 reg_startpos[0].col = col;
5378 reg_endpos[0].lnum = reglnum;
5379 reg_endpos[0].col = col + len2;
5380 }
5381 else
5382 {
5383 reg_startp[0] = regline + col;
5384 reg_endp[0] = regline + col + len2;
5385 }
5386 return 1L;
5387 }
5388
5389 /* Try finding regstart after the current match. */
5390 col += MB_CHAR2LEN(regstart); /* skip regstart */
5391 if (skip_to_start(regstart, &col) == FAIL)
5392 break;
5393 }
5394 return 0L;
5395}
5396
5397/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005398 * Main matching routine.
5399 *
5400 * Run NFA to determine whether it matches reginput.
5401 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005402 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005403 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005404 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005405 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005406 * Note: Caller must ensure that: start != NULL.
5407 */
5408 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005409nfa_regmatch(prog, start, submatch, m)
5410 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005411 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005412 regsubs_T *submatch;
5413 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005414{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005415 int result;
5416 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005417 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005418 int go_to_nextline = FALSE;
5419 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005420 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005421 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005422 nfa_list_T *thislist;
5423 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005424 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005425 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005426 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005427 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005428 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005429 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005430#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005431 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005432
5433 if (debug == NULL)
5434 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005435 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005436 return FALSE;
5437 }
5438#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005439 /* Some patterns may take a long time to match, especially when using
5440 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5441 fast_breakcheck();
5442 if (got_int)
5443 return FALSE;
5444
Bram Moolenaar963fee22013-05-26 21:47:28 +02005445 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005446
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005447 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005448 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005449 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005450 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005451 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005452 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005453 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005454 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005455
5456#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005457 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005458 if (log_fd != NULL)
5459 {
5460 fprintf(log_fd, "**********************************\n");
5461 nfa_set_code(start->c);
5462 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5463 abs(start->id), code);
5464 fprintf(log_fd, "**********************************\n");
5465 }
5466 else
5467 {
5468 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5469 log_fd = stderr;
5470 }
5471#endif
5472
5473 thislist = &list[0];
5474 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005475 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 nextlist = &list[1];
5477 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005478 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005479#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005480 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005481#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005482 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005483
5484 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5485 * it's the first MOPEN. */
5486 if (toplevel)
5487 {
5488 if (REG_MULTI)
5489 {
5490 m->norm.list.multi[0].start.lnum = reglnum;
5491 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5492 }
5493 else
5494 m->norm.list.line[0].start = reginput;
5495 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005496 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005497 }
5498 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005499 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005500
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005501#define ADD_STATE_IF_MATCH(state) \
5502 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005503 add_state = state->out; \
5504 add_off = clen; \
5505 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005506
5507 /*
5508 * Run for each character.
5509 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005510 for (;;)
5511 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005512 int curc;
5513 int clen;
5514
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515#ifdef FEAT_MBYTE
5516 if (has_mbyte)
5517 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005518 curc = (*mb_ptr2char)(reginput);
5519 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005520 }
5521 else
5522#endif
5523 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005524 curc = *reginput;
5525 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005527 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005528 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005529 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005530 go_to_nextline = FALSE;
5531 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005532
5533 /* swap lists */
5534 thislist = &list[flag];
5535 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005536 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005537 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005538 ++nfa_listid;
5539 thislist->id = nfa_listid;
5540 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005541
5542#ifdef ENABLE_LOG
5543 fprintf(log_fd, "------------------------------------------\n");
5544 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005545 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005547 {
5548 int i;
5549
5550 for (i = 0; i < thislist->n; i++)
5551 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5552 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553 fprintf(log_fd, "\n");
5554#endif
5555
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005556#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005557 fprintf(debug, "\n-------------------\n");
5558#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005559 /*
5560 * If the state lists are empty we can stop.
5561 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005562 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005563 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564
5565 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005566 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005567 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005568 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005569
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005570#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005571 nfa_set_code(t->state->c);
5572 fprintf(debug, "%s, ", code);
5573#endif
5574#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005575 {
5576 int col;
5577
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005578 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005579 col = -1;
5580 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005581 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005582 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005583 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005584 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005585 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5586 abs(t->state->id), (int)t->state->c, code, col,
5587 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005588 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005589#endif
5590
5591 /*
5592 * Handle the possible codes of the current state.
5593 * The most important is NFA_MATCH.
5594 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005595 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005596 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005597 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598 switch (t->state->c)
5599 {
5600 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005601 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005602#ifdef FEAT_MBYTE
5603 /* If the match ends before a composing characters and
5604 * ireg_icombine is not set, that is not really a match. */
5605 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5606 break;
5607#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005608 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005609 copy_sub(&submatch->norm, &t->subs.norm);
5610#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005611 if (nfa_has_zsubexpr)
5612 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005613#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005615 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005616#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005617 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005618 * states at this position. When the list of states is going
5619 * to be empty quit without advancing, so that "reginput" is
5620 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005621 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005622 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005623 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005624 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005625
5626 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005627 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005628 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005629 /*
5630 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005631 * NFA_START_INVISIBLE_BEFORE node.
5632 * They surround a zero-width group, used with "\@=", "\&",
5633 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005634 * If we got here, it means that the current "invisible" group
5635 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005636 * nfa_regmatch(). For a look-behind match only when it ends
5637 * in the position in "nfa_endp".
5638 * Submatches are stored in *m, and used in the parent call.
5639 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005640#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005641 if (nfa_endp != NULL)
5642 {
5643 if (REG_MULTI)
5644 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5645 (int)reglnum,
5646 (int)nfa_endp->se_u.pos.lnum,
5647 (int)(reginput - regline),
5648 nfa_endp->se_u.pos.col);
5649 else
5650 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5651 (int)(reginput - regline),
5652 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005653 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005654#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005655 /* If "nfa_endp" is set it's only a match if it ends at
5656 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005657 if (nfa_endp != NULL && (REG_MULTI
5658 ? (reglnum != nfa_endp->se_u.pos.lnum
5659 || (int)(reginput - regline)
5660 != nfa_endp->se_u.pos.col)
5661 : reginput != nfa_endp->se_u.ptr))
5662 break;
5663
5664 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005665 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005666 {
5667 copy_sub(&m->norm, &t->subs.norm);
5668#ifdef FEAT_SYN_HL
5669 if (nfa_has_zsubexpr)
5670 copy_sub(&m->synt, &t->subs.synt);
5671#endif
5672 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005673#ifdef ENABLE_LOG
5674 fprintf(log_fd, "Match found:\n");
5675 log_subsexpr(m);
5676#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005677 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005678 /* See comment above at "goto nextchar". */
5679 if (nextlist->n == 0)
5680 clen = 0;
5681 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005682
5683 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005684 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005685 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005686 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005687 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005688 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005689 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005690 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005691 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005692#ifdef ENABLE_LOG
5693 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5694 failure_chance(t->state->out, 0),
5695 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005696#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005697 /* Do it directly if there already is a PIM or when
5698 * nfa_postprocess() detected it will work better. */
5699 if (t->pim.result != NFA_PIM_UNUSED
5700 || t->state->c == NFA_START_INVISIBLE_FIRST
5701 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5702 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5703 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005704 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005705 int in_use = m->norm.in_use;
5706
Bram Moolenaar699c1202013-09-25 16:41:54 +02005707 /* Copy submatch info for the recursive call, opposite
5708 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005709 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005710#ifdef FEAT_SYN_HL
5711 if (nfa_has_zsubexpr)
5712 copy_sub_off(&m->synt, &t->subs.synt);
5713#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005714
Bram Moolenaara2d95102013-06-04 14:23:05 +02005715 /*
5716 * First try matching the invisible match, then what
5717 * follows.
5718 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005719 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005720 submatch, m, &listids);
5721
Bram Moolenaardecd9542013-06-07 16:31:50 +02005722 /* for \@! and \@<! it is a match when the result is
5723 * FALSE */
5724 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005725 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5726 || t->state->c
5727 == NFA_START_INVISIBLE_BEFORE_NEG
5728 || t->state->c
5729 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005730 {
5731 /* Copy submatch info from the recursive call */
5732 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005733#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005734 if (nfa_has_zsubexpr)
5735 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005736#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005737 /* If the pattern has \ze and it matched in the
5738 * sub pattern, use it. */
5739 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005740
Bram Moolenaara2d95102013-06-04 14:23:05 +02005741 /* t->state->out1 is the corresponding
5742 * END_INVISIBLE node; Add its out to the current
5743 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005744 add_here = TRUE;
5745 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005746 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005747 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005748 }
5749 else
5750 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005751 nfa_pim_T pim;
5752
Bram Moolenaara2d95102013-06-04 14:23:05 +02005753 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005754 * First try matching what follows. Only if a match
5755 * is found verify the invisible match matches. Add a
5756 * nfa_pim_T to the following states, it contains info
5757 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005758 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005759 pim.state = t->state;
5760 pim.result = NFA_PIM_TODO;
5761 pim.subs.norm.in_use = 0;
5762#ifdef FEAT_SYN_HL
5763 pim.subs.synt.in_use = 0;
5764#endif
5765 if (REG_MULTI)
5766 {
5767 pim.end.pos.col = (int)(reginput - regline);
5768 pim.end.pos.lnum = reglnum;
5769 }
5770 else
5771 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005772
5773 /* t->state->out1 is the corresponding END_INVISIBLE
5774 * node; Add its out to the current list (zero-width
5775 * match). */
5776 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005777 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005778 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005779 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005780 break;
5781
Bram Moolenaar87953742013-06-05 18:52:40 +02005782 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005783 {
5784 nfa_state_T *skip = NULL;
5785#ifdef ENABLE_LOG
5786 int skip_lid = 0;
5787#endif
5788
5789 /* There is no point in trying to match the pattern if the
5790 * output state is not going to be added to the list. */
5791 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5792 {
5793 skip = t->state->out1->out;
5794#ifdef ENABLE_LOG
5795 skip_lid = nextlist->id;
5796#endif
5797 }
5798 else if (state_in_list(nextlist,
5799 t->state->out1->out->out, &t->subs))
5800 {
5801 skip = t->state->out1->out->out;
5802#ifdef ENABLE_LOG
5803 skip_lid = nextlist->id;
5804#endif
5805 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005806 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005807 t->state->out1->out->out, &t->subs))
5808 {
5809 skip = t->state->out1->out->out;
5810#ifdef ENABLE_LOG
5811 skip_lid = thislist->id;
5812#endif
5813 }
5814 if (skip != NULL)
5815 {
5816#ifdef ENABLE_LOG
5817 nfa_set_code(skip->c);
5818 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5819 abs(skip->id), skip_lid, skip->c, code);
5820#endif
5821 break;
5822 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005823 /* Copy submatch info to the recursive call, opposite of what
5824 * happens afterwards. */
5825 copy_sub_off(&m->norm, &t->subs.norm);
5826#ifdef FEAT_SYN_HL
5827 if (nfa_has_zsubexpr)
5828 copy_sub_off(&m->synt, &t->subs.synt);
5829#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005830
Bram Moolenaar87953742013-06-05 18:52:40 +02005831 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005832 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005833 submatch, m, &listids);
5834 if (result)
5835 {
5836 int bytelen;
5837
5838#ifdef ENABLE_LOG
5839 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5840 log_subsexpr(m);
5841#endif
5842 /* Copy submatch info from the recursive call */
5843 copy_sub_off(&t->subs.norm, &m->norm);
5844#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005845 if (nfa_has_zsubexpr)
5846 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005847#endif
5848 /* Now we need to skip over the matched text and then
5849 * continue with what follows. */
5850 if (REG_MULTI)
5851 /* TODO: multi-line match */
5852 bytelen = m->norm.list.multi[0].end.col
5853 - (int)(reginput - regline);
5854 else
5855 bytelen = (int)(m->norm.list.line[0].end - reginput);
5856
5857#ifdef ENABLE_LOG
5858 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5859#endif
5860 if (bytelen == 0)
5861 {
5862 /* empty match, output of corresponding
5863 * NFA_END_PATTERN/NFA_SKIP to be used at current
5864 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005865 add_here = TRUE;
5866 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005867 }
5868 else if (bytelen <= clen)
5869 {
5870 /* match current character, output of corresponding
5871 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005872 add_state = t->state->out1->out->out;
5873 add_off = clen;
5874 }
5875 else
5876 {
5877 /* skip over the matched characters, set character
5878 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005879 add_state = t->state->out1->out;
5880 add_off = bytelen;
5881 add_count = bytelen - clen;
5882 }
5883 }
5884 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005885 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005886
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005887 case NFA_BOL:
5888 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005889 {
5890 add_here = TRUE;
5891 add_state = t->state->out;
5892 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005893 break;
5894
5895 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005896 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005897 {
5898 add_here = TRUE;
5899 add_state = t->state->out;
5900 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005901 break;
5902
5903 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005904 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005905
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005906 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005907 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005908#ifdef FEAT_MBYTE
5909 else if (has_mbyte)
5910 {
5911 int this_class;
5912
5913 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005914 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005915 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005916 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005917 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005918 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005919 }
5920#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005921 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005922 || (reginput > regline
5923 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005924 result = FALSE;
5925 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005926 {
5927 add_here = TRUE;
5928 add_state = t->state->out;
5929 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005930 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005931
5932 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005933 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005934 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005935 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005936#ifdef FEAT_MBYTE
5937 else if (has_mbyte)
5938 {
5939 int this_class, prev_class;
5940
5941 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005942 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005943 prev_class = reg_prev_class();
5944 if (this_class == prev_class
5945 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005946 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005947 }
5948#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005949 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005950 || (reginput[0] != NUL
5951 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005952 result = FALSE;
5953 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005954 {
5955 add_here = TRUE;
5956 add_state = t->state->out;
5957 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005958 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005959
Bram Moolenaar4b780632013-05-31 22:14:52 +02005960 case NFA_BOF:
5961 if (reglnum == 0 && reginput == regline
5962 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005963 {
5964 add_here = TRUE;
5965 add_state = t->state->out;
5966 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005967 break;
5968
5969 case NFA_EOF:
5970 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005971 {
5972 add_here = TRUE;
5973 add_state = t->state->out;
5974 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005975 break;
5976
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005977#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005978 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005979 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005980 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005981 int len = 0;
5982 nfa_state_T *end;
5983 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005984 int cchars[MAX_MCO];
5985 int ccount = 0;
5986 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005987
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005988 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005989 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005990 if (utf_iscomposing(sta->c))
5991 {
5992 /* Only match composing character(s), ignore base
5993 * character. Used for ".{composing}" and "{composing}"
5994 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005995 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005996 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005997 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005998 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005999 /* If \Z was present, then ignore composing characters.
6000 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006001 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006002 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006003 else
6004 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006005 while (sta->c != NFA_END_COMPOSING)
6006 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006007 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006008
6009 /* Check base character matches first, unless ignored. */
6010 else if (len > 0 || mc == sta->c)
6011 {
6012 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006013 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006014 len += mb_char2len(mc);
6015 sta = sta->out;
6016 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006017
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006018 /* We don't care about the order of composing characters.
6019 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006020 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006021 {
6022 mc = mb_ptr2char(reginput + len);
6023 cchars[ccount++] = mc;
6024 len += mb_char2len(mc);
6025 if (ccount == MAX_MCO)
6026 break;
6027 }
6028
6029 /* Check that each composing char in the pattern matches a
6030 * composing char in the text. We do not check if all
6031 * composing chars are matched. */
6032 result = OK;
6033 while (sta->c != NFA_END_COMPOSING)
6034 {
6035 for (j = 0; j < ccount; ++j)
6036 if (cchars[j] == sta->c)
6037 break;
6038 if (j == ccount)
6039 {
6040 result = FAIL;
6041 break;
6042 }
6043 sta = sta->out;
6044 }
6045 }
6046 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006047 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006048
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006049 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006050 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006051 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006052 }
6053#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054
6055 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006056 if (curc == NUL && !reg_line_lbr && REG_MULTI
6057 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006058 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006059 go_to_nextline = TRUE;
6060 /* Pass -1 for the offset, which means taking the position
6061 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006062 add_state = t->state->out;
6063 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006064 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006065 else if (curc == '\n' && reg_line_lbr)
6066 {
6067 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006068 add_state = t->state->out;
6069 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006070 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006071 break;
6072
Bram Moolenaar417bad22013-06-07 14:08:30 +02006073 case NFA_START_COLL:
6074 case NFA_START_NEG_COLL:
6075 {
6076 /* What follows is a list of characters, until NFA_END_COLL.
6077 * One of them must match or none of them must match. */
6078 nfa_state_T *state;
6079 int result_if_matched;
6080 int c1, c2;
6081
6082 /* Never match EOL. If it's part of the collection it is added
6083 * as a separate state with an OR. */
6084 if (curc == NUL)
6085 break;
6086
6087 state = t->state->out;
6088 result_if_matched = (t->state->c == NFA_START_COLL);
6089 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006090 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006091 if (state->c == NFA_END_COLL)
6092 {
6093 result = !result_if_matched;
6094 break;
6095 }
6096 if (state->c == NFA_RANGE_MIN)
6097 {
6098 c1 = state->val;
6099 state = state->out; /* advance to NFA_RANGE_MAX */
6100 c2 = state->val;
6101#ifdef ENABLE_LOG
6102 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6103 curc, c1, c2);
6104#endif
6105 if (curc >= c1 && curc <= c2)
6106 {
6107 result = result_if_matched;
6108 break;
6109 }
6110 if (ireg_ic)
6111 {
6112 int curc_low = MB_TOLOWER(curc);
6113 int done = FALSE;
6114
6115 for ( ; c1 <= c2; ++c1)
6116 if (MB_TOLOWER(c1) == curc_low)
6117 {
6118 result = result_if_matched;
6119 done = TRUE;
6120 break;
6121 }
6122 if (done)
6123 break;
6124 }
6125 }
6126 else if (state->c < 0 ? check_char_class(state->c, curc)
6127 : (curc == state->c
6128 || (ireg_ic && MB_TOLOWER(curc)
6129 == MB_TOLOWER(state->c))))
6130 {
6131 result = result_if_matched;
6132 break;
6133 }
6134 state = state->out;
6135 }
6136 if (result)
6137 {
6138 /* next state is in out of the NFA_END_COLL, out1 of
6139 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006140 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006141 add_off = clen;
6142 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006143 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006144 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006145
6146 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006147 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006148 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006149 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006150 add_state = t->state->out;
6151 add_off = clen;
6152 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006153 break;
6154
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006155 case NFA_ANY_COMPOSING:
6156 /* On a composing character skip over it. Otherwise do
6157 * nothing. Always matches. */
6158#ifdef FEAT_MBYTE
6159 if (enc_utf8 && utf_iscomposing(curc))
6160 {
6161 add_off = clen;
6162 }
6163 else
6164#endif
6165 {
6166 add_here = TRUE;
6167 add_off = 0;
6168 }
6169 add_state = t->state->out;
6170 break;
6171
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006172 /*
6173 * Character classes like \a for alpha, \d for digit etc.
6174 */
6175 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006176 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006177 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006178 break;
6179
6180 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006181 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006182 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006183 break;
6184
6185 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006186 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006187 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006188 break;
6189
6190 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006191 result = !VIM_ISDIGIT(curc)
6192 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006193 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006194 break;
6195
6196 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006197 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006198 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006199 break;
6200
6201 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006202 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006203 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006204 break;
6205
6206 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006207 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006208 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006209 break;
6210
6211 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006212 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006213 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006214 break;
6215
6216 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006217 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006218 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006219 break;
6220
6221 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006222 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006223 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006224 break;
6225
6226 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006227 result = ri_digit(curc);
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_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006232 result = curc != NUL && !ri_digit(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_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006237 result = ri_hex(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_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006242 result = curc != NUL && !ri_hex(curc);
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_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006247 result = ri_octal(curc);
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_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006252 result = curc != NUL && !ri_octal(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_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006257 result = ri_word(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_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006262 result = curc != NUL && !ri_word(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_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006267 result = ri_head(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_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006272 result = curc != NUL && !ri_head(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_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006277 result = ri_alpha(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_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006282 result = curc != NUL && !ri_alpha(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_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006287 result = ri_lower(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_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006292 result = curc != NUL && !ri_lower(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_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006297 result = ri_upper(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_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006302 result = curc != NUL && !ri_upper(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
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006306 case NFA_LOWER_IC: /* [a-z] */
6307 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6308 ADD_STATE_IF_MATCH(t->state);
6309 break;
6310
6311 case NFA_NLOWER_IC: /* [^a-z] */
6312 result = curc != NUL
6313 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6314 ADD_STATE_IF_MATCH(t->state);
6315 break;
6316
6317 case NFA_UPPER_IC: /* [A-Z] */
6318 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6319 ADD_STATE_IF_MATCH(t->state);
6320 break;
6321
6322 case NFA_NUPPER_IC: /* ^[A-Z] */
6323 result = curc != NUL
6324 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6325 ADD_STATE_IF_MATCH(t->state);
6326 break;
6327
Bram Moolenaar5714b802013-05-28 22:03:20 +02006328 case NFA_BACKREF1:
6329 case NFA_BACKREF2:
6330 case NFA_BACKREF3:
6331 case NFA_BACKREF4:
6332 case NFA_BACKREF5:
6333 case NFA_BACKREF6:
6334 case NFA_BACKREF7:
6335 case NFA_BACKREF8:
6336 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006337#ifdef FEAT_SYN_HL
6338 case NFA_ZREF1:
6339 case NFA_ZREF2:
6340 case NFA_ZREF3:
6341 case NFA_ZREF4:
6342 case NFA_ZREF5:
6343 case NFA_ZREF6:
6344 case NFA_ZREF7:
6345 case NFA_ZREF8:
6346 case NFA_ZREF9:
6347#endif
6348 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006349 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006350 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006351 int bytelen;
6352
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006353 if (t->state->c <= NFA_BACKREF9)
6354 {
6355 subidx = t->state->c - NFA_BACKREF1 + 1;
6356 result = match_backref(&t->subs.norm, subidx, &bytelen);
6357 }
6358#ifdef FEAT_SYN_HL
6359 else
6360 {
6361 subidx = t->state->c - NFA_ZREF1 + 1;
6362 result = match_zref(subidx, &bytelen);
6363 }
6364#endif
6365
Bram Moolenaar5714b802013-05-28 22:03:20 +02006366 if (result)
6367 {
6368 if (bytelen == 0)
6369 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006370 /* empty match always works, output of NFA_SKIP to be
6371 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006372 add_here = TRUE;
6373 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006374 }
6375 else if (bytelen <= clen)
6376 {
6377 /* match current character, jump ahead to out of
6378 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006379 add_state = t->state->out->out;
6380 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006381 }
6382 else
6383 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006384 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006385 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006386 add_state = t->state->out;
6387 add_off = bytelen;
6388 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006389 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006390 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006391 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006392 }
6393 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006394 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006395 if (t->count - clen <= 0)
6396 {
6397 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006398 add_state = t->state->out;
6399 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006400 }
6401 else
6402 {
6403 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006404 add_state = t->state;
6405 add_off = 0;
6406 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006407 }
6408 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006409
Bram Moolenaar423532e2013-05-29 21:14:42 +02006410 case NFA_LNUM:
6411 case NFA_LNUM_GT:
6412 case NFA_LNUM_LT:
6413 result = (REG_MULTI &&
6414 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6415 (long_u)(reglnum + reg_firstlnum)));
6416 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006417 {
6418 add_here = TRUE;
6419 add_state = t->state->out;
6420 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006421 break;
6422
6423 case NFA_COL:
6424 case NFA_COL_GT:
6425 case NFA_COL_LT:
6426 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6427 (long_u)(reginput - regline) + 1);
6428 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006429 {
6430 add_here = TRUE;
6431 add_state = t->state->out;
6432 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006433 break;
6434
6435 case NFA_VCOL:
6436 case NFA_VCOL_GT:
6437 case NFA_VCOL_LT:
6438 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
6439 (long_u)win_linetabsize(
6440 reg_win == NULL ? curwin : reg_win,
6441 regline, (colnr_T)(reginput - regline)) + 1);
6442 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006443 {
6444 add_here = TRUE;
6445 add_state = t->state->out;
6446 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006447 break;
6448
Bram Moolenaar044aa292013-06-04 21:27:38 +02006449 case NFA_MARK:
6450 case NFA_MARK_GT:
6451 case NFA_MARK_LT:
6452 {
6453 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6454
6455 /* Compare the mark position to the match position. */
6456 result = (pos != NULL /* mark doesn't exist */
6457 && pos->lnum > 0 /* mark isn't set in reg_buf */
6458 && (pos->lnum == reglnum + reg_firstlnum
6459 ? (pos->col == (colnr_T)(reginput - regline)
6460 ? t->state->c == NFA_MARK
6461 : (pos->col < (colnr_T)(reginput - regline)
6462 ? t->state->c == NFA_MARK_GT
6463 : t->state->c == NFA_MARK_LT))
6464 : (pos->lnum < reglnum + reg_firstlnum
6465 ? t->state->c == NFA_MARK_GT
6466 : t->state->c == NFA_MARK_LT)));
6467 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006468 {
6469 add_here = TRUE;
6470 add_state = t->state->out;
6471 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006472 break;
6473 }
6474
Bram Moolenaar423532e2013-05-29 21:14:42 +02006475 case NFA_CURSOR:
6476 result = (reg_win != NULL
6477 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6478 && ((colnr_T)(reginput - regline)
6479 == reg_win->w_cursor.col));
6480 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006481 {
6482 add_here = TRUE;
6483 add_state = t->state->out;
6484 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006485 break;
6486
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006487 case NFA_VISUAL:
6488 result = reg_match_visual();
6489 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006490 {
6491 add_here = TRUE;
6492 add_state = t->state->out;
6493 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006494 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006495
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006496 case NFA_MOPEN1:
6497 case NFA_MOPEN2:
6498 case NFA_MOPEN3:
6499 case NFA_MOPEN4:
6500 case NFA_MOPEN5:
6501 case NFA_MOPEN6:
6502 case NFA_MOPEN7:
6503 case NFA_MOPEN8:
6504 case NFA_MOPEN9:
6505#ifdef FEAT_SYN_HL
6506 case NFA_ZOPEN:
6507 case NFA_ZOPEN1:
6508 case NFA_ZOPEN2:
6509 case NFA_ZOPEN3:
6510 case NFA_ZOPEN4:
6511 case NFA_ZOPEN5:
6512 case NFA_ZOPEN6:
6513 case NFA_ZOPEN7:
6514 case NFA_ZOPEN8:
6515 case NFA_ZOPEN9:
6516#endif
6517 case NFA_NOPEN:
6518 case NFA_ZSTART:
6519 /* These states are only added to be able to bail out when
6520 * they are added again, nothing is to be done. */
6521 break;
6522
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006523 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006524 {
6525 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006526
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006527#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006528 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006529 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006530#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006531 result = (c == curc);
6532
6533 if (!result && ireg_ic)
6534 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006535#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006536 /* If ireg_icombine is not set only skip over the character
6537 * itself. When it is set skip over composing characters. */
6538 if (result && enc_utf8 && !ireg_icombine)
6539 clen = utf_char2len(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006540#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006541 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006542 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006543 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006544
6545 } /* switch (t->state->c) */
6546
6547 if (add_state != NULL)
6548 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006549 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006550 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006551
6552 if (t->pim.result == NFA_PIM_UNUSED)
6553 pim = NULL;
6554 else
6555 pim = &t->pim;
6556
6557 /* Handle the postponed invisible match if the match might end
6558 * without advancing and before the end of the line. */
6559 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006560 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006561 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006562 {
6563#ifdef ENABLE_LOG
6564 fprintf(log_fd, "\n");
6565 fprintf(log_fd, "==================================\n");
6566 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6567 fprintf(log_fd, "\n");
6568#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006569 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006570 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006571 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006572 /* for \@! and \@<! it is a match when the result is
6573 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006574 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006575 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6576 || pim->state->c
6577 == NFA_START_INVISIBLE_BEFORE_NEG
6578 || pim->state->c
6579 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006580 {
6581 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006582 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006583#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006584 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006585 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006586#endif
6587 }
6588 }
6589 else
6590 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006591 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006592#ifdef ENABLE_LOG
6593 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006594 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006595 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6596 fprintf(log_fd, "\n");
6597#endif
6598 }
6599
Bram Moolenaardecd9542013-06-07 16:31:50 +02006600 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006601 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006602 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6603 || pim->state->c
6604 == NFA_START_INVISIBLE_BEFORE_NEG
6605 || pim->state->c
6606 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006607 {
6608 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006609 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006610#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006611 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006612 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006613#endif
6614 }
6615 else
6616 /* look-behind match failed, don't add the state */
6617 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006618
6619 /* Postponed invisible match was handled, don't add it to
6620 * following states. */
6621 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006622 }
6623
Bram Moolenaara951e352013-10-06 15:46:11 +02006624 /* If "pim" points into l->t it will become invalid when
6625 * adding the state causes the list to be reallocated. Make a
6626 * local copy to avoid that. */
6627 if (pim == &t->pim)
6628 {
6629 copy_pim(&pim_copy, pim);
6630 pim = &pim_copy;
6631 }
6632
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006633 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006634 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006635 else
6636 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006637 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006638 if (add_count > 0)
6639 nextlist->t[nextlist->n - 1].count = add_count;
6640 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006641 }
6642
6643 } /* for (thislist = thislist; thislist->state; thislist++) */
6644
Bram Moolenaare23febd2013-05-26 18:40:14 +02006645 /* Look for the start of a match in the current position by adding the
6646 * start state to the list of states.
6647 * The first found match is the leftmost one, thus the order of states
6648 * matters!
6649 * Do not add the start state in recursive calls of nfa_regmatch(),
6650 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006651 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006652 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006653 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006654 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006655 && reglnum == 0
6656 && clen != 0
6657 && (ireg_maxcol == 0
6658 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006659 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006660 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006661 ? (reglnum < nfa_endp->se_u.pos.lnum
6662 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006663 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006664 < nfa_endp->se_u.pos.col))
6665 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006666 {
6667#ifdef ENABLE_LOG
6668 fprintf(log_fd, "(---) STARTSTATE\n");
6669#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006670 /* Inline optimized code for addstate() if we know the state is
6671 * the first MOPEN. */
6672 if (toplevel)
6673 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006674 int add = TRUE;
6675 int c;
6676
6677 if (prog->regstart != NUL && clen != 0)
6678 {
6679 if (nextlist->n == 0)
6680 {
6681 colnr_T col = (colnr_T)(reginput - regline) + clen;
6682
6683 /* Nextlist is empty, we can skip ahead to the
6684 * character that must appear at the start. */
6685 if (skip_to_start(prog->regstart, &col) == FAIL)
6686 break;
6687#ifdef ENABLE_LOG
6688 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6689 col - ((colnr_T)(reginput - regline) + clen));
6690#endif
6691 reginput = regline + col - clen;
6692 }
6693 else
6694 {
6695 /* Checking if the required start character matches is
6696 * cheaper than adding a state that won't match. */
6697 c = PTR2CHAR(reginput + clen);
6698 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6699 != MB_TOLOWER(prog->regstart)))
6700 {
6701#ifdef ENABLE_LOG
6702 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6703#endif
6704 add = FALSE;
6705 }
6706 }
6707 }
6708
6709 if (add)
6710 {
6711 if (REG_MULTI)
6712 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006713 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006714 else
6715 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006716 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006717 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006718 }
6719 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006720 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006721 }
6722
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006723#ifdef ENABLE_LOG
6724 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006725 {
6726 int i;
6727
6728 for (i = 0; i < thislist->n; i++)
6729 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6730 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006731 fprintf(log_fd, "\n");
6732#endif
6733
6734nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006735 /* Advance to the next character, or advance to the next line, or
6736 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006737 if (clen != 0)
6738 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006739 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6740 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006741 reg_nextline();
6742 else
6743 break;
6744 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006745
6746#ifdef ENABLE_LOG
6747 if (log_fd != stderr)
6748 fclose(log_fd);
6749 log_fd = NULL;
6750#endif
6751
6752theend:
6753 /* Free memory */
6754 vim_free(list[0].t);
6755 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006756 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006757#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006758#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006759 fclose(debug);
6760#endif
6761
Bram Moolenaar963fee22013-05-26 21:47:28 +02006762 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006763}
6764
6765/*
6766 * Try match of "prog" with at regline["col"].
6767 * Returns 0 for failure, number of lines contained in the match otherwise.
6768 */
6769 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006770nfa_regtry(prog, col)
6771 nfa_regprog_T *prog;
6772 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006773{
6774 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006775 regsubs_T subs, m;
6776 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006777#ifdef ENABLE_LOG
6778 FILE *f;
6779#endif
6780
6781 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006782
6783#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006784 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006785 if (f != NULL)
6786 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006787 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006788#ifdef DEBUG
6789 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6790#endif
6791 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006792 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006793 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006794 fprintf(f, "\n\n");
6795 fclose(f);
6796 }
6797 else
6798 EMSG(_("Could not open temporary log file for writing "));
6799#endif
6800
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006801 clear_sub(&subs.norm);
6802 clear_sub(&m.norm);
6803#ifdef FEAT_SYN_HL
6804 clear_sub(&subs.synt);
6805 clear_sub(&m.synt);
6806#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006807
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006808 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006809 return 0;
6810
6811 cleanup_subexpr();
6812 if (REG_MULTI)
6813 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006814 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006815 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006816 reg_startpos[i] = subs.norm.list.multi[i].start;
6817 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006818 }
6819
6820 if (reg_startpos[0].lnum < 0)
6821 {
6822 reg_startpos[0].lnum = 0;
6823 reg_startpos[0].col = col;
6824 }
6825 if (reg_endpos[0].lnum < 0)
6826 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006827 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006828 reg_endpos[0].lnum = reglnum;
6829 reg_endpos[0].col = (int)(reginput - regline);
6830 }
6831 else
6832 /* Use line number of "\ze". */
6833 reglnum = reg_endpos[0].lnum;
6834 }
6835 else
6836 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006837 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006838 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006839 reg_startp[i] = subs.norm.list.line[i].start;
6840 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006841 }
6842
6843 if (reg_startp[0] == NULL)
6844 reg_startp[0] = regline + col;
6845 if (reg_endp[0] == NULL)
6846 reg_endp[0] = reginput;
6847 }
6848
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006849#ifdef FEAT_SYN_HL
6850 /* Package any found \z(...\) matches for export. Default is none. */
6851 unref_extmatch(re_extmatch_out);
6852 re_extmatch_out = NULL;
6853
6854 if (prog->reghasz == REX_SET)
6855 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006856 cleanup_zsubexpr();
6857 re_extmatch_out = make_extmatch();
6858 for (i = 0; i < subs.synt.in_use; i++)
6859 {
6860 if (REG_MULTI)
6861 {
6862 struct multipos *mpos = &subs.synt.list.multi[i];
6863
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006864 /* Only accept single line matches that are valid. */
6865 if (mpos->start.lnum >= 0
6866 && mpos->start.lnum == mpos->end.lnum
6867 && mpos->end.col >= mpos->start.col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006868 re_extmatch_out->matches[i] =
6869 vim_strnsave(reg_getline(mpos->start.lnum)
6870 + mpos->start.col,
6871 mpos->end.col - mpos->start.col);
6872 }
6873 else
6874 {
6875 struct linepos *lpos = &subs.synt.list.line[i];
6876
6877 if (lpos->start != NULL && lpos->end != NULL)
6878 re_extmatch_out->matches[i] =
6879 vim_strnsave(lpos->start,
6880 (int)(lpos->end - lpos->start));
6881 }
6882 }
6883 }
6884#endif
6885
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006886 return 1 + reglnum;
6887}
6888
6889/*
6890 * Match a regexp against a string ("line" points to the string) or multiple
6891 * lines ("line" is NULL, use reg_getline()).
6892 *
6893 * Returns 0 for failure, number of lines contained in the match otherwise.
6894 */
6895 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006896nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006897 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006898 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006899{
6900 nfa_regprog_T *prog;
6901 long retval = 0L;
6902 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006903 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006904
6905 if (REG_MULTI)
6906 {
6907 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6908 line = reg_getline((linenr_T)0); /* relative to the cursor */
6909 reg_startpos = reg_mmatch->startpos;
6910 reg_endpos = reg_mmatch->endpos;
6911 }
6912 else
6913 {
6914 prog = (nfa_regprog_T *)reg_match->regprog;
6915 reg_startp = reg_match->startp;
6916 reg_endp = reg_match->endp;
6917 }
6918
6919 /* Be paranoid... */
6920 if (prog == NULL || line == NULL)
6921 {
6922 EMSG(_(e_null));
6923 goto theend;
6924 }
6925
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006926 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6927 if (prog->regflags & RF_ICASE)
6928 ireg_ic = TRUE;
6929 else if (prog->regflags & RF_NOICASE)
6930 ireg_ic = FALSE;
6931
6932#ifdef FEAT_MBYTE
6933 /* If pattern contains "\Z" overrule value of ireg_icombine */
6934 if (prog->regflags & RF_ICOMBINE)
6935 ireg_icombine = TRUE;
6936#endif
6937
6938 regline = line;
6939 reglnum = 0; /* relative to line */
6940
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006941 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006942 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006943 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006944 nfa_listid = 1;
6945 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006946#ifdef DEBUG
6947 nfa_regengine.expr = prog->pattern;
6948#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006949
Bram Moolenaard89616e2013-06-06 18:46:06 +02006950 if (prog->reganch && col > 0)
6951 return 0L;
6952
Bram Moolenaar473de612013-06-08 18:19:48 +02006953 need_clear_subexpr = TRUE;
6954#ifdef FEAT_SYN_HL
6955 /* Clear the external match subpointers if necessary. */
6956 if (prog->reghasz == REX_SET)
6957 {
6958 nfa_has_zsubexpr = TRUE;
6959 need_clear_zsubexpr = TRUE;
6960 }
6961 else
6962 nfa_has_zsubexpr = FALSE;
6963#endif
6964
Bram Moolenaard89616e2013-06-06 18:46:06 +02006965 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006966 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006967 /* Skip ahead until a character we know the match must start with.
6968 * When there is none there is no match. */
6969 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006970 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006971
Bram Moolenaar473de612013-06-08 18:19:48 +02006972 /* If match_text is set it contains the full text that must match.
6973 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006974 if (prog->match_text != NULL
6975#ifdef FEAT_MBYTE
6976 && !ireg_icombine
6977#endif
6978 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006979 return find_match_text(col, prog->regstart, prog->match_text);
6980 }
6981
Bram Moolenaard89616e2013-06-06 18:46:06 +02006982 /* If the start column is past the maximum column: no need to try. */
6983 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6984 goto theend;
6985
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006986 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006987 for (i = 0; i < nstate; ++i)
6988 {
6989 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006990 prog->state[i].lastlist[0] = 0;
6991 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992 }
6993
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006994 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006995
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006996#ifdef DEBUG
6997 nfa_regengine.expr = NULL;
6998#endif
6999
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000theend:
7001 return retval;
7002}
7003
7004/*
7005 * Compile a regular expression into internal code for the NFA matcher.
7006 * Returns the program in allocated space. Returns NULL for an error.
7007 */
7008 static regprog_T *
7009nfa_regcomp(expr, re_flags)
7010 char_u *expr;
7011 int re_flags;
7012{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007013 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007014 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007015 int *postfix;
7016
7017 if (expr == NULL)
7018 return NULL;
7019
7020#ifdef DEBUG
7021 nfa_regengine.expr = expr;
7022#endif
7023
7024 init_class_tab();
7025
7026 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7027 return NULL;
7028
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007029 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007030 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007031 postfix = re2post();
7032 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007033 {
7034 /* TODO: only give this error for debugging? */
7035 if (post_ptr >= post_end)
7036 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007037 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007038 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007039
7040 /*
7041 * In order to build the NFA, we parse the input regexp twice:
7042 * 1. first pass to count size (so we can allocate space)
7043 * 2. second to emit code
7044 */
7045#ifdef ENABLE_LOG
7046 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007047 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007048
7049 if (f != NULL)
7050 {
7051 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7052 fclose(f);
7053 }
7054 }
7055#endif
7056
7057 /*
7058 * PASS 1
7059 * Count number of NFA states in "nstate". Do not build the NFA.
7060 */
7061 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007062
Bram Moolenaar16619a22013-06-11 18:42:36 +02007063 /* allocate the regprog with space for the compiled regexp */
7064 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007065 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7066 if (prog == NULL)
7067 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007068 state_ptr = prog->state;
7069
7070 /*
7071 * PASS 2
7072 * Build the NFA
7073 */
7074 prog->start = post2nfa(postfix, post_ptr, FALSE);
7075 if (prog->start == NULL)
7076 goto fail;
7077
7078 prog->regflags = regflags;
7079 prog->engine = &nfa_regengine;
7080 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007081 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007082 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007083 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007084
Bram Moolenaara2947e22013-06-11 22:44:09 +02007085 nfa_postprocess(prog);
7086
Bram Moolenaard89616e2013-06-06 18:46:06 +02007087 prog->reganch = nfa_get_reganch(prog->start, 0);
7088 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007089 prog->match_text = nfa_get_match_text(prog->start);
7090
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007091#ifdef ENABLE_LOG
7092 nfa_postfix_dump(expr, OK);
7093 nfa_dump(prog);
7094#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007095#ifdef FEAT_SYN_HL
7096 /* Remember whether this pattern has any \z specials in it. */
7097 prog->reghasz = re_has_z;
7098#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007099#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02007100 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007101 nfa_regengine.expr = NULL;
7102#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007103
7104out:
7105 vim_free(post_start);
7106 post_start = post_ptr = post_end = NULL;
7107 state_ptr = NULL;
7108 return (regprog_T *)prog;
7109
7110fail:
7111 vim_free(prog);
7112 prog = NULL;
7113#ifdef ENABLE_LOG
7114 nfa_postfix_dump(expr, FAIL);
7115#endif
7116#ifdef DEBUG
7117 nfa_regengine.expr = NULL;
7118#endif
7119 goto out;
7120}
7121
Bram Moolenaar473de612013-06-08 18:19:48 +02007122/*
7123 * Free a compiled regexp program, returned by nfa_regcomp().
7124 */
7125 static void
7126nfa_regfree(prog)
7127 regprog_T *prog;
7128{
7129 if (prog != NULL)
7130 {
7131 vim_free(((nfa_regprog_T *)prog)->match_text);
7132#ifdef DEBUG
7133 vim_free(((nfa_regprog_T *)prog)->pattern);
7134#endif
7135 vim_free(prog);
7136 }
7137}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007138
7139/*
7140 * Match a regexp against a string.
7141 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7142 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007143 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007144 *
7145 * Return TRUE if there is a match, FALSE if not.
7146 */
7147 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007148nfa_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007149 regmatch_T *rmp;
7150 char_u *line; /* string to match against */
7151 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007152 int line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007153{
7154 reg_match = rmp;
7155 reg_mmatch = NULL;
7156 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007157 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007158 reg_buf = curbuf;
7159 reg_win = NULL;
7160 ireg_ic = rmp->rm_ic;
7161#ifdef FEAT_MBYTE
7162 ireg_icombine = FALSE;
7163#endif
7164 ireg_maxcol = 0;
7165 return (nfa_regexec_both(line, col) != 0);
7166}
7167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168
7169/*
7170 * Match a regexp against multiple lines.
7171 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7172 * Uses curbuf for line count and 'iskeyword'.
7173 *
7174 * Return zero if there is no match. Return number of lines contained in the
7175 * match otherwise.
7176 *
7177 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7178 *
7179 * ! Also NOTE : match may actually be in another line. e.g.:
7180 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7181 *
7182 * +-------------------------+
7183 * |a |
7184 * |b |
7185 * |c |
7186 * | |
7187 * +-------------------------+
7188 *
7189 * then nfa_regexec_multi() returns 3. while the original
7190 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7191 *
7192 * FIXME if this behavior is not compatible.
7193 */
7194 static long
7195nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7196 regmmatch_T *rmp;
7197 win_T *win; /* window in which to search or NULL */
7198 buf_T *buf; /* buffer in which to search */
7199 linenr_T lnum; /* nr of line to start looking for match */
7200 colnr_T col; /* column to start looking for match */
7201 proftime_T *tm UNUSED; /* timeout limit or NULL */
7202{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007203 reg_match = NULL;
7204 reg_mmatch = rmp;
7205 reg_buf = buf;
7206 reg_win = win;
7207 reg_firstlnum = lnum;
7208 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7209 reg_line_lbr = FALSE;
7210 ireg_ic = rmp->rmm_ic;
7211#ifdef FEAT_MBYTE
7212 ireg_icombine = FALSE;
7213#endif
7214 ireg_maxcol = rmp->rmm_maxcol;
7215
Bram Moolenaarf878bf02013-05-21 21:20:20 +02007216 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007217}
7218
7219#ifdef DEBUG
7220# undef ENABLE_LOG
7221#endif